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 | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 15 | #include "sanitizer_placement_new.h" |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 16 | #include "sanitizer_stacktrace.h" |
Alexey Samsonov | 70f3897 | 2014-11-05 22:44:36 +0000 | [diff] [blame] | 17 | #include "sanitizer_stacktrace_printer.h" |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 18 | #include "sanitizer_symbolizer.h" |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __sanitizer { |
| 21 | |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 22 | void StackTrace::Print() const { |
| 23 | if (trace == nullptr || size == 0) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 24 | Printf(" <empty stack>\n\n"); |
| 25 | return; |
| 26 | } |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 27 | InternalScopedString frame_desc(GetPageSizeCached() * 2); |
| 28 | uptr frame_num = 0; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 29 | for (uptr i = 0; i < size && trace[i]; i++) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 30 | // PCs in stack traces are actually the return addresses, that is, |
| 31 | // addresses of the next instructions after the call. |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 32 | uptr pc = GetPreviousInstructionPc(trace[i]); |
Alexey Samsonov | 0e90668 | 2014-12-02 19:48:40 +0000 | [diff] [blame] | 33 | SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc); |
| 34 | CHECK(frames); |
| 35 | for (SymbolizedStack *cur = frames; cur; cur = cur->next) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 36 | frame_desc.clear(); |
Alexey Samsonov | fbaaed6 | 2014-11-06 18:43:45 +0000 | [diff] [blame] | 37 | RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++, |
Filipe Cabecinhas | 215046b | 2015-06-04 01:20:06 +0000 | [diff] [blame] | 38 | cur->info, common_flags()->symbolize_vs_style, |
| 39 | common_flags()->strip_path_prefix); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 40 | Printf("%s\n", frame_desc.data()); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 41 | } |
Alexey Samsonov | 0e90668 | 2014-12-02 19:48:40 +0000 | [diff] [blame] | 42 | frames->ClearAll(); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 43 | } |
| 44 | // Always print a trailing empty line after stack trace. |
| 45 | Printf("\n"); |
| 46 | } |
| 47 | |
Evgeniy Stepanov | d38af30 | 2015-01-22 13:33:16 +0000 | [diff] [blame] | 48 | void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context, |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 49 | uptr stack_top, uptr stack_bottom, |
| 50 | bool request_fast_unwind) { |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [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; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 59 | trace_buffer[0] = pc; |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 60 | return; |
| 61 | } |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 62 | if (!WillUseFastUnwind(request_fast_unwind)) { |
Kuba Brecka | 3d2ba47 | 2015-07-02 13:56:37 +0000 | [diff] [blame^] | 63 | #if SANITIZER_CAN_SLOW_UNWIND |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 64 | if (context) |
| 65 | SlowUnwindStackWithContext(pc, context, max_depth); |
| 66 | else |
| 67 | SlowUnwindStack(pc, max_depth); |
Kuba Brecka | 3d2ba47 | 2015-07-02 13:56:37 +0000 | [diff] [blame^] | 68 | #else |
| 69 | UNREACHABLE("slow unwind requested but not available"); |
| 70 | #endif |
Evgeniy Stepanov | 2629e57 | 2014-02-11 13:45:01 +0000 | [diff] [blame] | 71 | } else { |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 72 | FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth); |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 73 | } |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace __sanitizer |