]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1999-2018 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 | #include <pthread/stack_np.h> | |
31 | #include "stack_logging.h" | |
32 | ||
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) | |
40 | #endif | |
41 | ||
42 | __attribute__((noinline)) | |
43 | static void | |
44 | __thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb, | |
45 | unsigned skip, void *startfp) | |
46 | { | |
47 | void *frame, *next; | |
48 | pthread_t self = pthread_self(); | |
49 | void *stacktop = pthread_get_stackaddr_np(self); | |
50 | void *stackbot = stacktop - pthread_get_stacksize_np(self); | |
51 | ||
52 | *nb = 0; | |
53 | ||
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); | |
58 | ||
59 | /* make sure return address is never out of bounds */ | |
60 | stacktop -= (next - frame); | |
61 | ||
62 | if(!INSTACK(frame) || !ISALIGNED(frame)) | |
63 | return; | |
64 | while (startfp || skip--) { | |
65 | if (startfp && startfp < next) break; | |
66 | if(!INSTACK(next) || !ISALIGNED(next) || next <= frame) | |
67 | return; | |
68 | frame = next; | |
69 | next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, NULL); | |
70 | } | |
71 | while (max--) { | |
72 | uintptr_t retaddr; | |
73 | next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, &retaddr); | |
74 | buffer[*nb] = retaddr; | |
75 | (*nb)++; | |
76 | if(!INSTACK(next) || !ISALIGNED(next) || next <= frame) | |
77 | return; | |
78 | frame = next; | |
79 | } | |
80 | } | |
81 | ||
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)) | |
85 | void | |
86 | _thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb, | |
87 | unsigned skip, void *startfp) | |
88 | { | |
89 | // skip this frame | |
90 | __thread_stack_pcs(buffer, max, nb, skip + 1, startfp); | |
91 | } | |
92 | ||
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. | |
96 | // | |
97 | // See <rdar://problem/5364825> "stack logging: frames keep getting truncated" | |
98 | // for why this is necessary. | |
99 | // | |
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)) | |
103 | void | |
104 | thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb) | |
105 | { | |
106 | __thread_stack_pcs(buffer, max, nb, 0, NULL); | |
107 | } |