Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 1 | //===-- sanitizer_stacktrace_libcdep.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 | |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 14 | #include "sanitizer_common.h" |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 15 | #include "sanitizer_stacktrace.h" |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 16 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 17 | |
| 18 | namespace __sanitizer { |
| 19 | |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 20 | static void PrintStackFramePrefix(InternalScopedString *buffer, uptr frame_num, |
| 21 | uptr pc) { |
| 22 | buffer->append(" #%zu 0x%zx", frame_num, pc); |
| 23 | } |
| 24 | |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame^] | 25 | void StackTrace::Print() const { |
| 26 | if (trace == nullptr || size == 0) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 27 | Printf(" <empty stack>\n\n"); |
| 28 | return; |
| 29 | } |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 30 | InternalScopedBuffer<AddressInfo> addr_frames(64); |
| 31 | InternalScopedString frame_desc(GetPageSizeCached() * 2); |
| 32 | uptr frame_num = 0; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame^] | 33 | for (uptr i = 0; i < size && trace[i]; i++) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 34 | // PCs in stack traces are actually the return addresses, that is, |
| 35 | // addresses of the next instructions after the call. |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame^] | 36 | uptr pc = GetPreviousInstructionPc(trace[i]); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 37 | uptr addr_frames_num = Symbolizer::GetOrInit()->SymbolizePC( |
| 38 | pc, addr_frames.data(), addr_frames.size()); |
Sergey Matveev | 2be4a28 | 2014-05-23 16:04:41 +0000 | [diff] [blame] | 39 | if (addr_frames_num == 0) { |
| 40 | frame_desc.clear(); |
Alexey Samsonov | 3eb69f5 | 2014-10-20 23:58:08 +0000 | [diff] [blame] | 41 | PrintStackFramePrefix(&frame_desc, frame_num++, pc); |
Sergey Matveev | 2be4a28 | 2014-05-23 16:04:41 +0000 | [diff] [blame] | 42 | frame_desc.append(" (<unknown module>)"); |
| 43 | Printf("%s\n", frame_desc.data()); |
Sergey Matveev | 2be4a28 | 2014-05-23 16:04:41 +0000 | [diff] [blame] | 44 | continue; |
| 45 | } |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 46 | for (uptr j = 0; j < addr_frames_num; j++) { |
| 47 | AddressInfo &info = addr_frames[j]; |
| 48 | frame_desc.clear(); |
Alexey Samsonov | 3eb69f5 | 2014-10-20 23:58:08 +0000 | [diff] [blame] | 49 | PrintStackFramePrefix(&frame_desc, frame_num++, pc); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 50 | if (info.function) { |
| 51 | frame_desc.append(" in %s", info.function); |
| 52 | // Print offset in function if we don't know the source file. |
| 53 | if (!info.file && info.function_offset != AddressInfo::kUnknown) |
| 54 | frame_desc.append("+0x%zx", info.function_offset); |
| 55 | } |
| 56 | if (info.file) { |
| 57 | frame_desc.append(" "); |
| 58 | PrintSourceLocation(&frame_desc, info.file, info.line, info.column); |
| 59 | } else if (info.module) { |
| 60 | frame_desc.append(" "); |
| 61 | PrintModuleAndOffset(&frame_desc, info.module, info.module_offset); |
| 62 | } |
| 63 | Printf("%s\n", frame_desc.data()); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 64 | info.Clear(); |
| 65 | } |
| 66 | } |
| 67 | // Always print a trailing empty line after stack trace. |
| 68 | Printf("\n"); |
| 69 | } |
| 70 | |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame^] | 71 | void BufferedStackTrace::Unwind(uptr max_depth, uptr pc, uptr bp, void *context, |
| 72 | uptr stack_top, uptr stack_bottom, |
| 73 | bool request_fast_unwind) { |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 74 | top_frame_bp = (max_depth > 0) ? bp : 0; |
| 75 | // Avoid doing any work for small max_depth. |
| 76 | if (max_depth == 0) { |
| 77 | size = 0; |
| 78 | return; |
| 79 | } |
| 80 | if (max_depth == 1) { |
| 81 | size = 1; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame^] | 82 | trace_buffer[0] = pc; |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 83 | return; |
| 84 | } |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 85 | if (!WillUseFastUnwind(request_fast_unwind)) { |
| 86 | if (context) |
| 87 | SlowUnwindStackWithContext(pc, context, max_depth); |
| 88 | else |
| 89 | SlowUnwindStack(pc, max_depth); |
Evgeniy Stepanov | 2629e57 | 2014-02-11 13:45:01 +0000 | [diff] [blame] | 90 | } else { |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 91 | FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth); |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 92 | } |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } // namespace __sanitizer |