Alexey Samsonov | e74968c | 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 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 14 | #include "sanitizer_common.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 15 | #include "sanitizer_placement_new.h" |
Alexey Samsonov | e74968c | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 16 | #include "sanitizer_stacktrace.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 17 | #include "sanitizer_stacktrace_printer.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 18 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | e74968c | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __sanitizer { |
| 21 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 22 | void StackTrace::Print() const { |
| 23 | if (trace == nullptr || size == 0) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 24 | Printf(" <empty stack>\n\n"); |
| 25 | return; |
| 26 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 27 | InternalScopedString frame_desc(GetPageSizeCached() * 2); |
| 28 | uptr frame_num = 0; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 29 | for (uptr i = 0; i < size && trace[i]; i++) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 30 | // PCs in stack traces are actually the return addresses, that is, |
| 31 | // addresses of the next instructions after the call. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 32 | uptr pc = GetPreviousInstructionPc(trace[i]); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 33 | SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc); |
| 34 | CHECK(frames); |
| 35 | for (SymbolizedStack *cur = frames; cur; cur = cur->next) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | frame_desc.clear(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 37 | RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++, |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 38 | cur->info, common_flags()->symbolize_vs_style, |
| 39 | common_flags()->strip_path_prefix); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 40 | Printf("%s\n", frame_desc.data()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 41 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 42 | frames->ClearAll(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 43 | } |
| 44 | // Always print a trailing empty line after stack trace. |
| 45 | Printf("\n"); |
| 46 | } |
| 47 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 48 | void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 49 | uptr stack_top, uptr stack_bottom, |
| 50 | bool request_fast_unwind) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 51 | top_frame_bp = (max_depth > 0) ? bp : 0; |
| 52 | // Avoid doing any work for small max_depth. |
| 53 | if (max_depth == 0) { |
| 54 | size = 0; |
| 55 | return; |
| 56 | } |
| 57 | if (max_depth == 1) { |
| 58 | size = 1; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 59 | trace_buffer[0] = pc; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | if (!WillUseFastUnwind(request_fast_unwind)) { |
| 63 | if (context) |
| 64 | SlowUnwindStackWithContext(pc, context, max_depth); |
| 65 | else |
| 66 | SlowUnwindStack(pc, max_depth); |
| 67 | } else { |
| 68 | FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth); |
| 69 | } |
Alexey Samsonov | e74968c | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace __sanitizer |