blob: c87b18e1b696a4fe616f78cd07d44bba9d57d337 [file] [log] [blame]
Alexey Samsonove13f7752013-11-07 06:33:06 +00001//===-- 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 Samsonovca7a2132013-12-25 09:29:54 +000014#include "sanitizer_common.h"
Alexey Samsonov70f38972014-11-05 22:44:36 +000015#include "sanitizer_placement_new.h"
Alexey Samsonove13f7752013-11-07 06:33:06 +000016#include "sanitizer_stacktrace.h"
Alexey Samsonov70f38972014-11-05 22:44:36 +000017#include "sanitizer_stacktrace_printer.h"
Alexey Samsonovca7a2132013-12-25 09:29:54 +000018#include "sanitizer_symbolizer.h"
Alexey Samsonove13f7752013-11-07 06:33:06 +000019
20namespace __sanitizer {
21
Alexey Samsonov9c859272014-10-26 03:35:14 +000022void StackTrace::Print() const {
23 if (trace == nullptr || size == 0) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000024 Printf(" <empty stack>\n\n");
25 return;
26 }
Alexey Samsonovca7a2132013-12-25 09:29:54 +000027 InternalScopedString frame_desc(GetPageSizeCached() * 2);
Kostya Serebryanye31e7782016-05-28 01:25:44 +000028 InternalScopedString dedup_token(GetPageSizeCached());
29 int dedup_frames = common_flags()->dedup_token_length;
Alexey Samsonovca7a2132013-12-25 09:29:54 +000030 uptr frame_num = 0;
Alexey Samsonov9c859272014-10-26 03:35:14 +000031 for (uptr i = 0; i < size && trace[i]; i++) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000032 // PCs in stack traces are actually the return addresses, that is,
33 // addresses of the next instructions after the call.
Alexey Samsonov9c859272014-10-26 03:35:14 +000034 uptr pc = GetPreviousInstructionPc(trace[i]);
Alexey Samsonov0e906682014-12-02 19:48:40 +000035 SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc);
36 CHECK(frames);
37 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000038 frame_desc.clear();
Alexey Samsonovfbaaed62014-11-06 18:43:45 +000039 RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
Filipe Cabecinhas215046b2015-06-04 01:20:06 +000040 cur->info, common_flags()->symbolize_vs_style,
41 common_flags()->strip_path_prefix);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000042 Printf("%s\n", frame_desc.data());
Kostya Serebryanye31e7782016-05-28 01:25:44 +000043 if (dedup_frames-- > 0) {
44 if (dedup_token.length())
45 dedup_token.append("--");
George Karpenkov0b4ebb12017-07-10 20:06:06 +000046 if (cur->info.function != nullptr)
47 dedup_token.append(cur->info.function);
Kostya Serebryanye31e7782016-05-28 01:25:44 +000048 }
Alexey Samsonovca7a2132013-12-25 09:29:54 +000049 }
Alexey Samsonov0e906682014-12-02 19:48:40 +000050 frames->ClearAll();
Alexey Samsonovca7a2132013-12-25 09:29:54 +000051 }
52 // Always print a trailing empty line after stack trace.
53 Printf("\n");
Kostya Serebryanye31e7782016-05-28 01:25:44 +000054 if (dedup_token.length())
55 Printf("DEDUP_TOKEN: %s\n", dedup_token.data());
Alexey Samsonovca7a2132013-12-25 09:29:54 +000056}
57
Evgeniy Stepanovd38af302015-01-22 13:33:16 +000058void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context,
Alexey Samsonov9c859272014-10-26 03:35:14 +000059 uptr stack_top, uptr stack_bottom,
60 bool request_fast_unwind) {
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000061 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 Samsonov9c859272014-10-26 03:35:14 +000069 trace_buffer[0] = pc;
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000070 return;
71 }
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000072 if (!WillUseFastUnwind(request_fast_unwind)) {
Kuba Brecka3d2ba472015-07-02 13:56:37 +000073#if SANITIZER_CAN_SLOW_UNWIND
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000074 if (context)
75 SlowUnwindStackWithContext(pc, context, max_depth);
76 else
77 SlowUnwindStack(pc, max_depth);
Kuba Brecka3d2ba472015-07-02 13:56:37 +000078#else
79 UNREACHABLE("slow unwind requested but not available");
80#endif
Evgeniy Stepanov2629e572014-02-11 13:45:01 +000081 } else {
Alexey Samsonove13f7752013-11-07 06:33:06 +000082 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000083 }
Alexey Samsonove13f7752013-11-07 06:33:06 +000084}
85
Mike Aizatsky0d98da72016-12-05 21:45:14 +000086static 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 Samsonove13f7752013-11-07 06:33:06 +0000101} // namespace __sanitizer
Kostya Serebryany88b93162016-09-19 05:10:32 +0000102using namespace __sanitizer;
Kostya Serebryany9aab75f2016-08-25 21:35:29 +0000103
104extern "C" {
Anna Zaksde3fb6f2016-09-15 22:18:36 +0000105SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany88b93162016-09-19 05:10:32 +0000106void __sanitizer_symbolize_pc(uptr pc, const char *fmt, char *out_buf,
107 uptr out_buf_size) {
Kostya Serebryanyd77e8c02016-09-09 02:13:27 +0000108 if (!out_buf_size) return;
Kostya Serebryany9aab75f2016-08-25 21:35:29 +0000109 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 Serebryanyd77e8c02016-09-09 02:13:27 +0000113 out_buf[out_buf_size - 1] = 0;
Kostya Serebryany9aab75f2016-08-25 21:35:29 +0000114 return;
115 }
116 InternalScopedString frame_desc(GetPageSizeCached());
Vitaly Buka83e57e22018-10-02 17:01:18 +0000117 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 Serebryany9aab75f2016-08-25 21:35:29 +0000136}
Kostya Serebryany88b93162016-09-19 05:10:32 +0000137
138SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryanyf0ca1602016-09-19 14:18:16 +0000139void __sanitizer_symbolize_global(uptr data_addr, const char *fmt,
140 char *out_buf, uptr out_buf_size) {
Kostya Serebryany88b93162016-09-19 05:10:32 +0000141 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 Aizatsky0d98da72016-12-05 21:45:14 +0000150
151SANITIZER_INTERFACE_ATTRIBUTE
152int __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 Serebryany9aab75f2016-08-25 21:35:29 +0000157} // extern "C"