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_flags
|= 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
,
441 unsigned int actionid
)
446 kperf_ast_set_callstack_depth(context
->cur_thread
, depth
);
447 return kperf_ast_pend(context
->cur_thread
, T_KPERF_AST_CALLSTACK
,
452 chudxnu_kern_read(void *dstaddr
, vm_offset_t srcaddr
, vm_size_t size
)
454 return (ml_nofault_copy(srcaddr
, (vm_offset_t
)dstaddr
, size
) == size
) ?
455 KERN_SUCCESS
: KERN_FAILURE
;
465 //ppc version ported to arm
466 kern_return_t ret
= KERN_SUCCESS
;
468 if (ml_at_interrupt_context()) {
469 return KERN_FAILURE
; // can't look at tasks on interrupt stack
472 if (current_task() == task
) {
473 if (copyin(usraddr
, kernaddr
, size
)) {
477 vm_map_t map
= get_task_map(task
);
478 ret
= vm_map_read_user(map
, usraddr
, kernaddr
, size
);
484 static inline uint64_t
485 chudxnu_vm_unslide( uint64_t ptr
, int kaddr
)
491 return VM_KERNEL_UNSLIDE(ptr
);
495 #define ARM_SUPERVISOR_MODE(cpsr) ((((cpsr) & PSR_MODE_MASK) != PSR_USER_MODE) ? TRUE : FALSE)
496 #define CS_FLAG_EXTRASP 1 // capture extra sp register
498 chudxnu_thread_get_callstack64_internal(
501 mach_msg_type_number_t
*count
,
507 uint64_t currPC
= 0ULL, currLR
= 0ULL, currSP
= 0ULL;
508 uint64_t prevPC
= 0ULL;
509 uint32_t kernStackMin
= thread
->kernel_stack
;
510 uint32_t kernStackMax
= kernStackMin
+ kernel_stack_size
;
511 uint64_t *buffer
= callStack
;
514 int bufferMaxIndex
= 0;
515 boolean_t supervisor
= FALSE
;
516 struct arm_saved_state
*state
= NULL
;
517 uint32_t *fp
= NULL
, *nextFramePointer
= NULL
, *topfp
= NULL
;
520 task
= get_threadtask(thread
);
522 bufferMaxIndex
= *count
;
525 state
= find_user_regs(thread
);
527 state
= find_kern_regs(thread
);
535 /* make sure it is safe to dereference before you do it */
536 supervisor
= ARM_SUPERVISOR_MODE(state
->cpsr
);
538 /* can't take a kernel callstack if we've got a user frame */
539 if (!user_only
&& !supervisor
) {
544 * Reserve space for saving LR (and sometimes SP) at the end of the
547 if (flags
& CS_FLAG_EXTRASP
) {
553 if (bufferMaxIndex
< 2) {
555 return KERN_RESOURCE_SHORTAGE
;
558 currPC
= (uint64_t)state
->pc
; /* r15 */
559 if (state
->cpsr
& PSR_TF
) {
560 currPC
|= 1ULL; /* encode thumb mode into low bit of PC */
562 currLR
= (uint64_t)state
->lr
; /* r14 */
563 currSP
= (uint64_t)state
->sp
; /* r13 */
565 fp
= (uint32_t *)state
->r
[7]; /* frame pointer */
568 bufferIndex
= 0; // start with a stack of size zero
569 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currPC
, supervisor
); // save PC in position 0.
571 // Now, fill buffer with stack backtraces.
572 while (bufferIndex
< bufferMaxIndex
) {
575 * Below the frame pointer, the following values are saved:
580 * Note that we read the pc even for the first stack frame
581 * (which, in theory, is always empty because the callee fills
582 * it in just before it lowers the stack. However, if we
583 * catch the program in between filling in the return address
584 * and lowering the stack, we want to still have a valid
585 * backtrace. FixupStack correctly disregards this value if
589 if ((uint32_t)fp
== 0 || ((uint32_t)fp
& 0x3) != 0) {
590 /* frame pointer is invalid - stop backtracing */
596 if (((uint32_t)fp
> kernStackMax
) ||
597 ((uint32_t)fp
< kernStackMin
)) {
600 kr
= chudxnu_kern_read(&frame
,
602 (vm_size_t
)sizeof(frame
));
603 if (kr
== KERN_SUCCESS
) {
604 pc
= (uint64_t)frame
[1];
605 nextFramePointer
= (uint32_t *) (frame
[0]);
608 nextFramePointer
= 0ULL;
613 kr
= chudxnu_task_read(task
,
615 (((uint64_t)(uint32_t)fp
) & 0x00000000FFFFFFFFULL
),
617 if (kr
== KERN_SUCCESS
) {
618 pc
= (uint64_t) frame
[1];
619 nextFramePointer
= (uint32_t *) (frame
[0]);
622 nextFramePointer
= 0ULL;
627 if (kr
!= KERN_SUCCESS
) {
632 if (nextFramePointer
) {
633 buffer
[bufferIndex
++] = chudxnu_vm_unslide(pc
, supervisor
);
637 if (nextFramePointer
< fp
) {
640 fp
= nextFramePointer
;
644 if (bufferIndex
>= bufferMaxIndex
) {
645 bufferIndex
= bufferMaxIndex
;
646 kr
= KERN_RESOURCE_SHORTAGE
;
651 // Save link register and R13 (sp) at bottom of stack (used for later fixup).
652 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currLR
, supervisor
);
653 if (flags
& CS_FLAG_EXTRASP
) {
654 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currSP
, supervisor
);
657 *count
= bufferIndex
;
662 chudxnu_thread_get_callstack64_kperf(
665 mach_msg_type_number_t
*count
,
668 return chudxnu_thread_get_callstack64_internal( thread
, callStack
, count
, user_only
, 0 );
672 #if defined(HAS_APPLE_PAC)
676 // chudxnu_thread_get_callstack gathers a raw callstack along with any information needed to
677 // fix it up later (in case we stopped program as it was saving values into prev stack frame, etc.)
678 // after sampling has finished.
680 // For an N-entry callstack:
683 // [1..N-3] stack frames (including current one)
684 // [N-2] current LR (return value if we're in a leaf function)
685 // [N-1] current r0 (in case we've saved LR in r0) (optional)
688 #define ARM_SUPERVISOR_MODE(cpsr) ((((cpsr) & PSR_MODE_MASK) != PSR_USER_MODE) ? TRUE : FALSE)
690 #define CS_FLAG_EXTRASP 1 // capture extra sp register
693 chudxnu_thread_get_callstack64_internal(
696 mach_msg_type_number_t
*count
,
700 kern_return_t kr
= KERN_SUCCESS
;
702 uint64_t currPC
= 0ULL, currLR
= 0ULL, currSP
= 0ULL;
703 uint64_t prevPC
= 0ULL;
704 uint64_t kernStackMin
= thread
->kernel_stack
;
705 uint64_t kernStackMax
= kernStackMin
+ kernel_stack_size
;
706 uint64_t *buffer
= callStack
;
708 int bufferMaxIndex
= 0;
709 boolean_t kernel
= FALSE
;
710 struct arm_saved_state
*sstate
= NULL
;
713 task
= get_threadtask(thread
);
714 bufferMaxIndex
= *count
;
717 sstate
= find_user_regs(thread
);
719 sstate
= find_kern_regs(thread
);
727 if (is_saved_state64(sstate
)) {
728 struct arm_saved_state64
*state
= NULL
;
729 uint64_t *fp
= NULL
, *nextFramePointer
= NULL
, *topfp
= NULL
;
732 state
= saved_state64(sstate
);
734 /* make sure it is safe to dereference before you do it */
735 kernel
= PSR64_IS_KERNEL(state
->cpsr
);
737 /* can't take a kernel callstack if we've got a user frame */
738 if (!user_only
&& !kernel
) {
743 * Reserve space for saving LR (and sometimes SP) at the end of the
746 if (flags
& CS_FLAG_EXTRASP
) {
752 if (bufferMaxIndex
< 2) {
754 return KERN_RESOURCE_SHORTAGE
;
761 fp
= (uint64_t *)state
->fp
; /* frame pointer */
764 bufferIndex
= 0; // start with a stack of size zero
765 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currPC
, kernel
); // save PC in position 0.
767 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_START
, kernel
, 0);
769 // Now, fill buffer with stack backtraces.
770 while (bufferIndex
< bufferMaxIndex
) {
773 * Below the frame pointer, the following values are saved:
778 * Note that we read the pc even for the first stack frame
779 * (which, in theory, is always empty because the callee fills
780 * it in just before it lowers the stack. However, if we
781 * catch the program in between filling in the return address
782 * and lowering the stack, we want to still have a valid
783 * backtrace. FixupStack correctly disregards this value if
787 if ((uint64_t)fp
== 0 || ((uint64_t)fp
& 0x3) != 0) {
788 /* frame pointer is invalid - stop backtracing */
794 if (((uint64_t)fp
> kernStackMax
) ||
795 ((uint64_t)fp
< kernStackMin
)) {
798 kr
= chudxnu_kern_read(&frame
,
800 (vm_size_t
)sizeof(frame
));
801 if (kr
== KERN_SUCCESS
) {
802 #if defined(HAS_APPLE_PAC)
803 /* return addresses on stack will be signed by arm64e ABI */
804 pc
= (uint64_t)ptrauth_strip((void *)frame
[1], ptrauth_key_return_address
);
808 nextFramePointer
= (uint64_t *)frame
[0];
811 nextFramePointer
= 0ULL;
816 kr
= chudxnu_task_read(task
,
819 (vm_size_t
)sizeof(frame
));
820 if (kr
== KERN_SUCCESS
) {
821 #if defined(HAS_APPLE_PAC)
822 /* return addresses on stack will be signed by arm64e ABI */
823 pc
= (uint64_t)ptrauth_strip((void *)frame
[1], ptrauth_key_return_address
);
827 nextFramePointer
= (uint64_t *)(frame
[0]);
830 nextFramePointer
= 0ULL;
835 if (kr
!= KERN_SUCCESS
) {
840 if (nextFramePointer
) {
841 buffer
[bufferIndex
++] = chudxnu_vm_unslide(pc
, kernel
);
845 if (nextFramePointer
< fp
) {
848 fp
= nextFramePointer
;
852 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_END
, bufferIndex
);
854 if (bufferIndex
>= bufferMaxIndex
) {
855 bufferIndex
= bufferMaxIndex
;
856 kr
= KERN_RESOURCE_SHORTAGE
;
861 // Save link register and SP at bottom of stack (used for later fixup).
862 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currLR
, kernel
);
863 if (flags
& CS_FLAG_EXTRASP
) {
864 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currSP
, kernel
);
867 struct arm_saved_state32
*state
= NULL
;
868 uint32_t *fp
= NULL
, *nextFramePointer
= NULL
, *topfp
= NULL
;
870 /* 64-bit kernel stacks, 32-bit user stacks */
874 state
= saved_state32(sstate
);
876 /* make sure it is safe to dereference before you do it */
877 kernel
= ARM_SUPERVISOR_MODE(state
->cpsr
);
879 /* can't take a kernel callstack if we've got a user frame */
880 if (!user_only
&& !kernel
) {
885 * Reserve space for saving LR (and sometimes SP) at the end of the
888 if (flags
& CS_FLAG_EXTRASP
) {
894 if (bufferMaxIndex
< 2) {
896 return KERN_RESOURCE_SHORTAGE
;
899 currPC
= (uint64_t)state
->pc
; /* r15 */
900 if (state
->cpsr
& PSR_TF
) {
901 currPC
|= 1ULL; /* encode thumb mode into low bit of PC */
903 currLR
= (uint64_t)state
->lr
; /* r14 */
904 currSP
= (uint64_t)state
->sp
; /* r13 */
906 fp
= (uint32_t *)(uintptr_t)state
->r
[7]; /* frame pointer */
909 bufferIndex
= 0; // start with a stack of size zero
910 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currPC
, kernel
); // save PC in position 0.
912 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_START
, kernel
, 1);
914 // Now, fill buffer with stack backtraces.
915 while (bufferIndex
< bufferMaxIndex
) {
918 * Below the frame pointer, the following values are saved:
923 * Note that we read the pc even for the first stack frame
924 * (which, in theory, is always empty because the callee fills
925 * it in just before it lowers the stack. However, if we
926 * catch the program in between filling in the return address
927 * and lowering the stack, we want to still have a valid
928 * backtrace. FixupStack correctly disregards this value if
932 if ((uint32_t)fp
== 0 || ((uint32_t)fp
& 0x3) != 0) {
933 /* frame pointer is invalid - stop backtracing */
939 if (((uint32_t)fp
> kernStackMax
) ||
940 ((uint32_t)fp
< kernStackMin
)) {
943 kr
= chudxnu_kern_read(&frame
,
945 (vm_size_t
)sizeof(frame
));
946 if (kr
== KERN_SUCCESS
) {
947 pc
= (uint64_t)frame
[1];
948 nextFramePointer
= (uint32_t *) (frame
[0]);
951 nextFramePointer
= 0ULL;
956 kr
= chudxnu_task_read(task
,
958 (((uint64_t)(uint32_t)fp
) & 0x00000000FFFFFFFFULL
),
960 if (kr
== KERN_SUCCESS
) {
961 pc
= (uint64_t)frame32
[1];
962 nextFramePointer
= (uint32_t *)(uintptr_t)(frame32
[0]);
965 nextFramePointer
= 0ULL;
970 if (kr
!= KERN_SUCCESS
) {
975 if (nextFramePointer
) {
976 buffer
[bufferIndex
++] = chudxnu_vm_unslide(pc
, kernel
);
980 if (nextFramePointer
< fp
) {
983 fp
= nextFramePointer
;
987 BUF_VERB(PERF_CS_BACKTRACE
| DBG_FUNC_END
, bufferIndex
);
989 /* clamp callstack size to max */
990 if (bufferIndex
>= bufferMaxIndex
) {
991 bufferIndex
= bufferMaxIndex
;
992 kr
= KERN_RESOURCE_SHORTAGE
;
994 /* ignore all other failures */
998 // Save link register and R13 (sp) at bottom of stack (used for later fixup).
999 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currLR
, kernel
);
1000 if (flags
& CS_FLAG_EXTRASP
) {
1001 buffer
[bufferIndex
++] = chudxnu_vm_unslide(currSP
, kernel
);
1005 *count
= bufferIndex
;
1010 chudxnu_thread_get_callstack64_kperf(
1012 uint64_t *callStack
,
1013 mach_msg_type_number_t
*count
,
1014 boolean_t user_only
)
1016 return chudxnu_thread_get_callstack64_internal( thread
, callStack
, count
, user_only
, 0 );
1020 #define VALID_STACK_ADDRESS(supervisor, addr, minKernAddr, maxKernAddr) (supervisor ? (addr>=minKernAddr && addr<=maxKernAddr) : TRUE)
1021 // don't try to read in the hole
1022 #define VALID_STACK_ADDRESS64(supervisor, addr, minKernAddr, maxKernAddr) \
1023 (supervisor ? ((uint64_t)addr >= minKernAddr && (uint64_t)addr <= maxKernAddr) : \
1024 ((uint64_t)addr != 0ULL && ((uint64_t)addr <= 0x00007FFFFFFFFFFFULL || (uint64_t)addr >= 0xFFFF800000000000ULL)))
1026 typedef struct _cframe64_t
{
1027 uint64_t prevFP
; // can't use a real pointer here until we're a 64 bit kernel
1033 typedef struct _cframe_t
{
1034 uint32_t prev
; // this is really a user32-space pointer to the previous frame
1039 extern void * find_user_regs(thread_t
);
1040 extern x86_saved_state32_t
*find_kern_regs(thread_t
);
1042 static kern_return_t
1043 do_kernel_backtrace(
1045 struct x86_kernel_state
*regs
,
1047 mach_msg_type_number_t
*start_idx
,
1048 mach_msg_type_number_t max_idx
)
1050 uint64_t kernStackMin
= (uint64_t)thread
->kernel_stack
;
1051 uint64_t kernStackMax
= (uint64_t)kernStackMin
+ kernel_stack_size
;
1052 mach_msg_type_number_t ct
= *start_idx
;
1053 kern_return_t kr
= KERN_FAILURE
;
1056 uint64_t currPC
= 0ULL;
1057 uint64_t currFP
= 0ULL;
1058 uint64_t prevPC
= 0ULL;
1059 uint64_t prevFP
= 0ULL;
1060 if (KERN_SUCCESS
!= chudxnu_kern_read(&currPC
, (vm_offset_t
)&(regs
->k_rip
), sizeof(uint64_t))) {
1061 return KERN_FAILURE
;
1063 if (KERN_SUCCESS
!= chudxnu_kern_read(&currFP
, (vm_offset_t
)&(regs
->k_rbp
), sizeof(uint64_t))) {
1064 return KERN_FAILURE
;
1067 uint32_t currPC
= 0U;
1068 uint32_t currFP
= 0U;
1069 uint32_t prevPC
= 0U;
1070 uint32_t prevFP
= 0U;
1071 if (KERN_SUCCESS
!= chudxnu_kern_read(&currPC
, (vm_offset_t
)&(regs
->k_eip
), sizeof(uint32_t))) {
1072 return KERN_FAILURE
;
1074 if (KERN_SUCCESS
!= chudxnu_kern_read(&currFP
, (vm_offset_t
)&(regs
->k_ebp
), sizeof(uint32_t))) {
1075 return KERN_FAILURE
;
1079 if (*start_idx
>= max_idx
) {
1080 return KERN_RESOURCE_SHORTAGE
; // no frames traced
1083 return KERN_FAILURE
;
1086 frames
[ct
++] = chudxnu_vm_unslide((uint64_t)currPC
, 1);
1088 // build a backtrace of this kernel state
1090 while (VALID_STACK_ADDRESS64(TRUE
, currFP
, kernStackMin
, kernStackMax
)) {
1091 // this is the address where caller lives in the user thread
1092 uint64_t caller
= currFP
+ sizeof(uint64_t);
1094 while (VALID_STACK_ADDRESS(TRUE
, currFP
, kernStackMin
, kernStackMax
)) {
1095 uint32_t caller
= (uint32_t)currFP
+ sizeof(uint32_t);
1098 if (!currFP
|| !currPC
) {
1103 if (ct
>= max_idx
) {
1105 return KERN_RESOURCE_SHORTAGE
;
1108 /* read our caller */
1109 kr
= chudxnu_kern_read(&currPC
, (vm_offset_t
)caller
, sizeof(currPC
));
1111 if (kr
!= KERN_SUCCESS
|| !currPC
) {
1117 * retrive contents of the frame pointer and advance to the next stack
1118 * frame if it's valid
1121 kr
= chudxnu_kern_read(&prevFP
, (vm_offset_t
)currFP
, sizeof(currPC
));
1124 if (VALID_STACK_ADDRESS64(TRUE
, prevFP
, kernStackMin
, kernStackMax
)) {
1126 if (VALID_STACK_ADDRESS(TRUE
, prevFP
, kernStackMin
, kernStackMax
)) {
1128 frames
[ct
++] = chudxnu_vm_unslide((uint64_t)currPC
, 1);
1131 if (prevFP
<= currFP
) {
1139 return KERN_SUCCESS
;
1144 static kern_return_t
1148 x86_saved_state32_t
*regs
,
1150 mach_msg_type_number_t
*start_idx
,
1151 mach_msg_type_number_t max_idx
,
1152 boolean_t supervisor
)
1154 uint32_t tmpWord
= 0UL;
1155 uint64_t currPC
= (uint64_t) regs
->eip
;
1156 uint64_t currFP
= (uint64_t) regs
->ebp
;
1157 uint64_t prevPC
= 0ULL;
1158 uint64_t prevFP
= 0ULL;
1159 uint64_t kernStackMin
= thread
->kernel_stack
;
1160 uint64_t kernStackMax
= kernStackMin
+ kernel_stack_size
;
1161 mach_msg_type_number_t ct
= *start_idx
;
1162 kern_return_t kr
= KERN_FAILURE
;
1164 if (ct
>= max_idx
) {
1165 return KERN_RESOURCE_SHORTAGE
; // no frames traced
1167 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1169 // build a backtrace of this 32 bit state.
1170 while (VALID_STACK_ADDRESS(supervisor
, currFP
, kernStackMin
, kernStackMax
)) {
1171 cframe_t
*fp
= (cframe_t
*) (uintptr_t) currFP
;
1178 if (ct
>= max_idx
) {
1180 return KERN_RESOURCE_SHORTAGE
;
1183 /* read our caller */
1185 kr
= chudxnu_kern_read(&tmpWord
, (vm_offset_t
) &fp
->caller
, sizeof(uint32_t));
1187 kr
= chudxnu_task_read(task
, &tmpWord
, (vm_offset_t
) &fp
->caller
, sizeof(uint32_t));
1190 if (kr
!= KERN_SUCCESS
) {
1195 currPC
= (uint64_t) tmpWord
; // promote 32 bit address
1198 * retrive contents of the frame pointer and advance to the next stack
1199 * frame if it's valid
1203 kr
= chudxnu_kern_read(&tmpWord
, (vm_offset_t
)&fp
->prev
, sizeof(uint32_t));
1205 kr
= chudxnu_task_read(task
, &tmpWord
, (vm_offset_t
)&fp
->prev
, sizeof(uint32_t));
1207 prevFP
= (uint64_t) tmpWord
; // promote 32 bit address
1210 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1213 if (prevFP
< currFP
) {
1221 return KERN_SUCCESS
;
1224 static kern_return_t
1228 x86_saved_state64_t
*regs
,
1230 mach_msg_type_number_t
*start_idx
,
1231 mach_msg_type_number_t max_idx
,
1232 boolean_t supervisor
)
1234 uint64_t currPC
= regs
->isf
.rip
;
1235 uint64_t currFP
= regs
->rbp
;
1236 uint64_t prevPC
= 0ULL;
1237 uint64_t prevFP
= 0ULL;
1238 uint64_t kernStackMin
= (uint64_t)thread
->kernel_stack
;
1239 uint64_t kernStackMax
= (uint64_t)kernStackMin
+ kernel_stack_size
;
1240 mach_msg_type_number_t ct
= *start_idx
;
1241 kern_return_t kr
= KERN_FAILURE
;
1243 if (*start_idx
>= max_idx
) {
1244 return KERN_RESOURCE_SHORTAGE
; // no frames traced
1246 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1248 // build a backtrace of this 32 bit state.
1249 while (VALID_STACK_ADDRESS64(supervisor
, currFP
, kernStackMin
, kernStackMax
)) {
1250 // this is the address where caller lives in the user thread
1251 uint64_t caller
= currFP
+ sizeof(uint64_t);
1258 if (ct
>= max_idx
) {
1260 return KERN_RESOURCE_SHORTAGE
;
1263 /* read our caller */
1265 kr
= chudxnu_kern_read(&currPC
, (vm_offset_t
)caller
, sizeof(uint64_t));
1267 kr
= chudxnu_task_read(task
, &currPC
, caller
, sizeof(uint64_t));
1270 if (kr
!= KERN_SUCCESS
) {
1276 * retrive contents of the frame pointer and advance to the next stack
1277 * frame if it's valid
1281 kr
= chudxnu_kern_read(&prevFP
, (vm_offset_t
)currFP
, sizeof(uint64_t));
1283 kr
= chudxnu_task_read(task
, &prevFP
, currFP
, sizeof(uint64_t));
1286 if (VALID_STACK_ADDRESS64(supervisor
, prevFP
, kernStackMin
, kernStackMax
)) {
1287 frames
[ct
++] = chudxnu_vm_unslide(currPC
, supervisor
);
1290 if (prevFP
< currFP
) {
1298 return KERN_SUCCESS
;
1301 static kern_return_t
1302 chudxnu_thread_get_callstack64_internal(
1304 uint64_t *callstack
,
1305 mach_msg_type_number_t
*count
,
1306 boolean_t user_only
,
1307 boolean_t kern_only
)
1309 kern_return_t kr
= KERN_FAILURE
;
1310 task_t task
= thread
->task
;
1311 uint64_t currPC
= 0ULL;
1312 boolean_t supervisor
= FALSE
;
1313 mach_msg_type_number_t bufferIndex
= 0;
1314 mach_msg_type_number_t bufferMaxIndex
= *count
;
1315 x86_saved_state_t
*tagged_regs
= NULL
; // kernel register state
1316 x86_saved_state64_t
*regs64
= NULL
;
1317 x86_saved_state32_t
*regs32
= NULL
;
1318 x86_saved_state32_t
*u_regs32
= NULL
;
1319 x86_saved_state64_t
*u_regs64
= NULL
;
1320 struct x86_kernel_state
*kregs
= NULL
;
1322 if (ml_at_interrupt_context()) {
1324 /* can't backtrace user state on interrupt stack. */
1325 return KERN_FAILURE
;
1328 /* backtracing at interrupt context? */
1329 if (thread
== current_thread() && current_cpu_datap()->cpu_int_state
) {
1331 * Locate the registers for the interrupted thread, assuming it is
1334 tagged_regs
= current_cpu_datap()->cpu_int_state
;
1336 if (is_saved_state64(tagged_regs
)) {
1337 /* 64 bit registers */
1338 regs64
= saved_state64(tagged_regs
);
1339 supervisor
= ((regs64
->isf
.cs
& SEL_PL
) != SEL_PL_U
);
1341 /* 32 bit registers */
1342 regs32
= saved_state32(tagged_regs
);
1343 supervisor
= ((regs32
->cs
& SEL_PL
) != SEL_PL_U
);
1348 if (!ml_at_interrupt_context() && kernel_task
== task
) {
1349 if (!thread
->kernel_stack
) {
1350 return KERN_FAILURE
;
1353 // Kernel thread not at interrupt context
1354 kregs
= (struct x86_kernel_state
*)NULL
;
1356 // nofault read of the thread->kernel_stack pointer
1357 if (KERN_SUCCESS
!= chudxnu_kern_read(&kregs
, (vm_offset_t
)&(thread
->kernel_stack
), sizeof(void *))) {
1358 return KERN_FAILURE
;
1361 // Adjust to find the saved kernel state
1362 kregs
= STACK_IKS((vm_offset_t
)(uintptr_t)kregs
);
1365 } else if (!tagged_regs
) {
1367 * not at interrupt context, or tracing a different thread than
1368 * current_thread() at interrupt context
1370 tagged_regs
= USER_STATE(thread
);
1371 if (is_saved_state64(tagged_regs
)) {
1372 /* 64 bit registers */
1373 regs64
= saved_state64(tagged_regs
);
1374 supervisor
= ((regs64
->isf
.cs
& SEL_PL
) != SEL_PL_U
);
1376 /* 32 bit registers */
1377 regs32
= saved_state32(tagged_regs
);
1378 supervisor
= ((regs32
->cs
& SEL_PL
) != SEL_PL_U
);
1385 // the caller only wants a user callstack.
1387 // bail - we've only got kernel state
1388 return KERN_FAILURE
;
1391 // regs32(64) is not in supervisor mode.
1399 /* we only want to backtrace the user mode */
1400 if (!(u_regs32
|| u_regs64
)) {
1401 /* no user state to look at */
1402 return KERN_FAILURE
;
1407 * Order of preference for top of stack:
1408 * 64 bit kernel state (not likely)
1409 * 32 bit kernel state
1410 * 64 bit user land state
1411 * 32 bit user land state
1416 * nofault read of the registers from the kernel stack (as they can
1417 * disappear on the fly).
1420 if (KERN_SUCCESS
!= chudxnu_kern_read(&currPC
, (vm_offset_t
)&(kregs
->k_rip
), sizeof(uint64_t))) {
1421 return KERN_FAILURE
;
1423 } else if (regs64
) {
1424 currPC
= regs64
->isf
.rip
;
1425 } else if (regs32
) {
1426 currPC
= (uint64_t) regs32
->eip
;
1427 } else if (u_regs64
) {
1428 currPC
= u_regs64
->isf
.rip
;
1429 } else if (u_regs32
) {
1430 currPC
= (uint64_t) u_regs32
->eip
;
1434 /* no top of the stack, bail out */
1435 return KERN_FAILURE
;
1440 if (bufferMaxIndex
< 1) {
1442 return KERN_RESOURCE_SHORTAGE
;
1445 /* backtrace kernel */
1447 addr64_t address
= 0ULL;
1451 kr
= do_kernel_backtrace(thread
, kregs
, callstack
, &bufferIndex
, bufferMaxIndex
);
1453 // and do a nofault read of (r|e)sp
1454 uint64_t rsp
= 0ULL;
1455 size
= sizeof(uint64_t);
1457 if (KERN_SUCCESS
!= chudxnu_kern_read(&address
, (vm_offset_t
)&(kregs
->k_rsp
), size
)) {
1461 if (address
&& KERN_SUCCESS
== chudxnu_kern_read(&rsp
, (vm_offset_t
)address
, size
) && bufferIndex
< bufferMaxIndex
) {
1462 callstack
[bufferIndex
++] = (uint64_t)rsp
;
1464 } else if (regs64
) {
1465 uint64_t rsp
= 0ULL;
1467 // backtrace the 64bit side.
1468 kr
= do_backtrace64(task
, thread
, regs64
, callstack
, &bufferIndex
,
1469 bufferMaxIndex
- 1, TRUE
);
1471 if (KERN_SUCCESS
== chudxnu_kern_read(&rsp
, (vm_offset_t
) regs64
->isf
.rsp
, sizeof(uint64_t)) &&
1472 bufferIndex
< bufferMaxIndex
) {
1473 callstack
[bufferIndex
++] = rsp
;
1475 } else if (regs32
) {
1478 // backtrace the 32bit side.
1479 kr
= do_backtrace32(task
, thread
, regs32
, callstack
, &bufferIndex
,
1480 bufferMaxIndex
- 1, TRUE
);
1482 if (KERN_SUCCESS
== chudxnu_kern_read(&esp
, (vm_offset_t
) regs32
->uesp
, sizeof(uint32_t)) &&
1483 bufferIndex
< bufferMaxIndex
) {
1484 callstack
[bufferIndex
++] = (uint64_t) esp
;
1486 } else if (u_regs64
&& !kern_only
) {
1487 /* backtrace user land */
1488 uint64_t rsp
= 0ULL;
1490 kr
= do_backtrace64(task
, thread
, u_regs64
, callstack
, &bufferIndex
,
1491 bufferMaxIndex
- 1, FALSE
);
1493 if (KERN_SUCCESS
== chudxnu_task_read(task
, &rsp
, (addr64_t
) u_regs64
->isf
.rsp
, sizeof(uint64_t)) &&
1494 bufferIndex
< bufferMaxIndex
) {
1495 callstack
[bufferIndex
++] = rsp
;
1497 } else if (u_regs32
&& !kern_only
) {
1500 kr
= do_backtrace32(task
, thread
, u_regs32
, callstack
, &bufferIndex
,
1501 bufferMaxIndex
- 1, FALSE
);
1503 if (KERN_SUCCESS
== chudxnu_task_read(task
, &esp
, (addr64_t
) u_regs32
->uesp
, sizeof(uint32_t)) &&
1504 bufferIndex
< bufferMaxIndex
) {
1505 callstack
[bufferIndex
++] = (uint64_t) esp
;
1509 *count
= bufferIndex
;
1515 chudxnu_thread_get_callstack64_kperf(
1517 uint64_t *callstack
,
1518 mach_msg_type_number_t
*count
,
1521 return chudxnu_thread_get_callstack64_internal(thread
, callstack
, count
, is_user
, !is_user
);
1523 #else /* !__arm__ && !__arm64__ && !__x86_64__ */
1524 #error kperf: unsupported architecture
1525 #endif /* !__arm__ && !__arm64__ && !__x86_64__ */