[libsanitizer] merge from upstream r168699
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.cc
blob368d05d8f4aa23ac63ce35a0a4f05c0f39d23918
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 #if defined(__powerpc__) || defined(__powerpc64__)
35 // PCs are always 4 byte aligned.
36 return pc - 4;
37 #else
38 return pc - 1;
39 #endif
42 static void PrintStackFramePrefix(uptr frame_num, uptr pc) {
43 Printf(" #%zu 0x%zx", frame_num, pc);
46 static void PrintSourceLocation(const char *file, int line, int column,
47 const char *strip_file_prefix) {
48 CHECK(file);
49 Printf(" %s", StripPathPrefix(file, strip_file_prefix));
50 if (line > 0) {
51 Printf(":%d", line);
52 if (column > 0)
53 Printf(":%d", column);
57 static void PrintModuleAndOffset(const char *module, uptr offset,
58 const char *strip_file_prefix) {
59 Printf(" (%s+0x%zx)", StripPathPrefix(module, strip_file_prefix), offset);
62 void StackTrace::PrintStack(const uptr *addr, uptr size,
63 bool symbolize, const char *strip_file_prefix,
64 SymbolizeCallback symbolize_callback ) {
65 MemoryMappingLayout proc_maps;
66 InternalScopedBuffer<char> buff(GetPageSizeCached() * 2);
67 InternalScopedBuffer<AddressInfo> addr_frames(64);
68 uptr frame_num = 0;
69 for (uptr i = 0; i < size && addr[i]; i++) {
70 uptr pc = patch_pc(addr[i]);
71 uptr addr_frames_num = 0; // The number of stack frames for current
72 // instruction address.
73 if (symbolize_callback) {
74 if (symbolize_callback((void*)pc, buff.data(), buff.size())) {
75 addr_frames_num = 1;
76 PrintStackFramePrefix(frame_num, pc);
77 // We can't know anything about the string returned by external
78 // symbolizer, but if it starts with filename, try to strip path prefix
79 // from it.
80 Printf(" %s\n", StripPathPrefix(buff.data(), strip_file_prefix));
81 frame_num++;
84 if (symbolize && addr_frames_num == 0) {
85 // Use our own (online) symbolizer, if necessary.
86 addr_frames_num = SymbolizeCode(pc, addr_frames.data(),
87 addr_frames.size());
88 for (uptr j = 0; j < addr_frames_num; j++) {
89 AddressInfo &info = addr_frames[j];
90 PrintStackFramePrefix(frame_num, pc);
91 if (info.function) {
92 Printf(" in %s", info.function);
94 if (info.file) {
95 PrintSourceLocation(info.file, info.line, info.column,
96 strip_file_prefix);
97 } else if (info.module) {
98 PrintModuleAndOffset(info.module, info.module_offset,
99 strip_file_prefix);
101 Printf("\n");
102 info.Clear();
103 frame_num++;
106 if (addr_frames_num == 0) {
107 // If online symbolization failed, try to output at least module and
108 // offset for instruction.
109 PrintStackFramePrefix(frame_num, pc);
110 uptr offset;
111 if (proc_maps.GetObjectNameAndOffset(pc, &offset,
112 buff.data(), buff.size())) {
113 PrintModuleAndOffset(buff.data(), offset, strip_file_prefix);
115 Printf("\n");
116 frame_num++;
121 uptr StackTrace::GetCurrentPc() {
122 return GET_CALLER_PC();
125 void StackTrace::FastUnwindStack(uptr pc, uptr bp,
126 uptr stack_top, uptr stack_bottom) {
127 CHECK(size == 0 && trace[0] == pc);
128 size = 1;
129 uhwptr *frame = (uhwptr *)bp;
130 uhwptr *prev_frame = frame;
131 while (frame >= prev_frame &&
132 frame < (uhwptr *)stack_top - 2 &&
133 frame > (uhwptr *)stack_bottom &&
134 size < max_size) {
135 uhwptr pc1 = frame[1];
136 if (pc1 != pc) {
137 trace[size++] = (uptr) pc1;
139 prev_frame = frame;
140 frame = (uhwptr *)frame[0];
144 void StackTrace::PopStackFrames(uptr count) {
145 CHECK(size >= count);
146 size -= count;
147 for (uptr i = 0; i < size; i++) {
148 trace[i] = trace[i + count];
152 // On 32-bits we don't compress stack traces.
153 // On 64-bits we compress stack traces: if a given pc differes slightly from
154 // the previous one, we record a 31-bit offset instead of the full pc.
155 SANITIZER_INTERFACE_ATTRIBUTE
156 uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) {
157 #if SANITIZER_WORDSIZE == 32
158 // Don't compress, just copy.
159 uptr res = 0;
160 for (uptr i = 0; i < stack->size && i < size; i++) {
161 compressed[i] = stack->trace[i];
162 res++;
164 if (stack->size < size)
165 compressed[stack->size] = 0;
166 #else // 64 bits, compress.
167 uptr prev_pc = 0;
168 const uptr kMaxOffset = (1ULL << 30) - 1;
169 uptr c_index = 0;
170 uptr res = 0;
171 for (uptr i = 0, n = stack->size; i < n; i++) {
172 uptr pc = stack->trace[i];
173 if (!pc) break;
174 if ((s64)pc < 0) break;
175 // Printf("C pc[%zu] %zx\n", i, pc);
176 if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
177 uptr offset = (s64)(pc - prev_pc);
178 offset |= (1U << 31);
179 if (c_index >= size) break;
180 // Printf("C co[%zu] offset %zx\n", i, offset);
181 compressed[c_index++] = offset;
182 } else {
183 uptr hi = pc >> 32;
184 uptr lo = (pc << 32) >> 32;
185 CHECK_EQ((hi & (1 << 31)), 0);
186 if (c_index + 1 >= size) break;
187 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
188 compressed[c_index++] = hi;
189 compressed[c_index++] = lo;
191 res++;
192 prev_pc = pc;
194 if (c_index < size)
195 compressed[c_index] = 0;
196 if (c_index + 1 < size)
197 compressed[c_index + 1] = 0;
198 #endif // SANITIZER_WORDSIZE
200 // debug-only code
201 #if 0
202 StackTrace check_stack;
203 UncompressStack(&check_stack, compressed, size);
204 if (res < check_stack.size) {
205 Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
206 check_stack.size, size);
208 // |res| may be greater than check_stack.size, because
209 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
210 CHECK(res >= check_stack.size);
211 CHECK_EQ(0, REAL(memcmp)(check_stack.trace, stack->trace,
212 check_stack.size * sizeof(uptr)));
213 #endif
215 return res;
218 SANITIZER_INTERFACE_ATTRIBUTE
219 void StackTrace::UncompressStack(StackTrace *stack,
220 u32 *compressed, uptr size) {
221 #if SANITIZER_WORDSIZE == 32
222 // Don't uncompress, just copy.
223 stack->size = 0;
224 for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
225 if (!compressed[i]) break;
226 stack->size++;
227 stack->trace[i] = compressed[i];
229 #else // 64 bits, uncompress
230 uptr prev_pc = 0;
231 stack->size = 0;
232 for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
233 u32 x = compressed[i];
234 uptr pc = 0;
235 if (x & (1U << 31)) {
236 // Printf("U co[%zu] offset: %x\n", i, x);
237 // this is an offset
238 s32 offset = x;
239 offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend.
240 pc = prev_pc + offset;
241 CHECK(pc);
242 } else {
243 // CHECK(i + 1 < size);
244 if (i + 1 >= size) break;
245 uptr hi = x;
246 uptr lo = compressed[i+1];
247 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
248 i++;
249 pc = (hi << 32) | lo;
250 if (!pc) break;
252 // Printf("U pc[%zu] %zx\n", stack->size, pc);
253 stack->trace[stack->size++] = pc;
254 prev_pc = pc;
256 #endif // SANITIZER_WORDSIZE
259 } // namespace __sanitizer