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); |
Kostya Serebryany | e31e778 | 2016-05-28 01:25:44 +0000 | [diff] [blame] | 28 | InternalScopedString dedup_token(GetPageSizeCached()); |
| 29 | int dedup_frames = common_flags()->dedup_token_length; |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 30 | uptr frame_num = 0; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 31 | for (uptr i = 0; i < size && trace[i]; i++) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 32 | // PCs in stack traces are actually the return addresses, that is, |
| 33 | // addresses of the next instructions after the call. |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 34 | uptr pc = GetPreviousInstructionPc(trace[i]); |
Alexey Samsonov | 0e90668 | 2014-12-02 19:48:40 +0000 | [diff] [blame] | 35 | SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc); |
| 36 | CHECK(frames); |
| 37 | for (SymbolizedStack *cur = frames; cur; cur = cur->next) { |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 38 | frame_desc.clear(); |
Alexey Samsonov | fbaaed6 | 2014-11-06 18:43:45 +0000 | [diff] [blame] | 39 | RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++, |
Filipe Cabecinhas | 215046b | 2015-06-04 01:20:06 +0000 | [diff] [blame] | 40 | cur->info, common_flags()->symbolize_vs_style, |
| 41 | common_flags()->strip_path_prefix); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 42 | Printf("%s\n", frame_desc.data()); |
Kostya Serebryany | e31e778 | 2016-05-28 01:25:44 +0000 | [diff] [blame] | 43 | if (dedup_frames-- > 0) { |
| 44 | if (dedup_token.length()) |
| 45 | dedup_token.append("--"); |
| 46 | dedup_token.append(cur->info.function); |
| 47 | } |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 48 | } |
Alexey Samsonov | 0e90668 | 2014-12-02 19:48:40 +0000 | [diff] [blame] | 49 | frames->ClearAll(); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 50 | } |
| 51 | // Always print a trailing empty line after stack trace. |
| 52 | Printf("\n"); |
Kostya Serebryany | e31e778 | 2016-05-28 01:25:44 +0000 | [diff] [blame] | 53 | if (dedup_token.length()) |
| 54 | Printf("DEDUP_TOKEN: %s\n", dedup_token.data()); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Evgeniy Stepanov | d38af30 | 2015-01-22 13:33:16 +0000 | [diff] [blame] | 57 | void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context, |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 58 | uptr stack_top, uptr stack_bottom, |
| 59 | bool request_fast_unwind) { |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 60 | top_frame_bp = (max_depth > 0) ? bp : 0; |
| 61 | // Avoid doing any work for small max_depth. |
| 62 | if (max_depth == 0) { |
| 63 | size = 0; |
| 64 | return; |
| 65 | } |
| 66 | if (max_depth == 1) { |
| 67 | size = 1; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 68 | trace_buffer[0] = pc; |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 69 | return; |
| 70 | } |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 71 | if (!WillUseFastUnwind(request_fast_unwind)) { |
Kuba Brecka | 3d2ba47 | 2015-07-02 13:56:37 +0000 | [diff] [blame] | 72 | #if SANITIZER_CAN_SLOW_UNWIND |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 73 | if (context) |
| 74 | SlowUnwindStackWithContext(pc, context, max_depth); |
| 75 | else |
| 76 | SlowUnwindStack(pc, max_depth); |
Kuba Brecka | 3d2ba47 | 2015-07-02 13:56:37 +0000 | [diff] [blame] | 77 | #else |
| 78 | UNREACHABLE("slow unwind requested but not available"); |
| 79 | #endif |
Evgeniy Stepanov | 2629e57 | 2014-02-11 13:45:01 +0000 | [diff] [blame] | 80 | } else { |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 81 | FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth); |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 82 | } |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace __sanitizer |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 86 | |
| 87 | extern "C" { |
| 88 | SANITIZER_INTERFACE_ATTRIBUTE |
| 89 | void __sanitizer_symbolize_pc(uptr pc, const char *fmt, char *out_buf, |
| 90 | uptr out_buf_size) { |
Kostya Serebryany | d77e8c0 | 2016-09-09 02:13:27 +0000 | [diff] [blame^] | 91 | if (!out_buf_size) return; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 92 | using namespace __sanitizer; |
| 93 | pc = StackTrace::GetPreviousInstructionPc(pc); |
| 94 | SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc); |
| 95 | if (!frame) { |
| 96 | internal_strncpy(out_buf, "<can't symbolize>", out_buf_size); |
Kostya Serebryany | d77e8c0 | 2016-09-09 02:13:27 +0000 | [diff] [blame^] | 97 | out_buf[out_buf_size - 1] = 0; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | InternalScopedString frame_desc(GetPageSizeCached()); |
| 101 | RenderFrame(&frame_desc, fmt, 0, frame->info, |
| 102 | common_flags()->symbolize_vs_style, |
| 103 | common_flags()->strip_path_prefix); |
| 104 | internal_strncpy(out_buf, frame_desc.data(), out_buf_size); |
Kostya Serebryany | d77e8c0 | 2016-09-09 02:13:27 +0000 | [diff] [blame^] | 105 | out_buf[out_buf_size - 1] = 0; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 106 | } |
| 107 | } // extern "C" |