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