]> git.saurik.com Git - apple/libc.git/blob - gen/thread_stack_pcs.c
Libc-498.1.1.tar.gz
[apple/libc.git] / gen / thread_stack_pcs.c
1 /*
2 * Copyright (c) 1999, 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /* Bertrand from vmutils -> CF -> System */
25
26 #include <pthread.h>
27 #include <mach/mach.h>
28 #include <mach/vm_statistics.h>
29 #include <stdlib.h>
30
31 #if defined(__i386__) || defined(__x86_64__)
32 #define FP_LINK_OFFSET 1
33 #elif defined(__ppc__) || defined(__ppc64__)
34 #define FP_LINK_OFFSET 2
35 #else
36 #error ********** Unimplemented architecture
37 #endif
38
39 #define INSTACK(a) ((a) >= stackbot && (a) <= stacktop)
40 #if defined(__ppc__) || defined(__ppc64__) || defined(__x86_64__)
41 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 0)
42 #elif defined(__i386__)
43 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 8)
44 #endif
45
46 __private_extern__ __attribute__((noinline))
47 void
48 _thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb, unsigned skip)
49 {
50 void *frame, *next;
51 pthread_t self = pthread_self();
52 void *stacktop = pthread_get_stackaddr_np(self);
53 void *stackbot = stacktop - pthread_get_stacksize_np(self);
54
55 *nb = 0;
56
57 /* make sure return address is never out of bounds */
58 stacktop -= (FP_LINK_OFFSET + 1) * sizeof(void *);
59
60 /*
61 * The original implementation called the first_frame_address() function,
62 * which returned the stack frame pointer. The problem was that in ppc,
63 * it was a leaf function, so no new stack frame was set up with
64 * optimization turned on (while a new stack frame was set up without
65 * optimization). We now inline the code to get the stack frame pointer,
66 * so we are consistent about the stack frame.
67 */
68 #if defined(__i386__) || defined(__x86_64__)
69 frame = __builtin_frame_address(0);
70 #elif defined(__ppc__) || defined(__ppc64__)
71 /* __builtin_frame_address IS BROKEN IN BEAKER: RADAR #2340421 */
72 __asm__ volatile("mr %0, r1" : "=r" (frame));
73 #endif
74 if(!INSTACK(frame) || !ISALIGNED(frame))
75 return;
76 #if defined(__ppc__) || defined(__ppc64__)
77 /* back up the stack pointer up over the current stack frame */
78 next = *(void **)frame;
79 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
80 return;
81 frame = next;
82 #endif
83 while (skip--) {
84 next = *(void **)frame;
85 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
86 return;
87 frame = next;
88 }
89 while (max--) {
90 buffer[*nb] = *(vm_address_t *)(((void **)frame) + FP_LINK_OFFSET);
91 (*nb)++;
92 next = *(void **)frame;
93 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
94 return;
95 frame = next;
96 }
97 }
98
99 void
100 thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb)
101 {
102 _thread_stack_pcs(buffer, max, nb, 0);
103
104 // The following prevents thread_stack_pcs() from getting tail-call-optimized into _thread_stack_pcs() on 64-bit environments,
105 // thus making the "number of hot frames to skip" be more predictable, giving more consistent backtraces.
106 // See <rdar://problem/5364825> "stack logging: frames keep getting truncated" for why this is necessary.
107 __asm__ volatile("");
108 }