]>
git.saurik.com Git - apple/libc.git/blob - gen/thread_stack_pcs.c
2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
24 /* Bertrand from vmutils -> CF -> System */
27 #include <mach/mach.h>
28 #include <mach/vm_statistics.h>
30 #include <pthread/stack_np.h>
31 #include "stack_logging.h"
33 #define INSTACK(a) ((a) >= stackbot && (a) <= stacktop)
34 #if defined(__x86_64__)
35 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 0)
36 #elif defined(__i386__)
37 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 8)
38 #elif defined(__arm__) || defined(__arm64__)
39 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0x1) == 0)
42 __attribute__((noinline
))
44 __thread_stack_pcs(vm_address_t
*buffer
, unsigned max
, unsigned *nb
,
45 unsigned skip
, void *startfp
)
48 pthread_t self
= pthread_self();
49 void *stacktop
= pthread_get_stackaddr_np(self
);
50 void *stackbot
= stacktop
- pthread_get_stacksize_np(self
);
54 // Rely on the fact that our caller has an empty stackframe (no local vars)
55 // to determine the minimum size of a stackframe (frame ptr & return addr)
56 frame
= __builtin_frame_address(0);
57 next
= (void*)pthread_stack_frame_decode_np((uintptr_t)frame
, NULL
);
59 /* make sure return address is never out of bounds */
60 stacktop
-= (next
- frame
);
62 if(!INSTACK(frame
) || !ISALIGNED(frame
))
64 while (startfp
|| skip
--) {
65 if (startfp
&& startfp
< next
) break;
66 if(!INSTACK(next
) || !ISALIGNED(next
) || next
<= frame
)
69 next
= (void*)pthread_stack_frame_decode_np((uintptr_t)frame
, NULL
);
73 next
= (void*)pthread_stack_frame_decode_np((uintptr_t)frame
, &retaddr
);
74 buffer
[*nb
] = retaddr
;
76 if(!INSTACK(next
) || !ISALIGNED(next
) || next
<= frame
)
82 // Note that callee relies on this function having a minimal stackframe
83 // to introspect (i.e. no tailcall and no local variables)
84 __private_extern__
__attribute__((disable_tail_calls
))
86 _thread_stack_pcs(vm_address_t
*buffer
, unsigned max
, unsigned *nb
,
87 unsigned skip
, void *startfp
)
90 __thread_stack_pcs(buffer
, max
, nb
, skip
+ 1, startfp
);
93 // Prevent thread_stack_pcs() from getting tail-call-optimized into
94 // __thread_stack_pcs() on 64-bit environments, thus making the "number of hot
95 // frames to skip" be more predictable, giving more consistent backtraces.
97 // See <rdar://problem/5364825> "stack logging: frames keep getting truncated"
98 // for why this is necessary.
100 // Note that callee relies on this function having a minimal stackframe
101 // to introspect (i.e. no tailcall and no local variables)
102 __attribute__((disable_tail_calls
))
104 thread_stack_pcs(vm_address_t
*buffer
, unsigned max
, unsigned *nb
)
106 __thread_stack_pcs(buffer
, max
, nb
, 0, NULL
);