]>
Commit | Line | Data |
---|---|---|
fe8ab488 | 1 | /* |
5ba3f43e | 2 | * Copyright (c) 2013-2017 Apple Inc. All rights reserved. |
fe8ab488 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5ba3f43e | 5 | * |
fe8ab488 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
5ba3f43e | 14 | * |
fe8ab488 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
5ba3f43e | 17 | * |
fe8ab488 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
5ba3f43e | 25 | * |
fe8ab488 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
29 | #include <mach/mach_types.h> | |
30 | #include <mach/vm_param.h> | |
3e170ce0 | 31 | #include <mach/mach_vm.h> |
39037602 | 32 | #include <mach/clock_types.h> |
3e170ce0 A |
33 | #include <sys/errno.h> |
34 | #include <sys/stackshot.h> | |
fe8ab488 A |
35 | #ifdef IMPORTANCE_INHERITANCE |
36 | #include <ipc/ipc_importance.h> | |
37 | #endif | |
38 | #include <sys/appleapiopts.h> | |
39 | #include <kern/debug.h> | |
813fb2f6 | 40 | #include <kern/block_hint.h> |
fe8ab488 A |
41 | #include <uuid/uuid.h> |
42 | ||
43 | #include <kdp/kdp_dyld.h> | |
44 | #include <kdp/kdp_en_debugger.h> | |
45 | ||
46 | #include <libsa/types.h> | |
47 | #include <libkern/version.h> | |
48 | ||
49 | #include <string.h> /* bcopy */ | |
50 | ||
5ba3f43e | 51 | #include <kern/coalition.h> |
fe8ab488 A |
52 | #include <kern/processor.h> |
53 | #include <kern/thread.h> | |
5ba3f43e | 54 | #include <kern/thread_group.h> |
39037602 | 55 | #include <kern/task.h> |
3e170ce0 | 56 | #include <kern/telemetry.h> |
fe8ab488 | 57 | #include <kern/clock.h> |
39037602 | 58 | #include <kern/policy_internal.h> |
fe8ab488 A |
59 | #include <vm/vm_map.h> |
60 | #include <vm/vm_kern.h> | |
61 | #include <vm/vm_pageout.h> | |
3e170ce0 | 62 | #include <vm/vm_fault.h> |
fe8ab488 A |
63 | #include <vm/vm_shared_region.h> |
64 | #include <libkern/OSKextLibPrivate.h> | |
65 | ||
5ba3f43e | 66 | #if CONFIG_EMBEDDED |
3e170ce0 A |
67 | #include <pexpert/pexpert.h> /* For gPanicBase/gPanicBase */ |
68 | #endif | |
69 | ||
5ba3f43e A |
70 | #if MONOTONIC |
71 | #include <kern/monotonic.h> | |
72 | #endif /* MONOTONIC */ | |
73 | ||
74 | #include <san/kasan.h> | |
75 | ||
fe8ab488 A |
76 | extern unsigned int not_in_kdp; |
77 | ||
5ba3f43e A |
78 | #if CONFIG_EMBEDDED |
79 | uuid_t kernelcache_uuid; | |
80 | #endif | |
39037602 | 81 | |
813fb2f6 A |
82 | /* indicate to the compiler that some accesses are unaligned */ |
83 | typedef uint64_t unaligned_u64 __attribute__((aligned(1))); | |
84 | ||
fe8ab488 | 85 | extern addr64_t kdp_vtophys(pmap_t pmap, addr64_t va); |
39037602 | 86 | extern void * proc_get_uthread_uu_threadlist(void * uthread_v); |
fe8ab488 | 87 | |
39037602 A |
88 | int kdp_snapshot = 0; |
89 | static kern_return_t stack_snapshot_ret = 0; | |
3e170ce0 | 90 | static uint32_t stack_snapshot_bytes_traced = 0; |
fe8ab488 | 91 | |
3e170ce0 | 92 | static kcdata_descriptor_t stackshot_kcdata_p = NULL; |
fe8ab488 A |
93 | static void *stack_snapshot_buf; |
94 | static uint32_t stack_snapshot_bufsize; | |
95 | int stack_snapshot_pid; | |
96 | static uint32_t stack_snapshot_flags; | |
39037602 A |
97 | static uint64_t stack_snapshot_delta_since_timestamp; |
98 | static boolean_t panic_stackshot; | |
99 | ||
100 | static boolean_t stack_enable_faulting = FALSE; | |
101 | static struct stackshot_fault_stats fault_stats; | |
3e170ce0 | 102 | |
813fb2f6 | 103 | static unaligned_u64 * stackshot_duration_outer; |
39037602 | 104 | static uint64_t stackshot_microsecs; |
3e170ce0 | 105 | |
39037602 A |
106 | void * kernel_stackshot_buf = NULL; /* Pointer to buffer for stackshots triggered from the kernel and retrieved later */ |
107 | int kernel_stackshot_buf_size = 0; | |
fe8ab488 | 108 | |
39037602 A |
109 | void * stackshot_snapbuf = NULL; /* Used by stack_snapshot2 (to be removed) */ |
110 | ||
111 | __private_extern__ void stackshot_init( void ); | |
3e170ce0 | 112 | static boolean_t memory_iszero(void *addr, size_t size); |
3e170ce0 A |
113 | #if CONFIG_TELEMETRY |
114 | kern_return_t stack_microstackshot(user_addr_t tracebuf, uint32_t tracebuf_size, uint32_t flags, int32_t *retval); | |
115 | #endif | |
116 | uint32_t get_stackshot_estsize(uint32_t prev_size_hint); | |
117 | kern_return_t kern_stack_snapshot_internal(int stackshot_config_version, void *stackshot_config, | |
118 | size_t stackshot_config_size, boolean_t stackshot_from_user); | |
5ba3f43e A |
119 | kern_return_t do_stackshot(void *); |
120 | void kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size, uint32_t flags, kcdata_descriptor_t data_p, uint64_t since_timestamp); | |
39037602 | 121 | boolean_t stackshot_thread_is_idle_worker_unsafe(thread_t thread); |
3e170ce0 | 122 | static int kdp_stackshot_kcdata_format(int pid, uint32_t trace_flags, uint32_t *pBytesTraced); |
3e170ce0 | 123 | uint32_t kdp_stack_snapshot_bytes_traced(void); |
fe8ab488 | 124 | static void kdp_mem_and_io_snapshot(struct mem_and_io_snapshot *memio_snap); |
39037602 A |
125 | static boolean_t kdp_copyin(vm_map_t map, uint64_t uaddr, void *dest, size_t size, boolean_t try_fault, uint32_t *kdp_fault_result); |
126 | static boolean_t kdp_copyin_word(task_t task, uint64_t addr, uint64_t *result, boolean_t try_fault, uint32_t *kdp_fault_results); | |
fe8ab488 | 127 | static uint64_t proc_was_throttled_from_task(task_t task); |
813fb2f6 A |
128 | static void stackshot_thread_wait_owner_info(thread_t thread, thread_waitinfo_t * waitinfo); |
129 | static int stackshot_thread_has_valid_waitinfo(thread_t thread); | |
fe8ab488 | 130 | |
5ba3f43e A |
131 | #if CONFIG_COALITIONS |
132 | static void stackshot_coalition_jetsam_count(void *arg, int i, coalition_t coal); | |
133 | static void stackshot_coalition_jetsam_snapshot(void *arg, int i, coalition_t coal); | |
134 | #endif /* CONFIG_COALITIONS */ | |
135 | ||
136 | ||
39037602 A |
137 | extern uint32_t workqueue_get_pwq_state_kdp(void *proc); |
138 | ||
fe8ab488 A |
139 | extern int proc_pid(void *p); |
140 | extern uint64_t proc_uniqueid(void *p); | |
141 | extern uint64_t proc_was_throttled(void *p); | |
142 | extern uint64_t proc_did_throttle(void *p); | |
39037602 A |
143 | static uint64_t proc_did_throttle_from_task(task_t task); |
144 | extern void proc_name_kdp(task_t task, char * buf, int size); | |
145 | extern int proc_threadname_kdp(void * uth, char * buf, size_t size); | |
146 | extern void proc_starttime_kdp(void * p, uint64_t * tv_sec, uint64_t * tv_usec, uint64_t * abstime); | |
3e170ce0 | 147 | extern int memorystatus_get_pressure_status_kdp(void); |
39037602 A |
148 | extern boolean_t memorystatus_proc_is_dirty_unsafe(void * v); |
149 | ||
150 | extern int count_busy_buffers(void); /* must track with declaration in bsd/sys/buf_internal.h */ | |
151 | extern void bcopy_phys(addr64_t, addr64_t, vm_size_t); | |
152 | ||
153 | #if CONFIG_TELEMETRY | |
154 | extern kern_return_t stack_microstackshot(user_addr_t tracebuf, uint32_t tracebuf_size, uint32_t flags, int32_t *retval); | |
155 | #endif /* CONFIG_TELEMETRY */ | |
fe8ab488 | 156 | |
39037602 A |
157 | extern kern_return_t kern_stack_snapshot_with_reason(char* reason); |
158 | 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); | |
fe8ab488 | 159 | |
39037602 A |
160 | /* |
161 | * Validates that the given address is both a valid page and has | |
162 | * default caching attributes for the current map. Returns | |
fe8ab488 A |
163 | * 0 if the address is invalid, and a kernel virtual address for |
164 | * the given address if it is valid. | |
165 | */ | |
3e170ce0 | 166 | vm_offset_t machine_trace_thread_get_kva(vm_offset_t cur_target_addr, vm_map_t map, uint32_t *thread_trace_flags); |
fe8ab488 | 167 | |
39037602 A |
168 | #define KDP_FAULT_RESULT_PAGED_OUT 0x1 /* some data was unable to be retrieved */ |
169 | #define KDP_FAULT_RESULT_TRIED_FAULT 0x2 /* tried to fault in data */ | |
170 | #define KDP_FAULT_RESULT_FAULTED_IN 0x4 /* successfully faulted in data */ | |
171 | ||
172 | /* | |
173 | * Looks up the physical translation for the given address in the target map, attempting | |
174 | * to fault data in if requested and it is not resident. Populates thread_trace_flags if requested | |
175 | * as well. | |
176 | */ | |
177 | vm_offset_t kdp_find_phys(vm_map_t map, vm_offset_t target_addr, boolean_t try_fault, uint32_t *kdp_fault_results); | |
178 | ||
179 | static size_t stackshot_strlcpy(char *dst, const char *src, size_t maxlen); | |
180 | static void stackshot_memcpy(void *dst, const void *src, size_t len); | |
181 | ||
fe8ab488 | 182 | /* Clears caching information used by the above validation routine |
39037602 | 183 | * (in case the current map has been changed or cleared). |
fe8ab488 A |
184 | */ |
185 | void machine_trace_thread_clear_validation_cache(void); | |
186 | ||
187 | #define MAX_FRAMES 1000 | |
3e170ce0 | 188 | #define MAX_LOADINFOS 500 |
3e170ce0 | 189 | #define TASK_IMP_WALK_LIMIT 20 |
fe8ab488 A |
190 | |
191 | typedef struct thread_snapshot *thread_snapshot_t; | |
192 | typedef struct task_snapshot *task_snapshot_t; | |
193 | ||
194 | #if CONFIG_KDP_INTERACTIVE_DEBUGGING | |
195 | extern kdp_send_t kdp_en_send_pkt; | |
196 | #endif | |
197 | ||
198 | /* | |
199 | * Globals to support machine_trace_thread_get_kva. | |
200 | */ | |
201 | static vm_offset_t prev_target_page = 0; | |
202 | static vm_offset_t prev_target_kva = 0; | |
203 | static boolean_t validate_next_addr = TRUE; | |
204 | ||
3e170ce0 A |
205 | /* |
206 | * Stackshot locking and other defines. | |
207 | */ | |
208 | static lck_grp_t *stackshot_subsys_lck_grp; | |
209 | static lck_grp_attr_t *stackshot_subsys_lck_grp_attr; | |
210 | static lck_attr_t *stackshot_subsys_lck_attr; | |
211 | static lck_mtx_t stackshot_subsys_mutex; | |
212 | ||
213 | #define STACKSHOT_SUBSYS_LOCK() lck_mtx_lock(&stackshot_subsys_mutex) | |
39037602 | 214 | #define STACKSHOT_SUBSYS_TRY_LOCK() lck_mtx_try_lock(&stackshot_subsys_mutex) |
3e170ce0 | 215 | #define STACKSHOT_SUBSYS_UNLOCK() lck_mtx_unlock(&stackshot_subsys_mutex) |
3e170ce0 | 216 | |
39037602 A |
217 | #define SANE_BOOTPROFILE_TRACEBUF_SIZE (64 * 1024 * 1024) |
218 | #define SANE_TRACEBUF_SIZE (8 * 1024 * 1024) | |
219 | ||
220 | /* | |
221 | * We currently set a ceiling of 3 milliseconds spent in the kdp fault path | |
222 | * for non-panic stackshots where faulting is requested. | |
223 | */ | |
224 | #define KDP_FAULT_PATH_MAX_TIME_PER_STACKSHOT_NSECS (3 * NSEC_PER_MSEC) | |
225 | ||
226 | #define STACKSHOT_SUPP_SIZE (16 * 1024) /* Minimum stackshot size */ | |
227 | #define TASK_UUID_AVG_SIZE (16 * sizeof(uuid_t)) /* Average space consumed by UUIDs/task */ | |
228 | ||
229 | /* | |
230 | * Initialize the mutex governing access to the stack snapshot subsystem | |
231 | * and other stackshot related bits. | |
232 | */ | |
3e170ce0 | 233 | __private_extern__ void |
39037602 | 234 | stackshot_init( void ) |
3e170ce0 | 235 | { |
39037602 A |
236 | mach_timebase_info_data_t timebase; |
237 | ||
3e170ce0 A |
238 | stackshot_subsys_lck_grp_attr = lck_grp_attr_alloc_init(); |
239 | ||
240 | stackshot_subsys_lck_grp = lck_grp_alloc_init("stackshot_subsys_lock", stackshot_subsys_lck_grp_attr); | |
241 | ||
242 | stackshot_subsys_lck_attr = lck_attr_alloc_init(); | |
243 | ||
244 | lck_mtx_init(&stackshot_subsys_mutex, stackshot_subsys_lck_grp, stackshot_subsys_lck_attr); | |
3e170ce0 | 245 | |
39037602 A |
246 | clock_timebase_info(&timebase); |
247 | fault_stats.sfs_system_max_fault_time = ((KDP_FAULT_PATH_MAX_TIME_PER_STACKSHOT_NSECS * timebase.denom)/ timebase.numer); | |
248 | } | |
fe8ab488 A |
249 | |
250 | /* | |
251 | * Method for grabbing timer values safely, in the sense that no infinite loop will occur | |
252 | * Certain flavors of the timer_grab function, which would seem to be the thing to use, | |
253 | * can loop infinitely if called while the timer is in the process of being updated. | |
254 | * Unfortunately, it is (rarely) possible to get inconsistent top and bottom halves of | |
255 | * the timer using this method. This seems insoluble, since stackshot runs in a context | |
256 | * where the timer might be half-updated, and has no way of yielding control just long | |
257 | * enough to finish the update. | |
258 | */ | |
259 | ||
260 | static uint64_t safe_grab_timer_value(struct timer *t) | |
261 | { | |
262 | #if defined(__LP64__) | |
263 | return t->all_bits; | |
264 | #else | |
265 | uint64_t time = t->high_bits; /* endian independent grab */ | |
266 | time = (time << 32) | t->low_bits; | |
267 | return time; | |
268 | #endif | |
269 | } | |
270 | ||
5ba3f43e A |
271 | /* |
272 | * Called with interrupts disabled after stackshot context has been | |
273 | * initialized. Updates stack_snapshot_ret. | |
274 | */ | |
275 | static kern_return_t | |
276 | stackshot_trap() | |
277 | { | |
278 | return DebuggerTrapWithState(DBOP_STACKSHOT, NULL, NULL, NULL, 0, FALSE, 0); | |
279 | } | |
280 | ||
281 | ||
3e170ce0 | 282 | kern_return_t |
39037602 | 283 | stack_snapshot_from_kernel(int pid, void *buf, uint32_t size, uint32_t flags, uint64_t delta_since_timestamp, unsigned *bytes_traced) |
3e170ce0 | 284 | { |
39037602 | 285 | kern_return_t error = KERN_SUCCESS; |
3e170ce0 A |
286 | boolean_t istate; |
287 | ||
5ba3f43e A |
288 | #if DEVELOPMENT || DEBUG |
289 | if (kern_feature_override(KF_STACKSHOT_OVRD) == TRUE) { | |
290 | error = KERN_NOT_SUPPORTED; | |
291 | goto out; | |
292 | } | |
293 | #endif | |
3e170ce0 A |
294 | if ((buf == NULL) || (size <= 0) || (bytes_traced == NULL)) { |
295 | return KERN_INVALID_ARGUMENT; | |
296 | } | |
297 | ||
298 | /* cap in individual stackshot to SANE_TRACEBUF_SIZE */ | |
299 | if (size > SANE_TRACEBUF_SIZE) { | |
300 | size = SANE_TRACEBUF_SIZE; | |
301 | } | |
302 | ||
303 | /* Serialize tracing */ | |
39037602 A |
304 | if (flags & STACKSHOT_TRYLOCK) { |
305 | if (!STACKSHOT_SUBSYS_TRY_LOCK()) { | |
306 | return KERN_LOCK_OWNED; | |
307 | } | |
308 | } else { | |
309 | STACKSHOT_SUBSYS_LOCK(); | |
310 | } | |
311 | ||
39037602 A |
312 | struct kcdata_descriptor kcdata; |
313 | uint32_t hdr_tag = (flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) ? | |
314 | KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT : KCDATA_BUFFER_BEGIN_STACKSHOT; | |
315 | ||
316 | error = kcdata_memory_static_init(&kcdata, (mach_vm_address_t)buf, hdr_tag, size, | |
317 | KCFLAG_USE_MEMCOPY | KCFLAG_NO_AUTO_ENDBUFFER); | |
318 | if (error) { | |
319 | goto out; | |
320 | } | |
3e170ce0 | 321 | |
5ba3f43e A |
322 | istate = ml_set_interrupts_enabled(FALSE); |
323 | ||
3e170ce0 | 324 | /* Preload trace parameters*/ |
39037602 | 325 | kdp_snapshot_preflight(pid, buf, size, flags, &kcdata, delta_since_timestamp); |
3e170ce0 | 326 | |
5ba3f43e A |
327 | /* |
328 | * Trap to the debugger to obtain a coherent stack snapshot; this populates | |
3e170ce0 A |
329 | * the trace buffer |
330 | */ | |
5ba3f43e | 331 | error = stackshot_trap(); |
3e170ce0 A |
332 | |
333 | ml_set_interrupts_enabled(istate); | |
334 | ||
335 | *bytes_traced = kdp_stack_snapshot_bytes_traced(); | |
336 | ||
39037602 | 337 | out: |
5ba3f43e | 338 | stackshot_kcdata_p = NULL; |
3e170ce0 | 339 | STACKSHOT_SUBSYS_UNLOCK(); |
3e170ce0 A |
340 | return error; |
341 | } | |
342 | ||
343 | #if CONFIG_TELEMETRY | |
344 | kern_return_t | |
345 | stack_microstackshot(user_addr_t tracebuf, uint32_t tracebuf_size, uint32_t flags, int32_t *retval) | |
346 | { | |
347 | int error = KERN_SUCCESS; | |
348 | uint32_t bytes_traced = 0; | |
349 | ||
350 | *retval = -1; | |
351 | ||
352 | /* | |
353 | * Control related operations | |
354 | */ | |
355 | if (flags & STACKSHOT_GLOBAL_MICROSTACKSHOT_ENABLE) { | |
356 | telemetry_global_ctl(1); | |
357 | *retval = 0; | |
358 | goto exit; | |
359 | } else if (flags & STACKSHOT_GLOBAL_MICROSTACKSHOT_DISABLE) { | |
360 | telemetry_global_ctl(0); | |
361 | *retval = 0; | |
362 | goto exit; | |
363 | } | |
364 | ||
3e170ce0 A |
365 | /* |
366 | * Data related operations | |
367 | */ | |
368 | *retval = -1; | |
369 | ||
370 | if ((((void*)tracebuf) == NULL) || (tracebuf_size == 0)) { | |
371 | error = KERN_INVALID_ARGUMENT; | |
372 | goto exit; | |
373 | } | |
374 | ||
375 | STACKSHOT_SUBSYS_LOCK(); | |
376 | ||
377 | if (flags & STACKSHOT_GET_MICROSTACKSHOT) { | |
378 | if (tracebuf_size > SANE_TRACEBUF_SIZE) { | |
379 | error = KERN_INVALID_ARGUMENT; | |
380 | goto unlock_exit; | |
381 | } | |
382 | ||
383 | bytes_traced = tracebuf_size; | |
384 | error = telemetry_gather(tracebuf, &bytes_traced, | |
385 | (flags & STACKSHOT_SET_MICROSTACKSHOT_MARK) ? TRUE : FALSE); | |
386 | *retval = (int)bytes_traced; | |
387 | goto unlock_exit; | |
388 | } | |
389 | ||
3e170ce0 A |
390 | if (flags & STACKSHOT_GET_BOOT_PROFILE) { |
391 | ||
392 | if (tracebuf_size > SANE_BOOTPROFILE_TRACEBUF_SIZE) { | |
393 | error = KERN_INVALID_ARGUMENT; | |
394 | goto unlock_exit; | |
395 | } | |
396 | ||
397 | bytes_traced = tracebuf_size; | |
398 | error = bootprofile_gather(tracebuf, &bytes_traced); | |
399 | *retval = (int)bytes_traced; | |
400 | } | |
401 | ||
402 | unlock_exit: | |
403 | STACKSHOT_SUBSYS_UNLOCK(); | |
404 | exit: | |
405 | return error; | |
406 | } | |
407 | #endif /* CONFIG_TELEMETRY */ | |
408 | ||
409 | /* | |
410 | * Return the estimated size of a stackshot based on the | |
411 | * number of currently running threads and tasks. | |
412 | */ | |
413 | uint32_t | |
414 | get_stackshot_estsize(uint32_t prev_size_hint) | |
415 | { | |
416 | vm_size_t thread_total; | |
417 | vm_size_t task_total; | |
418 | uint32_t estimated_size; | |
419 | ||
420 | thread_total = (threads_count * sizeof(struct thread_snapshot)); | |
421 | task_total = (tasks_count * (sizeof(struct task_snapshot) + TASK_UUID_AVG_SIZE)); | |
422 | ||
423 | estimated_size = (uint32_t) VM_MAP_ROUND_PAGE((thread_total + task_total + STACKSHOT_SUPP_SIZE), PAGE_MASK); | |
424 | if (estimated_size < prev_size_hint) { | |
425 | estimated_size = (uint32_t) VM_MAP_ROUND_PAGE(prev_size_hint, PAGE_MASK); | |
426 | } | |
427 | ||
428 | return estimated_size; | |
429 | } | |
430 | ||
431 | /* | |
432 | * stackshot_remap_buffer: Utility function to remap bytes_traced bytes starting at stackshotbuf | |
433 | * into the current task's user space and subsequently copy out the address | |
434 | * at which the buffer has been mapped in user space to out_buffer_addr. | |
435 | * | |
436 | * Inputs: stackshotbuf - pointer to the original buffer in the kernel's address space | |
437 | * bytes_traced - length of the buffer to remap starting from stackshotbuf | |
438 | * out_buffer_addr - pointer to placeholder where newly mapped buffer will be mapped. | |
439 | * out_size_addr - pointer to be filled in with the size of the buffer | |
440 | * | |
441 | * Outputs: ENOSPC if there is not enough free space in the task's address space to remap the buffer | |
442 | * EINVAL for all other errors returned by task_remap_buffer/mach_vm_remap | |
443 | * an error from copyout | |
444 | */ | |
445 | static kern_return_t | |
446 | stackshot_remap_buffer(void *stackshotbuf, uint32_t bytes_traced, uint64_t out_buffer_addr, uint64_t out_size_addr) | |
447 | { | |
448 | int error = 0; | |
449 | mach_vm_offset_t stackshotbuf_user_addr = (mach_vm_offset_t)NULL; | |
450 | vm_prot_t cur_prot, max_prot; | |
451 | ||
5ba3f43e A |
452 | error = mach_vm_remap_kernel(get_task_map(current_task()), &stackshotbuf_user_addr, bytes_traced, 0, |
453 | VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_NONE, kernel_map, (mach_vm_offset_t)stackshotbuf, FALSE, &cur_prot, &max_prot, VM_INHERIT_DEFAULT); | |
3e170ce0 A |
454 | /* |
455 | * If the call to mach_vm_remap fails, we return the appropriate converted error | |
456 | */ | |
457 | if (error == KERN_SUCCESS) { | |
458 | /* | |
459 | * If we fail to copy out the address or size of the new buffer, we remove the buffer mapping that | |
460 | * we just made in the task's user space. | |
461 | */ | |
462 | error = copyout(CAST_DOWN(void *, &stackshotbuf_user_addr), (user_addr_t)out_buffer_addr, sizeof(stackshotbuf_user_addr)); | |
463 | if (error != KERN_SUCCESS) { | |
464 | mach_vm_deallocate(get_task_map(current_task()), stackshotbuf_user_addr, (mach_vm_size_t)bytes_traced); | |
465 | return error; | |
466 | } | |
467 | error = copyout(&bytes_traced, (user_addr_t)out_size_addr, sizeof(bytes_traced)); | |
468 | if (error != KERN_SUCCESS) { | |
469 | mach_vm_deallocate(get_task_map(current_task()), stackshotbuf_user_addr, (mach_vm_size_t)bytes_traced); | |
470 | return error; | |
471 | } | |
472 | } | |
473 | return error; | |
474 | } | |
475 | ||
476 | kern_return_t | |
477 | kern_stack_snapshot_internal(int stackshot_config_version, void *stackshot_config, size_t stackshot_config_size, boolean_t stackshot_from_user) | |
478 | { | |
479 | int error = 0; | |
480 | boolean_t prev_interrupt_state; | |
481 | uint32_t bytes_traced = 0; | |
482 | uint32_t stackshotbuf_size = 0; | |
483 | void * stackshotbuf = NULL; | |
484 | kcdata_descriptor_t kcdata_p = NULL; | |
485 | ||
486 | void * buf_to_free = NULL; | |
487 | int size_to_free = 0; | |
488 | ||
489 | /* Parsed arguments */ | |
490 | uint64_t out_buffer_addr; | |
491 | uint64_t out_size_addr; | |
492 | int pid = -1; | |
493 | uint32_t flags; | |
494 | uint64_t since_timestamp; | |
3e170ce0 A |
495 | uint32_t size_hint = 0; |
496 | ||
497 | if(stackshot_config == NULL) { | |
498 | return KERN_INVALID_ARGUMENT; | |
499 | } | |
5ba3f43e A |
500 | #if DEVELOPMENT || DEBUG |
501 | /* TBD: ask stackshot clients to avoid issuing stackshots in this | |
502 | * configuration in lieu of the kernel feature override. | |
503 | */ | |
504 | if (kern_feature_override(KF_STACKSHOT_OVRD) == TRUE) { | |
505 | return KERN_NOT_SUPPORTED; | |
506 | } | |
507 | #endif | |
3e170ce0 A |
508 | |
509 | switch (stackshot_config_version) { | |
510 | case STACKSHOT_CONFIG_TYPE: | |
511 | if (stackshot_config_size != sizeof(stackshot_config_t)) { | |
512 | return KERN_INVALID_ARGUMENT; | |
513 | } | |
514 | stackshot_config_t *config = (stackshot_config_t *) stackshot_config; | |
515 | out_buffer_addr = config->sc_out_buffer_addr; | |
516 | out_size_addr = config->sc_out_size_addr; | |
517 | pid = config->sc_pid; | |
518 | flags = config->sc_flags; | |
39037602 | 519 | since_timestamp = config->sc_delta_timestamp; |
3e170ce0 A |
520 | if (config->sc_size <= SANE_TRACEBUF_SIZE) { |
521 | size_hint = config->sc_size; | |
522 | } | |
523 | break; | |
524 | default: | |
525 | return KERN_NOT_SUPPORTED; | |
39037602 A |
526 | } |
527 | ||
528 | /* | |
529 | * Currently saving a kernel buffer and trylock are only supported from the | |
530 | * internal/KEXT API. | |
531 | */ | |
532 | if (stackshot_from_user) { | |
533 | if (flags & (STACKSHOT_TRYLOCK | STACKSHOT_SAVE_IN_KERNEL_BUFFER | STACKSHOT_FROM_PANIC)) { | |
534 | return KERN_NO_ACCESS; | |
535 | } | |
536 | } else { | |
3e170ce0 A |
537 | if (!(flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER)) { |
538 | return KERN_NOT_SUPPORTED; | |
539 | } | |
540 | } | |
541 | ||
d190cdc3 | 542 | if (!((flags & STACKSHOT_KCDATA_FORMAT) || (flags & STACKSHOT_RETRIEVE_EXISTING_BUFFER))) { |
3e170ce0 A |
543 | return KERN_NOT_SUPPORTED; |
544 | } | |
545 | ||
546 | /* | |
d190cdc3 | 547 | * If we're not saving the buffer in the kernel pointer, we need a place to copy into. |
3e170ce0 A |
548 | */ |
549 | if ((!out_buffer_addr || !out_size_addr) && !(flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER)) { | |
550 | return KERN_INVALID_ARGUMENT; | |
551 | } | |
552 | ||
39037602 A |
553 | if (since_timestamp != 0 && ((flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) == 0)) { |
554 | return KERN_INVALID_ARGUMENT; | |
3e170ce0 A |
555 | } |
556 | ||
5ba3f43e A |
557 | #if MONOTONIC |
558 | if (!mt_core_supported) { | |
559 | flags &= ~STACKSHOT_INSTRS_CYCLES; | |
560 | } | |
561 | #else /* MONOTONIC */ | |
562 | flags &= ~STACKSHOT_INSTRS_CYCLES; | |
563 | #endif /* !MONOTONIC */ | |
564 | ||
3e170ce0 A |
565 | STACKSHOT_SUBSYS_LOCK(); |
566 | ||
567 | if (flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER) { | |
568 | /* | |
569 | * Don't overwrite an existing stackshot | |
570 | */ | |
571 | if (kernel_stackshot_buf != NULL) { | |
572 | error = KERN_MEMORY_PRESENT; | |
573 | goto error_exit; | |
574 | } | |
575 | } else if (flags & STACKSHOT_RETRIEVE_EXISTING_BUFFER) { | |
576 | if ((kernel_stackshot_buf == NULL) || (kernel_stackshot_buf_size <= 0)) { | |
577 | error = KERN_NOT_IN_SET; | |
578 | goto error_exit; | |
579 | } | |
580 | error = stackshot_remap_buffer(kernel_stackshot_buf, kernel_stackshot_buf_size, | |
581 | out_buffer_addr, out_size_addr); | |
582 | /* | |
583 | * If we successfully remapped the buffer into the user's address space, we | |
584 | * set buf_to_free and size_to_free so the prior kernel mapping will be removed | |
585 | * and then clear the kernel stackshot pointer and associated size. | |
586 | */ | |
587 | if (error == KERN_SUCCESS) { | |
588 | buf_to_free = kernel_stackshot_buf; | |
589 | size_to_free = (int) VM_MAP_ROUND_PAGE(kernel_stackshot_buf_size, PAGE_MASK); | |
590 | kernel_stackshot_buf = NULL; | |
591 | kernel_stackshot_buf_size = 0; | |
592 | } | |
593 | ||
594 | goto error_exit; | |
595 | } | |
596 | ||
39037602 A |
597 | if (flags & STACKSHOT_GET_BOOT_PROFILE) { |
598 | void *bootprofile = NULL; | |
599 | uint32_t len = 0; | |
600 | #if CONFIG_TELEMETRY | |
601 | bootprofile_get(&bootprofile, &len); | |
602 | #endif | |
603 | if (!bootprofile || !len) { | |
604 | error = KERN_NOT_IN_SET; | |
605 | goto error_exit; | |
606 | } | |
607 | error = stackshot_remap_buffer(bootprofile, len, out_buffer_addr, out_size_addr); | |
608 | goto error_exit; | |
609 | } | |
610 | ||
3e170ce0 A |
611 | stackshotbuf_size = get_stackshot_estsize(size_hint); |
612 | ||
613 | for (; stackshotbuf_size <= SANE_TRACEBUF_SIZE; stackshotbuf_size <<= 1) { | |
614 | if (kmem_alloc(kernel_map, (vm_offset_t *)&stackshotbuf, stackshotbuf_size, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) { | |
615 | error = KERN_RESOURCE_SHORTAGE; | |
616 | goto error_exit; | |
617 | } | |
618 | ||
3e170ce0 | 619 | |
39037602 A |
620 | uint32_t hdr_tag = (flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) ? KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT : KCDATA_BUFFER_BEGIN_STACKSHOT; |
621 | kcdata_p = kcdata_memory_alloc_init((mach_vm_address_t)stackshotbuf, hdr_tag, stackshotbuf_size, | |
622 | KCFLAG_USE_MEMCOPY | KCFLAG_NO_AUTO_ENDBUFFER); | |
3e170ce0 | 623 | |
39037602 A |
624 | stackshot_duration_outer = NULL; |
625 | uint64_t time_start = mach_absolute_time(); | |
3e170ce0 A |
626 | |
627 | /* | |
628 | * Disable interrupts and save the current interrupt state. | |
629 | */ | |
630 | prev_interrupt_state = ml_set_interrupts_enabled(FALSE); | |
631 | ||
632 | /* | |
633 | * Load stackshot parameters. | |
634 | */ | |
39037602 | 635 | kdp_snapshot_preflight(pid, stackshotbuf, stackshotbuf_size, flags, kcdata_p, since_timestamp); |
3e170ce0 | 636 | |
5ba3f43e | 637 | error = stackshot_trap(); |
3e170ce0 A |
638 | |
639 | ml_set_interrupts_enabled(prev_interrupt_state); | |
640 | ||
39037602 A |
641 | /* record the duration that interupts were disabled */ |
642 | ||
643 | uint64_t time_end = mach_absolute_time(); | |
644 | if (stackshot_duration_outer) { | |
645 | *stackshot_duration_outer = time_end - time_start; | |
646 | } | |
647 | ||
39037602 | 648 | if (error != KERN_SUCCESS) { |
3e170ce0 A |
649 | if (kcdata_p != NULL) { |
650 | kcdata_memory_destroy(kcdata_p); | |
651 | kcdata_p = NULL; | |
652 | stackshot_kcdata_p = NULL; | |
653 | } | |
654 | kmem_free(kernel_map, (vm_offset_t)stackshotbuf, stackshotbuf_size); | |
655 | stackshotbuf = NULL; | |
39037602 A |
656 | if (error == KERN_INSUFFICIENT_BUFFER_SIZE) { |
657 | /* | |
658 | * If we didn't allocate a big enough buffer, deallocate and try again. | |
659 | */ | |
660 | continue; | |
661 | } else { | |
662 | goto error_exit; | |
663 | } | |
3e170ce0 A |
664 | } |
665 | ||
666 | bytes_traced = kdp_stack_snapshot_bytes_traced(); | |
667 | ||
668 | if (bytes_traced <= 0) { | |
39037602 | 669 | error = KERN_ABORTED; |
3e170ce0 A |
670 | goto error_exit; |
671 | } | |
672 | ||
673 | assert(bytes_traced <= stackshotbuf_size); | |
674 | if (!(flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER)) { | |
675 | error = stackshot_remap_buffer(stackshotbuf, bytes_traced, out_buffer_addr, out_size_addr); | |
676 | goto error_exit; | |
677 | } | |
678 | ||
679 | /* | |
680 | * Save the stackshot in the kernel buffer. | |
681 | */ | |
682 | kernel_stackshot_buf = stackshotbuf; | |
683 | kernel_stackshot_buf_size = bytes_traced; | |
684 | /* | |
685 | * Figure out if we didn't use all the pages in the buffer. If so, we set buf_to_free to the beginning of | |
686 | * the next page after the end of the stackshot in the buffer so that the kmem_free clips the buffer and | |
687 | * update size_to_free for kmem_free accordingly. | |
688 | */ | |
689 | size_to_free = stackshotbuf_size - (int) VM_MAP_ROUND_PAGE(bytes_traced, PAGE_MASK); | |
690 | ||
691 | assert(size_to_free >= 0); | |
692 | ||
693 | if (size_to_free != 0) { | |
694 | buf_to_free = (void *)((uint64_t)stackshotbuf + stackshotbuf_size - size_to_free); | |
695 | } | |
696 | ||
697 | stackshotbuf = NULL; | |
698 | stackshotbuf_size = 0; | |
699 | goto error_exit; | |
700 | } | |
701 | ||
702 | if (stackshotbuf_size > SANE_TRACEBUF_SIZE) { | |
703 | error = KERN_RESOURCE_SHORTAGE; | |
704 | } | |
705 | ||
706 | error_exit: | |
707 | if (kcdata_p != NULL) { | |
708 | kcdata_memory_destroy(kcdata_p); | |
709 | kcdata_p = NULL; | |
710 | stackshot_kcdata_p = NULL; | |
711 | } | |
712 | ||
713 | if (stackshotbuf != NULL) { | |
714 | kmem_free(kernel_map, (vm_offset_t)stackshotbuf, stackshotbuf_size); | |
715 | } | |
716 | if (buf_to_free != NULL) { | |
717 | kmem_free(kernel_map, (vm_offset_t)buf_to_free, size_to_free); | |
718 | } | |
719 | STACKSHOT_SUBSYS_UNLOCK(); | |
720 | return error; | |
721 | } | |
722 | ||
5ba3f43e A |
723 | /* |
724 | * Cache stack snapshot parameters in preparation for a trace. | |
725 | */ | |
fe8ab488 | 726 | void |
3e170ce0 | 727 | kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size, uint32_t flags, |
39037602 | 728 | kcdata_descriptor_t data_p, uint64_t since_timestamp) |
fe8ab488 | 729 | { |
39037602 A |
730 | uint64_t microsecs = 0, secs = 0; |
731 | clock_get_calendar_microtime((clock_sec_t *)&secs, (clock_usec_t *)µsecs); | |
732 | ||
733 | stackshot_microsecs = microsecs + (secs * USEC_PER_SEC); | |
fe8ab488 A |
734 | stack_snapshot_pid = pid; |
735 | stack_snapshot_buf = tracebuf; | |
736 | stack_snapshot_bufsize = tracebuf_size; | |
737 | stack_snapshot_flags = flags; | |
39037602 A |
738 | stack_snapshot_delta_since_timestamp = since_timestamp; |
739 | ||
740 | panic_stackshot = ((flags & STACKSHOT_FROM_PANIC) != 0); | |
741 | ||
5ba3f43e A |
742 | assert(data_p != NULL); |
743 | assert(stackshot_kcdata_p == NULL); | |
744 | stackshot_kcdata_p = data_p; | |
745 | ||
746 | stack_snapshot_bytes_traced = 0; | |
fe8ab488 A |
747 | } |
748 | ||
cc8bc92a A |
749 | void |
750 | panic_stackshot_reset_state() | |
751 | { | |
752 | stackshot_kcdata_p = NULL; | |
753 | } | |
754 | ||
5ba3f43e A |
755 | boolean_t |
756 | stackshot_active() | |
fe8ab488 | 757 | { |
5ba3f43e | 758 | return (stackshot_kcdata_p != NULL); |
fe8ab488 A |
759 | } |
760 | ||
3e170ce0 | 761 | uint32_t |
fe8ab488 A |
762 | kdp_stack_snapshot_bytes_traced(void) |
763 | { | |
764 | return stack_snapshot_bytes_traced; | |
765 | } | |
766 | ||
3e170ce0 A |
767 | static boolean_t memory_iszero(void *addr, size_t size) |
768 | { | |
769 | char *data = (char *)addr; | |
770 | for (size_t i = 0; i < size; i++){ | |
771 | if (data[i] != 0) | |
772 | return FALSE; | |
773 | } | |
774 | return TRUE; | |
775 | } | |
776 | ||
3e170ce0 A |
777 | #define kcd_end_address(kcd) ((void *)((uint64_t)((kcd)->kcd_addr_begin) + kcdata_memory_get_used_bytes((kcd)))) |
778 | #define kcd_max_address(kcd) ((void *)((kcd)->kcd_addr_begin + (kcd)->kcd_length)) | |
39037602 A |
779 | /* |
780 | * Use of the kcd_exit_on_error(action) macro requires a local | |
781 | * 'kern_return_t error' variable and 'error_exit' label. | |
782 | */ | |
783 | #define kcd_exit_on_error(action) \ | |
784 | do { \ | |
785 | if (KERN_SUCCESS != (error = (action))) { \ | |
786 | if (error == KERN_RESOURCE_SHORTAGE) { \ | |
787 | error = KERN_INSUFFICIENT_BUFFER_SIZE; \ | |
788 | } \ | |
789 | goto error_exit; \ | |
790 | } \ | |
3e170ce0 A |
791 | } while (0); /* end kcd_exit_on_error */ |
792 | ||
39037602 A |
793 | static uint64_t |
794 | kcdata_get_task_ss_flags(task_t task) | |
795 | { | |
796 | uint64_t ss_flags = 0; | |
797 | boolean_t task64 = task_has_64BitAddr(task); | |
798 | ||
799 | if (task64) | |
800 | ss_flags |= kUser64_p; | |
801 | if (!task->active || task_is_a_corpse(task)) | |
802 | ss_flags |= kTerminatedSnapshot; | |
803 | if (task->pidsuspended) | |
804 | ss_flags |= kPidSuspended; | |
805 | if (task->frozen) | |
806 | ss_flags |= kFrozen; | |
807 | if (task->effective_policy.tep_darwinbg == 1) | |
808 | ss_flags |= kTaskDarwinBG; | |
809 | if (task->requested_policy.trp_role == TASK_FOREGROUND_APPLICATION) | |
810 | ss_flags |= kTaskIsForeground; | |
811 | if (task->requested_policy.trp_boosted == 1) | |
812 | ss_flags |= kTaskIsBoosted; | |
813 | if (task->effective_policy.tep_sup_active == 1) | |
814 | ss_flags |= kTaskIsSuppressed; | |
815 | #if CONFIG_MEMORYSTATUS | |
816 | if (memorystatus_proc_is_dirty_unsafe(task->bsd_info)) | |
817 | ss_flags |= kTaskIsDirty; | |
818 | #endif | |
819 | ||
820 | ss_flags |= (0x7 & workqueue_get_pwq_state_kdp(task->bsd_info)) << 17; | |
821 | ||
822 | #if IMPORTANCE_INHERITANCE | |
823 | if (task->task_imp_base) { | |
824 | if (task->task_imp_base->iit_donor) | |
825 | ss_flags |= kTaskIsImpDonor; | |
826 | if (task->task_imp_base->iit_live_donor) | |
827 | ss_flags |= kTaskIsLiveImpDonor; | |
828 | } | |
829 | #endif | |
830 | ||
831 | return ss_flags; | |
832 | } | |
833 | ||
834 | static kern_return_t | |
5ba3f43e | 835 | kcdata_record_shared_cache_info(kcdata_descriptor_t kcd, task_t task, struct dyld_uuid_info_64_v2 *sys_shared_cache_loadinfo, unaligned_u64 *task_snap_ss_flags) |
39037602 A |
836 | { |
837 | kern_return_t error = KERN_SUCCESS; | |
3e170ce0 | 838 | mach_vm_address_t out_addr = 0; |
3e170ce0 | 839 | |
39037602 A |
840 | uint64_t shared_cache_slide = 0; |
841 | uint64_t shared_cache_base_address = 0; | |
842 | int task_pid = pid_from_task(task); | |
39037602 | 843 | uint32_t kdp_fault_results = 0; |
3e170ce0 | 844 | |
39037602 | 845 | assert(task_snap_ss_flags != NULL); |
3e170ce0 | 846 | |
39037602 A |
847 | if (task->shared_region && ml_validate_nofault((vm_offset_t)task->shared_region, sizeof(struct vm_shared_region))) { |
848 | struct vm_shared_region *sr = task->shared_region; | |
849 | shared_cache_base_address = sr->sr_base_address + sr->sr_first_mapping; | |
850 | } else { | |
851 | *task_snap_ss_flags |= kTaskSharedRegionInfoUnavailable; | |
5ba3f43e | 852 | goto error_exit; |
39037602 | 853 | } |
3e170ce0 | 854 | |
5ba3f43e A |
855 | /* We haven't copied in the shared region UUID yet as part of setup */ |
856 | if (!shared_cache_base_address || !task->shared_region->sr_uuid_copied) { | |
3e170ce0 A |
857 | goto error_exit; |
858 | } | |
859 | ||
5ba3f43e A |
860 | /* |
861 | * No refcounting here, but we are in debugger | |
862 | * context, so that should be safe. | |
863 | */ | |
864 | shared_cache_slide = task->shared_region->sr_slide_info.slide; | |
3e170ce0 | 865 | |
39037602 A |
866 | if (sys_shared_cache_loadinfo) { |
867 | if (task_pid == 1) { | |
868 | /* save launchd's shared cache info as system level */ | |
5ba3f43e | 869 | stackshot_memcpy(sys_shared_cache_loadinfo->imageUUID, &task->shared_region->sr_uuid, sizeof(task->shared_region->sr_uuid)); |
39037602 A |
870 | sys_shared_cache_loadinfo->imageLoadAddress = shared_cache_slide; |
871 | sys_shared_cache_loadinfo->imageSlidBaseAddress = shared_cache_slide + task->shared_region->sr_base_address; | |
3e170ce0 | 872 | |
39037602 A |
873 | goto error_exit; |
874 | } else { | |
875 | if (shared_cache_slide == sys_shared_cache_loadinfo->imageLoadAddress && | |
5ba3f43e A |
876 | 0 == memcmp(&task->shared_region->sr_uuid, sys_shared_cache_loadinfo->imageUUID, |
877 | sizeof(task->shared_region->sr_uuid))) { | |
39037602 A |
878 | /* skip adding shared cache info. its same as system level one */ |
879 | goto error_exit; | |
880 | } | |
881 | } | |
882 | } | |
3e170ce0 | 883 | |
39037602 A |
884 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO, sizeof(struct dyld_uuid_info_64_v2), &out_addr)); |
885 | struct dyld_uuid_info_64_v2 *shared_cache_data = (struct dyld_uuid_info_64_v2 *)out_addr; | |
886 | shared_cache_data->imageLoadAddress = shared_cache_slide; | |
5ba3f43e | 887 | stackshot_memcpy(shared_cache_data->imageUUID, task->shared_region->sr_uuid, sizeof(task->shared_region->sr_uuid)); |
39037602 | 888 | shared_cache_data->imageSlidBaseAddress = shared_cache_base_address; |
3e170ce0 | 889 | |
39037602 A |
890 | error_exit: |
891 | if (kdp_fault_results & KDP_FAULT_RESULT_PAGED_OUT) { | |
892 | *task_snap_ss_flags |= kTaskUUIDInfoMissing; | |
893 | } | |
3e170ce0 | 894 | |
39037602 A |
895 | if (kdp_fault_results & KDP_FAULT_RESULT_TRIED_FAULT) { |
896 | *task_snap_ss_flags |= kTaskUUIDInfoTriedFault; | |
897 | } | |
3e170ce0 | 898 | |
39037602 A |
899 | if (kdp_fault_results & KDP_FAULT_RESULT_FAULTED_IN) { |
900 | *task_snap_ss_flags |= kTaskUUIDInfoFaultedIn; | |
901 | } | |
3e170ce0 | 902 | |
39037602 A |
903 | return error; |
904 | } | |
3e170ce0 | 905 | |
39037602 | 906 | static kern_return_t |
813fb2f6 | 907 | kcdata_record_uuid_info(kcdata_descriptor_t kcd, task_t task, uint32_t trace_flags, boolean_t have_pmap, unaligned_u64 *task_snap_ss_flags) |
39037602 A |
908 | { |
909 | boolean_t save_loadinfo_p = ((trace_flags & STACKSHOT_SAVE_LOADINFO) != 0); | |
910 | boolean_t save_kextloadinfo_p = ((trace_flags & STACKSHOT_SAVE_KEXT_LOADINFO) != 0); | |
911 | boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0); | |
912 | boolean_t minimize_uuids = collect_delta_stackshot && ((trace_flags & STACKSHOT_TAILSPIN) != 0); | |
913 | boolean_t should_fault = (trace_flags & STACKSHOT_ENABLE_UUID_FAULTING); | |
3e170ce0 | 914 | |
39037602 A |
915 | kern_return_t error = KERN_SUCCESS; |
916 | mach_vm_address_t out_addr = 0; | |
3e170ce0 | 917 | |
39037602 A |
918 | uint32_t uuid_info_count = 0; |
919 | mach_vm_address_t uuid_info_addr = 0; | |
920 | uint64_t uuid_info_timestamp = 0; | |
921 | uint32_t kdp_fault_results = 0; | |
922 | ||
923 | assert(task_snap_ss_flags != NULL); | |
924 | ||
925 | int task_pid = pid_from_task(task); | |
926 | boolean_t task64 = task_has_64BitAddr(task); | |
927 | ||
928 | if (save_loadinfo_p && have_pmap && task->active && task_pid > 0) { | |
929 | /* Read the dyld_all_image_infos struct from the task memory to get UUID array count and location */ | |
930 | if (task64) { | |
931 | struct user64_dyld_all_image_infos task_image_infos; | |
932 | if (kdp_copyin(task->map, task->all_image_info_addr, &task_image_infos, | |
933 | sizeof(struct user64_dyld_all_image_infos), should_fault, &kdp_fault_results)) { | |
934 | uuid_info_count = (uint32_t)task_image_infos.uuidArrayCount; | |
935 | uuid_info_addr = task_image_infos.uuidArray; | |
5ba3f43e | 936 | if (task_image_infos.version >= DYLD_ALL_IMAGE_INFOS_TIMESTAMP_MINIMUM_VERSION) { |
39037602 A |
937 | uuid_info_timestamp = task_image_infos.timestamp; |
938 | } | |
939 | } | |
940 | } else { | |
941 | struct user32_dyld_all_image_infos task_image_infos; | |
942 | if (kdp_copyin(task->map, task->all_image_info_addr, &task_image_infos, | |
943 | sizeof(struct user32_dyld_all_image_infos), should_fault, &kdp_fault_results)) { | |
944 | uuid_info_count = task_image_infos.uuidArrayCount; | |
945 | uuid_info_addr = task_image_infos.uuidArray; | |
5ba3f43e | 946 | if (task_image_infos.version >= DYLD_ALL_IMAGE_INFOS_TIMESTAMP_MINIMUM_VERSION) { |
39037602 A |
947 | uuid_info_timestamp = task_image_infos.timestamp; |
948 | } | |
3e170ce0 A |
949 | } |
950 | } | |
951 | ||
39037602 A |
952 | /* |
953 | * If we get a NULL uuid_info_addr (which can happen when we catch dyld in the middle of updating | |
954 | * this data structure), we zero the uuid_info_count so that we won't even try to save load info | |
955 | * for this task. | |
956 | */ | |
957 | if (!uuid_info_addr) { | |
958 | uuid_info_count = 0; | |
959 | } | |
960 | } | |
3e170ce0 | 961 | |
39037602 A |
962 | if (have_pmap && task_pid == 0) { |
963 | if (save_kextloadinfo_p && ml_validate_nofault((vm_offset_t)(gLoadedKextSummaries), sizeof(OSKextLoadedKextSummaryHeader))) { | |
964 | uuid_info_count = gLoadedKextSummaries->numSummaries + 1; /* include main kernel UUID */ | |
965 | } else { | |
966 | uuid_info_count = 1; /* include kernelcache UUID (embedded) or kernel UUID (desktop) */ | |
967 | } | |
968 | } | |
3e170ce0 | 969 | |
39037602 A |
970 | if (task_pid > 0 && uuid_info_count > 0 && uuid_info_count < MAX_LOADINFOS) { |
971 | if (minimize_uuids && uuid_info_timestamp != 0 && uuid_info_timestamp < stack_snapshot_delta_since_timestamp) | |
972 | goto error_exit; | |
3e170ce0 | 973 | |
39037602 A |
974 | uint32_t uuid_info_size = (uint32_t)(task64 ? sizeof(struct user64_dyld_uuid_info) : sizeof(struct user32_dyld_uuid_info)); |
975 | uint32_t uuid_info_array_size = uuid_info_count * uuid_info_size; | |
3e170ce0 | 976 | |
39037602 A |
977 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, (task64 ? KCDATA_TYPE_LIBRARY_LOADINFO64 : KCDATA_TYPE_LIBRARY_LOADINFO), |
978 | uuid_info_size, uuid_info_count, &out_addr)); | |
3e170ce0 | 979 | |
39037602 A |
980 | /* Copy in the UUID info array |
981 | * It may be nonresident, in which case just fix up nloadinfos to 0 in the task_snap | |
982 | */ | |
983 | if (have_pmap && !kdp_copyin(task->map, uuid_info_addr, (void *)out_addr, uuid_info_array_size, should_fault, &kdp_fault_results)) { | |
984 | bzero((void *)out_addr, uuid_info_array_size); | |
985 | } | |
3e170ce0 | 986 | |
39037602 A |
987 | } else if (task_pid == 0 && uuid_info_count > 0 && uuid_info_count < MAX_LOADINFOS) { |
988 | if (minimize_uuids && gLoadedKextSummaries != 0 && gLoadedKextSummariesTimestamp < stack_snapshot_delta_since_timestamp) | |
989 | goto error_exit; | |
3e170ce0 | 990 | |
39037602 A |
991 | uintptr_t image_load_address; |
992 | ||
993 | do { | |
994 | ||
5ba3f43e A |
995 | #if CONFIG_EMBEDDED |
996 | if (!save_kextloadinfo_p) { | |
997 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO, sizeof(struct dyld_uuid_info_64), &out_addr)); | |
998 | struct dyld_uuid_info_64 *kc_uuid = (struct dyld_uuid_info_64 *)out_addr; | |
999 | kc_uuid->imageLoadAddress = VM_MIN_KERNEL_AND_KEXT_ADDRESS; | |
1000 | stackshot_memcpy(&kc_uuid->imageUUID, &kernelcache_uuid, sizeof(uuid_t)); | |
1001 | break; | |
1002 | } | |
1003 | #endif /* CONFIG_EMBEDDED */ | |
39037602 A |
1004 | |
1005 | if (!kernel_uuid || !ml_validate_nofault((vm_offset_t)kernel_uuid, sizeof(uuid_t))) { | |
1006 | /* Kernel UUID not found or inaccessible */ | |
1007 | break; | |
3e170ce0 | 1008 | } |
3e170ce0 | 1009 | |
39037602 A |
1010 | kcd_exit_on_error(kcdata_get_memory_addr_for_array( |
1011 | kcd, (sizeof(kernel_uuid_info) == sizeof(struct user64_dyld_uuid_info)) ? KCDATA_TYPE_LIBRARY_LOADINFO64 | |
1012 | : KCDATA_TYPE_LIBRARY_LOADINFO, | |
1013 | sizeof(kernel_uuid_info), uuid_info_count, &out_addr)); | |
1014 | kernel_uuid_info *uuid_info_array = (kernel_uuid_info *)out_addr; | |
1015 | image_load_address = (uintptr_t)VM_KERNEL_UNSLIDE(vm_kernel_stext); | |
1016 | uuid_info_array[0].imageLoadAddress = image_load_address; | |
1017 | stackshot_memcpy(&uuid_info_array[0].imageUUID, kernel_uuid, sizeof(uuid_t)); | |
1018 | ||
1019 | if (save_kextloadinfo_p && | |
1020 | ml_validate_nofault((vm_offset_t)(gLoadedKextSummaries), sizeof(OSKextLoadedKextSummaryHeader)) && | |
1021 | ml_validate_nofault((vm_offset_t)(&gLoadedKextSummaries->summaries[0]), | |
1022 | gLoadedKextSummaries->entry_size * gLoadedKextSummaries->numSummaries)) { | |
1023 | uint32_t kexti; | |
1024 | for (kexti=0 ; kexti < gLoadedKextSummaries->numSummaries; kexti++) { | |
1025 | image_load_address = (uintptr_t)VM_KERNEL_UNSLIDE(gLoadedKextSummaries->summaries[kexti].address); | |
1026 | uuid_info_array[kexti + 1].imageLoadAddress = image_load_address; | |
1027 | stackshot_memcpy(&uuid_info_array[kexti + 1].imageUUID, &gLoadedKextSummaries->summaries[kexti].uuid, sizeof(uuid_t)); | |
3e170ce0 | 1028 | } |
39037602 A |
1029 | } |
1030 | } while(0); | |
1031 | } | |
3e170ce0 | 1032 | |
39037602 A |
1033 | error_exit: |
1034 | if (kdp_fault_results & KDP_FAULT_RESULT_PAGED_OUT) { | |
1035 | *task_snap_ss_flags |= kTaskUUIDInfoMissing; | |
1036 | } | |
3e170ce0 | 1037 | |
39037602 A |
1038 | if (kdp_fault_results & KDP_FAULT_RESULT_TRIED_FAULT) { |
1039 | *task_snap_ss_flags |= kTaskUUIDInfoTriedFault; | |
1040 | } | |
3e170ce0 | 1041 | |
39037602 A |
1042 | if (kdp_fault_results & KDP_FAULT_RESULT_FAULTED_IN) { |
1043 | *task_snap_ss_flags |= kTaskUUIDInfoFaultedIn; | |
1044 | } | |
3e170ce0 | 1045 | |
39037602 A |
1046 | return error; |
1047 | } | |
3e170ce0 | 1048 | |
39037602 A |
1049 | static kern_return_t |
1050 | kcdata_record_task_iostats(kcdata_descriptor_t kcd, task_t task) | |
1051 | { | |
1052 | kern_return_t error = KERN_SUCCESS; | |
1053 | mach_vm_address_t out_addr = 0; | |
3e170ce0 | 1054 | |
39037602 A |
1055 | /* I/O Statistics if any counters are non zero */ |
1056 | assert(IO_NUM_PRIORITIES == STACKSHOT_IO_NUM_PRIORITIES); | |
1057 | if (task->task_io_stats && !memory_iszero(task->task_io_stats, sizeof(struct io_stat_info))) { | |
1058 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_IOSTATS, sizeof(struct io_stats_snapshot), &out_addr)); | |
1059 | struct io_stats_snapshot *_iostat = (struct io_stats_snapshot *)out_addr; | |
1060 | _iostat->ss_disk_reads_count = task->task_io_stats->disk_reads.count; | |
1061 | _iostat->ss_disk_reads_size = task->task_io_stats->disk_reads.size; | |
1062 | _iostat->ss_disk_writes_count = (task->task_io_stats->total_io.count - task->task_io_stats->disk_reads.count); | |
1063 | _iostat->ss_disk_writes_size = (task->task_io_stats->total_io.size - task->task_io_stats->disk_reads.size); | |
1064 | _iostat->ss_paging_count = task->task_io_stats->paging.count; | |
1065 | _iostat->ss_paging_size = task->task_io_stats->paging.size; | |
1066 | _iostat->ss_non_paging_count = (task->task_io_stats->total_io.count - task->task_io_stats->paging.count); | |
1067 | _iostat->ss_non_paging_size = (task->task_io_stats->total_io.size - task->task_io_stats->paging.size); | |
1068 | _iostat->ss_metadata_count = task->task_io_stats->metadata.count; | |
1069 | _iostat->ss_metadata_size = task->task_io_stats->metadata.size; | |
1070 | _iostat->ss_data_count = (task->task_io_stats->total_io.count - task->task_io_stats->metadata.count); | |
1071 | _iostat->ss_data_size = (task->task_io_stats->total_io.size - task->task_io_stats->metadata.size); | |
1072 | for(int i = 0; i < IO_NUM_PRIORITIES; i++) { | |
1073 | _iostat->ss_io_priority_count[i] = task->task_io_stats->io_priority[i].count; | |
1074 | _iostat->ss_io_priority_size[i] = task->task_io_stats->io_priority[i].size; | |
1075 | } | |
1076 | } | |
1077 | ||
1078 | error_exit: | |
1079 | return error; | |
1080 | } | |
1081 | ||
a39ff7e2 A |
1082 | #if MONOTONIC |
1083 | static kern_return_t | |
1084 | kcdata_record_task_instrs_cycles(kcdata_descriptor_t kcd, task_t task) | |
1085 | { | |
1086 | uint64_t instrs = 0, cycles = 0; | |
1087 | mt_stackshot_task(task, &instrs, &cycles); | |
1088 | ||
1089 | kern_return_t error = KERN_SUCCESS; | |
1090 | mach_vm_address_t out_addr = 0; | |
1091 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_INSTRS_CYCLES, sizeof(struct instrs_cycles_snapshot), &out_addr)); | |
1092 | struct instrs_cycles_snapshot *instrs_cycles = (struct instrs_cycles_snapshot *)out_addr; | |
1093 | instrs_cycles->ics_instructions = instrs; | |
1094 | instrs_cycles->ics_cycles = cycles; | |
1095 | ||
1096 | error_exit: | |
1097 | return error; | |
1098 | } | |
1099 | #endif /* MONOTONIC */ | |
1100 | ||
39037602 | 1101 | static kern_return_t |
813fb2f6 | 1102 | kcdata_record_task_snapshot(kcdata_descriptor_t kcd, task_t task, uint32_t trace_flags, boolean_t have_pmap, unaligned_u64 **task_snap_ss_flags) |
39037602 A |
1103 | { |
1104 | boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0); | |
1105 | boolean_t collect_iostats = !collect_delta_stackshot && !(trace_flags & STACKSHOT_TAILSPIN) && !(trace_flags & STACKSHOT_NO_IO_STATS); | |
5ba3f43e A |
1106 | #if MONOTONIC |
1107 | boolean_t collect_instrs_cycles = ((trace_flags & STACKSHOT_INSTRS_CYCLES) != 0); | |
1108 | #endif /* MONOTONIC */ | |
39037602 A |
1109 | |
1110 | kern_return_t error = KERN_SUCCESS; | |
1111 | mach_vm_address_t out_addr = 0; | |
1112 | struct task_snapshot_v2 * cur_tsnap = NULL; | |
1113 | ||
1114 | assert(task_snap_ss_flags != NULL); | |
1115 | ||
1116 | int task_pid = pid_from_task(task); | |
1117 | uint64_t task_uniqueid = get_task_uniqueid(task); | |
813fb2f6 | 1118 | uint64_t proc_starttime_secs = 0; |
39037602 A |
1119 | |
1120 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_TASK_SNAPSHOT, sizeof(struct task_snapshot_v2), &out_addr)); | |
1121 | ||
1122 | cur_tsnap = (struct task_snapshot_v2 *)out_addr; | |
1123 | ||
1124 | cur_tsnap->ts_unique_pid = task_uniqueid; | |
1125 | cur_tsnap->ts_ss_flags = kcdata_get_task_ss_flags(task); | |
813fb2f6 | 1126 | *task_snap_ss_flags = (unaligned_u64 *)&cur_tsnap->ts_ss_flags; |
39037602 A |
1127 | cur_tsnap->ts_user_time_in_terminated_threads = task->total_user_time; |
1128 | cur_tsnap->ts_system_time_in_terminated_threads = task->total_system_time; | |
1129 | ||
813fb2f6 A |
1130 | proc_starttime_kdp(task->bsd_info, &proc_starttime_secs, NULL, NULL); |
1131 | cur_tsnap->ts_p_start_sec = proc_starttime_secs; | |
39037602 | 1132 | |
5ba3f43e A |
1133 | #if CONFIG_EMBEDDED |
1134 | cur_tsnap->ts_task_size = have_pmap ? get_task_phys_footprint(task) : 0; | |
1135 | #else | |
39037602 | 1136 | cur_tsnap->ts_task_size = have_pmap ? (pmap_resident_count(task->map->pmap) * PAGE_SIZE) : 0; |
5ba3f43e | 1137 | #endif |
39037602 A |
1138 | cur_tsnap->ts_max_resident_size = get_task_resident_max(task); |
1139 | cur_tsnap->ts_suspend_count = task->suspend_count; | |
1140 | cur_tsnap->ts_faults = task->faults; | |
1141 | cur_tsnap->ts_pageins = task->pageins; | |
1142 | cur_tsnap->ts_cow_faults = task->cow_faults; | |
1143 | cur_tsnap->ts_was_throttled = (uint32_t) proc_was_throttled_from_task(task); | |
1144 | cur_tsnap->ts_did_throttle = (uint32_t) proc_did_throttle_from_task(task); | |
1145 | cur_tsnap->ts_latency_qos = (task->effective_policy.tep_latency_qos == LATENCY_QOS_TIER_UNSPECIFIED) ? | |
1146 | LATENCY_QOS_TIER_UNSPECIFIED : ((0xFF << 16) | task->effective_policy.tep_latency_qos); | |
1147 | cur_tsnap->ts_pid = task_pid; | |
1148 | ||
1149 | /* Add the BSD process identifiers */ | |
5ba3f43e | 1150 | if (task_pid != -1 && task->bsd_info != NULL) { |
39037602 | 1151 | proc_name_kdp(task, cur_tsnap->ts_p_comm, sizeof(cur_tsnap->ts_p_comm)); |
5ba3f43e A |
1152 | #if CONFIG_COALITIONS |
1153 | if (trace_flags & STACKSHOT_SAVE_JETSAM_COALITIONS) { | |
1154 | uint64_t jetsam_coal_id = coalition_id(task->coalition[COALITION_TYPE_JETSAM]); | |
1155 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_JETSAM_COALITION, sizeof(jetsam_coal_id), &out_addr)); | |
1156 | stackshot_memcpy((void*)out_addr, &jetsam_coal_id, sizeof(jetsam_coal_id)); | |
1157 | } | |
1158 | #endif /* CONFIG_COALITIONS */ | |
1159 | } | |
39037602 A |
1160 | else { |
1161 | cur_tsnap->ts_p_comm[0] = '\0'; | |
1162 | #if IMPORTANCE_INHERITANCE && (DEVELOPMENT || DEBUG) | |
1163 | if (task->task_imp_base != NULL) { | |
1164 | stackshot_strlcpy(cur_tsnap->ts_p_comm, &task->task_imp_base->iit_procname[0], | |
1165 | MIN((int)sizeof(task->task_imp_base->iit_procname), (int)sizeof(cur_tsnap->ts_p_comm))); | |
1166 | } | |
5ba3f43e | 1167 | #endif /* IMPORTANCE_INHERITANCE && (DEVELOPMENT || DEBUG) */ |
39037602 | 1168 | } |
3e170ce0 | 1169 | |
39037602 A |
1170 | if (collect_iostats) { |
1171 | kcd_exit_on_error(kcdata_record_task_iostats(kcd, task)); | |
1172 | } | |
3e170ce0 | 1173 | |
5ba3f43e A |
1174 | #if MONOTONIC |
1175 | if (collect_instrs_cycles) { | |
a39ff7e2 | 1176 | kcd_exit_on_error(kcdata_record_task_instrs_cycles(kcd, task)); |
5ba3f43e A |
1177 | } |
1178 | #endif /* MONOTONIC */ | |
1179 | ||
39037602 A |
1180 | error_exit: |
1181 | return error; | |
1182 | } | |
3e170ce0 | 1183 | |
39037602 | 1184 | static kern_return_t |
a39ff7e2 | 1185 | kcdata_record_task_delta_snapshot(kcdata_descriptor_t kcd, task_t task, uint32_t trace_flags, boolean_t have_pmap, unaligned_u64 **task_snap_ss_flags) |
39037602 A |
1186 | { |
1187 | kern_return_t error = KERN_SUCCESS; | |
1188 | struct task_delta_snapshot_v2 * cur_tsnap = NULL; | |
1189 | mach_vm_address_t out_addr = 0; | |
a39ff7e2 A |
1190 | #if MONOTONIC |
1191 | boolean_t collect_instrs_cycles = ((trace_flags & STACKSHOT_INSTRS_CYCLES) != 0); | |
1192 | #else | |
1193 | (void)trace_flags; | |
1194 | #endif /* MONOTONIC */ | |
3e170ce0 | 1195 | |
39037602 A |
1196 | uint64_t task_uniqueid = get_task_uniqueid(task); |
1197 | assert(task_snap_ss_flags != NULL); | |
3e170ce0 | 1198 | |
39037602 | 1199 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT, sizeof(struct task_delta_snapshot_v2), &out_addr)); |
3e170ce0 | 1200 | |
39037602 | 1201 | cur_tsnap = (struct task_delta_snapshot_v2 *)out_addr; |
3e170ce0 | 1202 | |
39037602 A |
1203 | cur_tsnap->tds_unique_pid = task_uniqueid; |
1204 | cur_tsnap->tds_ss_flags = kcdata_get_task_ss_flags(task); | |
813fb2f6 | 1205 | *task_snap_ss_flags = (unaligned_u64 *)&cur_tsnap->tds_ss_flags; |
3e170ce0 | 1206 | |
39037602 A |
1207 | cur_tsnap->tds_user_time_in_terminated_threads = task->total_user_time; |
1208 | cur_tsnap->tds_system_time_in_terminated_threads = task->total_system_time; | |
3e170ce0 | 1209 | |
5ba3f43e A |
1210 | #if CONFIG_EMBEDDED |
1211 | cur_tsnap->tds_task_size = have_pmap ? get_task_phys_footprint(task) : 0; | |
1212 | #else | |
39037602 | 1213 | cur_tsnap->tds_task_size = have_pmap ? (pmap_resident_count(task->map->pmap) * PAGE_SIZE) : 0; |
5ba3f43e | 1214 | #endif |
3e170ce0 | 1215 | |
39037602 A |
1216 | cur_tsnap->tds_max_resident_size = get_task_resident_max(task); |
1217 | cur_tsnap->tds_suspend_count = task->suspend_count; | |
1218 | cur_tsnap->tds_faults = task->faults; | |
1219 | cur_tsnap->tds_pageins = task->pageins; | |
1220 | cur_tsnap->tds_cow_faults = task->cow_faults; | |
1221 | cur_tsnap->tds_was_throttled = (uint32_t)proc_was_throttled_from_task(task); | |
1222 | cur_tsnap->tds_did_throttle = (uint32_t)proc_did_throttle_from_task(task); | |
1223 | cur_tsnap->tds_latency_qos = (task-> effective_policy.tep_latency_qos == LATENCY_QOS_TIER_UNSPECIFIED) | |
1224 | ? LATENCY_QOS_TIER_UNSPECIFIED | |
1225 | : ((0xFF << 16) | task-> effective_policy.tep_latency_qos); | |
3e170ce0 | 1226 | |
a39ff7e2 A |
1227 | #if MONOTONIC |
1228 | if (collect_instrs_cycles) { | |
1229 | kcd_exit_on_error(kcdata_record_task_instrs_cycles(kcd, task)); | |
1230 | } | |
1231 | #endif /* MONOTONIC */ | |
1232 | ||
39037602 A |
1233 | error_exit: |
1234 | return error; | |
1235 | } | |
3e170ce0 | 1236 | |
39037602 A |
1237 | static kern_return_t |
1238 | kcdata_record_thread_iostats(kcdata_descriptor_t kcd, thread_t thread) | |
1239 | { | |
1240 | kern_return_t error = KERN_SUCCESS; | |
1241 | mach_vm_address_t out_addr = 0; | |
3e170ce0 | 1242 | |
39037602 A |
1243 | /* I/O Statistics */ |
1244 | assert(IO_NUM_PRIORITIES == STACKSHOT_IO_NUM_PRIORITIES); | |
1245 | if (thread->thread_io_stats && !memory_iszero(thread->thread_io_stats, sizeof(struct io_stat_info))) { | |
1246 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_IOSTATS, sizeof(struct io_stats_snapshot), &out_addr)); | |
1247 | struct io_stats_snapshot *_iostat = (struct io_stats_snapshot *)out_addr; | |
1248 | _iostat->ss_disk_reads_count = thread->thread_io_stats->disk_reads.count; | |
1249 | _iostat->ss_disk_reads_size = thread->thread_io_stats->disk_reads.size; | |
1250 | _iostat->ss_disk_writes_count = (thread->thread_io_stats->total_io.count - thread->thread_io_stats->disk_reads.count); | |
1251 | _iostat->ss_disk_writes_size = (thread->thread_io_stats->total_io.size - thread->thread_io_stats->disk_reads.size); | |
1252 | _iostat->ss_paging_count = thread->thread_io_stats->paging.count; | |
1253 | _iostat->ss_paging_size = thread->thread_io_stats->paging.size; | |
1254 | _iostat->ss_non_paging_count = (thread->thread_io_stats->total_io.count - thread->thread_io_stats->paging.count); | |
1255 | _iostat->ss_non_paging_size = (thread->thread_io_stats->total_io.size - thread->thread_io_stats->paging.size); | |
1256 | _iostat->ss_metadata_count = thread->thread_io_stats->metadata.count; | |
1257 | _iostat->ss_metadata_size = thread->thread_io_stats->metadata.size; | |
1258 | _iostat->ss_data_count = (thread->thread_io_stats->total_io.count - thread->thread_io_stats->metadata.count); | |
1259 | _iostat->ss_data_size = (thread->thread_io_stats->total_io.size - thread->thread_io_stats->metadata.size); | |
1260 | for(int i = 0; i < IO_NUM_PRIORITIES; i++) { | |
1261 | _iostat->ss_io_priority_count[i] = thread->thread_io_stats->io_priority[i].count; | |
1262 | _iostat->ss_io_priority_size[i] = thread->thread_io_stats->io_priority[i].size; | |
1263 | } | |
1264 | } | |
3e170ce0 | 1265 | |
39037602 A |
1266 | error_exit: |
1267 | return error; | |
1268 | } | |
3e170ce0 | 1269 | |
39037602 A |
1270 | static kern_return_t |
1271 | kcdata_record_thread_snapshot( | |
1272 | kcdata_descriptor_t kcd, thread_t thread, task_t task, uint32_t trace_flags, boolean_t have_pmap, boolean_t thread_on_core) | |
1273 | { | |
1274 | boolean_t dispatch_p = ((trace_flags & STACKSHOT_GET_DQ) != 0); | |
1275 | boolean_t active_kthreads_only_p = ((trace_flags & STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY) != 0); | |
00867663 | 1276 | boolean_t trace_fp_p = false; |
39037602 A |
1277 | boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0); |
1278 | boolean_t collect_iostats = !collect_delta_stackshot && !(trace_flags & STACKSHOT_TAILSPIN) && !(trace_flags & STACKSHOT_NO_IO_STATS); | |
5ba3f43e A |
1279 | #if MONOTONIC |
1280 | boolean_t collect_instrs_cycles = ((trace_flags & STACKSHOT_INSTRS_CYCLES) != 0); | |
1281 | #endif /* MONOTONIC */ | |
3e170ce0 | 1282 | |
39037602 A |
1283 | kern_return_t error = KERN_SUCCESS; |
1284 | mach_vm_address_t out_addr = 0; | |
1285 | int saved_count = 0; | |
1286 | ||
5ba3f43e | 1287 | struct thread_snapshot_v4 * cur_thread_snap = NULL; |
39037602 A |
1288 | char cur_thread_name[STACKSHOT_MAX_THREAD_NAME_SIZE]; |
1289 | uint64_t tval = 0; | |
1290 | boolean_t task64 = task_has_64BitAddr(task); | |
1291 | ||
5ba3f43e A |
1292 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_THREAD_SNAPSHOT, sizeof(struct thread_snapshot_v4), &out_addr)); |
1293 | cur_thread_snap = (struct thread_snapshot_v4 *)out_addr; | |
39037602 A |
1294 | |
1295 | /* Populate the thread snapshot header */ | |
1296 | cur_thread_snap->ths_thread_id = thread_tid(thread); | |
1297 | cur_thread_snap->ths_wait_event = VM_KERNEL_UNSLIDE_OR_PERM(thread->wait_event); | |
1298 | cur_thread_snap->ths_continuation = VM_KERNEL_UNSLIDE(thread->continuation); | |
1299 | cur_thread_snap->ths_total_syscalls = thread->syscalls_mach + thread->syscalls_unix; | |
1300 | ||
1301 | if (IPC_VOUCHER_NULL != thread->ith_voucher) | |
1302 | cur_thread_snap->ths_voucher_identifier = VM_KERNEL_ADDRPERM(thread->ith_voucher); | |
1303 | else | |
1304 | cur_thread_snap->ths_voucher_identifier = 0; | |
1305 | ||
1306 | cur_thread_snap->ths_dqserialnum = 0; | |
1307 | if (dispatch_p && (task != kernel_task) && (task->active) && have_pmap) { | |
1308 | uint64_t dqkeyaddr = thread_dispatchqaddr(thread); | |
1309 | if (dqkeyaddr != 0) { | |
1310 | uint64_t dqaddr = 0; | |
1311 | boolean_t copyin_ok = kdp_copyin_word(task, dqkeyaddr, &dqaddr, FALSE, NULL); | |
1312 | if (copyin_ok && dqaddr != 0) { | |
1313 | uint64_t dqserialnumaddr = dqaddr + get_task_dispatchqueue_serialno_offset(task); | |
1314 | uint64_t dqserialnum = 0; | |
1315 | copyin_ok = kdp_copyin_word(task, dqserialnumaddr, &dqserialnum, FALSE, NULL); | |
1316 | if (copyin_ok) { | |
1317 | cur_thread_snap->ths_ss_flags |= kHasDispatchSerial; | |
1318 | cur_thread_snap->ths_dqserialnum = dqserialnum; | |
3e170ce0 | 1319 | } |
39037602 A |
1320 | } |
1321 | } | |
1322 | } | |
3e170ce0 | 1323 | |
39037602 A |
1324 | tval = safe_grab_timer_value(&thread->user_timer); |
1325 | cur_thread_snap->ths_user_time = tval; | |
1326 | tval = safe_grab_timer_value(&thread->system_timer); | |
3e170ce0 | 1327 | |
39037602 A |
1328 | if (thread->precise_user_kernel_time) { |
1329 | cur_thread_snap->ths_sys_time = tval; | |
1330 | } else { | |
1331 | cur_thread_snap->ths_user_time += tval; | |
1332 | cur_thread_snap->ths_sys_time = 0; | |
1333 | } | |
3e170ce0 | 1334 | |
39037602 | 1335 | cur_thread_snap->ths_ss_flags = 0; |
5ba3f43e A |
1336 | if (thread->thread_tag & THREAD_TAG_MAINTHREAD) |
1337 | cur_thread_snap->ths_ss_flags |= kThreadMain; | |
39037602 A |
1338 | if (thread->effective_policy.thep_darwinbg) |
1339 | cur_thread_snap->ths_ss_flags |= kThreadDarwinBG; | |
1340 | if (proc_get_effective_thread_policy(thread, TASK_POLICY_PASSIVE_IO)) | |
1341 | cur_thread_snap->ths_ss_flags |= kThreadIOPassive; | |
1342 | if (thread->suspend_count > 0) | |
1343 | cur_thread_snap->ths_ss_flags |= kThreadSuspended; | |
1344 | if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) | |
1345 | cur_thread_snap->ths_ss_flags |= kGlobalForcedIdle; | |
1346 | if (thread_on_core) | |
1347 | cur_thread_snap->ths_ss_flags |= kThreadOnCore; | |
1348 | if (stackshot_thread_is_idle_worker_unsafe(thread)) | |
1349 | cur_thread_snap->ths_ss_flags |= kThreadIdleWorker; | |
1350 | ||
1351 | /* make sure state flags defined in kcdata.h still match internal flags */ | |
1352 | static_assert(SS_TH_WAIT == TH_WAIT); | |
1353 | static_assert(SS_TH_SUSP == TH_SUSP); | |
1354 | static_assert(SS_TH_RUN == TH_RUN); | |
1355 | static_assert(SS_TH_UNINT == TH_UNINT); | |
1356 | static_assert(SS_TH_TERMINATE == TH_TERMINATE); | |
1357 | static_assert(SS_TH_TERMINATE2 == TH_TERMINATE2); | |
1358 | static_assert(SS_TH_IDLE == TH_IDLE); | |
1359 | ||
1360 | cur_thread_snap->ths_last_run_time = thread->last_run_time; | |
1361 | cur_thread_snap->ths_last_made_runnable_time = thread->last_made_runnable_time; | |
1362 | cur_thread_snap->ths_state = thread->state; | |
1363 | cur_thread_snap->ths_sched_flags = thread->sched_flags; | |
1364 | cur_thread_snap->ths_base_priority = thread->base_pri; | |
1365 | cur_thread_snap->ths_sched_priority = thread->sched_pri; | |
1366 | cur_thread_snap->ths_eqos = thread->effective_policy.thep_qos; | |
1367 | cur_thread_snap->ths_rqos = thread->requested_policy.thrp_qos; | |
1368 | cur_thread_snap->ths_rqos_override = thread->requested_policy.thrp_qos_override; | |
1369 | cur_thread_snap->ths_io_tier = proc_get_effective_thread_policy(thread, TASK_POLICY_IO); | |
5ba3f43e A |
1370 | cur_thread_snap->ths_thread_t = VM_KERNEL_UNSLIDE_OR_PERM(thread); |
1371 | ||
1372 | static_assert(sizeof(thread->effective_policy) == sizeof(uint64_t)); | |
1373 | static_assert(sizeof(thread->requested_policy) == sizeof(uint64_t)); | |
1374 | cur_thread_snap->ths_requested_policy = *(unaligned_u64 *) &thread->requested_policy; | |
1375 | cur_thread_snap->ths_effective_policy = *(unaligned_u64 *) &thread->effective_policy; | |
39037602 A |
1376 | |
1377 | /* if there is thread name then add to buffer */ | |
1378 | cur_thread_name[0] = '\0'; | |
1379 | proc_threadname_kdp(thread->uthread, cur_thread_name, STACKSHOT_MAX_THREAD_NAME_SIZE); | |
1380 | if (strnlen(cur_thread_name, STACKSHOT_MAX_THREAD_NAME_SIZE) > 0) { | |
1381 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_THREAD_NAME, sizeof(cur_thread_name), &out_addr)); | |
1382 | stackshot_memcpy((void *)out_addr, (void *)cur_thread_name, sizeof(cur_thread_name)); | |
1383 | } | |
3e170ce0 | 1384 | |
39037602 A |
1385 | /* record system and user cpu times */ |
1386 | time_value_t user_time; | |
1387 | time_value_t system_time; | |
1388 | thread_read_times(thread, &user_time, &system_time); | |
1389 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_CPU_TIMES, sizeof(struct stackshot_cpu_times), &out_addr)); | |
1390 | struct stackshot_cpu_times * stackshot_cpu_times = (struct stackshot_cpu_times *)out_addr; | |
1391 | stackshot_cpu_times->user_usec = ((uint64_t)user_time.seconds) * USEC_PER_SEC + user_time.microseconds; | |
1392 | stackshot_cpu_times->system_usec = ((uint64_t)system_time.seconds) * USEC_PER_SEC + system_time.microseconds; | |
1393 | ||
1394 | /* Trace user stack, if any */ | |
1395 | if (!active_kthreads_only_p && task->active && thread->task->map != kernel_map) { | |
1396 | uint32_t thread_snapshot_flags = 0; | |
1397 | /* 64-bit task? */ | |
1398 | if (task64) { | |
1399 | out_addr = (mach_vm_address_t)kcd_end_address(kcd); | |
1400 | saved_count = machine_trace_thread64(thread, (char *)out_addr, (char *)kcd_max_address(kcd), MAX_FRAMES, TRUE, | |
1401 | trace_fp_p, &thread_snapshot_flags); | |
1402 | if (saved_count > 0) { | |
1403 | int frame_size = trace_fp_p ? sizeof(struct stack_snapshot_frame64) : sizeof(uint64_t); | |
1404 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, trace_fp_p ? STACKSHOT_KCTYPE_USER_STACKFRAME64 | |
1405 | : STACKSHOT_KCTYPE_USER_STACKLR64, | |
1406 | frame_size, saved_count / frame_size, &out_addr)); | |
1407 | cur_thread_snap->ths_ss_flags |= kUser64_p; | |
1408 | } | |
1409 | } else { | |
1410 | out_addr = (mach_vm_address_t)kcd_end_address(kcd); | |
1411 | saved_count = machine_trace_thread(thread, (char *)out_addr, (char *)kcd_max_address(kcd), MAX_FRAMES, TRUE, trace_fp_p, | |
1412 | &thread_snapshot_flags); | |
1413 | if (saved_count > 0) { | |
1414 | int frame_size = trace_fp_p ? sizeof(struct stack_snapshot_frame32) : sizeof(uint32_t); | |
1415 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, trace_fp_p ? STACKSHOT_KCTYPE_USER_STACKFRAME | |
1416 | : STACKSHOT_KCTYPE_USER_STACKLR, | |
1417 | frame_size, saved_count / frame_size, &out_addr)); | |
1418 | } | |
1419 | } | |
3e170ce0 | 1420 | |
39037602 A |
1421 | if (thread_snapshot_flags != 0) { |
1422 | cur_thread_snap->ths_ss_flags |= thread_snapshot_flags; | |
1423 | } | |
1424 | } | |
3e170ce0 | 1425 | |
39037602 A |
1426 | /* Call through to the machine specific trace routines |
1427 | * Frames are added past the snapshot header. | |
1428 | */ | |
1429 | if (thread->kernel_stack != 0) { | |
1430 | uint32_t thread_snapshot_flags = 0; | |
3e170ce0 | 1431 | #if defined(__LP64__) |
39037602 A |
1432 | out_addr = (mach_vm_address_t)kcd_end_address(kcd); |
1433 | saved_count = machine_trace_thread64(thread, (char *)out_addr, (char *)kcd_max_address(kcd), MAX_FRAMES, FALSE, trace_fp_p, | |
1434 | &thread_snapshot_flags); | |
1435 | if (saved_count > 0) { | |
1436 | int frame_size = trace_fp_p ? sizeof(struct stack_snapshot_frame64) : sizeof(uint64_t); | |
1437 | cur_thread_snap->ths_ss_flags |= kKernel64_p; | |
1438 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, trace_fp_p ? STACKSHOT_KCTYPE_KERN_STACKFRAME64 | |
1439 | : STACKSHOT_KCTYPE_KERN_STACKLR64, | |
1440 | frame_size, saved_count / frame_size, &out_addr)); | |
1441 | } | |
3e170ce0 | 1442 | #else |
39037602 A |
1443 | out_addr = (mach_vm_address_t)kcd_end_address(kcd); |
1444 | saved_count = machine_trace_thread(thread, (char *)out_addr, (char *)kcd_max_address(kcd), MAX_FRAMES, FALSE, trace_fp_p, | |
1445 | &thread_snapshot_flags); | |
1446 | if (saved_count > 0) { | |
1447 | int frame_size = trace_fp_p ? sizeof(struct stack_snapshot_frame32) : sizeof(uint32_t); | |
1448 | kcd_exit_on_error( | |
1449 | kcdata_get_memory_addr_for_array(kcd, trace_fp_p ? STACKSHOT_KCTYPE_KERN_STACKFRAME : STACKSHOT_KCTYPE_KERN_STACKLR, | |
1450 | frame_size, saved_count / frame_size, &out_addr)); | |
1451 | } | |
3e170ce0 | 1452 | #endif |
39037602 A |
1453 | if (thread_snapshot_flags != 0) { |
1454 | cur_thread_snap->ths_ss_flags |= thread_snapshot_flags; | |
3e170ce0 A |
1455 | } |
1456 | } | |
1457 | ||
5ba3f43e | 1458 | |
39037602 A |
1459 | if (collect_iostats) { |
1460 | kcd_exit_on_error(kcdata_record_thread_iostats(kcd, thread)); | |
1461 | } | |
3e170ce0 | 1462 | |
5ba3f43e A |
1463 | #if MONOTONIC |
1464 | if (collect_instrs_cycles) { | |
1465 | uint64_t instrs = 0, cycles = 0; | |
1466 | mt_stackshot_thread(thread, &instrs, &cycles); | |
1467 | ||
1468 | kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_INSTRS_CYCLES, sizeof(struct instrs_cycles_snapshot), &out_addr)); | |
1469 | struct instrs_cycles_snapshot *instrs_cycles = (struct instrs_cycles_snapshot *)out_addr; | |
1470 | instrs_cycles->ics_instructions = instrs; | |
1471 | instrs_cycles->ics_cycles = cycles; | |
1472 | } | |
1473 | #endif /* MONOTONIC */ | |
1474 | ||
3e170ce0 | 1475 | error_exit: |
3e170ce0 A |
1476 | return error; |
1477 | } | |
1478 | ||
1479 | static int | |
a39ff7e2 | 1480 | kcdata_record_thread_delta_snapshot(struct thread_delta_snapshot_v3 * cur_thread_snap, thread_t thread, boolean_t thread_on_core) |
39037602 A |
1481 | { |
1482 | cur_thread_snap->tds_thread_id = thread_tid(thread); | |
1483 | if (IPC_VOUCHER_NULL != thread->ith_voucher) | |
1484 | cur_thread_snap->tds_voucher_identifier = VM_KERNEL_ADDRPERM(thread->ith_voucher); | |
1485 | else | |
1486 | cur_thread_snap->tds_voucher_identifier = 0; | |
1487 | ||
1488 | cur_thread_snap->tds_ss_flags = 0; | |
1489 | if (thread->effective_policy.thep_darwinbg) | |
1490 | cur_thread_snap->tds_ss_flags |= kThreadDarwinBG; | |
1491 | if (proc_get_effective_thread_policy(thread, TASK_POLICY_PASSIVE_IO)) | |
1492 | cur_thread_snap->tds_ss_flags |= kThreadIOPassive; | |
1493 | if (thread->suspend_count > 0) | |
1494 | cur_thread_snap->tds_ss_flags |= kThreadSuspended; | |
1495 | if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) | |
1496 | cur_thread_snap->tds_ss_flags |= kGlobalForcedIdle; | |
1497 | if (thread_on_core) | |
1498 | cur_thread_snap->tds_ss_flags |= kThreadOnCore; | |
1499 | if (stackshot_thread_is_idle_worker_unsafe(thread)) | |
1500 | cur_thread_snap->tds_ss_flags |= kThreadIdleWorker; | |
1501 | ||
1502 | cur_thread_snap->tds_last_made_runnable_time = thread->last_made_runnable_time; | |
1503 | cur_thread_snap->tds_state = thread->state; | |
1504 | cur_thread_snap->tds_sched_flags = thread->sched_flags; | |
1505 | cur_thread_snap->tds_base_priority = thread->base_pri; | |
1506 | cur_thread_snap->tds_sched_priority = thread->sched_pri; | |
1507 | cur_thread_snap->tds_eqos = thread->effective_policy.thep_qos; | |
1508 | cur_thread_snap->tds_rqos = thread->requested_policy.thrp_qos; | |
1509 | cur_thread_snap->tds_rqos_override = thread->requested_policy.thrp_qos_override; | |
1510 | cur_thread_snap->tds_io_tier = proc_get_effective_thread_policy(thread, TASK_POLICY_IO); | |
1511 | ||
a39ff7e2 A |
1512 | static_assert(sizeof(thread->effective_policy) == sizeof(uint64_t)); |
1513 | static_assert(sizeof(thread->requested_policy) == sizeof(uint64_t)); | |
1514 | cur_thread_snap->tds_requested_policy = *(unaligned_u64 *) &thread->requested_policy; | |
1515 | cur_thread_snap->tds_effective_policy = *(unaligned_u64 *) &thread->effective_policy; | |
1516 | ||
39037602 A |
1517 | return 0; |
1518 | } | |
1519 | ||
1520 | /* | |
1521 | * Why 12? 12 strikes a decent balance between allocating a large array on | |
1522 | * the stack and having large kcdata item overheads for recording nonrunable | |
1523 | * tasks. | |
1524 | */ | |
1525 | #define UNIQUEIDSPERFLUSH 12 | |
1526 | ||
1527 | struct saved_uniqueids { | |
1528 | uint64_t ids[UNIQUEIDSPERFLUSH]; | |
1529 | unsigned count; | |
1530 | }; | |
1531 | ||
1532 | static kern_return_t | |
1533 | flush_nonrunnable_tasks(struct saved_uniqueids * ids) | |
1534 | { | |
1535 | if (ids->count == 0) | |
1536 | return KERN_SUCCESS; | |
1537 | mach_vm_address_t out_addr = 0; | |
1538 | kern_return_t ret = kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_NONRUNNABLE_TASKS, sizeof(uint64_t), | |
1539 | ids->count, &out_addr); | |
1540 | if (ret != KERN_SUCCESS) { | |
1541 | return ret; | |
1542 | } | |
1543 | stackshot_memcpy((void *)out_addr, ids->ids, sizeof(uint64_t) * ids->count); | |
1544 | ids->count = 0; | |
1545 | return ret; | |
1546 | } | |
1547 | ||
1548 | static kern_return_t | |
1549 | handle_nonrunnable_task(struct saved_uniqueids * ids, uint64_t pid) | |
1550 | { | |
1551 | kern_return_t ret = KERN_SUCCESS; | |
1552 | ids->ids[ids->count] = pid; | |
1553 | ids->count++; | |
1554 | assert(ids->count <= UNIQUEIDSPERFLUSH); | |
1555 | if (ids->count == UNIQUEIDSPERFLUSH) | |
1556 | ret = flush_nonrunnable_tasks(ids); | |
1557 | return ret; | |
1558 | } | |
1559 | ||
1560 | enum thread_classification { | |
1561 | tc_full_snapshot, /* take a full snapshot */ | |
1562 | tc_delta_snapshot, /* take a delta snapshot */ | |
1563 | tc_nonrunnable, /* only report id */ | |
1564 | }; | |
1565 | ||
1566 | static enum thread_classification | |
1567 | classify_thread(thread_t thread, boolean_t * thread_on_core_p, uint32_t trace_flags) | |
fe8ab488 | 1568 | { |
39037602 A |
1569 | boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0); |
1570 | boolean_t minimize_nonrunnables = ((trace_flags & STACKSHOT_TAILSPIN) != 0); | |
fe8ab488 | 1571 | |
39037602 A |
1572 | processor_t last_processor = thread->last_processor; |
1573 | ||
1574 | boolean_t thread_on_core = | |
1575 | (last_processor != PROCESSOR_NULL && last_processor->state == PROCESSOR_RUNNING && last_processor->active_thread == thread); | |
1576 | ||
1577 | *thread_on_core_p = thread_on_core; | |
1578 | ||
1579 | /* Capture the full thread snapshot if this is not a delta stackshot or if the thread has run subsequent to the | |
1580 | * previous full stackshot */ | |
1581 | if (!collect_delta_stackshot || thread_on_core || (thread->last_run_time > stack_snapshot_delta_since_timestamp)) { | |
1582 | return tc_full_snapshot; | |
1583 | } else { | |
1584 | if (minimize_nonrunnables && !(thread->state & TH_RUN)) { | |
1585 | return tc_nonrunnable; | |
1586 | } else { | |
1587 | return tc_delta_snapshot; | |
1588 | } | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | static kern_return_t | |
1593 | kdp_stackshot_kcdata_format(int pid, uint32_t trace_flags, uint32_t * pBytesTraced) | |
1594 | { | |
1595 | kern_return_t error = KERN_SUCCESS; | |
1596 | mach_vm_address_t out_addr = 0; | |
1597 | uint64_t abs_time = 0, abs_time_end = 0; | |
1598 | uint64_t *abs_time_addr = NULL; | |
1599 | uint64_t system_state_flags = 0; | |
1600 | int saved_count = 0; | |
fe8ab488 A |
1601 | task_t task = TASK_NULL; |
1602 | thread_t thread = THREAD_NULL; | |
39037602 A |
1603 | mach_timebase_info_data_t timebase = {0, 0}; |
1604 | uint32_t length_to_copy = 0, tmp32 = 0; | |
fe8ab488 | 1605 | |
39037602 A |
1606 | abs_time = mach_absolute_time(); |
1607 | ||
1608 | /* process the flags */ | |
1609 | boolean_t active_kthreads_only_p = ((trace_flags & STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY) != 0); | |
1610 | boolean_t save_donating_pids_p = ((trace_flags & STACKSHOT_SAVE_IMP_DONATION_PIDS) != 0); | |
1611 | boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0); | |
1612 | boolean_t minimize_nonrunnables = ((trace_flags & STACKSHOT_TAILSPIN) != 0); | |
1613 | boolean_t use_fault_path = ((trace_flags & (STACKSHOT_ENABLE_UUID_FAULTING | STACKSHOT_ENABLE_BT_FAULTING)) != 0); | |
813fb2f6 | 1614 | boolean_t save_owner_info = ((trace_flags & STACKSHOT_THREAD_WAITINFO) != 0); |
39037602 A |
1615 | stack_enable_faulting = (trace_flags & (STACKSHOT_ENABLE_BT_FAULTING)); |
1616 | ||
5ba3f43e A |
1617 | #if CONFIG_EMBEDDED |
1618 | /* KEXTs can't be described by just a base address on embedded */ | |
1619 | trace_flags &= ~(STACKSHOT_SAVE_KEXT_LOADINFO); | |
1620 | #endif | |
39037602 A |
1621 | |
1622 | struct saved_uniqueids saved_uniqueids = {.count = 0}; | |
1623 | ||
1624 | if (use_fault_path) { | |
1625 | fault_stats.sfs_pages_faulted_in = 0; | |
1626 | fault_stats.sfs_time_spent_faulting = 0; | |
1627 | fault_stats.sfs_stopped_faulting = (uint8_t) FALSE; | |
1628 | } | |
1629 | ||
1630 | if (sizeof(void *) == 8) | |
1631 | system_state_flags |= kKernel64_p; | |
1632 | ||
1633 | if (stackshot_kcdata_p == NULL || pBytesTraced == NULL) { | |
1634 | error = KERN_INVALID_ARGUMENT; | |
1635 | goto error_exit; | |
1636 | } | |
1637 | ||
1638 | /* setup mach_absolute_time and timebase info -- copy out in some cases and needed to convert since_timestamp to seconds for proc start time */ | |
1639 | clock_timebase_info(&timebase); | |
1640 | ||
1641 | /* begin saving data into the buffer */ | |
1642 | *pBytesTraced = 0; | |
1643 | kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, trace_flags, "stackshot_in_flags")); | |
1644 | kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, (uint32_t)pid, "stackshot_in_pid")); | |
1645 | kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, system_state_flags, "system_state_flags")); | |
1646 | ||
1647 | #if CONFIG_JETSAM | |
1648 | tmp32 = memorystatus_get_pressure_status_kdp(); | |
1649 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_JETSAM_LEVEL, sizeof(uint32_t), &out_addr)); | |
1650 | stackshot_memcpy((void *)out_addr, &tmp32, sizeof(tmp32)); | |
1651 | #endif | |
1652 | ||
1653 | if (!collect_delta_stackshot) { | |
5ba3f43e A |
1654 | tmp32 = THREAD_POLICY_INTERNAL_STRUCT_VERSION; |
1655 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_POLICY_VERSION, sizeof(uint32_t), &out_addr)); | |
1656 | stackshot_memcpy((void *)out_addr, &tmp32, sizeof(tmp32)); | |
1657 | ||
39037602 A |
1658 | tmp32 = PAGE_SIZE; |
1659 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_KERN_PAGE_SIZE, sizeof(uint32_t), &out_addr)); | |
1660 | stackshot_memcpy((void *)out_addr, &tmp32, sizeof(tmp32)); | |
1661 | ||
1662 | /* save boot-args and osversion string */ | |
1663 | length_to_copy = MIN((uint32_t)(strlen(version) + 1), OSVERSIZE); | |
1664 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_OSVERSION, length_to_copy, &out_addr)); | |
1665 | stackshot_strlcpy((char*)out_addr, &version[0], length_to_copy); | |
1666 | ||
1667 | length_to_copy = MIN((uint32_t)(strlen(PE_boot_args()) + 1), OSVERSIZE); | |
1668 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_BOOTARGS, length_to_copy, &out_addr)); | |
1669 | stackshot_strlcpy((char*)out_addr, PE_boot_args(), length_to_copy); | |
1670 | ||
1671 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, KCDATA_TYPE_TIMEBASE, sizeof(timebase), &out_addr)); | |
1672 | stackshot_memcpy((void *)out_addr, &timebase, sizeof(timebase)); | |
1673 | } else { | |
1674 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP, sizeof(uint64_t), &out_addr)); | |
1675 | stackshot_memcpy((void*)out_addr, &stack_snapshot_delta_since_timestamp, sizeof(stack_snapshot_delta_since_timestamp)); | |
1676 | } | |
1677 | ||
1678 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, KCDATA_TYPE_MACH_ABSOLUTE_TIME, sizeof(uint64_t), &out_addr)); | |
1679 | abs_time_addr = (uint64_t *)out_addr; | |
1680 | stackshot_memcpy((void *)abs_time_addr, &abs_time, sizeof(uint64_t)); | |
1681 | ||
1682 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, KCDATA_TYPE_USECS_SINCE_EPOCH, sizeof(uint64_t), &out_addr)); | |
1683 | stackshot_memcpy((void *)out_addr, &stackshot_microsecs, sizeof(uint64_t)); | |
1684 | ||
1685 | /* reserve space of system level shared cache load info */ | |
1686 | struct dyld_uuid_info_64_v2 * sys_shared_cache_loadinfo = NULL; | |
1687 | if (!collect_delta_stackshot) { | |
1688 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO, | |
1689 | sizeof(struct dyld_uuid_info_64_v2), &out_addr)); | |
1690 | sys_shared_cache_loadinfo = (struct dyld_uuid_info_64_v2 *)out_addr; | |
1691 | bzero((void *)sys_shared_cache_loadinfo, sizeof(struct dyld_uuid_info_64_v2)); | |
fe8ab488 | 1692 | } |
fe8ab488 | 1693 | |
39037602 A |
1694 | /* Add requested information first */ |
1695 | if (trace_flags & STACKSHOT_GET_GLOBAL_MEM_STATS) { | |
1696 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_GLOBAL_MEM_STATS, sizeof(struct mem_and_io_snapshot), &out_addr)); | |
1697 | kdp_mem_and_io_snapshot((struct mem_and_io_snapshot *)out_addr); | |
1698 | } | |
1699 | ||
5ba3f43e A |
1700 | #if CONFIG_COALITIONS |
1701 | int num_coalitions = 0; | |
1702 | struct jetsam_coalition_snapshot *coalitions = NULL; | |
1703 | /* Iterate over coalitions */ | |
1704 | if (trace_flags & STACKSHOT_SAVE_JETSAM_COALITIONS) { | |
1705 | if (coalition_iterate_stackshot(stackshot_coalition_jetsam_count, &num_coalitions, COALITION_TYPE_JETSAM) != KERN_SUCCESS) { | |
1706 | trace_flags &= ~(STACKSHOT_SAVE_JETSAM_COALITIONS); | |
1707 | } | |
1708 | } | |
1709 | if (trace_flags & STACKSHOT_SAVE_JETSAM_COALITIONS) { | |
1710 | if (num_coalitions > 0) { | |
1711 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT, sizeof(struct jetsam_coalition_snapshot), num_coalitions, &out_addr)); | |
1712 | coalitions = (struct jetsam_coalition_snapshot*)out_addr; | |
1713 | } | |
1714 | ||
1715 | if (coalition_iterate_stackshot(stackshot_coalition_jetsam_snapshot, coalitions, COALITION_TYPE_JETSAM) != KERN_SUCCESS) { | |
1716 | error = KERN_FAILURE; | |
1717 | goto error_exit; | |
1718 | } | |
1719 | ||
1720 | } | |
1721 | #else | |
1722 | trace_flags &= ~(STACKSHOT_SAVE_JETSAM_COALITIONS); | |
1723 | #endif /* CONFIG_COALITIONS */ | |
1724 | ||
1725 | trace_flags &= ~(STACKSHOT_THREAD_GROUP); | |
1726 | ||
39037602 A |
1727 | /* Iterate over tasks */ |
1728 | queue_head_t *task_list = &tasks; | |
fe8ab488 | 1729 | queue_iterate(task_list, task, task_t, tasks) { |
39037602 A |
1730 | int task_pid = 0; |
1731 | uint64_t task_uniqueid = 0; | |
1732 | int num_delta_thread_snapshots = 0; | |
1733 | int num_nonrunnable_threads = 0; | |
813fb2f6 | 1734 | int num_waitinfo_threads = 0; |
5ba3f43e | 1735 | |
39037602 A |
1736 | uint64_t task_start_abstime = 0; |
1737 | boolean_t task_delta_stackshot = FALSE; | |
1738 | boolean_t task64 = FALSE, have_map = FALSE, have_pmap = FALSE; | |
1739 | boolean_t some_thread_ran = FALSE; | |
813fb2f6 | 1740 | unaligned_u64 *task_snap_ss_flags = NULL; |
39037602 A |
1741 | |
1742 | if ((task == NULL) || !ml_validate_nofault((vm_offset_t)task, sizeof(struct task))) { | |
1743 | error = KERN_FAILURE; | |
fe8ab488 | 1744 | goto error_exit; |
39037602 A |
1745 | } |
1746 | ||
1747 | have_map = (task->map != NULL) && (ml_validate_nofault((vm_offset_t)(task->map), sizeof(struct _vm_map))); | |
1748 | have_pmap = have_map && (task->map->pmap != NULL) && (ml_validate_nofault((vm_offset_t)(task->map->pmap), sizeof(struct pmap))); | |
fe8ab488 | 1749 | |
39037602 A |
1750 | task_pid = pid_from_task(task); |
1751 | task_uniqueid = get_task_uniqueid(task); | |
1752 | task64 = task_has_64BitAddr(task); | |
fe8ab488 | 1753 | |
3e170ce0 A |
1754 | if (!task->active || task_is_a_corpse(task)) { |
1755 | /* | |
fe8ab488 A |
1756 | * Not interested in terminated tasks without threads, and |
1757 | * at the moment, stackshot can't handle a task without a name. | |
1758 | */ | |
1759 | if (queue_empty(&task->threads) || task_pid == -1) { | |
1760 | continue; | |
1761 | } | |
1762 | } | |
1763 | ||
39037602 A |
1764 | if (collect_delta_stackshot) { |
1765 | proc_starttime_kdp(task->bsd_info, NULL, NULL, &task_start_abstime); | |
1766 | } | |
1767 | ||
fe8ab488 A |
1768 | /* Trace everything, unless a process was specified */ |
1769 | if ((pid == -1) || (pid == task_pid)) { | |
39037602 A |
1770 | #if DEBUG || DEVELOPMENT |
1771 | /* we might want to call kcdata_undo_add_container_begin(), which is | |
1772 | * only safe if we call it after kcdata_add_container_marker() but | |
1773 | * before adding any other kcdata items. In development kernels, | |
1774 | * we'll remember where the buffer end was and confirm after calling | |
1775 | * kcdata_undo_add_container_begin() that it's in exactly the same | |
1776 | * place.*/ | |
1777 | mach_vm_address_t revert_addr = stackshot_kcdata_p->kcd_addr_end; | |
1778 | #endif | |
fe8ab488 | 1779 | |
39037602 A |
1780 | /* add task snapshot marker */ |
1781 | kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN, | |
1782 | STACKSHOT_KCCONTAINER_TASK, task_uniqueid)); | |
fe8ab488 | 1783 | |
39037602 A |
1784 | if (!collect_delta_stackshot || (task_start_abstime == 0) || |
1785 | (task_start_abstime > stack_snapshot_delta_since_timestamp)) { | |
1786 | kcd_exit_on_error(kcdata_record_task_snapshot(stackshot_kcdata_p, task, trace_flags, have_pmap, &task_snap_ss_flags)); | |
1787 | } else { | |
1788 | task_delta_stackshot = TRUE; | |
1789 | if (minimize_nonrunnables) { | |
1790 | // delay taking the task snapshot. If there are no runnable threads we'll skip it. | |
1791 | } else { | |
a39ff7e2 | 1792 | kcd_exit_on_error(kcdata_record_task_delta_snapshot(stackshot_kcdata_p, task, trace_flags, have_pmap, &task_snap_ss_flags)); |
fe8ab488 A |
1793 | } |
1794 | } | |
1795 | ||
39037602 A |
1796 | /* Iterate over task threads */ |
1797 | queue_iterate(&task->threads, thread, thread_t, task_threads) | |
1798 | { | |
1799 | uint64_t thread_uniqueid; | |
fe8ab488 | 1800 | |
39037602 A |
1801 | if ((thread == NULL) || !ml_validate_nofault((vm_offset_t)thread, sizeof(struct thread))) { |
1802 | error = KERN_FAILURE; | |
1803 | goto error_exit; | |
1804 | } | |
fe8ab488 | 1805 | |
39037602 A |
1806 | if (active_kthreads_only_p && thread->kernel_stack == 0) |
1807 | continue; | |
fe8ab488 | 1808 | |
39037602 | 1809 | thread_uniqueid = thread_tid(thread); |
fe8ab488 | 1810 | |
39037602 A |
1811 | boolean_t thread_on_core; |
1812 | enum thread_classification thread_classification = classify_thread(thread, &thread_on_core, trace_flags); | |
fe8ab488 | 1813 | |
39037602 A |
1814 | switch (thread_classification) { |
1815 | case tc_full_snapshot: | |
1816 | /* add thread marker */ | |
1817 | kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN, | |
1818 | STACKSHOT_KCCONTAINER_THREAD, thread_uniqueid)); | |
1819 | kcd_exit_on_error( | |
1820 | kcdata_record_thread_snapshot(stackshot_kcdata_p, thread, task, trace_flags, have_pmap, thread_on_core)); | |
fe8ab488 | 1821 | |
39037602 A |
1822 | /* mark end of thread snapshot data */ |
1823 | kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_END, | |
1824 | STACKSHOT_KCCONTAINER_THREAD, thread_uniqueid)); | |
fe8ab488 | 1825 | |
39037602 A |
1826 | some_thread_ran = TRUE; |
1827 | break; | |
fe8ab488 | 1828 | |
39037602 A |
1829 | case tc_delta_snapshot: |
1830 | num_delta_thread_snapshots++; | |
1831 | break; | |
fe8ab488 | 1832 | |
39037602 A |
1833 | case tc_nonrunnable: |
1834 | num_nonrunnable_threads++; | |
1835 | break; | |
fe8ab488 | 1836 | } |
813fb2f6 A |
1837 | |
1838 | /* We want to report owner information regardless of whether a thread | |
1839 | * has changed since the last delta, whether it's a normal stackshot, | |
1840 | * or whether it's nonrunnable */ | |
1841 | if (save_owner_info && stackshot_thread_has_valid_waitinfo(thread)) | |
1842 | num_waitinfo_threads++; | |
fe8ab488 | 1843 | } |
fe8ab488 | 1844 | |
39037602 A |
1845 | if (task_delta_stackshot && minimize_nonrunnables) { |
1846 | if (some_thread_ran || num_delta_thread_snapshots > 0) { | |
a39ff7e2 | 1847 | kcd_exit_on_error(kcdata_record_task_delta_snapshot(stackshot_kcdata_p, task, trace_flags, have_pmap, &task_snap_ss_flags)); |
39037602 A |
1848 | } else { |
1849 | kcd_exit_on_error(kcdata_undo_add_container_begin(stackshot_kcdata_p)); | |
fe8ab488 | 1850 | |
39037602 A |
1851 | #if DEBUG || DEVELOPMENT |
1852 | mach_vm_address_t undo_addr = stackshot_kcdata_p->kcd_addr_end; | |
1853 | if (revert_addr != undo_addr) { | |
1854 | panic("tried to revert a container begin but we already moved past it. revert=%p undo=%p", | |
1855 | (void *)revert_addr, (void *)undo_addr); | |
1856 | } | |
1857 | #endif | |
1858 | kcd_exit_on_error(handle_nonrunnable_task(&saved_uniqueids, task_uniqueid)); | |
1859 | continue; | |
fe8ab488 | 1860 | } |
39037602 | 1861 | } |
fe8ab488 | 1862 | |
a39ff7e2 | 1863 | struct thread_delta_snapshot_v3 * delta_snapshots = NULL; |
39037602 | 1864 | int current_delta_snapshot_index = 0; |
fe8ab488 | 1865 | |
39037602 A |
1866 | if (num_delta_thread_snapshots > 0) { |
1867 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT, | |
a39ff7e2 | 1868 | sizeof(struct thread_delta_snapshot_v3), |
39037602 | 1869 | num_delta_thread_snapshots, &out_addr)); |
a39ff7e2 | 1870 | delta_snapshots = (struct thread_delta_snapshot_v3 *)out_addr; |
fe8ab488 | 1871 | } |
3e170ce0 | 1872 | |
39037602 A |
1873 | uint64_t * nonrunnable_tids = NULL; |
1874 | int current_nonrunnable_index = 0; | |
1875 | ||
1876 | if (num_nonrunnable_threads > 0) { | |
1877 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_NONRUNNABLE_TIDS, | |
1878 | sizeof(uint64_t), num_nonrunnable_threads, &out_addr)); | |
1879 | nonrunnable_tids = (uint64_t *)out_addr; | |
fe8ab488 A |
1880 | } |
1881 | ||
813fb2f6 A |
1882 | thread_waitinfo_t *thread_waitinfo = NULL; |
1883 | int current_waitinfo_index = 0; | |
1884 | ||
1885 | if (num_waitinfo_threads > 0) { | |
1886 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_WAITINFO, | |
1887 | sizeof(thread_waitinfo_t), num_waitinfo_threads, &out_addr)); | |
1888 | thread_waitinfo = (thread_waitinfo_t *)out_addr; | |
1889 | } | |
1890 | ||
1891 | if (num_delta_thread_snapshots > 0 || num_nonrunnable_threads > 0 || num_waitinfo_threads > 0) { | |
39037602 A |
1892 | queue_iterate(&task->threads, thread, thread_t, task_threads) |
1893 | { | |
1894 | if (active_kthreads_only_p && thread->kernel_stack == 0) | |
1895 | continue; | |
fe8ab488 | 1896 | |
813fb2f6 A |
1897 | /* If we want owner info, we should capture it regardless of its classification */ |
1898 | if (save_owner_info && stackshot_thread_has_valid_waitinfo(thread)) { | |
1899 | stackshot_thread_wait_owner_info( | |
1900 | thread, | |
1901 | &thread_waitinfo[current_waitinfo_index++]); | |
1902 | } | |
1903 | ||
39037602 A |
1904 | boolean_t thread_on_core; |
1905 | enum thread_classification thread_classification = classify_thread(thread, &thread_on_core, trace_flags); | |
fe8ab488 | 1906 | |
39037602 A |
1907 | switch (thread_classification) { |
1908 | case tc_full_snapshot: | |
1909 | /* full thread snapshot captured above */ | |
1910 | continue; | |
fe8ab488 | 1911 | |
39037602 A |
1912 | case tc_delta_snapshot: |
1913 | kcd_exit_on_error(kcdata_record_thread_delta_snapshot(&delta_snapshots[current_delta_snapshot_index++], | |
1914 | thread, thread_on_core)); | |
1915 | break; | |
3e170ce0 | 1916 | |
39037602 A |
1917 | case tc_nonrunnable: |
1918 | nonrunnable_tids[current_nonrunnable_index++] = thread_tid(thread); | |
1919 | continue; | |
1920 | } | |
3e170ce0 A |
1921 | } |
1922 | ||
39037602 A |
1923 | #if DEBUG || DEVELOPMENT |
1924 | if (current_delta_snapshot_index != num_delta_thread_snapshots) { | |
1925 | panic("delta thread snapshot count mismatch while capturing snapshots for task %p. expected %d, found %d", task, | |
1926 | num_delta_thread_snapshots, current_delta_snapshot_index); | |
fe8ab488 | 1927 | } |
39037602 | 1928 | if (current_nonrunnable_index != num_nonrunnable_threads) { |
813fb2f6 | 1929 | panic("nonrunnable thread count mismatch while capturing snapshots for task %p. expected %d, found %d", task, |
39037602 | 1930 | num_nonrunnable_threads, current_nonrunnable_index); |
fe8ab488 | 1931 | } |
813fb2f6 A |
1932 | if (current_waitinfo_index != num_waitinfo_threads) { |
1933 | panic("thread wait info count mismatch while capturing snapshots for task %p. expected %d, found %d", task, | |
1934 | num_waitinfo_threads, current_waitinfo_index); | |
1935 | } | |
fe8ab488 | 1936 | #endif |
fe8ab488 A |
1937 | } |
1938 | ||
39037602 A |
1939 | #if IMPORTANCE_INHERITANCE |
1940 | if (save_donating_pids_p) { | |
1941 | kcd_exit_on_error( | |
1942 | ((((mach_vm_address_t)kcd_end_address(stackshot_kcdata_p) + (TASK_IMP_WALK_LIMIT * sizeof(int32_t))) < | |
1943 | (mach_vm_address_t)kcd_max_address(stackshot_kcdata_p)) | |
1944 | ? KERN_SUCCESS | |
1945 | : KERN_RESOURCE_SHORTAGE)); | |
1946 | saved_count = task_importance_list_pids(task, TASK_IMP_LIST_DONATING_PIDS, | |
1947 | (void *)kcd_end_address(stackshot_kcdata_p), TASK_IMP_WALK_LIMIT); | |
1948 | if (saved_count > 0) | |
1949 | kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_DONATING_PIDS, | |
1950 | sizeof(int32_t), saved_count, &out_addr)); | |
1951 | } | |
1952 | #endif | |
1953 | ||
1954 | if (!collect_delta_stackshot || (num_delta_thread_snapshots != task->thread_count) || !task_delta_stackshot) { | |
1955 | /* | |
1956 | * Collect shared cache info and UUID info in these scenarios | |
1957 | * 1) a full stackshot | |
1958 | * 2) a delta stackshot where the task started after the previous full stackshot OR | |
1959 | * any thread from the task has run since the previous full stackshot | |
1960 | */ | |
1961 | ||
5ba3f43e | 1962 | kcd_exit_on_error(kcdata_record_shared_cache_info(stackshot_kcdata_p, task, sys_shared_cache_loadinfo, task_snap_ss_flags)); |
39037602 A |
1963 | kcd_exit_on_error(kcdata_record_uuid_info(stackshot_kcdata_p, task, trace_flags, have_pmap, task_snap_ss_flags)); |
1964 | } | |
1965 | /* mark end of task snapshot data */ | |
1966 | kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_END, STACKSHOT_KCCONTAINER_TASK, | |
1967 | task_uniqueid)); | |
fe8ab488 A |
1968 | } |
1969 | } | |
1970 | ||
39037602 A |
1971 | if (minimize_nonrunnables) { |
1972 | flush_nonrunnable_tasks(&saved_uniqueids); | |
1973 | } | |
1974 | ||
1975 | if (use_fault_path) { | |
1976 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS, | |
1977 | sizeof(struct stackshot_fault_stats), &out_addr)); | |
1978 | stackshot_memcpy((void*)out_addr, &fault_stats, sizeof(struct stackshot_fault_stats)); | |
fe8ab488 A |
1979 | } |
1980 | ||
39037602 A |
1981 | /* update timestamp of the stackshot */ |
1982 | abs_time_end = mach_absolute_time(); | |
1983 | #if DEVELOPMENT || DEBUG | |
1984 | kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_STACKSHOT_DURATION, | |
1985 | sizeof(struct stackshot_duration), &out_addr)); | |
1986 | struct stackshot_duration * stackshot_duration = (struct stackshot_duration *)out_addr; | |
1987 | stackshot_duration->stackshot_duration = (abs_time_end - abs_time); | |
1988 | stackshot_duration->stackshot_duration_outer = 0; | |
813fb2f6 | 1989 | stackshot_duration_outer = (unaligned_u64 *)&stackshot_duration->stackshot_duration_outer; |
39037602 A |
1990 | #endif |
1991 | stackshot_memcpy((void *)abs_time_addr, &abs_time_end, sizeof(uint64_t)); | |
1992 | ||
5ba3f43e | 1993 | kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, trace_flags, "stackshot_out_flags")); |
39037602 A |
1994 | |
1995 | kcd_exit_on_error(kcdata_write_buffer_end(stackshot_kcdata_p)); | |
1996 | ||
1997 | /* === END of populating stackshot data === */ | |
1998 | ||
1999 | *pBytesTraced = (uint32_t) kcdata_memory_get_used_bytes(stackshot_kcdata_p); | |
fe8ab488 | 2000 | error_exit: |
fe8ab488 | 2001 | |
5ba3f43e A |
2002 | #if INTERRUPT_MASKED_DEBUG |
2003 | if (!panic_stackshot) { | |
2004 | /* | |
2005 | * Try to catch instances where stackshot takes too long BEFORE returning from | |
2006 | * the debugger | |
2007 | */ | |
2008 | ml_check_interrupts_disabled_duration(current_thread()); | |
2009 | } | |
2010 | #endif | |
2011 | ||
39037602 | 2012 | stack_enable_faulting = FALSE; |
fe8ab488 A |
2013 | |
2014 | return error; | |
2015 | } | |
2016 | ||
fe8ab488 A |
2017 | static uint64_t |
2018 | proc_was_throttled_from_task(task_t task) | |
2019 | { | |
2020 | uint64_t was_throttled = 0; | |
2021 | ||
2022 | if (task->bsd_info) | |
2023 | was_throttled = proc_was_throttled(task->bsd_info); | |
2024 | ||
2025 | return was_throttled; | |
2026 | } | |
2027 | ||
2028 | static uint64_t | |
2029 | proc_did_throttle_from_task(task_t task) | |
2030 | { | |
2031 | uint64_t did_throttle = 0; | |
2032 | ||
2033 | if (task->bsd_info) | |
2034 | did_throttle = proc_did_throttle(task->bsd_info); | |
2035 | ||
2036 | return did_throttle; | |
2037 | } | |
2038 | ||
2039 | static void | |
2040 | kdp_mem_and_io_snapshot(struct mem_and_io_snapshot *memio_snap) | |
2041 | { | |
2042 | unsigned int pages_reclaimed; | |
2043 | unsigned int pages_wanted; | |
2044 | kern_return_t kErr; | |
2045 | ||
2046 | processor_t processor; | |
2047 | vm_statistics64_t stat; | |
2048 | vm_statistics64_data_t host_vm_stat; | |
2049 | ||
2050 | processor = processor_list; | |
2051 | stat = &PROCESSOR_DATA(processor, vm_stat); | |
2052 | host_vm_stat = *stat; | |
2053 | ||
2054 | if (processor_count > 1) { | |
2055 | /* | |
2056 | * processor_list may be in the process of changing as we are | |
2057 | * attempting a stackshot. Ordinarily it will be lock protected, | |
2058 | * but it is not safe to lock in the context of the debugger. | |
2059 | * Fortunately we never remove elements from the processor list, | |
2060 | * and only add to to the end of the list, so we SHOULD be able | |
2061 | * to walk it. If we ever want to truly tear down processors, | |
2062 | * this will have to change. | |
2063 | */ | |
2064 | while ((processor = processor->processor_list) != NULL) { | |
2065 | stat = &PROCESSOR_DATA(processor, vm_stat); | |
2066 | host_vm_stat.compressions += stat->compressions; | |
2067 | host_vm_stat.decompressions += stat->decompressions; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | memio_snap->snapshot_magic = STACKSHOT_MEM_AND_IO_SNAPSHOT_MAGIC; | |
2072 | memio_snap->free_pages = vm_page_free_count; | |
2073 | memio_snap->active_pages = vm_page_active_count; | |
2074 | memio_snap->inactive_pages = vm_page_inactive_count; | |
2075 | memio_snap->purgeable_pages = vm_page_purgeable_count; | |
2076 | memio_snap->wired_pages = vm_page_wire_count; | |
2077 | memio_snap->speculative_pages = vm_page_speculative_count; | |
2078 | memio_snap->throttled_pages = vm_page_throttled_count; | |
2079 | memio_snap->busy_buffer_count = count_busy_buffers(); | |
2080 | memio_snap->filebacked_pages = vm_page_pageable_external_count; | |
2081 | memio_snap->compressions = (uint32_t)host_vm_stat.compressions; | |
2082 | memio_snap->decompressions = (uint32_t)host_vm_stat.decompressions; | |
2083 | memio_snap->compressor_size = VM_PAGE_COMPRESSOR_COUNT; | |
2084 | kErr = mach_vm_pressure_monitor(FALSE, VM_PRESSURE_TIME_WINDOW, &pages_reclaimed, &pages_wanted); | |
2085 | ||
2086 | if ( ! kErr ) { | |
2087 | memio_snap->pages_wanted = (uint32_t)pages_wanted; | |
2088 | memio_snap->pages_reclaimed = (uint32_t)pages_reclaimed; | |
2089 | memio_snap->pages_wanted_reclaimed_valid = 1; | |
2090 | } else { | |
2091 | memio_snap->pages_wanted = 0; | |
2092 | memio_snap->pages_reclaimed = 0; | |
2093 | memio_snap->pages_wanted_reclaimed_valid = 0; | |
2094 | } | |
2095 | } | |
2096 | ||
39037602 A |
2097 | void |
2098 | stackshot_memcpy(void *dst, const void *src, size_t len) | |
2099 | { | |
5ba3f43e A |
2100 | #if CONFIG_EMBEDDED |
2101 | if (panic_stackshot) { | |
2102 | uint8_t *dest_bytes = (uint8_t *)dst; | |
2103 | const uint8_t *src_bytes = (const uint8_t *)src; | |
2104 | for (size_t i = 0; i < len; i++) { | |
2105 | dest_bytes[i] = src_bytes[i]; | |
2106 | } | |
2107 | } else | |
2108 | #endif | |
39037602 A |
2109 | memcpy(dst, src, len); |
2110 | } | |
2111 | ||
2112 | size_t | |
2113 | stackshot_strlcpy(char *dst, const char *src, size_t maxlen) | |
2114 | { | |
2115 | const size_t srclen = strlen(src); | |
2116 | ||
2117 | if (srclen < maxlen) { | |
2118 | stackshot_memcpy(dst, src, srclen+1); | |
2119 | } else if (maxlen != 0) { | |
2120 | stackshot_memcpy(dst, src, maxlen-1); | |
2121 | dst[maxlen-1] = '\0'; | |
2122 | } | |
2123 | ||
2124 | return srclen; | |
2125 | } | |
2126 | ||
2127 | ||
2128 | /* | |
2129 | * Returns the physical address of the specified map:target address, | |
2130 | * using the kdp fault path if requested and the page is not resident. | |
2131 | */ | |
2132 | vm_offset_t | |
2133 | kdp_find_phys(vm_map_t map, vm_offset_t target_addr, boolean_t try_fault, uint32_t *kdp_fault_results) | |
2134 | { | |
2135 | vm_offset_t cur_phys_addr; | |
2136 | unsigned cur_wimg_bits; | |
2137 | uint64_t fault_start_time = 0; | |
2138 | ||
2139 | if (map == VM_MAP_NULL) { | |
2140 | return 0; | |
2141 | } | |
2142 | ||
2143 | cur_phys_addr = kdp_vtophys(map->pmap, target_addr); | |
2144 | if (!pmap_valid_page((ppnum_t) atop(cur_phys_addr))) { | |
2145 | if (!try_fault || fault_stats.sfs_stopped_faulting) { | |
2146 | if (kdp_fault_results) | |
2147 | *kdp_fault_results |= KDP_FAULT_RESULT_PAGED_OUT; | |
2148 | ||
2149 | return 0; | |
2150 | } | |
2151 | ||
2152 | /* | |
2153 | * The pmap doesn't have a valid page so we start at the top level | |
2154 | * vm map and try a lightweight fault. Update fault path usage stats. | |
2155 | */ | |
2156 | fault_start_time = mach_absolute_time(); | |
2157 | cur_phys_addr = kdp_lightweight_fault(map, (target_addr & ~PAGE_MASK)); | |
2158 | fault_stats.sfs_time_spent_faulting += (mach_absolute_time() - fault_start_time); | |
2159 | ||
2160 | if ((fault_stats.sfs_time_spent_faulting >= fault_stats.sfs_system_max_fault_time) && !panic_stackshot) { | |
2161 | fault_stats.sfs_stopped_faulting = (uint8_t) TRUE; | |
2162 | } | |
2163 | ||
2164 | cur_phys_addr += (target_addr & PAGE_MASK); | |
2165 | ||
2166 | if (!pmap_valid_page((ppnum_t) atop(cur_phys_addr))) { | |
2167 | if (kdp_fault_results) | |
2168 | *kdp_fault_results |= (KDP_FAULT_RESULT_TRIED_FAULT | KDP_FAULT_RESULT_PAGED_OUT); | |
2169 | ||
2170 | return 0; | |
2171 | } | |
2172 | ||
2173 | if (kdp_fault_results) | |
2174 | *kdp_fault_results |= KDP_FAULT_RESULT_FAULTED_IN; | |
2175 | ||
2176 | fault_stats.sfs_pages_faulted_in++; | |
2177 | } else { | |
2178 | /* | |
2179 | * This check is done in kdp_lightweight_fault for the fault path. | |
2180 | */ | |
2181 | cur_wimg_bits = pmap_cache_attributes((ppnum_t) atop(cur_phys_addr)); | |
2182 | ||
2183 | if ((cur_wimg_bits & VM_WIMG_MASK) != VM_WIMG_DEFAULT) { | |
2184 | return 0; | |
2185 | } | |
2186 | } | |
2187 | ||
2188 | return cur_phys_addr; | |
2189 | } | |
2190 | ||
fe8ab488 | 2191 | boolean_t |
39037602 A |
2192 | kdp_copyin_word( |
2193 | task_t task, uint64_t addr, uint64_t *result, boolean_t try_fault, uint32_t *kdp_fault_results) | |
2194 | { | |
2195 | if (task_has_64BitAddr(task)) { | |
2196 | return kdp_copyin(task->map, addr, result, sizeof(uint64_t), try_fault, kdp_fault_results); | |
2197 | } else { | |
2198 | uint32_t buf; | |
2199 | boolean_t r = kdp_copyin(task->map, addr, &buf, sizeof(uint32_t), try_fault, kdp_fault_results); | |
2200 | *result = buf; | |
2201 | return r; | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | boolean_t | |
2206 | kdp_copyin(vm_map_t map, uint64_t uaddr, void *dest, size_t size, boolean_t try_fault, uint32_t *kdp_fault_results) | |
fe8ab488 A |
2207 | { |
2208 | size_t rem = size; | |
2209 | char *kvaddr = dest; | |
2210 | ||
5ba3f43e | 2211 | #if CONFIG_EMBEDDED |
3e170ce0 | 2212 | /* Identify if destination buffer is in panic storage area */ |
5ba3f43e A |
2213 | if (panic_stackshot && ((vm_offset_t)dest >= gPanicBase) && ((vm_offset_t)dest < (gPanicBase + gPanicSize))) { |
2214 | if (((vm_offset_t)dest + size) > (gPanicBase + gPanicSize)) { | |
3e170ce0 A |
2215 | return FALSE; |
2216 | } | |
3e170ce0 A |
2217 | } |
2218 | #endif | |
2219 | ||
fe8ab488 | 2220 | while (rem) { |
39037602 | 2221 | uint64_t phys_src = kdp_find_phys(map, uaddr, try_fault, kdp_fault_results); |
fe8ab488 A |
2222 | uint64_t phys_dest = kvtophys((vm_offset_t)kvaddr); |
2223 | uint64_t src_rem = PAGE_SIZE - (phys_src & PAGE_MASK); | |
2224 | uint64_t dst_rem = PAGE_SIZE - (phys_dest & PAGE_MASK); | |
2225 | size_t cur_size = (uint32_t) MIN(src_rem, dst_rem); | |
2226 | cur_size = MIN(cur_size, rem); | |
2227 | ||
39037602 | 2228 | if (phys_src && phys_dest) { |
5ba3f43e A |
2229 | #if CONFIG_EMBEDDED |
2230 | /* | |
2231 | * On embedded the panic buffer is mapped as device memory and doesn't allow | |
2232 | * unaligned accesses. To prevent these, we copy over bytes individually here. | |
2233 | */ | |
2234 | if (panic_stackshot) | |
2235 | stackshot_memcpy(kvaddr, (const void *)phystokv(phys_src), cur_size); | |
2236 | else | |
2237 | #endif /* CONFIG_EMBEDDED */ | |
39037602 A |
2238 | bcopy_phys(phys_src, phys_dest, cur_size); |
2239 | } else { | |
fe8ab488 | 2240 | break; |
39037602 A |
2241 | } |
2242 | ||
fe8ab488 A |
2243 | uaddr += cur_size; |
2244 | kvaddr += cur_size; | |
3e170ce0 | 2245 | rem -= cur_size; |
fe8ab488 | 2246 | } |
39037602 | 2247 | |
fe8ab488 A |
2248 | return (rem == 0); |
2249 | } | |
2250 | ||
39037602 A |
2251 | kern_return_t |
2252 | do_stackshot(void *context) | |
fe8ab488 | 2253 | { |
39037602 A |
2254 | #pragma unused(context) |
2255 | kdp_snapshot++; | |
2256 | ||
2257 | stack_snapshot_ret = kdp_stackshot_kcdata_format(stack_snapshot_pid, | |
3e170ce0 A |
2258 | stack_snapshot_flags, |
2259 | &stack_snapshot_bytes_traced); | |
39037602 A |
2260 | |
2261 | kdp_snapshot--; | |
2262 | return stack_snapshot_ret; | |
fe8ab488 A |
2263 | } |
2264 | ||
2265 | /* | |
2266 | * A fantastical routine that tries to be fast about returning | |
2267 | * translations. Caches the last page we found a translation | |
2268 | * for, so that we can be quick about multiple queries to the | |
2269 | * same page. It turns out this is exactly the workflow | |
2270 | * machine_trace_thread and its relatives tend to throw at us. | |
2271 | * | |
2272 | * Please zero the nasty global this uses after a bulk lookup; | |
39037602 | 2273 | * this isn't safe across a switch of the map or changes |
fe8ab488 A |
2274 | * to a pmap. |
2275 | * | |
2276 | * This also means that if zero is a valid KVA, we are | |
2277 | * screwed. Sucks to be us. Fortunately, this should never | |
2278 | * happen. | |
2279 | */ | |
2280 | vm_offset_t | |
3e170ce0 | 2281 | machine_trace_thread_get_kva(vm_offset_t cur_target_addr, vm_map_t map, uint32_t *thread_trace_flags) |
fe8ab488 | 2282 | { |
fe8ab488 A |
2283 | vm_offset_t cur_target_page; |
2284 | vm_offset_t cur_phys_addr; | |
2285 | vm_offset_t kern_virt_target_addr; | |
39037602 | 2286 | uint32_t kdp_fault_results = 0; |
fe8ab488 A |
2287 | |
2288 | cur_target_page = atop(cur_target_addr); | |
2289 | ||
2290 | if ((cur_target_page != prev_target_page) || validate_next_addr) { | |
39037602 | 2291 | |
fe8ab488 A |
2292 | /* |
2293 | * Alright; it wasn't our previous page. So | |
2294 | * we must validate that there is a page | |
2295 | * table entry for this address under the | |
39037602 | 2296 | * current pmap, and that it has default |
fe8ab488 A |
2297 | * cache attributes (otherwise it may not be |
2298 | * safe to access it). | |
2299 | */ | |
39037602 A |
2300 | cur_phys_addr = kdp_find_phys(map, cur_target_addr, stack_enable_faulting, &kdp_fault_results); |
2301 | if (thread_trace_flags) { | |
2302 | if (kdp_fault_results & KDP_FAULT_RESULT_PAGED_OUT) { | |
2303 | *thread_trace_flags |= kThreadTruncatedBT; | |
3e170ce0 | 2304 | } |
fe8ab488 | 2305 | |
39037602 A |
2306 | if (kdp_fault_results & KDP_FAULT_RESULT_TRIED_FAULT) { |
2307 | *thread_trace_flags |= kThreadTriedFaultBT; | |
2308 | } | |
3e170ce0 | 2309 | |
39037602 A |
2310 | if (kdp_fault_results & KDP_FAULT_RESULT_FAULTED_IN) { |
2311 | *thread_trace_flags |= kThreadFaultedBT; | |
3e170ce0 | 2312 | } |
fe8ab488 A |
2313 | } |
2314 | ||
39037602 A |
2315 | if (cur_phys_addr == 0) { |
2316 | return 0; | |
2317 | } | |
fe8ab488 A |
2318 | #if __x86_64__ |
2319 | kern_virt_target_addr = (vm_offset_t) PHYSMAP_PTOV(cur_phys_addr); | |
5ba3f43e A |
2320 | #elif __arm__ || __arm64__ |
2321 | kern_virt_target_addr = phystokv(cur_phys_addr); | |
fe8ab488 A |
2322 | #else |
2323 | #error Oh come on... we should really unify the physical -> kernel virtual interface | |
2324 | #endif | |
2325 | prev_target_page = cur_target_page; | |
2326 | prev_target_kva = (kern_virt_target_addr & ~PAGE_MASK); | |
2327 | validate_next_addr = FALSE; | |
fe8ab488 A |
2328 | } else { |
2329 | /* We found a translation, so stash this page */ | |
2330 | kern_virt_target_addr = prev_target_kva + (cur_target_addr & PAGE_MASK); | |
fe8ab488 | 2331 | } |
5ba3f43e A |
2332 | |
2333 | #if KASAN | |
2334 | kasan_notify_address(kern_virt_target_addr, sizeof(uint64_t)); | |
2335 | #endif | |
2336 | return kern_virt_target_addr; | |
fe8ab488 A |
2337 | } |
2338 | ||
2339 | void | |
2340 | machine_trace_thread_clear_validation_cache(void) | |
2341 | { | |
2342 | validate_next_addr = TRUE; | |
2343 | } | |
2344 | ||
39037602 A |
2345 | boolean_t |
2346 | stackshot_thread_is_idle_worker_unsafe(thread_t thread) | |
2347 | { | |
813fb2f6 A |
2348 | /* When the pthread kext puts a worker thread to sleep, it will |
2349 | * set kThreadWaitParkedWorkQueue in the block_hint of the thread | |
2350 | * struct. See parkit() in kern/kern_support.c in libpthread. | |
39037602 | 2351 | */ |
813fb2f6 A |
2352 | return (thread->state & TH_WAIT) && |
2353 | (thread->block_hint == kThreadWaitParkedWorkQueue); | |
2354 | } | |
2355 | ||
5ba3f43e A |
2356 | #if CONFIG_COALITIONS |
2357 | static void | |
2358 | stackshot_coalition_jetsam_count(void *arg, int i, coalition_t coal) | |
2359 | { | |
2360 | #pragma unused(i, coal) | |
2361 | unsigned int *coalition_count = (unsigned int*)arg; | |
2362 | (*coalition_count)++; | |
2363 | } | |
2364 | ||
2365 | static void | |
2366 | stackshot_coalition_jetsam_snapshot(void *arg, int i, coalition_t coal) | |
2367 | { | |
2368 | if (coalition_type(coal) != COALITION_TYPE_JETSAM) | |
2369 | return; | |
2370 | ||
2371 | struct jetsam_coalition_snapshot *coalitions = (struct jetsam_coalition_snapshot*)arg; | |
2372 | struct jetsam_coalition_snapshot *jcs = &coalitions[i]; | |
2373 | task_t leader = TASK_NULL; | |
2374 | jcs->jcs_id = coalition_id(coal); | |
2375 | jcs->jcs_flags = 0; | |
2376 | ||
2377 | if (coalition_term_requested(coal)) | |
2378 | jcs->jcs_flags |= kCoalitionTermRequested; | |
2379 | if (coalition_is_terminated(coal)) | |
2380 | jcs->jcs_flags |= kCoalitionTerminated; | |
2381 | if (coalition_is_reaped(coal)) | |
2382 | jcs->jcs_flags |= kCoalitionReaped; | |
2383 | if (coalition_is_privileged(coal)) | |
2384 | jcs->jcs_flags |= kCoalitionPrivileged; | |
2385 | ||
2386 | ||
2387 | leader = kdp_coalition_get_leader(coal); | |
2388 | if (leader) | |
2389 | jcs->jcs_leader_task_uniqueid = get_task_uniqueid(leader); | |
2390 | else | |
2391 | jcs->jcs_leader_task_uniqueid = 0; | |
2392 | } | |
2393 | #endif /* CONFIG_COALITIONS */ | |
2394 | ||
2395 | ||
813fb2f6 A |
2396 | /* Determine if a thread has waitinfo that stackshot can provide */ |
2397 | static int | |
2398 | stackshot_thread_has_valid_waitinfo(thread_t thread) | |
2399 | { | |
2400 | if (!(thread->state & TH_WAIT)) | |
2401 | return 0; | |
2402 | ||
2403 | switch (thread->block_hint) { | |
2404 | // If set to None or is a parked work queue, ignore it | |
2405 | case kThreadWaitParkedWorkQueue: | |
2406 | case kThreadWaitNone: | |
2407 | return 0; | |
2408 | // There is a short window where the pthread kext removes a thread | |
2409 | // from its ksyn wait queue before waking the thread up | |
2410 | case kThreadWaitPThreadMutex: | |
2411 | case kThreadWaitPThreadRWLockRead: | |
2412 | case kThreadWaitPThreadRWLockWrite: | |
2413 | case kThreadWaitPThreadCondVar: | |
2414 | return (kdp_pthread_get_thread_kwq(thread) != NULL); | |
2415 | // All other cases are valid block hints if in a wait state | |
2416 | default: | |
2417 | return 1; | |
2418 | } | |
2419 | } | |
2420 | ||
2421 | static void | |
2422 | stackshot_thread_wait_owner_info(thread_t thread, thread_waitinfo_t *waitinfo) | |
2423 | { | |
2424 | waitinfo->waiter = thread_tid(thread); | |
2425 | waitinfo->wait_type = thread->block_hint; | |
2426 | switch (waitinfo->wait_type) { | |
2427 | case kThreadWaitKernelMutex: | |
2428 | kdp_lck_mtx_find_owner(thread->waitq, thread->wait_event, waitinfo); | |
2429 | break; | |
2430 | case kThreadWaitPortReceive: | |
2431 | kdp_mqueue_recv_find_owner(thread->waitq, thread->wait_event, waitinfo); | |
2432 | break; | |
2433 | case kThreadWaitPortSend: | |
2434 | kdp_mqueue_send_find_owner(thread->waitq, thread->wait_event, waitinfo); | |
2435 | break; | |
2436 | case kThreadWaitSemaphore: | |
2437 | kdp_sema_find_owner(thread->waitq, thread->wait_event, waitinfo); | |
2438 | break; | |
2439 | case kThreadWaitUserLock: | |
2440 | kdp_ulock_find_owner(thread->waitq, thread->wait_event, waitinfo); | |
2441 | break; | |
2442 | case kThreadWaitKernelRWLockRead: | |
2443 | case kThreadWaitKernelRWLockWrite: | |
2444 | case kThreadWaitKernelRWLockUpgrade: | |
2445 | kdp_rwlck_find_owner(thread->waitq, thread->wait_event, waitinfo); | |
2446 | break; | |
2447 | case kThreadWaitPThreadMutex: | |
2448 | case kThreadWaitPThreadRWLockRead: | |
2449 | case kThreadWaitPThreadRWLockWrite: | |
2450 | case kThreadWaitPThreadCondVar: | |
2451 | kdp_pthread_find_owner(thread, waitinfo); | |
2452 | break; | |
5ba3f43e A |
2453 | case kThreadWaitWorkloopSyncWait: |
2454 | kdp_workloop_sync_wait_find_owner(thread, thread->wait_event, waitinfo); | |
2455 | break; | |
813fb2f6 A |
2456 | default: |
2457 | waitinfo->owner = 0; | |
2458 | waitinfo->context = 0; | |
2459 | break; | |
2460 | } | |
39037602 | 2461 | } |
5ba3f43e | 2462 |