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_getreg(kd_regtype
*);
138 int kdbg_setreg(kd_regtype
*);
139 int kdbg_setrtcdec(kd_regtype
*);
140 int kdbg_setpidex(kd_regtype
*);
141 int kdbg_setpid(kd_regtype
*);
142 void kdbg_thrmap_init(void);
143 int kdbg_reinit(boolean_t
);
144 int kdbg_bootstrap(boolean_t
);
146 int kdbg_cpumap_init_internal(kd_iop_t
* iops
, uint32_t cpu_count
,
147 uint8_t** cpumap
, uint32_t* cpumap_size
);
149 kd_threadmap
* kdbg_thrmap_init_internal(unsigned int count
,
150 unsigned int *mapsize
,
151 unsigned int *mapcount
);
153 static boolean_t
kdebug_current_proc_enabled(uint32_t debugid
);
154 static boolean_t
kdebug_debugid_enabled(uint32_t debugid
);
155 static errno_t
kdebug_check_trace_string(uint32_t debugid
, uint64_t str_id
);
157 int kdbg_write_v3_header(user_addr_t
, size_t *, int);
158 int kdbg_write_v3_chunk_header(user_addr_t buffer
, uint32_t tag
,
159 uint32_t sub_tag
, uint64_t length
,
160 vnode_t vp
, vfs_context_t ctx
);
162 user_addr_t
kdbg_write_v3_event_chunk_header(user_addr_t buffer
, uint32_t tag
,
163 uint64_t length
, vnode_t vp
,
166 static int kdbg_enable_typefilter(void);
167 static int kdbg_disable_typefilter(void);
168 static int kdbg_allocate_typefilter(void);
169 static int kdbg_deallocate_typefilter(void);
171 static int create_buffers(boolean_t
);
172 static void delete_buffers(void);
174 extern void IOSleep(int);
176 /* trace enable status */
177 unsigned int kdebug_enable
= 0;
179 /* A static buffer to record events prior to the start of regular logging */
180 #define KD_EARLY_BUFFER_MAX 64
181 static kd_buf kd_early_buffer
[KD_EARLY_BUFFER_MAX
];
182 static int kd_early_index
= 0;
183 static boolean_t kd_early_overflow
= FALSE
;
185 #define SLOW_NOLOG 0x01
186 #define SLOW_CHECKS 0x02
187 #define SLOW_CHUD 0x08
189 #define EVENTS_PER_STORAGE_UNIT 2048
190 #define MIN_STORAGE_UNITS_PER_CPU 4
192 #define POINTER_FROM_KDS_PTR(x) (&kd_bufs[x.buffer_index].kdsb_addr[x.offset])
196 uint32_t buffer_index
:21;
203 union kds_ptr kds_next
;
204 uint32_t kds_bufindx
;
206 uint32_t kds_readlast
;
207 boolean_t kds_lostevents
;
208 uint64_t kds_timestamp
;
210 kd_buf kds_records
[EVENTS_PER_STORAGE_UNIT
];
213 #define MAX_BUFFER_SIZE (1024 * 1024 * 128)
214 #define N_STORAGE_UNITS_PER_BUFFER (MAX_BUFFER_SIZE / sizeof(struct kd_storage))
216 struct kd_storage_buffers
{
217 struct kd_storage
*kdsb_addr
;
221 #define KDS_PTR_NULL 0xffffffff
222 struct kd_storage_buffers
*kd_bufs
= NULL
;
223 int n_storage_units
= 0;
224 int n_storage_buffers
= 0;
225 int n_storage_threshold
= 0;
230 union kds_ptr kd_list_head
;
231 union kds_ptr kd_list_tail
;
232 boolean_t kd_lostevents
;
234 uint64_t kd_prev_timebase
;
236 } __attribute__(( aligned(MAX_CPU_CACHE_LINE_SIZE
) ));
240 * In principle, this control block can be shared in DRAM with other
241 * coprocessors and runtimes, for configuring what tracing is enabled.
243 struct kd_ctrl_page_t
{
244 union kds_ptr kds_free_list
;
248 uint32_t kdebug_flags
;
249 uint32_t kdebug_slowcheck
;
251 * The number of kd_bufinfo structs allocated may not match the current
252 * number of active cpus. We capture the iops list head at initialization
253 * which we could use to calculate the number of cpus we allocated data for,
254 * unless it happens to be null. To avoid that case, we explicitly also
255 * capture a cpu count.
257 kd_iop_t
* kdebug_iops
;
258 uint32_t kdebug_cpus
;
259 } kd_ctrl_page
= { .kds_free_list
= {.raw
= KDS_PTR_NULL
}, .kdebug_slowcheck
= SLOW_NOLOG
};
263 struct kd_bufinfo
*kdbip
= NULL
;
265 #define KDCOPYBUF_COUNT 8192
266 #define KDCOPYBUF_SIZE (KDCOPYBUF_COUNT * sizeof(kd_buf))
268 #define PAGE_4KB 4096
269 #define PAGE_16KB 16384
271 kd_buf
*kdcopybuf
= NULL
;
273 boolean_t kdlog_bg_trace
= FALSE
;
274 boolean_t kdlog_bg_trace_running
= FALSE
;
275 unsigned int bg_nkdbufs
= 0;
277 unsigned int nkdbufs
= 0;
278 unsigned int kdlog_beg
=0;
279 unsigned int kdlog_end
=0;
280 unsigned int kdlog_value1
=0;
281 unsigned int kdlog_value2
=0;
282 unsigned int kdlog_value3
=0;
283 unsigned int kdlog_value4
=0;
285 static lck_spin_t
* kdw_spin_lock
;
286 static lck_spin_t
* kds_spin_lock
;
287 static lck_mtx_t
* kd_trace_mtx_sysctl
;
288 static lck_grp_t
* kd_trace_mtx_sysctl_grp
;
289 static lck_attr_t
* kd_trace_mtx_sysctl_attr
;
290 static lck_grp_attr_t
*kd_trace_mtx_sysctl_grp_attr
;
292 extern kern_return_t
stack_snapshot2(int pid
, user_addr_t tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, int32_t *retval
);
295 extern kern_return_t
stack_microstackshot(user_addr_t tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, int32_t *retval
);
296 #endif /* CONFIG_TELEMETRY */
298 extern kern_return_t
kern_stack_snapshot_with_reason(char* reason
);
300 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
);
302 extern kern_return_t
stack_snapshot_from_kernel_internal(int pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *bytes_traced
);
304 int stack_snapshot_from_kernel(pid_t pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *bytes_traced
);
306 kd_threadmap
*kd_mapptr
= 0;
307 unsigned int kd_mapsize
= 0;
308 unsigned int kd_mapcount
= 0;
310 off_t RAW_file_offset
= 0;
311 int RAW_file_written
= 0;
313 #define RAW_FLUSH_SIZE (2 * 1024 * 1024)
315 pid_t global_state_pid
= -1; /* Used to control exclusive use of kd_buffer */
318 * A globally increasing counter for identifying strings in trace. Starts at
319 * 1 because 0 is a reserved return value.
321 __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE
)))
322 static uint64_t g_curr_str_id
= 1;
324 #define STR_ID_SIG_OFFSET (48)
325 #define STR_ID_MASK ((1ULL << STR_ID_SIG_OFFSET) - 1)
326 #define STR_ID_SIG_MASK (~STR_ID_MASK)
329 * A bit pattern for identifying string IDs generated by
330 * kdebug_trace_string(2).
332 static uint64_t g_str_id_signature
= (0x70acULL
<< STR_ID_SIG_OFFSET
);
334 #define INTERRUPT 0x01050000
335 #define MACH_vmfault 0x01300008
336 #define BSC_SysCall 0x040c0000
337 #define MACH_SysCall 0x010c0000
339 /* task to string structure */
342 task_t task
; /* from procs task */
343 pid_t pid
; /* from procs p_pid */
344 char task_comm
[20]; /* from procs p_comm */
347 typedef struct tts tts_t
;
351 kd_threadmap
*map
; /* pointer to the map buffer */
357 typedef struct krt krt_t
;
359 /* This is for the CHUD toolkit call */
360 typedef void (*kd_chudhook_fn
) (uint32_t debugid
, uintptr_t arg1
,
361 uintptr_t arg2
, uintptr_t arg3
,
362 uintptr_t arg4
, uintptr_t arg5
);
364 volatile kd_chudhook_fn kdebug_chudhook
= 0; /* pointer to CHUD toolkit function */
366 static uint8_t *type_filter_bitmap
;
369 * This allows kperf to swap out the global state pid when kperf ownership is
370 * passed from one process to another. It checks the old global state pid so
371 * that kperf can't accidentally steal control of trace when a non-kperf trace user has
375 kdbg_swap_global_state_pid(pid_t old_pid
, pid_t new_pid
);
378 kdbg_swap_global_state_pid(pid_t old_pid
, pid_t new_pid
)
380 if (!(kd_ctrl_page
.kdebug_flags
& KDBG_LOCKINIT
))
383 lck_mtx_lock(kd_trace_mtx_sysctl
);
385 if (old_pid
== global_state_pid
)
386 global_state_pid
= new_pid
;
388 lck_mtx_unlock(kd_trace_mtx_sysctl
);
392 kdbg_cpu_count(boolean_t early_trace
)
396 * we've started tracing before the IOKit has even
397 * started running... just use the static max value
402 host_basic_info_data_t hinfo
;
403 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
404 host_info((host_t
)1 /* BSD_HOST */, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
405 assert(hinfo
.logical_cpu_max
> 0);
406 return hinfo
.logical_cpu_max
;
410 #endif /* MACH_ASSERT */
413 kdbg_iop_list_callback(kd_iop_t
* iop
, kd_callback_type type
, void* arg
)
416 iop
->callback
.func(iop
->callback
.context
, type
, arg
);
422 kdbg_set_tracing_enabled(boolean_t enabled
, uint32_t trace_type
)
424 int s
= ml_set_interrupts_enabled(FALSE
);
425 lck_spin_lock(kds_spin_lock
);
427 kdebug_enable
|= trace_type
;
428 kd_ctrl_page
.kdebug_slowcheck
&= ~SLOW_NOLOG
;
429 kd_ctrl_page
.enabled
= 1;
430 commpage_update_kdebug_enable();
432 kdebug_enable
&= ~(KDEBUG_ENABLE_TRACE
|KDEBUG_ENABLE_PPT
);
433 kd_ctrl_page
.kdebug_slowcheck
|= SLOW_NOLOG
;
434 kd_ctrl_page
.enabled
= 0;
435 commpage_update_kdebug_enable();
437 lck_spin_unlock(kds_spin_lock
);
438 ml_set_interrupts_enabled(s
);
441 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_KDEBUG_ENABLED
, NULL
);
444 * If you do not flush the IOP trace buffers, they can linger
445 * for a considerable period; consider code which disables and
446 * deallocates without a final sync flush.
448 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_KDEBUG_DISABLED
, NULL
);
449 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_SYNC_FLUSH
, NULL
);
454 kdbg_set_flags(int slowflag
, int enableflag
, boolean_t enabled
)
456 int s
= ml_set_interrupts_enabled(FALSE
);
457 lck_spin_lock(kds_spin_lock
);
460 kd_ctrl_page
.kdebug_slowcheck
|= slowflag
;
461 kdebug_enable
|= enableflag
;
463 kd_ctrl_page
.kdebug_slowcheck
&= ~slowflag
;
464 kdebug_enable
&= ~enableflag
;
467 lck_spin_unlock(kds_spin_lock
);
468 ml_set_interrupts_enabled(s
);
472 disable_wrap(uint32_t *old_slowcheck
, uint32_t *old_flags
)
474 int s
= ml_set_interrupts_enabled(FALSE
);
475 lck_spin_lock(kds_spin_lock
);
477 *old_slowcheck
= kd_ctrl_page
.kdebug_slowcheck
;
478 *old_flags
= kd_ctrl_page
.kdebug_flags
;
480 kd_ctrl_page
.kdebug_flags
&= ~KDBG_WRAPPED
;
481 kd_ctrl_page
.kdebug_flags
|= KDBG_NOWRAP
;
483 lck_spin_unlock(kds_spin_lock
);
484 ml_set_interrupts_enabled(s
);
488 enable_wrap(uint32_t old_slowcheck
, boolean_t lostevents
)
490 int s
= ml_set_interrupts_enabled(FALSE
);
491 lck_spin_lock(kds_spin_lock
);
493 kd_ctrl_page
.kdebug_flags
&= ~KDBG_NOWRAP
;
495 if ( !(old_slowcheck
& SLOW_NOLOG
))
496 kd_ctrl_page
.kdebug_slowcheck
&= ~SLOW_NOLOG
;
498 if (lostevents
== TRUE
)
499 kd_ctrl_page
.kdebug_flags
|= KDBG_WRAPPED
;
501 lck_spin_unlock(kds_spin_lock
);
502 ml_set_interrupts_enabled(s
);
506 create_buffers(boolean_t early_trace
)
515 * For the duration of this allocation, trace code will only reference
516 * kdebug_iops. Any iops registered after this enabling will not be
517 * messaged until the buffers are reallocated.
519 * TLDR; Must read kd_iops once and only once!
521 kd_ctrl_page
.kdebug_iops
= kd_iops
;
525 * If the list is valid, it is sorted, newest -> oldest. Each iop entry
526 * has a cpu_id of "the older entry + 1", so the highest cpu_id will
527 * be the list head + 1.
530 kd_ctrl_page
.kdebug_cpus
= kd_ctrl_page
.kdebug_iops
? kd_ctrl_page
.kdebug_iops
->cpu_id
+ 1 : kdbg_cpu_count(early_trace
);
532 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kdbip
, sizeof(struct kd_bufinfo
) * kd_ctrl_page
.kdebug_cpus
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
537 if (nkdbufs
< (kd_ctrl_page
.kdebug_cpus
* EVENTS_PER_STORAGE_UNIT
* MIN_STORAGE_UNITS_PER_CPU
))
538 n_storage_units
= kd_ctrl_page
.kdebug_cpus
* MIN_STORAGE_UNITS_PER_CPU
;
540 n_storage_units
= nkdbufs
/ EVENTS_PER_STORAGE_UNIT
;
542 nkdbufs
= n_storage_units
* EVENTS_PER_STORAGE_UNIT
;
544 f_buffers
= n_storage_units
/ N_STORAGE_UNITS_PER_BUFFER
;
545 n_storage_buffers
= f_buffers
;
547 f_buffer_size
= N_STORAGE_UNITS_PER_BUFFER
* sizeof(struct kd_storage
);
548 p_buffer_size
= (n_storage_units
% N_STORAGE_UNITS_PER_BUFFER
) * sizeof(struct kd_storage
);
555 if (kdcopybuf
== 0) {
556 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&kdcopybuf
, (vm_size_t
)KDCOPYBUF_SIZE
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
561 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
) {
565 bzero(kd_bufs
, n_storage_buffers
* sizeof(struct kd_storage_buffers
));
567 for (i
= 0; i
< f_buffers
; i
++) {
568 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
) {
572 bzero(kd_bufs
[i
].kdsb_addr
, f_buffer_size
);
574 kd_bufs
[i
].kdsb_size
= f_buffer_size
;
577 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
) {
581 bzero(kd_bufs
[i
].kdsb_addr
, p_buffer_size
);
583 kd_bufs
[i
].kdsb_size
= p_buffer_size
;
587 for (i
= 0; i
< n_storage_buffers
; i
++) {
588 struct kd_storage
*kds
;
592 n_elements
= kd_bufs
[i
].kdsb_size
/ sizeof(struct kd_storage
);
593 kds
= kd_bufs
[i
].kdsb_addr
;
595 for (n
= 0; n
< n_elements
; n
++) {
596 kds
[n
].kds_next
.buffer_index
= kd_ctrl_page
.kds_free_list
.buffer_index
;
597 kds
[n
].kds_next
.offset
= kd_ctrl_page
.kds_free_list
.offset
;
599 kd_ctrl_page
.kds_free_list
.buffer_index
= i
;
600 kd_ctrl_page
.kds_free_list
.offset
= n
;
602 n_storage_units
+= n_elements
;
605 bzero((char *)kdbip
, sizeof(struct kd_bufinfo
) * kd_ctrl_page
.kdebug_cpus
);
607 for (i
= 0; i
< (int)kd_ctrl_page
.kdebug_cpus
; i
++) {
608 kdbip
[i
].kd_list_head
.raw
= KDS_PTR_NULL
;
609 kdbip
[i
].kd_list_tail
.raw
= KDS_PTR_NULL
;
610 kdbip
[i
].kd_lostevents
= FALSE
;
611 kdbip
[i
].num_bufs
= 0;
614 kd_ctrl_page
.kdebug_flags
|= KDBG_BUFINIT
;
616 kd_ctrl_page
.kds_inuse_count
= 0;
617 n_storage_threshold
= n_storage_units
/ 2;
631 for (i
= 0; i
< n_storage_buffers
; i
++) {
632 if (kd_bufs
[i
].kdsb_addr
) {
633 kmem_free(kernel_map
, (vm_offset_t
)kd_bufs
[i
].kdsb_addr
, (vm_size_t
)kd_bufs
[i
].kdsb_size
);
636 kmem_free(kernel_map
, (vm_offset_t
)kd_bufs
, (vm_size_t
)(n_storage_buffers
* sizeof(struct kd_storage_buffers
)));
639 n_storage_buffers
= 0;
642 kmem_free(kernel_map
, (vm_offset_t
)kdcopybuf
, KDCOPYBUF_SIZE
);
646 kd_ctrl_page
.kds_free_list
.raw
= KDS_PTR_NULL
;
649 kmem_free(kernel_map
, (vm_offset_t
)kdbip
, sizeof(struct kd_bufinfo
) * kd_ctrl_page
.kdebug_cpus
);
653 kd_ctrl_page
.kdebug_iops
= NULL
;
654 kd_ctrl_page
.kdebug_cpus
= 0;
655 kd_ctrl_page
.kdebug_flags
&= ~KDBG_BUFINIT
;
659 release_storage_unit(int cpu
, uint32_t kdsp_raw
)
662 struct kd_storage
*kdsp_actual
;
663 struct kd_bufinfo
*kdbp
;
668 s
= ml_set_interrupts_enabled(FALSE
);
669 lck_spin_lock(kds_spin_lock
);
673 if (kdsp
.raw
== kdbp
->kd_list_head
.raw
) {
675 * it's possible for the storage unit pointed to
676 * by kdsp to have already been stolen... so
677 * check to see if it's still the head of the list
678 * now that we're behind the lock that protects
679 * adding and removing from the queue...
680 * since we only ever release and steal units from
681 * that position, if it's no longer the head
682 * we having nothing to do in this context
684 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
685 kdbp
->kd_list_head
= kdsp_actual
->kds_next
;
687 kdsp_actual
->kds_next
= kd_ctrl_page
.kds_free_list
;
688 kd_ctrl_page
.kds_free_list
= kdsp
;
690 kd_ctrl_page
.kds_inuse_count
--;
692 lck_spin_unlock(kds_spin_lock
);
693 ml_set_interrupts_enabled(s
);
698 allocate_storage_unit(int cpu
)
701 struct kd_storage
*kdsp_actual
, *kdsp_next_actual
;
702 struct kd_bufinfo
*kdbp
, *kdbp_vict
, *kdbp_try
;
703 uint64_t oldest_ts
, ts
;
704 boolean_t retval
= TRUE
;
707 s
= ml_set_interrupts_enabled(FALSE
);
708 lck_spin_lock(kds_spin_lock
);
712 /* If someone beat us to the allocate, return success */
713 if (kdbp
->kd_list_tail
.raw
!= KDS_PTR_NULL
) {
714 kdsp_actual
= POINTER_FROM_KDS_PTR(kdbp
->kd_list_tail
);
716 if (kdsp_actual
->kds_bufindx
< EVENTS_PER_STORAGE_UNIT
)
720 if ((kdsp
= kd_ctrl_page
.kds_free_list
).raw
!= KDS_PTR_NULL
) {
721 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
722 kd_ctrl_page
.kds_free_list
= kdsp_actual
->kds_next
;
724 kd_ctrl_page
.kds_inuse_count
++;
726 if (kd_ctrl_page
.kdebug_flags
& KDBG_NOWRAP
) {
727 kd_ctrl_page
.kdebug_slowcheck
|= SLOW_NOLOG
;
728 kdbp
->kd_lostevents
= TRUE
;
733 oldest_ts
= (uint64_t)-1;
735 for (kdbp_try
= &kdbip
[0]; kdbp_try
< &kdbip
[kd_ctrl_page
.kdebug_cpus
]; kdbp_try
++) {
737 if (kdbp_try
->kd_list_head
.raw
== KDS_PTR_NULL
) {
739 * no storage unit to steal
744 kdsp_actual
= POINTER_FROM_KDS_PTR(kdbp_try
->kd_list_head
);
746 if (kdsp_actual
->kds_bufcnt
< EVENTS_PER_STORAGE_UNIT
) {
748 * make sure we don't steal the storage unit
749 * being actively recorded to... need to
750 * move on because we don't want an out-of-order
751 * set of events showing up later
755 ts
= kdbg_get_timestamp(&kdsp_actual
->kds_records
[0]);
757 if (ts
< oldest_ts
) {
759 * when 'wrapping', we want to steal the
760 * storage unit that has the 'earliest' time
761 * associated with it (first event time)
764 kdbp_vict
= kdbp_try
;
767 if (kdbp_vict
== NULL
) {
769 kd_ctrl_page
.enabled
= 0;
770 commpage_update_kdebug_enable();
774 kdsp
= kdbp_vict
->kd_list_head
;
775 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
776 kdbp_vict
->kd_list_head
= kdsp_actual
->kds_next
;
778 if (kdbp_vict
->kd_list_head
.raw
!= KDS_PTR_NULL
) {
779 kdsp_next_actual
= POINTER_FROM_KDS_PTR(kdbp_vict
->kd_list_head
);
780 kdsp_next_actual
->kds_lostevents
= TRUE
;
782 kdbp_vict
->kd_lostevents
= TRUE
;
784 kd_ctrl_page
.kdebug_flags
|= KDBG_WRAPPED
;
786 kdsp_actual
->kds_timestamp
= mach_absolute_time();
787 kdsp_actual
->kds_next
.raw
= KDS_PTR_NULL
;
788 kdsp_actual
->kds_bufcnt
= 0;
789 kdsp_actual
->kds_readlast
= 0;
791 kdsp_actual
->kds_lostevents
= kdbp
->kd_lostevents
;
792 kdbp
->kd_lostevents
= FALSE
;
793 kdsp_actual
->kds_bufindx
= 0;
795 if (kdbp
->kd_list_head
.raw
== KDS_PTR_NULL
)
796 kdbp
->kd_list_head
= kdsp
;
798 POINTER_FROM_KDS_PTR(kdbp
->kd_list_tail
)->kds_next
= kdsp
;
799 kdbp
->kd_list_tail
= kdsp
;
801 lck_spin_unlock(kds_spin_lock
);
802 ml_set_interrupts_enabled(s
);
808 kernel_debug_register_callback(kd_callback_t callback
)
811 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iop
, sizeof(kd_iop_t
), VM_KERN_MEMORY_DIAG
) == KERN_SUCCESS
) {
812 memcpy(&iop
->callback
, &callback
, sizeof(kd_callback_t
));
815 * <rdar://problem/13351477> Some IOP clients are not providing a name.
820 boolean_t is_valid_name
= FALSE
;
821 for (uint32_t length
=0; length
<sizeof(callback
.iop_name
); ++length
) {
822 /* This is roughly isprintable(c) */
823 if (callback
.iop_name
[length
] > 0x20 && callback
.iop_name
[length
] < 0x7F)
825 if (callback
.iop_name
[length
] == 0) {
827 is_valid_name
= TRUE
;
832 if (!is_valid_name
) {
833 strlcpy(iop
->callback
.iop_name
, "IOP-???", sizeof(iop
->callback
.iop_name
));
837 iop
->last_timestamp
= 0;
841 * We use two pieces of state, the old list head
842 * pointer, and the value of old_list_head->cpu_id.
843 * If we read kd_iops more than once, it can change
846 * TLDR; Must not read kd_iops more than once per loop.
849 iop
->cpu_id
= iop
->next
? (iop
->next
->cpu_id
+1) : kdbg_cpu_count(FALSE
);
852 * Header says OSCompareAndSwapPtr has a memory barrier
854 } while (!OSCompareAndSwapPtr(iop
->next
, iop
, (void* volatile*)&kd_iops
));
876 struct kd_bufinfo
*kdbp
;
877 struct kd_storage
*kdsp_actual
;
878 union kds_ptr kds_raw
;
880 if (kd_ctrl_page
.kdebug_slowcheck
) {
882 if ( (kd_ctrl_page
.kdebug_slowcheck
& SLOW_NOLOG
) || !(kdebug_enable
& (KDEBUG_ENABLE_TRACE
|KDEBUG_ENABLE_PPT
)))
885 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
887 * Recheck if TYPEFILTER is being used, and if so,
888 * dereference bitmap. If the trace facility is being
889 * disabled, we have ~100ms of preemption-free CPU
890 * usage to access the bitmap.
892 disable_preemption();
893 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
894 if (isset(type_filter_bitmap
, KDBG_EXTRACT_CSC(debugid
)))
895 goto record_event_preempt_disabled
;
900 else if (kd_ctrl_page
.kdebug_flags
& KDBG_RANGECHECK
) {
901 if (debugid
>= kdlog_beg
&& debugid
<= kdlog_end
)
905 else if (kd_ctrl_page
.kdebug_flags
& KDBG_VALCHECK
) {
906 if ((debugid
& KDBG_EVENTID_MASK
) != kdlog_value1
&&
907 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value2
&&
908 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value3
&&
909 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value4
)
916 disable_preemption();
918 record_event_preempt_disabled
:
919 if (kd_ctrl_page
.enabled
== 0)
922 kdbp
= &kdbip
[coreid
];
923 timestamp
&= KDBG_TIMESTAMP_MASK
;
925 #if KDEBUG_MOJO_TRACE
926 if (kdebug_enable
& KDEBUG_ENABLE_SERIAL
)
927 kdebug_serial_print(coreid
, debugid
, timestamp
,
928 arg1
, arg2
, arg3
, arg4
, threadid
);
932 kds_raw
= kdbp
->kd_list_tail
;
934 if (kds_raw
.raw
!= KDS_PTR_NULL
) {
935 kdsp_actual
= POINTER_FROM_KDS_PTR(kds_raw
);
936 bindx
= kdsp_actual
->kds_bufindx
;
940 if (kdsp_actual
== NULL
|| bindx
>= EVENTS_PER_STORAGE_UNIT
) {
941 if (allocate_storage_unit(coreid
) == FALSE
) {
943 * this can only happen if wrapping
950 if ( !OSCompareAndSwap(bindx
, bindx
+ 1, &kdsp_actual
->kds_bufindx
))
953 // IOP entries can be allocated before xnu allocates and inits the buffer
954 if (timestamp
< kdsp_actual
->kds_timestamp
)
955 kdsp_actual
->kds_timestamp
= timestamp
;
957 kd
= &kdsp_actual
->kds_records
[bindx
];
959 kd
->debugid
= debugid
;
966 kdbg_set_timestamp_and_cpu(kd
, timestamp
, coreid
);
968 OSAddAtomic(1, &kdsp_actual
->kds_bufcnt
);
972 if ((kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
)) {
973 boolean_t need_kds_wakeup
= FALSE
;
977 * try to take the lock here to synchronize with the
978 * waiter entering the blocked state... use the try
979 * mode to prevent deadlocks caused by re-entering this
980 * routine due to various trace points triggered in the
981 * lck_spin_sleep_xxxx routines used to actually enter
982 * our wait condition... no problem if we fail,
983 * there will be lots of additional events coming in that
984 * will eventually succeed in grabbing this lock
986 s
= ml_set_interrupts_enabled(FALSE
);
988 if (lck_spin_try_lock(kdw_spin_lock
)) {
990 if (kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
) {
992 need_kds_wakeup
= TRUE
;
994 lck_spin_unlock(kdw_spin_lock
);
996 ml_set_interrupts_enabled(s
);
998 if (need_kds_wakeup
== TRUE
)
1007 kernel_debug_internal(
1015 struct proc
*curproc
;
1021 struct kd_bufinfo
*kdbp
;
1022 struct kd_storage
*kdsp_actual
;
1023 union kds_ptr kds_raw
;
1027 if (kd_ctrl_page
.kdebug_slowcheck
) {
1029 if (kdebug_enable
& KDEBUG_ENABLE_CHUD
) {
1030 kd_chudhook_fn chudhook
;
1032 * Mask interrupts to minimize the interval across
1033 * which the driver providing the hook could be
1036 s
= ml_set_interrupts_enabled(FALSE
);
1037 chudhook
= kdebug_chudhook
;
1039 chudhook(debugid
, arg1
, arg2
, arg3
, arg4
, arg5
);
1040 ml_set_interrupts_enabled(s
);
1042 if ( (kd_ctrl_page
.kdebug_slowcheck
& SLOW_NOLOG
) || !(kdebug_enable
& (KDEBUG_ENABLE_TRACE
|KDEBUG_ENABLE_PPT
)))
1045 if ( !ml_at_interrupt_context()) {
1046 if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDCHECK
) {
1048 * If kdebug flag is not set for current proc, return
1050 curproc
= current_proc();
1052 if ((curproc
&& !(curproc
->p_kdebug
)) &&
1053 ((debugid
& 0xffff0000) != (MACHDBG_CODE(DBG_MACH_SCHED
, 0) | DBG_FUNC_NONE
)) &&
1054 (debugid
>> 24 != DBG_TRACE
))
1057 else if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDEXCLUDE
) {
1059 * If kdebug flag is set for current proc, return
1061 curproc
= current_proc();
1063 if ((curproc
&& curproc
->p_kdebug
) &&
1064 ((debugid
& 0xffff0000) != (MACHDBG_CODE(DBG_MACH_SCHED
, 0) | DBG_FUNC_NONE
)) &&
1065 (debugid
>> 24 != DBG_TRACE
))
1070 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1071 /* Always record trace system info */
1072 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
)
1076 * Recheck if TYPEFILTER is being used, and if so,
1077 * dereference bitmap. If the trace facility is being
1078 * disabled, we have ~100ms of preemption-free CPU
1079 * usage to access the bitmap.
1081 disable_preemption();
1082 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1083 if (isset(type_filter_bitmap
, KDBG_EXTRACT_CSC(debugid
)))
1084 goto record_event_preempt_disabled
;
1086 enable_preemption();
1089 else if (kd_ctrl_page
.kdebug_flags
& KDBG_RANGECHECK
) {
1090 /* Always record trace system info */
1091 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
)
1094 if (debugid
< kdlog_beg
|| debugid
> kdlog_end
)
1097 else if (kd_ctrl_page
.kdebug_flags
& KDBG_VALCHECK
) {
1098 /* Always record trace system info */
1099 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
)
1102 if ((debugid
& KDBG_EVENTID_MASK
) != kdlog_value1
&&
1103 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value2
&&
1104 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value3
&&
1105 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value4
)
1110 disable_preemption();
1112 record_event_preempt_disabled
:
1113 if (kd_ctrl_page
.enabled
== 0)
1119 #if KDEBUG_MOJO_TRACE
1120 if (kdebug_enable
& KDEBUG_ENABLE_SERIAL
)
1121 kdebug_serial_print(cpu
, debugid
,
1122 mach_absolute_time() & KDBG_TIMESTAMP_MASK
,
1123 arg1
, arg2
, arg3
, arg4
, arg5
);
1127 kds_raw
= kdbp
->kd_list_tail
;
1129 if (kds_raw
.raw
!= KDS_PTR_NULL
) {
1130 kdsp_actual
= POINTER_FROM_KDS_PTR(kds_raw
);
1131 bindx
= kdsp_actual
->kds_bufindx
;
1135 if (kdsp_actual
== NULL
|| bindx
>= EVENTS_PER_STORAGE_UNIT
) {
1136 if (allocate_storage_unit(cpu
) == FALSE
) {
1138 * this can only happen if wrapping
1145 now
= mach_absolute_time() & KDBG_TIMESTAMP_MASK
;
1147 if ( !OSCompareAndSwap(bindx
, bindx
+ 1, &kdsp_actual
->kds_bufindx
))
1150 kd
= &kdsp_actual
->kds_records
[bindx
];
1152 kd
->debugid
= debugid
;
1159 kdbg_set_timestamp_and_cpu(kd
, now
, cpu
);
1161 OSAddAtomic(1, &kdsp_actual
->kds_bufcnt
);
1163 enable_preemption();
1165 if (kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
) {
1169 etype
= debugid
& KDBG_EVENTID_MASK
;
1170 stype
= debugid
& KDBG_CSC_MASK
;
1172 if (etype
== INTERRUPT
|| etype
== MACH_vmfault
||
1173 stype
== BSC_SysCall
|| stype
== MACH_SysCall
) {
1175 boolean_t need_kds_wakeup
= FALSE
;
1178 * try to take the lock here to synchronize with the
1179 * waiter entering the blocked state... use the try
1180 * mode to prevent deadlocks caused by re-entering this
1181 * routine due to various trace points triggered in the
1182 * lck_spin_sleep_xxxx routines used to actually enter
1183 * one of our 2 wait conditions... no problem if we fail,
1184 * there will be lots of additional events coming in that
1185 * will eventually succeed in grabbing this lock
1187 s
= ml_set_interrupts_enabled(FALSE
);
1189 if (lck_spin_try_lock(kdw_spin_lock
)) {
1191 if (kds_waiter
&& kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
) {
1193 need_kds_wakeup
= TRUE
;
1195 lck_spin_unlock(kdw_spin_lock
);
1197 ml_set_interrupts_enabled(s
);
1199 if (need_kds_wakeup
== TRUE
)
1200 wakeup(&kds_waiter
);
1212 __unused
uintptr_t arg5
)
1214 kernel_debug_internal(debugid
, arg1
, arg2
, arg3
, arg4
, (uintptr_t)thread_tid(current_thread()));
1226 kernel_debug_internal(debugid
, arg1
, arg2
, arg3
, arg4
, arg5
);
1230 kernel_debug_string_simple(const char *message
)
1232 uintptr_t arg
[4] = {0, 0, 0, 0};
1234 /* Stuff the message string in the args and log it. */
1235 strncpy((char *)arg
, message
, MIN(sizeof(arg
), strlen(message
)));
1238 arg
[0], arg
[1], arg
[2], arg
[3]);
1241 extern int master_cpu
; /* MACH_KERNEL_PRIVATE */
1243 * Used prior to start_kern_tracing() being called.
1244 * Log temporarily into a static buffer.
1254 /* If tracing is already initialized, use it */
1256 KERNEL_DEBUG_CONSTANT(debugid
, arg1
, arg2
, arg3
, arg4
, 0);
1260 /* Do nothing if the buffer is full or we're not on the boot cpu */
1261 kd_early_overflow
= kd_early_index
>= KD_EARLY_BUFFER_MAX
;
1262 if (kd_early_overflow
||
1263 cpu_number() != master_cpu
)
1266 kd_early_buffer
[kd_early_index
].debugid
= debugid
;
1267 kd_early_buffer
[kd_early_index
].timestamp
= mach_absolute_time();
1268 kd_early_buffer
[kd_early_index
].arg1
= arg1
;
1269 kd_early_buffer
[kd_early_index
].arg2
= arg2
;
1270 kd_early_buffer
[kd_early_index
].arg3
= arg3
;
1271 kd_early_buffer
[kd_early_index
].arg4
= arg4
;
1272 kd_early_buffer
[kd_early_index
].arg5
= 0;
1277 * Transfen the contents of the temporary buffer into the trace buffers.
1278 * Precede that by logging the rebase time (offset) - the TSC-based time (in ns)
1279 * when mach_absolute_time is set to 0.
1282 kernel_debug_early_end(void)
1286 if (cpu_number() != master_cpu
)
1287 panic("kernel_debug_early_end() not call on boot processor");
1289 /* Fake sentinel marking the start of kernel time relative to TSC */
1294 (uint32_t)(tsc_rebase_abs_time
>> 32),
1295 (uint32_t)tsc_rebase_abs_time
,
1299 for (i
= 0; i
< kd_early_index
; i
++) {
1302 kd_early_buffer
[i
].debugid
,
1303 kd_early_buffer
[i
].timestamp
,
1304 kd_early_buffer
[i
].arg1
,
1305 kd_early_buffer
[i
].arg2
,
1306 kd_early_buffer
[i
].arg3
,
1307 kd_early_buffer
[i
].arg4
,
1311 /* Cut events-lost event on overflow */
1312 if (kd_early_overflow
)
1313 KERNEL_DEBUG_CONSTANT(
1314 TRACE_LOST_EVENTS
, 0, 0, 0, 0, 0);
1316 /* This trace marks the start of kernel tracing */
1317 kernel_debug_string_simple("early trace done");
1321 * Returns non-zero if debugid is in a reserved class.
1324 kdebug_validate_debugid(uint32_t debugid
)
1326 uint8_t debugid_class
;
1328 debugid_class
= KDBG_EXTRACT_CLASS(debugid
);
1329 switch (debugid_class
) {
1338 * Support syscall SYS_kdebug_trace. U64->K32 args may get truncated in kdebug_trace64
1341 kdebug_trace(struct proc
*p
, struct kdebug_trace_args
*uap
, int32_t *retval
)
1343 struct kdebug_trace64_args uap64
;
1345 uap64
.code
= uap
->code
;
1346 uap64
.arg1
= uap
->arg1
;
1347 uap64
.arg2
= uap
->arg2
;
1348 uap64
.arg3
= uap
->arg3
;
1349 uap64
.arg4
= uap
->arg4
;
1351 return kdebug_trace64(p
, &uap64
, retval
);
1355 * Support syscall SYS_kdebug_trace64. 64-bit args on K32 will get truncated to fit in 32-bit record format.
1357 int kdebug_trace64(__unused
struct proc
*p
, struct kdebug_trace64_args
*uap
, __unused
int32_t *retval
)
1361 if ((err
= kdebug_validate_debugid(uap
->code
)) != 0) {
1365 if ( __probable(kdebug_enable
== 0) )
1368 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()));
1374 * Adding enough padding to contain a full tracepoint for the last
1375 * portion of the string greatly simplifies the logic of splitting the
1376 * string between tracepoints. Full tracepoints can be generated using
1377 * the buffer itself, without having to manually add zeros to pad the
1381 /* 2 string args in first tracepoint and 9 string data tracepoints */
1382 #define STR_BUF_ARGS (2 + (9 * 4))
1383 /* times the size of each arg on K64 */
1384 #define MAX_STR_LEN (STR_BUF_ARGS * sizeof(uint64_t))
1385 /* on K32, ending straddles a tracepoint, so reserve blanks */
1386 #define STR_BUF_SIZE (MAX_STR_LEN + (2 * sizeof(uint32_t)))
1389 * This function does no error checking and assumes that it is called with
1390 * the correct arguments, including that the buffer pointed to by str is at
1391 * least STR_BUF_SIZE bytes. However, str must be aligned to word-size and
1392 * be NUL-terminated. In cases where a string can fit evenly into a final
1393 * tracepoint without its NUL-terminator, this function will not end those
1394 * strings with a NUL in trace. It's up to clients to look at the function
1395 * qualifier for DBG_FUNC_END in this case, to end the string.
1398 kernel_debug_string_internal(uint32_t debugid
, uint64_t str_id
, void *vstr
,
1401 /* str must be word-aligned */
1402 uintptr_t *str
= vstr
;
1404 uintptr_t thread_id
;
1406 uint32_t trace_debugid
= TRACEDBG_CODE(DBG_TRACE_STRING
,
1407 TRACE_STRING_GLOBAL
);
1409 thread_id
= (uintptr_t)thread_tid(current_thread());
1411 /* if the ID is being invalidated, just emit that */
1412 if (str_id
!= 0 && str_len
== 0) {
1413 kernel_debug_internal(trace_debugid
| DBG_FUNC_START
| DBG_FUNC_END
,
1414 (uintptr_t)debugid
, (uintptr_t)str_id
, 0, 0,
1419 /* generate an ID, if necessary */
1421 str_id
= OSIncrementAtomic64((SInt64
*)&g_curr_str_id
);
1422 str_id
= (str_id
& STR_ID_MASK
) | g_str_id_signature
;
1425 trace_debugid
|= DBG_FUNC_START
;
1426 /* string can fit in a single tracepoint */
1427 if (str_len
<= (2 * sizeof(uintptr_t))) {
1428 trace_debugid
|= DBG_FUNC_END
;
1431 kernel_debug_internal(trace_debugid
, (uintptr_t)debugid
,
1432 (uintptr_t)str_id
, str
[0],
1435 trace_debugid
&= KDBG_EVENTID_MASK
;
1437 written
+= 2 * sizeof(uintptr_t);
1439 for (; written
< str_len
; i
+= 4, written
+= 4 * sizeof(uintptr_t)) {
1440 if ((written
+ (4 * sizeof(uintptr_t))) >= str_len
) {
1441 trace_debugid
|= DBG_FUNC_END
;
1443 kernel_debug_internal(trace_debugid
, str
[i
],
1446 str
[i
+ 3], thread_id
);
1453 * Returns true if the current process can emit events, and false otherwise.
1454 * Trace system and scheduling events circumvent this check, as do events
1455 * emitted in interrupt context.
1458 kdebug_current_proc_enabled(uint32_t debugid
)
1460 /* can't determine current process in interrupt context */
1461 if (ml_at_interrupt_context()) {
1465 /* always emit trace system and scheduling events */
1466 if ((KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
||
1467 (debugid
& KDBG_CSC_MASK
) == MACHDBG_CODE(DBG_MACH_SCHED
, 0)))
1472 if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDCHECK
) {
1473 proc_t cur_proc
= current_proc();
1475 /* only the process with the kdebug bit set is allowed */
1476 if (cur_proc
&& !(cur_proc
->p_kdebug
)) {
1479 } else if (kd_ctrl_page
.kdebug_flags
& KDBG_PIDEXCLUDE
) {
1480 proc_t cur_proc
= current_proc();
1482 /* every process except the one with the kdebug bit set is allowed */
1483 if (cur_proc
&& cur_proc
->p_kdebug
) {
1492 * Returns true if the debugid is disabled by filters, and false if the
1493 * debugid is allowed to be traced. A debugid may not be traced if the
1494 * typefilter disables its class and subclass, it's outside a range
1495 * check, or if it's not an allowed debugid in a value check. Trace
1496 * system events bypass this check.
1499 kdebug_debugid_enabled(uint32_t debugid
)
1501 boolean_t is_enabled
= TRUE
;
1503 /* if no filtering is enabled */
1504 if (!kd_ctrl_page
.kdebug_slowcheck
) {
1508 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_TRACE
) {
1512 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1513 disable_preemption();
1516 * Recheck if typefilter is still being used. If tracing is being
1517 * disabled, there's a 100ms sleep on the other end to keep the
1518 * bitmap around for this check.
1520 if (kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) {
1521 if (!(isset(type_filter_bitmap
, KDBG_EXTRACT_CSC(debugid
)))) {
1526 enable_preemption();
1527 } else if (kd_ctrl_page
.kdebug_flags
& KDBG_RANGECHECK
) {
1528 if (debugid
< kdlog_beg
|| debugid
> kdlog_end
) {
1531 } else if (kd_ctrl_page
.kdebug_flags
& KDBG_VALCHECK
) {
1532 if ((debugid
& KDBG_EVENTID_MASK
) != kdlog_value1
&&
1533 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value2
&&
1534 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value3
&&
1535 (debugid
& KDBG_EVENTID_MASK
) != kdlog_value4
)
1545 * Returns 0 if a string can be traced with these arguments. Returns errno
1546 * value if error occurred.
1549 kdebug_check_trace_string(uint32_t debugid
, uint64_t str_id
)
1551 /* if there are function qualifiers on the debugid */
1552 if (debugid
& ~KDBG_EVENTID_MASK
) {
1556 if (kdebug_validate_debugid(debugid
)) {
1560 if (str_id
!= 0 && (str_id
& STR_ID_SIG_MASK
) != g_str_id_signature
) {
1568 * Implementation of KPI kernel_debug_string.
1571 kernel_debug_string(uint32_t debugid
, uint64_t *str_id
, const char *str
)
1573 /* arguments to tracepoints must be word-aligned */
1574 __attribute__((aligned(sizeof(uintptr_t)))) char str_buf
[STR_BUF_SIZE
];
1575 assert_static(sizeof(str_buf
) > MAX_STR_LEN
);
1576 vm_size_t len_copied
;
1581 if (__probable(kdebug_enable
== 0)) {
1585 if (!kdebug_current_proc_enabled(debugid
)) {
1589 if (!kdebug_debugid_enabled(debugid
)) {
1593 if ((err
= kdebug_check_trace_string(debugid
, *str_id
)) != 0) {
1602 *str_id
= kernel_debug_string_internal(debugid
, *str_id
, NULL
, 0);
1606 memset(str_buf
, 0, sizeof(str_buf
));
1607 len_copied
= strlcpy(str_buf
, str
, MAX_STR_LEN
+ 1);
1608 *str_id
= kernel_debug_string_internal(debugid
, *str_id
, str_buf
,
1614 * Support syscall kdebug_trace_string.
1617 kdebug_trace_string(__unused
struct proc
*p
,
1618 struct kdebug_trace_string_args
*uap
,
1621 __attribute__((aligned(sizeof(uintptr_t)))) char str_buf
[STR_BUF_SIZE
];
1622 assert_static(sizeof(str_buf
) > MAX_STR_LEN
);
1626 if (__probable(kdebug_enable
== 0)) {
1630 if (!kdebug_current_proc_enabled(uap
->debugid
)) {
1634 if (!kdebug_debugid_enabled(uap
->debugid
)) {
1638 if ((err
= kdebug_check_trace_string(uap
->debugid
, uap
->str_id
)) != 0) {
1642 if (uap
->str
== USER_ADDR_NULL
) {
1643 if (uap
->str_id
== 0) {
1647 *retval
= kernel_debug_string_internal(uap
->debugid
, uap
->str_id
,
1652 memset(str_buf
, 0, sizeof(str_buf
));
1653 err
= copyinstr(uap
->str
, str_buf
, MAX_STR_LEN
+ 1, &len_copied
);
1655 /* it's alright to truncate the string, so allow ENAMETOOLONG */
1656 if (err
== ENAMETOOLONG
) {
1657 str_buf
[MAX_STR_LEN
] = '\0';
1662 if (len_copied
<= 1) {
1666 /* convert back to a length */
1669 *retval
= kernel_debug_string_internal(uap
->debugid
, uap
->str_id
, str_buf
,
1675 kdbg_lock_init(void)
1677 if (kd_ctrl_page
.kdebug_flags
& KDBG_LOCKINIT
)
1681 * allocate lock group attribute and group
1683 kd_trace_mtx_sysctl_grp_attr
= lck_grp_attr_alloc_init();
1684 kd_trace_mtx_sysctl_grp
= lck_grp_alloc_init("kdebug", kd_trace_mtx_sysctl_grp_attr
);
1687 * allocate the lock attribute
1689 kd_trace_mtx_sysctl_attr
= lck_attr_alloc_init();
1693 * allocate and initialize mutex's
1695 kd_trace_mtx_sysctl
= lck_mtx_alloc_init(kd_trace_mtx_sysctl_grp
, kd_trace_mtx_sysctl_attr
);
1696 kds_spin_lock
= lck_spin_alloc_init(kd_trace_mtx_sysctl_grp
, kd_trace_mtx_sysctl_attr
);
1697 kdw_spin_lock
= lck_spin_alloc_init(kd_trace_mtx_sysctl_grp
, kd_trace_mtx_sysctl_attr
);
1699 kd_ctrl_page
.kdebug_flags
|= KDBG_LOCKINIT
;
1704 kdbg_bootstrap(boolean_t early_trace
)
1706 kd_ctrl_page
.kdebug_flags
&= ~KDBG_WRAPPED
;
1708 return (create_buffers(early_trace
));
1712 kdbg_reinit(boolean_t early_trace
)
1717 * Disable trace collecting
1718 * First make sure we're not in
1719 * the middle of cutting a trace
1721 kdbg_set_tracing_enabled(FALSE
, KDEBUG_ENABLE_TRACE
);
1724 * make sure the SLOW_NOLOG is seen
1725 * by everyone that might be trying
1732 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
1733 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
1734 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
1739 ret
= kdbg_bootstrap(early_trace
);
1741 RAW_file_offset
= 0;
1742 RAW_file_written
= 0;
1748 kdbg_trace_data(struct proc
*proc
, long *arg_pid
)
1753 *arg_pid
= proc
->p_pid
;
1758 kdbg_trace_string(struct proc
*proc
, long *arg1
, long *arg2
, long *arg3
, long *arg4
)
1772 * Collect the pathname for tracing
1774 dbg_nameptr
= proc
->p_comm
;
1775 dbg_namelen
= (int)strlen(proc
->p_comm
);
1781 if(dbg_namelen
> (int)sizeof(dbg_parms
))
1782 dbg_namelen
= (int)sizeof(dbg_parms
);
1784 strncpy((char *)dbg_parms
, dbg_nameptr
, dbg_namelen
);
1793 kdbg_resolve_map(thread_t th_act
, void *opaque
)
1795 kd_threadmap
*mapptr
;
1796 krt_t
*t
= (krt_t
*)opaque
;
1798 if (t
->count
< t
->maxcount
) {
1799 mapptr
= &t
->map
[t
->count
];
1800 mapptr
->thread
= (uintptr_t)thread_tid(th_act
);
1802 (void) strlcpy (mapptr
->command
, t
->atts
->task_comm
,
1803 sizeof(t
->atts
->task_comm
));
1805 * Some kernel threads have no associated pid.
1806 * We still need to mark the entry as valid.
1809 mapptr
->valid
= t
->atts
->pid
;
1819 * Writes a cpumap for the given iops_list/cpu_count to the provided buffer.
1821 * You may provide a buffer and size, or if you set the buffer to NULL, a
1822 * buffer of sufficient size will be allocated.
1824 * If you provide a buffer and it is too small, sets cpumap_size to the number
1825 * of bytes required and returns EINVAL.
1827 * On success, if you provided a buffer, cpumap_size is set to the number of
1828 * bytes written. If you did not provide a buffer, cpumap is set to the newly
1829 * allocated buffer and cpumap_size is set to the number of bytes allocated.
1831 * NOTE: It may seem redundant to pass both iops and a cpu_count.
1833 * We may be reporting data from "now", or from the "past".
1835 * The "now" data would be for something like kdbg_readcurcpumap().
1836 * The "past" data would be for kdbg_readcpumap().
1838 * If we do not pass both iops and cpu_count, and iops is NULL, this function
1839 * will need to read "now" state to get the number of cpus, which would be in
1840 * error if we were reporting "past" state.
1844 kdbg_cpumap_init_internal(kd_iop_t
* iops
, uint32_t cpu_count
, uint8_t** cpumap
, uint32_t* cpumap_size
)
1847 assert(cpumap_size
);
1849 assert(!iops
|| iops
->cpu_id
+ 1 == cpu_count
);
1851 uint32_t bytes_needed
= sizeof(kd_cpumap_header
) + cpu_count
* sizeof(kd_cpumap
);
1852 uint32_t bytes_available
= *cpumap_size
;
1853 *cpumap_size
= bytes_needed
;
1855 if (*cpumap
== NULL
) {
1856 if (kmem_alloc(kernel_map
, (vm_offset_t
*)cpumap
, (vm_size_t
)*cpumap_size
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
1859 } else if (bytes_available
< bytes_needed
) {
1863 kd_cpumap_header
* header
= (kd_cpumap_header
*)(uintptr_t)*cpumap
;
1865 header
->version_no
= RAW_VERSION1
;
1866 header
->cpu_count
= cpu_count
;
1868 kd_cpumap
* cpus
= (kd_cpumap
*)&header
[1];
1870 int32_t index
= cpu_count
- 1;
1872 cpus
[index
].cpu_id
= iops
->cpu_id
;
1873 cpus
[index
].flags
= KDBG_CPUMAP_IS_IOP
;
1874 bzero(cpus
[index
].name
, sizeof(cpus
->name
));
1875 strlcpy(cpus
[index
].name
, iops
->callback
.iop_name
, sizeof(cpus
->name
));
1881 while (index
>= 0) {
1882 cpus
[index
].cpu_id
= index
;
1883 cpus
[index
].flags
= 0;
1884 bzero(cpus
[index
].name
, sizeof(cpus
->name
));
1885 strlcpy(cpus
[index
].name
, "AP", sizeof(cpus
->name
));
1890 return KERN_SUCCESS
;
1894 kdbg_thrmap_init(void)
1896 if (kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
)
1899 kd_mapptr
= kdbg_thrmap_init_internal(0, &kd_mapsize
, &kd_mapcount
);
1902 kd_ctrl_page
.kdebug_flags
|= KDBG_MAPINIT
;
1906 kd_threadmap
* kdbg_thrmap_init_internal(unsigned int count
, unsigned int *mapsize
, unsigned int *mapcount
)
1908 kd_threadmap
*mapptr
;
1911 int tts_count
; /* number of task-to-string structures */
1912 struct tts
*tts_mapptr
;
1913 unsigned int tts_mapsize
= 0;
1918 * need to use PROC_SCANPROCLIST with proc_iterate
1923 * Calculate the sizes of map buffers
1925 for (p
= allproc
.lh_first
, *mapcount
=0, tts_count
=0; p
; p
= p
->p_list
.le_next
) {
1926 *mapcount
+= get_task_numacts((task_t
)p
->task
);
1932 * The proc count could change during buffer allocation,
1933 * so introduce a small fudge factor to bump up the
1934 * buffer sizes. This gives new tasks some chance of
1935 * making into the tables. Bump up by 25%.
1937 *mapcount
+= *mapcount
/4;
1938 tts_count
+= tts_count
/4;
1940 *mapsize
= *mapcount
* sizeof(kd_threadmap
);
1942 if (count
&& count
< *mapcount
)
1945 if ((kmem_alloc(kernel_map
, &kaddr
, (vm_size_t
)*mapsize
, VM_KERN_MEMORY_DIAG
) == KERN_SUCCESS
)) {
1946 bzero((void *)kaddr
, *mapsize
);
1947 mapptr
= (kd_threadmap
*)kaddr
;
1951 tts_mapsize
= tts_count
* sizeof(struct tts
);
1953 if ((kmem_alloc(kernel_map
, &kaddr
, (vm_size_t
)tts_mapsize
, VM_KERN_MEMORY_DIAG
) == KERN_SUCCESS
)) {
1954 bzero((void *)kaddr
, tts_mapsize
);
1955 tts_mapptr
= (struct tts
*)kaddr
;
1957 kmem_free(kernel_map
, (vm_offset_t
)mapptr
, *mapsize
);
1962 * We need to save the procs command string
1963 * and take a reference for each task associated
1964 * with a valid process
1970 * should use proc_iterate
1972 for (p
= allproc
.lh_first
, i
=0; p
&& i
< tts_count
; p
= p
->p_list
.le_next
) {
1973 if (p
->p_lflag
& P_LEXIT
)
1977 task_reference(p
->task
);
1978 tts_mapptr
[i
].task
= p
->task
;
1979 tts_mapptr
[i
].pid
= p
->p_pid
;
1980 (void)strlcpy(tts_mapptr
[i
].task_comm
, p
->p_comm
, sizeof(tts_mapptr
[i
].task_comm
));
1989 * Initialize thread map data
1993 akrt
.maxcount
= *mapcount
;
1995 for (i
= 0; i
< tts_count
; i
++) {
1996 akrt
.atts
= &tts_mapptr
[i
];
1997 task_act_iterate_wth_args(tts_mapptr
[i
].task
, kdbg_resolve_map
, &akrt
);
1998 task_deallocate((task_t
) tts_mapptr
[i
].task
);
2000 kmem_free(kernel_map
, (vm_offset_t
)tts_mapptr
, tts_mapsize
);
2002 *mapcount
= akrt
.count
;
2011 * Clean up the trace buffer
2012 * First make sure we're not in
2013 * the middle of cutting a trace
2015 kdbg_set_tracing_enabled(FALSE
, KDEBUG_ENABLE_TRACE
);
2016 kdbg_disable_typefilter();
2019 * make sure the SLOW_NOLOG is seen
2020 * by everyone that might be trying
2025 global_state_pid
= -1;
2026 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2027 kd_ctrl_page
.kdebug_flags
&= ~(KDBG_NOWRAP
| KDBG_RANGECHECK
| KDBG_VALCHECK
);
2028 kd_ctrl_page
.kdebug_flags
&= ~(KDBG_PIDCHECK
| KDBG_PIDEXCLUDE
);
2030 kdbg_deallocate_typefilter();
2034 /* Clean up the thread map buffer */
2035 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
2037 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
2038 kd_mapptr
= (kd_threadmap
*) 0;
2043 RAW_file_offset
= 0;
2044 RAW_file_written
= 0;
2048 kdbg_setpid(kd_regtype
*kdr
)
2054 pid
= (pid_t
)kdr
->value1
;
2055 flag
= (int)kdr
->value2
;
2058 if ((p
= proc_find(pid
)) == NULL
)
2063 * turn on pid check for this and all pids
2065 kd_ctrl_page
.kdebug_flags
|= KDBG_PIDCHECK
;
2066 kd_ctrl_page
.kdebug_flags
&= ~KDBG_PIDEXCLUDE
;
2067 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2072 * turn off pid check for this pid value
2073 * Don't turn off all pid checking though
2075 * kd_ctrl_page.kdebug_flags &= ~KDBG_PIDCHECK;
2088 /* This is for pid exclusion in the trace buffer */
2090 kdbg_setpidex(kd_regtype
*kdr
)
2096 pid
= (pid_t
)kdr
->value1
;
2097 flag
= (int)kdr
->value2
;
2100 if ((p
= proc_find(pid
)) == NULL
)
2105 * turn on pid exclusion
2107 kd_ctrl_page
.kdebug_flags
|= KDBG_PIDEXCLUDE
;
2108 kd_ctrl_page
.kdebug_flags
&= ~KDBG_PIDCHECK
;
2109 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2115 * turn off pid exclusion for this pid value
2116 * Don't turn off all pid exclusion though
2118 * kd_ctrl_page.kdebug_flags &= ~KDBG_PIDEXCLUDE;
2132 * This is for setting a maximum decrementer value
2135 kdbg_setrtcdec(kd_regtype
*kdr
)
2140 decval
= (natural_t
)kdr
->value1
;
2142 if (decval
&& decval
< KDBG_MINRTCDEC
)
2151 kdbg_enable_typefilter(void)
2155 /* Allocate memory for bitmap if not already allocated */
2156 ret
= kdbg_allocate_typefilter();
2161 /* Turn off range and value checks */
2162 kd_ctrl_page
.kdebug_flags
&= ~(KDBG_RANGECHECK
| KDBG_VALCHECK
);
2164 /* Enable filter checking */
2165 kd_ctrl_page
.kdebug_flags
|= KDBG_TYPEFILTER_CHECK
;
2166 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2171 kdbg_disable_typefilter(void)
2173 /* Disable filter checking */
2174 kd_ctrl_page
.kdebug_flags
&= ~KDBG_TYPEFILTER_CHECK
;
2176 /* Turn off slow checks unless pid checks are using them */
2177 if ( (kd_ctrl_page
.kdebug_flags
& (KDBG_PIDCHECK
| KDBG_PIDEXCLUDE
)) )
2178 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2180 kdbg_set_flags(SLOW_CHECKS
, 0, FALSE
);
2182 /* typefilter bitmap will be deallocated later */
2188 kdbg_allocate_typefilter(void)
2190 if (type_filter_bitmap
== NULL
) {
2191 vm_offset_t bitmap
= 0;
2193 if (kmem_alloc(kernel_map
, &bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
2197 bzero((void *)bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2199 if (!OSCompareAndSwapPtr(NULL
, (void *)bitmap
, &type_filter_bitmap
)) {
2200 kmem_free(kernel_map
, bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2201 return 0; /* someone assigned a buffer */
2204 bzero(type_filter_bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2211 kdbg_deallocate_typefilter(void)
2213 if(type_filter_bitmap
) {
2214 vm_offset_t bitmap
= (vm_offset_t
)type_filter_bitmap
;
2216 if (OSCompareAndSwapPtr((void *)bitmap
, NULL
, &type_filter_bitmap
)) {
2217 kmem_free(kernel_map
, bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
);
2220 /* already swapped */
2228 kdbg_setreg(kd_regtype
* kdr
)
2231 unsigned int val_1
, val_2
, val
;
2232 switch (kdr
->type
) {
2234 case KDBG_CLASSTYPE
:
2235 val_1
= (kdr
->value1
& 0xff);
2236 val_2
= (kdr
->value2
& 0xff);
2237 kdlog_beg
= (val_1
<<24);
2238 kdlog_end
= (val_2
<<24);
2239 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2240 kd_ctrl_page
.kdebug_flags
&= ~KDBG_VALCHECK
; /* Turn off specific value check */
2241 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_CLASSTYPE
);
2242 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2244 case KDBG_SUBCLSTYPE
:
2245 val_1
= (kdr
->value1
& 0xff);
2246 val_2
= (kdr
->value2
& 0xff);
2248 kdlog_beg
= ((val_1
<<24) | (val_2
<< 16));
2249 kdlog_end
= ((val_1
<<24) | (val
<< 16));
2250 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2251 kd_ctrl_page
.kdebug_flags
&= ~KDBG_VALCHECK
; /* Turn off specific value check */
2252 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_SUBCLSTYPE
);
2253 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2255 case KDBG_RANGETYPE
:
2256 kdlog_beg
= (kdr
->value1
);
2257 kdlog_end
= (kdr
->value2
);
2258 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2259 kd_ctrl_page
.kdebug_flags
&= ~KDBG_VALCHECK
; /* Turn off specific value check */
2260 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_RANGETYPE
);
2261 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2264 kdlog_value1
= (kdr
->value1
);
2265 kdlog_value2
= (kdr
->value2
);
2266 kdlog_value3
= (kdr
->value3
);
2267 kdlog_value4
= (kdr
->value4
);
2268 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2269 kd_ctrl_page
.kdebug_flags
&= ~KDBG_RANGECHECK
; /* Turn off range check */
2270 kd_ctrl_page
.kdebug_flags
|= KDBG_VALCHECK
; /* Turn on specific value check */
2271 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2273 case KDBG_TYPENONE
:
2274 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2276 if ( (kd_ctrl_page
.kdebug_flags
& (KDBG_RANGECHECK
| KDBG_VALCHECK
|
2277 KDBG_PIDCHECK
| KDBG_PIDEXCLUDE
|
2278 KDBG_TYPEFILTER_CHECK
)) )
2279 kdbg_set_flags(SLOW_CHECKS
, 0, TRUE
);
2281 kdbg_set_flags(SLOW_CHECKS
, 0, FALSE
);
2294 kdbg_getreg(__unused kd_regtype
* kdr
)
2298 unsigned int val_1
, val_2
, val
;
2300 switch (kdr
->type
) {
2301 case KDBG_CLASSTYPE
:
2302 val_1
= (kdr
->value1
& 0xff);
2304 kdlog_beg
= (val_1
<<24);
2305 kdlog_end
= (val_2
<<24);
2306 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2307 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_CLASSTYPE
);
2309 case KDBG_SUBCLSTYPE
:
2310 val_1
= (kdr
->value1
& 0xff);
2311 val_2
= (kdr
->value2
& 0xff);
2313 kdlog_beg
= ((val_1
<<24) | (val_2
<< 16));
2314 kdlog_end
= ((val_1
<<24) | (val
<< 16));
2315 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2316 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_SUBCLSTYPE
);
2318 case KDBG_RANGETYPE
:
2319 kdlog_beg
= (kdr
->value1
);
2320 kdlog_end
= (kdr
->value2
);
2321 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2322 kd_ctrl_page
.kdebug_flags
|= (KDBG_RANGECHECK
| KDBG_RANGETYPE
);
2324 case KDBG_TYPENONE
:
2325 kd_ctrl_page
.kdebug_flags
&= (unsigned int)~KDBG_CKTYPES
;
2338 kdbg_write_to_vnode(caddr_t buffer
, size_t size
, vnode_t vp
, vfs_context_t ctx
, off_t file_offset
)
2340 return vn_rdwr(UIO_WRITE
, vp
, buffer
, size
, file_offset
, UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
,
2341 vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2345 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
)
2347 int ret
= KERN_SUCCESS
;
2348 kd_chunk_header_v3 header
;
2351 header
.sub_tag
= sub_tag
;
2352 header
.length
= length
;
2354 // Check that only one of them is valid
2355 assert(!buffer
^ !vp
);
2356 assert((vp
== NULL
) || (ctx
!= NULL
));
2358 // Write the 8-byte future_chunk_timestamp field in the payload
2361 ret
= kdbg_write_to_vnode((caddr_t
)&header
, sizeof(kd_chunk_header_v3
), vp
, ctx
, RAW_file_offset
);
2365 RAW_file_offset
+= (sizeof(kd_chunk_header_v3
));
2368 ret
= copyout(&header
, buffer
, sizeof(kd_chunk_header_v3
));
2379 kdbg_write_v3_chunk_header_to_buffer(void * buffer
, uint32_t tag
, uint32_t sub_tag
, uint64_t length
)
2381 kd_chunk_header_v3 header
;
2384 header
.sub_tag
= sub_tag
;
2385 header
.length
= length
;
2391 memcpy(buffer
, &header
, sizeof(kd_chunk_header_v3
));
2393 return (sizeof(kd_chunk_header_v3
));
2397 kdbg_write_v3_chunk_to_fd(uint32_t tag
, uint32_t sub_tag
, uint64_t length
, void *payload
, uint64_t payload_size
, int fd
)
2400 struct vfs_context context
;
2401 struct fileproc
*fp
;
2406 if ( (fp_lookup(p
, fd
, &fp
, 1)) ) {
2411 context
.vc_thread
= current_thread();
2412 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
2414 if (FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_VNODE
) {
2415 fp_drop(p
, fd
, fp
, 1);
2419 vp
= (struct vnode
*) fp
->f_fglob
->fg_data
;
2422 if ( (vnode_getwithref(vp
)) == 0 ) {
2423 RAW_file_offset
= fp
->f_fglob
->fg_offset
;
2425 kd_chunk_header_v3 chunk_header
= { .tag
= tag
, .sub_tag
= sub_tag
, .length
= length
};
2427 int ret
= kdbg_write_to_vnode((caddr_t
) &chunk_header
, sizeof(kd_chunk_header_v3
), vp
, &context
, RAW_file_offset
);
2429 RAW_file_offset
+= sizeof(kd_chunk_header_v3
);
2432 ret
= kdbg_write_to_vnode((caddr_t
) payload
, (size_t) payload_size
, vp
, &context
, RAW_file_offset
);
2434 RAW_file_offset
+= payload_size
;
2437 fp
->f_fglob
->fg_offset
= RAW_file_offset
;
2441 fp_drop(p
, fd
, fp
, 0);
2442 return KERN_SUCCESS
;
2446 kdbg_write_v3_event_chunk_header(user_addr_t buffer
, uint32_t tag
, uint64_t length
, vnode_t vp
, vfs_context_t ctx
)
2448 uint64_t future_chunk_timestamp
= 0;
2449 length
+= sizeof(uint64_t);
2451 if (kdbg_write_v3_chunk_header(buffer
, tag
, V3_EVENT_DATA_VERSION
, length
, vp
, ctx
)) {
2455 buffer
+= sizeof(kd_chunk_header_v3
);
2458 // Check that only one of them is valid
2459 assert(!buffer
^ !vp
);
2460 assert((vp
== NULL
) || (ctx
!= NULL
));
2462 // Write the 8-byte future_chunk_timestamp field in the payload
2465 int ret
= kdbg_write_to_vnode((caddr_t
)&future_chunk_timestamp
, sizeof(uint64_t), vp
, ctx
, RAW_file_offset
);
2467 RAW_file_offset
+= (sizeof(uint64_t));
2471 if (copyout(&future_chunk_timestamp
, buffer
, sizeof(uint64_t))) {
2477 return (buffer
+ sizeof(uint64_t));
2481 kdbg_write_v3_header(user_addr_t user_header
, size_t *user_header_size
, int fd
)
2483 int ret
= KERN_SUCCESS
;
2484 kd_header_v3 header
;
2486 uint8_t* cpumap
= 0;
2487 uint32_t cpumap_size
= 0;
2488 uint32_t thrmap_size
= 0;
2490 size_t bytes_needed
= 0;
2492 // Check that only one of them is valid
2493 assert(!user_header
^ !fd
);
2494 assert(user_header_size
);
2496 if ( !(kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) ) {
2501 if ( !(user_header
|| fd
) ) {
2506 // Initialize the cpu map
2507 ret
= kdbg_cpumap_init_internal(kd_ctrl_page
.kdebug_iops
, kd_ctrl_page
.kdebug_cpus
, &cpumap
, &cpumap_size
);
2508 if (ret
!= KERN_SUCCESS
) {
2512 // Check if a thread map is initialized
2517 thrmap_size
= kd_mapcount
* sizeof(kd_threadmap
);
2519 // Setup the header.
2520 // See v3 header description in sys/kdebug.h for more inforamtion.
2522 header
.tag
= RAW_VERSION3
;
2523 header
.sub_tag
= V3_HEADER_VERSION
;
2524 header
.length
= ( sizeof(kd_header_v3
) + cpumap_size
- sizeof(kd_cpumap_header
));
2526 mach_timebase_info_data_t timebase
= {0, 0};
2527 clock_timebase_info(&timebase
);
2528 header
.timebase_numer
= timebase
.numer
;
2529 header
.timebase_denom
= timebase
.denom
;
2530 header
.timestamp
= 0;
2531 header
.walltime_secs
= 0;
2532 header
.walltime_usecs
= 0;
2533 header
.timezone_minuteswest
= 0;
2534 header
.timezone_dst
= 0;
2536 #if defined __LP64__
2542 // If its a buffer, check if we have enough space to copy the header and the maps.
2544 bytes_needed
= header
.length
+ thrmap_size
+ (2 * sizeof(kd_chunk_header_v3
));
2545 if ( !user_header_size
) {
2549 if (*user_header_size
< bytes_needed
) {
2555 // Start writing the header
2557 void *hdr_ptr
= (void *)(((uintptr_t) &header
) + sizeof(kd_chunk_header_v3
));
2558 size_t payload_size
= (sizeof(kd_header_v3
) - sizeof(kd_chunk_header_v3
));
2560 ret
= kdbg_write_v3_chunk_to_fd(RAW_VERSION3
, V3_HEADER_VERSION
, header
.length
, hdr_ptr
, payload_size
, fd
);
2566 if (copyout(&header
, user_header
, sizeof(kd_header_v3
))) {
2570 // Update the user pointer
2571 user_header
+= sizeof(kd_header_v3
);
2574 // Write a cpu map. This is a sub chunk of the header
2575 cpumap
= (uint8_t*)((uintptr_t) cpumap
+ sizeof(kd_cpumap_header
));
2576 size_t payload_size
= (size_t)(cpumap_size
- sizeof(kd_cpumap_header
));
2578 ret
= kdbg_write_v3_chunk_to_fd(V3_CPU_MAP
, V3_CPUMAP_VERSION
, payload_size
, (void *)cpumap
, payload_size
, fd
);
2584 ret
= kdbg_write_v3_chunk_header(user_header
, V3_CPU_MAP
, V3_CPUMAP_VERSION
, payload_size
, NULL
, NULL
);
2588 user_header
+= sizeof(kd_chunk_header_v3
);
2589 if (copyout(cpumap
, user_header
, payload_size
)) {
2593 // Update the user pointer
2594 user_header
+= payload_size
;
2597 // Write a thread map
2599 ret
= kdbg_write_v3_chunk_to_fd(V3_THREAD_MAP
, V3_THRMAP_VERSION
, thrmap_size
, (void *)kd_mapptr
, thrmap_size
, fd
);
2605 ret
= kdbg_write_v3_chunk_header(user_header
, V3_THREAD_MAP
, V3_THRMAP_VERSION
, thrmap_size
, NULL
, NULL
);
2609 user_header
+= sizeof(kd_chunk_header_v3
);
2610 if (copyout(kd_mapptr
, user_header
, thrmap_size
)) {
2614 user_header
+= thrmap_size
;
2618 RAW_file_written
+= bytes_needed
;
2621 *user_header_size
= bytes_needed
;
2624 kmem_free(kernel_map
, (vm_offset_t
)cpumap
, cpumap_size
);
2630 kdbg_readcpumap(user_addr_t user_cpumap
, size_t *user_cpumap_size
)
2632 uint8_t* cpumap
= NULL
;
2633 uint32_t cpumap_size
= 0;
2634 int ret
= KERN_SUCCESS
;
2636 if (kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) {
2637 if (kdbg_cpumap_init_internal(kd_ctrl_page
.kdebug_iops
, kd_ctrl_page
.kdebug_cpus
, &cpumap
, &cpumap_size
) == KERN_SUCCESS
) {
2639 size_t bytes_to_copy
= (*user_cpumap_size
>= cpumap_size
) ? cpumap_size
: *user_cpumap_size
;
2640 if (copyout(cpumap
, user_cpumap
, (size_t)bytes_to_copy
)) {
2644 *user_cpumap_size
= cpumap_size
;
2645 kmem_free(kernel_map
, (vm_offset_t
)cpumap
, cpumap_size
);
2655 kdbg_readcurthrmap(user_addr_t buffer
, size_t *bufsize
)
2657 kd_threadmap
*mapptr
;
2658 unsigned int mapsize
;
2659 unsigned int mapcount
;
2660 unsigned int count
= 0;
2663 count
= *bufsize
/sizeof(kd_threadmap
);
2666 if ( (mapptr
= kdbg_thrmap_init_internal(count
, &mapsize
, &mapcount
)) ) {
2667 if (copyout(mapptr
, buffer
, mapcount
* sizeof(kd_threadmap
)))
2670 *bufsize
= (mapcount
* sizeof(kd_threadmap
));
2672 kmem_free(kernel_map
, (vm_offset_t
)mapptr
, mapsize
);
2680 kdbg_write_v1_plus_header(uint32_t count
, vnode_t vp
, vfs_context_t ctx
)
2688 uint32_t extra_thread_count
= 0;
2689 uint32_t cpumap_size
;
2690 unsigned int mapsize
= kd_mapcount
* sizeof(kd_threadmap
);
2693 * To write a RAW_VERSION1+ file, we
2694 * must embed a cpumap in the "padding"
2695 * used to page align the events following
2696 * the threadmap. If the threadmap happens
2697 * to not require enough padding, we
2698 * artificially increase its footprint
2699 * until it needs enough padding.
2705 pad_size
= PAGE_16KB
- ((sizeof(RAW_header
) + (count
* sizeof(kd_threadmap
))) & PAGE_MASK_64
);
2706 cpumap_size
= sizeof(kd_cpumap_header
) + kd_ctrl_page
.kdebug_cpus
* sizeof(kd_cpumap
);
2708 if (cpumap_size
> pad_size
) {
2709 /* If the cpu map doesn't fit in the current available pad_size,
2710 * we increase the pad_size by 16K. We do this so that the event
2711 * data is always available on a page aligned boundary for both
2712 * 4k and 16k systems. We enforce this alignment for the event
2713 * data so that we can take advantage of optimized file/disk writes.*/
2714 pad_size
+= PAGE_16KB
;
2717 /* The way we are silently embedding a cpumap in the "padding" is by artificially
2718 * increasing the number of thread entries. However, we'll also need to ensure that
2719 * the cpumap is embedded in the last 4K page before when the event data is expected.
2720 * This way the tools can read the data starting the next page boundary on both
2721 * 4K and 16K systems preserving compatibility with older versions of the tools
2723 if (pad_size
> PAGE_4KB
) {
2724 pad_size
-= PAGE_4KB
;
2725 extra_thread_count
= (pad_size
/ sizeof(kd_threadmap
)) + 1;
2728 header
.version_no
= RAW_VERSION1
;
2729 header
.thread_count
= count
+ extra_thread_count
;
2731 clock_get_calendar_microtime(&secs
, &usecs
);
2732 header
.TOD_secs
= secs
;
2733 header
.TOD_usecs
= usecs
;
2735 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)&header
, sizeof(RAW_header
), RAW_file_offset
,
2736 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2739 RAW_file_offset
+= sizeof(RAW_header
);
2741 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)kd_mapptr
, mapsize
, RAW_file_offset
,
2742 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2745 RAW_file_offset
+= mapsize
;
2747 if (extra_thread_count
) {
2748 pad_size
= extra_thread_count
* sizeof(kd_threadmap
);
2749 pad_buf
= (char *)kalloc(pad_size
);
2754 memset(pad_buf
, 0, pad_size
);
2756 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)pad_buf
, pad_size
, RAW_file_offset
,
2757 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2758 kfree(pad_buf
, pad_size
);
2762 RAW_file_offset
+= pad_size
;
2766 pad_size
= PAGE_SIZE
- (RAW_file_offset
& PAGE_MASK_64
);
2768 pad_buf
= (char *)kalloc(pad_size
);
2773 memset(pad_buf
, 0, pad_size
);
2776 * embed a cpumap in the padding bytes.
2777 * older code will skip this.
2778 * newer code will know how to read it.
2780 uint32_t temp
= pad_size
;
2781 if (kdbg_cpumap_init_internal(kd_ctrl_page
.kdebug_iops
, kd_ctrl_page
.kdebug_cpus
, (uint8_t**)&pad_buf
, &temp
) != KERN_SUCCESS
) {
2782 memset(pad_buf
, 0, pad_size
);
2785 ret
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)pad_buf
, pad_size
, RAW_file_offset
,
2786 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, vfs_context_ucred(ctx
), (int *) 0, vfs_context_proc(ctx
));
2787 kfree(pad_buf
, pad_size
);
2791 RAW_file_offset
+= pad_size
;
2793 RAW_file_written
+= sizeof(RAW_header
) + mapsize
+ pad_size
;
2800 kdbg_readthrmap(user_addr_t buffer
, size_t *number
, vnode_t vp
, vfs_context_t ctx
)
2806 unsigned int mapsize
;
2808 if ((!vp
&& !buffer
) || (vp
&& buffer
)) {
2813 assert((vp
== NULL
) || (ctx
!= NULL
));
2816 count
= avail
/sizeof (kd_threadmap
);
2817 mapsize
= kd_mapcount
* sizeof(kd_threadmap
);
2819 if (count
&& (count
<= kd_mapcount
)) {
2820 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
2821 if (*number
< mapsize
)
2825 ret
= kdbg_write_v1_plus_header(count
, vp
, ctx
);
2830 if (copyout(kd_mapptr
, buffer
, mapsize
))
2845 ret
= kdbg_write_to_vnode((caddr_t
)&count
, sizeof(uint32_t), vp
, ctx
, RAW_file_offset
);
2847 RAW_file_offset
+= sizeof(uint32_t);
2848 RAW_file_written
+= sizeof(uint32_t);
2852 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
)
2854 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
2855 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
2857 kd_mapptr
= (kd_threadmap
*) 0;
2864 kdbg_readthrmap_v3(user_addr_t buffer
, size_t *number
, int fd
)
2869 unsigned int mapsize
;
2871 if ((!fd
&& !buffer
) || (fd
&& buffer
)) {
2878 count
= avail
/sizeof (kd_threadmap
);
2879 mapsize
= kd_mapcount
* sizeof(kd_threadmap
);
2881 if (count
&& (count
<= kd_mapcount
)) {
2882 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
2883 if (*number
< mapsize
) {
2887 ret
= kdbg_write_v3_header(buffer
, number
, fd
);
2901 if ((kd_ctrl_page
.kdebug_flags
& KDBG_MAPINIT
) && kd_mapsize
&& kd_mapptr
) {
2902 kmem_free(kernel_map
, (vm_offset_t
)kd_mapptr
, kd_mapsize
);
2903 kd_ctrl_page
.kdebug_flags
&= ~KDBG_MAPINIT
;
2905 kd_mapptr
= (kd_threadmap
*) 0;
2913 kdbg_set_nkdbufs(unsigned int value
)
2916 * We allow a maximum buffer size of 50% of either ram or max mapped address, whichever is smaller
2917 * 'value' is the desired number of trace entries
2919 unsigned int max_entries
= (sane_size
/2) / sizeof(kd_buf
);
2921 if (value
<= max_entries
)
2924 return (max_entries
);
2929 kdbg_enable_bg_trace(void)
2933 if (kdlog_bg_trace
== TRUE
&& kdlog_bg_trace_running
== FALSE
&& n_storage_buffers
== 0) {
2934 nkdbufs
= bg_nkdbufs
;
2935 ret
= kdbg_reinit(FALSE
);
2937 kdbg_set_tracing_enabled(TRUE
, KDEBUG_ENABLE_TRACE
);
2938 kdlog_bg_trace_running
= TRUE
;
2940 wakeup(&kdlog_bg_trace
);
2946 kdbg_disable_bg_trace(void)
2948 if (kdlog_bg_trace_running
== TRUE
) {
2949 kdlog_bg_trace_running
= FALSE
;
2957 * This function is provided for the CHUD toolkit only.
2959 * zero disables kdebug_chudhook function call
2960 * non-zero enables kdebug_chudhook function call
2962 * address of the enabled kdebug_chudhook function
2966 kdbg_control_chud(int val
, void *fn
)
2971 /* enable chudhook */
2972 kdebug_chudhook
= fn
;
2973 kdbg_set_flags(SLOW_CHUD
, KDEBUG_ENABLE_CHUD
, TRUE
);
2976 /* disable chudhook */
2977 kdbg_set_flags(SLOW_CHUD
, KDEBUG_ENABLE_CHUD
, FALSE
);
2978 kdebug_chudhook
= 0;
2984 kdbg_control(int *name
, u_int namelen
, user_addr_t where
, size_t *sizep
)
2987 size_t size
= *sizep
;
2988 unsigned int value
= 0;
2990 kbufinfo_t kd_bufinfo
;
2994 if (name
[0] == KERN_KDGETENTROPY
||
2995 name
[0] == KERN_KDWRITETR
||
2996 name
[0] == KERN_KDWRITETR_V3
||
2997 name
[0] == KERN_KDWRITEMAP
||
2998 name
[0] == KERN_KDWRITEMAP_V3
||
2999 name
[0] == KERN_KDEFLAGS
||
3000 name
[0] == KERN_KDDFLAGS
||
3001 name
[0] == KERN_KDENABLE
||
3002 name
[0] == KERN_KDENABLE_BG_TRACE
||
3003 name
[0] == KERN_KDSETBUF
) {
3012 if ( !(kd_ctrl_page
.kdebug_flags
& KDBG_LOCKINIT
))
3015 lck_mtx_lock(kd_trace_mtx_sysctl
);
3020 * Does not alter the global_state_pid
3021 * This is a passive request.
3023 if (size
< sizeof(kd_bufinfo
.nkdbufs
)) {
3025 * There is not enough room to return even
3026 * the first element of the info structure.
3031 kd_bufinfo
.nkdbufs
= nkdbufs
;
3032 kd_bufinfo
.nkdthreads
= kd_mapcount
;
3034 if ( (kd_ctrl_page
.kdebug_slowcheck
& SLOW_NOLOG
) )
3035 kd_bufinfo
.nolog
= 1;
3037 kd_bufinfo
.nolog
= 0;
3039 kd_bufinfo
.flags
= kd_ctrl_page
.kdebug_flags
;
3040 #if defined(__LP64__)
3041 kd_bufinfo
.flags
|= KDBG_LP64
;
3043 kd_bufinfo
.bufid
= global_state_pid
;
3045 if (size
>= sizeof(kd_bufinfo
)) {
3047 * Provide all the info we have
3049 if (copyout(&kd_bufinfo
, where
, sizeof(kd_bufinfo
)))
3053 * For backwards compatibility, only provide
3054 * as much info as there is room for.
3056 if (copyout(&kd_bufinfo
, where
, size
))
3060 case KERN_KDGETENTROPY
: {
3061 /* Obsolescent - just fake with a random buffer */
3062 char *buffer
= (char *) kalloc(size
);
3063 read_frandom((void *) buffer
, size
);
3064 ret
= copyout(buffer
, where
, size
);
3065 kfree(buffer
, size
);
3069 case KERN_KDENABLE_BG_TRACE
:
3070 bg_nkdbufs
= kdbg_set_nkdbufs(value
);
3071 kdlog_bg_trace
= TRUE
;
3072 ret
= kdbg_enable_bg_trace();
3075 case KERN_KDDISABLE_BG_TRACE
:
3076 kdlog_bg_trace
= FALSE
;
3077 kdbg_disable_bg_trace();
3080 case KERN_KDWAIT_BG_TRACE_RESET
:
3081 if (!kdlog_bg_trace
){
3085 wait_result_t wait_result
= assert_wait(&kdlog_bg_trace
, THREAD_ABORTSAFE
);
3086 lck_mtx_unlock(kd_trace_mtx_sysctl
);
3087 if (wait_result
== THREAD_WAITING
)
3088 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
3089 if (wait_result
== THREAD_INTERRUPTED
)
3091 lck_mtx_lock(kd_trace_mtx_sysctl
);
3094 case KERN_KDSET_BG_TYPEFILTER
:
3095 if (!kdlog_bg_trace
|| !kdlog_bg_trace_running
){
3100 if (size
!= KDBG_TYPEFILTER_BITMAP_SIZE
) {
3105 if ((kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) == 0){
3106 if ((ret
= kdbg_enable_typefilter()))
3110 if (copyin(where
, type_filter_bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
)) {
3114 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_TYPEFILTER_CHANGED
, type_filter_bitmap
);
3118 if ((curproc
= current_proc()) != NULL
)
3119 curpid
= curproc
->p_pid
;
3124 if (global_state_pid
== -1)
3125 global_state_pid
= curpid
;
3126 else if (global_state_pid
!= curpid
) {
3127 if ((p
= proc_find(global_state_pid
)) == NULL
) {
3129 * The global pid no longer exists
3131 global_state_pid
= curpid
;
3134 * The global pid exists, deny this request
3145 kdbg_disable_bg_trace();
3147 value
&= KDBG_USERFLAGS
;
3148 kd_ctrl_page
.kdebug_flags
|= value
;
3151 kdbg_disable_bg_trace();
3153 value
&= KDBG_USERFLAGS
;
3154 kd_ctrl_page
.kdebug_flags
&= ~value
;
3158 * Enable tracing mechanism. Two types:
3159 * KDEBUG_TRACE is the standard one,
3160 * and KDEBUG_PPT which is a carefully
3161 * chosen subset to avoid performance impact.
3165 * enable only if buffer is initialized
3167 if (!(kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) ||
3168 !(value
== KDEBUG_ENABLE_TRACE
|| value
== KDEBUG_ENABLE_PPT
)) {
3174 kdbg_set_tracing_enabled(TRUE
, value
);
3178 kdbg_set_tracing_enabled(FALSE
, 0);
3182 kdbg_disable_bg_trace();
3184 nkdbufs
= kdbg_set_nkdbufs(value
);
3187 kdbg_disable_bg_trace();
3189 ret
= kdbg_reinit(FALSE
);
3193 ret
= kdbg_enable_bg_trace();
3196 if(size
< sizeof(kd_regtype
)) {
3200 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3204 kdbg_disable_bg_trace();
3206 ret
= kdbg_setreg(&kd_Reg
);
3209 if (size
< sizeof(kd_regtype
)) {
3213 ret
= kdbg_getreg(&kd_Reg
);
3214 if (copyout(&kd_Reg
, where
, sizeof(kd_regtype
))) {
3217 kdbg_disable_bg_trace();
3221 ret
= kdbg_read(where
, sizep
, NULL
, NULL
, RAW_VERSION1
);
3223 case KERN_KDWRITETR
:
3224 case KERN_KDWRITETR_V3
:
3225 case KERN_KDWRITEMAP
:
3226 case KERN_KDWRITEMAP_V3
:
3228 struct vfs_context context
;
3229 struct fileproc
*fp
;
3234 if (name
[0] == KERN_KDWRITETR
|| name
[0] == KERN_KDWRITETR_V3
) {
3236 int wait_result
= THREAD_AWAKENED
;
3241 ns
= ((u_int64_t
)*sizep
) * (u_int64_t
)(1000 * 1000);
3242 nanoseconds_to_absolutetime(ns
, &abstime
);
3243 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
3247 s
= ml_set_interrupts_enabled(FALSE
);
3248 lck_spin_lock(kdw_spin_lock
);
3250 while (wait_result
== THREAD_AWAKENED
&& kd_ctrl_page
.kds_inuse_count
< n_storage_threshold
) {
3255 wait_result
= lck_spin_sleep_deadline(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
, abstime
);
3257 wait_result
= lck_spin_sleep(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
);
3261 lck_spin_unlock(kdw_spin_lock
);
3262 ml_set_interrupts_enabled(s
);
3268 if ( (ret
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3272 context
.vc_thread
= current_thread();
3273 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
3275 if (FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_VNODE
) {
3276 fp_drop(p
, fd
, fp
, 1);
3282 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
3285 if ((ret
= vnode_getwithref(vp
)) == 0) {
3286 RAW_file_offset
= fp
->f_fglob
->fg_offset
;
3287 if (name
[0] == KERN_KDWRITETR
|| name
[0] == KERN_KDWRITETR_V3
) {
3288 number
= nkdbufs
* sizeof(kd_buf
);
3290 KERNEL_DEBUG_CONSTANT(TRACE_WRITING_EVENTS
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
3291 if (name
[0] == KERN_KDWRITETR_V3
)
3292 ret
= kdbg_read(0, &number
, vp
, &context
, RAW_VERSION3
);
3294 ret
= kdbg_read(0, &number
, vp
, &context
, RAW_VERSION1
);
3295 KERNEL_DEBUG_CONSTANT(TRACE_WRITING_EVENTS
| DBG_FUNC_END
, number
, 0, 0, 0, 0);
3299 number
= kd_mapcount
* sizeof(kd_threadmap
);
3300 if (name
[0] == KERN_KDWRITEMAP_V3
)
3301 kdbg_readthrmap_v3(0, &number
, fd
);
3303 kdbg_readthrmap(0, &number
, vp
, &context
);
3305 fp
->f_fglob
->fg_offset
= RAW_file_offset
;
3308 fp_drop(p
, fd
, fp
, 0);
3312 case KERN_KDBUFWAIT
:
3314 /* WRITETR lite -- just block until there's data */
3316 int wait_result
= THREAD_AWAKENED
;
3321 kdbg_disable_bg_trace();
3325 ns
= ((u_int64_t
)*sizep
) * (u_int64_t
)(1000 * 1000);
3326 nanoseconds_to_absolutetime(ns
, &abstime
);
3327 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
3331 s
= ml_set_interrupts_enabled(FALSE
);
3333 panic("trying to wait with interrupts off");
3334 lck_spin_lock(kdw_spin_lock
);
3336 /* drop the mutex so don't exclude others from
3339 lck_mtx_unlock(kd_trace_mtx_sysctl
);
3341 while (wait_result
== THREAD_AWAKENED
&&
3342 kd_ctrl_page
.kds_inuse_count
< n_storage_threshold
) {
3347 wait_result
= lck_spin_sleep_deadline(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
, abstime
);
3349 wait_result
= lck_spin_sleep(kdw_spin_lock
, 0, &kds_waiter
, THREAD_ABORTSAFE
);
3354 /* check the count under the spinlock */
3355 number
= (kd_ctrl_page
.kds_inuse_count
>= n_storage_threshold
);
3357 lck_spin_unlock(kdw_spin_lock
);
3358 ml_set_interrupts_enabled(s
);
3360 /* pick the mutex back up again */
3361 lck_mtx_lock(kd_trace_mtx_sysctl
);
3363 /* write out whether we've exceeded the threshold */
3368 if (size
< sizeof(kd_regtype
)) {
3372 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3376 kdbg_disable_bg_trace();
3378 ret
= kdbg_setpid(&kd_Reg
);
3381 if (size
< sizeof(kd_regtype
)) {
3385 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3389 kdbg_disable_bg_trace();
3391 ret
= kdbg_setpidex(&kd_Reg
);
3394 ret
= kdbg_readcpumap(where
, sizep
);
3397 ret
= kdbg_readthrmap(where
, sizep
, NULL
, NULL
);
3399 case KERN_KDREADCURTHRMAP
:
3400 ret
= kdbg_readcurthrmap(where
, sizep
);
3402 case KERN_KDSETRTCDEC
:
3403 if (size
< sizeof(kd_regtype
)) {
3407 if (copyin(where
, &kd_Reg
, sizeof(kd_regtype
))) {
3411 kdbg_disable_bg_trace();
3413 ret
= kdbg_setrtcdec(&kd_Reg
);
3415 case KERN_KDSET_TYPEFILTER
:
3416 kdbg_disable_bg_trace();
3418 if (size
!= KDBG_TYPEFILTER_BITMAP_SIZE
) {
3423 if ((kd_ctrl_page
.kdebug_flags
& KDBG_TYPEFILTER_CHECK
) == 0){
3424 if ((ret
= kdbg_enable_typefilter()))
3428 if (copyin(where
, type_filter_bitmap
, KDBG_TYPEFILTER_BITMAP_SIZE
)) {
3432 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_TYPEFILTER_CHANGED
, type_filter_bitmap
);
3438 lck_mtx_unlock(kd_trace_mtx_sysctl
);
3445 * This code can run for the most part concurrently with kernel_debug_internal()...
3446 * 'release_storage_unit' will take the kds_spin_lock which may cause us to briefly
3447 * synchronize with the recording side of this puzzle... otherwise, we are able to
3448 * move through the lists w/o use of any locks
3451 kdbg_read(user_addr_t buffer
, size_t *number
, vnode_t vp
, vfs_context_t ctx
, uint32_t file_version
)
3454 unsigned int cpu
, min_cpu
;
3455 uint64_t mintime
, t
, barrier
= 0;
3461 struct kd_storage
*kdsp_actual
;
3462 struct kd_bufinfo
*kdbp
;
3463 struct kd_bufinfo
*min_kdbp
;
3464 uint32_t tempbuf_count
;
3465 uint32_t tempbuf_number
;
3466 uint32_t old_kdebug_flags
;
3467 uint32_t old_kdebug_slowcheck
;
3468 boolean_t lostevents
= FALSE
;
3469 boolean_t out_of_events
= FALSE
;
3472 count
= *number
/sizeof(kd_buf
);
3475 if (count
== 0 || !(kd_ctrl_page
.kdebug_flags
& KDBG_BUFINIT
) || kdcopybuf
== 0)
3478 memset(&lostevent
, 0, sizeof(lostevent
));
3479 lostevent
.debugid
= TRACE_LOST_EVENTS
;
3481 /* Capture timestamp. Only sort events that have occured before the timestamp.
3482 * Since the iop is being flushed here, its possible that events occur on the AP
3483 * while running live tracing. If we are disabled, no new events should
3487 if (kd_ctrl_page
.enabled
)
3489 // timestamp is non-zero value
3490 barrier
= mach_absolute_time() & KDBG_TIMESTAMP_MASK
;
3493 // Request each IOP to provide us with up to date entries before merging buffers together.
3494 kdbg_iop_list_callback(kd_ctrl_page
.kdebug_iops
, KD_CALLBACK_SYNC_FLUSH
, NULL
);
3497 * because we hold kd_trace_mtx_sysctl, no other control threads can
3498 * be playing with kdebug_flags... the code that cuts new events could
3499 * be running, but it grabs kds_spin_lock if it needs to acquire a new
3500 * storage chunk which is where it examines kdebug_flags... it its adding
3501 * to the same chunk we're reading from, no problem...
3504 disable_wrap(&old_kdebug_slowcheck
, &old_kdebug_flags
);
3506 if (count
> nkdbufs
)
3509 if ((tempbuf_count
= count
) > KDCOPYBUF_COUNT
)
3510 tempbuf_count
= KDCOPYBUF_COUNT
;
3513 tempbuf
= kdcopybuf
;
3517 while (tempbuf_count
) {
3518 mintime
= 0xffffffffffffffffULL
;
3523 for (cpu
= 0, kdbp
= &kdbip
[0]; cpu
< kd_ctrl_page
.kdebug_cpus
; cpu
++, kdbp
++) {
3525 // Find one with raw data
3526 if ((kdsp
= kdbp
->kd_list_head
).raw
== KDS_PTR_NULL
)
3528 /* Debugging aid: maintain a copy of the "kdsp"
3531 volatile union kds_ptr kdsp_shadow
;
3535 // Get from cpu data to buffer header to buffer
3536 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
3538 volatile struct kd_storage
*kdsp_actual_shadow
;
3540 kdsp_actual_shadow
= kdsp_actual
;
3542 // See if there are actual data left in this buffer
3543 rcursor
= kdsp_actual
->kds_readlast
;
3545 if (rcursor
== kdsp_actual
->kds_bufindx
)
3548 t
= kdbg_get_timestamp(&kdsp_actual
->kds_records
[rcursor
]);
3550 if ((t
> barrier
) && (barrier
> 0)) {
3552 * Need to wait to flush iop again before we
3553 * sort any more data from the buffers
3555 out_of_events
= TRUE
;
3558 if (t
< kdsp_actual
->kds_timestamp
) {
3560 * indicates we've not yet completed filling
3562 * this should only occur when we're looking
3563 * at the buf that the record head is utilizing
3564 * we'll pick these events up on the next
3566 * we bail at this point so that we don't
3567 * get an out-of-order timestream by continuing
3568 * to read events from the other CPUs' timestream(s)
3570 out_of_events
= TRUE
;
3579 if (min_kdbp
== NULL
|| out_of_events
== TRUE
) {
3581 * all buffers ran empty
3583 out_of_events
= TRUE
;
3588 kdsp
= min_kdbp
->kd_list_head
;
3589 kdsp_actual
= POINTER_FROM_KDS_PTR(kdsp
);
3591 if (kdsp_actual
->kds_lostevents
== TRUE
) {
3592 kdbg_set_timestamp_and_cpu(&lostevent
, kdsp_actual
->kds_records
[kdsp_actual
->kds_readlast
].timestamp
, min_cpu
);
3593 *tempbuf
= lostevent
;
3595 kdsp_actual
->kds_lostevents
= FALSE
;
3602 *tempbuf
= kdsp_actual
->kds_records
[kdsp_actual
->kds_readlast
++];
3604 if (kdsp_actual
->kds_readlast
== EVENTS_PER_STORAGE_UNIT
)
3605 release_storage_unit(min_cpu
, kdsp
.raw
);
3608 * Watch for out of order timestamps
3610 if (mintime
< min_kdbp
->kd_prev_timebase
) {
3612 * if so, use the previous timestamp + 1 cycle
3614 min_kdbp
->kd_prev_timebase
++;
3615 kdbg_set_timestamp_and_cpu(tempbuf
, min_kdbp
->kd_prev_timebase
, kdbg_get_cpu(tempbuf
));
3617 min_kdbp
->kd_prev_timebase
= mintime
;
3623 if ((RAW_file_written
+= sizeof(kd_buf
)) >= RAW_FLUSH_SIZE
)
3626 if (tempbuf_number
) {
3627 if (file_version
== RAW_VERSION3
) {
3628 if ( !(kdbg_write_v3_event_chunk_header(buffer
, V3_RAW_EVENTS
, (tempbuf_number
* sizeof(kd_buf
)), vp
, ctx
))) {
3633 buffer
+= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t));
3635 assert(count
>= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t)));
3636 count
-= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t));
3637 *number
+= (sizeof(kd_chunk_header_v3
) + sizeof(uint64_t));
3640 size_t write_size
= tempbuf_number
* sizeof(kd_buf
);
3641 error
= kdbg_write_to_vnode((caddr_t
)kdcopybuf
, write_size
, vp
, ctx
, RAW_file_offset
);
3643 RAW_file_offset
+= write_size
;
3645 if (RAW_file_written
>= RAW_FLUSH_SIZE
) {
3646 cluster_push(vp
, 0);
3648 RAW_file_written
= 0;
3651 error
= copyout(kdcopybuf
, buffer
, tempbuf_number
* sizeof(kd_buf
));
3652 buffer
+= (tempbuf_number
* sizeof(kd_buf
));
3660 count
-= tempbuf_number
;
3661 *number
+= tempbuf_number
;
3663 if (out_of_events
== TRUE
)
3665 * all trace buffers are empty
3669 if ((tempbuf_count
= count
) > KDCOPYBUF_COUNT
)
3670 tempbuf_count
= KDCOPYBUF_COUNT
;
3672 if ( !(old_kdebug_flags
& KDBG_NOWRAP
)) {
3673 enable_wrap(old_kdebug_slowcheck
, lostevents
);
3679 unsigned char *getProcName(struct proc
*proc
);
3680 unsigned char *getProcName(struct proc
*proc
) {
3682 return (unsigned char *) &proc
->p_comm
; /* Return pointer to the proc name */
3687 stackshot_kern_return_to_bsd_error(kern_return_t kr
)
3692 case KERN_RESOURCE_SHORTAGE
:
3696 case KERN_NO_ACCESS
:
3698 case KERN_MEMORY_PRESENT
:
3700 case KERN_NOT_SUPPORTED
:
3702 case KERN_NOT_IN_SET
:
3711 * DEPRECATION WARNING: THIS SYSCALL IS BEING REPLACED WITH SYS_stack_snapshot_with_config and SYS_microstackshot.
3713 * stack_snapshot: Obtains a coherent set of stack traces for all threads
3714 * on the system, tracing both kernel and user stacks
3715 * where available. Uses machine specific trace routines
3716 * for ppc, ppc64 and x86.
3717 * Inputs: uap->pid - process id of process to be traced, or -1
3718 * for the entire system
3719 * uap->tracebuf - address of the user space destination
3721 * uap->tracebuf_size - size of the user space trace buffer
3722 * uap->options - various options, including the maximum
3723 * number of frames to trace.
3724 * Outputs: EPERM if the caller is not privileged
3725 * EINVAL if the supplied trace buffer isn't sanely sized
3726 * ENOMEM if we don't have enough memory to satisfy the
3728 * ENOENT if the target pid isn't found
3729 * ENOSPC if the supplied buffer is insufficient
3730 * *retval contains the number of bytes traced, if successful
3731 * and -1 otherwise. If the request failed due to
3732 * tracebuffer exhaustion, we copyout as much as possible.
3735 stack_snapshot(struct proc
*p
, register struct stack_snapshot_args
*uap
, int32_t *retval
) {
3739 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
3742 kr
= stack_snapshot2(uap
->pid
, uap
->tracebuf
, uap
->tracebuf_size
, uap
->flags
, retval
);
3743 return stackshot_kern_return_to_bsd_error(kr
);
3747 * stack_snapshot_with_config: Obtains a coherent set of stack traces for specified threads on the sysem,
3748 * tracing both kernel and user stacks where available. Allocates a buffer from the
3749 * kernel and maps the buffer into the calling task's address space.
3751 * Inputs: uap->stackshot_config_version - version of the stackshot config that is being passed
3752 * uap->stackshot_config - pointer to the stackshot config
3753 * uap->stackshot_config_size- size of the stackshot config being passed
3754 * Outputs: EINVAL if there is a problem with the arguments
3755 * EFAULT if we failed to copy in the arguments succesfully
3756 * EPERM if the caller is not privileged
3757 * ENOTSUP if the caller is passing a version of arguments that is not supported by the kernel
3758 * (indicates libsyscall:kernel mismatch) or if the caller is requesting unsupported flags
3759 * ENOENT if the caller is requesting an existing buffer that doesn't exist or if the
3760 * requested PID isn't found
3761 * ENOMEM if the kernel is unable to allocate enough memory to serve the request
3762 * ENOSPC if there isn't enough space in the caller's address space to remap the buffer
3763 * ESRCH if the target PID isn't found
3764 * returns KERN_SUCCESS on success
3767 stack_snapshot_with_config(struct proc
*p
, struct stack_snapshot_with_config_args
*uap
, __unused
int *retval
)
3772 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
3775 if((void*)uap
->stackshot_config
== NULL
) {
3779 switch (uap
->stackshot_config_version
) {
3780 case STACKSHOT_CONFIG_TYPE
:
3781 if (uap
->stackshot_config_size
!= sizeof(stackshot_config_t
)) {
3784 stackshot_config_t config
;
3785 error
= copyin(uap
->stackshot_config
, &config
, sizeof(stackshot_config_t
));
3786 if (error
!= KERN_SUCCESS
)
3790 kr
= kern_stack_snapshot_internal(uap
->stackshot_config_version
, &config
, sizeof(stackshot_config_t
), TRUE
);
3791 return stackshot_kern_return_to_bsd_error(kr
);
3797 #if CONFIG_TELEMETRY
3799 * microstackshot: Catch all system call for microstackshot related operations, including
3800 * enabling/disabling both global and windowed microstackshots as well
3801 * as retrieving windowed or global stackshots and the boot profile.
3802 * Inputs: uap->tracebuf - address of the user space destination
3804 * uap->tracebuf_size - size of the user space trace buffer
3805 * uap->flags - various flags
3806 * Outputs: EPERM if the caller is not privileged
3807 * EINVAL if the supplied mss_args is NULL, mss_args.tracebuf is NULL or mss_args.tracebuf_size is not sane
3808 * ENOMEM if we don't have enough memory to satisfy the request
3809 * *retval contains the number of bytes traced, if successful
3813 microstackshot(struct proc
*p
, struct microstackshot_args
*uap
, int32_t *retval
)
3818 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
3821 kr
= stack_microstackshot(uap
->tracebuf
, uap
->tracebuf_size
, uap
->flags
, retval
);
3822 return stackshot_kern_return_to_bsd_error(kr
);
3824 #endif /* CONFIG_TELEMETRY */
3827 * kern_stack_snapshot_with_reason: Obtains a coherent set of stack traces for specified threads on the sysem,
3828 * tracing both kernel and user stacks where available. Allocates a buffer from the
3829 * kernel and stores the address of this buffer.
3831 * Inputs: reason - the reason for triggering a stackshot (unused at the moment, but in the
3832 * future will be saved in the stackshot)
3833 * Outputs: EINVAL/ENOTSUP if there is a problem with the arguments
3834 * EPERM if the caller doesn't pass at least one KERNEL stackshot flag
3835 * ENOMEM if the kernel is unable to allocate enough memory to serve the request
3836 * ESRCH if the target PID isn't found
3837 * returns KERN_SUCCESS on success
3840 kern_stack_snapshot_with_reason(__unused
char *reason
)
3842 stackshot_config_t config
;
3846 config
.sc_flags
= (STACKSHOT_SAVE_LOADINFO
| STACKSHOT_GET_GLOBAL_MEM_STATS
| STACKSHOT_SAVE_IN_KERNEL_BUFFER
|
3847 STACKSHOT_KCDATA_FORMAT
);
3848 config
.sc_since_timestamp
= 0;
3849 config
.sc_out_buffer_addr
= 0;
3850 config
.sc_out_size_addr
= 0;
3852 kr
= kern_stack_snapshot_internal(STACKSHOT_CONFIG_TYPE
, &config
, sizeof(stackshot_config_t
), FALSE
);
3853 return stackshot_kern_return_to_bsd_error(kr
);
3857 * stack_snapshot_from_kernel: Stackshot function for kernel consumers who have their own buffer.
3859 * Inputs: pid - the PID to be traced or -1 for the whole system
3860 * buf - a pointer to the buffer where the stackshot should be written
3861 * size - the size of the buffer
3862 * flags - flags to be passed to the stackshot
3863 * *bytes_traced - a pointer to be filled with the length of the stackshot
3864 * Outputs: -1 if there is a problem with the arguments
3865 * the error returned by the stackshot code otherwise
3868 stack_snapshot_from_kernel(pid_t pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *bytes_traced
)
3872 kr
= stack_snapshot_from_kernel_internal(pid
, buf
, size
, flags
, bytes_traced
);
3873 if (kr
== KERN_FAILURE
) {
3881 start_kern_tracing(unsigned int new_nkdbufs
, boolean_t need_map
)
3886 nkdbufs
= kdbg_set_nkdbufs(new_nkdbufs
);
3889 kernel_debug_string_simple("start_kern_tracing");
3891 if (0 == kdbg_reinit(TRUE
)) {
3893 if (need_map
== TRUE
) {
3894 uint32_t old1
, old2
;
3898 disable_wrap(&old1
, &old2
);
3901 /* Hold off interrupts until the early traces are cut */
3902 boolean_t s
= ml_set_interrupts_enabled(FALSE
);
3904 kdbg_set_tracing_enabled(
3907 (KDEBUG_ENABLE_TRACE
| KDEBUG_ENABLE_SERIAL
) :
3908 KDEBUG_ENABLE_TRACE
);
3911 * Transfer all very early events from the static buffer
3912 * into the real buffers.
3914 kernel_debug_early_end();
3916 ml_set_interrupts_enabled(s
);
3918 printf("kernel tracing started\n");
3919 #if KDEBUG_MOJO_TRACE
3920 if (kdebug_serial
) {
3921 printf("serial output enabled with %lu named events\n",
3922 sizeof(kd_events
)/sizeof(kd_event_t
));
3926 printf("error from kdbg_reinit, kernel tracing not started\n");
3931 start_kern_tracing_with_typefilter(unsigned int new_nkdbufs
,
3933 unsigned int typefilter
)
3935 /* startup tracing */
3936 start_kern_tracing(new_nkdbufs
, need_map
);
3938 /* check that tracing was actually enabled */
3939 if (!(kdebug_enable
& KDEBUG_ENABLE_TRACE
))
3942 /* setup the typefiltering */
3943 if (0 == kdbg_enable_typefilter())
3944 setbit(type_filter_bitmap
,
3945 typefilter
& (KDBG_CSC_MASK
>> KDBG_CSC_OFFSET
));
3949 kdbg_dump_trace_to_file(const char *filename
)
3957 if ( !(kdebug_enable
& KDEBUG_ENABLE_TRACE
))
3960 if (global_state_pid
!= -1) {
3961 if ((proc_find(global_state_pid
)) != NULL
) {
3963 * The global pid exists, we're running
3964 * due to fs_usage, latency, etc...
3965 * don't cut the panic/shutdown trace file
3966 * Disable tracing from this point to avoid
3970 kd_ctrl_page
.enabled
= 0;
3971 commpage_update_kdebug_enable();
3975 KERNEL_DEBUG_CONSTANT(TRACE_PANIC
| DBG_FUNC_NONE
, 0, 0, 0, 0, 0);
3978 kd_ctrl_page
.enabled
= 0;
3979 commpage_update_kdebug_enable();
3981 ctx
= vfs_context_kernel();
3983 if ((error
= vnode_open(filename
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), 0600, 0, &vp
, ctx
)))
3986 number
= kd_mapcount
* sizeof(kd_threadmap
);
3987 kdbg_readthrmap(0, &number
, vp
, ctx
);
3989 number
= nkdbufs
*sizeof(kd_buf
);
3990 kdbg_read(0, &number
, vp
, ctx
, RAW_VERSION1
);
3992 vnode_close(vp
, FWRITE
, ctx
);
3994 sync(current_proc(), (void *)NULL
, (int *)NULL
);
3997 /* Helper function for filling in the BSD name for an address space
3998 * Defined here because the machine bindings know only Mach threads
3999 * and nothing about BSD processes.
4001 * FIXME: need to grab a lock during this?
4003 void kdbg_get_task_name(char* name_buf
, int len
, task_t task
)
4007 /* Note: we can't use thread->task (and functions that rely on it) here
4008 * because it hasn't been initialized yet when this function is called.
4009 * We use the explicitly-passed task parameter instead.
4011 proc
= get_bsdtask_info(task
);
4012 if (proc
!= PROC_NULL
)
4013 snprintf(name_buf
, len
, "%s/%d", proc
->p_comm
, proc
->p_pid
);
4015 snprintf(name_buf
, len
, "%p [!bsd]", task
);
4018 #if KDEBUG_MOJO_TRACE
4020 binary_search(uint32_t id
)
4025 high
= sizeof(kd_events
)/sizeof(kd_event_t
) - 1;
4029 mid
= (low
+ high
) / 2;
4032 return NULL
; /* failed */
4033 else if ( low
+ 1 >= high
) {
4034 /* We have a match */
4035 if (kd_events
[high
].id
== id
)
4036 return &kd_events
[high
];
4037 else if (kd_events
[low
].id
== id
)
4038 return &kd_events
[low
];
4040 return NULL
; /* search failed */
4042 else if (id
< kd_events
[mid
].id
)
4050 * Look up event id to get name string.
4051 * Using a per-cpu cache of a single entry
4052 * before resorting to a binary search of the full table.
4055 static kd_event_t
*last_hit
[MAX_CPUS
];
4057 event_lookup_cache(uint32_t cpu
, uint32_t id
)
4059 if (last_hit
[cpu
] == NULL
|| last_hit
[cpu
]->id
!= id
)
4060 last_hit
[cpu
] = binary_search(id
);
4061 return last_hit
[cpu
];
4064 static uint64_t kd_last_timstamp
;
4067 kdebug_serial_print(
4078 char kprintf_line
[192];
4080 uint64_t us
= timestamp
/ NSEC_PER_USEC
;
4081 uint64_t us_tenth
= (timestamp
% NSEC_PER_USEC
) / 100;
4082 uint64_t delta
= timestamp
- kd_last_timstamp
;
4083 uint64_t delta_us
= delta
/ NSEC_PER_USEC
;
4084 uint64_t delta_us_tenth
= (delta
% NSEC_PER_USEC
) / 100;
4085 uint32_t event_id
= debugid
& KDBG_EVENTID_MASK
;
4086 const char *command
;
4091 /* event time and delta from last */
4092 snprintf(kprintf_line
, sizeof(kprintf_line
),
4093 "%11llu.%1llu %8llu.%1llu ",
4094 us
, us_tenth
, delta_us
, delta_us_tenth
);
4097 /* event (id or name) - start prefixed by "[", end postfixed by "]" */
4098 bra
= (debugid
& DBG_FUNC_START
) ? "[" : " ";
4099 ket
= (debugid
& DBG_FUNC_END
) ? "]" : " ";
4100 ep
= event_lookup_cache(cpunum
, event_id
);
4102 if (strlen(ep
->name
) < sizeof(event
) - 3)
4103 snprintf(event
, sizeof(event
), "%s%s%s",
4104 bra
, ep
->name
, ket
);
4106 snprintf(event
, sizeof(event
), "%s%x(name too long)%s",
4107 bra
, event_id
, ket
);
4109 snprintf(event
, sizeof(event
), "%s%x%s",
4110 bra
, event_id
, ket
);
4112 snprintf(kprintf_line
+ strlen(kprintf_line
),
4113 sizeof(kprintf_line
) - strlen(kprintf_line
),
4116 /* arg1 .. arg4 with special cases for strings */
4119 case VFS_LOOKUP_DONE
:
4120 if (debugid
& DBG_FUNC_START
) {
4121 /* arg1 hex then arg2..arg4 chars */
4122 snprintf(kprintf_line
+ strlen(kprintf_line
),
4123 sizeof(kprintf_line
) - strlen(kprintf_line
),
4124 "%-16lx %-8s%-8s%-8s ",
4125 arg1
, (char*)&arg2
, (char*)&arg3
, (char*)&arg4
);
4128 /* else fall through for arg1..arg4 chars */
4129 case TRACE_STRING_EXEC
:
4130 case TRACE_STRING_NEWTHREAD
:
4131 case TRACE_INFO_STRING
:
4132 snprintf(kprintf_line
+ strlen(kprintf_line
),
4133 sizeof(kprintf_line
) - strlen(kprintf_line
),
4134 "%-8s%-8s%-8s%-8s ",
4135 (char*)&arg1
, (char*)&arg2
, (char*)&arg3
, (char*)&arg4
);
4138 snprintf(kprintf_line
+ strlen(kprintf_line
),
4139 sizeof(kprintf_line
) - strlen(kprintf_line
),
4140 "%-16lx %-16lx %-16lx %-16lx",
4141 arg1
, arg2
, arg3
, arg4
);
4144 /* threadid, cpu and command name */
4145 if (threadid
== (uintptr_t)thread_tid(current_thread()) &&
4147 current_proc()->p_comm
[0])
4148 command
= current_proc()->p_comm
;
4151 snprintf(kprintf_line
+ strlen(kprintf_line
),
4152 sizeof(kprintf_line
) - strlen(kprintf_line
),
4153 " %-16lx %-2d %s\n",
4154 threadid
, cpunum
, command
);
4156 kprintf("%s", kprintf_line
);
4157 kd_last_timstamp
= timestamp
;