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