sanitizer_stacktrace.cc revision 678e5436c6c1ecbd0cf50ce80bc7a2afb904c0ef
1//===-- sanitizer_stacktrace.cc -------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is shared between AddressSanitizer and ThreadSanitizer 11// run-time libraries. 12//===----------------------------------------------------------------------===// 13 14#include "sanitizer_common.h" 15#include "sanitizer_procmaps.h" 16#include "sanitizer_stacktrace.h" 17#include "sanitizer_symbolizer.h" 18 19namespace __sanitizer { 20static const char *StripPathPrefix(const char *filepath, 21 const char *strip_file_prefix) { 22 if (filepath == internal_strstr(filepath, strip_file_prefix)) 23 return filepath + internal_strlen(strip_file_prefix); 24 return filepath; 25} 26 27// ----------------------- StackTrace ----------------------------- {{{1 28// PCs in stack traces are actually the return addresses, that is, 29// addresses of the next instructions after the call. That's why we 30// decrement them. 31static uptr patch_pc(uptr pc) { 32#ifdef __arm__ 33 // Cancel Thumb bit. 34 pc = pc & (~1); 35#endif 36 return pc - 1; 37} 38 39void StackTrace::PrintStack(const uptr *addr, uptr size, 40 bool symbolize, const char *strip_file_prefix, 41 SymbolizeCallback symbolize_callback ) { 42 MemoryMappingLayout proc_maps; 43 InternalScopedBuffer<char> buff(kPageSize * 2); 44 InternalScopedBuffer<AddressInfo> addr_frames(64); 45 uptr frame_num = 0; 46 for (uptr i = 0; i < size && addr[i]; i++) { 47 uptr pc = patch_pc(addr[i]); 48 if (symbolize_callback) { 49 symbolize_callback((void*)pc, buff.data(), buff.size()); 50 // We can't know anything about the string returned by external 51 // symbolizer, but if it starts with filename, try to strip path prefix 52 // from it. 53 Printf(" #%zu 0x%zx %s\n", frame_num, pc, 54 StripPathPrefix(buff.data(), strip_file_prefix)); 55 frame_num++; 56 continue; 57 } 58 uptr addr_frames_num = 59 symbolize ? SymbolizeCode(pc, addr_frames.data(), addr_frames.size()) : 0; 60 if (addr_frames_num > 0) { 61 for (uptr j = 0; j < addr_frames_num; j++) { 62 AddressInfo &info = addr_frames[j]; 63 Printf(" #%zu 0x%zx", frame_num, pc); 64 if (info.function) { 65 Printf(" in %s", info.function); 66 } 67 if (info.file) { 68 Printf(" %s:%d:%d", StripPathPrefix(info.file, strip_file_prefix), 69 info.line, info.column); 70 } else if (info.module) { 71 Printf(" (%s+0x%zx)", StripPathPrefix(info.module, strip_file_prefix), 72 info.module_offset); 73 } 74 Printf("\n"); 75 info.Clear(); 76 frame_num++; 77 } 78 } else { 79 uptr offset; 80 if (proc_maps.GetObjectNameAndOffset(pc, &offset, 81 buff.data(), buff.size())) { 82 Printf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, 83 StripPathPrefix(buff.data(), strip_file_prefix), offset); 84 } else { 85 Printf(" #%zu 0x%zx\n", frame_num, pc); 86 } 87 frame_num++; 88 } 89 } 90} 91 92uptr StackTrace::GetCurrentPc() { 93 return GET_CALLER_PC(); 94} 95 96void StackTrace::FastUnwindStack(uptr pc, uptr bp, 97 uptr stack_top, uptr stack_bottom) { 98 CHECK(size == 0 && trace[0] == pc); 99 size = 1; 100 uptr *frame = (uptr*)bp; 101 uptr *prev_frame = frame; 102 while (frame >= prev_frame && 103 frame < (uptr*)stack_top - 2 && 104 frame > (uptr*)stack_bottom && 105 size < max_size) { 106 uptr pc1 = frame[1]; 107 if (pc1 != pc) { 108 trace[size++] = pc1; 109 } 110 prev_frame = frame; 111 frame = (uptr*)frame[0]; 112 } 113} 114 115// On 32-bits we don't compress stack traces. 116// On 64-bits we compress stack traces: if a given pc differes slightly from 117// the previous one, we record a 31-bit offset instead of the full pc. 118SANITIZER_INTERFACE_ATTRIBUTE 119uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) { 120#if __WORDSIZE == 32 121 // Don't compress, just copy. 122 uptr res = 0; 123 for (uptr i = 0; i < stack->size && i < size; i++) { 124 compressed[i] = stack->trace[i]; 125 res++; 126 } 127 if (stack->size < size) 128 compressed[stack->size] = 0; 129#else // 64 bits, compress. 130 uptr prev_pc = 0; 131 const uptr kMaxOffset = (1ULL << 30) - 1; 132 uptr c_index = 0; 133 uptr res = 0; 134 for (uptr i = 0, n = stack->size; i < n; i++) { 135 uptr pc = stack->trace[i]; 136 if (!pc) break; 137 if ((s64)pc < 0) break; 138 // Printf("C pc[%zu] %zx\n", i, pc); 139 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) { 140 uptr offset = (s64)(pc - prev_pc); 141 offset |= (1U << 31); 142 if (c_index >= size) break; 143 // Printf("C co[%zu] offset %zx\n", i, offset); 144 compressed[c_index++] = offset; 145 } else { 146 uptr hi = pc >> 32; 147 uptr lo = (pc << 32) >> 32; 148 CHECK_EQ((hi & (1 << 31)), 0); 149 if (c_index + 1 >= size) break; 150 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo); 151 compressed[c_index++] = hi; 152 compressed[c_index++] = lo; 153 } 154 res++; 155 prev_pc = pc; 156 } 157 if (c_index < size) 158 compressed[c_index] = 0; 159 if (c_index + 1 < size) 160 compressed[c_index + 1] = 0; 161#endif // __WORDSIZE 162 163 // debug-only code 164#if 0 165 StackTrace check_stack; 166 UncompressStack(&check_stack, compressed, size); 167 if (res < check_stack.size) { 168 Printf("res %zu check_stack.size %zu; c_size %zu\n", res, 169 check_stack.size, size); 170 } 171 // |res| may be greater than check_stack.size, because 172 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames. 173 CHECK(res >= check_stack.size); 174 CHECK_EQ(0, REAL(memcmp)(check_stack.trace, stack->trace, 175 check_stack.size * sizeof(uptr))); 176#endif 177 178 return res; 179} 180 181SANITIZER_INTERFACE_ATTRIBUTE 182void StackTrace::UncompressStack(StackTrace *stack, 183 u32 *compressed, uptr size) { 184#if __WORDSIZE == 32 185 // Don't uncompress, just copy. 186 stack->size = 0; 187 for (uptr i = 0; i < size && i < kStackTraceMax; i++) { 188 if (!compressed[i]) break; 189 stack->size++; 190 stack->trace[i] = compressed[i]; 191 } 192#else // 64 bits, uncompress 193 uptr prev_pc = 0; 194 stack->size = 0; 195 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) { 196 u32 x = compressed[i]; 197 uptr pc = 0; 198 if (x & (1U << 31)) { 199 // Printf("U co[%zu] offset: %x\n", i, x); 200 // this is an offset 201 s32 offset = x; 202 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend. 203 pc = prev_pc + offset; 204 CHECK(pc); 205 } else { 206 // CHECK(i + 1 < size); 207 if (i + 1 >= size) break; 208 uptr hi = x; 209 uptr lo = compressed[i+1]; 210 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo); 211 i++; 212 pc = (hi << 32) | lo; 213 if (!pc) break; 214 } 215 // Printf("U pc[%zu] %zx\n", stack->size, pc); 216 stack->trace[stack->size++] = pc; 217 prev_pc = pc; 218 } 219#endif // __WORDSIZE 220} 221 222} // namespace __sanitizer 223