1/* 2 * Copyright (c) 1999, 2007 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#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 33#define FP_LINK_OFFSET 1 34#elif defined(__ppc__) || defined(__ppc64__) 35#define FP_LINK_OFFSET 2 36#else 37#error ********** Unimplemented architecture 38#endif 39 40#define INSTACK(a) ((a) >= stackbot && (a) <= stacktop) 41#if defined(__ppc__) || defined(__ppc64__) || defined(__x86_64__) 42#define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 0) 43#elif defined(__arm__) 44#define ISALIGNED(a) ((((uintptr_t)(a)) & 0x1) == 0) 45#elif defined(__i386__) 46#define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 8) 47#endif 48 49__private_extern__ __attribute__((noinline)) 50void 51_thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb, unsigned skip) 52{ 53 void *frame, *next; 54 pthread_t self = pthread_self(); 55 void *stacktop = pthread_get_stackaddr_np(self); 56 void *stackbot = stacktop - pthread_get_stacksize_np(self); 57 58 *nb = 0; 59 60 /* make sure return address is never out of bounds */ 61 stacktop -= (FP_LINK_OFFSET + 1) * sizeof(void *); 62 63 /* 64 * The original implementation called the first_frame_address() function, 65 * which returned the stack frame pointer. The problem was that in ppc, 66 * it was a leaf function, so no new stack frame was set up with 67 * optimization turned on (while a new stack frame was set up without 68 * optimization). We now inline the code to get the stack frame pointer, 69 * so we are consistent about the stack frame. 70 */ 71#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 72 frame = __builtin_frame_address(0); 73#elif defined(__ppc__) || defined(__ppc64__) 74 /* __builtin_frame_address IS BROKEN IN BEAKER: RADAR #2340421 */ 75 __asm__ volatile("mr %0, r1" : "=r" (frame)); 76#endif 77 if(!INSTACK(frame) || !ISALIGNED(frame)) 78 return; 79#if defined(__ppc__) || defined(__ppc64__) 80 /* back up the stack pointer up over the current stack frame */ 81 next = *(void **)frame; 82 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame) 83 return; 84 frame = next; 85#endif 86 while (skip--) { 87 next = *(void **)frame; 88 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame) 89 return; 90 frame = next; 91 } 92 while (max--) { 93 buffer[*nb] = *(vm_address_t *)(((void **)frame) + FP_LINK_OFFSET); 94 (*nb)++; 95 next = *(void **)frame; 96 if(!INSTACK(next) || !ISALIGNED(next) || next <= frame) 97 return; 98 frame = next; 99 } 100} 101 102void 103thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb) 104{ 105 _thread_stack_pcs(buffer, max, nb, 0); 106 107 // The following prevents thread_stack_pcs() from getting tail-call-optimized into _thread_stack_pcs() on 64-bit environments, 108 // thus making the "number of hot frames to skip" be more predictable, giving more consistent backtraces. 109 // See <rdar://problem/5364825> "stack logging: frames keep getting truncated" for why this is necessary. 110 __asm__ volatile(""); 111} 112