]> git.saurik.com Git - apple/libc.git/blobdiff - gen/thread_stack_pcs.c
Libc-1353.11.2.tar.gz
[apple/libc.git] / gen / thread_stack_pcs.c
index 18bbf21e3149fe76593950f8620c54aa43785ed6..5066a52de8b515547cd1839f46dbd1be89030f92 100644 (file)
 #include <mach/mach.h>
 #include <mach/vm_statistics.h>
 #include <stdlib.h>
+#include <pthread/stack_np.h>
 #include "stack_logging.h"
 
-
-#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
-#define FP_LINK_OFFSET 1
-#else
-#error ********** Unimplemented architecture
-#endif
-
-
 #define        INSTACK(a)      ((a) >= stackbot && (a) <= stacktop)
 #if defined(__x86_64__)
 #define        ISALIGNED(a)    ((((uintptr_t)(a)) & 0xf) == 0)
@@ -46,9 +39,9 @@
 #define        ISALIGNED(a)    ((((uintptr_t)(a)) & 0x1) == 0)
 #endif
 
-__private_extern__  __attribute__((noinline))
-void
-_thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb,
+__attribute__((noinline))
+static void
+__thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb,
                unsigned skip, void *startfp)
 {
        void *frame, *next;
@@ -58,42 +51,57 @@ _thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb,
 
        *nb = 0;
 
+       // Rely on the fact that our caller has an empty stackframe (no local vars)
+       // to determine the minimum size of a stackframe (frame ptr & return addr)
+       frame = __builtin_frame_address(0);
+       next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, NULL);
+
        /* make sure return address is never out of bounds */
-       stacktop -= (FP_LINK_OFFSET + 1) * sizeof(void *);
+       stacktop -= (next - frame);
 
-       frame = __builtin_frame_address(0);
        if(!INSTACK(frame) || !ISALIGNED(frame))
                return;
-       while ((startfp && startfp >= *(void **)frame) || skip--) {
-               next = *(void **)frame;
+       while (startfp || skip--) {
+               if (startfp && startfp < next) break;
                if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
                        return;
                frame = next;
+               next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, NULL);
        }
        while (max--) {
-               void *retaddr = (void *)*(vm_address_t *)
-                               (((void **)frame) + FP_LINK_OFFSET);
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wint-conversion"
+               uintptr_t retaddr;
+               next = (void*)pthread_stack_frame_decode_np((uintptr_t)frame, &retaddr);
                buffer[*nb] = retaddr;
-#pragma clang diagnostic pop
                (*nb)++;
-               next = *(void **)frame;
                if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
                        return;
                frame = next;
        }
 }
 
+// Note that callee relies on this function having a minimal stackframe
+// to introspect (i.e. no tailcall and no local variables)
+__private_extern__ __attribute__((disable_tail_calls))
+void
+_thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb,
+               unsigned skip, void *startfp)
+{
+       // skip this frame
+       __thread_stack_pcs(buffer, max, nb, skip + 1, startfp);
+}
+
 // Prevent thread_stack_pcs() from getting tail-call-optimized into
-// _thread_stack_pcs() on 64-bit environments, thus making the "number of hot
+// __thread_stack_pcs() on 64-bit environments, thus making the "number of hot
 // frames to skip" be more predictable, giving more consistent backtraces.
 //
 // See <rdar://problem/5364825> "stack logging: frames keep getting truncated"
 // for why this is necessary.
+//
+// Note that callee relies on this function having a minimal stackframe
+// to introspect (i.e. no tailcall and no local variables)
 __attribute__((disable_tail_calls))
 void
 thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb)
 {
-       _thread_stack_pcs(buffer, max, nb, 0, NULL);
+       __thread_stack_pcs(buffer, max, nb, 0, NULL);
 }