blob: ccfb2d775372a03060b75ee7539161bba39293b7 [file] [log] [blame]
Alexey Samsonove13f7752013-11-07 06:33:06 +00001//===-- sanitizer_stacktrace_libcdep.cc -----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12//===----------------------------------------------------------------------===//
13
Alexey Samsonovca7a2132013-12-25 09:29:54 +000014#include "sanitizer_common.h"
Alexey Samsonove13f7752013-11-07 06:33:06 +000015#include "sanitizer_stacktrace.h"
Alexey Samsonovca7a2132013-12-25 09:29:54 +000016#include "sanitizer_symbolizer.h"
Alexey Samsonove13f7752013-11-07 06:33:06 +000017
18namespace __sanitizer {
19
Alexey Samsonovca7a2132013-12-25 09:29:54 +000020static void PrintStackFramePrefix(InternalScopedString *buffer, uptr frame_num,
21 uptr pc) {
22 buffer->append(" #%zu 0x%zx", frame_num, pc);
23}
24
Alexey Samsonov9c859272014-10-26 03:35:14 +000025void StackTrace::Print() const {
26 if (trace == nullptr || size == 0) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000027 Printf(" <empty stack>\n\n");
28 return;
29 }
Alexey Samsonovca7a2132013-12-25 09:29:54 +000030 InternalScopedBuffer<AddressInfo> addr_frames(64);
31 InternalScopedString frame_desc(GetPageSizeCached() * 2);
32 uptr frame_num = 0;
Alexey Samsonov9c859272014-10-26 03:35:14 +000033 for (uptr i = 0; i < size && trace[i]; i++) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000034 // PCs in stack traces are actually the return addresses, that is,
35 // addresses of the next instructions after the call.
Alexey Samsonov9c859272014-10-26 03:35:14 +000036 uptr pc = GetPreviousInstructionPc(trace[i]);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000037 uptr addr_frames_num = Symbolizer::GetOrInit()->SymbolizePC(
38 pc, addr_frames.data(), addr_frames.size());
Sergey Matveev2be4a282014-05-23 16:04:41 +000039 if (addr_frames_num == 0) {
40 frame_desc.clear();
Alexey Samsonov3eb69f52014-10-20 23:58:08 +000041 PrintStackFramePrefix(&frame_desc, frame_num++, pc);
Sergey Matveev2be4a282014-05-23 16:04:41 +000042 frame_desc.append(" (<unknown module>)");
43 Printf("%s\n", frame_desc.data());
Sergey Matveev2be4a282014-05-23 16:04:41 +000044 continue;
45 }
Alexey Samsonovca7a2132013-12-25 09:29:54 +000046 for (uptr j = 0; j < addr_frames_num; j++) {
47 AddressInfo &info = addr_frames[j];
48 frame_desc.clear();
Alexey Samsonov3eb69f52014-10-20 23:58:08 +000049 PrintStackFramePrefix(&frame_desc, frame_num++, pc);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000050 if (info.function) {
51 frame_desc.append(" in %s", info.function);
52 // Print offset in function if we don't know the source file.
53 if (!info.file && info.function_offset != AddressInfo::kUnknown)
54 frame_desc.append("+0x%zx", info.function_offset);
55 }
56 if (info.file) {
57 frame_desc.append(" ");
58 PrintSourceLocation(&frame_desc, info.file, info.line, info.column);
59 } else if (info.module) {
60 frame_desc.append(" ");
61 PrintModuleAndOffset(&frame_desc, info.module, info.module_offset);
62 }
63 Printf("%s\n", frame_desc.data());
Alexey Samsonovca7a2132013-12-25 09:29:54 +000064 info.Clear();
65 }
66 }
67 // Always print a trailing empty line after stack trace.
68 Printf("\n");
69}
70
Alexey Samsonov9c859272014-10-26 03:35:14 +000071void BufferedStackTrace::Unwind(uptr max_depth, uptr pc, uptr bp, void *context,
72 uptr stack_top, uptr stack_bottom,
73 bool request_fast_unwind) {
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000074 top_frame_bp = (max_depth > 0) ? bp : 0;
75 // Avoid doing any work for small max_depth.
76 if (max_depth == 0) {
77 size = 0;
78 return;
79 }
80 if (max_depth == 1) {
81 size = 1;
Alexey Samsonov9c859272014-10-26 03:35:14 +000082 trace_buffer[0] = pc;
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000083 return;
84 }
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000085 if (!WillUseFastUnwind(request_fast_unwind)) {
86 if (context)
87 SlowUnwindStackWithContext(pc, context, max_depth);
88 else
89 SlowUnwindStack(pc, max_depth);
Evgeniy Stepanov2629e572014-02-11 13:45:01 +000090 } else {
Alexey Samsonove13f7752013-11-07 06:33:06 +000091 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000092 }
Alexey Samsonove13f7752013-11-07 06:33:06 +000093}
94
95} // namespace __sanitizer