1 //===-- sanitizer_stacktrace.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_procmaps.h"
16 #include "sanitizer_stacktrace.h"
17 #include "sanitizer_symbolizer.h"
19 namespace __sanitizer
{
20 static const char *StripPathPrefix(const char *filepath
,
21 const char *strip_file_prefix
) {
22 if (filepath
== internal_strstr(filepath
, strip_file_prefix
))
23 return filepath
+ internal_strlen(strip_file_prefix
);
27 // ----------------------- StackTrace ----------------------------- {{{1
28 uptr
StackTrace::GetPreviousInstructionPc(uptr pc
) {
33 #if defined(__powerpc__) || defined(__powerpc64__)
34 // PCs are always 4 byte aligned.
36 #elif defined(__sparc__)
43 static void PrintStackFramePrefix(uptr frame_num
, uptr pc
) {
44 Printf(" #%zu 0x%zx", frame_num
, pc
);
47 static void PrintSourceLocation(const char *file
, int line
, int column
,
48 const char *strip_file_prefix
) {
50 Printf(" %s", StripPathPrefix(file
, strip_file_prefix
));
54 Printf(":%d", column
);
58 static void PrintModuleAndOffset(const char *module
, uptr offset
,
59 const char *strip_file_prefix
) {
60 Printf(" (%s+0x%zx)", StripPathPrefix(module
, strip_file_prefix
), offset
);
63 void StackTrace::PrintStack(const uptr
*addr
, uptr size
,
64 bool symbolize
, const char *strip_file_prefix
,
65 SymbolizeCallback symbolize_callback
) {
66 MemoryMappingLayout proc_maps
;
67 InternalScopedBuffer
<char> buff(GetPageSizeCached() * 2);
68 InternalScopedBuffer
<AddressInfo
> addr_frames(64);
70 for (uptr i
= 0; i
< size
&& addr
[i
]; i
++) {
71 // PCs in stack traces are actually the return addresses, that is,
72 // addresses of the next instructions after the call.
73 uptr pc
= GetPreviousInstructionPc(addr
[i
]);
74 uptr addr_frames_num
= 0; // The number of stack frames for current
75 // instruction address.
76 if (symbolize_callback
) {
77 if (symbolize_callback((void*)pc
, buff
.data(), buff
.size())) {
79 PrintStackFramePrefix(frame_num
, pc
);
80 // We can't know anything about the string returned by external
81 // symbolizer, but if it starts with filename, try to strip path prefix
83 Printf(" %s\n", StripPathPrefix(buff
.data(), strip_file_prefix
));
87 if (symbolize
&& addr_frames_num
== 0) {
88 // Use our own (online) symbolizer, if necessary.
89 addr_frames_num
= SymbolizeCode(pc
, addr_frames
.data(),
91 for (uptr j
= 0; j
< addr_frames_num
; j
++) {
92 AddressInfo
&info
= addr_frames
[j
];
93 PrintStackFramePrefix(frame_num
, pc
);
95 Printf(" in %s", info
.function
);
98 PrintSourceLocation(info
.file
, info
.line
, info
.column
,
100 } else if (info
.module
) {
101 PrintModuleAndOffset(info
.module
, info
.module_offset
,
109 if (addr_frames_num
== 0) {
110 // If online symbolization failed, try to output at least module and
111 // offset for instruction.
112 PrintStackFramePrefix(frame_num
, pc
);
114 if (proc_maps
.GetObjectNameAndOffset(pc
, &offset
,
115 buff
.data(), buff
.size())) {
116 PrintModuleAndOffset(buff
.data(), offset
, strip_file_prefix
);
124 uptr
StackTrace::GetCurrentPc() {
125 return GET_CALLER_PC();
128 void StackTrace::FastUnwindStack(uptr pc
, uptr bp
,
129 uptr stack_top
, uptr stack_bottom
) {
130 CHECK(size
== 0 && trace
[0] == pc
);
132 uhwptr
*frame
= (uhwptr
*)bp
;
133 uhwptr
*prev_frame
= frame
;
134 while (frame
>= prev_frame
&&
135 frame
< (uhwptr
*)stack_top
- 2 &&
136 frame
> (uhwptr
*)stack_bottom
&&
138 uhwptr pc1
= frame
[1];
140 trace
[size
++] = (uptr
) pc1
;
143 frame
= (uhwptr
*)frame
[0];
147 void StackTrace::PopStackFrames(uptr count
) {
148 CHECK(size
>= count
);
150 for (uptr i
= 0; i
< size
; i
++) {
151 trace
[i
] = trace
[i
+ count
];
155 // On 32-bits we don't compress stack traces.
156 // On 64-bits we compress stack traces: if a given pc differes slightly from
157 // the previous one, we record a 31-bit offset instead of the full pc.
158 SANITIZER_INTERFACE_ATTRIBUTE
159 uptr
StackTrace::CompressStack(StackTrace
*stack
, u32
*compressed
, uptr size
) {
160 #if SANITIZER_WORDSIZE == 32
161 // Don't compress, just copy.
163 for (uptr i
= 0; i
< stack
->size
&& i
< size
; i
++) {
164 compressed
[i
] = stack
->trace
[i
];
167 if (stack
->size
< size
)
168 compressed
[stack
->size
] = 0;
169 #else // 64 bits, compress.
171 const uptr kMaxOffset
= (1ULL << 30) - 1;
174 for (uptr i
= 0, n
= stack
->size
; i
< n
; i
++) {
175 uptr pc
= stack
->trace
[i
];
177 if ((s64
)pc
< 0) break;
178 // Printf("C pc[%zu] %zx\n", i, pc);
179 if (prev_pc
- pc
< kMaxOffset
|| pc
- prev_pc
< kMaxOffset
) {
180 uptr offset
= (s64
)(pc
- prev_pc
);
181 offset
|= (1U << 31);
182 if (c_index
>= size
) break;
183 // Printf("C co[%zu] offset %zx\n", i, offset);
184 compressed
[c_index
++] = offset
;
187 uptr lo
= (pc
<< 32) >> 32;
188 CHECK_EQ((hi
& (1 << 31)), 0);
189 if (c_index
+ 1 >= size
) break;
190 // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
191 compressed
[c_index
++] = hi
;
192 compressed
[c_index
++] = lo
;
198 compressed
[c_index
] = 0;
199 if (c_index
+ 1 < size
)
200 compressed
[c_index
+ 1] = 0;
201 #endif // SANITIZER_WORDSIZE
205 StackTrace check_stack
;
206 UncompressStack(&check_stack
, compressed
, size
);
207 if (res
< check_stack
.size
) {
208 Printf("res %zu check_stack.size %zu; c_size %zu\n", res
,
209 check_stack
.size
, size
);
211 // |res| may be greater than check_stack.size, because
212 // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
213 CHECK(res
>= check_stack
.size
);
214 CHECK_EQ(0, REAL(memcmp
)(check_stack
.trace
, stack
->trace
,
215 check_stack
.size
* sizeof(uptr
)));
221 SANITIZER_INTERFACE_ATTRIBUTE
222 void StackTrace::UncompressStack(StackTrace
*stack
,
223 u32
*compressed
, uptr size
) {
224 #if SANITIZER_WORDSIZE == 32
225 // Don't uncompress, just copy.
227 for (uptr i
= 0; i
< size
&& i
< kStackTraceMax
; i
++) {
228 if (!compressed
[i
]) break;
230 stack
->trace
[i
] = compressed
[i
];
232 #else // 64 bits, uncompress
235 for (uptr i
= 0; i
< size
&& stack
->size
< kStackTraceMax
; i
++) {
236 u32 x
= compressed
[i
];
238 if (x
& (1U << 31)) {
239 // Printf("U co[%zu] offset: %x\n", i, x);
242 offset
= (offset
<< 1) >> 1; // remove the 31-byte and sign-extend.
243 pc
= prev_pc
+ offset
;
246 // CHECK(i + 1 < size);
247 if (i
+ 1 >= size
) break;
249 uptr lo
= compressed
[i
+1];
250 // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
252 pc
= (hi
<< 32) | lo
;
255 // Printf("U pc[%zu] %zx\n", stack->size, pc);
256 stack
->trace
[stack
->size
++] = pc
;
259 #endif // SANITIZER_WORDSIZE
262 } // namespace __sanitizer