Import the asan runtime library into GCC tree
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.cc
blobf6d7a0966c2a1d9464044d065f6eff9be5825c91
1 //===-- sanitizer_stacktrace.cc -------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common.h"
13 #include "sanitizer_procmaps.h"
14 #include "sanitizer_stacktrace.h"
15 #include "sanitizer_symbolizer.h"
17 namespace __sanitizer {
18 static const char *StripPathPrefix(const char *filepath,
19 const char *strip_file_prefix) {
20 if (filepath == internal_strstr(filepath, strip_file_prefix))
21 return filepath + internal_strlen(strip_file_prefix);
22 return filepath;
25 // ----------------------- StackTrace ----------------------------- {{{1
26 // PCs in stack traces are actually the return addresses, that is,
27 // addresses of the next instructions after the call. That's why we
28 // decrement them.
29 static uptr patch_pc(uptr pc) {
30 #ifdef __arm__
31 // Cancel Thumb bit.
32 pc = pc & (~1);
33 #endif
34 return pc - 1;
37 static void PrintStackFramePrefix(uptr frame_num, uptr pc) {
38 Printf(" #%zu 0x%zx", frame_num, pc);
41 static void PrintSourceLocation(const char *file, int line, int column,
42 const char *strip_file_prefix) {
43 CHECK(file);
44 Printf(" %s", StripPathPrefix(file, strip_file_prefix));
45 if (line > 0) {
46 Printf(":%d", line);
47 if (column > 0)
48 Printf(":%d", column);
52 static void PrintModuleAndOffset(const char *module, uptr offset,
53 const char *strip_file_prefix) {
54 Printf(" (%s+0x%zx)", StripPathPrefix(module, strip_file_prefix), offset);
57 void StackTrace::PrintStack(const uptr *addr, uptr size,
58 bool symbolize, const char *strip_file_prefix,
59 SymbolizeCallback symbolize_callback ) {
60 MemoryMappingLayout proc_maps;
61 InternalScopedBuffer<char> buff(kPageSize * 2);
62 InternalScopedBuffer<AddressInfo> addr_frames(64);
63 uptr frame_num = 0;
64 for (uptr i = 0; i < size && addr[i]; i++) {
65 uptr pc = patch_pc(addr[i]);
66 uptr addr_frames_num = 0; // The number of stack frames for current
67 // instruction address.
68 if (symbolize_callback) {
69 if (symbolize_callback((void*)pc, buff.data(), buff.size())) {
70 addr_frames_num = 1;
71 PrintStackFramePrefix(frame_num, pc);
72 // We can't know anything about the string returned by external
73 // symbolizer, but if it starts with filename, try to strip path prefix
74 // from it.
75 Printf(" %s\n", StripPathPrefix(buff.data(), strip_file_prefix));
76 frame_num++;
78 } else if (symbolize) {
79 // Use our own (online) symbolizer, if necessary.
80 addr_frames_num = SymbolizeCode(pc, addr_frames.data(),
81 addr_frames.size());
82 for (uptr j = 0; j < addr_frames_num; j++) {
83 AddressInfo &info = addr_frames[j];
84 PrintStackFramePrefix(frame_num, pc);
85 if (info.function) {
86 Printf(" in %s", info.function);
88 if (info.file) {
89 PrintSourceLocation(info.file, info.line, info.column,
90 strip_file_prefix);
91 } else if (info.module) {
92 PrintModuleAndOffset(info.module, info.module_offset,
93 strip_file_prefix);
95 Printf("\n");
96 info.Clear();
97 frame_num++;
100 if (addr_frames_num == 0) {
101 // If online symbolization failed, try to output at least module and
102 // offset for instruction.
103 PrintStackFramePrefix(frame_num, pc);
104 uptr offset;
105 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
106 buff.data(), buff.size())) {
107 PrintModuleAndOffset(buff.data(), offset, strip_file_prefix);
109 Printf("\n");
110 frame_num++;
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 CHECK(size == 0 && trace[0] == pc);
122 size = 1;
123 uptr *frame = (uptr*)bp;
124 uptr *prev_frame = frame;
125 while (frame >= prev_frame &&
126 frame < (uptr*)stack_top - 2 &&
127 frame > (uptr*)stack_bottom &&
128 size < max_size) {
129 uptr pc1 = frame[1];
130 if (pc1 != pc) {
131 trace[size++] = pc1;
133 prev_frame = frame;
134 frame = (uptr*)frame[0];
138 // On 32-bits we don't compress stack traces.
139 // On 64-bits we compress stack traces: if a given pc differes slightly from
140 // the previous one, we record a 31-bit offset instead of the full pc.
141 SANITIZER_INTERFACE_ATTRIBUTE
142 uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) {
143 #if __WORDSIZE == 32
144 // Don't compress, just copy.
145 uptr res = 0;
146 for (uptr i = 0; i < stack->size && i < size; i++) {
147 compressed[i] = stack->trace[i];
148 res++;
150 if (stack->size < size)
151 compressed[stack->size] = 0;
152 #else // 64 bits, compress.
153 uptr prev_pc = 0;
154 const uptr kMaxOffset = (1ULL << 30) - 1;
155 uptr c_index = 0;
156 uptr res = 0;
157 for (uptr i = 0, n = stack->size; i < n; i++) {
158 uptr pc = stack->trace[i];
159 if (!pc) break;
160 if ((s64)pc < 0) break;
161 // Printf("C pc[%zu] %zx\n", i, pc);
162 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
163 uptr offset = (s64)(pc - prev_pc);
164 offset |= (1U << 31);
165 if (c_index >= size) break;
166 // Printf("C co[%zu] offset %zx\n", i, offset);
167 compressed[c_index++] = offset;
168 } else {
169 uptr hi = pc >> 32;
170 uptr lo = (pc << 32) >> 32;
171 CHECK_EQ((hi & (1 << 31)), 0);
172 if (c_index + 1 >= size) break;
173 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
174 compressed[c_index++] = hi;
175 compressed[c_index++] = lo;
177 res++;
178 prev_pc = pc;
180 if (c_index < size)
181 compressed[c_index] = 0;
182 if (c_index + 1 < size)
183 compressed[c_index + 1] = 0;
184 #endif // __WORDSIZE
186 // debug-only code
187 #if 0
188 StackTrace check_stack;
189 UncompressStack(&check_stack, compressed, size);
190 if (res < check_stack.size) {
191 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
192 check_stack.size, size);
194 // |res| may be greater than check_stack.size, because
195 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
196 CHECK(res >= check_stack.size);
197 CHECK_EQ(0, REAL(memcmp)(check_stack.trace, stack->trace,
198 check_stack.size * sizeof(uptr)));
199 #endif
201 return res;
204 SANITIZER_INTERFACE_ATTRIBUTE
205 void StackTrace::UncompressStack(StackTrace *stack,
206 u32 *compressed, uptr size) {
207 #if __WORDSIZE == 32
208 // Don't uncompress, just copy.
209 stack->size = 0;
210 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
211 if (!compressed[i]) break;
212 stack->size++;
213 stack->trace[i] = compressed[i];
215 #else // 64 bits, uncompress
216 uptr prev_pc = 0;
217 stack->size = 0;
218 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
219 u32 x = compressed[i];
220 uptr pc = 0;
221 if (x & (1U << 31)) {
222 // Printf("U co[%zu] offset: %x\n", i, x);
223 // this is an offset
224 s32 offset = x;
225 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
226 pc = prev_pc + offset;
227 CHECK(pc);
228 } else {
229 // CHECK(i + 1 < size);
230 if (i + 1 >= size) break;
231 uptr hi = x;
232 uptr lo = compressed[i+1];
233 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
234 i++;
235 pc = (hi << 32) | lo;
236 if (!pc) break;
238 // Printf("U pc[%zu] %zx\n", stack->size, pc);
239 stack->trace[stack->size++] = pc;
240 prev_pc = pc;
242 #endif // __WORDSIZE
245 } // namespace __sanitizer