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