xref: /xnu-10002.61.3/osfmk/kern/kern_stackshot.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 2013-2020 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <mach/mach_types.h>
30 #include <mach/vm_param.h>
31 #include <mach/mach_vm.h>
32 #include <mach/clock_types.h>
33 #include <sys/code_signing.h>
34 #include <sys/errno.h>
35 #include <sys/stackshot.h>
36 #ifdef IMPORTANCE_INHERITANCE
37 #include <ipc/ipc_importance.h>
38 #endif
39 #include <sys/appleapiopts.h>
40 #include <kern/debug.h>
41 #include <kern/block_hint.h>
42 #include <uuid/uuid.h>
43 
44 #include <kdp/kdp_dyld.h>
45 #include <kdp/kdp_en_debugger.h>
46 #include <kdp/processor_core.h>
47 #include <kdp/kdp_common.h>
48 
49 #include <libsa/types.h>
50 #include <libkern/version.h>
51 #include <libkern/section_keywords.h>
52 
53 #include <string.h> /* bcopy */
54 
55 #include <kern/backtrace.h>
56 #include <kern/coalition.h>
57 #include <kern/processor.h>
58 #include <kern/host_statistics.h>
59 #include <kern/counter.h>
60 #include <kern/thread.h>
61 #include <kern/thread_group.h>
62 #include <kern/task.h>
63 #include <kern/telemetry.h>
64 #include <kern/clock.h>
65 #include <kern/policy_internal.h>
66 #include <kern/socd_client.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_pageout.h>
70 #include <vm/vm_fault.h>
71 #include <vm/vm_shared_region.h>
72 #include <vm/vm_compressor.h>
73 #include <libkern/OSKextLibPrivate.h>
74 #include <os/log.h>
75 
76 #if defined(__x86_64__)
77 #include <i386/mp.h>
78 #include <i386/cpu_threads.h>
79 #endif
80 
81 #include <pexpert/pexpert.h>
82 
83 #if CONFIG_PERVASIVE_CPI
84 #include <kern/monotonic.h>
85 #endif /* CONFIG_PERVASIVE_CPI */
86 
87 #include <san/kasan.h>
88 
89 #if DEBUG || DEVELOPMENT
90 # define STACKSHOT_COLLECTS_LATENCY_INFO 1
91 #else
92 # define STACKSHOT_COLLECTS_LATENCY_INFO 0
93 #endif /* DEBUG || DEVELOPMENT */
94 
95 extern unsigned int not_in_kdp;
96 
97 /* indicate to the compiler that some accesses are unaligned */
98 typedef uint64_t unaligned_u64 __attribute__((aligned(1)));
99 
100 int kdp_snapshot                            = 0;
101 static kern_return_t stack_snapshot_ret     = 0;
102 static uint32_t stack_snapshot_bytes_traced = 0;
103 static uint32_t stack_snapshot_bytes_uncompressed  = 0;
104 
105 #if STACKSHOT_COLLECTS_LATENCY_INFO
106 static bool collect_latency_info = true;
107 #endif
108 static kcdata_descriptor_t stackshot_kcdata_p = NULL;
109 static void *stack_snapshot_buf;
110 static uint32_t stack_snapshot_bufsize;
111 int stack_snapshot_pid;
112 static uint64_t stack_snapshot_flags;
113 static uint64_t stack_snapshot_delta_since_timestamp;
114 static uint32_t stack_snapshot_pagetable_mask;
115 static boolean_t panic_stackshot;
116 
117 static boolean_t stack_enable_faulting = FALSE;
118 static struct stackshot_fault_stats fault_stats;
119 
120 static uint64_t stackshot_last_abs_start;       /* start time of last stackshot */
121 static uint64_t stackshot_last_abs_end;         /* end time of last stackshot */
122 static uint64_t stackshots_taken;               /* total stackshots taken since boot */
123 static uint64_t stackshots_duration;            /* total abs time spent in stackshot_trap() since boot */
124 
125 /*
126  * Experimentally, our current estimates are 40% short 77% of the time; adding
127  * 75% to the estimate gets us into 99%+ territory.  In the longer run, we need
128  * to make stackshot estimates use a better approach (rdar://78880038); this is
129  * intended to be a short-term fix.
130  */
131 uint32_t stackshot_estimate_adj = 75; /* experiment factor: 0-100, adjust our estimate up by this amount */
132 
133 static uint32_t stackshot_initial_estimate;
134 static uint32_t stackshot_initial_estimate_adj;
135 static uint64_t stackshot_duration_prior_abs;   /* prior attempts, abs */
136 static unaligned_u64 * stackshot_duration_outer;
137 static uint64_t stackshot_microsecs;
138 
139 void * kernel_stackshot_buf   = NULL; /* Pointer to buffer for stackshots triggered from the kernel and retrieved later */
140 int kernel_stackshot_buf_size = 0;
141 
142 void * stackshot_snapbuf = NULL; /* Used by stack_snapshot2 (to be removed) */
143 
144 __private_extern__ void stackshot_init( void );
145 static boolean_t memory_iszero(void *addr, size_t size);
146 uint32_t                get_stackshot_estsize(uint32_t prev_size_hint, uint32_t adj);
147 kern_return_t           kern_stack_snapshot_internal(int stackshot_config_version, void *stackshot_config,
148     size_t stackshot_config_size, boolean_t stackshot_from_user);
149 kern_return_t           do_stackshot(void *);
150 void                    kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size, uint64_t flags, kcdata_descriptor_t data_p, uint64_t since_timestamp, uint32_t pagetable_mask);
151 boolean_t               stackshot_thread_is_idle_worker_unsafe(thread_t thread);
152 static int              kdp_stackshot_kcdata_format(int pid, uint64_t trace_flags, uint32_t *pBytesTraced, uint32_t *pBytesUncompressed);
153 uint32_t                kdp_stack_snapshot_bytes_traced(void);
154 uint32_t                kdp_stack_snapshot_bytes_uncompressed(void);
155 static void             kdp_mem_and_io_snapshot(struct mem_and_io_snapshot *memio_snap);
156 static vm_offset_t      stackshot_find_phys(vm_map_t map, vm_offset_t target_addr, kdp_fault_flags_t fault_flags, uint32_t *kdp_fault_result_flags);
157 static boolean_t        stackshot_copyin(vm_map_t map, uint64_t uaddr, void *dest, size_t size, boolean_t try_fault, uint32_t *kdp_fault_result);
158 static int              stackshot_copyin_string(task_t task, uint64_t addr, char *buf, int buf_sz, boolean_t try_fault, uint32_t *kdp_fault_results);
159 static boolean_t        stackshot_copyin_word(task_t task, uint64_t addr, uint64_t *result, boolean_t try_fault, uint32_t *kdp_fault_results);
160 static uint64_t         proc_was_throttled_from_task(task_t task);
161 static void             stackshot_thread_wait_owner_info(thread_t thread, thread_waitinfo_v2_t * waitinfo);
162 static int              stackshot_thread_has_valid_waitinfo(thread_t thread);
163 static void             stackshot_thread_turnstileinfo(thread_t thread, thread_turnstileinfo_v2_t *tsinfo);
164 static int              stackshot_thread_has_valid_turnstileinfo(thread_t thread);
165 
166 #if CONFIG_COALITIONS
167 static void             stackshot_coalition_jetsam_count(void *arg, int i, coalition_t coal);
168 static void             stackshot_coalition_jetsam_snapshot(void *arg, int i, coalition_t coal);
169 #endif /* CONFIG_COALITIONS */
170 
171 #if CONFIG_THREAD_GROUPS
172 static void             stackshot_thread_group_count(void *arg, int i, struct thread_group *tg);
173 static void             stackshot_thread_group_snapshot(void *arg, int i, struct thread_group *tg);
174 #endif /* CONFIG_THREAD_GROUPS */
175 
176 extern uint32_t         workqueue_get_pwq_state_kdp(void *proc);
177 
178 struct proc;
179 extern int              proc_pid(struct proc *p);
180 extern uint64_t         proc_uniqueid(void *p);
181 extern uint64_t         proc_was_throttled(void *p);
182 extern uint64_t         proc_did_throttle(void *p);
183 extern int              proc_exiting(void *p);
184 extern int              proc_in_teardown(void *p);
185 static uint64_t         proc_did_throttle_from_task(task_t task);
186 extern void             proc_name_kdp(struct proc *p, char * buf, int size);
187 extern int              proc_threadname_kdp(void * uth, char * buf, size_t size);
188 extern void             proc_starttime_kdp(void * p, uint64_t * tv_sec, uint64_t * tv_usec, uint64_t * abstime);
189 extern void             proc_archinfo_kdp(void* p, cpu_type_t* cputype, cpu_subtype_t* cpusubtype);
190 extern uint64_t         proc_getcsflags_kdp(void * p);
191 extern boolean_t        proc_binary_uuid_kdp(task_t task, uuid_t uuid);
192 extern int              memorystatus_get_pressure_status_kdp(void);
193 extern void             memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit);
194 
195 extern int count_busy_buffers(void); /* must track with declaration in bsd/sys/buf_internal.h */
196 
197 #if CONFIG_TELEMETRY
198 extern kern_return_t stack_microstackshot(user_addr_t tracebuf, uint32_t tracebuf_size, uint32_t flags, int32_t *retval);
199 #endif /* CONFIG_TELEMETRY */
200 
201 extern kern_return_t kern_stack_snapshot_with_reason(char* reason);
202 extern kern_return_t kern_stack_snapshot_internal(int stackshot_config_version, void *stackshot_config, size_t stackshot_config_size, boolean_t stackshot_from_user);
203 
204 static size_t stackshot_plh_est_size(void);
205 
206 /*
207  * Validates that the given address for a word is both a valid page and has
208  * default caching attributes for the current map.
209  */
210 bool machine_trace_thread_validate_kva(vm_offset_t);
211 /*
212  * Validates a region that stackshot will potentially inspect.
213  */
214 static bool _stackshot_validate_kva(vm_offset_t, size_t);
215 /*
216  * Must be called whenever stackshot is re-driven.
217  */
218 static void _stackshot_validation_reset(void);
219 /*
220  * A kdp-safe strlen() call.  Returns:
221  *      -1 if we reach maxlen or a bad address before the end of the string, or
222  *      strlen(s)
223  */
224 static long _stackshot_strlen(const char *s, size_t maxlen);
225 
226 #define MAX_FRAMES 1000
227 #define MAX_LOADINFOS 500
228 #define MAX_DYLD_COMPACTINFO (20 * 1024)  // max bytes of compactinfo to include per proc/shared region
229 #define TASK_IMP_WALK_LIMIT 20
230 
231 typedef struct thread_snapshot *thread_snapshot_t;
232 typedef struct task_snapshot *task_snapshot_t;
233 
234 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
235 extern kdp_send_t    kdp_en_send_pkt;
236 #endif
237 
238 /*
239  * Stackshot locking and other defines.
240  */
241 static LCK_GRP_DECLARE(stackshot_subsys_lck_grp, "stackshot_subsys_lock");
242 static LCK_MTX_DECLARE(stackshot_subsys_mutex, &stackshot_subsys_lck_grp);
243 
244 #define STACKSHOT_SUBSYS_LOCK() lck_mtx_lock(&stackshot_subsys_mutex)
245 #define STACKSHOT_SUBSYS_TRY_LOCK() lck_mtx_try_lock(&stackshot_subsys_mutex)
246 #define STACKSHOT_SUBSYS_UNLOCK() lck_mtx_unlock(&stackshot_subsys_mutex)
247 
248 #define SANE_BOOTPROFILE_TRACEBUF_SIZE (64ULL * 1024ULL * 1024ULL)
249 #define SANE_TRACEBUF_SIZE (8ULL * 1024ULL * 1024ULL)
250 
251 #define TRACEBUF_SIZE_PER_GB (1024ULL * 1024ULL)
252 #define GIGABYTES (1024ULL * 1024ULL * 1024ULL)
253 
254 SECURITY_READ_ONLY_LATE(static uint32_t) max_tracebuf_size = SANE_TRACEBUF_SIZE;
255 
256 /*
257  * We currently set a ceiling of 3 milliseconds spent in the kdp fault path
258  * for non-panic stackshots where faulting is requested.
259  */
260 #define KDP_FAULT_PATH_MAX_TIME_PER_STACKSHOT_NSECS (3 * NSEC_PER_MSEC)
261 
262 #define STACKSHOT_SUPP_SIZE (16 * 1024) /* Minimum stackshot size */
263 #define TASK_UUID_AVG_SIZE (16 * sizeof(uuid_t)) /* Average space consumed by UUIDs/task */
264 
265 #ifndef ROUNDUP
266 #define ROUNDUP(x, y)            ((((x)+(y)-1)/(y))*(y))
267 #endif
268 
269 #define STACKSHOT_QUEUE_LABEL_MAXSIZE  64
270 
271 /*
272  * Initialize the mutex governing access to the stack snapshot subsystem
273  * and other stackshot related bits.
274  */
275 __private_extern__ void
stackshot_init(void)276 stackshot_init( void )
277 {
278 	mach_timebase_info_data_t timebase;
279 
280 	clock_timebase_info(&timebase);
281 	fault_stats.sfs_system_max_fault_time = ((KDP_FAULT_PATH_MAX_TIME_PER_STACKSHOT_NSECS * timebase.denom) / timebase.numer);
282 
283 	max_tracebuf_size = MAX(max_tracebuf_size, ((ROUNDUP(max_mem, GIGABYTES) / GIGABYTES) * TRACEBUF_SIZE_PER_GB));
284 
285 	PE_parse_boot_argn("stackshot_maxsz", &max_tracebuf_size, sizeof(max_tracebuf_size));
286 }
287 
288 /*
289  * Called with interrupts disabled after stackshot context has been
290  * initialized. Updates stack_snapshot_ret.
291  */
292 static kern_return_t
stackshot_trap(void)293 stackshot_trap(void)
294 {
295 	kern_return_t   rv;
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 	stackshot_last_abs_start = mach_absolute_time();
322 	stackshot_last_abs_end = 0;
323 
324 	rv = DebuggerTrapWithState(DBOP_STACKSHOT, NULL, NULL, NULL, 0, NULL, FALSE, 0);
325 
326 	stackshot_last_abs_end = mach_absolute_time();
327 	stackshots_taken++;
328 	stackshots_duration += (stackshot_last_abs_end - stackshot_last_abs_start);
329 
330 #if defined(__x86_64__)
331 	mp_rendezvous_unlock();
332 #endif
333 	return rv;
334 }
335 
336 extern void stackshot_get_timing(uint64_t *last_abs_start, uint64_t *last_abs_end, uint64_t *count, uint64_t *total_duration);
337 void
stackshot_get_timing(uint64_t * last_abs_start,uint64_t * last_abs_end,uint64_t * count,uint64_t * total_duration)338 stackshot_get_timing(uint64_t *last_abs_start, uint64_t *last_abs_end, uint64_t *count, uint64_t *total_duration)
339 {
340 	STACKSHOT_SUBSYS_LOCK();
341 	*last_abs_start = stackshot_last_abs_start;
342 	*last_abs_end = stackshot_last_abs_end;
343 	*count = stackshots_taken;
344 	*total_duration = stackshots_duration;
345 	STACKSHOT_SUBSYS_UNLOCK();
346 }
347 
348 kern_return_t
stack_snapshot_from_kernel(int pid,void * buf,uint32_t size,uint64_t flags,uint64_t delta_since_timestamp,uint32_t pagetable_mask,unsigned * bytes_traced)349 stack_snapshot_from_kernel(int pid, void *buf, uint32_t size, uint64_t flags, uint64_t delta_since_timestamp, uint32_t pagetable_mask, unsigned *bytes_traced)
350 {
351 	kern_return_t error = KERN_SUCCESS;
352 	boolean_t istate;
353 
354 #if DEVELOPMENT || DEBUG
355 	if (kern_feature_override(KF_STACKSHOT_OVRD) == TRUE) {
356 		return KERN_NOT_SUPPORTED;
357 	}
358 #endif
359 	if ((buf == NULL) || (size <= 0) || (bytes_traced == NULL)) {
360 		return KERN_INVALID_ARGUMENT;
361 	}
362 
363 	/* cap in individual stackshot to max_tracebuf_size */
364 	if (size > max_tracebuf_size) {
365 		size = max_tracebuf_size;
366 	}
367 
368 	/* Serialize tracing */
369 	if (flags & STACKSHOT_TRYLOCK) {
370 		if (!STACKSHOT_SUBSYS_TRY_LOCK()) {
371 			return KERN_LOCK_OWNED;
372 		}
373 	} else {
374 		STACKSHOT_SUBSYS_LOCK();
375 	}
376 
377 	struct kcdata_descriptor kcdata;
378 	uint32_t hdr_tag = (flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) ?
379 	    KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT : KCDATA_BUFFER_BEGIN_STACKSHOT;
380 
381 	error = kcdata_memory_static_init(&kcdata, (mach_vm_address_t)buf, hdr_tag, size,
382 	    KCFLAG_USE_MEMCOPY | KCFLAG_NO_AUTO_ENDBUFFER);
383 	if (error) {
384 		goto out;
385 	}
386 
387 	stackshot_initial_estimate = 0;
388 	stackshot_duration_prior_abs = 0;
389 	stackshot_duration_outer = NULL;
390 
391 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_STACKSHOT, STACKSHOT_KERN_RECORD) | DBG_FUNC_START,
392 	    flags, size, pid, delta_since_timestamp);
393 
394 	istate = ml_set_interrupts_enabled(FALSE);
395 	uint64_t time_start      = mach_absolute_time();
396 
397 	/* Emit a SOCD tracepoint that we are initiating a stackshot */
398 	SOCD_TRACE_XNU_START(STACKSHOT);
399 
400 	/* Preload trace parameters*/
401 	kdp_snapshot_preflight(pid, buf, size, flags, &kcdata,
402 	    delta_since_timestamp, pagetable_mask);
403 
404 	/*
405 	 * Trap to the debugger to obtain a coherent stack snapshot; this populates
406 	 * the trace buffer
407 	 */
408 	error = stackshot_trap();
409 
410 	uint64_t time_end               = mach_absolute_time();
411 
412 	/* Emit a SOCD tracepoint that we have completed the stackshot */
413 	SOCD_TRACE_XNU_END(STACKSHOT);
414 
415 	ml_set_interrupts_enabled(istate);
416 
417 	if (stackshot_duration_outer) {
418 		*stackshot_duration_outer = time_end - time_start;
419 	}
420 	*bytes_traced = kdp_stack_snapshot_bytes_traced();
421 
422 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_STACKSHOT, STACKSHOT_KERN_RECORD) | DBG_FUNC_END,
423 	    error, (time_end - time_start), size, *bytes_traced);
424 out:
425 	stackshot_kcdata_p = NULL;
426 	STACKSHOT_SUBSYS_UNLOCK();
427 	return error;
428 }
429 
430 #if CONFIG_TELEMETRY
431 kern_return_t
stack_microstackshot(user_addr_t tracebuf,uint32_t tracebuf_size,uint32_t flags,int32_t * retval)432 stack_microstackshot(user_addr_t tracebuf, uint32_t tracebuf_size, uint32_t flags, int32_t *retval)
433 {
434 	int error = KERN_SUCCESS;
435 	uint32_t bytes_traced = 0;
436 
437 	*retval = -1;
438 
439 	/*
440 	 * Control related operations
441 	 */
442 	if (flags & STACKSHOT_GLOBAL_MICROSTACKSHOT_ENABLE) {
443 		telemetry_global_ctl(1);
444 		*retval = 0;
445 		goto exit;
446 	} else if (flags & STACKSHOT_GLOBAL_MICROSTACKSHOT_DISABLE) {
447 		telemetry_global_ctl(0);
448 		*retval = 0;
449 		goto exit;
450 	}
451 
452 	/*
453 	 * Data related operations
454 	 */
455 	*retval = -1;
456 
457 	if ((((void*)tracebuf) == NULL) || (tracebuf_size == 0)) {
458 		error = KERN_INVALID_ARGUMENT;
459 		goto exit;
460 	}
461 
462 	STACKSHOT_SUBSYS_LOCK();
463 
464 	if (flags & STACKSHOT_GET_MICROSTACKSHOT) {
465 		if (tracebuf_size > max_tracebuf_size) {
466 			error = KERN_INVALID_ARGUMENT;
467 			goto unlock_exit;
468 		}
469 
470 		bytes_traced = tracebuf_size;
471 		error = telemetry_gather(tracebuf, &bytes_traced,
472 		    (flags & STACKSHOT_SET_MICROSTACKSHOT_MARK) ? true : false);
473 		*retval = (int)bytes_traced;
474 		goto unlock_exit;
475 	}
476 
477 unlock_exit:
478 	STACKSHOT_SUBSYS_UNLOCK();
479 exit:
480 	return error;
481 }
482 #endif /* CONFIG_TELEMETRY */
483 
484 /*
485  * Return the estimated size of a stackshot based on the
486  * number of currently running threads and tasks.
487  *
488  * adj is an adjustment in units of percentage
489  *
490  * This function is mostly unhinged from reality; struct thread_snapshot and
491  * struct task_stackshot are legacy, much larger versions of the structures we
492  * actually use, and there's no accounting for how we actually generate
493  * task & thread information.  rdar://78880038 intends to replace this all.
494  */
495 uint32_t
get_stackshot_estsize(uint32_t prev_size_hint,uint32_t adj)496 get_stackshot_estsize(uint32_t prev_size_hint, uint32_t adj)
497 {
498 	vm_size_t thread_total;
499 	vm_size_t task_total;
500 	uint64_t size;
501 	uint32_t estimated_size;
502 	size_t est_thread_size = sizeof(struct thread_snapshot);
503 	size_t est_task_size = sizeof(struct task_snapshot) + TASK_UUID_AVG_SIZE;
504 
505 	adj = MIN(adj, 100u);   /* no more than double our estimate */
506 
507 #if STACKSHOT_COLLECTS_LATENCY_INFO
508 	if (collect_latency_info) {
509 		est_thread_size += sizeof(struct stackshot_latency_thread);
510 		est_task_size += sizeof(struct stackshot_latency_task);
511 	}
512 #endif
513 
514 	thread_total = (threads_count * est_thread_size);
515 	task_total = (tasks_count  * est_task_size);
516 
517 	size = thread_total + task_total + STACKSHOT_SUPP_SIZE;                 /* estimate */
518 	size += (size * adj) / 100;                                                                     /* add adj */
519 	size = MAX(size, prev_size_hint);                                                               /* allow hint to increase */
520 	size += stackshot_plh_est_size(); /* add space for the port label hash */
521 	size = MIN(size, VM_MAP_TRUNC_PAGE(UINT32_MAX, PAGE_MASK));             /* avoid overflow */
522 	estimated_size = (uint32_t) VM_MAP_ROUND_PAGE(size, PAGE_MASK); /* round to pagesize */
523 
524 	return estimated_size;
525 }
526 
527 /*
528  * stackshot_remap_buffer:	Utility function to remap bytes_traced bytes starting at stackshotbuf
529  *				into the current task's user space and subsequently copy out the address
530  *				at which the buffer has been mapped in user space to out_buffer_addr.
531  *
532  * Inputs:			stackshotbuf - pointer to the original buffer in the kernel's address space
533  *				bytes_traced - length of the buffer to remap starting from stackshotbuf
534  *				out_buffer_addr - pointer to placeholder where newly mapped buffer will be mapped.
535  *				out_size_addr - pointer to be filled in with the size of the buffer
536  *
537  * Outputs:			ENOSPC if there is not enough free space in the task's address space to remap the buffer
538  *				EINVAL for all other errors returned by task_remap_buffer/mach_vm_remap
539  *				an error from copyout
540  */
541 static kern_return_t
stackshot_remap_buffer(void * stackshotbuf,uint32_t bytes_traced,uint64_t out_buffer_addr,uint64_t out_size_addr)542 stackshot_remap_buffer(void *stackshotbuf, uint32_t bytes_traced, uint64_t out_buffer_addr, uint64_t out_size_addr)
543 {
544 	int                     error = 0;
545 	mach_vm_offset_t        stackshotbuf_user_addr = (mach_vm_offset_t)NULL;
546 	vm_prot_t               cur_prot, max_prot;
547 
548 	error = mach_vm_remap_kernel(get_task_map(current_task()), &stackshotbuf_user_addr, bytes_traced, 0,
549 	    VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_NONE, kernel_map, (mach_vm_offset_t)stackshotbuf, FALSE, &cur_prot, &max_prot, VM_INHERIT_DEFAULT);
550 	/*
551 	 * If the call to mach_vm_remap fails, we return the appropriate converted error
552 	 */
553 	if (error == KERN_SUCCESS) {
554 		/*
555 		 * If we fail to copy out the address or size of the new buffer, we remove the buffer mapping that
556 		 * we just made in the task's user space.
557 		 */
558 		error = copyout(CAST_DOWN(void *, &stackshotbuf_user_addr), (user_addr_t)out_buffer_addr, sizeof(stackshotbuf_user_addr));
559 		if (error != KERN_SUCCESS) {
560 			mach_vm_deallocate(get_task_map(current_task()), stackshotbuf_user_addr, (mach_vm_size_t)bytes_traced);
561 			return error;
562 		}
563 		error = copyout(&bytes_traced, (user_addr_t)out_size_addr, sizeof(bytes_traced));
564 		if (error != KERN_SUCCESS) {
565 			mach_vm_deallocate(get_task_map(current_task()), stackshotbuf_user_addr, (mach_vm_size_t)bytes_traced);
566 			return error;
567 		}
568 	}
569 	return error;
570 }
571 
572 kern_return_t
kern_stack_snapshot_internal(int stackshot_config_version,void * stackshot_config,size_t stackshot_config_size,boolean_t stackshot_from_user)573 kern_stack_snapshot_internal(int stackshot_config_version, void *stackshot_config, size_t stackshot_config_size, boolean_t stackshot_from_user)
574 {
575 	int error = 0;
576 	boolean_t prev_interrupt_state;
577 	uint32_t bytes_traced = 0;
578 	uint32_t stackshot_estimate = 0;
579 	uint32_t stackshotbuf_size = 0;
580 	void * stackshotbuf = NULL;
581 	kcdata_descriptor_t kcdata_p = NULL;
582 
583 	void * buf_to_free = NULL;
584 	int size_to_free = 0;
585 	bool is_traced = false;    /* has FUNC_START tracepoint fired? */
586 	uint64_t tot_interrupts_off_abs = 0; /* sum(time with interrupts off) */
587 
588 	/* Parsed arguments */
589 	uint64_t                out_buffer_addr;
590 	uint64_t                out_size_addr;
591 	int                     pid = -1;
592 	uint64_t                flags;
593 	uint64_t                since_timestamp;
594 	uint32_t                size_hint = 0;
595 	uint32_t                pagetable_mask = STACKSHOT_PAGETABLES_MASK_ALL;
596 
597 	if (stackshot_config == NULL) {
598 		return KERN_INVALID_ARGUMENT;
599 	}
600 #if DEVELOPMENT || DEBUG
601 	/* TBD: ask stackshot clients to avoid issuing stackshots in this
602 	 * configuration in lieu of the kernel feature override.
603 	 */
604 	if (kern_feature_override(KF_STACKSHOT_OVRD) == TRUE) {
605 		return KERN_NOT_SUPPORTED;
606 	}
607 #endif
608 
609 	switch (stackshot_config_version) {
610 	case STACKSHOT_CONFIG_TYPE:
611 		if (stackshot_config_size != sizeof(stackshot_config_t)) {
612 			return KERN_INVALID_ARGUMENT;
613 		}
614 		stackshot_config_t *config = (stackshot_config_t *) stackshot_config;
615 		out_buffer_addr = config->sc_out_buffer_addr;
616 		out_size_addr = config->sc_out_size_addr;
617 		pid = config->sc_pid;
618 		flags = config->sc_flags;
619 		since_timestamp = config->sc_delta_timestamp;
620 		if (config->sc_size <= max_tracebuf_size) {
621 			size_hint = config->sc_size;
622 		}
623 		/*
624 		 * Retain the pre-sc_pagetable_mask behavior of STACKSHOT_PAGE_TABLES,
625 		 * dump every level if the pagetable_mask is not set
626 		 */
627 		if (flags & STACKSHOT_PAGE_TABLES && config->sc_pagetable_mask) {
628 			pagetable_mask = config->sc_pagetable_mask;
629 		}
630 		break;
631 	default:
632 		return KERN_NOT_SUPPORTED;
633 	}
634 
635 	/*
636 	 * Currently saving a kernel buffer and trylock are only supported from the
637 	 * internal/KEXT API.
638 	 */
639 	if (stackshot_from_user) {
640 		if (flags & (STACKSHOT_TRYLOCK | STACKSHOT_SAVE_IN_KERNEL_BUFFER | STACKSHOT_FROM_PANIC)) {
641 			return KERN_NO_ACCESS;
642 		}
643 #if !DEVELOPMENT && !DEBUG
644 		if (flags & (STACKSHOT_DO_COMPRESS)) {
645 			return KERN_NO_ACCESS;
646 		}
647 #endif
648 	} else {
649 		if (!(flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER)) {
650 			return KERN_NOT_SUPPORTED;
651 		}
652 	}
653 
654 	if (!((flags & STACKSHOT_KCDATA_FORMAT) || (flags & STACKSHOT_RETRIEVE_EXISTING_BUFFER))) {
655 		return KERN_NOT_SUPPORTED;
656 	}
657 
658 	/* Compresssed delta stackshots or page dumps are not yet supported */
659 	if (((flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) || (flags & STACKSHOT_PAGE_TABLES))
660 	    && (flags & STACKSHOT_DO_COMPRESS)) {
661 		return KERN_NOT_SUPPORTED;
662 	}
663 
664 	/*
665 	 * If we're not saving the buffer in the kernel pointer, we need a place to copy into.
666 	 */
667 	if ((!out_buffer_addr || !out_size_addr) && !(flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER)) {
668 		return KERN_INVALID_ARGUMENT;
669 	}
670 
671 	if (since_timestamp != 0 && ((flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) == 0)) {
672 		return KERN_INVALID_ARGUMENT;
673 	}
674 
675 #if CONFIG_PERVASIVE_CPI && MONOTONIC
676 	if (!mt_core_supported) {
677 		flags &= ~STACKSHOT_INSTRS_CYCLES;
678 	}
679 #else /* CONFIG_PERVASIVE_CPI && MONOTONIC */
680 	flags &= ~STACKSHOT_INSTRS_CYCLES;
681 #endif /* !CONFIG_PERVASIVE_CPI || !MONOTONIC */
682 
683 	STACKSHOT_SUBSYS_LOCK();
684 
685 	if (flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER) {
686 		/*
687 		 * Don't overwrite an existing stackshot
688 		 */
689 		if (kernel_stackshot_buf != NULL) {
690 			error = KERN_MEMORY_PRESENT;
691 			goto error_exit;
692 		}
693 	} else if (flags & STACKSHOT_RETRIEVE_EXISTING_BUFFER) {
694 		if ((kernel_stackshot_buf == NULL) || (kernel_stackshot_buf_size <= 0)) {
695 			error = KERN_NOT_IN_SET;
696 			goto error_exit;
697 		}
698 		error = stackshot_remap_buffer(kernel_stackshot_buf, kernel_stackshot_buf_size,
699 		    out_buffer_addr, out_size_addr);
700 		/*
701 		 * If we successfully remapped the buffer into the user's address space, we
702 		 * set buf_to_free and size_to_free so the prior kernel mapping will be removed
703 		 * and then clear the kernel stackshot pointer and associated size.
704 		 */
705 		if (error == KERN_SUCCESS) {
706 			buf_to_free = kernel_stackshot_buf;
707 			size_to_free = (int) VM_MAP_ROUND_PAGE(kernel_stackshot_buf_size, PAGE_MASK);
708 			kernel_stackshot_buf = NULL;
709 			kernel_stackshot_buf_size = 0;
710 		}
711 
712 		goto error_exit;
713 	}
714 
715 	if (flags & STACKSHOT_GET_BOOT_PROFILE) {
716 		void *bootprofile = NULL;
717 		uint32_t len = 0;
718 #if CONFIG_TELEMETRY
719 		bootprofile_get(&bootprofile, &len);
720 #endif
721 		if (!bootprofile || !len) {
722 			error = KERN_NOT_IN_SET;
723 			goto error_exit;
724 		}
725 		error = stackshot_remap_buffer(bootprofile, len, out_buffer_addr, out_size_addr);
726 		goto error_exit;
727 	}
728 
729 	stackshot_duration_prior_abs = 0;
730 	stackshot_initial_estimate_adj = os_atomic_load(&stackshot_estimate_adj, relaxed);
731 	stackshotbuf_size = stackshot_estimate =
732 	    get_stackshot_estsize(size_hint, stackshot_initial_estimate_adj);
733 	stackshot_initial_estimate = stackshot_estimate;
734 
735 	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_STACKSHOT, STACKSHOT_RECORD) | DBG_FUNC_START,
736 	    flags, stackshotbuf_size, pid, since_timestamp);
737 	is_traced = true;
738 
739 	for (; stackshotbuf_size <= max_tracebuf_size; stackshotbuf_size <<= 1) {
740 		if (kmem_alloc(kernel_map, (vm_offset_t *)&stackshotbuf, stackshotbuf_size,
741 		    KMA_ZERO | KMA_DATA, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
742 			error = KERN_RESOURCE_SHORTAGE;
743 			goto error_exit;
744 		}
745 
746 
747 		uint32_t hdr_tag = (flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) ? KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT
748 		    : (flags & STACKSHOT_DO_COMPRESS) ? KCDATA_BUFFER_BEGIN_COMPRESSED
749 		    : KCDATA_BUFFER_BEGIN_STACKSHOT;
750 		kcdata_p = kcdata_memory_alloc_init((mach_vm_address_t)stackshotbuf, hdr_tag, stackshotbuf_size,
751 		    KCFLAG_USE_MEMCOPY | KCFLAG_NO_AUTO_ENDBUFFER);
752 
753 		stackshot_duration_outer = NULL;
754 
755 		/* if compression was requested, allocate the extra zlib scratch area */
756 		if (flags & STACKSHOT_DO_COMPRESS) {
757 			hdr_tag = (flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) ? KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT
758 			    : KCDATA_BUFFER_BEGIN_STACKSHOT;
759 			error = kcdata_init_compress(kcdata_p, hdr_tag, kdp_memcpy, KCDCT_ZLIB);
760 			if (error != KERN_SUCCESS) {
761 				os_log(OS_LOG_DEFAULT, "failed to initialize compression: %d!\n",
762 				    (int) error);
763 				goto error_exit;
764 			}
765 		}
766 
767 		/*
768 		 * Disable interrupts and save the current interrupt state.
769 		 */
770 		prev_interrupt_state = ml_set_interrupts_enabled(FALSE);
771 		uint64_t time_start      = mach_absolute_time();
772 
773 		/* Emit a SOCD tracepoint that we are initiating a stackshot */
774 		SOCD_TRACE_XNU_START(STACKSHOT);
775 
776 		/*
777 		 * Load stackshot parameters.
778 		 */
779 		kdp_snapshot_preflight(pid, stackshotbuf, stackshotbuf_size, flags, kcdata_p, since_timestamp,
780 		    pagetable_mask);
781 
782 		error = stackshot_trap();
783 
784 		/* record the duration that interupts were disabled */
785 		uint64_t time_end = mach_absolute_time();
786 
787 		/* Emit a SOCD tracepoint that we have completed the stackshot */
788 		SOCD_TRACE_XNU_END(STACKSHOT);
789 		ml_set_interrupts_enabled(prev_interrupt_state);
790 
791 		if (stackshot_duration_outer) {
792 			*stackshot_duration_outer = time_end - time_start;
793 		}
794 		tot_interrupts_off_abs += time_end - time_start;
795 
796 		if (error != KERN_SUCCESS) {
797 			if (kcdata_p != NULL) {
798 				kcdata_memory_destroy(kcdata_p);
799 				kcdata_p = NULL;
800 				stackshot_kcdata_p = NULL;
801 			}
802 			kmem_free(kernel_map, (vm_offset_t)stackshotbuf, stackshotbuf_size);
803 			stackshotbuf = NULL;
804 			if (error == KERN_INSUFFICIENT_BUFFER_SIZE) {
805 				/*
806 				 * If we didn't allocate a big enough buffer, deallocate and try again.
807 				 */
808 				KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_STACKSHOT, STACKSHOT_RECORD_SHORT) | DBG_FUNC_NONE,
809 				    time_end - time_start, stackshot_estimate, stackshotbuf_size);
810 				stackshot_duration_prior_abs += (time_end - time_start);
811 				continue;
812 			} else {
813 				goto error_exit;
814 			}
815 		}
816 
817 		bytes_traced = kdp_stack_snapshot_bytes_traced();
818 		if (bytes_traced <= 0) {
819 			error = KERN_ABORTED;
820 			goto error_exit;
821 		}
822 
823 		assert(bytes_traced <= stackshotbuf_size);
824 		if (!(flags & STACKSHOT_SAVE_IN_KERNEL_BUFFER)) {
825 			error = stackshot_remap_buffer(stackshotbuf, bytes_traced, out_buffer_addr, out_size_addr);
826 			goto error_exit;
827 		}
828 
829 		/*
830 		 * Save the stackshot in the kernel buffer.
831 		 */
832 		kernel_stackshot_buf = stackshotbuf;
833 		kernel_stackshot_buf_size =  bytes_traced;
834 		/*
835 		 * Figure out if we didn't use all the pages in the buffer. If so, we set buf_to_free to the beginning of
836 		 * the next page after the end of the stackshot in the buffer so that the kmem_free clips the buffer and
837 		 * update size_to_free for kmem_free accordingly.
838 		 */
839 		size_to_free = stackshotbuf_size - (int) VM_MAP_ROUND_PAGE(bytes_traced, PAGE_MASK);
840 
841 		assert(size_to_free >= 0);
842 
843 		if (size_to_free != 0) {
844 			buf_to_free = (void *)((uint64_t)stackshotbuf + stackshotbuf_size - size_to_free);
845 		}
846 
847 		stackshotbuf = NULL;
848 		stackshotbuf_size = 0;
849 		goto error_exit;
850 	}
851 
852 	if (stackshotbuf_size > max_tracebuf_size) {
853 		error = KERN_RESOURCE_SHORTAGE;
854 	}
855 
856 error_exit:
857 	if (is_traced) {
858 		KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_STACKSHOT, STACKSHOT_RECORD) | DBG_FUNC_END,
859 		    error, tot_interrupts_off_abs, stackshotbuf_size, bytes_traced);
860 	}
861 	if (kcdata_p != NULL) {
862 		kcdata_memory_destroy(kcdata_p);
863 		kcdata_p = NULL;
864 		stackshot_kcdata_p = NULL;
865 	}
866 
867 	if (stackshotbuf != NULL) {
868 		kmem_free(kernel_map, (vm_offset_t)stackshotbuf, stackshotbuf_size);
869 	}
870 	if (buf_to_free != NULL) {
871 		kmem_free(kernel_map, (vm_offset_t)buf_to_free, size_to_free);
872 	}
873 	STACKSHOT_SUBSYS_UNLOCK();
874 	return error;
875 }
876 
877 /*
878  * Cache stack snapshot parameters in preparation for a trace.
879  */
880 void
kdp_snapshot_preflight(int pid,void * tracebuf,uint32_t tracebuf_size,uint64_t flags,kcdata_descriptor_t data_p,uint64_t since_timestamp,uint32_t pagetable_mask)881 kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size, uint64_t flags,
882     kcdata_descriptor_t data_p, uint64_t since_timestamp, uint32_t pagetable_mask)
883 {
884 	uint64_t microsecs = 0, secs = 0;
885 	clock_get_calendar_microtime((clock_sec_t *)&secs, (clock_usec_t *)&microsecs);
886 
887 	stackshot_microsecs = microsecs + (secs * USEC_PER_SEC);
888 	stack_snapshot_pid = pid;
889 	stack_snapshot_buf = tracebuf;
890 	stack_snapshot_bufsize = tracebuf_size;
891 	stack_snapshot_flags = flags;
892 	stack_snapshot_delta_since_timestamp = since_timestamp;
893 	stack_snapshot_pagetable_mask = pagetable_mask;
894 
895 	panic_stackshot = ((flags & STACKSHOT_FROM_PANIC) != 0);
896 
897 	assert(data_p != NULL);
898 	assert(stackshot_kcdata_p == NULL);
899 	stackshot_kcdata_p = data_p;
900 
901 	stack_snapshot_bytes_traced = 0;
902 	stack_snapshot_bytes_uncompressed = 0;
903 }
904 
905 void
panic_stackshot_reset_state(void)906 panic_stackshot_reset_state(void)
907 {
908 	stackshot_kcdata_p = NULL;
909 }
910 
911 boolean_t
stackshot_active(void)912 stackshot_active(void)
913 {
914 	return stackshot_kcdata_p != NULL;
915 }
916 
917 uint32_t
kdp_stack_snapshot_bytes_traced(void)918 kdp_stack_snapshot_bytes_traced(void)
919 {
920 	return stack_snapshot_bytes_traced;
921 }
922 
923 uint32_t
kdp_stack_snapshot_bytes_uncompressed(void)924 kdp_stack_snapshot_bytes_uncompressed(void)
925 {
926 	return stack_snapshot_bytes_uncompressed;
927 }
928 
929 static boolean_t
memory_iszero(void * addr,size_t size)930 memory_iszero(void *addr, size_t size)
931 {
932 	char *data = (char *)addr;
933 	for (size_t i = 0; i < size; i++) {
934 		if (data[i] != 0) {
935 			return FALSE;
936 		}
937 	}
938 	return TRUE;
939 }
940 
941 /*
942  * Keep a simple cache of the most recent validation done at a page granularity
943  * to avoid the expensive software KVA-to-phys translation in the VM.
944  */
945 
946 struct _stackshot_validation_state {
947 	vm_offset_t last_valid_page_kva;
948 	size_t last_valid_size;
949 } g_validation_state;
950 
951 static void
_stackshot_validation_reset(void)952 _stackshot_validation_reset(void)
953 {
954 	g_validation_state.last_valid_page_kva = -1;
955 	g_validation_state.last_valid_size = 0;
956 }
957 
958 static bool
_stackshot_validate_kva(vm_offset_t addr,size_t size)959 _stackshot_validate_kva(vm_offset_t addr, size_t size)
960 {
961 	vm_offset_t page_addr = atop_kernel(addr);
962 	if (g_validation_state.last_valid_page_kva == page_addr &&
963 	    g_validation_state.last_valid_size <= size) {
964 		return true;
965 	}
966 
967 	if (ml_validate_nofault(addr, size)) {
968 		g_validation_state.last_valid_page_kva = page_addr;
969 		g_validation_state.last_valid_size = size;
970 		return true;
971 	}
972 	return false;
973 }
974 
975 static long
_stackshot_strlen(const char * s,size_t maxlen)976 _stackshot_strlen(const char *s, size_t maxlen)
977 {
978 	size_t len = 0;
979 	for (len = 0; _stackshot_validate_kva((vm_offset_t)s, 1); len++, s++) {
980 		if (*s == 0) {
981 			return len;
982 		}
983 		if (len >= maxlen) {
984 			return -1;
985 		}
986 	}
987 	return -1; /* failed before end of string */
988 }
989 
990 #define kcd_end_address(kcd) ((void *)((uint64_t)((kcd)->kcd_addr_begin) + kcdata_memory_get_used_bytes((kcd))))
991 #define kcd_max_address(kcd) ((void *)((kcd)->kcd_addr_begin + (kcd)->kcd_length))
992 /*
993  * Use of the kcd_exit_on_error(action) macro requires a local
994  * 'kern_return_t error' variable and 'error_exit' label.
995  */
996 #define kcd_exit_on_error(action)                      \
997 	do {                                               \
998 	        if (KERN_SUCCESS != (error = (action))) {      \
999 	                if (error == KERN_RESOURCE_SHORTAGE) {     \
1000 	                        error = KERN_INSUFFICIENT_BUFFER_SIZE; \
1001 	                }                                          \
1002 	                goto error_exit;                           \
1003 	        }                                              \
1004 	} while (0); /* end kcd_exit_on_error */
1005 
1006 
1007 /*
1008  * For port labels, we have a small hash table we use to track the
1009  * struct ipc_service_port_label pointers we see along the way.
1010  * This structure encapsulates the global state.
1011  *
1012  * The hash table is insert-only, similar to "intern"ing strings.  It's
1013  * only used an manipulated in during the stackshot collection.  We use
1014  * seperate chaining, with the hash elements and chains being int16_ts
1015  * indexes into the parallel arrays, with -1 ending the chain.  Array indices are
1016  * allocated using a bump allocator.
1017  *
1018  * The parallel arrays contain:
1019  *      - plh_array[idx]	the pointer entered
1020  *      - plh_chains[idx]	the hash chain
1021  *      - plh_gen[idx]		the last 'generation #' seen
1022  *
1023  * Generation IDs are used to track entries looked up in the current
1024  * task; 0 is never used, and the plh_gen array is cleared to 0 on
1025  * rollover.
1026  *
1027  * The portlabel_ids we report externally are just the index in the array,
1028  * plus 1 to avoid 0 as a value.  0 is NONE, -1 is UNKNOWN (e.g. there is
1029  * one, but we ran out of space)
1030  */
1031 struct port_label_hash {
1032 	uint16_t                plh_size;       /* size of allocations; 0 disables tracking */
1033 	uint16_t                plh_count;      /* count of used entries in plh_array */
1034 	struct ipc_service_port_label **plh_array; /* _size allocated, _count used */
1035 	int16_t                *plh_chains;    /* _size allocated */
1036 	uint8_t                *plh_gen;       /* last 'gen #' seen in */
1037 	int16_t                *plh_hash;      /* (1 << STACKSHOT_PLH_SHIFT) entry hash table: hash(ptr) -> array index */
1038 	int16_t                 plh_curgen_min; /* min idx seen for this gen */
1039 	int16_t                 plh_curgen_max; /* max idx seen for this gen */
1040 	uint8_t                 plh_curgen;     /* current gen */
1041 #if DEVELOPMENT || DEBUG
1042 	/* statistics */
1043 	uint32_t                plh_lookups;    /* # lookups or inserts */
1044 	uint32_t                plh_found;
1045 	uint32_t                plh_found_depth;
1046 	uint32_t                plh_insert;
1047 	uint32_t                plh_insert_depth;
1048 	uint32_t                plh_bad;
1049 	uint32_t                plh_bad_depth;
1050 	uint32_t                plh_lookup_send;
1051 	uint32_t                plh_lookup_receive;
1052 #define PLH_STAT_OP(...)    (void)(__VA_ARGS__)
1053 #else /* DEVELOPMENT || DEBUG */
1054 #define PLH_STAT_OP(...)    (void)(0)
1055 #endif /* DEVELOPMENT || DEBUG */
1056 } port_label_hash;
1057 
1058 #define STACKSHOT_PLH_SHIFT    7
1059 #define STACKSHOT_PLH_SIZE_MAX ((kdp_ipc_have_splabel)? 1024 : 0)
1060 size_t stackshot_port_label_size = (2 * (1u << STACKSHOT_PLH_SHIFT));
1061 #define STASKSHOT_PLH_SIZE(x) MIN((x), STACKSHOT_PLH_SIZE_MAX)
1062 
1063 static size_t
stackshot_plh_est_size(void)1064 stackshot_plh_est_size(void)
1065 {
1066 	struct port_label_hash *plh = &port_label_hash;
1067 	size_t size = STASKSHOT_PLH_SIZE(stackshot_port_label_size);
1068 
1069 	if (size == 0) {
1070 		return 0;
1071 	}
1072 #define SIZE_EST(x) ROUNDUP((x), sizeof (uintptr_t))
1073 	return SIZE_EST(size * sizeof(*plh->plh_array)) +
1074 	       SIZE_EST(size * sizeof(*plh->plh_chains)) +
1075 	       SIZE_EST(size * sizeof(*plh->plh_gen)) +
1076 	       SIZE_EST((1ul << STACKSHOT_PLH_SHIFT) * sizeof(*plh->plh_hash));
1077 #undef SIZE_EST
1078 }
1079 
1080 static void
stackshot_plh_reset(void)1081 stackshot_plh_reset(void)
1082 {
1083 	port_label_hash = (struct port_label_hash){.plh_size = 0};  /* structure assignment */
1084 }
1085 
1086 static void
stackshot_plh_setup(kcdata_descriptor_t data)1087 stackshot_plh_setup(kcdata_descriptor_t data)
1088 {
1089 	struct port_label_hash plh = {
1090 		.plh_size = STASKSHOT_PLH_SIZE(stackshot_port_label_size),
1091 		.plh_count = 0,
1092 		.plh_curgen = 1,
1093 		.plh_curgen_min = STACKSHOT_PLH_SIZE_MAX,
1094 		.plh_curgen_max = 0,
1095 	};
1096 	stackshot_plh_reset();
1097 	size_t size = plh.plh_size;
1098 	if (size == 0) {
1099 		return;
1100 	}
1101 	plh.plh_array = kcdata_endalloc(data, size * sizeof(*plh.plh_array));
1102 	plh.plh_chains = kcdata_endalloc(data, size * sizeof(*plh.plh_chains));
1103 	plh.plh_gen = kcdata_endalloc(data, size * sizeof(*plh.plh_gen));
1104 	plh.plh_hash = kcdata_endalloc(data, (1ul << STACKSHOT_PLH_SHIFT) * sizeof(*plh.plh_hash));
1105 	if (plh.plh_array == NULL || plh.plh_chains == NULL || plh.plh_gen == NULL || plh.plh_hash == NULL) {
1106 		PLH_STAT_OP(port_label_hash.plh_bad++);
1107 		return;
1108 	}
1109 	for (int x = 0; x < size; x++) {
1110 		plh.plh_array[x] = NULL;
1111 		plh.plh_chains[x] = -1;
1112 		plh.plh_gen[x] = 0;
1113 	}
1114 	for (int x = 0; x < (1ul << STACKSHOT_PLH_SHIFT); x++) {
1115 		plh.plh_hash[x] = -1;
1116 	}
1117 	port_label_hash = plh;  /* structure assignment */
1118 }
1119 
1120 static int16_t
stackshot_plh_hash(struct ipc_service_port_label * ispl)1121 stackshot_plh_hash(struct ipc_service_port_label *ispl)
1122 {
1123 	uintptr_t ptr = (uintptr_t)ispl;
1124 	static_assert(STACKSHOT_PLH_SHIFT < 16, "plh_hash must fit in 15 bits");
1125 #define PLH_HASH_STEP(ptr, x) \
1126 	    ((((x) * STACKSHOT_PLH_SHIFT) < (sizeof(ispl) * CHAR_BIT)) ? ((ptr) >> ((x) * STACKSHOT_PLH_SHIFT)) : 0)
1127 	ptr ^= PLH_HASH_STEP(ptr, 16);
1128 	ptr ^= PLH_HASH_STEP(ptr, 8);
1129 	ptr ^= PLH_HASH_STEP(ptr, 4);
1130 	ptr ^= PLH_HASH_STEP(ptr, 2);
1131 	ptr ^= PLH_HASH_STEP(ptr, 1);
1132 #undef PLH_HASH_STEP
1133 	return (int16_t)(ptr & ((1ul << STACKSHOT_PLH_SHIFT) - 1));
1134 }
1135 
1136 enum stackshot_plh_lookup_type {
1137 	STACKSHOT_PLH_LOOKUP_UNKNOWN,
1138 	STACKSHOT_PLH_LOOKUP_SEND,
1139 	STACKSHOT_PLH_LOOKUP_RECEIVE,
1140 };
1141 
1142 static void
stackshot_plh_resetgen(void)1143 stackshot_plh_resetgen(void)
1144 {
1145 	struct port_label_hash *plh = &port_label_hash;
1146 	if (plh->plh_curgen_min == STACKSHOT_PLH_SIZE_MAX && plh->plh_curgen_max == 0) {
1147 		return;  // no lookups, nothing using the current generation
1148 	}
1149 	plh->plh_curgen++;
1150 	plh->plh_curgen_min = STACKSHOT_PLH_SIZE_MAX;
1151 	plh->plh_curgen_max = 0;
1152 	if (plh->plh_curgen == 0) { // wrapped, zero the array and increment the generation
1153 		for (int x = 0; x < plh->plh_size; x++) {
1154 			plh->plh_gen[x] = 0;
1155 		}
1156 		plh->plh_curgen = 1;
1157 	}
1158 }
1159 
1160 static int16_t
stackshot_plh_lookup(struct ipc_service_port_label * ispl,enum stackshot_plh_lookup_type type)1161 stackshot_plh_lookup(struct ipc_service_port_label *ispl, enum stackshot_plh_lookup_type type)
1162 {
1163 	struct port_label_hash *plh = &port_label_hash;
1164 	int depth;
1165 	int16_t cur;
1166 	if (ispl == NULL) {
1167 		return STACKSHOT_PORTLABELID_NONE;
1168 	}
1169 	switch (type) {
1170 	case STACKSHOT_PLH_LOOKUP_SEND:
1171 		PLH_STAT_OP(plh->plh_lookup_send++);
1172 		break;
1173 	case STACKSHOT_PLH_LOOKUP_RECEIVE:
1174 		PLH_STAT_OP(plh->plh_lookup_receive++);
1175 		break;
1176 	default:
1177 		break;
1178 	}
1179 	PLH_STAT_OP(plh->plh_lookups++);
1180 	if (plh->plh_size == 0) {
1181 		return STACKSHOT_PORTLABELID_MISSING;
1182 	}
1183 	int16_t hash = stackshot_plh_hash(ispl);
1184 	assert(hash >= 0 && hash < (1ul << STACKSHOT_PLH_SHIFT));
1185 	depth = 0;
1186 	for (cur = plh->plh_hash[hash]; cur >= 0; cur = plh->plh_chains[cur]) {
1187 		/* cur must be in-range, and chain depth can never be above our # allocated */
1188 		if (cur >= plh->plh_count || depth > plh->plh_count || depth > plh->plh_size) {
1189 			PLH_STAT_OP((plh->plh_bad++), (plh->plh_bad_depth += depth));
1190 			return STACKSHOT_PORTLABELID_MISSING;
1191 		}
1192 		assert(cur < plh->plh_count);
1193 		if (plh->plh_array[cur] == ispl) {
1194 			PLH_STAT_OP((plh->plh_found++), (plh->plh_found_depth += depth));
1195 			goto found;
1196 		}
1197 		depth++;
1198 	}
1199 	/* not found in hash table, so alloc and insert it */
1200 	if (cur != -1) {
1201 		PLH_STAT_OP((plh->plh_bad++), (plh->plh_bad_depth += depth));
1202 		return STACKSHOT_PORTLABELID_MISSING; /* bad end of chain */
1203 	}
1204 	PLH_STAT_OP((plh->plh_insert++), (plh->plh_insert_depth += depth));
1205 	if (plh->plh_count >= plh->plh_size) {
1206 		return STACKSHOT_PORTLABELID_MISSING; /* no space */
1207 	}
1208 	cur = plh->plh_count;
1209 	plh->plh_count++;
1210 	plh->plh_array[cur] = ispl;
1211 	plh->plh_chains[cur] = plh->plh_hash[hash];
1212 	plh->plh_hash[hash] = cur;
1213 found:
1214 	plh->plh_gen[cur] = plh->plh_curgen;
1215 	if (plh->plh_curgen_min > cur) {
1216 		plh->plh_curgen_min = cur;
1217 	}
1218 	if (plh->plh_curgen_max < cur) {
1219 		plh->plh_curgen_max = cur;
1220 	}
1221 	return cur + 1;   /* offset to avoid 0 */
1222 }
1223 
1224 // record any PLH referenced since the last stackshot_plh_resetgen() call
1225 static kern_return_t
kdp_stackshot_plh_record(void)1226 kdp_stackshot_plh_record(void)
1227 {
1228 	kern_return_t error = KERN_SUCCESS;
1229 	struct port_label_hash *plh = &port_label_hash;
1230 	uint16_t count = plh->plh_count;
1231 	uint8_t curgen = plh->plh_curgen;
1232 	int16_t curgen_min = plh->plh_curgen_min;
1233 	int16_t curgen_max = plh->plh_curgen_max;
1234 	if (curgen_min <= curgen_max && curgen_max < count &&
1235 	    count <= plh->plh_size && plh->plh_size <= STACKSHOT_PLH_SIZE_MAX) {
1236 		struct ipc_service_port_label **arr = plh->plh_array;
1237 		size_t ispl_size, max_namelen;
1238 		kdp_ipc_splabel_size(&ispl_size, &max_namelen);
1239 		for (int idx = curgen_min; idx <= curgen_max; idx++) {
1240 			struct ipc_service_port_label *ispl = arr[idx];
1241 			struct portlabel_info spl = {
1242 				.portlabel_id = (idx + 1),
1243 			};
1244 			const char *name = NULL;
1245 			long name_sz = 0;
1246 			if (plh->plh_gen[idx] != curgen) {
1247 				continue;
1248 			}
1249 			if (_stackshot_validate_kva((vm_offset_t)ispl, ispl_size)) {
1250 				kdp_ipc_fill_splabel(ispl, &spl, &name);
1251 			}
1252 			kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN,
1253 			    STACKSHOT_KCCONTAINER_PORTLABEL, idx + 1));
1254 			if (name != NULL && (name_sz = _stackshot_strlen(name, max_namelen)) > 0) {   /* validates the kva */
1255 				kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_PORTLABEL_NAME, name_sz + 1, name));
1256 			} else {
1257 				spl.portlabel_flags |= STACKSHOT_PORTLABEL_READFAILED;
1258 			}
1259 			kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_PORTLABEL, sizeof(spl), &spl));
1260 			kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_END,
1261 			    STACKSHOT_KCCONTAINER_PORTLABEL, idx + 1));
1262 		}
1263 	}
1264 
1265 error_exit:
1266 	return error;
1267 }
1268 
1269 #if DEVELOPMENT || DEBUG
1270 static kern_return_t
kdp_stackshot_plh_stats(void)1271 kdp_stackshot_plh_stats(void)
1272 {
1273 	kern_return_t error = KERN_SUCCESS;
1274 	struct port_label_hash *plh = &port_label_hash;
1275 
1276 #define PLH_STAT(x) do { if (plh->x != 0) { \
1277 	kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, plh->x, "stackshot_" #x)); \
1278 } } while (0)
1279 	PLH_STAT(plh_size);
1280 	PLH_STAT(plh_lookups);
1281 	PLH_STAT(plh_found);
1282 	PLH_STAT(plh_found_depth);
1283 	PLH_STAT(plh_insert);
1284 	PLH_STAT(plh_insert_depth);
1285 	PLH_STAT(plh_bad);
1286 	PLH_STAT(plh_bad_depth);
1287 	PLH_STAT(plh_lookup_send);
1288 	PLH_STAT(plh_lookup_receive);
1289 #undef PLH_STAT
1290 
1291 error_exit:
1292 	return error;
1293 }
1294 #endif /* DEVELOPMENT || DEBUG */
1295 
1296 static uint64_t
kcdata_get_task_ss_flags(task_t task)1297 kcdata_get_task_ss_flags(task_t task)
1298 {
1299 	uint64_t ss_flags = 0;
1300 	boolean_t task_64bit_addr = task_has_64Bit_addr(task);
1301 	void *bsd_info = get_bsdtask_info(task);
1302 
1303 	if (task_64bit_addr) {
1304 		ss_flags |= kUser64_p;
1305 	}
1306 	if (!task->active || task_is_a_corpse(task) || proc_exiting(bsd_info)) {
1307 		ss_flags |= kTerminatedSnapshot;
1308 	}
1309 	if (task->pidsuspended) {
1310 		ss_flags |= kPidSuspended;
1311 	}
1312 	if (task->frozen) {
1313 		ss_flags |= kFrozen;
1314 	}
1315 	if (task->effective_policy.tep_darwinbg == 1) {
1316 		ss_flags |= kTaskDarwinBG;
1317 	}
1318 	if (task->requested_policy.trp_role == TASK_FOREGROUND_APPLICATION) {
1319 		ss_flags |= kTaskIsForeground;
1320 	}
1321 	if (task->requested_policy.trp_boosted == 1) {
1322 		ss_flags |= kTaskIsBoosted;
1323 	}
1324 	if (task->effective_policy.tep_sup_active == 1) {
1325 		ss_flags |= kTaskIsSuppressed;
1326 	}
1327 #if CONFIG_MEMORYSTATUS
1328 
1329 	boolean_t dirty = FALSE, dirty_tracked = FALSE, allow_idle_exit = FALSE;
1330 	memorystatus_proc_flags_unsafe(bsd_info, &dirty, &dirty_tracked, &allow_idle_exit);
1331 	if (dirty) {
1332 		ss_flags |= kTaskIsDirty;
1333 	}
1334 	if (dirty_tracked) {
1335 		ss_flags |= kTaskIsDirtyTracked;
1336 	}
1337 	if (allow_idle_exit) {
1338 		ss_flags |= kTaskAllowIdleExit;
1339 	}
1340 
1341 #endif
1342 	if (task->effective_policy.tep_tal_engaged) {
1343 		ss_flags |= kTaskTALEngaged;
1344 	}
1345 
1346 	ss_flags |= (0x7 & workqueue_get_pwq_state_kdp(bsd_info)) << 17;
1347 
1348 #if IMPORTANCE_INHERITANCE
1349 	if (task->task_imp_base) {
1350 		if (task->task_imp_base->iit_donor) {
1351 			ss_flags |= kTaskIsImpDonor;
1352 		}
1353 		if (task->task_imp_base->iit_live_donor) {
1354 			ss_flags |= kTaskIsLiveImpDonor;
1355 		}
1356 	}
1357 #endif
1358 	return ss_flags;
1359 }
1360 
1361 static kern_return_t
kcdata_record_shared_cache_info(kcdata_descriptor_t kcd,task_t task,unaligned_u64 * task_snap_ss_flags)1362 kcdata_record_shared_cache_info(kcdata_descriptor_t kcd, task_t task, unaligned_u64 *task_snap_ss_flags)
1363 {
1364 	kern_return_t error = KERN_SUCCESS;
1365 
1366 	uint64_t shared_cache_slide = 0;
1367 	uint64_t shared_cache_first_mapping = 0;
1368 	uint32_t kdp_fault_results = 0;
1369 	uint32_t shared_cache_id = 0;
1370 	struct dyld_shared_cache_loadinfo shared_cache_data = {0};
1371 
1372 
1373 	assert(task_snap_ss_flags != NULL);
1374 
1375 	/* Get basic info about the shared region pointer, regardless of any failures */
1376 	if (task->shared_region == NULL) {
1377 		*task_snap_ss_flags |= kTaskSharedRegionNone;
1378 	} else if (task->shared_region == primary_system_shared_region) {
1379 		*task_snap_ss_flags |= kTaskSharedRegionSystem;
1380 	} else {
1381 		*task_snap_ss_flags |= kTaskSharedRegionOther;
1382 	}
1383 
1384 	if (task->shared_region && _stackshot_validate_kva((vm_offset_t)task->shared_region, sizeof(struct vm_shared_region))) {
1385 		struct vm_shared_region *sr = task->shared_region;
1386 		shared_cache_first_mapping = sr->sr_base_address + sr->sr_first_mapping;
1387 
1388 		shared_cache_id = sr->sr_id;
1389 	} else {
1390 		*task_snap_ss_flags |= kTaskSharedRegionInfoUnavailable;
1391 		goto error_exit;
1392 	}
1393 
1394 	/* We haven't copied in the shared region UUID yet as part of setup */
1395 	if (!shared_cache_first_mapping || !task->shared_region->sr_uuid_copied) {
1396 		goto error_exit;
1397 	}
1398 
1399 
1400 	/*
1401 	 * No refcounting here, but we are in debugger context, so that should be safe.
1402 	 */
1403 	shared_cache_slide = task->shared_region->sr_slide;
1404 
1405 	if (task->shared_region == primary_system_shared_region) {
1406 		/* skip adding shared cache info -- it's the same as the system level one */
1407 		goto error_exit;
1408 	}
1409 	/*
1410 	 * New-style shared cache reference: for non-primary shared regions,
1411 	 * just include the ID of the shared cache we're attached to.  Consumers
1412 	 * should use the following info from the task's ts_ss_flags as well:
1413 	 *
1414 	 * kTaskSharedRegionNone - task is not attached to a shared region
1415 	 * kTaskSharedRegionSystem - task is attached to the shared region
1416 	 *     with kSharedCacheSystemPrimary set in sharedCacheFlags.
1417 	 * kTaskSharedRegionOther - task is attached to the shared region with
1418 	 *     sharedCacheID matching the STACKSHOT_KCTYPE_SHAREDCACHE_ID entry.
1419 	 */
1420 	kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_SHAREDCACHE_ID, sizeof(shared_cache_id), &shared_cache_id));
1421 
1422 	/*
1423 	 * For backwards compatibility; this should eventually be removed.
1424 	 *
1425 	 * Historically, this data was in a dyld_uuid_info_64 structure, but the
1426 	 * naming of both the structure and fields for this use wasn't great.  The
1427 	 * dyld_shared_cache_loadinfo structure has better names, but the same
1428 	 * layout and content as the original.
1429 	 *
1430 	 * The imageSlidBaseAddress/sharedCacheUnreliableSlidBaseAddress field
1431 	 * has been used inconsistently for STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT
1432 	 * entries; here, it's the slid first mapping, and we leave it that way
1433 	 * for backwards compatibility.
1434 	 */
1435 	shared_cache_data.sharedCacheSlide = shared_cache_slide;
1436 	kdp_memcpy(&shared_cache_data.sharedCacheUUID, task->shared_region->sr_uuid, sizeof(task->shared_region->sr_uuid));
1437 	shared_cache_data.sharedCacheUnreliableSlidBaseAddress = shared_cache_first_mapping;
1438 	shared_cache_data.sharedCacheSlidFirstMapping = shared_cache_first_mapping;
1439 	kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO, sizeof(shared_cache_data), &shared_cache_data));
1440 
1441 error_exit:
1442 	if (kdp_fault_results & KDP_FAULT_RESULT_PAGED_OUT) {
1443 		*task_snap_ss_flags |= kTaskUUIDInfoMissing;
1444 	}
1445 
1446 	if (kdp_fault_results & KDP_FAULT_RESULT_TRIED_FAULT) {
1447 		*task_snap_ss_flags |= kTaskUUIDInfoTriedFault;
1448 	}
1449 
1450 	if (kdp_fault_results & KDP_FAULT_RESULT_FAULTED_IN) {
1451 		*task_snap_ss_flags |= kTaskUUIDInfoFaultedIn;
1452 	}
1453 
1454 	return error;
1455 }
1456 
1457 static kern_return_t
kcdata_record_uuid_info(kcdata_descriptor_t kcd,task_t task,uint64_t trace_flags,boolean_t have_pmap,unaligned_u64 * task_snap_ss_flags)1458 kcdata_record_uuid_info(kcdata_descriptor_t kcd, task_t task, uint64_t trace_flags, boolean_t have_pmap, unaligned_u64 *task_snap_ss_flags)
1459 {
1460 	bool save_loadinfo_p         = ((trace_flags & STACKSHOT_SAVE_LOADINFO) != 0);
1461 	bool save_kextloadinfo_p     = ((trace_flags & STACKSHOT_SAVE_KEXT_LOADINFO) != 0);
1462 	bool save_compactinfo_p      = ((trace_flags & STACKSHOT_SAVE_DYLD_COMPACTINFO) != 0);
1463 	bool should_fault            = (trace_flags & STACKSHOT_ENABLE_UUID_FAULTING);
1464 
1465 	kern_return_t error        = KERN_SUCCESS;
1466 	mach_vm_address_t out_addr = 0;
1467 
1468 	mach_vm_address_t dyld_compactinfo_addr = 0;
1469 	uint32_t dyld_compactinfo_size = 0;
1470 
1471 	uint32_t uuid_info_count         = 0;
1472 	mach_vm_address_t uuid_info_addr = 0;
1473 	uint64_t uuid_info_timestamp     = 0;
1474 	kdp_fault_result_flags_t kdp_fault_results = 0;
1475 
1476 
1477 	assert(task_snap_ss_flags != NULL);
1478 
1479 	int task_pid     = pid_from_task(task);
1480 	boolean_t task_64bit_addr = task_has_64Bit_addr(task);
1481 
1482 	if ((save_loadinfo_p || save_compactinfo_p) && have_pmap && task->active && task_pid > 0) {
1483 		/* Read the dyld_all_image_infos struct from the task memory to get UUID array count and location */
1484 		if (task_64bit_addr) {
1485 			struct user64_dyld_all_image_infos task_image_infos;
1486 			if (stackshot_copyin(task->map, task->all_image_info_addr, &task_image_infos,
1487 			    sizeof(struct user64_dyld_all_image_infos), should_fault, &kdp_fault_results)) {
1488 				uuid_info_count = (uint32_t)task_image_infos.uuidArrayCount;
1489 				uuid_info_addr = task_image_infos.uuidArray;
1490 				if (task_image_infos.version >= DYLD_ALL_IMAGE_INFOS_TIMESTAMP_MINIMUM_VERSION) {
1491 					uuid_info_timestamp = task_image_infos.timestamp;
1492 				}
1493 				if (task_image_infos.version >= DYLD_ALL_IMAGE_INFOS_COMPACTINFO_MINIMUM_VERSION) {
1494 					dyld_compactinfo_addr = task_image_infos.compact_dyld_image_info_addr;
1495 					dyld_compactinfo_size = task_image_infos.compact_dyld_image_info_size;
1496 				}
1497 
1498 			}
1499 		} else {
1500 			struct user32_dyld_all_image_infos task_image_infos;
1501 			if (stackshot_copyin(task->map, task->all_image_info_addr, &task_image_infos,
1502 			    sizeof(struct user32_dyld_all_image_infos), should_fault, &kdp_fault_results)) {
1503 				uuid_info_count = task_image_infos.uuidArrayCount;
1504 				uuid_info_addr = task_image_infos.uuidArray;
1505 				if (task_image_infos.version >= DYLD_ALL_IMAGE_INFOS_TIMESTAMP_MINIMUM_VERSION) {
1506 					uuid_info_timestamp = task_image_infos.timestamp;
1507 				}
1508 				if (task_image_infos.version >= DYLD_ALL_IMAGE_INFOS_COMPACTINFO_MINIMUM_VERSION) {
1509 					dyld_compactinfo_addr = task_image_infos.compact_dyld_image_info_addr;
1510 					dyld_compactinfo_size = task_image_infos.compact_dyld_image_info_size;
1511 				}
1512 			}
1513 		}
1514 
1515 		/*
1516 		 * If we get a NULL uuid_info_addr (which can happen when we catch dyld in the middle of updating
1517 		 * this data structure), we zero the uuid_info_count so that we won't even try to save load info
1518 		 * for this task.
1519 		 */
1520 		if (!uuid_info_addr) {
1521 			uuid_info_count = 0;
1522 		}
1523 
1524 		if (!dyld_compactinfo_addr) {
1525 			dyld_compactinfo_size = 0;
1526 		}
1527 
1528 	}
1529 
1530 	if (have_pmap && task_pid == 0) {
1531 		if (save_kextloadinfo_p && _stackshot_validate_kva((vm_offset_t)(gLoadedKextSummaries), sizeof(OSKextLoadedKextSummaryHeader))) {
1532 			uuid_info_count = gLoadedKextSummaries->numSummaries + 1; /* include main kernel UUID */
1533 		} else {
1534 			uuid_info_count = 1; /* include kernelcache UUID (embedded) or kernel UUID (desktop) */
1535 		}
1536 	}
1537 
1538 	if (save_compactinfo_p && task_pid > 0) {
1539 		if (dyld_compactinfo_size == 0) {
1540 			*task_snap_ss_flags |= kTaskDyldCompactInfoNone;
1541 		} else if (dyld_compactinfo_size > MAX_DYLD_COMPACTINFO) {
1542 			*task_snap_ss_flags |= kTaskDyldCompactInfoTooBig;
1543 		} else {
1544 			kdp_fault_result_flags_t ci_kdp_fault_results = 0;
1545 
1546 			/* Open a compression window to avoid overflowing the stack */
1547 			kcdata_compression_window_open(kcd);
1548 			kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_DYLD_COMPACTINFO,
1549 			    dyld_compactinfo_size, &out_addr));
1550 
1551 			if (!stackshot_copyin(task->map, dyld_compactinfo_addr, (void *)out_addr,
1552 			    dyld_compactinfo_size, should_fault, &ci_kdp_fault_results)) {
1553 				bzero((void *)out_addr, dyld_compactinfo_size);
1554 			}
1555 			if (ci_kdp_fault_results & KDP_FAULT_RESULT_PAGED_OUT) {
1556 				*task_snap_ss_flags |= kTaskDyldCompactInfoMissing;
1557 			}
1558 
1559 			if (ci_kdp_fault_results & KDP_FAULT_RESULT_TRIED_FAULT) {
1560 				*task_snap_ss_flags |= kTaskDyldCompactInfoTriedFault;
1561 			}
1562 
1563 			if (ci_kdp_fault_results & KDP_FAULT_RESULT_FAULTED_IN) {
1564 				*task_snap_ss_flags |= kTaskDyldCompactInfoFaultedIn;
1565 			}
1566 
1567 			kcd_exit_on_error(kcdata_compression_window_close(kcd));
1568 		}
1569 	}
1570 	if (save_loadinfo_p && task_pid > 0 && (uuid_info_count < MAX_LOADINFOS)) {
1571 		uint32_t copied_uuid_count = 0;
1572 		uint32_t uuid_info_size = (uint32_t)(task_64bit_addr ? sizeof(struct user64_dyld_uuid_info) : sizeof(struct user32_dyld_uuid_info));
1573 		uint32_t uuid_info_array_size = 0;
1574 
1575 		/* Open a compression window to avoid overflowing the stack */
1576 		kcdata_compression_window_open(kcd);
1577 
1578 		/* If we found some UUID information, first try to copy it in -- this will only be non-zero if we had a pmap above */
1579 		if (uuid_info_count > 0) {
1580 			uuid_info_array_size = uuid_info_count * uuid_info_size;
1581 
1582 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, (task_64bit_addr ? KCDATA_TYPE_LIBRARY_LOADINFO64 : KCDATA_TYPE_LIBRARY_LOADINFO),
1583 			    uuid_info_size, uuid_info_count, &out_addr));
1584 
1585 			if (!stackshot_copyin(task->map, uuid_info_addr, (void *)out_addr, uuid_info_array_size, should_fault, &kdp_fault_results)) {
1586 				bzero((void *)out_addr, uuid_info_array_size);
1587 			} else {
1588 				copied_uuid_count = uuid_info_count;
1589 			}
1590 		}
1591 
1592 		uuid_t binary_uuid;
1593 		if (!copied_uuid_count && proc_binary_uuid_kdp(task, binary_uuid)) {
1594 			/* We failed to copyin the UUID information, try to store the UUID of the main binary we have in the proc */
1595 			if (uuid_info_array_size == 0) {
1596 				/* We just need to store one UUID */
1597 				uuid_info_array_size = uuid_info_size;
1598 				kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, (task_64bit_addr ? KCDATA_TYPE_LIBRARY_LOADINFO64 : KCDATA_TYPE_LIBRARY_LOADINFO),
1599 				    uuid_info_size, 1, &out_addr));
1600 			}
1601 
1602 			if (task_64bit_addr) {
1603 				struct user64_dyld_uuid_info *uuid_info = (struct user64_dyld_uuid_info *)out_addr;
1604 				uint64_t image_load_address = task->mach_header_vm_address;
1605 
1606 				kdp_memcpy(&uuid_info->imageUUID, binary_uuid, sizeof(uuid_t));
1607 				kdp_memcpy(&uuid_info->imageLoadAddress, &image_load_address, sizeof(image_load_address));
1608 			} else {
1609 				struct user32_dyld_uuid_info *uuid_info = (struct user32_dyld_uuid_info *)out_addr;
1610 				uint32_t image_load_address = (uint32_t) task->mach_header_vm_address;
1611 
1612 				kdp_memcpy(&uuid_info->imageUUID, binary_uuid, sizeof(uuid_t));
1613 				kdp_memcpy(&uuid_info->imageLoadAddress, &image_load_address, sizeof(image_load_address));
1614 			}
1615 		}
1616 
1617 		kcd_exit_on_error(kcdata_compression_window_close(kcd));
1618 	} else if (task_pid == 0 && uuid_info_count > 0 && uuid_info_count < MAX_LOADINFOS) {
1619 		uintptr_t image_load_address;
1620 
1621 		do {
1622 #if defined(__arm64__)
1623 			if (kernelcache_uuid_valid && !save_kextloadinfo_p) {
1624 				struct dyld_uuid_info_64 kc_uuid = {0};
1625 				kc_uuid.imageLoadAddress = VM_MIN_KERNEL_AND_KEXT_ADDRESS;
1626 				kdp_memcpy(&kc_uuid.imageUUID, &kernelcache_uuid, sizeof(uuid_t));
1627 				kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO, sizeof(struct dyld_uuid_info_64), &kc_uuid));
1628 				break;
1629 			}
1630 #endif /* defined(__arm64__) */
1631 
1632 			if (!kernel_uuid || !_stackshot_validate_kva((vm_offset_t)kernel_uuid, sizeof(uuid_t))) {
1633 				/* Kernel UUID not found or inaccessible */
1634 				break;
1635 			}
1636 
1637 			uint32_t uuid_type = KCDATA_TYPE_LIBRARY_LOADINFO;
1638 			if ((sizeof(kernel_uuid_info) == sizeof(struct user64_dyld_uuid_info))) {
1639 				uuid_type = KCDATA_TYPE_LIBRARY_LOADINFO64;
1640 #if  defined(__arm64__)
1641 				kc_format_t primary_kc_type = KCFormatUnknown;
1642 				if (PE_get_primary_kc_format(&primary_kc_type) && (primary_kc_type == KCFormatFileset)) {
1643 					/* return TEXT_EXEC based load information on arm devices running with fileset kernelcaches */
1644 					uuid_type = STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC;
1645 				}
1646 #endif
1647 			}
1648 
1649 			/*
1650 			 * The element count of the array can vary - avoid overflowing the
1651 			 * stack by opening a window.
1652 			 */
1653 			kcdata_compression_window_open(kcd);
1654 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, uuid_type,
1655 			    sizeof(kernel_uuid_info), uuid_info_count, &out_addr));
1656 			kernel_uuid_info *uuid_info_array = (kernel_uuid_info *)out_addr;
1657 
1658 			image_load_address = (uintptr_t)VM_KERNEL_UNSLIDE(vm_kernel_stext);
1659 #if defined(__arm64__)
1660 			if (uuid_type == STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC) {
1661 				/* If we're reporting TEXT_EXEC load info, populate the TEXT_EXEC base instead */
1662 				extern vm_offset_t segTEXTEXECB;
1663 				image_load_address = (uintptr_t)VM_KERNEL_UNSLIDE(segTEXTEXECB);
1664 			}
1665 #endif
1666 			uuid_info_array[0].imageLoadAddress = image_load_address;
1667 			kdp_memcpy(&uuid_info_array[0].imageUUID, kernel_uuid, sizeof(uuid_t));
1668 
1669 			if (save_kextloadinfo_p &&
1670 			    _stackshot_validate_kva((vm_offset_t)(gLoadedKextSummaries), sizeof(OSKextLoadedKextSummaryHeader)) &&
1671 			    _stackshot_validate_kva((vm_offset_t)(&gLoadedKextSummaries->summaries[0]),
1672 			    gLoadedKextSummaries->entry_size * gLoadedKextSummaries->numSummaries)) {
1673 				uint32_t kexti;
1674 				for (kexti = 0; kexti < gLoadedKextSummaries->numSummaries; kexti++) {
1675 					image_load_address = (uintptr_t)VM_KERNEL_UNSLIDE(gLoadedKextSummaries->summaries[kexti].address);
1676 #if defined(__arm64__)
1677 					if (uuid_type == STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC) {
1678 						/* If we're reporting TEXT_EXEC load info, populate the TEXT_EXEC base instead */
1679 						image_load_address = (uintptr_t)VM_KERNEL_UNSLIDE(gLoadedKextSummaries->summaries[kexti].text_exec_address);
1680 					}
1681 #endif
1682 					uuid_info_array[kexti + 1].imageLoadAddress = image_load_address;
1683 					kdp_memcpy(&uuid_info_array[kexti + 1].imageUUID, &gLoadedKextSummaries->summaries[kexti].uuid, sizeof(uuid_t));
1684 				}
1685 			}
1686 			kcd_exit_on_error(kcdata_compression_window_close(kcd));
1687 		} while (0);
1688 	}
1689 
1690 error_exit:
1691 	if (kdp_fault_results & KDP_FAULT_RESULT_PAGED_OUT) {
1692 		*task_snap_ss_flags |= kTaskUUIDInfoMissing;
1693 	}
1694 
1695 	if (kdp_fault_results & KDP_FAULT_RESULT_TRIED_FAULT) {
1696 		*task_snap_ss_flags |= kTaskUUIDInfoTriedFault;
1697 	}
1698 
1699 	if (kdp_fault_results & KDP_FAULT_RESULT_FAULTED_IN) {
1700 		*task_snap_ss_flags |= kTaskUUIDInfoFaultedIn;
1701 	}
1702 
1703 	return error;
1704 }
1705 
1706 static kern_return_t
kcdata_record_task_iostats(kcdata_descriptor_t kcd,task_t task)1707 kcdata_record_task_iostats(kcdata_descriptor_t kcd, task_t task)
1708 {
1709 	kern_return_t error = KERN_SUCCESS;
1710 	mach_vm_address_t out_addr = 0;
1711 
1712 	/* I/O Statistics if any counters are non zero */
1713 	assert(IO_NUM_PRIORITIES == STACKSHOT_IO_NUM_PRIORITIES);
1714 	if (task->task_io_stats && !memory_iszero(task->task_io_stats, sizeof(struct io_stat_info))) {
1715 		/* struct io_stats_snapshot is quite large - avoid overflowing the stack. */
1716 		kcdata_compression_window_open(kcd);
1717 		kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_IOSTATS, sizeof(struct io_stats_snapshot), &out_addr));
1718 		struct io_stats_snapshot *_iostat = (struct io_stats_snapshot *)out_addr;
1719 		_iostat->ss_disk_reads_count = task->task_io_stats->disk_reads.count;
1720 		_iostat->ss_disk_reads_size = task->task_io_stats->disk_reads.size;
1721 		_iostat->ss_disk_writes_count = (task->task_io_stats->total_io.count - task->task_io_stats->disk_reads.count);
1722 		_iostat->ss_disk_writes_size = (task->task_io_stats->total_io.size - task->task_io_stats->disk_reads.size);
1723 		_iostat->ss_paging_count = task->task_io_stats->paging.count;
1724 		_iostat->ss_paging_size = task->task_io_stats->paging.size;
1725 		_iostat->ss_non_paging_count = (task->task_io_stats->total_io.count - task->task_io_stats->paging.count);
1726 		_iostat->ss_non_paging_size = (task->task_io_stats->total_io.size - task->task_io_stats->paging.size);
1727 		_iostat->ss_metadata_count = task->task_io_stats->metadata.count;
1728 		_iostat->ss_metadata_size = task->task_io_stats->metadata.size;
1729 		_iostat->ss_data_count = (task->task_io_stats->total_io.count - task->task_io_stats->metadata.count);
1730 		_iostat->ss_data_size = (task->task_io_stats->total_io.size - task->task_io_stats->metadata.size);
1731 		for (int i = 0; i < IO_NUM_PRIORITIES; i++) {
1732 			_iostat->ss_io_priority_count[i] = task->task_io_stats->io_priority[i].count;
1733 			_iostat->ss_io_priority_size[i] = task->task_io_stats->io_priority[i].size;
1734 		}
1735 		kcd_exit_on_error(kcdata_compression_window_close(kcd));
1736 	}
1737 
1738 
1739 error_exit:
1740 	return error;
1741 }
1742 
1743 #if CONFIG_PERVASIVE_CPI
1744 static kern_return_t
kcdata_record_task_instrs_cycles(kcdata_descriptor_t kcd,task_t task)1745 kcdata_record_task_instrs_cycles(kcdata_descriptor_t kcd, task_t task)
1746 {
1747 	struct instrs_cycles_snapshot_v2 instrs_cycles = { 0 };
1748 	struct recount_usage usage = { 0 };
1749 	struct recount_usage perf_only = { 0 };
1750 	recount_task_terminated_usage_perf_only(task, &usage, &perf_only);
1751 	instrs_cycles.ics_instructions = usage.ru_instructions;
1752 	instrs_cycles.ics_cycles = usage.ru_cycles;
1753 	instrs_cycles.ics_p_instructions = perf_only.ru_instructions;
1754 	instrs_cycles.ics_p_cycles = perf_only.ru_cycles;
1755 
1756 	return kcdata_push_data(kcd, STACKSHOT_KCTYPE_INSTRS_CYCLES, sizeof(instrs_cycles), &instrs_cycles);
1757 }
1758 #endif /* CONFIG_PERVASIVE_CPI */
1759 
1760 static kern_return_t
kcdata_record_task_cpu_architecture(kcdata_descriptor_t kcd,task_t task)1761 kcdata_record_task_cpu_architecture(kcdata_descriptor_t kcd, task_t task)
1762 {
1763 	struct stackshot_cpu_architecture cpu_architecture = {0};
1764 	int32_t cputype;
1765 	int32_t cpusubtype;
1766 
1767 	proc_archinfo_kdp(get_bsdtask_info(task), &cputype, &cpusubtype);
1768 	cpu_architecture.cputype = cputype;
1769 	cpu_architecture.cpusubtype = cpusubtype;
1770 
1771 	return kcdata_push_data(kcd, STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE, sizeof(struct stackshot_cpu_architecture), &cpu_architecture);
1772 }
1773 
1774 static kern_return_t
kcdata_record_task_codesigning_info(kcdata_descriptor_t kcd,task_t task)1775 kcdata_record_task_codesigning_info(kcdata_descriptor_t kcd, task_t task)
1776 {
1777 	struct stackshot_task_codesigning_info codesigning_info = {};
1778 	void * bsdtask_info = NULL;
1779 	uint32_t trust = 0;
1780 	kern_return_t ret = 0;
1781 	pmap_t pmap = get_task_pmap(task);
1782 	if (task != kernel_task) {
1783 		bsdtask_info = get_bsdtask_info(task);
1784 		codesigning_info.csflags = proc_getcsflags_kdp(bsdtask_info);
1785 		ret = get_trust_level_kdp(pmap, &trust);
1786 		if (ret != KERN_SUCCESS) {
1787 			trust = KCDATA_INVALID_CS_TRUST_LEVEL;
1788 		}
1789 		codesigning_info.cs_trust_level = trust;
1790 	} else {
1791 		return KERN_SUCCESS;
1792 	}
1793 	return kcdata_push_data(kcd, STACKSHOT_KCTYPE_CODESIGNING_INFO, sizeof(struct stackshot_task_codesigning_info), &codesigning_info);
1794 }
1795 #if CONFIG_TASK_SUSPEND_STATS
1796 static kern_return_t
kcdata_record_task_suspension_info(kcdata_descriptor_t kcd,task_t task)1797 kcdata_record_task_suspension_info(kcdata_descriptor_t kcd, task_t task)
1798 {
1799 	kern_return_t ret = KERN_SUCCESS;
1800 	struct stackshot_suspension_info suspension_info = {};
1801 	task_suspend_stats_data_t suspend_stats;
1802 	task_suspend_source_array_t suspend_sources;
1803 	struct stackshot_suspension_source suspension_sources[TASK_SUSPEND_SOURCES_MAX];
1804 	int i;
1805 
1806 	if (task == kernel_task) {
1807 		return KERN_SUCCESS;
1808 	}
1809 
1810 	ret = task_get_suspend_stats_kdp(task, &suspend_stats);
1811 	if (ret != KERN_SUCCESS) {
1812 		return ret;
1813 	}
1814 
1815 	suspension_info.tss_count = suspend_stats.tss_count;
1816 	suspension_info.tss_duration = suspend_stats.tss_duration;
1817 	suspension_info.tss_last_end = suspend_stats.tss_last_end;
1818 	suspension_info.tss_last_start = suspend_stats.tss_last_start;
1819 	ret = kcdata_push_data(kcd, STACKSHOT_KCTYPE_SUSPENSION_INFO, sizeof(suspension_info), &suspension_info);
1820 	if (ret != KERN_SUCCESS) {
1821 		return ret;
1822 	}
1823 
1824 	ret = task_get_suspend_sources_kdp(task, suspend_sources);
1825 	if (ret != KERN_SUCCESS) {
1826 		return ret;
1827 	}
1828 
1829 	for (i = 0; i < TASK_SUSPEND_SOURCES_MAX; ++i) {
1830 		suspension_sources[i].tss_pid = suspend_sources[i].tss_pid;
1831 		strlcpy(suspension_sources[i].tss_procname, suspend_sources[i].tss_procname, sizeof(suspend_sources[i].tss_procname));
1832 		suspension_sources[i].tss_tid = suspend_sources[i].tss_tid;
1833 		suspension_sources[i].tss_time = suspend_sources[i].tss_time;
1834 	}
1835 	return kcdata_push_array(kcd, STACKSHOT_KCTYPE_SUSPENSION_SOURCE, sizeof(suspension_sources[0]), TASK_SUSPEND_SOURCES_MAX, &suspension_sources);
1836 }
1837 #endif /* CONFIG_TASK_SUSPEND_STATS */
1838 
1839 static kern_return_t
kcdata_record_transitioning_task_snapshot(kcdata_descriptor_t kcd,task_t task,unaligned_u64 task_snap_ss_flags,uint64_t transition_type)1840 kcdata_record_transitioning_task_snapshot(kcdata_descriptor_t kcd, task_t task, unaligned_u64 task_snap_ss_flags, uint64_t transition_type)
1841 {
1842 	kern_return_t error                 = KERN_SUCCESS;
1843 	mach_vm_address_t out_addr          = 0;
1844 	struct transitioning_task_snapshot * cur_tsnap = NULL;
1845 
1846 	int task_pid           = pid_from_task(task);
1847 	/* Is returning -1 ok for terminating task ok ??? */
1848 	uint64_t task_uniqueid = get_task_uniqueid(task);
1849 
1850 	if (task_pid && (task_did_exec_internal(task) || task_is_exec_copy_internal(task))) {
1851 		/*
1852 		 * if this task is a transit task from another one, show the pid as
1853 		 * negative
1854 		 */
1855 		task_pid = 0 - task_pid;
1856 	}
1857 
1858 	/* the task_snapshot_v2 struct is large - avoid overflowing the stack */
1859 	kcdata_compression_window_open(kcd);
1860 	kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_TRANSITIONING_TASK_SNAPSHOT, sizeof(struct transitioning_task_snapshot), &out_addr));
1861 	cur_tsnap = (struct transitioning_task_snapshot *)out_addr;
1862 	bzero(cur_tsnap, sizeof(*cur_tsnap));
1863 
1864 	cur_tsnap->tts_unique_pid = task_uniqueid;
1865 	cur_tsnap->tts_ss_flags = kcdata_get_task_ss_flags(task);
1866 	cur_tsnap->tts_ss_flags |= task_snap_ss_flags;
1867 	cur_tsnap->tts_transition_type = transition_type;
1868 	cur_tsnap->tts_pid = task_pid;
1869 
1870 	/* Add the BSD process identifiers */
1871 	if (task_pid != -1 && get_bsdtask_info(task) != NULL) {
1872 		proc_name_kdp(get_bsdtask_info(task), cur_tsnap->tts_p_comm, sizeof(cur_tsnap->tts_p_comm));
1873 	} else {
1874 		cur_tsnap->tts_p_comm[0] = '\0';
1875 	}
1876 
1877 	kcd_exit_on_error(kcdata_compression_window_close(kcd));
1878 
1879 error_exit:
1880 	return error;
1881 }
1882 
1883 static kern_return_t
1884 #if STACKSHOT_COLLECTS_LATENCY_INFO
kcdata_record_task_snapshot(kcdata_descriptor_t kcd,task_t task,uint64_t trace_flags,boolean_t have_pmap,unaligned_u64 task_snap_ss_flags,struct stackshot_latency_task * latency_info)1885 kcdata_record_task_snapshot(kcdata_descriptor_t kcd, task_t task, uint64_t trace_flags, boolean_t have_pmap, unaligned_u64 task_snap_ss_flags, struct stackshot_latency_task *latency_info)
1886 #else
1887 kcdata_record_task_snapshot(kcdata_descriptor_t kcd, task_t task, uint64_t trace_flags, boolean_t have_pmap, unaligned_u64 task_snap_ss_flags)
1888 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
1889 {
1890 	bool collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0);
1891 	bool collect_iostats         = !collect_delta_stackshot && !(trace_flags & STACKSHOT_NO_IO_STATS);
1892 #if CONFIG_PERVASIVE_CPI
1893 	bool collect_instrs_cycles   = ((trace_flags & STACKSHOT_INSTRS_CYCLES) != 0);
1894 #endif /* CONFIG_PERVASIVE_CPI */
1895 #if __arm64__
1896 	bool collect_asid            = ((trace_flags & STACKSHOT_ASID) != 0);
1897 #endif
1898 	bool collect_pagetables      = ((trace_flags & STACKSHOT_PAGE_TABLES) != 0);
1899 
1900 
1901 	kern_return_t error                 = KERN_SUCCESS;
1902 	mach_vm_address_t out_addr          = 0;
1903 	struct task_snapshot_v2 * cur_tsnap = NULL;
1904 #if STACKSHOT_COLLECTS_LATENCY_INFO
1905 	latency_info->cur_tsnap_latency = mach_absolute_time();
1906 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
1907 
1908 	int task_pid           = pid_from_task(task);
1909 	uint64_t task_uniqueid = get_task_uniqueid(task);
1910 	void *bsd_info = get_bsdtask_info(task);
1911 	uint64_t proc_starttime_secs = 0;
1912 
1913 	if (task_pid && (task_did_exec_internal(task) || task_is_exec_copy_internal(task))) {
1914 		/*
1915 		 * if this task is a transit task from another one, show the pid as
1916 		 * negative
1917 		 */
1918 		task_pid = 0 - task_pid;
1919 	}
1920 
1921 	/* the task_snapshot_v2 struct is large - avoid overflowing the stack */
1922 	kcdata_compression_window_open(kcd);
1923 	kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_TASK_SNAPSHOT, sizeof(struct task_snapshot_v2), &out_addr));
1924 	cur_tsnap = (struct task_snapshot_v2 *)out_addr;
1925 	bzero(cur_tsnap, sizeof(*cur_tsnap));
1926 
1927 	cur_tsnap->ts_unique_pid = task_uniqueid;
1928 	cur_tsnap->ts_ss_flags = kcdata_get_task_ss_flags(task);
1929 	cur_tsnap->ts_ss_flags |= task_snap_ss_flags;
1930 
1931 	struct recount_usage term_usage = { 0 };
1932 	recount_task_terminated_usage(task, &term_usage);
1933 	cur_tsnap->ts_user_time_in_terminated_threads =
1934 	    term_usage.ru_user_time_mach;
1935 	cur_tsnap->ts_system_time_in_terminated_threads =
1936 	    term_usage.ru_system_time_mach;
1937 
1938 	proc_starttime_kdp(bsd_info, &proc_starttime_secs, NULL, NULL);
1939 	cur_tsnap->ts_p_start_sec = proc_starttime_secs;
1940 	cur_tsnap->ts_task_size = have_pmap ? get_task_phys_footprint(task) : 0;
1941 	cur_tsnap->ts_max_resident_size = get_task_resident_max(task);
1942 	cur_tsnap->ts_was_throttled = (uint32_t) proc_was_throttled_from_task(task);
1943 	cur_tsnap->ts_did_throttle = (uint32_t) proc_did_throttle_from_task(task);
1944 
1945 	cur_tsnap->ts_suspend_count = task->suspend_count;
1946 	cur_tsnap->ts_faults = counter_load(&task->faults);
1947 	cur_tsnap->ts_pageins = counter_load(&task->pageins);
1948 	cur_tsnap->ts_cow_faults = counter_load(&task->cow_faults);
1949 	cur_tsnap->ts_latency_qos = (task->effective_policy.tep_latency_qos == LATENCY_QOS_TIER_UNSPECIFIED) ?
1950 	    LATENCY_QOS_TIER_UNSPECIFIED : ((0xFF << 16) | task->effective_policy.tep_latency_qos);
1951 	cur_tsnap->ts_pid = task_pid;
1952 
1953 	/* Add the BSD process identifiers */
1954 	if (task_pid != -1 && bsd_info != NULL) {
1955 		proc_name_kdp(bsd_info, cur_tsnap->ts_p_comm, sizeof(cur_tsnap->ts_p_comm));
1956 	} else {
1957 		cur_tsnap->ts_p_comm[0] = '\0';
1958 #if IMPORTANCE_INHERITANCE && (DEVELOPMENT || DEBUG)
1959 		if (task->task_imp_base != NULL) {
1960 			kdp_strlcpy(cur_tsnap->ts_p_comm, &task->task_imp_base->iit_procname[0],
1961 			    MIN((int)sizeof(task->task_imp_base->iit_procname), (int)sizeof(cur_tsnap->ts_p_comm)));
1962 		}
1963 #endif /* IMPORTANCE_INHERITANCE && (DEVELOPMENT || DEBUG) */
1964 	}
1965 
1966 	kcd_exit_on_error(kcdata_compression_window_close(kcd));
1967 
1968 #if CONFIG_COALITIONS
1969 	if (task_pid != -1 && bsd_info != NULL &&
1970 	    (task->coalition[COALITION_TYPE_JETSAM] != NULL)) {
1971 		/*
1972 		 * The jetsam coalition ID is always saved, even if
1973 		 * STACKSHOT_SAVE_JETSAM_COALITIONS is not set.
1974 		 */
1975 		uint64_t jetsam_coal_id = coalition_id(task->coalition[COALITION_TYPE_JETSAM]);
1976 		kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_JETSAM_COALITION, sizeof(jetsam_coal_id), &jetsam_coal_id));
1977 	}
1978 #endif /* CONFIG_COALITIONS */
1979 
1980 #if __arm64__
1981 	if (collect_asid && have_pmap) {
1982 		uint32_t asid = PMAP_VASID(task->map->pmap);
1983 		kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_ASID, sizeof(asid), &asid));
1984 	}
1985 #endif
1986 
1987 #if STACKSHOT_COLLECTS_LATENCY_INFO
1988 	latency_info->cur_tsnap_latency = mach_absolute_time() - latency_info->cur_tsnap_latency;
1989 	latency_info->pmap_latency = mach_absolute_time();
1990 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
1991 
1992 	if (collect_pagetables && have_pmap) {
1993 #if SCHED_HYGIENE_DEBUG
1994 		// pagetable dumps can be large; reset the interrupt timeout to avoid a panic
1995 		ml_spin_debug_clear_self();
1996 #endif
1997 		size_t bytes_dumped = 0;
1998 		error = pmap_dump_page_tables(task->map->pmap, kcd_end_address(kcd), kcd_max_address(kcd), stack_snapshot_pagetable_mask, &bytes_dumped);
1999 		if (error != KERN_SUCCESS) {
2000 			goto error_exit;
2001 		} else {
2002 			/* Variable size array - better not have it on the stack. */
2003 			kcdata_compression_window_open(kcd);
2004 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, STACKSHOT_KCTYPE_PAGE_TABLES,
2005 			    sizeof(uint64_t), (uint32_t)(bytes_dumped / sizeof(uint64_t)), &out_addr));
2006 			kcd_exit_on_error(kcdata_compression_window_close(kcd));
2007 		}
2008 	}
2009 
2010 #if STACKSHOT_COLLECTS_LATENCY_INFO
2011 	latency_info->pmap_latency = mach_absolute_time() - latency_info->pmap_latency;
2012 	latency_info->bsd_proc_ids_latency = mach_absolute_time();
2013 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2014 
2015 #if STACKSHOT_COLLECTS_LATENCY_INFO
2016 	latency_info->bsd_proc_ids_latency = mach_absolute_time() - latency_info->bsd_proc_ids_latency;
2017 	latency_info->end_latency = mach_absolute_time();
2018 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2019 
2020 	if (collect_iostats) {
2021 		kcd_exit_on_error(kcdata_record_task_iostats(kcd, task));
2022 	}
2023 
2024 #if CONFIG_PERVASIVE_CPI
2025 	if (collect_instrs_cycles) {
2026 		kcd_exit_on_error(kcdata_record_task_instrs_cycles(kcd, task));
2027 	}
2028 #endif /* CONFIG_PERVASIVE_CPI */
2029 
2030 	kcd_exit_on_error(kcdata_record_task_cpu_architecture(kcd, task));
2031 	kcd_exit_on_error(kcdata_record_task_codesigning_info(kcd, task));
2032 
2033 #if CONFIG_TASK_SUSPEND_STATS
2034 	kcd_exit_on_error(kcdata_record_task_suspension_info(kcd, task));
2035 #endif /* CONFIG_TASK_SUSPEND_STATS */
2036 
2037 #if STACKSHOT_COLLECTS_LATENCY_INFO
2038 	latency_info->end_latency = mach_absolute_time() - latency_info->end_latency;
2039 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2040 
2041 error_exit:
2042 	return error;
2043 }
2044 
2045 static kern_return_t
kcdata_record_task_delta_snapshot(kcdata_descriptor_t kcd,task_t task,uint64_t trace_flags,boolean_t have_pmap,unaligned_u64 task_snap_ss_flags)2046 kcdata_record_task_delta_snapshot(kcdata_descriptor_t kcd, task_t task, uint64_t trace_flags, boolean_t have_pmap, unaligned_u64 task_snap_ss_flags)
2047 {
2048 #if !CONFIG_PERVASIVE_CPI
2049 #pragma unused(trace_flags)
2050 #endif /* !CONFIG_PERVASIVE_CPI */
2051 	kern_return_t error                       = KERN_SUCCESS;
2052 	struct task_delta_snapshot_v2 * cur_tsnap = NULL;
2053 	mach_vm_address_t out_addr                = 0;
2054 	(void) trace_flags;
2055 #if __arm64__
2056 	boolean_t collect_asid                    = ((trace_flags & STACKSHOT_ASID) != 0);
2057 #endif
2058 #if CONFIG_PERVASIVE_CPI
2059 	boolean_t collect_instrs_cycles           = ((trace_flags & STACKSHOT_INSTRS_CYCLES) != 0);
2060 #endif /* CONFIG_PERVASIVE_CPI */
2061 
2062 	uint64_t task_uniqueid = get_task_uniqueid(task);
2063 
2064 	kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT, sizeof(struct task_delta_snapshot_v2), &out_addr));
2065 
2066 	cur_tsnap = (struct task_delta_snapshot_v2 *)out_addr;
2067 
2068 	cur_tsnap->tds_unique_pid = task_uniqueid;
2069 	cur_tsnap->tds_ss_flags = kcdata_get_task_ss_flags(task);
2070 	cur_tsnap->tds_ss_flags |= task_snap_ss_flags;
2071 
2072 	struct recount_usage usage = { 0 };
2073 	recount_task_terminated_usage(task, &usage);
2074 
2075 	cur_tsnap->tds_user_time_in_terminated_threads = usage.ru_user_time_mach;
2076 	cur_tsnap->tds_system_time_in_terminated_threads =
2077 	    usage.ru_system_time_mach;
2078 
2079 	cur_tsnap->tds_task_size = have_pmap ? get_task_phys_footprint(task) : 0;
2080 
2081 	cur_tsnap->tds_max_resident_size = get_task_resident_max(task);
2082 	cur_tsnap->tds_suspend_count = task->suspend_count;
2083 	cur_tsnap->tds_faults            = counter_load(&task->faults);
2084 	cur_tsnap->tds_pageins           = counter_load(&task->pageins);
2085 	cur_tsnap->tds_cow_faults        = counter_load(&task->cow_faults);
2086 	cur_tsnap->tds_was_throttled     = (uint32_t)proc_was_throttled_from_task(task);
2087 	cur_tsnap->tds_did_throttle      = (uint32_t)proc_did_throttle_from_task(task);
2088 	cur_tsnap->tds_latency_qos       = (task->effective_policy.tep_latency_qos == LATENCY_QOS_TIER_UNSPECIFIED)
2089 	    ? LATENCY_QOS_TIER_UNSPECIFIED
2090 	    : ((0xFF << 16) | task->effective_policy.tep_latency_qos);
2091 
2092 #if __arm64__
2093 	if (collect_asid && have_pmap) {
2094 		uint32_t asid = PMAP_VASID(task->map->pmap);
2095 		kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_ASID, sizeof(uint32_t), &out_addr));
2096 		kdp_memcpy((void*)out_addr, &asid, sizeof(asid));
2097 	}
2098 #endif
2099 
2100 #if CONFIG_PERVASIVE_CPI
2101 	if (collect_instrs_cycles) {
2102 		kcd_exit_on_error(kcdata_record_task_instrs_cycles(kcd, task));
2103 	}
2104 #endif /* CONFIG_PERVASIVE_CPI */
2105 
2106 error_exit:
2107 	return error;
2108 }
2109 
2110 static kern_return_t
kcdata_record_thread_iostats(kcdata_descriptor_t kcd,thread_t thread)2111 kcdata_record_thread_iostats(kcdata_descriptor_t kcd, thread_t thread)
2112 {
2113 	kern_return_t error = KERN_SUCCESS;
2114 	mach_vm_address_t out_addr = 0;
2115 
2116 	/* I/O Statistics */
2117 	assert(IO_NUM_PRIORITIES == STACKSHOT_IO_NUM_PRIORITIES);
2118 	if (thread->thread_io_stats && !memory_iszero(thread->thread_io_stats, sizeof(struct io_stat_info))) {
2119 		kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_IOSTATS, sizeof(struct io_stats_snapshot), &out_addr));
2120 		struct io_stats_snapshot *_iostat = (struct io_stats_snapshot *)out_addr;
2121 		_iostat->ss_disk_reads_count = thread->thread_io_stats->disk_reads.count;
2122 		_iostat->ss_disk_reads_size = thread->thread_io_stats->disk_reads.size;
2123 		_iostat->ss_disk_writes_count = (thread->thread_io_stats->total_io.count - thread->thread_io_stats->disk_reads.count);
2124 		_iostat->ss_disk_writes_size = (thread->thread_io_stats->total_io.size - thread->thread_io_stats->disk_reads.size);
2125 		_iostat->ss_paging_count = thread->thread_io_stats->paging.count;
2126 		_iostat->ss_paging_size = thread->thread_io_stats->paging.size;
2127 		_iostat->ss_non_paging_count = (thread->thread_io_stats->total_io.count - thread->thread_io_stats->paging.count);
2128 		_iostat->ss_non_paging_size = (thread->thread_io_stats->total_io.size - thread->thread_io_stats->paging.size);
2129 		_iostat->ss_metadata_count = thread->thread_io_stats->metadata.count;
2130 		_iostat->ss_metadata_size = thread->thread_io_stats->metadata.size;
2131 		_iostat->ss_data_count = (thread->thread_io_stats->total_io.count - thread->thread_io_stats->metadata.count);
2132 		_iostat->ss_data_size = (thread->thread_io_stats->total_io.size - thread->thread_io_stats->metadata.size);
2133 		for (int i = 0; i < IO_NUM_PRIORITIES; i++) {
2134 			_iostat->ss_io_priority_count[i] = thread->thread_io_stats->io_priority[i].count;
2135 			_iostat->ss_io_priority_size[i] = thread->thread_io_stats->io_priority[i].size;
2136 		}
2137 	}
2138 
2139 error_exit:
2140 	return error;
2141 }
2142 
2143 bool
machine_trace_thread_validate_kva(vm_offset_t addr)2144 machine_trace_thread_validate_kva(vm_offset_t addr)
2145 {
2146 	return _stackshot_validate_kva(addr, sizeof(uintptr_t));
2147 }
2148 
2149 struct _stackshot_backtrace_context {
2150 	vm_map_t sbc_map;
2151 	vm_offset_t sbc_prev_page;
2152 	vm_offset_t sbc_prev_kva;
2153 	uint32_t sbc_flags;
2154 	bool sbc_allow_faulting;
2155 };
2156 
2157 static errno_t
_stackshot_backtrace_copy(void * vctx,void * dst,user_addr_t src,size_t size)2158 _stackshot_backtrace_copy(void *vctx, void *dst, user_addr_t src, size_t size)
2159 {
2160 	struct _stackshot_backtrace_context *ctx = vctx;
2161 	size_t map_page_mask = 0;
2162 	size_t __assert_only map_page_size = kdp_vm_map_get_page_size(ctx->sbc_map,
2163 	    &map_page_mask);
2164 	assert(size < map_page_size);
2165 	if (src & (size - 1)) {
2166 		// The source should be aligned to the size passed in, like a stack
2167 		// frame or word.
2168 		return EINVAL;
2169 	}
2170 
2171 	vm_offset_t src_page = src & ~map_page_mask;
2172 	vm_offset_t src_kva = 0;
2173 
2174 	if (src_page != ctx->sbc_prev_page) {
2175 		uint32_t res = 0;
2176 		uint32_t flags = 0;
2177 		vm_offset_t src_pa = stackshot_find_phys(ctx->sbc_map, src,
2178 		    ctx->sbc_allow_faulting, &res);
2179 
2180 		flags |= (res & KDP_FAULT_RESULT_PAGED_OUT) ? kThreadTruncatedBT : 0;
2181 		flags |= (res & KDP_FAULT_RESULT_TRIED_FAULT) ? kThreadTriedFaultBT : 0;
2182 		flags |= (res & KDP_FAULT_RESULT_FAULTED_IN) ? kThreadFaultedBT : 0;
2183 		ctx->sbc_flags |= flags;
2184 		if (src_pa == 0) {
2185 			return EFAULT;
2186 		}
2187 
2188 		src_kva = phystokv(src_pa);
2189 		ctx->sbc_prev_page = src_page;
2190 		ctx->sbc_prev_kva = (src_kva & ~map_page_mask);
2191 	} else {
2192 		src_kva = ctx->sbc_prev_kva + (src & map_page_mask);
2193 	}
2194 
2195 #if KASAN
2196 	/*
2197 	 * KASan does not monitor accesses to userspace pages. Therefore, it is
2198 	 * pointless to maintain a shadow map for them. Instead, they are all
2199 	 * mapped to a single, always valid shadow map page. This approach saves
2200 	 * a considerable amount of shadow map pages which are limited and
2201 	 * precious.
2202 	 */
2203 	kasan_notify_address_nopoison(src_kva, size);
2204 #endif
2205 	memcpy(dst, (const void *)src_kva, size);
2206 
2207 	return 0;
2208 }
2209 
2210 static kern_return_t
kcdata_record_thread_snapshot(kcdata_descriptor_t kcd,thread_t thread,task_t task,uint64_t trace_flags,boolean_t have_pmap,boolean_t thread_on_core)2211 kcdata_record_thread_snapshot(
2212 	kcdata_descriptor_t kcd, thread_t thread, task_t task, uint64_t trace_flags, boolean_t have_pmap, boolean_t thread_on_core)
2213 {
2214 	boolean_t dispatch_p              = ((trace_flags & STACKSHOT_GET_DQ) != 0);
2215 	boolean_t active_kthreads_only_p  = ((trace_flags & STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY) != 0);
2216 	boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0);
2217 	boolean_t collect_iostats         = !collect_delta_stackshot && !(trace_flags & STACKSHOT_NO_IO_STATS);
2218 #if CONFIG_PERVASIVE_CPI
2219 	boolean_t collect_instrs_cycles   = ((trace_flags & STACKSHOT_INSTRS_CYCLES) != 0);
2220 #endif /* CONFIG_PERVASIVE_CPI */
2221 	kern_return_t error        = KERN_SUCCESS;
2222 
2223 #if STACKSHOT_COLLECTS_LATENCY_INFO
2224 	struct stackshot_latency_thread latency_info;
2225 	latency_info.cur_thsnap1_latency = mach_absolute_time();
2226 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2227 
2228 	mach_vm_address_t out_addr = 0;
2229 	int saved_count            = 0;
2230 
2231 	struct thread_snapshot_v4 * cur_thread_snap = NULL;
2232 	char cur_thread_name[STACKSHOT_MAX_THREAD_NAME_SIZE];
2233 
2234 	kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_THREAD_SNAPSHOT, sizeof(struct thread_snapshot_v4), &out_addr));
2235 	cur_thread_snap = (struct thread_snapshot_v4 *)out_addr;
2236 
2237 	/* Populate the thread snapshot header */
2238 	cur_thread_snap->ths_ss_flags = 0;
2239 	cur_thread_snap->ths_thread_id = thread_tid(thread);
2240 	cur_thread_snap->ths_wait_event = VM_KERNEL_UNSLIDE_OR_PERM(thread->wait_event);
2241 	cur_thread_snap->ths_continuation = VM_KERNEL_UNSLIDE(thread->continuation);
2242 	cur_thread_snap->ths_total_syscalls = thread->syscalls_mach + thread->syscalls_unix;
2243 
2244 	if (IPC_VOUCHER_NULL != thread->ith_voucher) {
2245 		cur_thread_snap->ths_voucher_identifier = VM_KERNEL_ADDRPERM(thread->ith_voucher);
2246 	} else {
2247 		cur_thread_snap->ths_voucher_identifier = 0;
2248 	}
2249 
2250 #if STACKSHOT_COLLECTS_LATENCY_INFO
2251 	latency_info.cur_thsnap1_latency = mach_absolute_time() - latency_info.cur_thsnap1_latency;
2252 	latency_info.dispatch_serial_latency = mach_absolute_time();
2253 	latency_info.dispatch_label_latency = 0;
2254 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2255 
2256 	cur_thread_snap->ths_dqserialnum = 0;
2257 	if (dispatch_p && (task != kernel_task) && (task->active) && have_pmap) {
2258 		uint64_t dqkeyaddr = thread_dispatchqaddr(thread);
2259 		if (dqkeyaddr != 0) {
2260 			uint64_t dqaddr = 0;
2261 			boolean_t copyin_ok = stackshot_copyin_word(task, dqkeyaddr, &dqaddr, FALSE, NULL);
2262 			if (copyin_ok && dqaddr != 0) {
2263 				uint64_t dqserialnumaddr = dqaddr + get_task_dispatchqueue_serialno_offset(task);
2264 				uint64_t dqserialnum = 0;
2265 				copyin_ok = stackshot_copyin_word(task, dqserialnumaddr, &dqserialnum, FALSE, NULL);
2266 				if (copyin_ok) {
2267 					cur_thread_snap->ths_ss_flags |= kHasDispatchSerial;
2268 					cur_thread_snap->ths_dqserialnum = dqserialnum;
2269 				}
2270 
2271 #if STACKSHOT_COLLECTS_LATENCY_INFO
2272 				latency_info.dispatch_serial_latency = mach_absolute_time() - latency_info.dispatch_serial_latency;
2273 				latency_info.dispatch_label_latency = mach_absolute_time();
2274 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2275 
2276 				/* try copying in the queue label */
2277 				uint64_t label_offs = get_task_dispatchqueue_label_offset(task);
2278 				if (label_offs) {
2279 					uint64_t dqlabeladdr = dqaddr + label_offs;
2280 					uint64_t actual_dqlabeladdr = 0;
2281 
2282 					copyin_ok = stackshot_copyin_word(task, dqlabeladdr, &actual_dqlabeladdr, FALSE, NULL);
2283 					if (copyin_ok && actual_dqlabeladdr != 0) {
2284 						char label_buf[STACKSHOT_QUEUE_LABEL_MAXSIZE];
2285 						int len;
2286 
2287 						bzero(label_buf, STACKSHOT_QUEUE_LABEL_MAXSIZE * sizeof(char));
2288 						len = stackshot_copyin_string(task, actual_dqlabeladdr, label_buf, STACKSHOT_QUEUE_LABEL_MAXSIZE, FALSE, NULL);
2289 						if (len > 0) {
2290 							mach_vm_address_t label_addr = 0;
2291 							kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL, len, &label_addr));
2292 							kdp_strlcpy((char*)label_addr, &label_buf[0], len);
2293 						}
2294 					}
2295 				}
2296 #if STACKSHOT_COLLECTS_LATENCY_INFO
2297 				latency_info.dispatch_label_latency = mach_absolute_time() - latency_info.dispatch_label_latency;
2298 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2299 			}
2300 		}
2301 	}
2302 
2303 #if STACKSHOT_COLLECTS_LATENCY_INFO
2304 	if ((cur_thread_snap->ths_ss_flags & kHasDispatchSerial) == 0) {
2305 		latency_info.dispatch_serial_latency = 0;
2306 	}
2307 	latency_info.cur_thsnap2_latency = mach_absolute_time();
2308 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2309 
2310 	struct recount_times_mach times = recount_thread_times(thread);
2311 	cur_thread_snap->ths_user_time = times.rtm_user;
2312 	cur_thread_snap->ths_sys_time = times.rtm_system;
2313 
2314 	if (thread->thread_tag & THREAD_TAG_MAINTHREAD) {
2315 		cur_thread_snap->ths_ss_flags |= kThreadMain;
2316 	}
2317 	if (thread->effective_policy.thep_darwinbg) {
2318 		cur_thread_snap->ths_ss_flags |= kThreadDarwinBG;
2319 	}
2320 	if (proc_get_effective_thread_policy(thread, TASK_POLICY_PASSIVE_IO)) {
2321 		cur_thread_snap->ths_ss_flags |= kThreadIOPassive;
2322 	}
2323 	if (thread->suspend_count > 0) {
2324 		cur_thread_snap->ths_ss_flags |= kThreadSuspended;
2325 	}
2326 	if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) {
2327 		cur_thread_snap->ths_ss_flags |= kGlobalForcedIdle;
2328 	}
2329 	if (thread_on_core) {
2330 		cur_thread_snap->ths_ss_flags |= kThreadOnCore;
2331 	}
2332 	if (stackshot_thread_is_idle_worker_unsafe(thread)) {
2333 		cur_thread_snap->ths_ss_flags |= kThreadIdleWorker;
2334 	}
2335 
2336 	/* make sure state flags defined in kcdata.h still match internal flags */
2337 	static_assert(SS_TH_WAIT == TH_WAIT);
2338 	static_assert(SS_TH_SUSP == TH_SUSP);
2339 	static_assert(SS_TH_RUN == TH_RUN);
2340 	static_assert(SS_TH_UNINT == TH_UNINT);
2341 	static_assert(SS_TH_TERMINATE == TH_TERMINATE);
2342 	static_assert(SS_TH_TERMINATE2 == TH_TERMINATE2);
2343 	static_assert(SS_TH_IDLE == TH_IDLE);
2344 
2345 	cur_thread_snap->ths_last_run_time           = thread->last_run_time;
2346 	cur_thread_snap->ths_last_made_runnable_time = thread->last_made_runnable_time;
2347 	cur_thread_snap->ths_state                   = thread->state;
2348 	cur_thread_snap->ths_sched_flags             = thread->sched_flags;
2349 	cur_thread_snap->ths_base_priority = thread->base_pri;
2350 	cur_thread_snap->ths_sched_priority = thread->sched_pri;
2351 	cur_thread_snap->ths_eqos = thread->effective_policy.thep_qos;
2352 	cur_thread_snap->ths_rqos = thread->requested_policy.thrp_qos;
2353 	cur_thread_snap->ths_rqos_override = MAX(thread->requested_policy.thrp_qos_override,
2354 	    thread->requested_policy.thrp_qos_workq_override);
2355 	cur_thread_snap->ths_io_tier = (uint8_t) proc_get_effective_thread_policy(thread, TASK_POLICY_IO);
2356 	cur_thread_snap->ths_thread_t = VM_KERNEL_UNSLIDE_OR_PERM(thread);
2357 
2358 	static_assert(sizeof(thread->effective_policy) == sizeof(uint64_t));
2359 	static_assert(sizeof(thread->requested_policy) == sizeof(uint64_t));
2360 	cur_thread_snap->ths_requested_policy = *(unaligned_u64 *) &thread->requested_policy;
2361 	cur_thread_snap->ths_effective_policy = *(unaligned_u64 *) &thread->effective_policy;
2362 
2363 #if STACKSHOT_COLLECTS_LATENCY_INFO
2364 	latency_info.cur_thsnap2_latency = mach_absolute_time()  - latency_info.cur_thsnap2_latency;
2365 	latency_info.thread_name_latency = mach_absolute_time();
2366 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2367 
2368 	/* if there is thread name then add to buffer */
2369 	cur_thread_name[0] = '\0';
2370 	proc_threadname_kdp(get_bsdthread_info(thread), cur_thread_name, STACKSHOT_MAX_THREAD_NAME_SIZE);
2371 	if (strnlen(cur_thread_name, STACKSHOT_MAX_THREAD_NAME_SIZE) > 0) {
2372 		kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_THREAD_NAME, sizeof(cur_thread_name), &out_addr));
2373 		kdp_memcpy((void *)out_addr, (void *)cur_thread_name, sizeof(cur_thread_name));
2374 	}
2375 
2376 #if STACKSHOT_COLLECTS_LATENCY_INFO
2377 	latency_info.thread_name_latency = mach_absolute_time()  - latency_info.thread_name_latency;
2378 	latency_info.sur_times_latency = mach_absolute_time();
2379 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2380 
2381 	/* record system, user, and runnable times */
2382 	time_value_t runnable_time;
2383 	thread_read_times(thread, NULL, NULL, &runnable_time);
2384 	clock_sec_t user_sec = 0, system_sec = 0;
2385 	clock_usec_t user_usec = 0, system_usec = 0;
2386 	absolutetime_to_microtime(times.rtm_user, &user_sec, &user_usec);
2387 	absolutetime_to_microtime(times.rtm_system, &system_sec, &system_usec);
2388 
2389 	kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_CPU_TIMES, sizeof(struct stackshot_cpu_times_v2), &out_addr));
2390 	struct stackshot_cpu_times_v2 *stackshot_cpu_times = (struct stackshot_cpu_times_v2 *)out_addr;
2391 	*stackshot_cpu_times = (struct stackshot_cpu_times_v2){
2392 		.user_usec = user_sec * USEC_PER_SEC + user_usec,
2393 		.system_usec = system_sec * USEC_PER_SEC + system_usec,
2394 		.runnable_usec = (uint64_t)runnable_time.seconds * USEC_PER_SEC + runnable_time.microseconds,
2395 	};
2396 
2397 #if STACKSHOT_COLLECTS_LATENCY_INFO
2398 	latency_info.sur_times_latency = mach_absolute_time()  - latency_info.sur_times_latency;
2399 	latency_info.user_stack_latency = mach_absolute_time();
2400 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2401 
2402 	/* Trace user stack, if any */
2403 	if (!active_kthreads_only_p && task->active && task->map != kernel_map) {
2404 		uint32_t user_ths_ss_flags = 0;
2405 
2406 		/*
2407 		 * This relies on knowing the "end" address points to the start of the
2408 		 * next elements data and, in the case of arrays, the elements.
2409 		 */
2410 		out_addr = (mach_vm_address_t)kcd_end_address(kcd);
2411 		mach_vm_address_t max_addr = (mach_vm_address_t)kcd_max_address(kcd);
2412 		assert(out_addr <= max_addr);
2413 		size_t avail_frames = (max_addr - out_addr) / sizeof(uintptr_t);
2414 		size_t max_frames = MIN(avail_frames, MAX_FRAMES);
2415 		if (max_frames == 0) {
2416 			error = KERN_RESOURCE_SHORTAGE;
2417 			goto error_exit;
2418 		}
2419 		struct _stackshot_backtrace_context ctx = {
2420 			.sbc_map = task->map,
2421 			.sbc_allow_faulting = stack_enable_faulting,
2422 			.sbc_prev_page = -1,
2423 			.sbc_prev_kva = -1,
2424 		};
2425 		struct backtrace_control ctl = {
2426 			.btc_user_thread = thread,
2427 			.btc_user_copy = _stackshot_backtrace_copy,
2428 			.btc_user_copy_context = &ctx,
2429 		};
2430 		struct backtrace_user_info info = BTUINFO_INIT;
2431 
2432 		saved_count = backtrace_user((uintptr_t *)out_addr, max_frames, &ctl,
2433 		    &info);
2434 		if (saved_count > 0) {
2435 #if __LP64__
2436 #define STACKLR_WORDS STACKSHOT_KCTYPE_USER_STACKLR64
2437 #else // __LP64__
2438 #define STACKLR_WORDS STACKSHOT_KCTYPE_USER_STACKLR
2439 #endif // !__LP64__
2440 			mach_vm_address_t out_addr_array;
2441 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd,
2442 			    STACKLR_WORDS, sizeof(uintptr_t), saved_count,
2443 			    &out_addr_array));
2444 			/*
2445 			 * Ensure the kcd_end_address (above) trick worked.
2446 			 */
2447 			assert(out_addr == out_addr_array);
2448 			if (info.btui_info & BTI_64_BIT) {
2449 				user_ths_ss_flags |= kUser64_p;
2450 			}
2451 			if ((info.btui_info & BTI_TRUNCATED) ||
2452 			    (ctx.sbc_flags & kThreadTruncatedBT)) {
2453 				user_ths_ss_flags |= kThreadTruncatedBT;
2454 				user_ths_ss_flags |= kThreadTruncUserBT;
2455 			}
2456 			user_ths_ss_flags |= ctx.sbc_flags;
2457 			ctx.sbc_flags = 0;
2458 #if __LP64__
2459 			/* We only support async stacks on 64-bit kernels */
2460 			if (info.btui_async_frame_addr != 0) {
2461 				uint32_t async_start_offset = info.btui_async_start_index;
2462 				kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_USER_ASYNC_START_INDEX,
2463 				    sizeof(async_start_offset), &async_start_offset));
2464 				out_addr = (mach_vm_address_t)kcd_end_address(kcd);
2465 				assert(out_addr <= max_addr);
2466 
2467 				avail_frames = (max_addr - out_addr) / sizeof(uintptr_t);
2468 				max_frames = MIN(avail_frames, MAX_FRAMES);
2469 				if (max_frames == 0) {
2470 					error = KERN_RESOURCE_SHORTAGE;
2471 					goto error_exit;
2472 				}
2473 				ctl.btc_frame_addr = info.btui_async_frame_addr;
2474 				ctl.btc_addr_offset = BTCTL_ASYNC_ADDR_OFFSET;
2475 				info = BTUINFO_INIT;
2476 				unsigned int async_count = backtrace_user((uintptr_t *)out_addr, max_frames, &ctl,
2477 				    &info);
2478 				if (async_count > 0) {
2479 					mach_vm_address_t async_out_addr;
2480 					kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd,
2481 					    STACKSHOT_KCTYPE_USER_ASYNC_STACKLR64, sizeof(uintptr_t), async_count,
2482 					    &async_out_addr));
2483 					/*
2484 					 * Ensure the kcd_end_address (above) trick worked.
2485 					 */
2486 					assert(out_addr == async_out_addr);
2487 					if ((info.btui_info & BTI_TRUNCATED) ||
2488 					    (ctx.sbc_flags & kThreadTruncatedBT)) {
2489 						user_ths_ss_flags |= kThreadTruncatedBT;
2490 						user_ths_ss_flags |= kThreadTruncUserAsyncBT;
2491 					}
2492 					user_ths_ss_flags |= ctx.sbc_flags;
2493 				}
2494 			}
2495 #endif /* _LP64 */
2496 		}
2497 		if (user_ths_ss_flags != 0) {
2498 			cur_thread_snap->ths_ss_flags |= user_ths_ss_flags;
2499 		}
2500 	}
2501 
2502 #if STACKSHOT_COLLECTS_LATENCY_INFO
2503 	latency_info.user_stack_latency = mach_absolute_time()  - latency_info.user_stack_latency;
2504 	latency_info.kernel_stack_latency = mach_absolute_time();
2505 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2506 
2507 	/* Call through to the machine specific trace routines
2508 	 * Frames are added past the snapshot header.
2509 	 */
2510 	if (thread->kernel_stack != 0) {
2511 		uint32_t kern_ths_ss_flags = 0;
2512 		out_addr = (mach_vm_address_t)kcd_end_address(kcd);
2513 #if defined(__LP64__)
2514 		uint32_t stack_kcdata_type = STACKSHOT_KCTYPE_KERN_STACKLR64;
2515 		extern int machine_trace_thread64(thread_t thread, char *tracepos,
2516 		    char *tracebound, int nframes, uint32_t *thread_trace_flags);
2517 		saved_count = machine_trace_thread64(
2518 #else
2519 		uint32_t stack_kcdata_type = STACKSHOT_KCTYPE_KERN_STACKLR;
2520 		extern int machine_trace_thread(thread_t thread, char *tracepos,
2521 		    char *tracebound, int nframes, uint32_t *thread_trace_flags);
2522 		saved_count = machine_trace_thread(
2523 #endif
2524 			thread, (char *)out_addr, (char *)kcd_max_address(kcd), MAX_FRAMES,
2525 			&kern_ths_ss_flags);
2526 		if (saved_count > 0) {
2527 			int frame_size = sizeof(uintptr_t);
2528 #if defined(__LP64__)
2529 			cur_thread_snap->ths_ss_flags |= kKernel64_p;
2530 #endif
2531 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(kcd, stack_kcdata_type,
2532 			    frame_size, saved_count / frame_size, &out_addr));
2533 		}
2534 		if (kern_ths_ss_flags & kThreadTruncatedBT) {
2535 			kern_ths_ss_flags |= kThreadTruncKernBT;
2536 		}
2537 		if (kern_ths_ss_flags != 0) {
2538 			cur_thread_snap->ths_ss_flags |= kern_ths_ss_flags;
2539 		}
2540 	}
2541 
2542 #if STACKSHOT_COLLECTS_LATENCY_INFO
2543 	latency_info.kernel_stack_latency = mach_absolute_time()  - latency_info.kernel_stack_latency;
2544 	latency_info.misc_latency = mach_absolute_time();
2545 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2546 
2547 #if CONFIG_THREAD_GROUPS
2548 	if (trace_flags & STACKSHOT_THREAD_GROUP) {
2549 		uint64_t thread_group_id = thread->thread_group ? thread_group_get_id(thread->thread_group) : 0;
2550 		kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_THREAD_GROUP, sizeof(thread_group_id), &out_addr));
2551 		kdp_memcpy((void*)out_addr, &thread_group_id, sizeof(uint64_t));
2552 	}
2553 #endif /* CONFIG_THREAD_GROUPS */
2554 
2555 	if (collect_iostats) {
2556 		kcd_exit_on_error(kcdata_record_thread_iostats(kcd, thread));
2557 	}
2558 
2559 #if CONFIG_PERVASIVE_CPI
2560 	if (collect_instrs_cycles) {
2561 		struct recount_usage usage = { 0 };
2562 		recount_sum_unsafe(&recount_thread_plan, thread->th_recount.rth_lifetime,
2563 		    &usage);
2564 
2565 		kcd_exit_on_error(kcdata_get_memory_addr(kcd, STACKSHOT_KCTYPE_INSTRS_CYCLES, sizeof(struct instrs_cycles_snapshot), &out_addr));
2566 		struct instrs_cycles_snapshot *instrs_cycles = (struct instrs_cycles_snapshot *)out_addr;
2567 		    instrs_cycles->ics_instructions = usage.ru_instructions;
2568 		    instrs_cycles->ics_cycles = usage.ru_cycles;
2569 	}
2570 #endif /* CONFIG_PERVASIVE_CPI */
2571 
2572 #if STACKSHOT_COLLECTS_LATENCY_INFO
2573 	latency_info.misc_latency = mach_absolute_time() - latency_info.misc_latency;
2574 	if (collect_latency_info) {
2575 		kcd_exit_on_error(kcdata_push_data(kcd, STACKSHOT_KCTYPE_LATENCY_INFO_THREAD, sizeof(latency_info), &latency_info));
2576 	}
2577 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2578 
2579 error_exit:
2580 	return error;
2581 }
2582 
2583 static int
kcdata_record_thread_delta_snapshot(struct thread_delta_snapshot_v3 * cur_thread_snap,thread_t thread,boolean_t thread_on_core)2584 kcdata_record_thread_delta_snapshot(struct thread_delta_snapshot_v3 * cur_thread_snap, thread_t thread, boolean_t thread_on_core)
2585 {
2586 	cur_thread_snap->tds_thread_id = thread_tid(thread);
2587 	if (IPC_VOUCHER_NULL != thread->ith_voucher) {
2588 		cur_thread_snap->tds_voucher_identifier  = VM_KERNEL_ADDRPERM(thread->ith_voucher);
2589 	} else {
2590 		cur_thread_snap->tds_voucher_identifier = 0;
2591 	}
2592 
2593 	cur_thread_snap->tds_ss_flags = 0;
2594 	if (thread->effective_policy.thep_darwinbg) {
2595 		cur_thread_snap->tds_ss_flags |= kThreadDarwinBG;
2596 	}
2597 	if (proc_get_effective_thread_policy(thread, TASK_POLICY_PASSIVE_IO)) {
2598 		cur_thread_snap->tds_ss_flags |= kThreadIOPassive;
2599 	}
2600 	if (thread->suspend_count > 0) {
2601 		cur_thread_snap->tds_ss_flags |= kThreadSuspended;
2602 	}
2603 	if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) {
2604 		cur_thread_snap->tds_ss_flags |= kGlobalForcedIdle;
2605 	}
2606 	if (thread_on_core) {
2607 		cur_thread_snap->tds_ss_flags |= kThreadOnCore;
2608 	}
2609 	if (stackshot_thread_is_idle_worker_unsafe(thread)) {
2610 		cur_thread_snap->tds_ss_flags |= kThreadIdleWorker;
2611 	}
2612 
2613 	cur_thread_snap->tds_last_made_runnable_time = thread->last_made_runnable_time;
2614 	cur_thread_snap->tds_state                   = thread->state;
2615 	cur_thread_snap->tds_sched_flags             = thread->sched_flags;
2616 	cur_thread_snap->tds_base_priority           = thread->base_pri;
2617 	cur_thread_snap->tds_sched_priority          = thread->sched_pri;
2618 	cur_thread_snap->tds_eqos                    = thread->effective_policy.thep_qos;
2619 	cur_thread_snap->tds_rqos                    = thread->requested_policy.thrp_qos;
2620 	cur_thread_snap->tds_rqos_override           = MAX(thread->requested_policy.thrp_qos_override,
2621 	    thread->requested_policy.thrp_qos_workq_override);
2622 	cur_thread_snap->tds_io_tier                 = (uint8_t) proc_get_effective_thread_policy(thread, TASK_POLICY_IO);
2623 
2624 	static_assert(sizeof(thread->effective_policy) == sizeof(uint64_t));
2625 	static_assert(sizeof(thread->requested_policy) == sizeof(uint64_t));
2626 	cur_thread_snap->tds_requested_policy = *(unaligned_u64 *) &thread->requested_policy;
2627 	cur_thread_snap->tds_effective_policy = *(unaligned_u64 *) &thread->effective_policy;
2628 
2629 	return 0;
2630 }
2631 
2632 /*
2633  * Why 12?  12 strikes a decent balance between allocating a large array on
2634  * the stack and having large kcdata item overheads for recording nonrunable
2635  * tasks.
2636  */
2637 #define UNIQUEIDSPERFLUSH 12
2638 
2639 struct saved_uniqueids {
2640 	uint64_t ids[UNIQUEIDSPERFLUSH];
2641 	unsigned count;
2642 };
2643 
2644 enum thread_classification {
2645 	tc_full_snapshot,  /* take a full snapshot */
2646 	tc_delta_snapshot, /* take a delta snapshot */
2647 };
2648 
2649 static enum thread_classification
classify_thread(thread_t thread,boolean_t * thread_on_core_p,boolean_t collect_delta_stackshot)2650 classify_thread(thread_t thread, boolean_t * thread_on_core_p, boolean_t collect_delta_stackshot)
2651 {
2652 	processor_t last_processor = thread->last_processor;
2653 
2654 	boolean_t thread_on_core = FALSE;
2655 	if (last_processor != PROCESSOR_NULL) {
2656 		/* Idle threads are always treated as on-core, since the processor state can change while they are running. */
2657 		thread_on_core = (thread == last_processor->idle_thread) ||
2658 		    ((last_processor->state == PROCESSOR_SHUTDOWN || last_processor->state == PROCESSOR_RUNNING) &&
2659 		    last_processor->active_thread == thread);
2660 	}
2661 
2662 	*thread_on_core_p = thread_on_core;
2663 
2664 	/* Capture the full thread snapshot if this is not a delta stackshot or if the thread has run subsequent to the
2665 	 * previous full stackshot */
2666 	if (!collect_delta_stackshot || thread_on_core || (thread->last_run_time > stack_snapshot_delta_since_timestamp)) {
2667 		return tc_full_snapshot;
2668 	} else {
2669 		return tc_delta_snapshot;
2670 	}
2671 }
2672 
2673 struct stackshot_context {
2674 	int pid;
2675 	uint64_t trace_flags;
2676 	bool include_drivers;
2677 };
2678 
2679 static kern_return_t
kdp_stackshot_record_task(struct stackshot_context * ctx,task_t task)2680 kdp_stackshot_record_task(struct stackshot_context *ctx, task_t task)
2681 {
2682 	boolean_t active_kthreads_only_p  = ((ctx->trace_flags & STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY) != 0);
2683 	boolean_t save_donating_pids_p    = ((ctx->trace_flags & STACKSHOT_SAVE_IMP_DONATION_PIDS) != 0);
2684 	boolean_t collect_delta_stackshot = ((ctx->trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0);
2685 	boolean_t save_owner_info         = ((ctx->trace_flags & STACKSHOT_THREAD_WAITINFO) != 0);
2686 
2687 	kern_return_t error = KERN_SUCCESS;
2688 	mach_vm_address_t out_addr = 0;
2689 	int saved_count = 0;
2690 
2691 	int task_pid                   = 0;
2692 	uint64_t task_uniqueid         = 0;
2693 	int num_delta_thread_snapshots = 0;
2694 	int num_waitinfo_threads       = 0;
2695 	int num_turnstileinfo_threads  = 0;
2696 
2697 	uint64_t task_start_abstime    = 0;
2698 	boolean_t have_map = FALSE, have_pmap = FALSE;
2699 	boolean_t some_thread_ran = FALSE;
2700 	unaligned_u64 task_snap_ss_flags = 0;
2701 #if STACKSHOT_COLLECTS_LATENCY_INFO
2702 	struct stackshot_latency_task latency_info;
2703 	latency_info.setup_latency = mach_absolute_time();
2704 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2705 
2706 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
2707 	uint64_t task_begin_cpu_cycle_count = 0;
2708 	if (!panic_stackshot) {
2709 		task_begin_cpu_cycle_count = mt_cur_cpu_cycles();
2710 	}
2711 #endif
2712 
2713 	if ((task == NULL) || !_stackshot_validate_kva((vm_offset_t)task, sizeof(struct task))) {
2714 		error = KERN_FAILURE;
2715 		goto error_exit;
2716 	}
2717 
2718 	void *bsd_info = get_bsdtask_info(task);
2719 	boolean_t task_in_teardown        = (bsd_info == NULL) || proc_in_teardown(bsd_info);// has P_LPEXIT set during proc_exit()
2720 	boolean_t task_in_transition      = task_in_teardown;         // here we can add other types of transition.
2721 	uint32_t  container_type          = (task_in_transition) ? STACKSHOT_KCCONTAINER_TRANSITIONING_TASK : STACKSHOT_KCCONTAINER_TASK;
2722 	uint32_t  transition_type         = (task_in_teardown) ? kTaskIsTerminated : 0;
2723 
2724 	if (task_in_transition) {
2725 		collect_delta_stackshot = FALSE;
2726 	}
2727 
2728 	have_map = (task->map != NULL) && (_stackshot_validate_kva((vm_offset_t)(task->map), sizeof(struct _vm_map)));
2729 	have_pmap = have_map && (task->map->pmap != NULL) && (_stackshot_validate_kva((vm_offset_t)(task->map->pmap), sizeof(struct pmap)));
2730 
2731 	task_pid = pid_from_task(task);
2732 	/* Is returning -1 ok for terminating task ok ??? */
2733 	task_uniqueid = get_task_uniqueid(task);
2734 
2735 	if (!task->active || task_is_a_corpse(task) || task_is_a_corpse_fork(task)) {
2736 		/*
2737 		 * Not interested in terminated tasks without threads.
2738 		 */
2739 		if (queue_empty(&task->threads) || task_pid == -1) {
2740 			return KERN_SUCCESS;
2741 		}
2742 	}
2743 
2744 	/* All PIDs should have the MSB unset */
2745 	assert((task_pid & (1ULL << 31)) == 0);
2746 
2747 #if STACKSHOT_COLLECTS_LATENCY_INFO
2748 	latency_info.setup_latency = mach_absolute_time() - latency_info.setup_latency;
2749 	latency_info.task_uniqueid = task_uniqueid;
2750 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2751 
2752 	/* Trace everything, unless a process was specified. Add in driver tasks if requested. */
2753 	if ((ctx->pid == -1) || (ctx->pid == task_pid) || (ctx->include_drivers && task_is_driver(task))) {
2754 		/* add task snapshot marker */
2755 		kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN,
2756 		    container_type, task_uniqueid));
2757 
2758 		if (collect_delta_stackshot) {
2759 			/*
2760 			 * For delta stackshots we need to know if a thread from this task has run since the
2761 			 * previous timestamp to decide whether we're going to record a full snapshot and UUID info.
2762 			 */
2763 			thread_t thread = THREAD_NULL;
2764 			queue_iterate(&task->threads, thread, thread_t, task_threads)
2765 			{
2766 				if ((thread == NULL) || !_stackshot_validate_kva((vm_offset_t)thread, sizeof(struct thread))) {
2767 					error = KERN_FAILURE;
2768 					goto error_exit;
2769 				}
2770 
2771 				if (active_kthreads_only_p && thread->kernel_stack == 0) {
2772 					continue;
2773 				}
2774 
2775 				boolean_t thread_on_core;
2776 				enum thread_classification thread_classification = classify_thread(thread, &thread_on_core, collect_delta_stackshot);
2777 
2778 				switch (thread_classification) {
2779 				case tc_full_snapshot:
2780 					some_thread_ran = TRUE;
2781 					break;
2782 				case tc_delta_snapshot:
2783 					num_delta_thread_snapshots++;
2784 					break;
2785 				}
2786 			}
2787 		}
2788 
2789 		if (collect_delta_stackshot) {
2790 			proc_starttime_kdp(get_bsdtask_info(task), NULL, NULL, &task_start_abstime);
2791 		}
2792 
2793 		/* Next record any relevant UUID info and store the task snapshot */
2794 		if (task_in_transition ||
2795 		    !collect_delta_stackshot ||
2796 		    (task_start_abstime == 0) ||
2797 		    (task_start_abstime > stack_snapshot_delta_since_timestamp) ||
2798 		    some_thread_ran) {
2799 			/*
2800 			 * Collect full task information in these scenarios:
2801 			 *
2802 			 * 1) a full stackshot or the task is in transition
2803 			 * 2) a delta stackshot where the task started after the previous full stackshot
2804 			 * 3) a delta stackshot where any thread from the task has run since the previous full stackshot
2805 			 *
2806 			 * because the task may have exec'ed, changing its name, architecture, load info, etc
2807 			 */
2808 
2809 			kcd_exit_on_error(kcdata_record_shared_cache_info(stackshot_kcdata_p, task, &task_snap_ss_flags));
2810 			kcd_exit_on_error(kcdata_record_uuid_info(stackshot_kcdata_p, task, ctx->trace_flags, have_pmap, &task_snap_ss_flags));
2811 #if STACKSHOT_COLLECTS_LATENCY_INFO
2812 			if (!task_in_transition) {
2813 				kcd_exit_on_error(kcdata_record_task_snapshot(stackshot_kcdata_p, task, ctx->trace_flags, have_pmap, task_snap_ss_flags, &latency_info));
2814 			} else {
2815 				kcd_exit_on_error(kcdata_record_transitioning_task_snapshot(stackshot_kcdata_p, task, task_snap_ss_flags, transition_type));
2816 			}
2817 #else
2818 			if (!task_in_transition) {
2819 				kcd_exit_on_error(kcdata_record_task_snapshot(stackshot_kcdata_p, task, ctx->trace_flags, have_pmap, task_snap_ss_flags));
2820 			} else {
2821 				kcd_exit_on_error(kcdata_record_transitioning_task_snapshot(stackshot_kcdata_p, task, task_snap_ss_flags, transition_type));
2822 			}
2823 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2824 		} else {
2825 			kcd_exit_on_error(kcdata_record_task_delta_snapshot(stackshot_kcdata_p, task, ctx->trace_flags, have_pmap, task_snap_ss_flags));
2826 		}
2827 
2828 #if STACKSHOT_COLLECTS_LATENCY_INFO
2829 		latency_info.misc_latency = mach_absolute_time();
2830 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2831 
2832 		struct thread_delta_snapshot_v3 * delta_snapshots = NULL;
2833 		int current_delta_snapshot_index                  = 0;
2834 		if (num_delta_thread_snapshots > 0) {
2835 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT,
2836 			    sizeof(struct thread_delta_snapshot_v3),
2837 			    num_delta_thread_snapshots, &out_addr));
2838 			delta_snapshots = (struct thread_delta_snapshot_v3 *)out_addr;
2839 		}
2840 
2841 
2842 #if STACKSHOT_COLLECTS_LATENCY_INFO
2843 		latency_info.task_thread_count_loop_latency = mach_absolute_time();
2844 #endif
2845 		/*
2846 		 * Iterate over the task threads to save thread snapshots and determine
2847 		 * how much space we need for waitinfo and turnstile info
2848 		 */
2849 		thread_t thread = THREAD_NULL;
2850 		queue_iterate(&task->threads, thread, thread_t, task_threads)
2851 		{
2852 			if ((thread == NULL) || !_stackshot_validate_kva((vm_offset_t)thread, sizeof(struct thread))) {
2853 				error = KERN_FAILURE;
2854 				goto error_exit;
2855 			}
2856 
2857 			uint64_t thread_uniqueid;
2858 			if (active_kthreads_only_p && thread->kernel_stack == 0) {
2859 				continue;
2860 			}
2861 			thread_uniqueid = thread_tid(thread);
2862 
2863 			boolean_t thread_on_core;
2864 			enum thread_classification thread_classification = classify_thread(thread, &thread_on_core, collect_delta_stackshot);
2865 
2866 			switch (thread_classification) {
2867 			case tc_full_snapshot:
2868 				/* add thread marker */
2869 				kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN,
2870 				    STACKSHOT_KCCONTAINER_THREAD, thread_uniqueid));
2871 
2872 				/* thread snapshot can be large, including strings, avoid overflowing the stack. */
2873 				kcdata_compression_window_open(stackshot_kcdata_p);
2874 
2875 				kcd_exit_on_error(kcdata_record_thread_snapshot(stackshot_kcdata_p, thread, task, ctx->trace_flags, have_pmap, thread_on_core));
2876 
2877 				kcd_exit_on_error(kcdata_compression_window_close(stackshot_kcdata_p));
2878 
2879 				/* mark end of thread snapshot data */
2880 				kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_END,
2881 				    STACKSHOT_KCCONTAINER_THREAD, thread_uniqueid));
2882 				break;
2883 			case tc_delta_snapshot:
2884 				kcd_exit_on_error(kcdata_record_thread_delta_snapshot(&delta_snapshots[current_delta_snapshot_index++], thread, thread_on_core));
2885 				break;
2886 			}
2887 
2888 			/*
2889 			 * We want to report owner information regardless of whether a thread
2890 			 * has changed since the last delta, whether it's a normal stackshot,
2891 			 * or whether it's nonrunnable
2892 			 */
2893 			if (save_owner_info) {
2894 				if (stackshot_thread_has_valid_waitinfo(thread)) {
2895 					num_waitinfo_threads++;
2896 				}
2897 
2898 				if (stackshot_thread_has_valid_turnstileinfo(thread)) {
2899 					num_turnstileinfo_threads++;
2900 				}
2901 			}
2902 		}
2903 #if STACKSHOT_COLLECTS_LATENCY_INFO
2904 		latency_info.task_thread_count_loop_latency = mach_absolute_time() - latency_info.task_thread_count_loop_latency;
2905 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2906 
2907 
2908 		thread_waitinfo_v2_t *thread_waitinfo           = NULL;
2909 		thread_turnstileinfo_v2_t *thread_turnstileinfo = NULL;
2910 		int current_waitinfo_index              = 0;
2911 		int current_turnstileinfo_index         = 0;
2912 		/* allocate space for the wait and turnstil info */
2913 		if (num_waitinfo_threads > 0 || num_turnstileinfo_threads > 0) {
2914 			/* thread waitinfo and turnstileinfo can be quite large, avoid overflowing the stack */
2915 			kcdata_compression_window_open(stackshot_kcdata_p);
2916 
2917 			if (num_waitinfo_threads > 0) {
2918 				kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_WAITINFO,
2919 				    sizeof(thread_waitinfo_v2_t), num_waitinfo_threads, &out_addr));
2920 				thread_waitinfo = (thread_waitinfo_v2_t *)out_addr;
2921 			}
2922 
2923 			if (num_turnstileinfo_threads > 0) {
2924 				/* get space for the turnstile info */
2925 				kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO,
2926 				    sizeof(thread_turnstileinfo_v2_t), num_turnstileinfo_threads, &out_addr));
2927 				thread_turnstileinfo = (thread_turnstileinfo_v2_t *)out_addr;
2928 			}
2929 
2930 			stackshot_plh_resetgen();  // so we know which portlabel_ids are referenced
2931 		}
2932 
2933 #if STACKSHOT_COLLECTS_LATENCY_INFO
2934 		latency_info.misc_latency = mach_absolute_time() - latency_info.misc_latency;
2935 		latency_info.task_thread_data_loop_latency = mach_absolute_time();
2936 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2937 
2938 		/* Iterate over the task's threads to save the wait and turnstile info */
2939 		queue_iterate(&task->threads, thread, thread_t, task_threads)
2940 		{
2941 			uint64_t thread_uniqueid;
2942 
2943 			if (active_kthreads_only_p && thread->kernel_stack == 0) {
2944 				continue;
2945 			}
2946 
2947 			thread_uniqueid = thread_tid(thread);
2948 
2949 			/* If we want owner info, we should capture it regardless of its classification */
2950 			if (save_owner_info) {
2951 				if (stackshot_thread_has_valid_waitinfo(thread)) {
2952 					stackshot_thread_wait_owner_info(
2953 						thread,
2954 						&thread_waitinfo[current_waitinfo_index++]);
2955 				}
2956 
2957 				if (stackshot_thread_has_valid_turnstileinfo(thread)) {
2958 					stackshot_thread_turnstileinfo(
2959 						thread,
2960 						&thread_turnstileinfo[current_turnstileinfo_index++]);
2961 				}
2962 			}
2963 		}
2964 
2965 #if STACKSHOT_COLLECTS_LATENCY_INFO
2966 		latency_info.task_thread_data_loop_latency = mach_absolute_time() - latency_info.task_thread_data_loop_latency;
2967 		latency_info.misc2_latency = mach_absolute_time();
2968 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
2969 
2970 #if DEBUG || DEVELOPMENT
2971 		if (current_delta_snapshot_index != num_delta_thread_snapshots) {
2972 			panic("delta thread snapshot count mismatch while capturing snapshots for task %p. expected %d, found %d", task,
2973 			    num_delta_thread_snapshots, current_delta_snapshot_index);
2974 		}
2975 		if (current_waitinfo_index != num_waitinfo_threads) {
2976 			panic("thread wait info count mismatch while capturing snapshots for task %p. expected %d, found %d", task,
2977 			    num_waitinfo_threads, current_waitinfo_index);
2978 		}
2979 #endif
2980 
2981 		if (num_waitinfo_threads > 0 || num_turnstileinfo_threads > 0) {
2982 			kcd_exit_on_error(kcdata_compression_window_close(stackshot_kcdata_p));
2983 			// now, record the portlabel hashes.
2984 			kcd_exit_on_error(kdp_stackshot_plh_record());
2985 		}
2986 
2987 #if IMPORTANCE_INHERITANCE
2988 		if (save_donating_pids_p) {
2989 			kcd_exit_on_error(
2990 				((((mach_vm_address_t)kcd_end_address(stackshot_kcdata_p) + (TASK_IMP_WALK_LIMIT * sizeof(int32_t))) <
2991 				(mach_vm_address_t)kcd_max_address(stackshot_kcdata_p))
2992 				? KERN_SUCCESS
2993 				: KERN_RESOURCE_SHORTAGE));
2994 			saved_count = task_importance_list_pids(task, TASK_IMP_LIST_DONATING_PIDS,
2995 			    (void *)kcd_end_address(stackshot_kcdata_p), TASK_IMP_WALK_LIMIT);
2996 			if (saved_count > 0) {
2997 				/* Variable size array - better not have it on the stack. */
2998 				kcdata_compression_window_open(stackshot_kcdata_p);
2999 				kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_DONATING_PIDS,
3000 				    sizeof(int32_t), saved_count, &out_addr));
3001 				kcd_exit_on_error(kcdata_compression_window_close(stackshot_kcdata_p));
3002 			}
3003 		}
3004 #endif
3005 
3006 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3007 		if (!panic_stackshot) {
3008 			kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, (mt_cur_cpu_cycles() - task_begin_cpu_cycle_count),
3009 			    "task_cpu_cycle_count"));
3010 		}
3011 #endif
3012 
3013 #if STACKSHOT_COLLECTS_LATENCY_INFO
3014 		latency_info.misc2_latency = mach_absolute_time() - latency_info.misc2_latency;
3015 		if (collect_latency_info) {
3016 			kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_LATENCY_INFO_TASK, sizeof(latency_info), &latency_info));
3017 		}
3018 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3019 
3020 		/* mark end of task snapshot data */
3021 		kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_END, container_type,
3022 		    task_uniqueid));
3023 	}
3024 
3025 
3026 error_exit:
3027 	return error;
3028 }
3029 
3030 /* Record global shared regions */
3031 static kern_return_t
kdp_stackshot_shared_regions(uint64_t trace_flags)3032 kdp_stackshot_shared_regions(uint64_t trace_flags)
3033 {
3034 	kern_return_t error        = KERN_SUCCESS;
3035 
3036 	boolean_t collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0);
3037 	extern queue_head_t vm_shared_region_queue;
3038 	vm_shared_region_t sr;
3039 
3040 	extern queue_head_t vm_shared_region_queue;
3041 	queue_iterate(&vm_shared_region_queue,
3042 	    sr,
3043 	    vm_shared_region_t,
3044 	    sr_q) {
3045 		struct dyld_shared_cache_loadinfo_v2 scinfo = {0};
3046 		if (!_stackshot_validate_kva((vm_offset_t)sr, sizeof(*sr))) {
3047 			break;
3048 		}
3049 		if (collect_delta_stackshot && sr->sr_install_time < stack_snapshot_delta_since_timestamp) {
3050 			continue; // only include new shared caches in delta stackshots
3051 		}
3052 		uint32_t sharedCacheFlags = ((sr == primary_system_shared_region) ? kSharedCacheSystemPrimary : 0) |
3053 		    (sr->sr_driverkit ? kSharedCacheDriverkit : 0);
3054 		kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN,
3055 		    STACKSHOT_KCCONTAINER_SHAREDCACHE, sr->sr_id));
3056 		kdp_memcpy(scinfo.sharedCacheUUID, sr->sr_uuid, sizeof(sr->sr_uuid));
3057 		scinfo.sharedCacheSlide = sr->sr_slide;
3058 		scinfo.sharedCacheUnreliableSlidBaseAddress = sr->sr_base_address + sr->sr_first_mapping;
3059 		scinfo.sharedCacheSlidFirstMapping = sr->sr_base_address + sr->sr_first_mapping;
3060 		scinfo.sharedCacheID = sr->sr_id;
3061 		scinfo.sharedCacheFlags = sharedCacheFlags;
3062 
3063 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_SHAREDCACHE_INFO,
3064 		    sizeof(scinfo), &scinfo));
3065 
3066 		if ((trace_flags & STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT) && sr->sr_images != NULL &&
3067 		    _stackshot_validate_kva((vm_offset_t)sr->sr_images, sr->sr_images_count * sizeof(struct dyld_uuid_info_64))) {
3068 			assert(sr->sr_images_count != 0);
3069 			kcd_exit_on_error(kcdata_push_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT, sizeof(struct dyld_uuid_info_64), sr->sr_images_count, sr->sr_images));
3070 		}
3071 		kcd_exit_on_error(kcdata_add_container_marker(stackshot_kcdata_p, KCDATA_TYPE_CONTAINER_END,
3072 		    STACKSHOT_KCCONTAINER_SHAREDCACHE, sr->sr_id));
3073 	}
3074 
3075 	/*
3076 	 * For backwards compatibility; this will eventually be removed.
3077 	 * Another copy of the Primary System Shared Region, for older readers.
3078 	 */
3079 	sr = primary_system_shared_region;
3080 	/* record system level shared cache load info (if available) */
3081 	if (!collect_delta_stackshot && sr &&
3082 	    _stackshot_validate_kva((vm_offset_t)sr, sizeof(struct vm_shared_region))) {
3083 		struct dyld_shared_cache_loadinfo scinfo = {0};
3084 
3085 		/*
3086 		 * Historically, this data was in a dyld_uuid_info_64 structure, but the
3087 		 * naming of both the structure and fields for this use isn't great.  The
3088 		 * dyld_shared_cache_loadinfo structure has better names, but the same
3089 		 * layout and content as the original.
3090 		 *
3091 		 * The imageSlidBaseAddress/sharedCacheUnreliableSlidBaseAddress field
3092 		 * has been used inconsistently for STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT
3093 		 * entries; here, it's the slid base address, and we leave it that way
3094 		 * for backwards compatibility.
3095 		 */
3096 		kdp_memcpy(scinfo.sharedCacheUUID, &sr->sr_uuid, sizeof(sr->sr_uuid));
3097 		scinfo.sharedCacheSlide = sr->sr_slide;
3098 		scinfo.sharedCacheUnreliableSlidBaseAddress = sr->sr_slide + sr->sr_base_address;
3099 		scinfo.sharedCacheSlidFirstMapping = sr->sr_base_address + sr->sr_first_mapping;
3100 
3101 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO,
3102 		    sizeof(scinfo), &scinfo));
3103 
3104 		if (trace_flags & STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT) {
3105 			/*
3106 			 * Include a map of the system shared cache layout if it has been populated
3107 			 * (which is only when the system is using a custom shared cache).
3108 			 */
3109 			if (sr->sr_images && _stackshot_validate_kva((vm_offset_t)sr->sr_images,
3110 			    (sr->sr_images_count * sizeof(struct dyld_uuid_info_64)))) {
3111 				assert(sr->sr_images_count != 0);
3112 				kcd_exit_on_error(kcdata_push_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT, sizeof(struct dyld_uuid_info_64), sr->sr_images_count, sr->sr_images));
3113 			}
3114 		}
3115 	}
3116 
3117 error_exit:
3118 	return error;
3119 }
3120 
3121 static kern_return_t
kdp_stackshot_kcdata_format(int pid,uint64_t trace_flags,uint32_t * pBytesTraced,uint32_t * pBytesUncompressed)3122 kdp_stackshot_kcdata_format(int pid, uint64_t trace_flags, uint32_t * pBytesTraced, uint32_t * pBytesUncompressed)
3123 {
3124 	kern_return_t error        = KERN_SUCCESS;
3125 	mach_vm_address_t out_addr = 0;
3126 	uint64_t abs_time = 0, abs_time_end = 0;
3127 	uint64_t system_state_flags = 0;
3128 	task_t task = TASK_NULL;
3129 	mach_timebase_info_data_t timebase = {0, 0};
3130 	uint32_t length_to_copy = 0, tmp32 = 0;
3131 	abs_time = mach_absolute_time();
3132 	uint64_t last_task_start_time = 0;
3133 
3134 #if STACKSHOT_COLLECTS_LATENCY_INFO
3135 	struct stackshot_latency_collection latency_info;
3136 #endif
3137 
3138 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3139 	uint64_t stackshot_begin_cpu_cycle_count = 0;
3140 
3141 	if (!panic_stackshot) {
3142 		stackshot_begin_cpu_cycle_count = mt_cur_cpu_cycles();
3143 	}
3144 #endif
3145 
3146 #if STACKSHOT_COLLECTS_LATENCY_INFO
3147 	collect_latency_info = trace_flags & STACKSHOT_DISABLE_LATENCY_INFO ? false : true;
3148 #endif
3149 
3150 	/* process the flags */
3151 	bool collect_delta_stackshot = ((trace_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) != 0);
3152 	bool use_fault_path          = ((trace_flags & (STACKSHOT_ENABLE_UUID_FAULTING | STACKSHOT_ENABLE_BT_FAULTING)) != 0);
3153 	stack_enable_faulting = (trace_flags & (STACKSHOT_ENABLE_BT_FAULTING));
3154 
3155 	/* Currently we only support returning explicit KEXT load info on fileset kernels */
3156 	kc_format_t primary_kc_type = KCFormatUnknown;
3157 	if (PE_get_primary_kc_format(&primary_kc_type) && (primary_kc_type != KCFormatFileset)) {
3158 		trace_flags &= ~(STACKSHOT_SAVE_KEXT_LOADINFO);
3159 	}
3160 
3161 	struct stackshot_context ctx = {};
3162 	ctx.trace_flags = trace_flags;
3163 	ctx.pid = pid;
3164 	ctx.include_drivers = (pid == 0 && (trace_flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) != 0);
3165 
3166 	if (use_fault_path) {
3167 		fault_stats.sfs_pages_faulted_in = 0;
3168 		fault_stats.sfs_time_spent_faulting = 0;
3169 		fault_stats.sfs_stopped_faulting = (uint8_t) FALSE;
3170 	}
3171 
3172 	if (sizeof(void *) == 8) {
3173 		system_state_flags |= kKernel64_p;
3174 	}
3175 
3176 	if (stackshot_kcdata_p == NULL || pBytesTraced == NULL) {
3177 		error = KERN_INVALID_ARGUMENT;
3178 		goto error_exit;
3179 	}
3180 
3181 	_stackshot_validation_reset();
3182 	stackshot_plh_setup(stackshot_kcdata_p); /* set up port label hash */
3183 
3184 	/* setup mach_absolute_time and timebase info -- copy out in some cases and needed to convert since_timestamp to seconds for proc start time */
3185 	clock_timebase_info(&timebase);
3186 
3187 	/* begin saving data into the buffer */
3188 	*pBytesTraced = 0;
3189 	if (pBytesUncompressed) {
3190 		*pBytesUncompressed = 0;
3191 	}
3192 	kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, trace_flags, "stackshot_in_flags"));
3193 	kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, (uint32_t)pid, "stackshot_in_pid"));
3194 	kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, system_state_flags, "system_state_flags"));
3195 	if (trace_flags & STACKSHOT_PAGE_TABLES) {
3196 		kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, stack_snapshot_pagetable_mask, "stackshot_pagetable_mask"));
3197 	}
3198 	if (stackshot_initial_estimate != 0) {
3199 		kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, stackshot_initial_estimate, "stackshot_size_estimate"));
3200 		kcd_exit_on_error(kcdata_add_uint32_with_description(stackshot_kcdata_p, stackshot_initial_estimate_adj, "stackshot_size_estimate_adj"));
3201 	}
3202 
3203 #if STACKSHOT_COLLECTS_LATENCY_INFO
3204 	latency_info.setup_latency = mach_absolute_time();
3205 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3206 
3207 #if CONFIG_JETSAM
3208 	tmp32 = memorystatus_get_pressure_status_kdp();
3209 	kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_JETSAM_LEVEL, sizeof(uint32_t), &tmp32));
3210 #endif
3211 
3212 	if (!collect_delta_stackshot) {
3213 		tmp32 = THREAD_POLICY_INTERNAL_STRUCT_VERSION;
3214 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_POLICY_VERSION, sizeof(uint32_t), &tmp32));
3215 
3216 		tmp32 = PAGE_SIZE;
3217 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_KERN_PAGE_SIZE, sizeof(uint32_t), &tmp32));
3218 
3219 		/* save boot-args and osversion string */
3220 		length_to_copy =  MIN((uint32_t)(strlen(version) + 1), OSVERSIZE);
3221 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_OSVERSION, length_to_copy, (const void *)version));
3222 
3223 
3224 		length_to_copy =  MIN((uint32_t)(strlen(PE_boot_args()) + 1), BOOT_LINE_LENGTH);
3225 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_BOOTARGS, length_to_copy, PE_boot_args()));
3226 
3227 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, KCDATA_TYPE_TIMEBASE, sizeof(timebase), &timebase));
3228 	} else {
3229 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP, sizeof(uint64_t), &stack_snapshot_delta_since_timestamp));
3230 	}
3231 
3232 	kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, KCDATA_TYPE_MACH_ABSOLUTE_TIME, sizeof(uint64_t), &abs_time));
3233 
3234 	kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, KCDATA_TYPE_USECS_SINCE_EPOCH, sizeof(uint64_t), &stackshot_microsecs));
3235 
3236 	kcd_exit_on_error(kdp_stackshot_shared_regions(trace_flags));
3237 
3238 	/* Add requested information first */
3239 	if (trace_flags & STACKSHOT_GET_GLOBAL_MEM_STATS) {
3240 		struct mem_and_io_snapshot mais = {0};
3241 		kdp_mem_and_io_snapshot(&mais);
3242 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_GLOBAL_MEM_STATS, sizeof(mais), &mais));
3243 	}
3244 
3245 #if CONFIG_THREAD_GROUPS
3246 	struct thread_group_snapshot_v3 *thread_groups = NULL;
3247 	int num_thread_groups = 0;
3248 
3249 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3250 	uint64_t thread_group_begin_cpu_cycle_count = 0;
3251 
3252 	if (!panic_stackshot && (trace_flags & STACKSHOT_THREAD_GROUP)) {
3253 		thread_group_begin_cpu_cycle_count = mt_cur_cpu_cycles();
3254 	}
3255 #endif
3256 
3257 
3258 	/* Iterate over thread group names */
3259 	if (trace_flags & STACKSHOT_THREAD_GROUP) {
3260 		/* Variable size array - better not have it on the stack. */
3261 		kcdata_compression_window_open(stackshot_kcdata_p);
3262 
3263 		if (thread_group_iterate_stackshot(stackshot_thread_group_count, &num_thread_groups) != KERN_SUCCESS) {
3264 			trace_flags &= ~(STACKSHOT_THREAD_GROUP);
3265 		}
3266 
3267 		if (num_thread_groups > 0) {
3268 			kcd_exit_on_error(kcdata_get_memory_addr_for_array(stackshot_kcdata_p, STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT, sizeof(struct thread_group_snapshot_v3), num_thread_groups, &out_addr));
3269 			thread_groups = (struct thread_group_snapshot_v3 *)out_addr;
3270 		}
3271 
3272 		if (thread_group_iterate_stackshot(stackshot_thread_group_snapshot, thread_groups) != KERN_SUCCESS) {
3273 			error = KERN_FAILURE;
3274 			goto error_exit;
3275 		}
3276 
3277 		kcd_exit_on_error(kcdata_compression_window_close(stackshot_kcdata_p));
3278 	}
3279 
3280 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3281 	if (!panic_stackshot && (thread_group_begin_cpu_cycle_count != 0)) {
3282 		kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, (mt_cur_cpu_cycles() - thread_group_begin_cpu_cycle_count),
3283 		    "thread_groups_cpu_cycle_count"));
3284 	}
3285 #endif
3286 #else
3287 	trace_flags &= ~(STACKSHOT_THREAD_GROUP);
3288 #endif /* CONFIG_THREAD_GROUPS */
3289 
3290 
3291 #if STACKSHOT_COLLECTS_LATENCY_INFO
3292 	latency_info.setup_latency = mach_absolute_time() - latency_info.setup_latency;
3293 	latency_info.total_task_iteration_latency = mach_absolute_time();
3294 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3295 
3296 	/* Iterate over tasks */
3297 	queue_iterate(&tasks, task, task_t, tasks)
3298 	{
3299 		if (collect_delta_stackshot) {
3300 			uint64_t abstime;
3301 			proc_starttime_kdp(get_bsdtask_info(task), NULL, NULL, &abstime);
3302 
3303 			if (abstime > last_task_start_time) {
3304 				last_task_start_time = abstime;
3305 			}
3306 		}
3307 
3308 		error = kdp_stackshot_record_task(&ctx, task);
3309 		if (error) {
3310 			goto error_exit;
3311 		}
3312 	}
3313 
3314 
3315 #if STACKSHOT_COLLECTS_LATENCY_INFO
3316 	latency_info.total_task_iteration_latency = mach_absolute_time() - latency_info.total_task_iteration_latency;
3317 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3318 
3319 #if CONFIG_COALITIONS
3320 	/* Don't collect jetsam coalition snapshots in delta stackshots - these don't change */
3321 	if (!collect_delta_stackshot || (last_task_start_time > stack_snapshot_delta_since_timestamp)) {
3322 		int num_coalitions = 0;
3323 		struct jetsam_coalition_snapshot *coalitions = NULL;
3324 
3325 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3326 		uint64_t coalition_begin_cpu_cycle_count = 0;
3327 
3328 		if (!panic_stackshot && (trace_flags & STACKSHOT_SAVE_JETSAM_COALITIONS)) {
3329 			coalition_begin_cpu_cycle_count = mt_cur_cpu_cycles();
3330 		}
3331 #endif /* SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI */
3332 
3333 		/* Iterate over coalitions */
3334 		if (trace_flags & STACKSHOT_SAVE_JETSAM_COALITIONS) {
3335 			if (coalition_iterate_stackshot(stackshot_coalition_jetsam_count, &num_coalitions, COALITION_TYPE_JETSAM) != KERN_SUCCESS) {
3336 				trace_flags &= ~(STACKSHOT_SAVE_JETSAM_COALITIONS);
3337 			}
3338 		}
3339 		if (trace_flags & STACKSHOT_SAVE_JETSAM_COALITIONS) {
3340 			if (num_coalitions > 0) {
3341 				/* Variable size array - better not have it on the stack. */
3342 				kcdata_compression_window_open(stackshot_kcdata_p);
3343 				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));
3344 				coalitions = (struct jetsam_coalition_snapshot*)out_addr;
3345 
3346 				if (coalition_iterate_stackshot(stackshot_coalition_jetsam_snapshot, coalitions, COALITION_TYPE_JETSAM) != KERN_SUCCESS) {
3347 					error = KERN_FAILURE;
3348 					goto error_exit;
3349 				}
3350 
3351 				kcd_exit_on_error(kcdata_compression_window_close(stackshot_kcdata_p));
3352 			}
3353 		}
3354 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3355 		if (!panic_stackshot && (coalition_begin_cpu_cycle_count != 0)) {
3356 			kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, (mt_cur_cpu_cycles() - coalition_begin_cpu_cycle_count),
3357 			    "coalitions_cpu_cycle_count"));
3358 		}
3359 #endif /* SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI */
3360 	}
3361 #else
3362 	trace_flags &= ~(STACKSHOT_SAVE_JETSAM_COALITIONS);
3363 #endif /* CONFIG_COALITIONS */
3364 
3365 #if STACKSHOT_COLLECTS_LATENCY_INFO
3366 	latency_info.total_terminated_task_iteration_latency = mach_absolute_time();
3367 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3368 
3369 	/*
3370 	 * Iterate over the tasks in the terminated tasks list. We only inspect
3371 	 * tasks that have a valid bsd_info pointer. The check for task transition
3372 	 * like past P_LPEXIT during proc_exit() is now checked for inside the
3373 	 * kdp_stackshot_record_task(), and then a safer and minimal
3374 	 * transitioning_task_snapshot struct is collected via
3375 	 * kcdata_record_transitioning_task_snapshot()
3376 	 */
3377 	queue_iterate(&terminated_tasks, task, task_t, tasks)
3378 	{
3379 		error = kdp_stackshot_record_task(&ctx, task);
3380 		if (error) {
3381 			goto error_exit;
3382 		}
3383 	}
3384 #if DEVELOPMENT || DEBUG
3385 	kcd_exit_on_error(kdp_stackshot_plh_stats());
3386 #endif /* DEVELOPMENT || DEBUG */
3387 
3388 #if STACKSHOT_COLLECTS_LATENCY_INFO
3389 	latency_info.total_terminated_task_iteration_latency = mach_absolute_time() - latency_info.total_terminated_task_iteration_latency;
3390 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3391 
3392 	if (use_fault_path) {
3393 		kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS,
3394 		    sizeof(struct stackshot_fault_stats), &fault_stats);
3395 	}
3396 
3397 #if STACKSHOT_COLLECTS_LATENCY_INFO
3398 	if (collect_latency_info) {
3399 		latency_info.latency_version = 1;
3400 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_LATENCY_INFO, sizeof(latency_info), &latency_info));
3401 	}
3402 #endif /* STACKSHOT_COLLECTS_LATENCY_INFO */
3403 
3404 	/* update timestamp of the stackshot */
3405 	abs_time_end = mach_absolute_time();
3406 	struct stackshot_duration_v2 stackshot_duration = {
3407 		.stackshot_duration         = (abs_time_end - abs_time),
3408 		.stackshot_duration_outer   = 0,
3409 		.stackshot_duration_prior   = stackshot_duration_prior_abs,
3410 	};
3411 
3412 	if ((trace_flags & STACKSHOT_DO_COMPRESS) == 0) {
3413 		kcd_exit_on_error(kcdata_get_memory_addr(stackshot_kcdata_p, STACKSHOT_KCTYPE_STACKSHOT_DURATION,
3414 		    sizeof(struct stackshot_duration_v2), &out_addr));
3415 		struct stackshot_duration_v2 *duration_p = (void *) out_addr;
3416 		kdp_memcpy(duration_p, &stackshot_duration, sizeof(*duration_p));
3417 		stackshot_duration_outer                   = (unaligned_u64 *)&duration_p->stackshot_duration_outer;
3418 	} else {
3419 		kcd_exit_on_error(kcdata_push_data(stackshot_kcdata_p, STACKSHOT_KCTYPE_STACKSHOT_DURATION, sizeof(stackshot_duration), &stackshot_duration));
3420 		stackshot_duration_outer = NULL;
3421 	}
3422 
3423 #if SCHED_HYGIENE_DEBUG && CONFIG_PERVASIVE_CPI
3424 	if (!panic_stackshot) {
3425 		kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, (mt_cur_cpu_cycles() - stackshot_begin_cpu_cycle_count),
3426 		    "stackshot_total_cpu_cycle_cnt"));
3427 	}
3428 #endif
3429 
3430 	kcd_finalize_compression(stackshot_kcdata_p);
3431 	kcd_exit_on_error(kcdata_add_uint64_with_description(stackshot_kcdata_p, trace_flags, "stackshot_out_flags"));
3432 
3433 	kcd_exit_on_error(kcdata_write_buffer_end(stackshot_kcdata_p));
3434 
3435 	/*  === END of populating stackshot data === */
3436 
3437 	*pBytesTraced = (uint32_t) kcdata_memory_get_used_bytes(stackshot_kcdata_p);
3438 	*pBytesUncompressed = (uint32_t) kcdata_memory_get_uncompressed_bytes(stackshot_kcdata_p);
3439 
3440 error_exit:;
3441 
3442 #if SCHED_HYGIENE_DEBUG
3443 	bool disable_interrupts_masked_check = kern_feature_override(
3444 		KF_INTERRUPT_MASKED_DEBUG_STACKSHOT_OVRD) ||
3445 	    (trace_flags & STACKSHOT_DO_COMPRESS) != 0;
3446 
3447 #if STACKSHOT_INTERRUPTS_MASKED_CHECK_DISABLED
3448 	disable_interrupts_masked_check = true;
3449 #endif /* STACKSHOT_INTERRUPTS_MASKED_CHECK_DISABLED */
3450 
3451 	if (disable_interrupts_masked_check) {
3452 		ml_spin_debug_clear_self();
3453 	}
3454 
3455 	if (!panic_stackshot && interrupt_masked_debug_mode) {
3456 		/*
3457 		 * Try to catch instances where stackshot takes too long BEFORE returning from
3458 		 * the debugger
3459 		 */
3460 		ml_handle_stackshot_interrupt_disabled_duration(current_thread());
3461 	}
3462 #endif /* SCHED_HYGIENE_DEBUG */
3463 	stackshot_plh_reset();
3464 	stack_enable_faulting = FALSE;
3465 
3466 	return error;
3467 }
3468 
3469 static uint64_t
proc_was_throttled_from_task(task_t task)3470 proc_was_throttled_from_task(task_t task)
3471 {
3472 	uint64_t was_throttled = 0;
3473 	void *bsd_info = get_bsdtask_info(task);
3474 
3475 	if (bsd_info) {
3476 		was_throttled = proc_was_throttled(bsd_info);
3477 	}
3478 
3479 	return was_throttled;
3480 }
3481 
3482 static uint64_t
proc_did_throttle_from_task(task_t task)3483 proc_did_throttle_from_task(task_t task)
3484 {
3485 	uint64_t did_throttle = 0;
3486 	void *bsd_info = get_bsdtask_info(task);
3487 
3488 	if (bsd_info) {
3489 		did_throttle = proc_did_throttle(bsd_info);
3490 	}
3491 
3492 	return did_throttle;
3493 }
3494 
3495 static void
kdp_mem_and_io_snapshot(struct mem_and_io_snapshot * memio_snap)3496 kdp_mem_and_io_snapshot(struct mem_and_io_snapshot *memio_snap)
3497 {
3498 	unsigned int pages_reclaimed;
3499 	unsigned int pages_wanted;
3500 	kern_return_t kErr;
3501 
3502 	uint64_t compressions = 0;
3503 	uint64_t decompressions = 0;
3504 
3505 	compressions = counter_load(&vm_statistics_compressions);
3506 	decompressions = counter_load(&vm_statistics_decompressions);
3507 
3508 	memio_snap->snapshot_magic = STACKSHOT_MEM_AND_IO_SNAPSHOT_MAGIC;
3509 	memio_snap->free_pages = vm_page_free_count;
3510 	memio_snap->active_pages = vm_page_active_count;
3511 	memio_snap->inactive_pages = vm_page_inactive_count;
3512 	memio_snap->purgeable_pages = vm_page_purgeable_count;
3513 	memio_snap->wired_pages = vm_page_wire_count;
3514 	memio_snap->speculative_pages = vm_page_speculative_count;
3515 	memio_snap->throttled_pages = vm_page_throttled_count;
3516 	memio_snap->busy_buffer_count = count_busy_buffers();
3517 	memio_snap->filebacked_pages = vm_page_pageable_external_count;
3518 	memio_snap->compressions = (uint32_t)compressions;
3519 	memio_snap->decompressions = (uint32_t)decompressions;
3520 	memio_snap->compressor_size = VM_PAGE_COMPRESSOR_COUNT;
3521 	kErr = mach_vm_pressure_monitor(FALSE, VM_PRESSURE_TIME_WINDOW, &pages_reclaimed, &pages_wanted);
3522 
3523 	if (!kErr) {
3524 		memio_snap->pages_wanted = (uint32_t)pages_wanted;
3525 		memio_snap->pages_reclaimed = (uint32_t)pages_reclaimed;
3526 		memio_snap->pages_wanted_reclaimed_valid = 1;
3527 	} else {
3528 		memio_snap->pages_wanted = 0;
3529 		memio_snap->pages_reclaimed = 0;
3530 		memio_snap->pages_wanted_reclaimed_valid = 0;
3531 	}
3532 }
3533 
3534 static vm_offset_t
stackshot_find_phys(vm_map_t map,vm_offset_t target_addr,kdp_fault_flags_t fault_flags,uint32_t * kdp_fault_result_flags)3535 stackshot_find_phys(vm_map_t map, vm_offset_t target_addr, kdp_fault_flags_t fault_flags, uint32_t *kdp_fault_result_flags)
3536 {
3537 	vm_offset_t result;
3538 	struct kdp_fault_result fault_results = {0};
3539 	if (fault_stats.sfs_stopped_faulting) {
3540 		fault_flags &= ~KDP_FAULT_FLAGS_ENABLE_FAULTING;
3541 	}
3542 
3543 	result = kdp_find_phys(map, target_addr, fault_flags, &fault_results);
3544 
3545 	if ((fault_results.flags & KDP_FAULT_RESULT_TRIED_FAULT) || (fault_results.flags & KDP_FAULT_RESULT_FAULTED_IN)) {
3546 		fault_stats.sfs_time_spent_faulting += fault_results.time_spent_faulting;
3547 
3548 		if ((fault_stats.sfs_time_spent_faulting >= fault_stats.sfs_system_max_fault_time) && !panic_stackshot) {
3549 			fault_stats.sfs_stopped_faulting = (uint8_t) TRUE;
3550 		}
3551 	}
3552 
3553 	if (fault_results.flags & KDP_FAULT_RESULT_FAULTED_IN) {
3554 		fault_stats.sfs_pages_faulted_in++;
3555 	}
3556 
3557 	if (kdp_fault_result_flags) {
3558 		*kdp_fault_result_flags = fault_results.flags;
3559 	}
3560 
3561 	return result;
3562 }
3563 
3564 /*
3565  * Wrappers around kdp_generic_copyin, kdp_generic_copyin_word, kdp_generic_copyin_string that use stackshot_find_phys
3566  * in order to:
3567  *   1. collect statistics on the number of pages faulted in
3568  *   2. stop faulting if the time spent faulting has exceeded the limit.
3569  */
3570 static boolean_t
stackshot_copyin(vm_map_t map,uint64_t uaddr,void * dest,size_t size,boolean_t try_fault,kdp_fault_result_flags_t * kdp_fault_result_flags)3571 stackshot_copyin(vm_map_t map, uint64_t uaddr, void *dest, size_t size, boolean_t try_fault, kdp_fault_result_flags_t *kdp_fault_result_flags)
3572 {
3573 	kdp_fault_flags_t fault_flags = KDP_FAULT_FLAGS_NONE;
3574 	if (try_fault) {
3575 		fault_flags |= KDP_FAULT_FLAGS_ENABLE_FAULTING;
3576 	}
3577 	return kdp_generic_copyin(map, uaddr, dest, size, fault_flags, (find_phys_fn_t)stackshot_find_phys, kdp_fault_result_flags) == KERN_SUCCESS;
3578 }
3579 static boolean_t
stackshot_copyin_word(task_t task,uint64_t addr,uint64_t * result,boolean_t try_fault,kdp_fault_result_flags_t * kdp_fault_result_flags)3580 stackshot_copyin_word(task_t task, uint64_t addr, uint64_t *result, boolean_t try_fault, kdp_fault_result_flags_t *kdp_fault_result_flags)
3581 {
3582 	kdp_fault_flags_t fault_flags = KDP_FAULT_FLAGS_NONE;
3583 	if (try_fault) {
3584 		fault_flags |= KDP_FAULT_FLAGS_ENABLE_FAULTING;
3585 	}
3586 	return kdp_generic_copyin_word(task, addr, result, fault_flags, (find_phys_fn_t)stackshot_find_phys, kdp_fault_result_flags) == KERN_SUCCESS;
3587 }
3588 static int
stackshot_copyin_string(task_t task,uint64_t addr,char * buf,int buf_sz,boolean_t try_fault,kdp_fault_result_flags_t * kdp_fault_result_flags)3589 stackshot_copyin_string(task_t task, uint64_t addr, char *buf, int buf_sz, boolean_t try_fault, kdp_fault_result_flags_t *kdp_fault_result_flags)
3590 {
3591 	kdp_fault_flags_t fault_flags = KDP_FAULT_FLAGS_NONE;
3592 	if (try_fault) {
3593 		fault_flags |= KDP_FAULT_FLAGS_ENABLE_FAULTING;
3594 	}
3595 	return kdp_generic_copyin_string(task, addr, buf, buf_sz, fault_flags, (find_phys_fn_t)stackshot_find_phys, kdp_fault_result_flags);
3596 }
3597 
3598 kern_return_t
do_stackshot(void * context)3599 do_stackshot(void *context)
3600 {
3601 #pragma unused(context)
3602 	kdp_snapshot++;
3603 
3604 	stack_snapshot_ret = kdp_stackshot_kcdata_format(stack_snapshot_pid,
3605 	    stack_snapshot_flags,
3606 	    &stack_snapshot_bytes_traced,
3607 	    &stack_snapshot_bytes_uncompressed);
3608 
3609 	if (stack_snapshot_ret == KERN_SUCCESS) {
3610 		/* releases and zeros and kcdata_end_alloc()s done */
3611 		kcdata_finish(stackshot_kcdata_p);
3612 	}
3613 
3614 	kdp_snapshot--;
3615 	return stack_snapshot_ret;
3616 }
3617 
3618 boolean_t
stackshot_thread_is_idle_worker_unsafe(thread_t thread)3619 stackshot_thread_is_idle_worker_unsafe(thread_t thread)
3620 {
3621 	/* When the pthread kext puts a worker thread to sleep, it will
3622 	 * set kThreadWaitParkedWorkQueue in the block_hint of the thread
3623 	 * struct. See parkit() in kern/kern_support.c in libpthread.
3624 	 */
3625 	return (thread->state & TH_WAIT) &&
3626 	       (thread->block_hint == kThreadWaitParkedWorkQueue);
3627 }
3628 
3629 #if CONFIG_COALITIONS
3630 static void
stackshot_coalition_jetsam_count(void * arg,int i,coalition_t coal)3631 stackshot_coalition_jetsam_count(void *arg, int i, coalition_t coal)
3632 {
3633 #pragma unused(i, coal)
3634 	unsigned int *coalition_count = (unsigned int*)arg;
3635 	(*coalition_count)++;
3636 }
3637 
3638 static void
stackshot_coalition_jetsam_snapshot(void * arg,int i,coalition_t coal)3639 stackshot_coalition_jetsam_snapshot(void *arg, int i, coalition_t coal)
3640 {
3641 	if (coalition_type(coal) != COALITION_TYPE_JETSAM) {
3642 		return;
3643 	}
3644 
3645 	struct jetsam_coalition_snapshot *coalitions = (struct jetsam_coalition_snapshot*)arg;
3646 	struct jetsam_coalition_snapshot *jcs = &coalitions[i];
3647 	task_t leader = TASK_NULL;
3648 	jcs->jcs_id = coalition_id(coal);
3649 	jcs->jcs_flags = 0;
3650 	jcs->jcs_thread_group = 0;
3651 
3652 	if (coalition_term_requested(coal)) {
3653 		jcs->jcs_flags |= kCoalitionTermRequested;
3654 	}
3655 	if (coalition_is_terminated(coal)) {
3656 		jcs->jcs_flags |= kCoalitionTerminated;
3657 	}
3658 	if (coalition_is_reaped(coal)) {
3659 		jcs->jcs_flags |= kCoalitionReaped;
3660 	}
3661 	if (coalition_is_privileged(coal)) {
3662 		jcs->jcs_flags |= kCoalitionPrivileged;
3663 	}
3664 
3665 #if CONFIG_THREAD_GROUPS
3666 	struct thread_group *thread_group = kdp_coalition_get_thread_group(coal);
3667 	if (thread_group) {
3668 		jcs->jcs_thread_group = thread_group_get_id(thread_group);
3669 	}
3670 #endif /* CONFIG_THREAD_GROUPS */
3671 
3672 	leader = kdp_coalition_get_leader(coal);
3673 	if (leader) {
3674 		jcs->jcs_leader_task_uniqueid = get_task_uniqueid(leader);
3675 	} else {
3676 		jcs->jcs_leader_task_uniqueid = 0;
3677 	}
3678 }
3679 #endif /* CONFIG_COALITIONS */
3680 
3681 #if CONFIG_THREAD_GROUPS
3682 static void
stackshot_thread_group_count(void * arg,int i,struct thread_group * tg)3683 stackshot_thread_group_count(void *arg, int i, struct thread_group *tg)
3684 {
3685 #pragma unused(i, tg)
3686 	unsigned int *n = (unsigned int*)arg;
3687 	(*n)++;
3688 }
3689 
3690 static void
stackshot_thread_group_snapshot(void * arg,int i,struct thread_group * tg)3691 stackshot_thread_group_snapshot(void *arg, int i, struct thread_group *tg)
3692 {
3693 	struct thread_group_snapshot_v3 *thread_groups = arg;
3694 	struct thread_group_snapshot_v3 *tgs = &thread_groups[i];
3695 	const char *name = thread_group_get_name(tg);
3696 	uint32_t flags = thread_group_get_flags(tg);
3697 	tgs->tgs_id = thread_group_get_id(tg);
3698 	static_assert(THREAD_GROUP_MAXNAME > sizeof(tgs->tgs_name));
3699 	kdp_memcpy(tgs->tgs_name, name, sizeof(tgs->tgs_name));
3700 	kdp_memcpy(tgs->tgs_name_cont, name + sizeof(tgs->tgs_name),
3701 	    sizeof(tgs->tgs_name_cont));
3702 	tgs->tgs_flags =
3703 	    ((flags & THREAD_GROUP_FLAGS_EFFICIENT)     ? kThreadGroupEfficient     : 0) |
3704 	    ((flags & THREAD_GROUP_FLAGS_APPLICATION)   ? kThreadGroupApplication   : 0) |
3705 	    ((flags & THREAD_GROUP_FLAGS_CRITICAL)      ? kThreadGroupCritical      : 0) |
3706 	    ((flags & THREAD_GROUP_FLAGS_BEST_EFFORT)   ? kThreadGroupBestEffort    : 0) |
3707 	    ((flags & THREAD_GROUP_FLAGS_UI_APP)        ? kThreadGroupUIApplication : 0) |
3708 	    ((flags & THREAD_GROUP_FLAGS_MANAGED)       ? kThreadGroupManaged       : 0) |
3709 	    ((flags & THREAD_GROUP_FLAGS_STRICT_TIMERS) ? kThreadGroupStrictTimers  : 0) |
3710 	    0;
3711 }
3712 #endif /* CONFIG_THREAD_GROUPS */
3713 
3714 /* Determine if a thread has waitinfo that stackshot can provide */
3715 static int
stackshot_thread_has_valid_waitinfo(thread_t thread)3716 stackshot_thread_has_valid_waitinfo(thread_t thread)
3717 {
3718 	if (!(thread->state & TH_WAIT)) {
3719 		return 0;
3720 	}
3721 
3722 	switch (thread->block_hint) {
3723 	// If set to None or is a parked work queue, ignore it
3724 	case kThreadWaitParkedWorkQueue:
3725 	case kThreadWaitNone:
3726 		return 0;
3727 	// There is a short window where the pthread kext removes a thread
3728 	// from its ksyn wait queue before waking the thread up
3729 	case kThreadWaitPThreadMutex:
3730 	case kThreadWaitPThreadRWLockRead:
3731 	case kThreadWaitPThreadRWLockWrite:
3732 	case kThreadWaitPThreadCondVar:
3733 		return kdp_pthread_get_thread_kwq(thread) != NULL;
3734 	// All other cases are valid block hints if in a wait state
3735 	default:
3736 		return 1;
3737 	}
3738 }
3739 
3740 /* Determine if a thread has turnstileinfo that stackshot can provide */
3741 static int
stackshot_thread_has_valid_turnstileinfo(thread_t thread)3742 stackshot_thread_has_valid_turnstileinfo(thread_t thread)
3743 {
3744 	struct turnstile *ts = thread_get_waiting_turnstile(thread);
3745 
3746 	return stackshot_thread_has_valid_waitinfo(thread) &&
3747 	       ts != TURNSTILE_NULL;
3748 }
3749 
3750 static void
stackshot_thread_turnstileinfo(thread_t thread,thread_turnstileinfo_v2_t * tsinfo)3751 stackshot_thread_turnstileinfo(thread_t thread, thread_turnstileinfo_v2_t *tsinfo)
3752 {
3753 	struct turnstile *ts;
3754 	struct ipc_service_port_label *ispl = NULL;
3755 
3756 	/* acquire turnstile information and store it in the stackshot */
3757 	ts = thread_get_waiting_turnstile(thread);
3758 	tsinfo->waiter = thread_tid(thread);
3759 	kdp_turnstile_fill_tsinfo(ts, tsinfo, &ispl);
3760 	tsinfo->portlabel_id = stackshot_plh_lookup(ispl,
3761 	    (tsinfo->turnstile_flags & STACKSHOT_TURNSTILE_STATUS_SENDPORT) ? STACKSHOT_PLH_LOOKUP_SEND :
3762 	    (tsinfo->turnstile_flags & STACKSHOT_TURNSTILE_STATUS_RECEIVEPORT) ? STACKSHOT_PLH_LOOKUP_RECEIVE :
3763 	    STACKSHOT_PLH_LOOKUP_UNKNOWN);
3764 }
3765 
3766 static void
stackshot_thread_wait_owner_info(thread_t thread,thread_waitinfo_v2_t * waitinfo)3767 stackshot_thread_wait_owner_info(thread_t thread, thread_waitinfo_v2_t *waitinfo)
3768 {
3769 	thread_waitinfo_t *waitinfo_v1 = (thread_waitinfo_t *)waitinfo;
3770 	struct ipc_service_port_label *ispl = NULL;
3771 
3772 	waitinfo->waiter        = thread_tid(thread);
3773 	waitinfo->wait_type     = thread->block_hint;
3774 	waitinfo->wait_flags    = 0;
3775 
3776 	switch (waitinfo->wait_type) {
3777 	case kThreadWaitKernelMutex:
3778 		kdp_lck_mtx_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo_v1);
3779 		break;
3780 	case kThreadWaitPortReceive:
3781 		kdp_mqueue_recv_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo, &ispl);
3782 		waitinfo->portlabel_id  = stackshot_plh_lookup(ispl, STACKSHOT_PLH_LOOKUP_RECEIVE);
3783 		break;
3784 	case kThreadWaitPortSend:
3785 		kdp_mqueue_send_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo, &ispl);
3786 		waitinfo->portlabel_id  = stackshot_plh_lookup(ispl, STACKSHOT_PLH_LOOKUP_SEND);
3787 		break;
3788 	case kThreadWaitSemaphore:
3789 		kdp_sema_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo_v1);
3790 		break;
3791 	case kThreadWaitUserLock:
3792 		kdp_ulock_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo_v1);
3793 		break;
3794 	case kThreadWaitKernelRWLockRead:
3795 	case kThreadWaitKernelRWLockWrite:
3796 	case kThreadWaitKernelRWLockUpgrade:
3797 		kdp_rwlck_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo_v1);
3798 		break;
3799 	case kThreadWaitPThreadMutex:
3800 	case kThreadWaitPThreadRWLockRead:
3801 	case kThreadWaitPThreadRWLockWrite:
3802 	case kThreadWaitPThreadCondVar:
3803 		kdp_pthread_find_owner(thread, waitinfo_v1);
3804 		break;
3805 	case kThreadWaitWorkloopSyncWait:
3806 		kdp_workloop_sync_wait_find_owner(thread, thread->wait_event, waitinfo_v1);
3807 		break;
3808 	case kThreadWaitOnProcess:
3809 		kdp_wait4_find_process(thread, thread->wait_event, waitinfo_v1);
3810 		break;
3811 	case kThreadWaitSleepWithInheritor:
3812 		kdp_sleep_with_inheritor_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo_v1);
3813 		break;
3814 	case kThreadWaitEventlink:
3815 		kdp_eventlink_find_owner(thread->waitq.wq_q, thread->wait_event, waitinfo_v1);
3816 		break;
3817 	case kThreadWaitCompressor:
3818 		kdp_compressor_busy_find_owner(thread->wait_event, waitinfo_v1);
3819 		break;
3820 	default:
3821 		waitinfo->owner = 0;
3822 		waitinfo->context = 0;
3823 		break;
3824 	}
3825 }
3826