]> git.saurik.com Git - apple/libc.git/blame - gen/thread_stack_pcs.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / gen / thread_stack_pcs.c
CommitLineData
224c7076 1/*
70ad1dc8 2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
224c7076
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
70ad1dc8 5 *
224c7076
A
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.
70ad1dc8 12 *
224c7076
A
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.
70ad1dc8 20 *
224c7076
A
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>
507116e3 30#include <pthread/stack_np.h>
6465356a 31#include "stack_logging.h"
224c7076 32
224c7076 33#define INSTACK(a) ((a) >= stackbot && (a) <= stacktop)
70ad1dc8 34#if defined(__x86_64__)
224c7076
A
35#define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 0)
36#elif defined(__i386__)
37#define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 8)
70ad1dc8
A
38#elif defined(__arm__) || defined(__arm64__)
39#define ISALIGNED(a) ((((uintptr_t)(a)) & 0x1) == 0)
224c7076
A
40#endif
41
507116e3
A
42__attribute__((noinline))
43static void
44__thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb,
70ad1dc8 45 unsigned skip, void *startfp)
224c7076 46{
70ad1dc8
A
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);
224c7076 51
70ad1dc8 52 *nb = 0;
224c7076 53
507116e3
A
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
70ad1dc8 59 /* make sure return address is never out of bounds */
507116e3 60 stacktop -= (next - frame);
224c7076 61
70ad1dc8
A
62 if(!INSTACK(frame) || !ISALIGNED(frame))
63 return;
507116e3
A
64 while (startfp || skip--) {
65 if (startfp && startfp < next) break;
70ad1dc8
A
66 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
67 return;
68 frame = next;
507116e3 69 next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, NULL);
70ad1dc8
A
70 }
71 while (max--) {
507116e3
A
72 uintptr_t retaddr;
73 next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, &retaddr);
70ad1dc8 74 buffer[*nb] = retaddr;
70ad1dc8 75 (*nb)++;
70ad1dc8
A
76 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
77 return;
78 frame = next;
79 }
224c7076
A
80}
81
507116e3
A
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))
85void
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
70ad1dc8 93// Prevent thread_stack_pcs() from getting tail-call-optimized into
507116e3 94// __thread_stack_pcs() on 64-bit environments, thus making the "number of hot
70ad1dc8
A
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.
507116e3
A
99//
100// Note that callee relies on this function having a minimal stackframe
101// to introspect (i.e. no tailcall and no local variables)
70ad1dc8 102__attribute__((disable_tail_calls))
224c7076
A
103void
104thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb)
105{
507116e3 106 __thread_stack_pcs(buffer, max, nb, 0, NULL);
224c7076 107}