sanitizer_stacktrace.cc revision 064da32709c8bf81283062abc04cf6e88e6c075d
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, bool symbolize, 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 (symbolize && addr_frames_num == 0 && &getSymbolizer) { 66 // Use our own (online) symbolizer, if necessary. 67 addr_frames_num = getSymbolizer()->SymbolizeCode( 68 pc, addr_frames.data(), addr_frames.size()); 69 for (uptr j = 0; j < addr_frames_num; j++) { 70 AddressInfo &info = addr_frames[j]; 71 PrintStackFramePrefix(frame_num, pc); 72 if (info.function) { 73 Printf(" in %s", info.function); 74 } 75 if (info.file) { 76 Printf(" "); 77 PrintSourceLocation(info.file, info.line, info.column); 78 } else if (info.module) { 79 Printf(" "); 80 PrintModuleAndOffset(info.module, info.module_offset); 81 } 82 Printf("\n"); 83 info.Clear(); 84 frame_num++; 85 } 86 } 87 if (addr_frames_num == 0) { 88 // If online symbolization failed, try to output at least module and 89 // offset for instruction. 90 PrintStackFramePrefix(frame_num, pc); 91 uptr offset; 92 if (proc_maps.GetObjectNameAndOffset(pc, &offset, 93 buff.data(), buff.size(), 94 /* protection */0)) { 95 Printf(" "); 96 PrintModuleAndOffset(buff.data(), offset); 97 } 98 Printf("\n"); 99 frame_num++; 100 } 101 } 102} 103 104uptr StackTrace::GetCurrentPc() { 105 return GET_CALLER_PC(); 106} 107 108void StackTrace::FastUnwindStack(uptr pc, uptr bp, 109 uptr stack_top, uptr stack_bottom, 110 uptr max_depth) { 111 max_size = max_depth; 112 if (max_size == 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_size) { 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. 147SANITIZER_INTERFACE_ATTRIBUTE 148uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) { 149#if SANITIZER_WORDSIZE == 32 150 // Don't compress, just copy. 151 uptr res = 0; 152 for (uptr i = 0; i < stack->size && i < size; i++) { 153 compressed[i] = stack->trace[i]; 154 res++; 155 } 156 if (stack->size < size) 157 compressed[stack->size] = 0; 158#else // 64 bits, compress. 159 uptr prev_pc = 0; 160 const uptr kMaxOffset = (1ULL << 30) - 1; 161 uptr c_index = 0; 162 uptr res = 0; 163 for (uptr i = 0, n = stack->size; i < n; i++) { 164 uptr pc = stack->trace[i]; 165 if (!pc) break; 166 if ((s64)pc < 0) break; 167 // Printf("C pc[%zu] %zx\n", i, pc); 168 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) { 169 uptr offset = (s64)(pc - prev_pc); 170 offset |= (1U << 31); 171 if (c_index >= size) break; 172 // Printf("C co[%zu] offset %zx\n", i, offset); 173 compressed[c_index++] = offset; 174 } else { 175 uptr hi = pc >> 32; 176 uptr lo = (pc << 32) >> 32; 177 CHECK_EQ((hi & (1 << 31)), 0); 178 if (c_index + 1 >= size) break; 179 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo); 180 compressed[c_index++] = hi; 181 compressed[c_index++] = lo; 182 } 183 res++; 184 prev_pc = pc; 185 } 186 if (c_index < size) 187 compressed[c_index] = 0; 188 if (c_index + 1 < size) 189 compressed[c_index + 1] = 0; 190#endif // SANITIZER_WORDSIZE 191 192 // debug-only code 193#if 0 194 StackTrace check_stack; 195 UncompressStack(&check_stack, compressed, size); 196 if (res < check_stack.size) { 197 Printf("res %zu check_stack.size %zu; c_size %zu\n", res, 198 check_stack.size, size); 199 } 200 // |res| may be greater than check_stack.size, because 201 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames. 202 CHECK(res >= check_stack.size); 203 CHECK_EQ(0, REAL(memcmp)(check_stack.trace, stack->trace, 204 check_stack.size * sizeof(uptr))); 205#endif 206 207 return res; 208} 209 210SANITIZER_INTERFACE_ATTRIBUTE 211void StackTrace::UncompressStack(StackTrace *stack, 212 u32 *compressed, uptr size) { 213#if SANITIZER_WORDSIZE == 32 214 // Don't uncompress, just copy. 215 stack->size = 0; 216 for (uptr i = 0; i < size && i < kStackTraceMax; i++) { 217 if (!compressed[i]) break; 218 stack->size++; 219 stack->trace[i] = compressed[i]; 220 } 221#else // 64 bits, uncompress 222 uptr prev_pc = 0; 223 stack->size = 0; 224 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) { 225 u32 x = compressed[i]; 226 uptr pc = 0; 227 if (x & (1U << 31)) { 228 // Printf("U co[%zu] offset: %x\n", i, x); 229 // this is an offset 230 s32 offset = x; 231 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend. 232 pc = prev_pc + offset; 233 CHECK(pc); 234 } else { 235 // CHECK(i + 1 < size); 236 if (i + 1 >= size) break; 237 uptr hi = x; 238 uptr lo = compressed[i+1]; 239 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo); 240 i++; 241 pc = (hi << 32) | lo; 242 if (!pc) break; 243 } 244 // Printf("U pc[%zu] %zx\n", stack->size, pc); 245 stack->trace[stack->size++] = pc; 246 prev_pc = pc; 247 } 248#endif // SANITIZER_WORDSIZE 249} 250 251} // namespace __sanitizer 252