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_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
) {
27 #if defined(__sparc__)
34 static void PrintStackFramePrefix(InternalScopedString
*buffer
, uptr frame_num
,
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");
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);
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())) {
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
66 StripPathPrefix(buff
.data(), common_flags()->strip_path_prefix
));
67 Printf("%s\n", frame_desc
.data());
71 if (common_flags()->symbolize
&& addr_frames_num
== 0) {
72 // Use our own (online) symbolizer, if necessary.
73 if (Symbolizer
*sym
= Symbolizer::GetOrNull())
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
];
79 PrintStackFramePrefix(&frame_desc
, frame_num
, pc
);
81 frame_desc
.append(" in %s", info
.function
);
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());
95 if (addr_frames_num
== 0) {
96 // If online symbolization failed, try to output at least module and
97 // offset for instruction.
99 PrintStackFramePrefix(&frame_desc
, frame_num
, pc
);
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());
111 // Always print a trailing empty line after stack trace.
115 uptr
StackTrace::GetCurrentPc() {
116 return GET_CALLER_PC();
119 void StackTrace::FastUnwindStack(uptr pc
, uptr bp
,
120 uptr stack_top
, uptr stack_bottom
,
122 if (max_depth
== 0) {
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
)) &&
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
);
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
))
169 } // namespace __sanitizer