sanitizer_stacktrace.cc revision 7996a2e2a1c461743c9216f13429c04d75050230
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_flags.h" 16#include "sanitizer_procmaps.h" 17#include "sanitizer_stacktrace.h" 18#include "sanitizer_symbolizer.h" 19 20namespace __sanitizer { 21 22uptr StackTrace::GetPreviousInstructionPc(uptr pc) { 23#ifdef __arm__ 24 // Cancel Thumb bit. 25 pc = pc & (~1); 26#endif 27#if defined(__powerpc__) || defined(__powerpc64__) 28 // PCs are always 4 byte aligned. 29 return pc - 4; 30#elif defined(__sparc__) 31 return pc - 8; 32#else 33 return pc - 1; 34#endif 35} 36 37static void PrintStackFramePrefix(uptr frame_num, uptr pc) { 38 Printf(" #%zu 0x%zx", frame_num, pc); 39} 40 41void StackTrace::PrintStack(const uptr *addr, uptr size, 42 SymbolizeCallback symbolize_callback) { 43 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 44 InternalScopedBuffer<char> buff(GetPageSizeCached() * 2); 45 InternalScopedBuffer<AddressInfo> addr_frames(64); 46 uptr frame_num = 0; 47 for (uptr i = 0; i < size && addr[i]; i++) { 48 // PCs in stack traces are actually the return addresses, that is, 49 // addresses of the next instructions after the call. 50 uptr pc = GetPreviousInstructionPc(addr[i]); 51 uptr addr_frames_num = 0; // The number of stack frames for current 52 // instruction address. 53 if (symbolize_callback) { 54 if (symbolize_callback((void*)pc, buff.data(), buff.size())) { 55 addr_frames_num = 1; 56 PrintStackFramePrefix(frame_num, pc); 57 // We can't know anything about the string returned by external 58 // symbolizer, but if it starts with filename, try to strip path prefix 59 // from it. 60 Printf(" %s\n", 61 StripPathPrefix(buff.data(), common_flags()->strip_path_prefix)); 62 frame_num++; 63 } 64 } 65 if (common_flags()->symbolize && addr_frames_num == 0) { 66 // Use our own (online) symbolizer, if necessary. 67 if (Symbolizer *sym = Symbolizer::GetOrNull()) 68 addr_frames_num = 69 sym->SymbolizeCode(pc, addr_frames.data(), addr_frames.size()); 70 for (uptr j = 0; j < addr_frames_num; j++) { 71 AddressInfo &info = addr_frames[j]; 72 PrintStackFramePrefix(frame_num, pc); 73 if (info.function) { 74 Printf(" in %s", info.function); 75 } 76 if (info.file) { 77 Printf(" "); 78 PrintSourceLocation(info.file, info.line, info.column); 79 } else if (info.module) { 80 Printf(" "); 81 PrintModuleAndOffset(info.module, info.module_offset); 82 } 83 Printf("\n"); 84 info.Clear(); 85 frame_num++; 86 } 87 } 88 if (addr_frames_num == 0) { 89 // If online symbolization failed, try to output at least module and 90 // offset for instruction. 91 PrintStackFramePrefix(frame_num, pc); 92 uptr offset; 93 if (proc_maps.GetObjectNameAndOffset(pc, &offset, 94 buff.data(), buff.size(), 95 /* protection */0)) { 96 Printf(" "); 97 PrintModuleAndOffset(buff.data(), offset); 98 } 99 Printf("\n"); 100 frame_num++; 101 } 102 } 103} 104 105uptr StackTrace::GetCurrentPc() { 106 return GET_CALLER_PC(); 107} 108 109void StackTrace::FastUnwindStack(uptr pc, uptr bp, 110 uptr stack_top, uptr stack_bottom, 111 uptr max_depth) { 112 if (max_depth == 0) { 113 size = 0; 114 return; 115 } 116 trace[0] = pc; 117 size = 1; 118 uhwptr *frame = (uhwptr *)bp; 119 uhwptr *prev_frame = frame - 1; 120 if (stack_top < 4096) return; // Sanity check for stack top. 121 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame. 122 while (frame > prev_frame && 123 frame < (uhwptr *)stack_top - 2 && 124 frame > (uhwptr *)stack_bottom && 125 IsAligned((uptr)frame, sizeof(*frame)) && 126 size < max_depth) { 127 uhwptr pc1 = frame[1]; 128 if (pc1 != pc) { 129 trace[size++] = (uptr) pc1; 130 } 131 prev_frame = frame; 132 frame = (uhwptr *)frame[0]; 133 } 134} 135 136void StackTrace::PopStackFrames(uptr count) { 137 CHECK(size >= count); 138 size -= count; 139 for (uptr i = 0; i < size; i++) { 140 trace[i] = trace[i + count]; 141 } 142} 143 144// On 32-bits we don't compress stack traces. 145// On 64-bits we compress stack traces: if a given pc differes slightly from 146// the previous one, we record a 31-bit offset instead of the full pc. 147uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) { 148#if SANITIZER_WORDSIZE == 32 149 // Don't compress, just copy. 150 uptr res = 0; 151 for (uptr i = 0; i < stack->size && i < size; i++) { 152 compressed[i] = stack->trace[i]; 153 res++; 154 } 155 if (stack->size < size) 156 compressed[stack->size] = 0; 157#else // 64 bits, compress. 158 uptr prev_pc = 0; 159 const uptr kMaxOffset = (1ULL << 30) - 1; 160 uptr c_index = 0; 161 uptr res = 0; 162 for (uptr i = 0, n = stack->size; i < n; i++) { 163 uptr pc = stack->trace[i]; 164 if (!pc) break; 165 if ((s64)pc < 0) break; 166 // Printf("C pc[%zu] %zx\n", i, pc); 167 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) { 168 uptr offset = (s64)(pc - prev_pc); 169 offset |= (1U << 31); 170 if (c_index >= size) break; 171 // Printf("C co[%zu] offset %zx\n", i, offset); 172 compressed[c_index++] = offset; 173 } else { 174 uptr hi = pc >> 32; 175 uptr lo = (pc << 32) >> 32; 176 CHECK_EQ((hi & (1 << 31)), 0); 177 if (c_index + 1 >= size) break; 178 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo); 179 compressed[c_index++] = hi; 180 compressed[c_index++] = lo; 181 } 182 res++; 183 prev_pc = pc; 184 } 185 if (c_index < size) 186 compressed[c_index] = 0; 187 if (c_index + 1 < size) 188 compressed[c_index + 1] = 0; 189#endif // SANITIZER_WORDSIZE 190 191 // debug-only code 192#if 0 193 StackTrace check_stack; 194 UncompressStack(&check_stack, compressed, size); 195 if (res < check_stack.size) { 196 Printf("res %zu check_stack.size %zu; c_size %zu\n", res, 197 check_stack.size, size); 198 } 199 // |res| may be greater than check_stack.size, because 200 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames. 201 CHECK(res >= check_stack.size); 202 CHECK_EQ(0, REAL(memcmp)(check_stack.trace, stack->trace, 203 check_stack.size * sizeof(uptr))); 204#endif 205 206 return res; 207} 208 209void StackTrace::UncompressStack(StackTrace *stack, 210 u32 *compressed, uptr size) { 211#if SANITIZER_WORDSIZE == 32 212 // Don't uncompress, just copy. 213 stack->size = 0; 214 for (uptr i = 0; i < size && i < kStackTraceMax; i++) { 215 if (!compressed[i]) break; 216 stack->size++; 217 stack->trace[i] = compressed[i]; 218 } 219#else // 64 bits, uncompress 220 uptr prev_pc = 0; 221 stack->size = 0; 222 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) { 223 u32 x = compressed[i]; 224 uptr pc = 0; 225 if (x & (1U << 31)) { 226 // Printf("U co[%zu] offset: %x\n", i, x); 227 // this is an offset 228 s32 offset = x; 229 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend. 230 pc = prev_pc + offset; 231 CHECK(pc); 232 } else { 233 // CHECK(i + 1 < size); 234 if (i + 1 >= size) break; 235 uptr hi = x; 236 uptr lo = compressed[i+1]; 237 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo); 238 i++; 239 pc = (hi << 32) | lo; 240 if (!pc) break; 241 } 242 // Printf("U pc[%zu] %zx\n", stack->size, pc); 243 stack->trace[stack->size++] = pc; 244 prev_pc = pc; 245 } 246#endif // SANITIZER_WORDSIZE 247} 248 249} // namespace __sanitizer 250