1 //===-- sanitizer_stacktrace_libcdep.cc -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common.h"
15 #include "sanitizer_placement_new.h"
16 #include "sanitizer_stacktrace.h"
17 #include "sanitizer_stacktrace_printer.h"
18 #include "sanitizer_symbolizer.h"
20 namespace __sanitizer {
22 void StackTrace::Print() const {
23 if (trace == nullptr || size == 0) {
24 Printf(" <empty stack>\n\n");
27 InternalScopedString frame_desc(GetPageSizeCached() * 2);
28 InternalScopedString dedup_token(GetPageSizeCached());
29 int dedup_frames = common_flags()->dedup_token_length;
31 for (uptr i = 0; i < size && trace[i]; i++) {
32 // PCs in stack traces are actually the return addresses, that is,
33 // addresses of the next instructions after the call.
34 uptr pc = GetPreviousInstructionPc(trace[i]);
35 SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc);
37 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
39 RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
40 cur->info, common_flags()->symbolize_vs_style,
41 common_flags()->strip_path_prefix);
42 Printf("%s\n", frame_desc.data());
43 if (dedup_frames-- > 0) {
44 if (dedup_token.length())
45 dedup_token.append("--");
46 if (cur->info.function != nullptr)
47 dedup_token.append(cur->info.function);
52 // Always print a trailing empty line after stack trace.
54 if (dedup_token.length())
55 Printf("DEDUP_TOKEN: %s\n", dedup_token.data());
58 void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context,
59 uptr stack_top, uptr stack_bottom,
60 bool request_fast_unwind) {
61 top_frame_bp = (max_depth > 0) ? bp : 0;
62 // Avoid doing any work for small max_depth.
72 if (!WillUseFastUnwind(request_fast_unwind)) {
73 #if SANITIZER_CAN_SLOW_UNWIND
75 SlowUnwindStackWithContext(pc, context, max_depth);
77 SlowUnwindStack(pc, max_depth);
79 UNREACHABLE("slow unwind requested but not available");
82 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
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);
92 if (!ok) return false;
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';
101 } // namespace __sanitizer
102 using namespace __sanitizer;
105 SANITIZER_INTERFACE_ATTRIBUTE
106 void __sanitizer_symbolize_pc(uptr pc, const char *fmt, char *out_buf,
108 if (!out_buf_size) return;
109 pc = StackTrace::GetPreviousInstructionPc(pc);
110 SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc);
112 internal_strncpy(out_buf, "<can't symbolize>", out_buf_size);
113 out_buf[out_buf_size - 1] = 0;
116 InternalScopedString frame_desc(GetPageSizeCached());
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;
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())
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());
134 CHECK(out_buf <= out_end);
138 SANITIZER_INTERFACE_ATTRIBUTE
139 void __sanitizer_symbolize_global(uptr data_addr, const char *fmt,
140 char *out_buf, uptr out_buf_size) {
141 if (!out_buf_size) return;
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;
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,