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