xref: /xnu-12377.81.4/libsyscall/wrappers/stackshot.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2014 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*043036a2SApple OSS Distributions  * file.
12*043036a2SApple OSS Distributions  *
13*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*043036a2SApple OSS Distributions  * limitations under the License.
20*043036a2SApple OSS Distributions  *
21*043036a2SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*043036a2SApple OSS Distributions  */
23*043036a2SApple OSS Distributions #include <sys/stackshot.h>
24*043036a2SApple OSS Distributions #include <mach/mach.h>
25*043036a2SApple OSS Distributions #include <mach/mach_vm.h>
26*043036a2SApple OSS Distributions #include <stdint.h>
27*043036a2SApple OSS Distributions #include <stdlib.h>
28*043036a2SApple OSS Distributions #include <errno.h>
29*043036a2SApple OSS Distributions 
30*043036a2SApple OSS Distributions /*
31*043036a2SApple OSS Distributions  * System call entry point
32*043036a2SApple OSS Distributions  */
33*043036a2SApple OSS Distributions int __stack_snapshot_with_config(int stackshot_config_version, user_addr_t stackshot_config, size_t stackshot_config_size);
34*043036a2SApple OSS Distributions 
35*043036a2SApple OSS Distributions /*
36*043036a2SApple OSS Distributions  * stackshot_config_create:	create and initialize the arguments for a stackshot
37*043036a2SApple OSS Distributions  *
38*043036a2SApple OSS Distributions  * Outputs:			NULL if malloc fails
39*043036a2SApple OSS Distributions  *				a pointer to a new stackshot_config_t on success
40*043036a2SApple OSS Distributions  */
41*043036a2SApple OSS Distributions stackshot_config_t *
stackshot_config_create(void)42*043036a2SApple OSS Distributions stackshot_config_create(void)
43*043036a2SApple OSS Distributions {
44*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
45*043036a2SApple OSS Distributions 
46*043036a2SApple OSS Distributions 	s_config = malloc(sizeof(stackshot_config_t));
47*043036a2SApple OSS Distributions 	if (s_config == NULL) {
48*043036a2SApple OSS Distributions 		return NULL;
49*043036a2SApple OSS Distributions 	}
50*043036a2SApple OSS Distributions 
51*043036a2SApple OSS Distributions 	s_config->sc_pid = -1;
52*043036a2SApple OSS Distributions 	s_config->sc_flags = 0;
53*043036a2SApple OSS Distributions 	s_config->sc_delta_timestamp = 0;
54*043036a2SApple OSS Distributions 	s_config->sc_buffer = 0;
55*043036a2SApple OSS Distributions 	s_config->sc_size = 0;
56*043036a2SApple OSS Distributions 	s_config->sc_pagetable_mask = 0;
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions 	return s_config;
59*043036a2SApple OSS Distributions }
60*043036a2SApple OSS Distributions 
61*043036a2SApple OSS Distributions /*
62*043036a2SApple OSS Distributions  * stackshot_config_set_pid:	set the PID to be traced
63*043036a2SApple OSS Distributions  *
64*043036a2SApple OSS Distributions  * Inputs:			stackshot_config - a pointer to the stackshot_config_t we want to update
65*043036a2SApple OSS Distributions  *				pid - process id of process to be traced, or -1 for the entire system
66*043036a2SApple OSS Distributions  *
67*043036a2SApple OSS Distributions  * Outputs:			EINVAL if the passed stackshot_config pointer is NULL
68*043036a2SApple OSS Distributions  *				0 on success
69*043036a2SApple OSS Distributions  */
70*043036a2SApple OSS Distributions int
stackshot_config_set_pid(stackshot_config_t * stackshot_config,int pid)71*043036a2SApple OSS Distributions stackshot_config_set_pid(stackshot_config_t *stackshot_config, int pid)
72*043036a2SApple OSS Distributions {
73*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
74*043036a2SApple OSS Distributions 
75*043036a2SApple OSS Distributions 	if (stackshot_config == NULL) {
76*043036a2SApple OSS Distributions 		return EINVAL;
77*043036a2SApple OSS Distributions 	}
78*043036a2SApple OSS Distributions 
79*043036a2SApple OSS Distributions 	s_config = (stackshot_config_t *) stackshot_config;
80*043036a2SApple OSS Distributions 	s_config->sc_pid = pid;
81*043036a2SApple OSS Distributions 
82*043036a2SApple OSS Distributions 	return 0;
83*043036a2SApple OSS Distributions }
84*043036a2SApple OSS Distributions 
85*043036a2SApple OSS Distributions /*
86*043036a2SApple OSS Distributions  * stackshot_config_set_flags:	set the flags to be passed for the stackshot
87*043036a2SApple OSS Distributions  *
88*043036a2SApple OSS Distributions  * Inputs:			stackshot_config - a pointer to the stackshot_config_t we want to update
89*043036a2SApple OSS Distributions  *				flags - flags to pass to stackshot
90*043036a2SApple OSS Distributions  *
91*043036a2SApple OSS Distributions  * Outputs:			EINVAL if the passed stackshot_config pointer is NULL
92*043036a2SApple OSS Distributions  *				0 on success
93*043036a2SApple OSS Distributions  */
94*043036a2SApple OSS Distributions int
stackshot_config_set_flags(stackshot_config_t * stackshot_config,uint64_t flags)95*043036a2SApple OSS Distributions stackshot_config_set_flags(stackshot_config_t *stackshot_config, uint64_t flags)
96*043036a2SApple OSS Distributions {
97*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
98*043036a2SApple OSS Distributions 
99*043036a2SApple OSS Distributions 	if (stackshot_config == NULL) {
100*043036a2SApple OSS Distributions 		return EINVAL;
101*043036a2SApple OSS Distributions 	}
102*043036a2SApple OSS Distributions 
103*043036a2SApple OSS Distributions 	s_config = (stackshot_config_t *) stackshot_config;
104*043036a2SApple OSS Distributions 	s_config->sc_flags = flags;
105*043036a2SApple OSS Distributions 
106*043036a2SApple OSS Distributions 	return 0;
107*043036a2SApple OSS Distributions }
108*043036a2SApple OSS Distributions 
109*043036a2SApple OSS Distributions /*
110*043036a2SApple OSS Distributions  * stackshot_capture_with_config:	take a stackshot with the provided config
111*043036a2SApple OSS Distributions  *
112*043036a2SApple OSS Distributions  * Inputs:				stackshot_config - a pointer to the stackshot_config_t we want to use
113*043036a2SApple OSS Distributions  *
114*043036a2SApple OSS Distributions  * Outputs:				EINVAL if the passed stackshot_config pointer is NULL, a caller is trying
115*043036a2SApple OSS Distributions  *						to reuse a config without deallocating its buffer or if there is a
116*043036a2SApple OSS Distributions  *						problem with the arguments
117*043036a2SApple OSS Distributions  *					EFAULT if there was a problem with accessing the arguments from the kernel
118*043036a2SApple OSS Distributions  *					EPERM if the caller is not privileged
119*043036a2SApple OSS Distributions  *					ENOTSUP if the caller is passing a stackshot config version that is not
120*043036a2SApple OSS Distributions  *						supported by the kernel (indicates libsyscall:kernel mismatch),
121*043036a2SApple OSS Distributions  *						or if the caller is requesting unsupported flags
122*043036a2SApple OSS Distributions  *					ENOMEM if the kernel is unable to allocate memory
123*043036a2SApple OSS Distributions  *					ENOSPC if the caller doesn't have enough space in their address space for
124*043036a2SApple OSS Distributions  *						the kernel to remap the buffer
125*043036a2SApple OSS Distributions  *					ENOENT if the caller is requesting an existing buffer that doesn't exist
126*043036a2SApple OSS Distributions  *						or the target PID isn't found
127*043036a2SApple OSS Distributions  *					0 on success
128*043036a2SApple OSS Distributions  */
129*043036a2SApple OSS Distributions int
stackshot_capture_with_config(stackshot_config_t * stackshot_config)130*043036a2SApple OSS Distributions stackshot_capture_with_config(stackshot_config_t *stackshot_config)
131*043036a2SApple OSS Distributions {
132*043036a2SApple OSS Distributions 	int ret;
133*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
134*043036a2SApple OSS Distributions 
135*043036a2SApple OSS Distributions 	if (stackshot_config == NULL) {
136*043036a2SApple OSS Distributions 		return EINVAL;
137*043036a2SApple OSS Distributions 	}
138*043036a2SApple OSS Distributions 
139*043036a2SApple OSS Distributions 	s_config = (stackshot_config_t *) stackshot_config;
140*043036a2SApple OSS Distributions 	if (s_config->sc_buffer != 0) {
141*043036a2SApple OSS Distributions 		return EINVAL;
142*043036a2SApple OSS Distributions 	}
143*043036a2SApple OSS Distributions 
144*043036a2SApple OSS Distributions 	s_config->sc_out_buffer_addr = (uintptr_t)&s_config->sc_buffer;
145*043036a2SApple OSS Distributions 	s_config->sc_out_size_addr = (uintptr_t)&s_config->sc_size;
146*043036a2SApple OSS Distributions 	ret = __stack_snapshot_with_config(STACKSHOT_CONFIG_TYPE, (uintptr_t)s_config, sizeof(stackshot_config_t));
147*043036a2SApple OSS Distributions 
148*043036a2SApple OSS Distributions 	if (ret != 0) {
149*043036a2SApple OSS Distributions 		ret = errno;
150*043036a2SApple OSS Distributions 		s_config->sc_buffer = 0;
151*043036a2SApple OSS Distributions 		s_config->sc_size = 0;
152*043036a2SApple OSS Distributions 	}
153*043036a2SApple OSS Distributions 
154*043036a2SApple OSS Distributions 	return ret;
155*043036a2SApple OSS Distributions }
156*043036a2SApple OSS Distributions 
157*043036a2SApple OSS Distributions /*
158*043036a2SApple OSS Distributions  * stackshot_config_get_stackshot_buffer:	get a pointer to the buffer containing the stackshot
159*043036a2SApple OSS Distributions  *
160*043036a2SApple OSS Distributions  * Inputs:					stackshot_config - a pointer to a stackshot_config_t
161*043036a2SApple OSS Distributions  *
162*043036a2SApple OSS Distributions  * Outputs:					NULL if the passed stackshot_config is NULL or if its buffer is NULL
163*043036a2SApple OSS Distributions  *						a pointer to the buffer containing the stackshot on success
164*043036a2SApple OSS Distributions  */
165*043036a2SApple OSS Distributions void *
stackshot_config_get_stackshot_buffer(stackshot_config_t * stackshot_config)166*043036a2SApple OSS Distributions stackshot_config_get_stackshot_buffer(stackshot_config_t *stackshot_config)
167*043036a2SApple OSS Distributions {
168*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
169*043036a2SApple OSS Distributions 
170*043036a2SApple OSS Distributions 	if (stackshot_config == NULL) {
171*043036a2SApple OSS Distributions 		return NULL;
172*043036a2SApple OSS Distributions 	}
173*043036a2SApple OSS Distributions 	s_config = (stackshot_config_t *) stackshot_config;
174*043036a2SApple OSS Distributions 
175*043036a2SApple OSS Distributions 	return (void *)s_config->sc_buffer;
176*043036a2SApple OSS Distributions }
177*043036a2SApple OSS Distributions 
178*043036a2SApple OSS Distributions /*
179*043036a2SApple OSS Distributions  * stackshot_config_get_stackshot_size:	get the size of the stackshot buffer
180*043036a2SApple OSS Distributions  *
181*043036a2SApple OSS Distributions  * Inputs:  stackshot_config - a pointer to a stackshot_config_t
182*043036a2SApple OSS Distributions  *
183*043036a2SApple OSS Distributions  * Outputs: -1 if the passed stackshot config is NULL or there is no buffer
184*043036a2SApple OSS Distributions  *             the length of the stackshot buffer on success
185*043036a2SApple OSS Distributions  */
186*043036a2SApple OSS Distributions uint32_t
stackshot_config_get_stackshot_size(stackshot_config_t * stackshot_config)187*043036a2SApple OSS Distributions stackshot_config_get_stackshot_size(stackshot_config_t * stackshot_config)
188*043036a2SApple OSS Distributions {
189*043036a2SApple OSS Distributions 	if (stackshot_config == NULL || (void *)stackshot_config->sc_buffer == NULL) {
190*043036a2SApple OSS Distributions 		return -1;
191*043036a2SApple OSS Distributions 	}
192*043036a2SApple OSS Distributions 
193*043036a2SApple OSS Distributions 	return stackshot_config->sc_size;
194*043036a2SApple OSS Distributions }
195*043036a2SApple OSS Distributions 
196*043036a2SApple OSS Distributions /*
197*043036a2SApple OSS Distributions  * stackshot_config_set_size_hint: set the size of the stackshot buffer
198*043036a2SApple OSS Distributions  *
199*043036a2SApple OSS Distributions  * Inputs:  stackshot_config - a pointer to a stackshot_config_t
200*043036a2SApple OSS Distributions  *          suggested_size - hint for size allocation of stackshot
201*043036a2SApple OSS Distributions  *
202*043036a2SApple OSS Distributions  * Outputs:  -1  if the passed stackshot config is NULL or there is existing stackshot buffer set.
203*043036a2SApple OSS Distributions  *              the length of the stackshot buffer on success.
204*043036a2SApple OSS Distributions  */
205*043036a2SApple OSS Distributions int
stackshot_config_set_size_hint(stackshot_config_t * stackshot_config,uint32_t suggested_size)206*043036a2SApple OSS Distributions stackshot_config_set_size_hint(stackshot_config_t *stackshot_config, uint32_t suggested_size)
207*043036a2SApple OSS Distributions {
208*043036a2SApple OSS Distributions 	if (stackshot_config == NULL || (void *)stackshot_config->sc_buffer != NULL) {
209*043036a2SApple OSS Distributions 		return -1;
210*043036a2SApple OSS Distributions 	}
211*043036a2SApple OSS Distributions 
212*043036a2SApple OSS Distributions 	stackshot_config->sc_size = suggested_size;
213*043036a2SApple OSS Distributions 
214*043036a2SApple OSS Distributions 	return 0;
215*043036a2SApple OSS Distributions }
216*043036a2SApple OSS Distributions 
217*043036a2SApple OSS Distributions /*
218*043036a2SApple OSS Distributions  * stackshot_config_set_delta_timestamp: set the timestamp to use as the basis for the delta stackshot
219*043036a2SApple OSS Distributions  *
220*043036a2SApple OSS Distributions  * This timestamp will be used along with STACKSHOT_COLLECT_DELTA_SNAPSHOT flag to collect delta stackshots
221*043036a2SApple OSS Distributions  *
222*043036a2SApple OSS Distributions  * Inputs:  stackshot_config - a pointer to a stackshot_config_t
223*043036a2SApple OSS Distributions  *          delta_timestamp - timestamp in MachAbsoluteTime units to be used as the basis for a delta stackshot
224*043036a2SApple OSS Distributions  *
225*043036a2SApple OSS Distributions  * Outputs:  -1  if the passed stackshot config is NULL or there is existing stackshot buffer set.
226*043036a2SApple OSS Distributions  *           0 on success
227*043036a2SApple OSS Distributions  */
228*043036a2SApple OSS Distributions int
stackshot_config_set_delta_timestamp(stackshot_config_t * stackshot_config,uint64_t delta_timestamp)229*043036a2SApple OSS Distributions stackshot_config_set_delta_timestamp(stackshot_config_t *stackshot_config, uint64_t delta_timestamp)
230*043036a2SApple OSS Distributions {
231*043036a2SApple OSS Distributions 	if (stackshot_config == NULL || (void *)stackshot_config->sc_buffer != NULL) {
232*043036a2SApple OSS Distributions 		return -1;
233*043036a2SApple OSS Distributions 	}
234*043036a2SApple OSS Distributions 
235*043036a2SApple OSS Distributions 	stackshot_config->sc_delta_timestamp = delta_timestamp;
236*043036a2SApple OSS Distributions 
237*043036a2SApple OSS Distributions 	return 0;
238*043036a2SApple OSS Distributions }
239*043036a2SApple OSS Distributions 
240*043036a2SApple OSS Distributions /*
241*043036a2SApple OSS Distributions  * stackshot_config_set_pagetable_mask: set the level mask for pagetable dumping
242*043036a2SApple OSS Distributions  *
243*043036a2SApple OSS Distributions  * Each bit of the mask corresponds to a level in the paging structure. Bit 0
244*043036a2SApple OSS Distributions  * corresponds to Level 0, bit 1 to level 1, and so on. It is undefined what
245*043036a2SApple OSS Distributions  * happens when a bit is set that's higher than the current maximum level of
246*043036a2SApple OSS Distributions  * pagetable structures.
247*043036a2SApple OSS Distributions  *
248*043036a2SApple OSS Distributions  * When using this setter, you must also pass STACKSHOT_PAGE_TABLES as a flag
249*043036a2SApple OSS Distributions  * before invoking stackshot, otherwise this setter is a no-operation.
250*043036a2SApple OSS Distributions  *
251*043036a2SApple OSS Distributions  * Inputs:  stackshot_config - a pointer to a stackshot_config_t
252*043036a2SApple OSS Distributions  *          level_mask - the pagetable level mask, as described above
253*043036a2SApple OSS Distributions  *
254*043036a2SApple OSS Distributions  * Outputs:  -1  if the passed stackshot config is NULL or there is existing stackshot buffer set.
255*043036a2SApple OSS Distributions  *           0 on success
256*043036a2SApple OSS Distributions  */
257*043036a2SApple OSS Distributions int
stackshot_config_set_pagetable_mask(stackshot_config_t * stackshot_config,uint32_t pagetable_mask)258*043036a2SApple OSS Distributions stackshot_config_set_pagetable_mask(stackshot_config_t *stackshot_config, uint32_t pagetable_mask)
259*043036a2SApple OSS Distributions {
260*043036a2SApple OSS Distributions 	if (stackshot_config == NULL || (void *)stackshot_config->sc_buffer != NULL) {
261*043036a2SApple OSS Distributions 		return -1;
262*043036a2SApple OSS Distributions 	}
263*043036a2SApple OSS Distributions 
264*043036a2SApple OSS Distributions 	stackshot_config->sc_pagetable_mask = pagetable_mask;
265*043036a2SApple OSS Distributions 
266*043036a2SApple OSS Distributions 	return 0;
267*043036a2SApple OSS Distributions }
268*043036a2SApple OSS Distributions 
269*043036a2SApple OSS Distributions 
270*043036a2SApple OSS Distributions /*
271*043036a2SApple OSS Distributions  * stackshot_config_dealloc_buffer:  dealloc the stackshot buffer and reset the size so that a
272*043036a2SApple OSS Distributions  *   stackshot_config_t can be used again
273*043036a2SApple OSS Distributions  *
274*043036a2SApple OSS Distributions  * Inputs:   stackshot_config - a pointer to a stackshot_config_t
275*043036a2SApple OSS Distributions  *
276*043036a2SApple OSS Distributions  * Outputs:  EINVAL if the passed stackshot_config is NULL or if its buffer is NULL
277*043036a2SApple OSS Distributions  *           0 otherwise
278*043036a2SApple OSS Distributions  */
279*043036a2SApple OSS Distributions int
stackshot_config_dealloc_buffer(stackshot_config_t * stackshot_config)280*043036a2SApple OSS Distributions stackshot_config_dealloc_buffer(stackshot_config_t *stackshot_config)
281*043036a2SApple OSS Distributions {
282*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
283*043036a2SApple OSS Distributions 
284*043036a2SApple OSS Distributions 	if (stackshot_config == NULL) {
285*043036a2SApple OSS Distributions 		return EINVAL;
286*043036a2SApple OSS Distributions 	}
287*043036a2SApple OSS Distributions 	s_config = (stackshot_config_t *) stackshot_config;
288*043036a2SApple OSS Distributions 
289*043036a2SApple OSS Distributions 	if (s_config->sc_size && s_config->sc_buffer) {
290*043036a2SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(), (mach_vm_offset_t)s_config->sc_buffer, (mach_vm_size_t)s_config->sc_size);
291*043036a2SApple OSS Distributions 	}
292*043036a2SApple OSS Distributions 
293*043036a2SApple OSS Distributions 	s_config->sc_buffer = 0;
294*043036a2SApple OSS Distributions 	s_config->sc_size = 0;
295*043036a2SApple OSS Distributions 
296*043036a2SApple OSS Distributions 	return 0;
297*043036a2SApple OSS Distributions }
298*043036a2SApple OSS Distributions 
299*043036a2SApple OSS Distributions /*
300*043036a2SApple OSS Distributions  * stackshot_config_dealloc:	dealloc the stackshot buffer and the stackshot config
301*043036a2SApple OSS Distributions  *
302*043036a2SApple OSS Distributions  * Inputs:			stackshot_config - a pointer to a stackshot_config_t
303*043036a2SApple OSS Distributions  *
304*043036a2SApple OSS Distributions  * Outputs:			EINVAL if the passed stackshot_cofnig is NULL
305*043036a2SApple OSS Distributions  *				0 otherwise
306*043036a2SApple OSS Distributions  */
307*043036a2SApple OSS Distributions int
stackshot_config_dealloc(stackshot_config_t * stackshot_config)308*043036a2SApple OSS Distributions stackshot_config_dealloc(stackshot_config_t *stackshot_config)
309*043036a2SApple OSS Distributions {
310*043036a2SApple OSS Distributions 	stackshot_config_t *s_config;
311*043036a2SApple OSS Distributions 
312*043036a2SApple OSS Distributions 	if (stackshot_config == NULL) {
313*043036a2SApple OSS Distributions 		return EINVAL;
314*043036a2SApple OSS Distributions 	}
315*043036a2SApple OSS Distributions 	s_config = (stackshot_config_t *) stackshot_config;
316*043036a2SApple OSS Distributions 
317*043036a2SApple OSS Distributions 	if (s_config->sc_size && s_config->sc_buffer) {
318*043036a2SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(), (mach_vm_offset_t)s_config->sc_buffer, (mach_vm_size_t)s_config->sc_size);
319*043036a2SApple OSS Distributions 	}
320*043036a2SApple OSS Distributions 
321*043036a2SApple OSS Distributions 	s_config->sc_buffer = 0;
322*043036a2SApple OSS Distributions 	s_config->sc_size = 0;
323*043036a2SApple OSS Distributions 
324*043036a2SApple OSS Distributions 	free(s_config);
325*043036a2SApple OSS Distributions 	return 0;
326*043036a2SApple OSS Distributions }
327