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("--"); |
George Karpenkov | 0b4ebb1 | 2017-07-10 20:06:06 +0000 | [diff] [blame] | 46 | if (cur->info.function != nullptr) |
| 47 | dedup_token.append(cur->info.function); |
Kostya Serebryany | e31e778 | 2016-05-28 01:25:44 +0000 | [diff] [blame] | 48 | } |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 49 | } |
Alexey Samsonov | 0e90668 | 2014-12-02 19:48:40 +0000 | [diff] [blame] | 50 | frames->ClearAll(); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 51 | } |
| 52 | // Always print a trailing empty line after stack trace. |
| 53 | Printf("\n"); |
Kostya Serebryany | e31e778 | 2016-05-28 01:25:44 +0000 | [diff] [blame] | 54 | if (dedup_token.length()) |
| 55 | Printf("DEDUP_TOKEN: %s\n", dedup_token.data()); |
Alexey Samsonov | ca7a213 | 2013-12-25 09:29:54 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Evgeniy Stepanov | d38af30 | 2015-01-22 13:33:16 +0000 | [diff] [blame] | 58 | void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context, |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 59 | uptr stack_top, uptr stack_bottom, |
| 60 | bool request_fast_unwind) { |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 61 | top_frame_bp = (max_depth > 0) ? bp : 0; |
| 62 | // Avoid doing any work for small max_depth. |
| 63 | if (max_depth == 0) { |
| 64 | size = 0; |
| 65 | return; |
| 66 | } |
| 67 | if (max_depth == 1) { |
| 68 | size = 1; |
Alexey Samsonov | 9c85927 | 2014-10-26 03:35:14 +0000 | [diff] [blame] | 69 | trace_buffer[0] = pc; |
Alexey Samsonov | 3e8467b | 2014-03-04 12:21:28 +0000 | [diff] [blame] | 70 | return; |
| 71 | } |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 72 | if (!WillUseFastUnwind(request_fast_unwind)) { |
Kuba Brecka | 3d2ba47 | 2015-07-02 13:56:37 +0000 | [diff] [blame] | 73 | #if SANITIZER_CAN_SLOW_UNWIND |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 74 | if (context) |
| 75 | SlowUnwindStackWithContext(pc, context, max_depth); |
| 76 | else |
| 77 | SlowUnwindStack(pc, max_depth); |
Kuba Brecka | 3d2ba47 | 2015-07-02 13:56:37 +0000 | [diff] [blame] | 78 | #else |
| 79 | UNREACHABLE("slow unwind requested but not available"); |
| 80 | #endif |
Evgeniy Stepanov | 2629e57 | 2014-02-11 13:45:01 +0000 | [diff] [blame] | 81 | } else { |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 82 | FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth); |
Evgeniy Stepanov | 769d46f | 2014-02-11 13:38:57 +0000 | [diff] [blame] | 83 | } |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Mike Aizatsky | 0d98da7 | 2016-12-05 21:45:14 +0000 | [diff] [blame] | 86 | static int GetModuleAndOffsetForPc(uptr pc, char *module_name, |
| 87 | uptr module_name_len, uptr *pc_offset) { |
| 88 | const char *found_module_name = nullptr; |
| 89 | bool ok = Symbolizer::GetOrInit()->GetModuleNameAndOffsetForPC( |
| 90 | pc, &found_module_name, pc_offset); |
| 91 | |
| 92 | if (!ok) return false; |
| 93 | |
| 94 | if (module_name && module_name_len) { |
| 95 | internal_strncpy(module_name, found_module_name, module_name_len); |
| 96 | module_name[module_name_len - 1] = '\x00'; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
Alexey Samsonov | e13f775 | 2013-11-07 06:33:06 +0000 | [diff] [blame] | 101 | } // namespace __sanitizer |
Kostya Serebryany | 88b9316 | 2016-09-19 05:10:32 +0000 | [diff] [blame] | 102 | using namespace __sanitizer; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 103 | |
| 104 | extern "C" { |
Anna Zaks | de3fb6f | 2016-09-15 22:18:36 +0000 | [diff] [blame] | 105 | SANITIZER_INTERFACE_ATTRIBUTE |
Kostya Serebryany | 88b9316 | 2016-09-19 05:10:32 +0000 | [diff] [blame] | 106 | void __sanitizer_symbolize_pc(uptr pc, const char *fmt, char *out_buf, |
| 107 | uptr out_buf_size) { |
Kostya Serebryany | d77e8c0 | 2016-09-09 02:13:27 +0000 | [diff] [blame] | 108 | if (!out_buf_size) return; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 109 | pc = StackTrace::GetPreviousInstructionPc(pc); |
| 110 | SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc); |
| 111 | if (!frame) { |
| 112 | internal_strncpy(out_buf, "<can't symbolize>", out_buf_size); |
Kostya Serebryany | d77e8c0 | 2016-09-09 02:13:27 +0000 | [diff] [blame] | 113 | out_buf[out_buf_size - 1] = 0; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | InternalScopedString frame_desc(GetPageSizeCached()); |
Vitaly Buka | 83e57e2 | 2018-10-02 17:01:18 +0000 | [diff] [blame] | 117 | uptr frame_num = 0; |
| 118 | // Reserve one byte for the final 0. |
| 119 | char *out_end = out_buf + out_buf_size - 1; |
| 120 | for (SymbolizedStack *cur = frame; cur && out_buf < out_end; |
| 121 | cur = cur->next) { |
| 122 | frame_desc.clear(); |
| 123 | RenderFrame(&frame_desc, fmt, frame_num++, cur->info, |
| 124 | common_flags()->symbolize_vs_style, |
| 125 | common_flags()->strip_path_prefix); |
| 126 | if (!frame_desc.length()) |
| 127 | continue; |
| 128 | // Reserve one byte for the terminating 0. |
| 129 | uptr n = out_end - out_buf - 1; |
| 130 | internal_strncpy(out_buf, frame_desc.data(), n); |
| 131 | out_buf += __sanitizer::Min<uptr>(n, frame_desc.length()); |
| 132 | *out_buf++ = 0; |
| 133 | } |
| 134 | CHECK(out_buf <= out_end); |
| 135 | *out_buf = 0; |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 136 | } |
Kostya Serebryany | 88b9316 | 2016-09-19 05:10:32 +0000 | [diff] [blame] | 137 | |
| 138 | SANITIZER_INTERFACE_ATTRIBUTE |
Kostya Serebryany | f0ca160 | 2016-09-19 14:18:16 +0000 | [diff] [blame] | 139 | void __sanitizer_symbolize_global(uptr data_addr, const char *fmt, |
| 140 | char *out_buf, uptr out_buf_size) { |
Kostya Serebryany | 88b9316 | 2016-09-19 05:10:32 +0000 | [diff] [blame] | 141 | if (!out_buf_size) return; |
| 142 | out_buf[0] = 0; |
| 143 | DataInfo DI; |
| 144 | if (!Symbolizer::GetOrInit()->SymbolizeData(data_addr, &DI)) return; |
| 145 | InternalScopedString data_desc(GetPageSizeCached()); |
| 146 | RenderData(&data_desc, fmt, &DI, common_flags()->strip_path_prefix); |
| 147 | internal_strncpy(out_buf, data_desc.data(), out_buf_size); |
| 148 | out_buf[out_buf_size - 1] = 0; |
| 149 | } |
Mike Aizatsky | 0d98da7 | 2016-12-05 21:45:14 +0000 | [diff] [blame] | 150 | |
| 151 | SANITIZER_INTERFACE_ATTRIBUTE |
| 152 | int __sanitizer_get_module_and_offset_for_pc( // NOLINT |
| 153 | uptr pc, char *module_name, uptr module_name_len, uptr *pc_offset) { |
| 154 | return __sanitizer::GetModuleAndOffsetForPc(pc, module_name, module_name_len, |
| 155 | pc_offset); |
| 156 | } |
Kostya Serebryany | 9aab75f | 2016-08-25 21:35:29 +0000 | [diff] [blame] | 157 | } // extern "C" |