]> git.saurik.com Git - apple/libc.git/blob - gen/thread_stack_pcs.c
18bbf21e3149fe76593950f8620c54aa43785ed6
[apple/libc.git] / gen / thread_stack_pcs.c
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 "stack_logging.h"
31
32
33 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
34 #define FP_LINK_OFFSET 1
35 #else
36 #error ********** Unimplemented architecture
37 #endif
38
39
40 #define INSTACK(a) ((a) >= stackbot && (a) <= stacktop)
41 #if defined(__x86_64__)
42 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 0)
43 #elif defined(__i386__)
44 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 8)
45 #elif defined(__arm__) || defined(__arm64__)
46 #define ISALIGNED(a) ((((uintptr_t)(a)) & 0x1) == 0)
47 #endif
48
49 __private_extern__ __attribute__((noinline))
50 void
51 _thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb,
52 unsigned skip, void *startfp)
53 {
54 void *frame, *next;
55 pthread_t self = pthread_self();
56 void *stacktop = pthread_get_stackaddr_np(self);
57 void *stackbot = stacktop - pthread_get_stacksize_np(self);
58
59 *nb = 0;
60
61 /* make sure return address is never out of bounds */
62 stacktop -= (FP_LINK_OFFSET + 1) * sizeof(void *);
63
64 frame = __builtin_frame_address(0);
65 if(!INSTACK(frame) || !ISALIGNED(frame))
66 return;
67 while ((startfp && startfp >= *(void **)frame) || skip--) {
68 next = *(void **)frame;
69 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
70 return;
71 frame = next;
72 }
73 while (max--) {
74 void *retaddr = (void *)*(vm_address_t *)
75 (((void **)frame) + FP_LINK_OFFSET);
76 #pragma clang diagnostic push
77 #pragma clang diagnostic ignored "-Wint-conversion"
78 buffer[*nb] = retaddr;
79 #pragma clang diagnostic pop
80 (*nb)++;
81 next = *(void **)frame;
82 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
83 return;
84 frame = next;
85 }
86 }
87
88 // Prevent thread_stack_pcs() from getting tail-call-optimized into
89 // _thread_stack_pcs() on 64-bit environments, thus making the "number of hot
90 // frames to skip" be more predictable, giving more consistent backtraces.
91 //
92 // See <rdar://problem/5364825> "stack logging: frames keep getting truncated"
93 // for why this is necessary.
94 __attribute__((disable_tail_calls))
95 void
96 thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb)
97 {
98 _thread_stack_pcs(buffer, max, nb, 0, NULL);
99 }