1 //===-- sanitizer_stacktrace.cc -------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
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
);
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
29 static uptr
patch_pc(uptr pc
) {
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
) {
44 Printf(" %s", StripPathPrefix(file
, strip_file_prefix
));
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);
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())) {
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
75 Printf(" %s\n", StripPathPrefix(buff
.data(), strip_file_prefix
));
78 } else if (symbolize
) {
79 // Use our own (online) symbolizer, if necessary.
80 addr_frames_num
= SymbolizeCode(pc
, addr_frames
.data(),
82 for (uptr j
= 0; j
< addr_frames_num
; j
++) {
83 AddressInfo
&info
= addr_frames
[j
];
84 PrintStackFramePrefix(frame_num
, pc
);
86 Printf(" in %s", info
.function
);
89 PrintSourceLocation(info
.file
, info
.line
, info
.column
,
91 } else if (info
.module
) {
92 PrintModuleAndOffset(info
.module
, info
.module_offset
,
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
);
105 if (proc_maps
.GetObjectNameAndOffset(pc
, &offset
,
106 buff
.data(), buff
.size())) {
107 PrintModuleAndOffset(buff
.data(), offset
, strip_file_prefix
);
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
);
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
&&
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
) {
144 // Don't compress, just copy.
146 for (uptr i
= 0; i
< stack
->size
&& i
< size
; i
++) {
147 compressed
[i
] = stack
->trace
[i
];
150 if (stack
->size
< size
)
151 compressed
[stack
->size
] = 0;
152 #else // 64 bits, compress.
154 const uptr kMaxOffset
= (1ULL << 30) - 1;
157 for (uptr i
= 0, n
= stack
->size
; i
< n
; i
++) {
158 uptr pc
= stack
->trace
[i
];
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
;
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
;
181 compressed
[c_index
] = 0;
182 if (c_index
+ 1 < size
)
183 compressed
[c_index
+ 1] = 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
)));
204 SANITIZER_INTERFACE_ATTRIBUTE
205 void StackTrace::UncompressStack(StackTrace
*stack
,
206 u32
*compressed
, uptr size
) {
208 // Don't uncompress, just copy.
210 for (uptr i
= 0; i
< size
&& i
< kStackTraceMax
; i
++) {
211 if (!compressed
[i
]) break;
213 stack
->trace
[i
] = compressed
[i
];
215 #else // 64 bits, uncompress
218 for (uptr i
= 0; i
< size
&& stack
->size
< kStackTraceMax
; i
++) {
219 u32 x
= compressed
[i
];
221 if (x
& (1U << 31)) {
222 // Printf("U co[%zu] offset: %x\n", i, x);
225 offset
= (offset
<< 1) >> 1; // remove the 31-byte and sign-extend.
226 pc
= prev_pc
+ offset
;
229 // CHECK(i + 1 < size);
230 if (i
+ 1 >= size
) break;
232 uptr lo
= compressed
[i
+1];
233 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
235 pc
= (hi
<< 32) | lo
;
238 // Printf("U pc[%zu] %zx\n", stack->size, pc);
239 stack
->trace
[stack
->size
++] = pc
;
245 } // namespace __sanitizer