[Sanitizers] Rename Symbolizer::SymbolizeCode to Symbolizer::SymbolizePC
[blocksruntime.git] / lib / sanitizer_common / sanitizer_stacktrace.cc
blobc6c3c69f60dd7a03159c4ce689cf227ac0331ec2
1 //===-- sanitizer_stacktrace.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 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common.h"
15 #include "sanitizer_flags.h"
16 #include "sanitizer_procmaps.h"
17 #include "sanitizer_stacktrace.h"
18 #include "sanitizer_symbolizer.h"
20 namespace __sanitizer {
22 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
23 #ifdef __arm__
24 // Cancel Thumb bit.
25 pc = pc & (~1);
26 #endif
27 #if defined(__sparc__)
28 return pc - 8;
29 #else
30 return pc - 1;
31 #endif
34 static void PrintStackFramePrefix(InternalScopedString *buffer, uptr frame_num,
35 uptr pc) {
36 buffer->append(" #%zu 0x%zx", frame_num, pc);
39 void StackTrace::PrintStack(const uptr *addr, uptr size,
40 SymbolizeCallback symbolize_callback) {
41 if (addr == 0 || size == 0) {
42 Printf(" <empty stack>\n\n");
43 return;
45 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
46 InternalScopedBuffer<char> buff(GetPageSizeCached() * 2);
47 InternalScopedBuffer<AddressInfo> addr_frames(64);
48 InternalScopedString frame_desc(GetPageSizeCached() * 2);
49 uptr frame_num = 0;
50 for (uptr i = 0; i < size && addr[i]; i++) {
51 // PCs in stack traces are actually the return addresses, that is,
52 // addresses of the next instructions after the call.
53 uptr pc = GetPreviousInstructionPc(addr[i]);
54 uptr addr_frames_num = 0; // The number of stack frames for current
55 // instruction address.
56 if (symbolize_callback) {
57 if (symbolize_callback((void*)pc, buff.data(), buff.size())) {
58 addr_frames_num = 1;
59 frame_desc.clear();
60 PrintStackFramePrefix(&frame_desc, frame_num, pc);
61 // We can't know anything about the string returned by external
62 // symbolizer, but if it starts with filename, try to strip path prefix
63 // from it.
64 frame_desc.append(
65 " %s",
66 StripPathPrefix(buff.data(), common_flags()->strip_path_prefix));
67 Printf("%s\n", frame_desc.data());
68 frame_num++;
71 if (common_flags()->symbolize && addr_frames_num == 0) {
72 // Use our own (online) symbolizer, if necessary.
73 if (Symbolizer *sym = Symbolizer::GetOrNull())
74 addr_frames_num =
75 sym->SymbolizePC(pc, addr_frames.data(), addr_frames.size());
76 for (uptr j = 0; j < addr_frames_num; j++) {
77 AddressInfo &info = addr_frames[j];
78 frame_desc.clear();
79 PrintStackFramePrefix(&frame_desc, frame_num, pc);
80 if (info.function) {
81 frame_desc.append(" in %s", info.function);
83 if (info.file) {
84 frame_desc.append(" ");
85 PrintSourceLocation(&frame_desc, info.file, info.line, info.column);
86 } else if (info.module) {
87 frame_desc.append(" ");
88 PrintModuleAndOffset(&frame_desc, info.module, info.module_offset);
90 Printf("%s\n", frame_desc.data());
91 frame_num++;
92 info.Clear();
95 if (addr_frames_num == 0) {
96 // If online symbolization failed, try to output at least module and
97 // offset for instruction.
98 frame_desc.clear();
99 PrintStackFramePrefix(&frame_desc, frame_num, pc);
100 uptr offset;
101 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
102 buff.data(), buff.size(),
103 /* protection */0)) {
104 frame_desc.append(" ");
105 PrintModuleAndOffset(&frame_desc, buff.data(), offset);
107 Printf("%s\n", frame_desc.data());
108 frame_num++;
111 // Always print a trailing empty line after stack trace.
112 Printf("\n");
115 uptr StackTrace::GetCurrentPc() {
116 return GET_CALLER_PC();
119 void StackTrace::FastUnwindStack(uptr pc, uptr bp,
120 uptr stack_top, uptr stack_bottom,
121 uptr max_depth) {
122 if (max_depth == 0) {
123 size = 0;
124 return;
126 trace[0] = pc;
127 size = 1;
128 uptr *frame = (uptr *)bp;
129 uptr *prev_frame = frame - 1;
130 if (stack_top < 4096) return; // Sanity check for stack top.
131 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
132 while (frame > prev_frame &&
133 frame < (uptr *)stack_top - 2 &&
134 frame > (uptr *)stack_bottom &&
135 IsAligned((uptr)frame, sizeof(*frame)) &&
136 size < max_depth) {
137 uptr pc1 = frame[1];
138 if (pc1 != pc) {
139 trace[size++] = pc1;
141 prev_frame = frame;
142 frame = (uptr*)frame[0];
146 static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
147 return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
150 void StackTrace::PopStackFrames(uptr count) {
151 CHECK_LT(count, size);
152 size -= count;
153 for (uptr i = 0; i < size; ++i) {
154 trace[i] = trace[i + count];
158 uptr StackTrace::LocatePcInTrace(uptr pc) {
159 // Use threshold to find PC in stack trace, as PC we want to unwind from may
160 // slightly differ from return address in the actual unwinded stack trace.
161 const int kPcThreshold = 192;
162 for (uptr i = 0; i < size; ++i) {
163 if (MatchPc(pc, trace[i], kPcThreshold))
164 return i;
166 return 0;
169 } // namespace __sanitizer