2 * Copyright (c) 2011 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* Collect kernel callstacks */
31 #include <mach/mach_types.h>
32 #include <kern/thread.h>
33 #include <kern/backtrace.h>
34 #include <vm/vm_map.h>
35 #include <kperf/buffer.h>
36 #include <kperf/context.h>
37 #include <kperf/callstack.h>
38 #include <kperf/ast.h>
39 #include <sys/errno.h>
41 #if defined(__arm__) || defined(__arm64__)
42 #include <arm/cpu_data.h>
43 #include <arm/cpu_data_internal.h>
47 callstack_fixup_user(struct kp_ucallstack
*cs
, thread_t thread
)
49 uint64_t fixup_val
= 0;
50 assert(cs
->kpuc_nframes
< MAX_UCALLSTACK_FRAMES
);
52 #if defined(__x86_64__)
55 x86_saved_state_t
*state
;
57 state
= get_user_regs(thread
);
62 user_64
= is_saved_state64(state
);
64 sp_user
= saved_state64(state
)->isf
.rsp
;
66 sp_user
= saved_state32(state
)->uesp
;
69 if (thread
== current_thread()) {
70 (void)copyin(sp_user
, (char *)&fixup_val
,
71 user_64
? sizeof(uint64_t) : sizeof(uint32_t));
73 (void)vm_map_read_user(get_task_map(get_threadtask(thread
)), sp_user
,
74 &fixup_val
, user_64
? sizeof(uint64_t) : sizeof(uint32_t));
77 #elif defined(__arm64__) || defined(__arm__)
79 struct arm_saved_state
*state
= get_user_regs(thread
);
84 /* encode thumb mode into low bit of PC */
85 if (get_saved_state_cpsr(state
) & PSR_TF
) {
86 cs
->kpuc_frames
[0] |= 1ULL;
89 fixup_val
= get_saved_state_lr(state
);
92 #error "callstack_fixup_user: unsupported architecture"
96 cs
->kpuc_frames
[cs
->kpuc_nframes
++] = fixup_val
;
99 #if defined(__x86_64__)
101 __attribute__((used
))
103 interrupted_kernel_sp_value(uintptr_t *sp_val
)
105 x86_saved_state_t
*state
;
109 uintptr_t top
, bottom
;
111 state
= current_cpu_datap()->cpu_int_state
;
116 state_64
= is_saved_state64(state
);
119 cs
= saved_state64(state
)->isf
.cs
;
121 cs
= saved_state32(state
)->cs
;
123 /* return early if interrupted a thread in user space */
124 if ((cs
& SEL_PL
) == SEL_PL_U
) {
129 sp
= saved_state64(state
)->isf
.rsp
;
131 sp
= saved_state32(state
)->uesp
;
134 /* make sure the stack pointer is pointing somewhere in this stack */
135 bottom
= current_thread()->kernel_stack
;
136 top
= bottom
+ kernel_stack_size
;
137 if (sp
>= bottom
&& sp
< top
) {
141 *sp_val
= *(uintptr_t *)sp
;
145 #elif defined(__arm64__)
147 __attribute__((used
))
149 interrupted_kernel_lr(uintptr_t *lr
)
151 struct arm_saved_state
*state
;
153 state
= getCpuDatap()->cpu_int_state
;
155 /* return early if interrupted a thread in user space */
156 if (PSR64_IS_USER(get_saved_state_cpsr(state
))) {
160 *lr
= get_saved_state_lr(state
);
164 #elif defined(__arm__)
166 __attribute__((used
))
168 interrupted_kernel_lr(uintptr_t *lr
)
170 struct arm_saved_state
*state
;
172 state
= getCpuDatap()->cpu_int_state
;
174 /* return early if interrupted a thread in user space */
175 if (PSR_IS_USER(get_saved_state_cpsr(state
))) {
179 *lr
= get_saved_state_lr(state
);
183 #else /* defined(__arm__) */
184 #error "interrupted_kernel_{sp,lr}: unsupported architecture"
185 #endif /* !defined(__arm__) */
189 callstack_fixup_interrupted(struct kp_kcallstack
*cs
)
191 uintptr_t fixup_val
= 0;
192 assert(cs
->kpkc_nframes
< MAX_KCALLSTACK_FRAMES
);
195 * Only provide arbitrary data on development or debug kernels.
197 #if DEVELOPMENT || DEBUG
198 #if defined(__x86_64__)
199 (void)interrupted_kernel_sp_value(&fixup_val
);
200 #elif defined(__arm64__) || defined(__arm__)
201 (void)interrupted_kernel_lr(&fixup_val
);
202 #endif /* defined(__x86_64__) */
203 #endif /* DEVELOPMENT || DEBUG */
205 assert(cs
->kpkc_flags
& CALLSTACK_KERNEL
);
206 cs
->kpkc_frames
[cs
->kpkc_nframes
++] = fixup_val
;
210 kperf_continuation_sample(struct kp_kcallstack
*cs
, struct kperf_context
*context
)
215 assert(context
!= NULL
);
217 thread
= context
->cur_thread
;
218 assert(thread
!= NULL
);
219 assert(thread
->continuation
!= NULL
);
221 cs
->kpkc_flags
= CALLSTACK_CONTINUATION
| CALLSTACK_VALID
| CALLSTACK_KERNEL
;
223 cs
->kpkc_flags
|= CALLSTACK_64BIT
;
226 cs
->kpkc_nframes
= 1;
227 cs
->kpkc_frames
[0] = VM_KERNEL_UNSLIDE(thread
->continuation
);
231 kperf_backtrace_sample(struct kp_kcallstack
*cs
, struct kperf_context
*context
)
234 assert(context
!= NULL
);
235 assert(context
->cur_thread
== current_thread());
237 cs
->kpkc_flags
= CALLSTACK_KERNEL
| CALLSTACK_KERNEL_WORDS
;
239 cs
->kpkc_flags
|= CALLSTACK_64BIT
;
242 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_START
, 1);
245 cs
->kpkc_nframes
= backtrace_frame(cs
->kpkc_word_frames
,
246 cs
->kpkc_nframes
- 1, context
->starting_fp
, &trunc
);
247 if (cs
->kpkc_nframes
> 0) {
248 cs
->kpkc_flags
|= CALLSTACK_VALID
;
250 * Fake the value pointed to by the stack pointer or the link
251 * register for symbolicators.
253 cs
->kpkc_word_frames
[cs
->kpkc_nframes
+ 1] = 0;
254 cs
->kpkc_nframes
+= 1;
257 cs
->kpkc_nframes
|= CALLSTACK_TRUNCATED
;
260 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_END
, cs
->kpkc_nframes
);
263 kern_return_t
chudxnu_thread_get_callstack64_kperf(thread_t thread
,
264 uint64_t *callStack
, mach_msg_type_number_t
*count
,
265 boolean_t user_only
);
268 kperf_kcallstack_sample(struct kp_kcallstack
*cs
, struct kperf_context
*context
)
273 assert(context
!= NULL
);
274 assert(cs
->kpkc_nframes
<= MAX_KCALLSTACK_FRAMES
);
276 thread
= context
->cur_thread
;
277 assert(thread
!= NULL
);
279 BUF_INFO(PERF_CS_KSAMPLE
| DBG_FUNC_START
, (uintptr_t)thread_tid(thread
),
282 cs
->kpkc_flags
= CALLSTACK_KERNEL
;
284 cs
->kpkc_flags
|= CALLSTACK_64BIT
;
287 if (ml_at_interrupt_context()) {
288 assert(thread
== current_thread());
289 cs
->kpkc_flags
|= CALLSTACK_KERNEL_WORDS
;
291 cs
->kpkc_nframes
= backtrace_interrupted(
292 cs
->kpkc_word_frames
, cs
->kpkc_nframes
- 1, &trunc
);
293 if (cs
->kpkc_nframes
!= 0) {
294 callstack_fixup_interrupted(cs
);
297 cs
->kpkc_flags
|= CALLSTACK_TRUNCATED
;
301 * Rely on legacy CHUD backtracer to backtrace kernel stacks on
305 kr
= chudxnu_thread_get_callstack64_kperf(thread
,
306 cs
->kpkc_frames
, &cs
->kpkc_nframes
, FALSE
);
307 if (kr
== KERN_SUCCESS
) {
308 cs
->kpkc_flags
|= CALLSTACK_VALID
;
309 } else if (kr
== KERN_RESOURCE_SHORTAGE
) {
310 cs
->kpkc_flags
|= CALLSTACK_VALID
;
311 cs
->kpkc_flags
|= CALLSTACK_TRUNCATED
;
313 cs
->kpkc_nframes
= 0;
317 if (!(cs
->kpkc_flags
& CALLSTACK_VALID
)) {
318 BUF_INFO(PERF_CS_ERROR
, ERR_GETSTACK
);
321 BUF_INFO(PERF_CS_KSAMPLE
| DBG_FUNC_END
, (uintptr_t)thread_tid(thread
),
322 cs
->kpkc_flags
, cs
->kpkc_nframes
);
326 kperf_ucallstack_sample(struct kp_ucallstack
*cs
, struct kperf_context
*context
)
328 assert(ml_get_interrupts_enabled() == TRUE
);
330 thread_t thread
= context
->cur_thread
;
331 assert(thread
!= NULL
);
333 BUF_INFO(PERF_CS_USAMPLE
| DBG_FUNC_START
,
334 (uintptr_t)thread_tid(thread
), cs
->kpuc_nframes
);
338 int err
= backtrace_thread_user(thread
, cs
->kpuc_frames
,
339 cs
->kpuc_nframes
- 1, &cs
->kpuc_nframes
, &user64
, &trunc
);
340 cs
->kpuc_flags
= CALLSTACK_KERNEL_WORDS
;
342 cs
->kpuc_flags
|= CALLSTACK_64BIT
;
345 cs
->kpuc_flags
|= CALLSTACK_TRUNCATED
;
348 if (!err
|| err
== EFAULT
) {
349 callstack_fixup_user(cs
, thread
);
350 cs
->kpuc_flags
|= CALLSTACK_VALID
;
352 cs
->kpuc_nframes
= 0;
353 BUF_INFO(PERF_CS_ERROR
, ERR_GETSTACK
, err
);
356 BUF_INFO(PERF_CS_USAMPLE
| DBG_FUNC_END
, (uintptr_t)thread_tid(thread
),
357 cs
->kpuc_flags
, cs
->kpuc_nframes
);
360 static inline uintptr_t
361 scrub_word(uintptr_t *bt
, int n_frames
, int frame
, bool kern
)
363 if (frame
< n_frames
) {
365 return VM_KERNEL_UNSLIDE(bt
[frame
]);
374 static inline uintptr_t
375 scrub_frame(uint64_t *bt
, int n_frames
, int frame
)
377 if (frame
< n_frames
) {
378 return (uintptr_t)(bt
[frame
]);
385 callstack_log(uint32_t hdrid
, uint32_t dataid
, void *vframes
,
386 unsigned int nframes
, unsigned int flags
)
388 BUF_VERB(PERF_CS_LOG
| DBG_FUNC_START
, flags
, nframes
);
390 BUF_DATA(hdrid
, flags
, nframes
);
392 unsigned int nevts
= nframes
/ 4;
393 unsigned int ovf
= nframes
% 4;
398 bool kern
= flags
& CALLSTACK_KERNEL
;
400 if (flags
& CALLSTACK_KERNEL_WORDS
) {
401 uintptr_t *frames
= vframes
;
402 for (unsigned int i
= 0; i
< nevts
; i
++) {
403 unsigned int j
= i
* 4;
405 scrub_word(frames
, nframes
, j
+ 0, kern
),
406 scrub_word(frames
, nframes
, j
+ 1, kern
),
407 scrub_word(frames
, nframes
, j
+ 2, kern
),
408 scrub_word(frames
, nframes
, j
+ 3, kern
));
411 for (unsigned int i
= 0; i
< nevts
; i
++) {
412 uint64_t *frames
= vframes
;
413 unsigned int j
= i
* 4;
415 scrub_frame(frames
, nframes
, j
+ 0),
416 scrub_frame(frames
, nframes
, j
+ 1),
417 scrub_frame(frames
, nframes
, j
+ 2),
418 scrub_frame(frames
, nframes
, j
+ 3));
422 BUF_VERB(PERF_CS_LOG
| DBG_FUNC_END
, flags
, nframes
);
426 kperf_kcallstack_log(struct kp_kcallstack
*cs
)
428 callstack_log(PERF_CS_KHDR
, PERF_CS_KDATA
, cs
->kpkc_frames
,
429 cs
->kpkc_nframes
, cs
->kpkc_flags
);
433 kperf_ucallstack_log(struct kp_ucallstack
*cs
)
435 callstack_log(PERF_CS_UHDR
, PERF_CS_UDATA
, cs
->kpuc_frames
,
436 cs
->kpuc_nframes
, cs
->kpuc_flags
);
440 kperf_ucallstack_pend(struct kperf_context
* context
, uint32_t depth
)
442 int did_pend
= kperf_ast_pend(context
->cur_thread
, T_KPERF_AST_CALLSTACK
);
443 kperf_ast_set_callstack_depth(context
->cur_thread
, depth
);
449 chudxnu_kern_read(void *dstaddr
, vm_offset_t srcaddr
, vm_size_t size
)
451 return (ml_nofault_copy(srcaddr
, (vm_offset_t
)dstaddr
, size
) == size
) ?
452 KERN_SUCCESS
: KERN_FAILURE
;
462 //ppc version ported to arm
463 kern_return_t ret
= KERN_SUCCESS
;
465 if (ml_at_interrupt_context()) {
466 return KERN_FAILURE
; // can't look at tasks on interrupt stack
469 if (current_task() == task
) {
470 if (copyin(usraddr
, kernaddr
, size
)) {
474 vm_map_t map
= get_task_map(task
);
475 ret
= vm_map_read_user(map
, usraddr
, kernaddr
, size
);
481 static inline uint64_t
482 chudxnu_vm_unslide( uint64_t ptr
, int kaddr
)
488 return VM_KERNEL_UNSLIDE(ptr
);
492 #define ARM_SUPERVISOR_MODE(cpsr) ((((cpsr) & PSR_MODE_MASK) != PSR_USER_MODE) ? TRUE : FALSE)
493 #define CS_FLAG_EXTRASP 1 // capture extra sp register
495 chudxnu_thread_get_callstack64_internal(
498 mach_msg_type_number_t
*count
,
504 uint64_t currPC
= 0ULL, currLR
= 0ULL, currSP
= 0ULL;
505 uint64_t prevPC
= 0ULL;
506 uint32_t kernStackMin
= thread
->kernel_stack
;
507 uint32_t kernStackMax
= kernStackMin
+ kernel_stack_size
;
508 uint64_t *buffer
= callStack
;
511 int bufferMaxIndex
= 0;
512 boolean_t supervisor
= FALSE
;
513 struct arm_saved_state
*state
= NULL
;
514 uint32_t *fp
= NULL
, *nextFramePointer
= NULL
, *topfp
= NULL
;
517 task
= get_threadtask(thread
);
519 bufferMaxIndex
= *count
;
522 state
= find_user_regs(thread
);
524 state
= find_kern_regs(thread
);
532 /* make sure it is safe to dereference before you do it */
533 supervisor
= ARM_SUPERVISOR_MODE(state
->cpsr
);
535 /* can't take a kernel callstack if we've got a user frame */
536 if (!user_only
&& !supervisor
) {
541 * Reserve space for saving LR (and sometimes SP) at the end of the
544 if (flags
& CS_FLAG_EXTRASP
) {
550 if (bufferMaxIndex
< 2) {
552 return KERN_RESOURCE_SHORTAGE
;
555 currPC
= (uint64_t)state
->pc
; /* r15 */
556 if (state
->cpsr
& PSR_TF
) {
557 currPC
|= 1ULL; /* encode thumb mode into low bit of PC */
559 currLR
= (uint64_t)state
->lr
; /* r14 */
560 currSP
= (uint64_t)state
->sp
; /* r13 */
562 fp
= (uint32_t *)state
->r
[7]; /* frame pointer */
565 bufferIndex
= 0; // start with a stack of size zero
566 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currPC
, supervisor
); // save PC in position 0.
568 // Now, fill buffer with stack backtraces.
569 while (bufferIndex
< bufferMaxIndex
) {
572 * Below the frame pointer, the following values are saved:
577 * Note that we read the pc even for the first stack frame
578 * (which, in theory, is always empty because the callee fills
579 * it in just before it lowers the stack. However, if we
580 * catch the program in between filling in the return address
581 * and lowering the stack, we want to still have a valid
582 * backtrace. FixupStack correctly disregards this value if
586 if ((uint32_t)fp
== 0 || ((uint32_t)fp
& 0x3) != 0) {
587 /* frame pointer is invalid - stop backtracing */
593 if (((uint32_t)fp
> kernStackMax
) ||
594 ((uint32_t)fp
< kernStackMin
)) {
597 kr
= chudxnu_kern_read(&frame
,
599 (vm_size_t
)sizeof(frame
));
600 if (kr
== KERN_SUCCESS
) {
601 pc
= (uint64_t)frame
[1];
602 nextFramePointer
= (uint32_t *) (frame
[0]);
605 nextFramePointer
= 0ULL;
610 kr
= chudxnu_task_read(task
,
612 (((uint64_t)(uint32_t)fp
) & 0x00000000FFFFFFFFULL
),
614 if (kr
== KERN_SUCCESS
) {
615 pc
= (uint64_t) frame
[1];
616 nextFramePointer
= (uint32_t *) (frame
[0]);
619 nextFramePointer
= 0ULL;
624 if (kr
!= KERN_SUCCESS
) {
629 if (nextFramePointer
) {
630 buffer
[bufferIndex
++] = chudxnu_vm_unslide(pc
, supervisor
);
634 if (nextFramePointer
< fp
) {
637 fp
= nextFramePointer
;
641 if (bufferIndex
>= bufferMaxIndex
) {
642 bufferIndex
= bufferMaxIndex
;
643 kr
= KERN_RESOURCE_SHORTAGE
;
648 // Save link register and R13 (sp) at bottom of stack (used for later fixup).
649 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currLR
, supervisor
);
650 if (flags
& CS_FLAG_EXTRASP
) {
651 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currSP
, supervisor
);
654 *count
= bufferIndex
;
659 chudxnu_thread_get_callstack64_kperf(
662 mach_msg_type_number_t
*count
,
665 return chudxnu_thread_get_callstack64_internal( thread
, callStack
, count
, user_only
, 0 );
669 #if defined(HAS_APPLE_PAC)
673 // chudxnu_thread_get_callstack gathers a raw callstack along with any information needed to
674 // fix it up later (in case we stopped program as it was saving values into prev stack frame, etc.)
675 // after sampling has finished.
677 // For an N-entry callstack:
680 // [1..N-3] stack frames (including current one)
681 // [N-2] current LR (return value if we're in a leaf function)
682 // [N-1] current r0 (in case we've saved LR in r0) (optional)
685 #define ARM_SUPERVISOR_MODE(cpsr) ((((cpsr) & PSR_MODE_MASK) != PSR_USER_MODE) ? TRUE : FALSE)
687 #define CS_FLAG_EXTRASP 1 // capture extra sp register
690 chudxnu_thread_get_callstack64_internal(
693 mach_msg_type_number_t
*count
,
697 kern_return_t kr
= KERN_SUCCESS
;
699 uint64_t currPC
= 0ULL, currLR
= 0ULL, currSP
= 0ULL;
700 uint64_t prevPC
= 0ULL;
701 uint64_t kernStackMin
= thread
->kernel_stack
;
702 uint64_t kernStackMax
= kernStackMin
+ kernel_stack_size
;
703 uint64_t *buffer
= callStack
;
705 int bufferMaxIndex
= 0;
706 boolean_t kernel
= FALSE
;
707 struct arm_saved_state
*sstate
= NULL
;
710 task
= get_threadtask(thread
);
711 bufferMaxIndex
= *count
;
714 sstate
= find_user_regs(thread
);
716 sstate
= find_kern_regs(thread
);
724 if (is_saved_state64(sstate
)) {
725 struct arm_saved_state64
*state
= NULL
;
726 uint64_t *fp
= NULL
, *nextFramePointer
= NULL
, *topfp
= NULL
;
729 state
= saved_state64(sstate
);
731 /* make sure it is safe to dereference before you do it */
732 kernel
= PSR64_IS_KERNEL(state
->cpsr
);
734 /* can't take a kernel callstack if we've got a user frame */
735 if (!user_only
&& !kernel
) {
740 * Reserve space for saving LR (and sometimes SP) at the end of the
743 if (flags
& CS_FLAG_EXTRASP
) {
749 if (bufferMaxIndex
< 2) {
751 return KERN_RESOURCE_SHORTAGE
;
758 fp
= (uint64_t *)state
->fp
; /* frame pointer */
761 bufferIndex
= 0; // start with a stack of size zero
762 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currPC
, kernel
); // save PC in position 0.
764 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_START
, kernel
, 0);
766 // Now, fill buffer with stack backtraces.
767 while (bufferIndex
< bufferMaxIndex
) {
770 * Below the frame pointer, the following values are saved:
775 * Note that we read the pc even for the first stack frame
776 * (which, in theory, is always empty because the callee fills
777 * it in just before it lowers the stack. However, if we
778 * catch the program in between filling in the return address
779 * and lowering the stack, we want to still have a valid
780 * backtrace. FixupStack correctly disregards this value if
784 if ((uint64_t)fp
== 0 || ((uint64_t)fp
& 0x3) != 0) {
785 /* frame pointer is invalid - stop backtracing */
791 if (((uint64_t)fp
> kernStackMax
) ||
792 ((uint64_t)fp
< kernStackMin
)) {
795 kr
= chudxnu_kern_read(&frame
,
797 (vm_size_t
)sizeof(frame
));
798 if (kr
== KERN_SUCCESS
) {
799 #if defined(HAS_APPLE_PAC)
800 /* return addresses on stack will be signed by arm64e ABI */
801 pc
= (uint64_t)ptrauth_strip((void *)frame
[1], ptrauth_key_return_address
);
805 nextFramePointer
= (uint64_t *)frame
[0];
808 nextFramePointer
= 0ULL;
813 kr
= chudxnu_task_read(task
,
816 (vm_size_t
)sizeof(frame
));
817 if (kr
== KERN_SUCCESS
) {
818 #if defined(HAS_APPLE_PAC)
819 /* return addresses on stack will be signed by arm64e ABI */
820 pc
= (uint64_t)ptrauth_strip((void *)frame
[1], ptrauth_key_return_address
);
824 nextFramePointer
= (uint64_t *)(frame
[0]);
827 nextFramePointer
= 0ULL;
832 if (kr
!= KERN_SUCCESS
) {
837 if (nextFramePointer
) {
838 buffer
[bufferIndex
++] = chudxnu_vm_unslide(pc
, kernel
);
842 if (nextFramePointer
< fp
) {
845 fp
= nextFramePointer
;
849 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_END
, bufferIndex
);
851 if (bufferIndex
>= bufferMaxIndex
) {
852 bufferIndex
= bufferMaxIndex
;
853 kr
= KERN_RESOURCE_SHORTAGE
;
858 // Save link register and SP at bottom of stack (used for later fixup).
859 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currLR
, kernel
);
860 if (flags
& CS_FLAG_EXTRASP
) {
861 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currSP
, kernel
);
864 struct arm_saved_state32
*state
= NULL
;
865 uint32_t *fp
= NULL
, *nextFramePointer
= NULL
, *topfp
= NULL
;
867 /* 64-bit kernel stacks, 32-bit user stacks */
871 state
= saved_state32(sstate
);
873 /* make sure it is safe to dereference before you do it */
874 kernel
= ARM_SUPERVISOR_MODE(state
->cpsr
);
876 /* can't take a kernel callstack if we've got a user frame */
877 if (!user_only
&& !kernel
) {
882 * Reserve space for saving LR (and sometimes SP) at the end of the
885 if (flags
& CS_FLAG_EXTRASP
) {
891 if (bufferMaxIndex
< 2) {
893 return KERN_RESOURCE_SHORTAGE
;
896 currPC
= (uint64_t)state
->pc
; /* r15 */
897 if (state
->cpsr
& PSR_TF
) {
898 currPC
|= 1ULL; /* encode thumb mode into low bit of PC */
900 currLR
= (uint64_t)state
->lr
; /* r14 */
901 currSP
= (uint64_t)state
->sp
; /* r13 */
903 fp
= (uint32_t *)(uintptr_t)state
->r
[7]; /* frame pointer */
906 bufferIndex
= 0; // start with a stack of size zero
907 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currPC
, kernel
); // save PC in position 0.
909 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_START
, kernel
, 1);
911 // Now, fill buffer with stack backtraces.
912 while (bufferIndex
< bufferMaxIndex
) {
915 * Below the frame pointer, the following values are saved:
920 * Note that we read the pc even for the first stack frame
921 * (which, in theory, is always empty because the callee fills
922 * it in just before it lowers the stack. However, if we
923 * catch the program in between filling in the return address
924 * and lowering the stack, we want to still have a valid
925 * backtrace. FixupStack correctly disregards this value if
929 if ((uint32_t)fp
== 0 || ((uint32_t)fp
& 0x3) != 0) {
930 /* frame pointer is invalid - stop backtracing */
936 if (((uint32_t)fp
> kernStackMax
) ||
937 ((uint32_t)fp
< kernStackMin
)) {
940 kr
= chudxnu_kern_read(&frame
,
942 (vm_size_t
)sizeof(frame
));
943 if (kr
== KERN_SUCCESS
) {
944 pc
= (uint64_t)frame
[1];
945 nextFramePointer
= (uint32_t *) (frame
[0]);
948 nextFramePointer
= 0ULL;
953 kr
= chudxnu_task_read(task
,
955 (((uint64_t)(uint32_t)fp
) & 0x00000000FFFFFFFFULL
),
957 if (kr
== KERN_SUCCESS
) {
958 pc
= (uint64_t)frame32
[1];
959 nextFramePointer
= (uint32_t *)(uintptr_t)(frame32
[0]);
962 nextFramePointer
= 0ULL;
967 if (kr
!= KERN_SUCCESS
) {
972 if (nextFramePointer
) {
973 buffer
[bufferIndex
++] = chudxnu_vm_unslide(pc
, kernel
);
977 if (nextFramePointer
< fp
) {
980 fp
= nextFramePointer
;
984 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_END
, bufferIndex
);
986 /* clamp callstack size to max */
987 if (bufferIndex
>= bufferMaxIndex
) {
988 bufferIndex
= bufferMaxIndex
;
989 kr
= KERN_RESOURCE_SHORTAGE
;
991 /* ignore all other failures */
995 // Save link register and R13 (sp) at bottom of stack (used for later fixup).
996 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currLR
, kernel
);
997 if (flags
& CS_FLAG_EXTRASP
) {
998 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currSP
, kernel
);
1002 *count
= bufferIndex
;
1007 chudxnu_thread_get_callstack64_kperf(
1009 uint64_t *callStack
,
1010 mach_msg_type_number_t
*count
,
1011 boolean_t user_only
)
1013 return chudxnu_thread_get_callstack64_internal( thread
, callStack
, count
, user_only
, 0 );
1017 #define VALID_STACK_ADDRESS(supervisor, addr, minKernAddr, maxKernAddr) (supervisor ? (addr>=minKernAddr && addr<=maxKernAddr) : TRUE)
1018 // don't try to read in the hole
1019 #define VALID_STACK_ADDRESS64(supervisor, addr, minKernAddr, maxKernAddr) \
1020 (supervisor ? ((uint64_t)addr >= minKernAddr && (uint64_t)addr <= maxKernAddr) : \
1021 ((uint64_t)addr != 0ULL && ((uint64_t)addr <= 0x00007FFFFFFFFFFFULL || (uint64_t)addr >= 0xFFFF800000000000ULL)))
1023 typedef struct _cframe64_t
{
1024 uint64_t prevFP
; // can't use a real pointer here until we're a 64 bit kernel
1030 typedef struct _cframe_t
{
1031 uint32_t prev
; // this is really a user32-space pointer to the previous frame
1036 extern void * find_user_regs(thread_t
);
1037 extern x86_saved_state32_t
*find_kern_regs(thread_t
);
1039 static kern_return_t
1040 do_kernel_backtrace(
1042 struct x86_kernel_state
*regs
,
1044 mach_msg_type_number_t
*start_idx
,
1045 mach_msg_type_number_t max_idx
)
1047 uint64_t kernStackMin
= (uint64_t)thread
->kernel_stack
;
1048 uint64_t kernStackMax
= (uint64_t)kernStackMin
+ kernel_stack_size
;
1049 mach_msg_type_number_t ct
= *start_idx
;
1050 kern_return_t kr
= KERN_FAILURE
;
1053 uint64_t currPC
= 0ULL;
1054 uint64_t currFP
= 0ULL;
1055 uint64_t prevPC
= 0ULL;
1056 uint64_t prevFP
= 0ULL;
1057 if (KERN_SUCCESS
!= chudxnu_kern_read(&currPC
, (vm_offset_t
)&(regs
->k_rip
), sizeof(uint64_t))) {
1058 return KERN_FAILURE
;
1060 if (KERN_SUCCESS
!= chudxnu_kern_read(&currFP
, (vm_offset_t
)&(regs
->k_rbp
), sizeof(uint64_t))) {
1061 return KERN_FAILURE
;
1064 uint32_t currPC
= 0U;
1065 uint32_t currFP
= 0U;
1066 uint32_t prevPC
= 0U;
1067 uint32_t prevFP
= 0U;
1068 if (KERN_SUCCESS
!= chudxnu_kern_read(&currPC
, (vm_offset_t
)&(regs
->k_eip
), sizeof(uint32_t))) {
1069 return KERN_FAILURE
;
1071 if (KERN_SUCCESS
!= chudxnu_kern_read(&currFP
, (vm_offset_t
)&(regs
->k_ebp
), sizeof(uint32_t))) {
1072 return KERN_FAILURE
;
1076 if (*start_idx
>= max_idx
) {
1077 return KERN_RESOURCE_SHORTAGE
; // no frames traced
1080 return KERN_FAILURE
;
1083 frames
[ct
++] = chudxnu_vm_unslide((uint64_t)currPC
, 1);
1085 // build a backtrace of this kernel state
1087 while (VALID_STACK_ADDRESS64(TRUE
, currFP
, kernStackMin
, kernStackMax
)) {
1088 // this is the address where caller lives in the user thread
1089 uint64_t caller
= currFP
+ sizeof(uint64_t);
1091 while (VALID_STACK_ADDRESS(TRUE
, currFP
, kernStackMin
, kernStackMax
)) {
1092 uint32_t caller
= (uint32_t)currFP
+ sizeof(uint32_t);
1095 if (!currFP
|| !currPC
) {
1100 if (ct
>= max_idx
) {
1102 return KERN_RESOURCE_SHORTAGE
;
1105 /* read our caller */
1106 kr
= chudxnu_kern_read(&currPC
, (vm_offset_t
)caller
, sizeof(currPC
));
1108 if (kr
!= KERN_SUCCESS
|| !currPC
) {
1114 * retrive contents of the frame pointer and advance to the next stack
1115 * frame if it's valid
1118 kr
= chudxnu_kern_read(&prevFP
, (vm_offset_t
)currFP
, sizeof(currPC
));
1121 if (VALID_STACK_ADDRESS64(TRUE
, prevFP
, kernStackMin
, kernStackMax
)) {
1123 if (VALID_STACK_ADDRESS(TRUE
, prevFP
, kernStackMin
, kernStackMax
)) {
1125 frames
[ct
++] = chudxnu_vm_unslide((uint64_t)currPC
, 1);
1128 if (prevFP
<= currFP
) {
1136 return KERN_SUCCESS
;
1141 static kern_return_t
1145 x86_saved_state32_t
*regs
,
1147 mach_msg_type_number_t
*start_idx
,
1148 mach_msg_type_number_t max_idx
,
1149 boolean_t supervisor
)
1151 uint32_t tmpWord
= 0UL;
1152 uint64_t currPC
= (uint64_t) regs
->eip
;
1153 uint64_t currFP
= (uint64_t) regs
->ebp
;
1154 uint64_t prevPC
= 0ULL;
1155 uint64_t prevFP
= 0ULL;
1156 uint64_t kernStackMin
= thread
->kernel_stack
;
1157 uint64_t kernStackMax
= kernStackMin
+ kernel_stack_size
;
1158 mach_msg_type_number_t ct
= *start_idx
;
1159 kern_return_t kr
= KERN_FAILURE
;
1161 if (ct
>= max_idx
) {
1162 return KERN_RESOURCE_SHORTAGE
; // no frames traced
1164 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1166 // build a backtrace of this 32 bit state.
1167 while (VALID_STACK_ADDRESS(supervisor
, currFP
, kernStackMin
, kernStackMax
)) {
1168 cframe_t
*fp
= (cframe_t
*) (uintptr_t) currFP
;
1175 if (ct
>= max_idx
) {
1177 return KERN_RESOURCE_SHORTAGE
;
1180 /* read our caller */
1182 kr
= chudxnu_kern_read(&tmpWord
, (vm_offset_t
) &fp
->caller
, sizeof(uint32_t));
1184 kr
= chudxnu_task_read(task
, &tmpWord
, (vm_offset_t
) &fp
->caller
, sizeof(uint32_t));
1187 if (kr
!= KERN_SUCCESS
) {
1192 currPC
= (uint64_t) tmpWord
; // promote 32 bit address
1195 * retrive contents of the frame pointer and advance to the next stack
1196 * frame if it's valid
1200 kr
= chudxnu_kern_read(&tmpWord
, (vm_offset_t
)&fp
->prev
, sizeof(uint32_t));
1202 kr
= chudxnu_task_read(task
, &tmpWord
, (vm_offset_t
)&fp
->prev
, sizeof(uint32_t));
1204 prevFP
= (uint64_t) tmpWord
; // promote 32 bit address
1207 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1210 if (prevFP
< currFP
) {
1218 return KERN_SUCCESS
;
1221 static kern_return_t
1225 x86_saved_state64_t
*regs
,
1227 mach_msg_type_number_t
*start_idx
,
1228 mach_msg_type_number_t max_idx
,
1229 boolean_t supervisor
)
1231 uint64_t currPC
= regs
->isf
.rip
;
1232 uint64_t currFP
= regs
->rbp
;
1233 uint64_t prevPC
= 0ULL;
1234 uint64_t prevFP
= 0ULL;
1235 uint64_t kernStackMin
= (uint64_t)thread
->kernel_stack
;
1236 uint64_t kernStackMax
= (uint64_t)kernStackMin
+ kernel_stack_size
;
1237 mach_msg_type_number_t ct
= *start_idx
;
1238 kern_return_t kr
= KERN_FAILURE
;
1240 if (*start_idx
>= max_idx
) {
1241 return KERN_RESOURCE_SHORTAGE
; // no frames traced
1243 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1245 // build a backtrace of this 32 bit state.
1246 while (VALID_STACK_ADDRESS64(supervisor
, currFP
, kernStackMin
, kernStackMax
)) {
1247 // this is the address where caller lives in the user thread
1248 uint64_t caller
= currFP
+ sizeof(uint64_t);
1255 if (ct
>= max_idx
) {
1257 return KERN_RESOURCE_SHORTAGE
;
1260 /* read our caller */
1262 kr
= chudxnu_kern_read(&currPC
, (vm_offset_t
)caller
, sizeof(uint64_t));
1264 kr
= chudxnu_task_read(task
, &currPC
, caller
, sizeof(uint64_t));
1267 if (kr
!= KERN_SUCCESS
) {
1273 * retrive contents of the frame pointer and advance to the next stack
1274 * frame if it's valid
1278 kr
= chudxnu_kern_read(&prevFP
, (vm_offset_t
)currFP
, sizeof(uint64_t));
1280 kr
= chudxnu_task_read(task
, &prevFP
, currFP
, sizeof(uint64_t));
1283 if (VALID_STACK_ADDRESS64(supervisor
, prevFP
, kernStackMin
, kernStackMax
)) {
1284 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1287 if (prevFP
< currFP
) {
1295 return KERN_SUCCESS
;
1298 static kern_return_t
1299 chudxnu_thread_get_callstack64_internal(
1301 uint64_t *callstack
,
1302 mach_msg_type_number_t
*count
,
1303 boolean_t user_only
,
1304 boolean_t kern_only
)
1306 kern_return_t kr
= KERN_FAILURE
;
1307 task_t task
= thread
->task
;
1308 uint64_t currPC
= 0ULL;
1309 boolean_t supervisor
= FALSE
;
1310 mach_msg_type_number_t bufferIndex
= 0;
1311 mach_msg_type_number_t bufferMaxIndex
= *count
;
1312 x86_saved_state_t
*tagged_regs
= NULL
; // kernel register state
1313 x86_saved_state64_t
*regs64
= NULL
;
1314 x86_saved_state32_t
*regs32
= NULL
;
1315 x86_saved_state32_t
*u_regs32
= NULL
;
1316 x86_saved_state64_t
*u_regs64
= NULL
;
1317 struct x86_kernel_state
*kregs
= NULL
;
1319 if (ml_at_interrupt_context()) {
1321 /* can't backtrace user state on interrupt stack. */
1322 return KERN_FAILURE
;
1325 /* backtracing at interrupt context? */
1326 if (thread
== current_thread() && current_cpu_datap()->cpu_int_state
) {
1328 * Locate the registers for the interrupted thread, assuming it is
1331 tagged_regs
= current_cpu_datap()->cpu_int_state
;
1333 if (is_saved_state64(tagged_regs
)) {
1334 /* 64 bit registers */
1335 regs64
= saved_state64(tagged_regs
);
1336 supervisor
= ((regs64
->isf
.cs
& SEL_PL
) != SEL_PL_U
);
1338 /* 32 bit registers */
1339 regs32
= saved_state32(tagged_regs
);
1340 supervisor
= ((regs32
->cs
& SEL_PL
) != SEL_PL_U
);
1345 if (!ml_at_interrupt_context() && kernel_task
== task
) {
1346 if (!thread
->kernel_stack
) {
1347 return KERN_FAILURE
;
1350 // Kernel thread not at interrupt context
1351 kregs
= (struct x86_kernel_state
*)NULL
;
1353 // nofault read of the thread->kernel_stack pointer
1354 if (KERN_SUCCESS
!= chudxnu_kern_read(&kregs
, (vm_offset_t
)&(thread
->kernel_stack
), sizeof(void *))) {
1355 return KERN_FAILURE
;
1358 // Adjust to find the saved kernel state
1359 kregs
= STACK_IKS((vm_offset_t
)(uintptr_t)kregs
);
1362 } else if (!tagged_regs
) {
1364 * not at interrupt context, or tracing a different thread than
1365 * current_thread() at interrupt context
1367 tagged_regs
= USER_STATE(thread
);
1368 if (is_saved_state64(tagged_regs
)) {
1369 /* 64 bit registers */
1370 regs64
= saved_state64(tagged_regs
);
1371 supervisor
= ((regs64
->isf
.cs
& SEL_PL
) != SEL_PL_U
);
1373 /* 32 bit registers */
1374 regs32
= saved_state32(tagged_regs
);
1375 supervisor
= ((regs32
->cs
& SEL_PL
) != SEL_PL_U
);
1382 // the caller only wants a user callstack.
1384 // bail - we've only got kernel state
1385 return KERN_FAILURE
;
1388 // regs32(64) is not in supervisor mode.
1396 /* we only want to backtrace the user mode */
1397 if (!(u_regs32
|| u_regs64
)) {
1398 /* no user state to look at */
1399 return KERN_FAILURE
;
1404 * Order of preference for top of stack:
1405 * 64 bit kernel state (not likely)
1406 * 32 bit kernel state
1407 * 64 bit user land state
1408 * 32 bit user land state
1413 * nofault read of the registers from the kernel stack (as they can
1414 * disappear on the fly).
1417 if (KERN_SUCCESS
!= chudxnu_kern_read(&currPC
, (vm_offset_t
)&(kregs
->k_rip
), sizeof(uint64_t))) {
1418 return KERN_FAILURE
;
1420 } else if (regs64
) {
1421 currPC
= regs64
->isf
.rip
;
1422 } else if (regs32
) {
1423 currPC
= (uint64_t) regs32
->eip
;
1424 } else if (u_regs64
) {
1425 currPC
= u_regs64
->isf
.rip
;
1426 } else if (u_regs32
) {
1427 currPC
= (uint64_t) u_regs32
->eip
;
1431 /* no top of the stack, bail out */
1432 return KERN_FAILURE
;
1437 if (bufferMaxIndex
< 1) {
1439 return KERN_RESOURCE_SHORTAGE
;
1442 /* backtrace kernel */
1444 addr64_t address
= 0ULL;
1448 kr
= do_kernel_backtrace(thread
, kregs
, callstack
, &bufferIndex
, bufferMaxIndex
);
1450 // and do a nofault read of (r|e)sp
1451 uint64_t rsp
= 0ULL;
1452 size
= sizeof(uint64_t);
1454 if (KERN_SUCCESS
!= chudxnu_kern_read(&address
, (vm_offset_t
)&(kregs
->k_rsp
), size
)) {
1458 if (address
&& KERN_SUCCESS
== chudxnu_kern_read(&rsp
, (vm_offset_t
)address
, size
) && bufferIndex
< bufferMaxIndex
) {
1459 callstack
[bufferIndex
++] = (uint64_t)rsp
;
1461 } else if (regs64
) {
1462 uint64_t rsp
= 0ULL;
1464 // backtrace the 64bit side.
1465 kr
= do_backtrace64(task
, thread
, regs64
, callstack
, &bufferIndex
,
1466 bufferMaxIndex
- 1, TRUE
);
1468 if (KERN_SUCCESS
== chudxnu_kern_read(&rsp
, (vm_offset_t
) regs64
->isf
.rsp
, sizeof(uint64_t)) &&
1469 bufferIndex
< bufferMaxIndex
) {
1470 callstack
[bufferIndex
++] = rsp
;
1472 } else if (regs32
) {
1475 // backtrace the 32bit side.
1476 kr
= do_backtrace32(task
, thread
, regs32
, callstack
, &bufferIndex
,
1477 bufferMaxIndex
- 1, TRUE
);
1479 if (KERN_SUCCESS
== chudxnu_kern_read(&esp
, (vm_offset_t
) regs32
->uesp
, sizeof(uint32_t)) &&
1480 bufferIndex
< bufferMaxIndex
) {
1481 callstack
[bufferIndex
++] = (uint64_t) esp
;
1483 } else if (u_regs64
&& !kern_only
) {
1484 /* backtrace user land */
1485 uint64_t rsp
= 0ULL;
1487 kr
= do_backtrace64(task
, thread
, u_regs64
, callstack
, &bufferIndex
,
1488 bufferMaxIndex
- 1, FALSE
);
1490 if (KERN_SUCCESS
== chudxnu_task_read(task
, &rsp
, (addr64_t
) u_regs64
->isf
.rsp
, sizeof(uint64_t)) &&
1491 bufferIndex
< bufferMaxIndex
) {
1492 callstack
[bufferIndex
++] = rsp
;
1494 } else if (u_regs32
&& !kern_only
) {
1497 kr
= do_backtrace32(task
, thread
, u_regs32
, callstack
, &bufferIndex
,
1498 bufferMaxIndex
- 1, FALSE
);
1500 if (KERN_SUCCESS
== chudxnu_task_read(task
, &esp
, (addr64_t
) u_regs32
->uesp
, sizeof(uint32_t)) &&
1501 bufferIndex
< bufferMaxIndex
) {
1502 callstack
[bufferIndex
++] = (uint64_t) esp
;
1506 *count
= bufferIndex
;
1512 chudxnu_thread_get_callstack64_kperf(
1514 uint64_t *callstack
,
1515 mach_msg_type_number_t
*count
,
1518 return chudxnu_thread_get_callstack64_internal(thread
, callstack
, count
, is_user
, !is_user
);
1520 #else /* !__arm__ && !__arm64__ && !__x86_64__ */
1521 #error kperf: unsupported architecture
1522 #endif /* !__arm__ && !__arm64__ && !__x86_64__ */