2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @Apple_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
24 #include <machine/spl.h>
26 #include <sys/errno.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/proc_internal.h>
31 #include <sys/sysctl.h>
32 #include <sys/kdebug.h>
33 #include <sys/sysproto.h>
34 #include <sys/bsdtask_info.h>
35 #include <sys/random.h>
36 #include <sys/stackshot.h>
39 #include <mach/clock_types.h>
40 #include <mach/mach_types.h>
41 #include <mach/mach_time.h>
42 #include <machine/machine_routines.h>
44 #if defined(__i386__) || defined(__x86_64__)
45 #include <i386/rtclock_protos.h>
47 #include <i386/machine_routines.h>
50 #include <kern/clock.h>
52 #include <kern/thread.h>
53 #include <kern/task.h>
54 #include <kern/debug.h>
55 #include <kern/kalloc.h>
56 #include <kern/cpu_data.h>
57 #include <kern/assert.h>
58 #include <kern/telemetry.h>
59 #include <kern/sched_prim.h>
60 #include <vm/vm_kern.h>
63 #include <sys/malloc.h>
64 #include <sys/mcache.h>
65 #include <sys/kauth.h>
67 #include <sys/vnode.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/fcntl.h>
70 #include <sys/file_internal.h>
72 #include <sys/param.h> /* for isset() */
74 #include <mach/mach_host.h> /* for host_info() */
75 #include <libkern/OSAtomic.h>
77 #include <machine/pal_routines.h>
79 extern boolean_t kdebug_serial
;
81 #include <sys/kdebugevents.h>
82 static void kdebug_serial_print( /* forward */
83 uint32_t, uint32_t, uint64_t,
84 uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
90 * https://coreoswiki.apple.com/wiki/pages/U6z3i0q9/Consistent_Logging_Implementers_Guide.html
92 * IOP(s) are auxiliary cores that want to participate in kdebug event logging.
93 * They are registered dynamically. Each is assigned a cpu_id at registration.
95 * NOTE: IOP trace events may not use the same clock hardware as "normal"
96 * cpus. There is an effort made to synchronize the IOP timebase with the
97 * AP, but it should be understood that there may be discrepancies.
99 * Once registered, an IOP is permanent, it cannot be unloaded/unregistered.
100 * The current implementation depends on this for thread safety.
102 * New registrations occur by allocating an kd_iop struct and assigning
103 * a provisional cpu_id of list_head->cpu_id + 1. Then a CAS to claim the
104 * list_head pointer resolves any races.
106 * You may safely walk the kd_iops list at any time, without holding locks.
108 * When allocating buffers, the current kd_iops head is captured. Any operations
109 * that depend on the buffer state (such as flushing IOP traces on reads,
110 * etc.) should use the captured list head. This will allow registrations to
111 * take place while trace is in use.
114 typedef struct kd_iop
{
115 kd_callback_t callback
;
117 uint64_t last_timestamp
; /* Prevent timer rollback */
121 static kd_iop_t
* kd_iops
= NULL
;
123 /* XXX should have prototypes, but Mach does not provide one */
124 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
125 int cpu_number(void); /* XXX <machine/...> include path broken */
126 void commpage_update_kdebug_enable(void); /* XXX sign */
128 /* XXX should probably be static, but it's debugging code... */
129 int kdbg_read(user_addr_t
, size_t *, vnode_t
, vfs_context_t
, uint32_t);
130 void kdbg_control_chud(int, void *);
131 int kdbg_control(int *, u_int
, user_addr_t
, size_t *);
132 int kdbg_readcpumap(user_addr_t
, size_t *);
133 int kdbg_readcurcpumap(user_addr_t
, size_t *);
134 int kdbg_readthrmap(user_addr_t
, size_t *, vnode_t
, vfs_context_t
);
135 int kdbg_readthrmap_v3(user_addr_t
, size_t *, int);
136 int kdbg_readcurthrmap(user_addr_t
, size_t *);
137 int kdbg_setreg(kd_regtype
*);
138 int kdbg_setrtcdec(kd_regtype
*);
139 int kdbg_setpidex(kd_regtype
*);
140 int kdbg_setpid(kd_regtype
*);
141 void kdbg_thrmap_init(void);
142 int kdbg_reinit(boolean_t
);
143 int kdbg_bootstrap(boolean_t
);
145 int kdbg_cpumap_init_internal(kd_iop_t
* iops
, uint32_t cpu_count
,
146 uint8_t** cpumap
, uint32_t* cpumap_size
);
148 kd_threadmap
* kdbg_thrmap_init_internal(unsigned int count
,
149 unsigned int *mapsize
,
150 unsigned int *mapcount
);
152 static boolean_t
kdebug_current_proc_enabled(uint32_t debugid
);
153 static boolean_t
kdebug_debugid_enabled(uint32_t debugid
);
154 static errno_t
kdebug_check_trace_string(uint32_t debugid
, uint64_t str_id
);
156 int kdbg_write_v3_header(user_addr_t
, size_t *, int);
157 int kdbg_write_v3_chunk_header(user_addr_t buffer
, uint32_t tag
,
158 uint32_t sub_tag
, uint64_t length
,
159 vnode_t vp
, vfs_context_t ctx
);
161 user_addr_t
kdbg_write_v3_event_chunk_header(user_addr_t buffer
, uint32_t tag
,
162 uint64_t length
, vnode_t vp
,
165 static int kdbg_enable_typefilter(void);
166 static int kdbg_disable_typefilter(void);
167 static int kdbg_allocate_typefilter(void);
168 static int kdbg_deallocate_typefilter(void);
170 static int create_buffers(boolean_t
);
171 static void delete_buffers(void);
173 extern void IOSleep(int);
175 /* trace enable status */
176 unsigned int kdebug_enable
= 0;
178 /* A static buffer to record events prior to the start of regular logging */
179 #define KD_EARLY_BUFFER_MAX 64
180 static kd_buf kd_early_buffer
[KD_EARLY_BUFFER_MAX
];
181 static int kd_early_index
= 0;
182 static boolean_t kd_early_overflow
= FALSE
;
184 #define SLOW_NOLOG 0x01
185 #define SLOW_CHECKS 0x02
186 #define SLOW_CHUD 0x08
188 #define EVENTS_PER_STORAGE_UNIT 2048
189 #define MIN_STORAGE_UNITS_PER_CPU 4
191 #define POINTER_FROM_KDS_PTR(x) (&kd_bufs[x.buffer_index].kdsb_addr[x.offset])
195 uint32_t buffer_index
:21;
202 union kds_ptr kds_next
;
203 uint32_t kds_bufindx
;
205 uint32_t kds_readlast
;
206 boolean_t kds_lostevents
;
207 uint64_t kds_timestamp
;
209 kd_buf kds_records
[EVENTS_PER_STORAGE_UNIT
];
212 #define MAX_BUFFER_SIZE (1024 * 1024 * 128)
213 #define N_STORAGE_UNITS_PER_BUFFER (MAX_BUFFER_SIZE / sizeof(struct kd_storage))
215 struct kd_storage_buffers
{
216 struct kd_storage
*kdsb_addr
;
220 #define KDS_PTR_NULL 0xffffffff
221 struct kd_storage_buffers
*kd_bufs
= NULL
;
222 int n_storage_units
= 0;
223 int n_storage_buffers
= 0;
224 int n_storage_threshold
= 0;
229 union kds_ptr kd_list_head
;
230 union kds_ptr kd_list_tail
;
231 boolean_t kd_lostevents
;
233 uint64_t kd_prev_timebase
;
235 } __attribute__(( aligned(MAX_CPU_CACHE_LINE_SIZE
) ));
239 * In principle, this control block can be shared in DRAM with other
240 * coprocessors and runtimes, for configuring what tracing is enabled.
242 struct kd_ctrl_page_t
{
243 union kds_ptr kds_free_list
;
247 uint32_t kdebug_flags
;
248 uint32_t kdebug_slowcheck
;
250 * The number of kd_bufinfo structs allocated may not match the current
251 * number of active cpus. We capture the iops list head at initialization
252 * which we could use to calculate the number of cpus we allocated data for,
253 * unless it happens to be null. To avoid that case, we explicitly also
254 * capture a cpu count.
256 kd_iop_t
* kdebug_iops
;
257 uint32_t kdebug_cpus
;
258 } kd_ctrl_page
= { .kds_free_list
= {.raw
= KDS_PTR_NULL
}, .kdebug_slowcheck
= SLOW_NOLOG
};
262 struct kd_bufinfo
*kdbip
= NULL
;
264 #define KDCOPYBUF_COUNT 8192
265 #define KDCOPYBUF_SIZE (KDCOPYBUF_COUNT * sizeof(kd_buf))
267 #define PAGE_4KB 4096
268 #define PAGE_16KB 16384
270 kd_buf
*kdcopybuf
= NULL
;
272 boolean_t kdlog_bg_trace
= FALSE
;
273 boolean_t kdlog_bg_trace_running
= FALSE
;
274 unsigned int bg_nkdbufs
= 0;
276 unsigned int nkdbufs
= 0;
277 unsigned int kdlog_beg
=0;
278 unsigned int kdlog_end
=0;
279 unsigned int kdlog_value1
=0;
280 unsigned int kdlog_value2
=0;
281 unsigned int kdlog_value3
=0;
282 unsigned int kdlog_value4
=0;
284 static lck_spin_t
* kdw_spin_lock
;
285 static lck_spin_t
* kds_spin_lock
;
286 static lck_mtx_t
* kd_trace_mtx_sysctl
;
287 static lck_grp_t
* kd_trace_mtx_sysctl_grp
;
288 static lck_attr_t
* kd_trace_mtx_sysctl_attr
;
289 static lck_grp_attr_t
*kd_trace_mtx_sysctl_grp_attr
;
291 extern kern_return_t
stack_snapshot2(int pid
, user_addr_t tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, int32_t *retval
);
294 extern kern_return_t
stack_microstackshot(user_addr_t tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, int32_t *retval
);
295 #endif /* CONFIG_TELEMETRY */
297 extern kern_return_t
kern_stack_snapshot_with_reason(char* reason
);
299 extern kern_return_t
kern_stack_snapshot_internal(int stackshot_config_version
, void *stackshot_config
, size_t stackshot_config_size
, boolean_t stackshot_from_user
);
301 extern kern_return_t
stack_snapshot_from_kernel_internal(int pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *bytes_traced
);
303 int stack_snapshot_from_kernel(pid_t pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *bytes_traced
);
305 kd_threadmap
*kd_mapptr
= 0;
306 unsigned int kd_mapsize
= 0;
307 unsigned int kd_mapcount
= 0;
309 off_t RAW_file_offset
= 0;
310 int RAW_file_written
= 0;
312 #define RAW_FLUSH_SIZE (2 * 1024 * 1024)
314 pid_t global_state_pid
= -1; /* Used to control exclusive use of kd_buffer */
317 * A globally increasing counter for identifying strings in trace. Starts at
318 * 1 because 0 is a reserved return value.
320 __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE
)))
321 static uint64_t g_curr_str_id
= 1;
323 #define STR_ID_SIG_OFFSET (48)
324 #define STR_ID_MASK ((1ULL << STR_ID_SIG_OFFSET) - 1)
325 #define STR_ID_SIG_MASK (~STR_ID_MASK)
328 * A bit pattern for identifying string IDs generated by
329 * kdebug_trace_string(2).
331 static uint64_t g_str_id_signature
= (0x70acULL
<< STR_ID_SIG_OFFSET
);
333 #define INTERRUPT 0x01050000
334 #define MACH_vmfault 0x01300008
335 #define BSC_SysCall 0x040c0000
336 #define MACH_SysCall 0x010c0000
338 /* task to string structure */
341 task_t task
; /* from procs task */
342 pid_t pid
; /* from procs p_pid */
343 char task_comm
[20]; /* from procs p_comm */
346 typedef struct tts tts_t
;
350 kd_threadmap
*map
; /* pointer to the map buffer */
356 typedef struct krt krt_t
;
358 /* This is for the CHUD toolkit call */
359 typedef void (*kd_chudhook_fn
) (uint32_t debugid
, uintptr_t arg1
,
360 uintptr_t arg2
, uintptr_t arg3
,
361 uintptr_t arg4
, uintptr_t arg5
);
363 volatile kd_chudhook_fn kdebug_chudhook
= 0; /* pointer to CHUD toolkit function */
365 static uint8_t *type_filter_bitmap
;
368 * This allows kperf to swap out the global state pid when kperf ownership is
369 * passed from one process to another. It checks the old global state pid so
370 * that kperf can't accidentally steal control of trace when a non-kperf trace user has
374 kdbg_swap_global_state_pid(pid_t old_pid
, pid_t new_pid
);
377 kdbg_swap_global_state_pid(pid_t old_pid
, pid_t new_pid
)
379 if (!(kd_ctrl_page
.kdebug_flags
& KDBG_LOCKINIT
))
382 lck_mtx_lock(kd_trace_mtx_sysctl
);
384 if (old_pid
== global_state_pid
)
385 global_state_pid
= new_pid
;
387 lck_mtx_unlock(kd_trace_mtx_sysctl
);
391 kdbg_cpu_count(boolean_t early_trace
)
395 * we've started tracing before the IOKit has even
396 * started running... just use the static max value
401 host_basic_info_data_t hinfo
;
402 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
403 host_info((host_t
)1 /* BSD_HOST */, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
404 assert(hinfo
.logical_cpu_max
> 0);
405 return hinfo
.logical_cpu_max
;
409 #endif /* MACH_ASSERT */
412 kdbg_iop_list_callback(kd_iop_t
* iop
, kd_callback_type type
, void* arg
)
415 iop
->callback
.func(iop
->callback
.context
, type
, arg
);
421 kdbg_set_tracing_enabled(boolean_t enabled
, uint32_t trace_type
)
423 int s
= ml_set_interrupts_enabled(FALSE
);
424 lck_spin_lock(kds_spin_lock
);
426 kdebug_enable
|= trace_type
;
427 kd_ctrl_page
.kdebug_slowcheck
&= ~SLOW_NOLOG
;
428 kd_ctrl_page
.enabled
= 1;
429 commpage_update_kdebug_enable();
431 kdebug_enable
&= ~(KDEBUG_ENABLE_TRACE
|KDEBUG_ENABLE_PPT
);
432 kd_ctrl_page
.kdebug_slowcheck
|= SLOW_NOLOG
;
433 kd_ctrl_page
.enabled
= 0;
434 commpage_update_kdebug_enable();
436 lck_spin_unlock(kds_spin_lock
);
437 ml_set_interrupts_enabled(s
);
440 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_KDEBUG_ENABLED
, NULL
);
443 * If you do not flush the IOP trace buffers, they can linger
444 * for a considerable period; consider code which disables and
445 * deallocates without a final sync flush.
447 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_KDEBUG_DISABLED
, NULL
);
448 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_SYNC_FLUSH
, NULL
);
453 kdbg_set_flags(int slowflag
, int enableflag
, boolean_t enabled
)
455 int s
= ml_set_interrupts_enabled(FALSE
);
456 lck_spin_lock(kds_spin_lock
);
459 kd_ctrl_page
.kdebug_slowcheck
|= slowflag
;
460 kdebug_enable
|= enableflag
;
462 kd_ctrl_page
.kdebug_slowcheck
&= ~slowflag
;
463 kdebug_enable
&= ~enableflag
;
466 lck_spin_unlock(kds_spin_lock
);
467 ml_set_interrupts_enabled(s
);
471 disable_wrap(uint32_t *old_slowcheck
, uint32_t *old_flags
)
473 int s
= ml_set_interrupts_enabled(FALSE
);
474 lck_spin_lock(kds_spin_lock
);
476 *old_slowcheck
= kd_ctrl_page
.kdebug_slowcheck
;
477 *old_flags
= kd_ctrl_page
.kdebug_flags
;
479 kd_ctrl_page
.kdebug_flags
&= ~KDBG_WRAPPED
;
480 kd_ctrl_page
.kdebug_flags
|= KDBG_NOWRAP
;
482 lck_spin_unlock(kds_spin_lock
);
483 ml_set_interrupts_enabled(s
);
487 enable_wrap(uint32_t old_slowcheck
, boolean_t lostevents
)
489 int s
= ml_set_interrupts_enabled(FALSE
);
490 lck_spin_lock(kds_spin_lock
);
492 kd_ctrl_page
.kdebug_flags
&= ~KDBG_NOWRAP
;
494 if ( !(old_slowcheck
& SLOW_NOLOG
))
495 kd_ctrl_page
.kdebug_slowcheck
&= ~SLOW_NOLOG
;
497 if (lostevents
== TRUE
)
498 kd_ctrl_page
.kdebug_flags
|= KDBG_WRAPPED
;
500 lck_spin_unlock(kds_spin_lock
);
501 ml_set_interrupts_enabled(s
);
505 create_buffers(boolean_t early_trace
)
514 * For the duration of this allocation, trace code will only reference
515 * kdebug_iops. Any iops registered after this enabling will not be
516 * messaged until the buffers are reallocated.
518 * TLDR; Must read kd_iops once and only once!
520 kd_ctrl_page
.kdebug_iops
= kd_iops
;
524 * If the list is valid, it is sorted, newest -> oldest. Each iop entry
525 * has a cpu_id of "the older entry + 1", so the highest cpu_id will
526 * be the list head + 1.
529 kd_ctrl_page
.kdebug_cpus
= kd_ctrl_page
.kdebug_iops
? kd_ctrl_page
.kdebug_iops
->cpu_id
+ 1 : kdbg_cpu_count(early_trace
);
531 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kdbip
, sizeof(struct kd_bufinfo
) * kd_ctrl_page
.kdebug_cpus
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
536 if (nkdbufs
< (kd_ctrl_page
.kdebug_cpus
* EVENTS_PER_STORAGE_UNIT
* MIN_STORAGE_UNITS_PER_CPU
))
537 n_storage_units
= kd_ctrl_page
.kdebug_cpus
* MIN_STORAGE_UNITS_PER_CPU
;
539 n_storage_units
= nkdbufs
/ EVENTS_PER_STORAGE_UNIT
;
541 nkdbufs
= n_storage_units
* EVENTS_PER_STORAGE_UNIT
;
543 f_buffers
= n_storage_units
/ N_STORAGE_UNITS_PER_BUFFER
;
544 n_storage_buffers
= f_buffers
;
546 f_buffer_size
= N_STORAGE_UNITS_PER_BUFFER
* sizeof(struct kd_storage
);
547 p_buffer_size
= (n_storage_units
% N_STORAGE_UNITS_PER_BUFFER
) * sizeof(struct kd_storage
);
554 if (kdcopybuf
== 0) {
555 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kdcopybuf
, (vm_size_t
)KDCOPYBUF_SIZE
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
560 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kd_bufs
, (vm_size_t
)(n_storage_buffers
* sizeof(struct kd_storage_buffers
)), VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
564 bzero(kd_bufs
, n_storage_buffers
* sizeof(struct kd_storage_buffers
));
566 for (i
= 0; i
< f_buffers
; i
++) {
567 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kd_bufs
[i
].kdsb_addr
, (vm_size_t
)f_buffer_size
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
571 bzero(kd_bufs
[i
].kdsb_addr
, f_buffer_size
);
573 kd_bufs
[i
].kdsb_size
= f_buffer_size
;
576 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kd_bufs
[i
].kdsb_addr
, (vm_size_t
)p_buffer_size
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
580 bzero(kd_bufs
[i
].kdsb_addr
, p_buffer_size
);
582 kd_bufs
[i
].kdsb_size
= p_buffer_size
;
586 for (i
= 0; i
< n_storage_buffers
; i
++) {
587 struct kd_storage
*kds
;
591 n_elements
= kd_bufs
[i
].kdsb_size
/ sizeof(struct kd_storage
);
592 kds
= kd_bufs
[i
].kdsb_addr
;
594 for (n
= 0; n
< n_elements
; n
++) {
595 kds
[n
].kds_next
.buffer_index
= kd_ctrl_page
.kds_free_list
.buffer_index
;
596 kds
[n
].kds_next
.offset
= kd_ctrl_page
.kds_free_list
.offset
;
598 kd_ctrl_page
.kds_free_list
.buffer_index
= i
;
599 kd_ctrl_page
.kds_free_list
.offset
= n
;
601 n_storage_units
+= n_elements
;
604 bzero((char *)kdbip
, sizeof(struct kd_bufinfo
) * kd_ctrl_page
.kdebug_cpus
);
606 for (i
= 0; i
< (int)kd_ctrl_page
.kdebug_cpus
; i
++) {
607 kdbip
[i
].kd_list_head
.raw
= KDS_PTR_NULL
;
608 kdbip
[i
].kd_list_tail
.raw
= KDS_PTR_NULL
;
609 kdbip
[i
].kd_lostevents
= FALSE
;
610 kdbip
[i
].num_bufs
= 0;
613 kd_ctrl_page
.kdebug_flags
|= KDBG_BUFINIT
;
615 kd_ctrl_page
.kds_inuse_count
= 0;
616 n_storage_threshold
= n_storage_units
/ 2;
630 for (i
= 0; i
< n_storage_buffers
; i
++) {
631 if (kd_bufs
[i
].kdsb_addr
) {
632 kmem_free(kernel_map
, (vm_offset_t
)kd_bufs
[i
].kdsb_addr
, (vm_size_t
)kd_bufs
[i
].kdsb_size
);
635 kmem_free(kernel_map
, (vm_offset_t
)kd_bufs
, (vm_size_t
)(n_storage_buffers
* sizeof(struct kd_storage_buffers
)));
638 n_storage_buffers
= 0;
641 kmem_free(kernel_map
, (vm_offset_t
)kdcopybuf
, KDCOPYBUF_SIZE
);
645 kd_ctrl_page
.kds_free_list
.raw
= KDS_PTR_NULL
;
648 kmem_free(kernel_map
, (vm_offset_t
)kdbip
, sizeof(struct kd_bufinfo
) * kd_ctrl_page
.kdebug_cpus
);
652 kd_ctrl_page
.kdebug_iops
= NULL
;
653 kd_ctrl_page
.kdebug_cpus
= 0;
654 kd_ctrl_page
.kdebug_flags
&= ~KDBG_BUFINIT
;
658 release_storage_unit(int cpu
, uint32_t kdsp_raw
)
661 struct kd_storage
*kdsp_actual
;
662 struct kd_bufinfo
*kdbp
;
667 s
= ml_set_interrupts_enabled(FALSE
);
668 lck_spin_lock(kds_spin_lock
);
672 if (kdsp
.raw
== kdbp
->kd_list_head
.raw
) {
674 * it's possible for the storage unit pointed to
675 * by kdsp to have already been stolen... so
676 * check to see if it's still the head of the list
677 * now that we're behind the lock that protects
678 * adding and removing from the queue...
679 * since we only ever release and steal units from
680 * that position, if it's no longer the head
681 * we having nothing to do in this context
683 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
684 kdbp
->kd_list_head
= kdsp_actual
->kds_next
;
686 kdsp_actual
->kds_next
= kd_ctrl_page
.kds_free_list
;
687 kd_ctrl_page
.kds_free_list
= kdsp
;
689 kd_ctrl_page
.kds_inuse_count
--;
691 lck_spin_unlock(kds_spin_lock
);
692 ml_set_interrupts_enabled(s
);
697 allocate_storage_unit(int cpu
)
700 struct kd_storage
*kdsp_actual
, *kdsp_next_actual
;
701 struct kd_bufinfo
*kdbp
, *kdbp_vict
, *kdbp_try
;
702 uint64_t oldest_ts
, ts
;
703 boolean_t retval
= TRUE
;
706 s
= ml_set_interrupts_enabled(FALSE
);
707 lck_spin_lock(kds_spin_lock
);
711 /* If someone beat us to the allocate, return success */
712 if (kdbp
->kd_list_tail
.raw
!= KDS_PTR_NULL
) {
713 kdsp_actual
= POINTER_FROM_KDS_PTR(kdbp
->kd_list_tail
);
715 if (kdsp_actual
->kds_bufindx
< EVENTS_PER_STORAGE_UNIT
)
719 if ((kdsp
= kd_ctrl_page
.kds_free_list
).raw
!= KDS_PTR_NULL
) {
720 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
721 kd_ctrl_page
.kds_free_list
= kdsp_actual
->kds_next
;
723 kd_ctrl_page
.kds_inuse_count
++;
725 if (kd_ctrl_page
.kdebug_flags
& KDBG_NOWRAP
) {
726 kd_ctrl_page
.kdebug_slowcheck
|= SLOW_NOLOG
;
727 kdbp
->kd_lostevents
= TRUE
;
732 oldest_ts
= (uint64_t)-1;
734 for (kdbp_try
= &kdbip
[0]; kdbp_try
< &kdbip
[kd_ctrl_page
.kdebug_cpus
]; kdbp_try
++) {
736 if (kdbp_try
->kd_list_head
.raw
== KDS_PTR_NULL
) {
738 * no storage unit to steal
743 kdsp_actual
= POINTER_FROM_KDS_PTR(kdbp_try
->kd_list_head
);
745 if (kdsp_actual
->kds_bufcnt
< EVENTS_PER_STORAGE_UNIT
) {
747 * make sure we don't steal the storage unit
748 * being actively recorded to... need to
749 * move on because we don't want an out-of-order
750 * set of events showing up later
754 ts
= kdbg_get_timestamp(&kdsp_actual
->kds_records
[0]);
756 if (ts
< oldest_ts
) {
758 * when 'wrapping', we want to steal the
759 * storage unit that has the 'earliest' time
760 * associated with it (first event time)
763 kdbp_vict
= kdbp_try
;
766 if (kdbp_vict
== NULL
) {
768 kd_ctrl_page
.enabled
= 0;
769 commpage_update_kdebug_enable();
773 kdsp
= kdbp_vict
->kd_list_head
;
774 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
775 kdbp_vict
->kd_list_head
= kdsp_actual
->kds_next
;
777 if (kdbp_vict
->kd_list_head
.raw
!= KDS_PTR_NULL
) {
778 kdsp_next_actual
= POINTER_FROM_KDS_PTR(kdbp_vict
->kd_list_head
);
779 kdsp_next_actual
->kds_lostevents
= TRUE
;
781 kdbp_vict
->kd_lostevents
= TRUE
;
783 kd_ctrl_page
.kdebug_flags
|= KDBG_WRAPPED
;
785 kdsp_actual
->kds_timestamp
= mach_absolute_time();
786 kdsp_actual
->kds_next
.raw
= KDS_PTR_NULL
;
787 kdsp_actual
->kds_bufcnt
= 0;
788 kdsp_actual
->kds_readlast
= 0;
790 kdsp_actual
->kds_lostevents
= kdbp
->kd_lostevents
;
791 kdbp
->kd_lostevents
= FALSE
;
792 kdsp_actual
->kds_bufindx
= 0;
794 if (kdbp
->kd_list_head
.raw
== KDS_PTR_NULL
)
795 kdbp
->kd_list_head
= kdsp
;
797 POINTER_FROM_KDS_PTR(kdbp
->kd_list_tail
)->kds_next
= kdsp
;
798 kdbp
->kd_list_tail
= kdsp
;
800 lck_spin_unlock(kds_spin_lock
);
801 ml_set_interrupts_enabled(s
);
807 kernel_debug_register_callback(kd_callback_t callback
)
810 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iop
, sizeof(kd_iop_t
), VM_KERN_MEMORY_DIAG
) == KERN_SUCCESS
) {
811 memcpy(&iop
->callback
, &callback
, sizeof(kd_callback_t
));
814 * <rdar://problem/13351477> Some IOP clients are not providing a name.
819 boolean_t is_valid_name
= FALSE
;
820 for (uint32_t length
=0; length
<sizeof(callback
.iop_name
); ++length
) {
821 /* This is roughly isprintable(c) */
822 if (callback
.iop_name
[length
] > 0x20 && callback
.iop_name
[length
] < 0x7F)
824 if (callback
.iop_name
[length
] == 0) {
826 is_valid_name
= TRUE
;
831 if (!is_valid_name
) {
832 strlcpy(iop
->callback
.iop_name
, "IOP-???", sizeof(iop
->callback
.iop_name
));
836 iop
->last_timestamp
= 0;
840 * We use two pieces of state, the old list head
841 * pointer, and the value of old_list_head->cpu_id.
842 * If we read kd_iops more than once, it can change
845 * TLDR; Must not read kd_iops more than once per loop.
848 iop
->cpu_id
= iop
->next
? (iop
->next
->cpu_id
+1) : kdbg_cpu_count(FALSE
);
851 * Header says OSCompareAndSwapPtr has a memory barrier
853 } while (!OSCompareAndSwapPtr(iop
->next
, iop
, (void* volatile*)&kd_iops
));
875 struct kd_bufinfo
*kdbp
;
876 struct kd_storage
*kdsp_actual
;
877 union kds_ptr kds_raw
;
879 if (kd_ctrl_page
.kdebug_slowcheck
) {
881 if ( (kd_ctrl_page
.kdebug_slowcheck
& SLOW_NOLOG
) || !(kdebug_enable
& (KDEBUG_ENABLE_TRACE
|KDEBUG_ENABLE_PPT
)))
884 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
886 * Recheck if TYPEFILTER is being used, and if so,
887 * dereference bitmap. If the trace facility is being
888 * disabled, we have ~100ms of preemption-free CPU
889 * usage to access the bitmap.
891 disable_preemption();
892 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
893 if (isset(type_filter_bitmap
, KDBG_EXTRACT_CSC(debugid
)))
894 goto record_event_preempt_disabled
;
899 else if (kd_ctrl_page
.kdebug_flags
& KDBG_RANGECHECK
) {
900 if (debugid
>= kdlog_beg
&& debugid
<= kdlog_end
)
904 else if (kd_ctrl_page
.kdebug_flags
& KDBG_VALCHECK
) {
905 if ((debugid
& KDBG_EVENTID_MASK
) != kdlog_value1
&&
906 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value2
&&
907 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value3
&&
908 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value4
)
915 disable_preemption();
917 record_event_preempt_disabled
:
918 if (kd_ctrl_page
.enabled
== 0)
921 kdbp
= &kdbip
[coreid
];
922 timestamp
&= KDBG_TIMESTAMP_MASK
;
924 #if KDEBUG_MOJO_TRACE
925 if (kdebug_enable
& KDEBUG_ENABLE_SERIAL
)
926 kdebug_serial_print(coreid
, debugid
, timestamp
,
927 arg1
, arg2
, arg3
, arg4
, threadid
);
931 kds_raw
= kdbp
->kd_list_tail
;
933 if (kds_raw
.raw
!= KDS_PTR_NULL
) {
934 kdsp_actual
= POINTER_FROM_KDS_PTR(kds_raw
);
935 bindx
= kdsp_actual
->kds_bufindx
;
939 if (kdsp_actual
== NULL
|| bindx
>= EVENTS_PER_STORAGE_UNIT
) {
940 if (allocate_storage_unit(coreid
) == FALSE
) {
942 * this can only happen if wrapping
949 if ( !OSCompareAndSwap(bindx
, bindx
+ 1, &kdsp_actual
->kds_bufindx
))
952 // IOP entries can be allocated before xnu allocates and inits the buffer
953 if (timestamp
< kdsp_actual
->kds_timestamp
)
954 kdsp_actual
->kds_timestamp
= timestamp
;
956 kd
= &kdsp_actual
->kds_records
[bindx
];
958 kd
->debugid
= debugid
;
965 kdbg_set_timestamp_and_cpu(kd
, timestamp
, coreid
);
967 OSAddAtomic(1, &kdsp_actual
->kds_bufcnt
);
971 if ((kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
)) {
972 boolean_t need_kds_wakeup
= FALSE
;
976 * try to take the lock here to synchronize with the
977 * waiter entering the blocked state... use the try
978 * mode to prevent deadlocks caused by re-entering this
979 * routine due to various trace points triggered in the
980 * lck_spin_sleep_xxxx routines used to actually enter
981 * our wait condition... no problem if we fail,
982 * there will be lots of additional events coming in that
983 * will eventually succeed in grabbing this lock
985 s
= ml_set_interrupts_enabled(FALSE
);
987 if (lck_spin_try_lock(kdw_spin_lock
)) {
989 if (kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
) {
991 need_kds_wakeup
= TRUE
;
993 lck_spin_unlock(kdw_spin_lock
);
996 ml_set_interrupts_enabled(s
);
998 if (need_kds_wakeup
== TRUE
)
1006 kernel_debug_internal(
1014 struct proc
*curproc
;
1020 struct kd_bufinfo
*kdbp
;
1021 struct kd_storage
*kdsp_actual
;
1022 union kds_ptr kds_raw
;
1026 if (kd_ctrl_page
.kdebug_slowcheck
) {
1028 if (kdebug_enable
& KDEBUG_ENABLE_CHUD
) {
1029 kd_chudhook_fn chudhook
;
1031 * Mask interrupts to minimize the interval across
1032 * which the driver providing the hook could be
1035 s
= ml_set_interrupts_enabled(FALSE
);
1036 chudhook
= kdebug_chudhook
;
1038 chudhook(debugid
, arg1
, arg2
, arg3
, arg4
, arg5
);
1039 ml_set_interrupts_enabled(s
);
1041 if ( (kd_ctrl_page
.kdebug_slowcheck
& SLOW_NOLOG
) || !(kdebug_enable
& (KDEBUG_ENABLE_TRACE
|KDEBUG_ENABLE_PPT
)))
1044 if ( !ml_at_interrupt_context()) {
1045 if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDCHECK
) {
1047 * If kdebug flag is not set for current proc, return
1049 curproc
= current_proc();
1051 if ((curproc
&& !(curproc
->p_kdebug
)) &&
1052 ((debugid
& 0xffff0000) != (MACHDBG_CODE(DBG_MACH_SCHED
, 0) | DBG_FUNC_NONE
)) &&
1053 (debugid
>> 24 != DBG_TRACE
))
1056 else if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDEXCLUDE
) {
1058 * If kdebug flag is set for current proc, return
1060 curproc
= current_proc();
1062 if ((curproc
&& curproc
->p_kdebug
) &&
1063 ((debugid
& 0xffff0000) != (MACHDBG_CODE(DBG_MACH_SCHED
, 0) | DBG_FUNC_NONE
)) &&
1064 (debugid
>> 24 != DBG_TRACE
))
1069 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1070 /* Always record trace system info */
1071 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
)
1075 * Recheck if TYPEFILTER is being used, and if so,
1076 * dereference bitmap. If the trace facility is being
1077 * disabled, we have ~100ms of preemption-free CPU
1078 * usage to access the bitmap.
1080 disable_preemption();
1081 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1082 if (isset(type_filter_bitmap
, KDBG_EXTRACT_CSC(debugid
)))
1083 goto record_event_preempt_disabled
;
1085 enable_preemption();
1088 else if (kd_ctrl_page
.kdebug_flags
& KDBG_RANGECHECK
) {
1089 /* Always record trace system info */
1090 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
)
1093 if (debugid
< kdlog_beg
|| debugid
> kdlog_end
)
1096 else if (kd_ctrl_page
.kdebug_flags
& KDBG_VALCHECK
) {
1097 /* Always record trace system info */
1098 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
)
1101 if ((debugid
& KDBG_EVENTID_MASK
) != kdlog_value1
&&
1102 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value2
&&
1103 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value3
&&
1104 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value4
)
1109 disable_preemption();
1111 record_event_preempt_disabled
:
1112 if (kd_ctrl_page
.enabled
== 0)
1118 #if KDEBUG_MOJO_TRACE
1119 if (kdebug_enable
& KDEBUG_ENABLE_SERIAL
)
1120 kdebug_serial_print(cpu
, debugid
,
1121 mach_absolute_time() & KDBG_TIMESTAMP_MASK
,
1122 arg1
, arg2
, arg3
, arg4
, arg5
);
1126 kds_raw
= kdbp
->kd_list_tail
;
1128 if (kds_raw
.raw
!= KDS_PTR_NULL
) {
1129 kdsp_actual
= POINTER_FROM_KDS_PTR(kds_raw
);
1130 bindx
= kdsp_actual
->kds_bufindx
;
1134 if (kdsp_actual
== NULL
|| bindx
>= EVENTS_PER_STORAGE_UNIT
) {
1135 if (allocate_storage_unit(cpu
) == FALSE
) {
1137 * this can only happen if wrapping
1144 now
= mach_absolute_time() & KDBG_TIMESTAMP_MASK
;
1146 if ( !OSCompareAndSwap(bindx
, bindx
+ 1, &kdsp_actual
->kds_bufindx
))
1149 kd
= &kdsp_actual
->kds_records
[bindx
];
1151 kd
->debugid
= debugid
;
1158 kdbg_set_timestamp_and_cpu(kd
, now
, cpu
);
1160 OSAddAtomic(1, &kdsp_actual
->kds_bufcnt
);
1162 enable_preemption();
1164 if (kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
) {
1168 etype
= debugid
& KDBG_EVENTID_MASK
;
1169 stype
= debugid
& KDBG_CSC_MASK
;
1171 if (etype
== INTERRUPT
|| etype
== MACH_vmfault
||
1172 stype
== BSC_SysCall
|| stype
== MACH_SysCall
) {
1174 boolean_t need_kds_wakeup
= FALSE
;
1177 * try to take the lock here to synchronize with the
1178 * waiter entering the blocked state... use the try
1179 * mode to prevent deadlocks caused by re-entering this
1180 * routine due to various trace points triggered in the
1181 * lck_spin_sleep_xxxx routines used to actually enter
1182 * one of our 2 wait conditions... no problem if we fail,
1183 * there will be lots of additional events coming in that
1184 * will eventually succeed in grabbing this lock
1186 s
= ml_set_interrupts_enabled(FALSE
);
1188 if (lck_spin_try_lock(kdw_spin_lock
)) {
1190 if (kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
) {
1192 need_kds_wakeup
= TRUE
;
1194 lck_spin_unlock(kdw_spin_lock
);
1196 ml_set_interrupts_enabled(s
);
1198 if (need_kds_wakeup
== TRUE
)
1199 wakeup(&kds_waiter
);
1211 __unused
uintptr_t arg5
)
1213 kernel_debug_internal(debugid
, arg1
, arg2
, arg3
, arg4
, (uintptr_t)thread_tid(current_thread()));
1225 kernel_debug_internal(debugid
, arg1
, arg2
, arg3
, arg4
, arg5
);
1229 kernel_debug_string_simple(const char *message
)
1231 uintptr_t arg
[4] = {0, 0, 0, 0};
1233 /* Stuff the message string in the args and log it. */
1234 strncpy((char *)arg
, message
, MIN(sizeof(arg
), strlen(message
)));
1237 arg
[0], arg
[1], arg
[2], arg
[3]);
1240 extern int master_cpu
; /* MACH_KERNEL_PRIVATE */
1242 * Used prior to start_kern_tracing() being called.
1243 * Log temporarily into a static buffer.
1253 /* If tracing is already initialized, use it */
1255 KERNEL_DEBUG_CONSTANT(debugid
, arg1
, arg2
, arg3
, arg4
, 0);
1259 /* Do nothing if the buffer is full or we're not on the boot cpu */
1260 kd_early_overflow
= kd_early_index
>= KD_EARLY_BUFFER_MAX
;
1261 if (kd_early_overflow
||
1262 cpu_number() != master_cpu
)
1265 kd_early_buffer
[kd_early_index
].debugid
= debugid
;
1266 kd_early_buffer
[kd_early_index
].timestamp
= mach_absolute_time();
1267 kd_early_buffer
[kd_early_index
].arg1
= arg1
;
1268 kd_early_buffer
[kd_early_index
].arg2
= arg2
;
1269 kd_early_buffer
[kd_early_index
].arg3
= arg3
;
1270 kd_early_buffer
[kd_early_index
].arg4
= arg4
;
1271 kd_early_buffer
[kd_early_index
].arg5
= 0;
1276 * Transfen the contents of the temporary buffer into the trace buffers.
1277 * Precede that by logging the rebase time (offset) - the TSC-based time (in ns)
1278 * when mach_absolute_time is set to 0.
1281 kernel_debug_early_end(void)
1285 if (cpu_number() != master_cpu
)
1286 panic("kernel_debug_early_end() not call on boot processor");
1288 /* Fake sentinel marking the start of kernel time relative to TSC */
1293 (uint32_t)(tsc_rebase_abs_time
>> 32),
1294 (uint32_t)tsc_rebase_abs_time
,
1298 for (i
= 0; i
< kd_early_index
; i
++) {
1301 kd_early_buffer
[i
].debugid
,
1302 kd_early_buffer
[i
].timestamp
,
1303 kd_early_buffer
[i
].arg1
,
1304 kd_early_buffer
[i
].arg2
,
1305 kd_early_buffer
[i
].arg3
,
1306 kd_early_buffer
[i
].arg4
,
1310 /* Cut events-lost event on overflow */
1311 if (kd_early_overflow
)
1312 KERNEL_DEBUG_CONSTANT(
1313 TRACE_LOST_EVENTS
, 0, 0, 0, 0, 0);
1315 /* This trace marks the start of kernel tracing */
1316 kernel_debug_string_simple("early trace done");
1320 * Returns non-zero if debugid is in a reserved class.
1323 kdebug_validate_debugid(uint32_t debugid
)
1325 uint8_t debugid_class
;
1327 debugid_class
= KDBG_EXTRACT_CLASS(debugid
);
1328 switch (debugid_class
) {
1337 * Support syscall SYS_kdebug_trace. U64->K32 args may get truncated in kdebug_trace64
1340 kdebug_trace(struct proc
*p
, struct kdebug_trace_args
*uap
, int32_t *retval
)
1342 struct kdebug_trace64_args uap64
;
1344 uap64
.code
= uap
->code
;
1345 uap64
.arg1
= uap
->arg1
;
1346 uap64
.arg2
= uap
->arg2
;
1347 uap64
.arg3
= uap
->arg3
;
1348 uap64
.arg4
= uap
->arg4
;
1350 return kdebug_trace64(p
, &uap64
, retval
);
1354 * Support syscall SYS_kdebug_trace64. 64-bit args on K32 will get truncated to fit in 32-bit record format.
1356 int kdebug_trace64(__unused
struct proc
*p
, struct kdebug_trace64_args
*uap
, __unused
int32_t *retval
)
1360 if ((err
= kdebug_validate_debugid(uap
->code
)) != 0) {
1364 if ( __probable(kdebug_enable
== 0) )
1367 kernel_debug_internal(uap
->code
, (uintptr_t)uap
->arg1
, (uintptr_t)uap
->arg2
, (uintptr_t)uap
->arg3
, (uintptr_t)uap
->arg4
, (uintptr_t)thread_tid(current_thread()));
1373 * Adding enough padding to contain a full tracepoint for the last
1374 * portion of the string greatly simplifies the logic of splitting the
1375 * string between tracepoints. Full tracepoints can be generated using
1376 * the buffer itself, without having to manually add zeros to pad the
1380 /* 2 string args in first tracepoint and 9 string data tracepoints */
1381 #define STR_BUF_ARGS (2 + (9 * 4))
1382 /* times the size of each arg on K64 */
1383 #define MAX_STR_LEN (STR_BUF_ARGS * sizeof(uint64_t))
1384 /* on K32, ending straddles a tracepoint, so reserve blanks */
1385 #define STR_BUF_SIZE (MAX_STR_LEN + (2 * sizeof(uint32_t)))
1388 * This function does no error checking and assumes that it is called with
1389 * the correct arguments, including that the buffer pointed to by str is at
1390 * least STR_BUF_SIZE bytes. However, str must be aligned to word-size and
1391 * be NUL-terminated. In cases where a string can fit evenly into a final
1392 * tracepoint without its NUL-terminator, this function will not end those
1393 * strings with a NUL in trace. It's up to clients to look at the function
1394 * qualifier for DBG_FUNC_END in this case, to end the string.
1397 kernel_debug_string_internal(uint32_t debugid
, uint64_t str_id
, void *vstr
,
1400 /* str must be word-aligned */
1401 uintptr_t *str
= vstr
;
1403 uintptr_t thread_id
;
1405 uint32_t trace_debugid
= TRACEDBG_CODE(DBG_TRACE_STRING
,
1406 TRACE_STRING_GLOBAL
);
1408 thread_id
= (uintptr_t)thread_tid(current_thread());
1410 /* if the ID is being invalidated, just emit that */
1411 if (str_id
!= 0 && str_len
== 0) {
1412 kernel_debug_internal(trace_debugid
| DBG_FUNC_START
| DBG_FUNC_END
,
1413 (uintptr_t)debugid
, (uintptr_t)str_id
, 0, 0,
1418 /* generate an ID, if necessary */
1420 str_id
= OSIncrementAtomic64((SInt64
*)&g_curr_str_id
);
1421 str_id
= (str_id
& STR_ID_MASK
) | g_str_id_signature
;
1424 trace_debugid
|= DBG_FUNC_START
;
1425 /* string can fit in a single tracepoint */
1426 if (str_len
<= (2 * sizeof(uintptr_t))) {
1427 trace_debugid
|= DBG_FUNC_END
;
1430 kernel_debug_internal(trace_debugid
, (uintptr_t)debugid
,
1431 (uintptr_t)str_id
, str
[0],
1434 trace_debugid
&= KDBG_EVENTID_MASK
;
1436 written
+= 2 * sizeof(uintptr_t);
1438 for (; written
< str_len
; i
+= 4, written
+= 4 * sizeof(uintptr_t)) {
1439 if ((written
+ (4 * sizeof(uintptr_t))) >= str_len
) {
1440 trace_debugid
|= DBG_FUNC_END
;
1442 kernel_debug_internal(trace_debugid
, str
[i
],
1445 str
[i
+ 3], thread_id
);
1452 * Returns true if the current process can emit events, and false otherwise.
1453 * Trace system and scheduling events circumvent this check, as do events
1454 * emitted in interrupt context.
1457 kdebug_current_proc_enabled(uint32_t debugid
)
1459 /* can't determine current process in interrupt context */
1460 if (ml_at_interrupt_context()) {
1464 /* always emit trace system and scheduling events */
1465 if ((KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
||
1466 (debugid
& KDBG_CSC_MASK
) == MACHDBG_CODE(DBG_MACH_SCHED
, 0)))
1471 if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDCHECK
) {
1472 proc_t cur_proc
= current_proc();
1474 /* only the process with the kdebug bit set is allowed */
1475 if (cur_proc
&& !(cur_proc
->p_kdebug
)) {
1478 } else if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDEXCLUDE
) {
1479 proc_t cur_proc
= current_proc();
1481 /* every process except the one with the kdebug bit set is allowed */
1482 if (cur_proc
&& cur_proc
->p_kdebug
) {
1491 * Returns true if the debugid is disabled by filters, and false if the
1492 * debugid is allowed to be traced. A debugid may not be traced if the
1493 * typefilter disables its class and subclass, it's outside a range
1494 * check, or if it's not an allowed debugid in a value check. Trace
1495 * system events bypass this check.
1498 kdebug_debugid_enabled(uint32_t debugid
)
1500 boolean_t is_enabled
= TRUE
;
1502 /* if no filtering is enabled */
1503 if (!kd_ctrl_page
.kdebug_slowcheck
) {
1507 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
) {
1511 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1512 disable_preemption();
1515 * Recheck if typefilter is still being used. If tracing is being
1516 * disabled, there's a 100ms sleep on the other end to keep the
1517 * bitmap around for this check.
1519 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1520 if (!(isset(type_filter_bitmap
, KDBG_EXTRACT_CSC(debugid
)))) {
1525 enable_preemption();
1526 } else if (kd_ctrl_page
.kdebug_flags
& KDBG_RANGECHECK
) {
1527 if (debugid
< kdlog_beg
|| debugid
> kdlog_end
) {
1530 } else if (kd_ctrl_page
.kdebug_flags
& KDBG_VALCHECK
) {
1531 if ((debugid
& KDBG_EVENTID_MASK
) != kdlog_value1
&&
1532 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value2
&&
1533 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value3
&&
1534 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value4
)
1544 * Returns 0 if a string can be traced with these arguments. Returns errno
1545 * value if error occurred.
1548 kdebug_check_trace_string(uint32_t debugid
, uint64_t str_id
)
1550 /* if there are function qualifiers on the debugid */
1551 if (debugid
& ~KDBG_EVENTID_MASK
) {
1555 if (kdebug_validate_debugid(debugid
)) {
1559 if (str_id
!= 0 && (str_id
& STR_ID_SIG_MASK
) != g_str_id_signature
) {
1567 * Implementation of KPI kernel_debug_string.
1570 kernel_debug_string(uint32_t debugid
, uint64_t *str_id
, const char *str
)
1572 /* arguments to tracepoints must be word-aligned */
1573 __attribute__((aligned(sizeof(uintptr_t)))) char str_buf
[STR_BUF_SIZE
];
1574 assert_static(sizeof(str_buf
) > MAX_STR_LEN
);
1575 vm_size_t len_copied
;
1580 if (__probable(kdebug_enable
== 0)) {
1584 if (!kdebug_current_proc_enabled(debugid
)) {
1588 if (!kdebug_debugid_enabled(debugid
)) {
1592 if ((err
= kdebug_check_trace_string(debugid
, *str_id
)) != 0) {
1601 *str_id
= kernel_debug_string_internal(debugid
, *str_id
, NULL
, 0);
1605 memset(str_buf
, 0, sizeof(str_buf
));
1606 len_copied
= strlcpy(str_buf
, str
, MAX_STR_LEN
+ 1);
1607 *str_id
= kernel_debug_string_internal(debugid
, *str_id
, str_buf
,
1613 * Support syscall kdebug_trace_string.
1616 kdebug_trace_string(__unused
struct proc
*p
,
1617 struct kdebug_trace_string_args
*uap
,
1620 __attribute__((aligned(sizeof(uintptr_t)))) char str_buf
[STR_BUF_SIZE
];
1621 assert_static(sizeof(str_buf
) > MAX_STR_LEN
);
1625 if (__probable(kdebug_enable
== 0)) {
1629 if (!kdebug_current_proc_enabled(uap
->debugid
)) {
1633 if (!kdebug_debugid_enabled(uap
->debugid
)) {
1637 if ((err
= kdebug_check_trace_string(uap
->debugid
, uap
->str_id
)) != 0) {
1641 if (uap
->str
== USER_ADDR_NULL
) {
1642 if (uap
->str_id
== 0) {
1646 *retval
= kernel_debug_string_internal(uap
->debugid
, uap
->str_id
,
1651 memset(str_buf
, 0, sizeof(str_buf
));
1652 err
= copyinstr(uap
->str
, str_buf
, MAX_STR_LEN
+ 1, &len_copied
);
1654 /* it's alright to truncate the string, so allow ENAMETOOLONG */
1655 if (err
== ENAMETOOLONG
) {
1656 str_buf
[MAX_STR_LEN
] = '\0';
1661 if (len_copied
<= 1) {
1665 /* convert back to a length */
1668 *retval
= kernel_debug_string_internal(uap
->debugid
, uap
->str_id
, str_buf
,
1674 kdbg_lock_init(void)
1676 if (kd_ctrl_page
.kdebug_flags
& KDBG_LOCKINIT
)
1680 * allocate lock group attribute and group
1682 kd_trace_mtx_sysctl_grp_attr
= lck_grp_attr_alloc_init();
1683 kd_trace_mtx_sysctl_grp
= lck_grp_alloc_init("kdebug", kd_trace_mtx_sysctl_grp_attr
);
1686 * allocate the lock attribute
1688 kd_trace_mtx_sysctl_attr
= lck_attr_alloc_init();
1692 * allocate and initialize mutex's
1694 kd_trace_mtx_sysctl
= lck_mtx_alloc_init(kd_trace_mtx_sysctl_grp
, kd_trace_mtx_sysctl_attr
);
1695 kds_spin_lock
= lck_spin_alloc_init(kd_trace_mtx_sysctl_grp
, kd_trace_mtx_sysctl_attr
);
1696 kdw_spin_lock
= lck_spin_alloc_init(kd_trace_mtx_sysctl_grp
, kd_trace_mtx_sysctl_attr
);
1698 kd_ctrl_page
.kdebug_flags
|= KDBG_LOCKINIT
;
1703 kdbg_bootstrap(boolean_t early_trace
)
1705 kd_ctrl_page
.kdebug_flags
&= ~KDBG_WRAPPED
;
1707 return (create_buffers(early_trace
));
1711 kdbg_reinit(boolean_t early_trace
)
1716 * Disable trace collecting
1717 * First make sure we're not in
1718 * the middle of cutting a trace
1720 kdbg_set_tracing_enabled(FALSE
, KDEBUG_ENABLE_TRACE
);
1723 * make sure the SLOW_NOLOG is seen
1724 * by everyone that might be trying
1731 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
1732 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
1733 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
1738 ret
= kdbg_bootstrap(early_trace
);
1740 RAW_file_offset
= 0;
1741 RAW_file_written
= 0;
1747 kdbg_trace_data(struct proc
*proc
, long *arg_pid
)
1752 *arg_pid
= proc
->p_pid
;
1757 kdbg_trace_string(struct proc
*proc
, long *arg1
, long *arg2
, long *arg3
, long *arg4
)
1771 * Collect the pathname for tracing
1773 dbg_nameptr
= proc
->p_comm
;
1774 dbg_namelen
= (int)strlen(proc
->p_comm
);
1780 if(dbg_namelen
> (int)sizeof(dbg_parms
))
1781 dbg_namelen
= (int)sizeof(dbg_parms
);
1783 strncpy((char *)dbg_parms
, dbg_nameptr
, dbg_namelen
);
1792 kdbg_resolve_map(thread_t th_act
, void *opaque
)
1794 kd_threadmap
*mapptr
;
1795 krt_t
*t
= (krt_t
*)opaque
;
1797 if (t
->count
< t
->maxcount
) {
1798 mapptr
= &t
->map
[t
->count
];
1799 mapptr
->thread
= (uintptr_t)thread_tid(th_act
);
1801 (void) strlcpy (mapptr
->command
, t
->atts
->task_comm
,
1802 sizeof(t
->atts
->task_comm
));
1804 * Some kernel threads have no associated pid.
1805 * We still need to mark the entry as valid.
1808 mapptr
->valid
= t
->atts
->pid
;
1818 * Writes a cpumap for the given iops_list/cpu_count to the provided buffer.
1820 * You may provide a buffer and size, or if you set the buffer to NULL, a
1821 * buffer of sufficient size will be allocated.
1823 * If you provide a buffer and it is too small, sets cpumap_size to the number
1824 * of bytes required and returns EINVAL.
1826 * On success, if you provided a buffer, cpumap_size is set to the number of
1827 * bytes written. If you did not provide a buffer, cpumap is set to the newly
1828 * allocated buffer and cpumap_size is set to the number of bytes allocated.
1830 * NOTE: It may seem redundant to pass both iops and a cpu_count.
1832 * We may be reporting data from "now", or from the "past".
1834 * The "now" data would be for something like kdbg_readcurcpumap().
1835 * The "past" data would be for kdbg_readcpumap().
1837 * If we do not pass both iops and cpu_count, and iops is NULL, this function
1838 * will need to read "now" state to get the number of cpus, which would be in
1839 * error if we were reporting "past" state.
1843 kdbg_cpumap_init_internal(kd_iop_t
* iops
, uint32_t cpu_count
, uint8_t** cpumap
, uint32_t* cpumap_size
)
1846 assert(cpumap_size
);
1848 assert(!iops
|| iops
->cpu_id
+ 1 == cpu_count
);
1850 uint32_t bytes_needed
= sizeof(kd_cpumap_header
) + cpu_count
* sizeof(kd_cpumap
);
1851 uint32_t bytes_available
= *cpumap_size
;
1852 *cpumap_size
= bytes_needed
;
1854 if (*cpumap
== NULL
) {
1855 if (kmem_alloc(kernel_map
, (vm_offset_t
*)cpumap
, (vm_size_t
)*cpumap_size
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
1858 } else if (bytes_available
< bytes_needed
) {
1862 kd_cpumap_header
* header
= (kd_cpumap_header
*)(uintptr_t)*cpumap
;
1864 header
->version_no
= RAW_VERSION1
;
1865 header
->cpu_count
= cpu_count
;
1867 kd_cpumap
* cpus
= (kd_cpumap
*)&header
[1];
1869 int32_t index
= cpu_count
- 1;
1871 cpus
[index
].cpu_id
= iops
->cpu_id
;
1872 cpus
[index
].flags
= KDBG_CPUMAP_IS_IOP
;
1873 bzero(cpus
[index
].name
, sizeof(cpus
->name
));
1874 strlcpy(cpus
[index
].name
, iops
->callback
.iop_name
, sizeof(cpus
->name
));
1880 while (index
>= 0) {
1881 cpus
[index
].cpu_id
= index
;
1882 cpus
[index
].flags
= 0;
1883 bzero(cpus
[index
].name
, sizeof(cpus
->name
));
1884 strlcpy(cpus
[index
].name
, "AP", sizeof(cpus
->name
));
1889 return KERN_SUCCESS
;
1893 kdbg_thrmap_init(void)
1895 if (kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
)
1898 kd_mapptr
= kdbg_thrmap_init_internal(0, &kd_mapsize
, &kd_mapcount
);
1901 kd_ctrl_page
.kdebug_flags
|= KDBG_MAPINIT
;
1905 kd_threadmap
* kdbg_thrmap_init_internal(unsigned int count
, unsigned int *mapsize
, unsigned int *mapcount
)
1907 kd_threadmap
*mapptr
;
1910 int tts_count
; /* number of task-to-string structures */
1911 struct tts
*tts_mapptr
;
1912 unsigned int tts_mapsize
= 0;
1917 * need to use PROC_SCANPROCLIST with proc_iterate
1922 * Calculate the sizes of map buffers
1924 for (p
= allproc
.lh_first
, *mapcount
=0, tts_count
=0; p
; p
= p
->p_list
.le_next
) {
1925 *mapcount
+= get_task_numacts((task_t
)p
->task
);
1931 * The proc count could change during buffer allocation,
1932 * so introduce a small fudge factor to bump up the
1933 * buffer sizes. This gives new tasks some chance of
1934 * making into the tables. Bump up by 25%.
1936 *mapcount
+= *mapcount
/4;
1937 tts_count
+= tts_count
/4;
1939 *mapsize
= *mapcount
* sizeof(kd_threadmap
);
1941 if (count
&& count
< *mapcount
)
1944 if ((kmem_alloc(kernel_map
, &kaddr
, (vm_size_t
)*mapsize
, VM_KERN_MEMORY_DIAG
) == KERN_SUCCESS
)) {
1945 bzero((void *)kaddr
, *mapsize
);
1946 mapptr
= (kd_threadmap
*)kaddr
;
1950 tts_mapsize
= tts_count
* sizeof(struct tts
);
1952 if ((kmem_alloc(kernel_map
, &kaddr
, (vm_size_t
)tts_mapsize
, VM_KERN_MEMORY_DIAG
) == KERN_SUCCESS
)) {
1953 bzero((void *)kaddr
, tts_mapsize
);
1954 tts_mapptr
= (struct tts
*)kaddr
;
1956 kmem_free(kernel_map
, (vm_offset_t
)mapptr
, *mapsize
);
1961 * We need to save the procs command string
1962 * and take a reference for each task associated
1963 * with a valid process
1969 * should use proc_iterate
1971 for (p
= allproc
.lh_first
, i
=0; p
&& i
< tts_count
; p
= p
->p_list
.le_next
) {
1972 if (p
->p_lflag
& P_LEXIT
)
1976 task_reference(p
->task
);
1977 tts_mapptr
[i
].task
= p
->task
;
1978 tts_mapptr
[i
].pid
= p
->p_pid
;
1979 (void)strlcpy(tts_mapptr
[i
].task_comm
, p
->p_comm
, sizeof(tts_mapptr
[i
].task_comm
));
1988 * Initialize thread map data
1992 akrt
.maxcount
= *mapcount
;
1994 for (i
= 0; i
< tts_count
; i
++) {
1995 akrt
.atts
= &tts_mapptr
[i
];
1996 task_act_iterate_wth_args(tts_mapptr
[i
].task
, kdbg_resolve_map
, &akrt
);
1997 task_deallocate((task_t
) tts_mapptr
[i
].task
);
1999 kmem_free(kernel_map
, (vm_offset_t
)tts_mapptr
, tts_mapsize
);
2001 *mapcount
= akrt
.count
;
2010 * Clean up the trace buffer
2011 * First make sure we're not in
2012 * the middle of cutting a trace
2014 kdbg_set_tracing_enabled(FALSE
, KDEBUG_ENABLE_TRACE
);
2015 kdbg_disable_typefilter();
2018 * make sure the SLOW_NOLOG is seen
2019 * by everyone that might be trying
2024 global_state_pid
= -1;
2025 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2026 kd_ctrl_page
.kdebug_flags
&= ~(KDBG_NOWRAP
| KDBG_RANGECHECK
| KDBG_VALCHECK
);
2027 kd_ctrl_page
.kdebug_flags
&= ~(KDBG_PIDCHECK
| KDBG_PIDEXCLUDE
);
2029 kdbg_deallocate_typefilter();
2033 /* Clean up the thread map buffer */
2034 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
2036 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
2037 kd_mapptr
= (kd_threadmap
*) 0;
2042 RAW_file_offset
= 0;
2043 RAW_file_written
= 0;
2047 kdbg_setpid(kd_regtype
*kdr
)
2053 pid
= (pid_t
)kdr
->value1
;
2054 flag
= (int)kdr
->value2
;
2057 if ((p
= proc_find(pid
)) == NULL
)
2062 * turn on pid check for this and all pids
2064 kd_ctrl_page
.kdebug_flags
|= KDBG_PIDCHECK
;
2065 kd_ctrl_page
.kdebug_flags
&= ~KDBG_PIDEXCLUDE
;
2066 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2071 * turn off pid check for this pid value
2072 * Don't turn off all pid checking though
2074 * kd_ctrl_page.kdebug_flags &= ~KDBG_PIDCHECK;
2087 /* This is for pid exclusion in the trace buffer */
2089 kdbg_setpidex(kd_regtype
*kdr
)
2095 pid
= (pid_t
)kdr
->value1
;
2096 flag
= (int)kdr
->value2
;
2099 if ((p
= proc_find(pid
)) == NULL
)
2104 * turn on pid exclusion
2106 kd_ctrl_page
.kdebug_flags
|= KDBG_PIDEXCLUDE
;
2107 kd_ctrl_page
.kdebug_flags
&= ~KDBG_PIDCHECK
;
2108 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2114 * turn off pid exclusion for this pid value
2115 * Don't turn off all pid exclusion though
2117 * kd_ctrl_page.kdebug_flags &= ~KDBG_PIDEXCLUDE;
2131 * This is for setting a maximum decrementer value
2134 kdbg_setrtcdec(kd_regtype
*kdr
)
2139 decval
= (natural_t
)kdr
->value1
;
2141 if (decval
&& decval
< KDBG_MINRTCDEC
)
2150 kdbg_enable_typefilter(void)
2154 /* Allocate memory for bitmap if not already allocated */
2155 ret
= kdbg_allocate_typefilter();
2160 /* Turn off range and value checks */
2161 kd_ctrl_page
.kdebug_flags
&= ~(KDBG_RANGECHECK
| KDBG_VALCHECK
);
2163 /* Enable filter checking */
2164 kd_ctrl_page
.kdebug_flags
|= KDBG_TYPEFILTER_CHECK
;
2165 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2170 kdbg_disable_typefilter(void)
2172 /* Disable filter checking */
2173 kd_ctrl_page
.kdebug_flags
&= ~KDBG_TYPEFILTER_CHECK
;
2175 /* Turn off slow checks unless pid checks are using them */
2176 if ( (kd_ctrl_page
.kdebug_flags
& (KDBG_PIDCHECK
| KDBG_PIDEXCLUDE
)) )
2177 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2179 kdbg_set_flags(SLOW_CHECKS
, 0, FALSE
);
2181 /* typefilter bitmap will be deallocated later */
2187 kdbg_allocate_typefilter(void)
2189 if (type_filter_bitmap
== NULL
) {
2190 vm_offset_t bitmap
= 0;
2192 if (kmem_alloc(kernel_map
, &bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
2196 bzero((void *)bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2198 if (!OSCompareAndSwapPtr(NULL
, (void *)bitmap
, &type_filter_bitmap
)) {
2199 kmem_free(kernel_map
, bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2200 return 0; /* someone assigned a buffer */
2203 bzero(type_filter_bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2210 kdbg_deallocate_typefilter(void)
2212 if(type_filter_bitmap
) {
2213 vm_offset_t bitmap
= (vm_offset_t
)type_filter_bitmap
;
2215 if (OSCompareAndSwapPtr((void *)bitmap
, NULL
, &type_filter_bitmap
)) {
2216 kmem_free(kernel_map
, bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2219 /* already swapped */
2227 kdbg_setreg(kd_regtype
* kdr
)
2230 unsigned int val_1
, val_2
, val
;
2231 switch (kdr
->type
) {
2233 case KDBG_CLASSTYPE
:
2234 val_1
= (kdr
->value1
& 0xff);
2235 val_2
= (kdr
->value2
& 0xff);
2236 kdlog_beg
= (val_1
<<24);
2237 kdlog_end
= (val_2
<<24);
2238 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2239 kd_ctrl_page
.kdebug_flags
&= ~KDBG_VALCHECK
; /* Turn off specific value check */
2240 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_CLASSTYPE
);
2241 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2243 case KDBG_SUBCLSTYPE
:
2244 val_1
= (kdr
->value1
& 0xff);
2245 val_2
= (kdr
->value2
& 0xff);
2247 kdlog_beg
= ((val_1
<<24) | (val_2
<< 16));
2248 kdlog_end
= ((val_1
<<24) | (val
<< 16));
2249 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2250 kd_ctrl_page
.kdebug_flags
&= ~KDBG_VALCHECK
; /* Turn off specific value check */
2251 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_SUBCLSTYPE
);
2252 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2254 case KDBG_RANGETYPE
:
2255 kdlog_beg
= (kdr
->value1
);
2256 kdlog_end
= (kdr
->value2
);
2257 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2258 kd_ctrl_page
.kdebug_flags
&= ~KDBG_VALCHECK
; /* Turn off specific value check */
2259 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_RANGETYPE
);
2260 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2263 kdlog_value1
= (kdr
->value1
);
2264 kdlog_value2
= (kdr
->value2
);
2265 kdlog_value3
= (kdr
->value3
);
2266 kdlog_value4
= (kdr
->value4
);
2267 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2268 kd_ctrl_page
.kdebug_flags
&= ~KDBG_RANGECHECK
; /* Turn off range check */
2269 kd_ctrl_page
.kdebug_flags
|= KDBG_VALCHECK
; /* Turn on specific value check */
2270 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2272 case KDBG_TYPENONE
:
2273 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2275 if ( (kd_ctrl_page
.kdebug_flags
& (KDBG_RANGECHECK
| KDBG_VALCHECK
|
2276 KDBG_PIDCHECK
| KDBG_PIDEXCLUDE
|
2277 KDBG_TYPEFILTER_CHECK
)) )
2278 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2280 kdbg_set_flags(SLOW_CHECKS
, 0, FALSE
);
2293 kdbg_write_to_vnode(caddr_t buffer
, size_t size
, vnode_t vp
, vfs_context_t ctx
, off_t file_offset
)
2295 return vn_rdwr(UIO_WRITE
, vp
, buffer
, size
, file_offset
, UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
,
2296 vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2300 kdbg_write_v3_chunk_header(user_addr_t buffer
, uint32_t tag
, uint32_t sub_tag
, uint64_t length
, vnode_t vp
, vfs_context_t ctx
)
2302 int ret
= KERN_SUCCESS
;
2303 kd_chunk_header_v3 header
;
2306 header
.sub_tag
= sub_tag
;
2307 header
.length
= length
;
2309 // Check that only one of them is valid
2310 assert(!buffer
^ !vp
);
2311 assert((vp
== NULL
) || (ctx
!= NULL
));
2313 // Write the 8-byte future_chunk_timestamp field in the payload
2316 ret
= kdbg_write_to_vnode((caddr_t
)&header
, sizeof(kd_chunk_header_v3
), vp
, ctx
, RAW_file_offset
);
2320 RAW_file_offset
+= (sizeof(kd_chunk_header_v3
));
2323 ret
= copyout(&header
, buffer
, sizeof(kd_chunk_header_v3
));
2334 kdbg_write_v3_chunk_header_to_buffer(void * buffer
, uint32_t tag
, uint32_t sub_tag
, uint64_t length
)
2336 kd_chunk_header_v3 header
;
2339 header
.sub_tag
= sub_tag
;
2340 header
.length
= length
;
2346 memcpy(buffer
, &header
, sizeof(kd_chunk_header_v3
));
2348 return (sizeof(kd_chunk_header_v3
));
2352 kdbg_write_v3_chunk_to_fd(uint32_t tag
, uint32_t sub_tag
, uint64_t length
, void *payload
, uint64_t payload_size
, int fd
)
2355 struct vfs_context context
;
2356 struct fileproc
*fp
;
2361 if ( (fp_lookup(p
, fd
, &fp
, 1)) ) {
2366 context
.vc_thread
= current_thread();
2367 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
2369 if (FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_VNODE
) {
2370 fp_drop(p
, fd
, fp
, 1);
2374 vp
= (struct vnode
*) fp
->f_fglob
->fg_data
;
2377 if ( (vnode_getwithref(vp
)) == 0 ) {
2378 RAW_file_offset
= fp
->f_fglob
->fg_offset
;
2380 kd_chunk_header_v3 chunk_header
= { .tag
= tag
, .sub_tag
= sub_tag
, .length
= length
};
2382 int ret
= kdbg_write_to_vnode((caddr_t
) &chunk_header
, sizeof(kd_chunk_header_v3
), vp
, &context
, RAW_file_offset
);
2384 RAW_file_offset
+= sizeof(kd_chunk_header_v3
);
2387 ret
= kdbg_write_to_vnode((caddr_t
) payload
, (size_t) payload_size
, vp
, &context
, RAW_file_offset
);
2389 RAW_file_offset
+= payload_size
;
2392 fp
->f_fglob
->fg_offset
= RAW_file_offset
;
2396 fp_drop(p
, fd
, fp
, 0);
2397 return KERN_SUCCESS
;
2401 kdbg_write_v3_event_chunk_header(user_addr_t buffer
, uint32_t tag
, uint64_t length
, vnode_t vp
, vfs_context_t ctx
)
2403 uint64_t future_chunk_timestamp
= 0;
2404 length
+= sizeof(uint64_t);
2406 if (kdbg_write_v3_chunk_header(buffer
, tag
, V3_EVENT_DATA_VERSION
, length
, vp
, ctx
)) {
2410 buffer
+= sizeof(kd_chunk_header_v3
);
2413 // Check that only one of them is valid
2414 assert(!buffer
^ !vp
);
2415 assert((vp
== NULL
) || (ctx
!= NULL
));
2417 // Write the 8-byte future_chunk_timestamp field in the payload
2420 int ret
= kdbg_write_to_vnode((caddr_t
)&future_chunk_timestamp
, sizeof(uint64_t), vp
, ctx
, RAW_file_offset
);
2422 RAW_file_offset
+= (sizeof(uint64_t));
2426 if (copyout(&future_chunk_timestamp
, buffer
, sizeof(uint64_t))) {
2432 return (buffer
+ sizeof(uint64_t));
2436 kdbg_write_v3_header(user_addr_t user_header
, size_t *user_header_size
, int fd
)
2438 int ret
= KERN_SUCCESS
;
2439 kd_header_v3 header
;
2441 uint8_t* cpumap
= 0;
2442 uint32_t cpumap_size
= 0;
2443 uint32_t thrmap_size
= 0;
2445 size_t bytes_needed
= 0;
2447 // Check that only one of them is valid
2448 assert(!user_header
^ !fd
);
2449 assert(user_header_size
);
2451 if ( !(kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) ) {
2456 if ( !(user_header
|| fd
) ) {
2461 // Initialize the cpu map
2462 ret
= kdbg_cpumap_init_internal(kd_ctrl_page
.kdebug_iops
, kd_ctrl_page
.kdebug_cpus
, &cpumap
, &cpumap_size
);
2463 if (ret
!= KERN_SUCCESS
) {
2467 // Check if a thread map is initialized
2472 thrmap_size
= kd_mapcount
* sizeof(kd_threadmap
);
2474 // Setup the header.
2475 // See v3 header description in sys/kdebug.h for more inforamtion.
2477 header
.tag
= RAW_VERSION3
;
2478 header
.sub_tag
= V3_HEADER_VERSION
;
2479 header
.length
= ( sizeof(kd_header_v3
) + cpumap_size
- sizeof(kd_cpumap_header
));
2481 mach_timebase_info_data_t timebase
= {0, 0};
2482 clock_timebase_info(&timebase
);
2483 header
.timebase_numer
= timebase
.numer
;
2484 header
.timebase_denom
= timebase
.denom
;
2485 header
.timestamp
= 0;
2486 header
.walltime_secs
= 0;
2487 header
.walltime_usecs
= 0;
2488 header
.timezone_minuteswest
= 0;
2489 header
.timezone_dst
= 0;
2491 #if defined __LP64__
2497 // If its a buffer, check if we have enough space to copy the header and the maps.
2499 bytes_needed
= header
.length
+ thrmap_size
+ (2 * sizeof(kd_chunk_header_v3
));
2500 if ( !user_header_size
) {
2504 if (*user_header_size
< bytes_needed
) {
2510 // Start writing the header
2512 void *hdr_ptr
= (void *)(((uintptr_t) &header
) + sizeof(kd_chunk_header_v3
));
2513 size_t payload_size
= (sizeof(kd_header_v3
) - sizeof(kd_chunk_header_v3
));
2515 ret
= kdbg_write_v3_chunk_to_fd(RAW_VERSION3
, V3_HEADER_VERSION
, header
.length
, hdr_ptr
, payload_size
, fd
);
2521 if (copyout(&header
, user_header
, sizeof(kd_header_v3
))) {
2525 // Update the user pointer
2526 user_header
+= sizeof(kd_header_v3
);
2529 // Write a cpu map. This is a sub chunk of the header
2530 cpumap
= (uint8_t*)((uintptr_t) cpumap
+ sizeof(kd_cpumap_header
));
2531 size_t payload_size
= (size_t)(cpumap_size
- sizeof(kd_cpumap_header
));
2533 ret
= kdbg_write_v3_chunk_to_fd(V3_CPU_MAP
, V3_CPUMAP_VERSION
, payload_size
, (void *)cpumap
, payload_size
, fd
);
2539 ret
= kdbg_write_v3_chunk_header(user_header
, V3_CPU_MAP
, V3_CPUMAP_VERSION
, payload_size
, NULL
, NULL
);
2543 user_header
+= sizeof(kd_chunk_header_v3
);
2544 if (copyout(cpumap
, user_header
, payload_size
)) {
2548 // Update the user pointer
2549 user_header
+= payload_size
;
2552 // Write a thread map
2554 ret
= kdbg_write_v3_chunk_to_fd(V3_THREAD_MAP
, V3_THRMAP_VERSION
, thrmap_size
, (void *)kd_mapptr
, thrmap_size
, fd
);
2560 ret
= kdbg_write_v3_chunk_header(user_header
, V3_THREAD_MAP
, V3_THRMAP_VERSION
, thrmap_size
, NULL
, NULL
);
2564 user_header
+= sizeof(kd_chunk_header_v3
);
2565 if (copyout(kd_mapptr
, user_header
, thrmap_size
)) {
2569 user_header
+= thrmap_size
;
2573 RAW_file_written
+= bytes_needed
;
2576 *user_header_size
= bytes_needed
;
2579 kmem_free(kernel_map
, (vm_offset_t
)cpumap
, cpumap_size
);
2585 kdbg_readcpumap(user_addr_t user_cpumap
, size_t *user_cpumap_size
)
2587 uint8_t* cpumap
= NULL
;
2588 uint32_t cpumap_size
= 0;
2589 int ret
= KERN_SUCCESS
;
2591 if (kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) {
2592 if (kdbg_cpumap_init_internal(kd_ctrl_page
.kdebug_iops
, kd_ctrl_page
.kdebug_cpus
, &cpumap
, &cpumap_size
) == KERN_SUCCESS
) {
2594 size_t bytes_to_copy
= (*user_cpumap_size
>= cpumap_size
) ? cpumap_size
: *user_cpumap_size
;
2595 if (copyout(cpumap
, user_cpumap
, (size_t)bytes_to_copy
)) {
2599 *user_cpumap_size
= cpumap_size
;
2600 kmem_free(kernel_map
, (vm_offset_t
)cpumap
, cpumap_size
);
2610 kdbg_readcurthrmap(user_addr_t buffer
, size_t *bufsize
)
2612 kd_threadmap
*mapptr
;
2613 unsigned int mapsize
;
2614 unsigned int mapcount
;
2615 unsigned int count
= 0;
2618 count
= *bufsize
/sizeof(kd_threadmap
);
2621 if ( (mapptr
= kdbg_thrmap_init_internal(count
, &mapsize
, &mapcount
)) ) {
2622 if (copyout(mapptr
, buffer
, mapcount
* sizeof(kd_threadmap
)))
2625 *bufsize
= (mapcount
* sizeof(kd_threadmap
));
2627 kmem_free(kernel_map
, (vm_offset_t
)mapptr
, mapsize
);
2635 kdbg_write_v1_plus_header(uint32_t count
, vnode_t vp
, vfs_context_t ctx
)
2643 uint32_t extra_thread_count
= 0;
2644 uint32_t cpumap_size
;
2645 unsigned int mapsize
= kd_mapcount
* sizeof(kd_threadmap
);
2648 * To write a RAW_VERSION1+ file, we
2649 * must embed a cpumap in the "padding"
2650 * used to page align the events following
2651 * the threadmap. If the threadmap happens
2652 * to not require enough padding, we
2653 * artificially increase its footprint
2654 * until it needs enough padding.
2660 pad_size
= PAGE_16KB
- ((sizeof(RAW_header
) + (count
* sizeof(kd_threadmap
))) & PAGE_MASK_64
);
2661 cpumap_size
= sizeof(kd_cpumap_header
) + kd_ctrl_page
.kdebug_cpus
* sizeof(kd_cpumap
);
2663 if (cpumap_size
> pad_size
) {
2664 /* If the cpu map doesn't fit in the current available pad_size,
2665 * we increase the pad_size by 16K. We do this so that the event
2666 * data is always available on a page aligned boundary for both
2667 * 4k and 16k systems. We enforce this alignment for the event
2668 * data so that we can take advantage of optimized file/disk writes.*/
2669 pad_size
+= PAGE_16KB
;
2672 /* The way we are silently embedding a cpumap in the "padding" is by artificially
2673 * increasing the number of thread entries. However, we'll also need to ensure that
2674 * the cpumap is embedded in the last 4K page before when the event data is expected.
2675 * This way the tools can read the data starting the next page boundary on both
2676 * 4K and 16K systems preserving compatibility with older versions of the tools
2678 if (pad_size
> PAGE_4KB
) {
2679 pad_size
-= PAGE_4KB
;
2680 extra_thread_count
= (pad_size
/ sizeof(kd_threadmap
)) + 1;
2683 header
.version_no
= RAW_VERSION1
;
2684 header
.thread_count
= count
+ extra_thread_count
;
2686 clock_get_calendar_microtime(&secs
, &usecs
);
2687 header
.TOD_secs
= secs
;
2688 header
.TOD_usecs
= usecs
;
2690 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)&header
, sizeof(RAW_header
), RAW_file_offset
,
2691 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2694 RAW_file_offset
+= sizeof(RAW_header
);
2696 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)kd_mapptr
, mapsize
, RAW_file_offset
,
2697 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2700 RAW_file_offset
+= mapsize
;
2702 if (extra_thread_count
) {
2703 pad_size
= extra_thread_count
* sizeof(kd_threadmap
);
2704 pad_buf
= (char *)kalloc(pad_size
);
2709 memset(pad_buf
, 0, pad_size
);
2711 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)pad_buf
, pad_size
, RAW_file_offset
,
2712 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2713 kfree(pad_buf
, pad_size
);
2717 RAW_file_offset
+= pad_size
;
2721 pad_size
= PAGE_SIZE
- (RAW_file_offset
& PAGE_MASK_64
);
2723 pad_buf
= (char *)kalloc(pad_size
);
2728 memset(pad_buf
, 0, pad_size
);
2731 * embed a cpumap in the padding bytes.
2732 * older code will skip this.
2733 * newer code will know how to read it.
2735 uint32_t temp
= pad_size
;
2736 if (kdbg_cpumap_init_internal(kd_ctrl_page
.kdebug_iops
, kd_ctrl_page
.kdebug_cpus
, (uint8_t**)&pad_buf
, &temp
) != KERN_SUCCESS
) {
2737 memset(pad_buf
, 0, pad_size
);
2740 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)pad_buf
, pad_size
, RAW_file_offset
,
2741 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2742 kfree(pad_buf
, pad_size
);
2746 RAW_file_offset
+= pad_size
;
2748 RAW_file_written
+= sizeof(RAW_header
) + mapsize
+ pad_size
;
2755 kdbg_readthrmap(user_addr_t buffer
, size_t *number
, vnode_t vp
, vfs_context_t ctx
)
2761 unsigned int mapsize
;
2763 if ((!vp
&& !buffer
) || (vp
&& buffer
)) {
2768 assert((vp
== NULL
) || (ctx
!= NULL
));
2771 count
= avail
/sizeof (kd_threadmap
);
2772 mapsize
= kd_mapcount
* sizeof(kd_threadmap
);
2774 if (count
&& (count
<= kd_mapcount
)) {
2775 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
2776 if (*number
< mapsize
)
2780 ret
= kdbg_write_v1_plus_header(count
, vp
, ctx
);
2785 if (copyout(kd_mapptr
, buffer
, mapsize
))
2800 ret
= kdbg_write_to_vnode((caddr_t
)&count
, sizeof(uint32_t), vp
, ctx
, RAW_file_offset
);
2802 RAW_file_offset
+= sizeof(uint32_t);
2803 RAW_file_written
+= sizeof(uint32_t);
2807 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
)
2809 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
2810 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
2812 kd_mapptr
= (kd_threadmap
*) 0;
2819 kdbg_readthrmap_v3(user_addr_t buffer
, size_t *number
, int fd
)
2824 unsigned int mapsize
;
2826 if ((!fd
&& !buffer
) || (fd
&& buffer
)) {
2833 count
= avail
/sizeof (kd_threadmap
);
2834 mapsize
= kd_mapcount
* sizeof(kd_threadmap
);
2836 if (count
&& (count
<= kd_mapcount
)) {
2837 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
2838 if (*number
< mapsize
) {
2842 ret
= kdbg_write_v3_header(buffer
, number
, fd
);
2856 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
2857 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
2858 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
2860 kd_mapptr
= (kd_threadmap
*) 0;
2868 kdbg_set_nkdbufs(unsigned int value
)
2871 * We allow a maximum buffer size of 50% of either ram or max mapped address, whichever is smaller
2872 * 'value' is the desired number of trace entries
2874 unsigned int max_entries
= (sane_size
/2) / sizeof(kd_buf
);
2876 if (value
<= max_entries
)
2879 return (max_entries
);
2884 kdbg_enable_bg_trace(void)
2888 if (kdlog_bg_trace
== TRUE
&& kdlog_bg_trace_running
== FALSE
&& n_storage_buffers
== 0) {
2889 nkdbufs
= bg_nkdbufs
;
2890 ret
= kdbg_reinit(FALSE
);
2892 kdbg_set_tracing_enabled(TRUE
, KDEBUG_ENABLE_TRACE
);
2893 kdlog_bg_trace_running
= TRUE
;
2895 wakeup(&kdlog_bg_trace
);
2901 kdbg_disable_bg_trace(void)
2903 if (kdlog_bg_trace_running
== TRUE
) {
2904 kdlog_bg_trace_running
= FALSE
;
2912 * This function is provided for the CHUD toolkit only.
2914 * zero disables kdebug_chudhook function call
2915 * non-zero enables kdebug_chudhook function call
2917 * address of the enabled kdebug_chudhook function
2921 kdbg_control_chud(int val
, void *fn
)
2926 /* enable chudhook */
2927 kdebug_chudhook
= fn
;
2928 kdbg_set_flags(SLOW_CHUD
, KDEBUG_ENABLE_CHUD
, TRUE
);
2931 /* disable chudhook */
2932 kdbg_set_flags(SLOW_CHUD
, KDEBUG_ENABLE_CHUD
, FALSE
);
2933 kdebug_chudhook
= 0;
2939 kdbg_control(int *name
, u_int namelen
, user_addr_t where
, size_t *sizep
)
2942 size_t size
= *sizep
;
2943 unsigned int value
= 0;
2945 kbufinfo_t kd_bufinfo
;
2949 if (name
[0] == KERN_KDGETENTROPY
||
2950 name
[0] == KERN_KDWRITETR
||
2951 name
[0] == KERN_KDWRITETR_V3
||
2952 name
[0] == KERN_KDWRITEMAP
||
2953 name
[0] == KERN_KDWRITEMAP_V3
||
2954 name
[0] == KERN_KDEFLAGS
||
2955 name
[0] == KERN_KDDFLAGS
||
2956 name
[0] == KERN_KDENABLE
||
2957 name
[0] == KERN_KDENABLE_BG_TRACE
||
2958 name
[0] == KERN_KDSETBUF
) {
2967 if ( !(kd_ctrl_page
.kdebug_flags
& KDBG_LOCKINIT
))
2970 lck_mtx_lock(kd_trace_mtx_sysctl
);
2975 * Does not alter the global_state_pid
2976 * This is a passive request.
2978 if (size
< sizeof(kd_bufinfo
.nkdbufs
)) {
2980 * There is not enough room to return even
2981 * the first element of the info structure.
2986 kd_bufinfo
.nkdbufs
= nkdbufs
;
2987 kd_bufinfo
.nkdthreads
= kd_mapcount
;
2989 if ( (kd_ctrl_page
.kdebug_slowcheck
& SLOW_NOLOG
) )
2990 kd_bufinfo
.nolog
= 1;
2992 kd_bufinfo
.nolog
= 0;
2994 kd_bufinfo
.flags
= kd_ctrl_page
.kdebug_flags
;
2995 #if defined(__LP64__)
2996 kd_bufinfo
.flags
|= KDBG_LP64
;
2998 kd_bufinfo
.bufid
= global_state_pid
;
3000 if (size
>= sizeof(kd_bufinfo
)) {
3002 * Provide all the info we have
3004 if (copyout(&kd_bufinfo
, where
, sizeof(kd_bufinfo
)))
3008 * For backwards compatibility, only provide
3009 * as much info as there is room for.
3011 if (copyout(&kd_bufinfo
, where
, size
))
3015 case KERN_KDGETENTROPY
: {
3016 /* Obsolescent - just fake with a random buffer */
3017 char *buffer
= (char *) kalloc(size
);
3018 read_frandom((void *) buffer
, size
);
3019 ret
= copyout(buffer
, where
, size
);
3020 kfree(buffer
, size
);
3024 case KERN_KDENABLE_BG_TRACE
:
3025 bg_nkdbufs
= kdbg_set_nkdbufs(value
);
3026 kdlog_bg_trace
= TRUE
;
3027 ret
= kdbg_enable_bg_trace();
3030 case KERN_KDDISABLE_BG_TRACE
:
3031 kdlog_bg_trace
= FALSE
;
3032 kdbg_disable_bg_trace();
3035 case KERN_KDWAIT_BG_TRACE_RESET
:
3036 if (!kdlog_bg_trace
){
3040 wait_result_t wait_result
= assert_wait(&kdlog_bg_trace
, THREAD_ABORTSAFE
);
3041 lck_mtx_unlock(kd_trace_mtx_sysctl
);
3042 if (wait_result
== THREAD_WAITING
)
3043 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
3044 if (wait_result
== THREAD_INTERRUPTED
)
3046 lck_mtx_lock(kd_trace_mtx_sysctl
);
3049 case KERN_KDSET_BG_TYPEFILTER
:
3050 if (!kdlog_bg_trace
|| !kdlog_bg_trace_running
){
3055 if (size
!= KDBG_TYPEFILTER_BITMAP_SIZE
) {
3060 if ((kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) == 0){
3061 if ((ret
= kdbg_enable_typefilter()))
3065 if (copyin(where
, type_filter_bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
)) {
3069 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_TYPEFILTER_CHANGED
, type_filter_bitmap
);
3073 if ((curproc
= current_proc()) != NULL
)
3074 curpid
= curproc
->p_pid
;
3079 if (global_state_pid
== -1)
3080 global_state_pid
= curpid
;
3081 else if (global_state_pid
!= curpid
) {
3082 if ((p
= proc_find(global_state_pid
)) == NULL
) {
3084 * The global pid no longer exists
3086 global_state_pid
= curpid
;
3089 * The global pid exists, deny this request
3100 kdbg_disable_bg_trace();
3102 value
&= KDBG_USERFLAGS
;
3103 kd_ctrl_page
.kdebug_flags
|= value
;
3106 kdbg_disable_bg_trace();
3108 value
&= KDBG_USERFLAGS
;
3109 kd_ctrl_page
.kdebug_flags
&= ~value
;
3113 * Enable tracing mechanism. Two types:
3114 * KDEBUG_TRACE is the standard one,
3115 * and KDEBUG_PPT which is a carefully
3116 * chosen subset to avoid performance impact.
3120 * enable only if buffer is initialized
3122 if (!(kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) ||
3123 !(value
== KDEBUG_ENABLE_TRACE
|| value
== KDEBUG_ENABLE_PPT
)) {
3129 kdbg_set_tracing_enabled(TRUE
, value
);
3133 kdbg_set_tracing_enabled(FALSE
, 0);
3137 kdbg_disable_bg_trace();
3139 nkdbufs
= kdbg_set_nkdbufs(value
);
3142 kdbg_disable_bg_trace();
3144 ret
= kdbg_reinit(FALSE
);
3148 ret
= kdbg_enable_bg_trace();
3151 if(size
< sizeof(kd_regtype
)) {
3155 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3159 kdbg_disable_bg_trace();
3161 ret
= kdbg_setreg(&kd_Reg
);
3164 kdbg_disable_bg_trace();
3168 ret
= kdbg_read(where
, sizep
, NULL
, NULL
, RAW_VERSION1
);
3170 case KERN_KDWRITETR
:
3171 case KERN_KDWRITETR_V3
:
3172 case KERN_KDWRITEMAP
:
3173 case KERN_KDWRITEMAP_V3
:
3175 struct vfs_context context
;
3176 struct fileproc
*fp
;
3181 if (name
[0] == KERN_KDWRITETR
|| name
[0] == KERN_KDWRITETR_V3
) {
3183 int wait_result
= THREAD_AWAKENED
;
3188 ns
= ((u_int64_t
)*sizep
) * (u_int64_t
)(1000 * 1000);
3189 nanoseconds_to_absolutetime(ns
, &abstime
);
3190 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
3194 s
= ml_set_interrupts_enabled(FALSE
);
3195 lck_spin_lock(kdw_spin_lock
);
3197 while (wait_result
== THREAD_AWAKENED
&& kd_ctrl_page
.kds_inuse_count
< n_storage_threshold
) {
3202 wait_result
= lck_spin_sleep_deadline(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
, abstime
);
3204 wait_result
= lck_spin_sleep(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
);
3208 lck_spin_unlock(kdw_spin_lock
);
3209 ml_set_interrupts_enabled(s
);
3215 if ( (ret
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3219 context
.vc_thread
= current_thread();
3220 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
3222 if (FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_VNODE
) {
3223 fp_drop(p
, fd
, fp
, 1);
3229 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
3232 if ((ret
= vnode_getwithref(vp
)) == 0) {
3233 RAW_file_offset
= fp
->f_fglob
->fg_offset
;
3234 if (name
[0] == KERN_KDWRITETR
|| name
[0] == KERN_KDWRITETR_V3
) {
3235 number
= nkdbufs
* sizeof(kd_buf
);
3237 KERNEL_DEBUG_CONSTANT(TRACE_WRITING_EVENTS
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
3238 if (name
[0] == KERN_KDWRITETR_V3
)
3239 ret
= kdbg_read(0, &number
, vp
, &context
, RAW_VERSION3
);
3241 ret
= kdbg_read(0, &number
, vp
, &context
, RAW_VERSION1
);
3242 KERNEL_DEBUG_CONSTANT(TRACE_WRITING_EVENTS
| DBG_FUNC_END
, number
, 0, 0, 0, 0);
3246 number
= kd_mapcount
* sizeof(kd_threadmap
);
3247 if (name
[0] == KERN_KDWRITEMAP_V3
)
3248 kdbg_readthrmap_v3(0, &number
, fd
);
3250 kdbg_readthrmap(0, &number
, vp
, &context
);
3252 fp
->f_fglob
->fg_offset
= RAW_file_offset
;
3255 fp_drop(p
, fd
, fp
, 0);
3259 case KERN_KDBUFWAIT
:
3261 /* WRITETR lite -- just block until there's data */
3263 int wait_result
= THREAD_AWAKENED
;
3268 kdbg_disable_bg_trace();
3272 ns
= ((u_int64_t
)*sizep
) * (u_int64_t
)(1000 * 1000);
3273 nanoseconds_to_absolutetime(ns
, &abstime
);
3274 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
3278 s
= ml_set_interrupts_enabled(FALSE
);
3280 panic("trying to wait with interrupts off");
3281 lck_spin_lock(kdw_spin_lock
);
3283 /* drop the mutex so don't exclude others from
3286 lck_mtx_unlock(kd_trace_mtx_sysctl
);
3288 while (wait_result
== THREAD_AWAKENED
&&
3289 kd_ctrl_page
.kds_inuse_count
< n_storage_threshold
) {
3294 wait_result
= lck_spin_sleep_deadline(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
, abstime
);
3296 wait_result
= lck_spin_sleep(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
);
3301 /* check the count under the spinlock */
3302 number
= (kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
);
3304 lck_spin_unlock(kdw_spin_lock
);
3305 ml_set_interrupts_enabled(s
);
3307 /* pick the mutex back up again */
3308 lck_mtx_lock(kd_trace_mtx_sysctl
);
3310 /* write out whether we've exceeded the threshold */
3315 if (size
< sizeof(kd_regtype
)) {
3319 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3323 kdbg_disable_bg_trace();
3325 ret
= kdbg_setpid(&kd_Reg
);
3328 if (size
< sizeof(kd_regtype
)) {
3332 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3336 kdbg_disable_bg_trace();
3338 ret
= kdbg_setpidex(&kd_Reg
);
3341 ret
= kdbg_readcpumap(where
, sizep
);
3344 ret
= kdbg_readthrmap(where
, sizep
, NULL
, NULL
);
3346 case KERN_KDREADCURTHRMAP
:
3347 ret
= kdbg_readcurthrmap(where
, sizep
);
3349 case KERN_KDSETRTCDEC
:
3350 if (size
< sizeof(kd_regtype
)) {
3354 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3358 kdbg_disable_bg_trace();
3360 ret
= kdbg_setrtcdec(&kd_Reg
);
3362 case KERN_KDSET_TYPEFILTER
:
3363 kdbg_disable_bg_trace();
3365 if (size
!= KDBG_TYPEFILTER_BITMAP_SIZE
) {
3370 if ((kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) == 0){
3371 if ((ret
= kdbg_enable_typefilter()))
3375 if (copyin(where
, type_filter_bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
)) {
3379 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_TYPEFILTER_CHANGED
, type_filter_bitmap
);
3385 lck_mtx_unlock(kd_trace_mtx_sysctl
);
3392 * This code can run for the most part concurrently with kernel_debug_internal()...
3393 * 'release_storage_unit' will take the kds_spin_lock which may cause us to briefly
3394 * synchronize with the recording side of this puzzle... otherwise, we are able to
3395 * move through the lists w/o use of any locks
3398 kdbg_read(user_addr_t buffer
, size_t *number
, vnode_t vp
, vfs_context_t ctx
, uint32_t file_version
)
3401 unsigned int cpu
, min_cpu
;
3402 uint64_t mintime
, t
, barrier
= 0;
3408 struct kd_storage
*kdsp_actual
;
3409 struct kd_bufinfo
*kdbp
;
3410 struct kd_bufinfo
*min_kdbp
;
3411 uint32_t tempbuf_count
;
3412 uint32_t tempbuf_number
;
3413 uint32_t old_kdebug_flags
;
3414 uint32_t old_kdebug_slowcheck
;
3415 boolean_t lostevents
= FALSE
;
3416 boolean_t out_of_events
= FALSE
;
3419 count
= *number
/sizeof(kd_buf
);
3422 if (count
== 0 || !(kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) || kdcopybuf
== 0)
3425 memset(&lostevent
, 0, sizeof(lostevent
));
3426 lostevent
.debugid
= TRACE_LOST_EVENTS
;
3428 /* Capture timestamp. Only sort events that have occured before the timestamp.
3429 * Since the iop is being flushed here, its possible that events occur on the AP
3430 * while running live tracing. If we are disabled, no new events should
3434 if (kd_ctrl_page
.enabled
)
3436 // timestamp is non-zero value
3437 barrier
= mach_absolute_time() & KDBG_TIMESTAMP_MASK
;
3440 // Request each IOP to provide us with up to date entries before merging buffers together.
3441 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_SYNC_FLUSH
, NULL
);
3444 * because we hold kd_trace_mtx_sysctl, no other control threads can
3445 * be playing with kdebug_flags... the code that cuts new events could
3446 * be running, but it grabs kds_spin_lock if it needs to acquire a new
3447 * storage chunk which is where it examines kdebug_flags... it its adding
3448 * to the same chunk we're reading from, no problem...
3451 disable_wrap(&old_kdebug_slowcheck
, &old_kdebug_flags
);
3453 if (count
> nkdbufs
)
3456 if ((tempbuf_count
= count
) > KDCOPYBUF_COUNT
)
3457 tempbuf_count
= KDCOPYBUF_COUNT
;
3460 tempbuf
= kdcopybuf
;
3464 while (tempbuf_count
) {
3465 mintime
= 0xffffffffffffffffULL
;
3470 for (cpu
= 0, kdbp
= &kdbip
[0]; cpu
< kd_ctrl_page
.kdebug_cpus
; cpu
++, kdbp
++) {
3472 // Find one with raw data
3473 if ((kdsp
= kdbp
->kd_list_head
).raw
== KDS_PTR_NULL
)
3475 /* Debugging aid: maintain a copy of the "kdsp"
3478 volatile union kds_ptr kdsp_shadow
;
3482 // Get from cpu data to buffer header to buffer
3483 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
3485 volatile struct kd_storage
*kdsp_actual_shadow
;
3487 kdsp_actual_shadow
= kdsp_actual
;
3489 // See if there are actual data left in this buffer
3490 rcursor
= kdsp_actual
->kds_readlast
;
3492 if (rcursor
== kdsp_actual
->kds_bufindx
)
3495 t
= kdbg_get_timestamp(&kdsp_actual
->kds_records
[rcursor
]);
3497 if ((t
> barrier
) && (barrier
> 0)) {
3499 * Need to wait to flush iop again before we
3500 * sort any more data from the buffers
3502 out_of_events
= TRUE
;
3505 if (t
< kdsp_actual
->kds_timestamp
) {
3507 * indicates we've not yet completed filling
3509 * this should only occur when we're looking
3510 * at the buf that the record head is utilizing
3511 * we'll pick these events up on the next
3513 * we bail at this point so that we don't
3514 * get an out-of-order timestream by continuing
3515 * to read events from the other CPUs' timestream(s)
3517 out_of_events
= TRUE
;
3526 if (min_kdbp
== NULL
|| out_of_events
== TRUE
) {
3528 * all buffers ran empty
3530 out_of_events
= TRUE
;
3535 kdsp
= min_kdbp
->kd_list_head
;
3536 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
3538 if (kdsp_actual
->kds_lostevents
== TRUE
) {
3539 kdbg_set_timestamp_and_cpu(&lostevent
, kdsp_actual
->kds_records
[kdsp_actual
->kds_readlast
].timestamp
, min_cpu
);
3540 *tempbuf
= lostevent
;
3542 kdsp_actual
->kds_lostevents
= FALSE
;
3549 *tempbuf
= kdsp_actual
->kds_records
[kdsp_actual
->kds_readlast
++];
3551 if (kdsp_actual
->kds_readlast
== EVENTS_PER_STORAGE_UNIT
)
3552 release_storage_unit(min_cpu
, kdsp
.raw
);
3555 * Watch for out of order timestamps
3557 if (mintime
< min_kdbp
->kd_prev_timebase
) {
3559 * if so, use the previous timestamp + 1 cycle
3561 min_kdbp
->kd_prev_timebase
++;
3562 kdbg_set_timestamp_and_cpu(tempbuf
, min_kdbp
->kd_prev_timebase
, kdbg_get_cpu(tempbuf
));
3564 min_kdbp
->kd_prev_timebase
= mintime
;
3570 if ((RAW_file_written
+= sizeof(kd_buf
)) >= RAW_FLUSH_SIZE
)
3573 if (tempbuf_number
) {
3574 if (file_version
== RAW_VERSION3
) {
3575 if ( !(kdbg_write_v3_event_chunk_header(buffer
, V3_RAW_EVENTS
, (tempbuf_number
* sizeof(kd_buf
)), vp
, ctx
))) {
3580 buffer
+= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t));
3582 assert(count
>= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t)));
3583 count
-= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t));
3584 *number
+= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t));
3587 size_t write_size
= tempbuf_number
* sizeof(kd_buf
);
3588 error
= kdbg_write_to_vnode((caddr_t
)kdcopybuf
, write_size
, vp
, ctx
, RAW_file_offset
);
3590 RAW_file_offset
+= write_size
;
3592 if (RAW_file_written
>= RAW_FLUSH_SIZE
) {
3593 cluster_push(vp
, 0);
3595 RAW_file_written
= 0;
3598 error
= copyout(kdcopybuf
, buffer
, tempbuf_number
* sizeof(kd_buf
));
3599 buffer
+= (tempbuf_number
* sizeof(kd_buf
));
3607 count
-= tempbuf_number
;
3608 *number
+= tempbuf_number
;
3610 if (out_of_events
== TRUE
)
3612 * all trace buffers are empty
3616 if ((tempbuf_count
= count
) > KDCOPYBUF_COUNT
)
3617 tempbuf_count
= KDCOPYBUF_COUNT
;
3619 if ( !(old_kdebug_flags
& KDBG_NOWRAP
)) {
3620 enable_wrap(old_kdebug_slowcheck
, lostevents
);
3626 unsigned char *getProcName(struct proc
*proc
);
3627 unsigned char *getProcName(struct proc
*proc
) {
3629 return (unsigned char *) &proc
->p_comm
; /* Return pointer to the proc name */
3634 stackshot_kern_return_to_bsd_error(kern_return_t kr
)
3639 case KERN_RESOURCE_SHORTAGE
:
3643 case KERN_NO_ACCESS
:
3645 case KERN_MEMORY_PRESENT
:
3647 case KERN_NOT_SUPPORTED
:
3649 case KERN_NOT_IN_SET
:
3658 * DEPRECATION WARNING: THIS SYSCALL IS BEING REPLACED WITH SYS_stack_snapshot_with_config and SYS_microstackshot.
3660 * stack_snapshot: Obtains a coherent set of stack traces for all threads
3661 * on the system, tracing both kernel and user stacks
3662 * where available. Uses machine specific trace routines
3663 * for ppc, ppc64 and x86.
3664 * Inputs: uap->pid - process id of process to be traced, or -1
3665 * for the entire system
3666 * uap->tracebuf - address of the user space destination
3668 * uap->tracebuf_size - size of the user space trace buffer
3669 * uap->options - various options, including the maximum
3670 * number of frames to trace.
3671 * Outputs: EPERM if the caller is not privileged
3672 * EINVAL if the supplied trace buffer isn't sanely sized
3673 * ENOMEM if we don't have enough memory to satisfy the
3675 * ENOENT if the target pid isn't found
3676 * ENOSPC if the supplied buffer is insufficient
3677 * *retval contains the number of bytes traced, if successful
3678 * and -1 otherwise. If the request failed due to
3679 * tracebuffer exhaustion, we copyout as much as possible.
3682 stack_snapshot(struct proc
*p
, register struct stack_snapshot_args
*uap
, int32_t *retval
) {
3686 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
3689 kr
= stack_snapshot2(uap
->pid
, uap
->tracebuf
, uap
->tracebuf_size
, uap
->flags
, retval
);
3690 return stackshot_kern_return_to_bsd_error(kr
);
3694 * stack_snapshot_with_config: Obtains a coherent set of stack traces for specified threads on the sysem,
3695 * tracing both kernel and user stacks where available. Allocates a buffer from the
3696 * kernel and maps the buffer into the calling task's address space.
3698 * Inputs: uap->stackshot_config_version - version of the stackshot config that is being passed
3699 * uap->stackshot_config - pointer to the stackshot config
3700 * uap->stackshot_config_size- size of the stackshot config being passed
3701 * Outputs: EINVAL if there is a problem with the arguments
3702 * EFAULT if we failed to copy in the arguments succesfully
3703 * EPERM if the caller is not privileged
3704 * ENOTSUP if the caller is passing a version of arguments that is not supported by the kernel
3705 * (indicates libsyscall:kernel mismatch) or if the caller is requesting unsupported flags
3706 * ENOENT if the caller is requesting an existing buffer that doesn't exist or if the
3707 * requested PID isn't found
3708 * ENOMEM if the kernel is unable to allocate enough memory to serve the request
3709 * ENOSPC if there isn't enough space in the caller's address space to remap the buffer
3710 * ESRCH if the target PID isn't found
3711 * returns KERN_SUCCESS on success
3714 stack_snapshot_with_config(struct proc
*p
, struct stack_snapshot_with_config_args
*uap
, __unused
int *retval
)
3719 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
3722 if((void*)uap
->stackshot_config
== NULL
) {
3726 switch (uap
->stackshot_config_version
) {
3727 case STACKSHOT_CONFIG_TYPE
:
3728 if (uap
->stackshot_config_size
!= sizeof(stackshot_config_t
)) {
3731 stackshot_config_t config
;
3732 error
= copyin(uap
->stackshot_config
, &config
, sizeof(stackshot_config_t
));
3733 if (error
!= KERN_SUCCESS
)
3737 kr
= kern_stack_snapshot_internal(uap
->stackshot_config_version
, &config
, sizeof(stackshot_config_t
), TRUE
);
3738 return stackshot_kern_return_to_bsd_error(kr
);
3744 #if CONFIG_TELEMETRY
3746 * microstackshot: Catch all system call for microstackshot related operations, including
3747 * enabling/disabling both global and windowed microstackshots as well
3748 * as retrieving windowed or global stackshots and the boot profile.
3749 * Inputs: uap->tracebuf - address of the user space destination
3751 * uap->tracebuf_size - size of the user space trace buffer
3752 * uap->flags - various flags
3753 * Outputs: EPERM if the caller is not privileged
3754 * EINVAL if the supplied mss_args is NULL, mss_args.tracebuf is NULL or mss_args.tracebuf_size is not sane
3755 * ENOMEM if we don't have enough memory to satisfy the request
3756 * *retval contains the number of bytes traced, if successful
3760 microstackshot(struct proc
*p
, struct microstackshot_args
*uap
, int32_t *retval
)
3765 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
3768 kr
= stack_microstackshot(uap
->tracebuf
, uap
->tracebuf_size
, uap
->flags
, retval
);
3769 return stackshot_kern_return_to_bsd_error(kr
);
3771 #endif /* CONFIG_TELEMETRY */
3774 * kern_stack_snapshot_with_reason: Obtains a coherent set of stack traces for specified threads on the sysem,
3775 * tracing both kernel and user stacks where available. Allocates a buffer from the
3776 * kernel and stores the address of this buffer.
3778 * Inputs: reason - the reason for triggering a stackshot (unused at the moment, but in the
3779 * future will be saved in the stackshot)
3780 * Outputs: EINVAL/ENOTSUP if there is a problem with the arguments
3781 * EPERM if the caller doesn't pass at least one KERNEL stackshot flag
3782 * ENOMEM if the kernel is unable to allocate enough memory to serve the request
3783 * ESRCH if the target PID isn't found
3784 * returns KERN_SUCCESS on success
3787 kern_stack_snapshot_with_reason(__unused
char *reason
)
3789 stackshot_config_t config
;
3793 config
.sc_flags
= (STACKSHOT_SAVE_LOADINFO
| STACKSHOT_GET_GLOBAL_MEM_STATS
| STACKSHOT_SAVE_IN_KERNEL_BUFFER
|
3794 STACKSHOT_KCDATA_FORMAT
);
3795 config
.sc_since_timestamp
= 0;
3796 config
.sc_out_buffer_addr
= 0;
3797 config
.sc_out_size_addr
= 0;
3799 kr
= kern_stack_snapshot_internal(STACKSHOT_CONFIG_TYPE
, &config
, sizeof(stackshot_config_t
), FALSE
);
3800 return stackshot_kern_return_to_bsd_error(kr
);
3804 * stack_snapshot_from_kernel: Stackshot function for kernel consumers who have their own buffer.
3806 * Inputs: pid - the PID to be traced or -1 for the whole system
3807 * buf - a pointer to the buffer where the stackshot should be written
3808 * size - the size of the buffer
3809 * flags - flags to be passed to the stackshot
3810 * *bytes_traced - a pointer to be filled with the length of the stackshot
3811 * Outputs: -1 if there is a problem with the arguments
3812 * the error returned by the stackshot code otherwise
3815 stack_snapshot_from_kernel(pid_t pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *bytes_traced
)
3819 kr
= stack_snapshot_from_kernel_internal(pid
, buf
, size
, flags
, bytes_traced
);
3820 if (kr
== KERN_FAILURE
) {
3828 start_kern_tracing(unsigned int new_nkdbufs
, boolean_t need_map
)
3833 nkdbufs
= kdbg_set_nkdbufs(new_nkdbufs
);
3836 kernel_debug_string_simple("start_kern_tracing");
3838 if (0 == kdbg_reinit(TRUE
)) {
3840 if (need_map
== TRUE
) {
3841 uint32_t old1
, old2
;
3845 disable_wrap(&old1
, &old2
);
3848 /* Hold off interrupts until the early traces are cut */
3849 boolean_t s
= ml_set_interrupts_enabled(FALSE
);
3851 kdbg_set_tracing_enabled(
3854 (KDEBUG_ENABLE_TRACE
| KDEBUG_ENABLE_SERIAL
) :
3855 KDEBUG_ENABLE_TRACE
);
3858 * Transfer all very early events from the static buffer
3859 * into the real buffers.
3861 kernel_debug_early_end();
3863 ml_set_interrupts_enabled(s
);
3865 printf("kernel tracing started\n");
3866 #if KDEBUG_MOJO_TRACE
3867 if (kdebug_serial
) {
3868 printf("serial output enabled with %lu named events\n",
3869 sizeof(kd_events
)/sizeof(kd_event_t
));
3873 printf("error from kdbg_reinit, kernel tracing not started\n");
3878 start_kern_tracing_with_typefilter(unsigned int new_nkdbufs
,
3880 unsigned int typefilter
)
3882 /* startup tracing */
3883 start_kern_tracing(new_nkdbufs
, need_map
);
3885 /* check that tracing was actually enabled */
3886 if (!(kdebug_enable
& KDEBUG_ENABLE_TRACE
))
3889 /* setup the typefiltering */
3890 if (0 == kdbg_enable_typefilter())
3891 setbit(type_filter_bitmap
,
3892 typefilter
& (KDBG_CSC_MASK
>> KDBG_CSC_OFFSET
));
3896 kdbg_dump_trace_to_file(const char *filename
)
3904 if ( !(kdebug_enable
& KDEBUG_ENABLE_TRACE
))
3907 if (global_state_pid
!= -1) {
3908 if ((proc_find(global_state_pid
)) != NULL
) {
3910 * The global pid exists, we're running
3911 * due to fs_usage, latency, etc...
3912 * don't cut the panic/shutdown trace file
3913 * Disable tracing from this point to avoid
3917 kd_ctrl_page
.enabled
= 0;
3918 commpage_update_kdebug_enable();
3922 KERNEL_DEBUG_CONSTANT(TRACE_PANIC
| DBG_FUNC_NONE
, 0, 0, 0, 0, 0);
3925 kd_ctrl_page
.enabled
= 0;
3926 commpage_update_kdebug_enable();
3928 ctx
= vfs_context_kernel();
3930 if ((error
= vnode_open(filename
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), 0600, 0, &vp
, ctx
)))
3933 number
= kd_mapcount
* sizeof(kd_threadmap
);
3934 kdbg_readthrmap(0, &number
, vp
, ctx
);
3936 number
= nkdbufs
*sizeof(kd_buf
);
3937 kdbg_read(0, &number
, vp
, ctx
, RAW_VERSION1
);
3939 vnode_close(vp
, FWRITE
, ctx
);
3941 sync(current_proc(), (void *)NULL
, (int *)NULL
);
3944 /* Helper function for filling in the BSD name for an address space
3945 * Defined here because the machine bindings know only Mach threads
3946 * and nothing about BSD processes.
3948 * FIXME: need to grab a lock during this?
3950 void kdbg_get_task_name(char* name_buf
, int len
, task_t task
)
3954 /* Note: we can't use thread->task (and functions that rely on it) here
3955 * because it hasn't been initialized yet when this function is called.
3956 * We use the explicitly-passed task parameter instead.
3958 proc
= get_bsdtask_info(task
);
3959 if (proc
!= PROC_NULL
)
3960 snprintf(name_buf
, len
, "%s/%d", proc
->p_comm
, proc
->p_pid
);
3962 snprintf(name_buf
, len
, "%p [!bsd]", task
);
3965 #if KDEBUG_MOJO_TRACE
3967 binary_search(uint32_t id
)
3972 high
= sizeof(kd_events
)/sizeof(kd_event_t
) - 1;
3976 mid
= (low
+ high
) / 2;
3979 return NULL
; /* failed */
3980 else if ( low
+ 1 >= high
) {
3981 /* We have a match */
3982 if (kd_events
[high
].id
== id
)
3983 return &kd_events
[high
];
3984 else if (kd_events
[low
].id
== id
)
3985 return &kd_events
[low
];
3987 return NULL
; /* search failed */
3989 else if (id
< kd_events
[mid
].id
)
3997 * Look up event id to get name string.
3998 * Using a per-cpu cache of a single entry
3999 * before resorting to a binary search of the full table.
4002 static kd_event_t
*last_hit
[MAX_CPUS
];
4004 event_lookup_cache(uint32_t cpu
, uint32_t id
)
4006 if (last_hit
[cpu
] == NULL
|| last_hit
[cpu
]->id
!= id
)
4007 last_hit
[cpu
] = binary_search(id
);
4008 return last_hit
[cpu
];
4011 static uint64_t kd_last_timstamp
;
4014 kdebug_serial_print(
4025 char kprintf_line
[192];
4027 uint64_t us
= timestamp
/ NSEC_PER_USEC
;
4028 uint64_t us_tenth
= (timestamp
% NSEC_PER_USEC
) / 100;
4029 uint64_t delta
= timestamp
- kd_last_timstamp
;
4030 uint64_t delta_us
= delta
/ NSEC_PER_USEC
;
4031 uint64_t delta_us_tenth
= (delta
% NSEC_PER_USEC
) / 100;
4032 uint32_t event_id
= debugid
& KDBG_EVENTID_MASK
;
4033 const char *command
;
4038 /* event time and delta from last */
4039 snprintf(kprintf_line
, sizeof(kprintf_line
),
4040 "%11llu.%1llu %8llu.%1llu ",
4041 us
, us_tenth
, delta_us
, delta_us_tenth
);
4044 /* event (id or name) - start prefixed by "[", end postfixed by "]" */
4045 bra
= (debugid
& DBG_FUNC_START
) ? "[" : " ";
4046 ket
= (debugid
& DBG_FUNC_END
) ? "]" : " ";
4047 ep
= event_lookup_cache(cpunum
, event_id
);
4049 if (strlen(ep
->name
) < sizeof(event
) - 3)
4050 snprintf(event
, sizeof(event
), "%s%s%s",
4051 bra
, ep
->name
, ket
);
4053 snprintf(event
, sizeof(event
), "%s%x(name too long)%s",
4054 bra
, event_id
, ket
);
4056 snprintf(event
, sizeof(event
), "%s%x%s",
4057 bra
, event_id
, ket
);
4059 snprintf(kprintf_line
+ strlen(kprintf_line
),
4060 sizeof(kprintf_line
) - strlen(kprintf_line
),
4063 /* arg1 .. arg4 with special cases for strings */
4066 case VFS_LOOKUP_DONE
:
4067 if (debugid
& DBG_FUNC_START
) {
4068 /* arg1 hex then arg2..arg4 chars */
4069 snprintf(kprintf_line
+ strlen(kprintf_line
),
4070 sizeof(kprintf_line
) - strlen(kprintf_line
),
4071 "%-16lx %-8s%-8s%-8s ",
4072 arg1
, (char*)&arg2
, (char*)&arg3
, (char*)&arg4
);
4075 /* else fall through for arg1..arg4 chars */
4076 case TRACE_STRING_EXEC
:
4077 case TRACE_STRING_NEWTHREAD
:
4078 case TRACE_INFO_STRING
:
4079 snprintf(kprintf_line
+ strlen(kprintf_line
),
4080 sizeof(kprintf_line
) - strlen(kprintf_line
),
4081 "%-8s%-8s%-8s%-8s ",
4082 (char*)&arg1
, (char*)&arg2
, (char*)&arg3
, (char*)&arg4
);
4085 snprintf(kprintf_line
+ strlen(kprintf_line
),
4086 sizeof(kprintf_line
) - strlen(kprintf_line
),
4087 "%-16lx %-16lx %-16lx %-16lx",
4088 arg1
, arg2
, arg3
, arg4
);
4091 /* threadid, cpu and command name */
4092 if (threadid
== (uintptr_t)thread_tid(current_thread()) &&
4094 current_proc()->p_comm
[0])
4095 command
= current_proc()->p_comm
;
4098 snprintf(kprintf_line
+ strlen(kprintf_line
),
4099 sizeof(kprintf_line
) - strlen(kprintf_line
),
4100 " %-16lx %-2d %s\n",
4101 threadid
, cpunum
, command
);
4103 kprintf("%s", kprintf_line
);
4104 kd_last_timstamp
= timestamp
;