blob: 13fb01f92e35ce1cd60e74b1b51bb43dbe201804 [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 Samsonov70f38972014-11-05 22:44:36 +000015#include "sanitizer_placement_new.h"
Alexey Samsonove13f7752013-11-07 06:33:06 +000016#include "sanitizer_stacktrace.h"
Alexey Samsonov70f38972014-11-05 22:44:36 +000017#include "sanitizer_stacktrace_printer.h"
Alexey Samsonovca7a2132013-12-25 09:29:54 +000018#include "sanitizer_symbolizer.h"
Alexey Samsonove13f7752013-11-07 06:33:06 +000019
20namespace __sanitizer {
21
Alexey Samsonov9c859272014-10-26 03:35:14 +000022void StackTrace::Print() const {
23 if (trace == nullptr || size == 0) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000024 Printf(" <empty stack>\n\n");
25 return;
26 }
Alexey Samsonov70f38972014-11-05 22:44:36 +000027 const int kMaxAddrFrames = 64;
28 InternalScopedBuffer<AddressInfo> addr_frames(kMaxAddrFrames);
29 for (uptr i = 0; i < kMaxAddrFrames; i++)
30 new(&addr_frames[i]) AddressInfo();
Alexey Samsonovca7a2132013-12-25 09:29:54 +000031 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(
Alexey Samsonov70f38972014-11-05 22:44:36 +000038 pc, addr_frames.data(), kMaxAddrFrames);
Sergey Matveev2be4a282014-05-23 16:04:41 +000039 if (addr_frames_num == 0) {
Alexey Samsonov70f38972014-11-05 22:44:36 +000040 addr_frames[0].address = pc;
41 addr_frames_num = 1;
Sergey Matveev2be4a282014-05-23 16:04:41 +000042 }
Alexey Samsonovca7a2132013-12-25 09:29:54 +000043 for (uptr j = 0; j < addr_frames_num; j++) {
44 AddressInfo &info = addr_frames[j];
45 frame_desc.clear();
Alexey Samsonovfbaaed62014-11-06 18:43:45 +000046 RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
47 info, common_flags()->strip_path_prefix);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000048 Printf("%s\n", frame_desc.data());
Alexey Samsonovca7a2132013-12-25 09:29:54 +000049 info.Clear();
50 }
51 }
52 // Always print a trailing empty line after stack trace.
53 Printf("\n");
54}
55
Alexey Samsonov9c859272014-10-26 03:35:14 +000056void BufferedStackTrace::Unwind(uptr max_depth, uptr pc, uptr bp, void *context,
57 uptr stack_top, uptr stack_bottom,
58 bool request_fast_unwind) {
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000059 top_frame_bp = (max_depth > 0) ? bp : 0;
60 // Avoid doing any work for small max_depth.
61 if (max_depth == 0) {
62 size = 0;
63 return;
64 }
65 if (max_depth == 1) {
66 size = 1;
Alexey Samsonov9c859272014-10-26 03:35:14 +000067 trace_buffer[0] = pc;
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000068 return;
69 }
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000070 if (!WillUseFastUnwind(request_fast_unwind)) {
71 if (context)
72 SlowUnwindStackWithContext(pc, context, max_depth);
73 else
74 SlowUnwindStack(pc, max_depth);
Evgeniy Stepanov2629e572014-02-11 13:45:01 +000075 } else {
Alexey Samsonove13f7752013-11-07 06:33:06 +000076 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000077 }
Alexey Samsonove13f7752013-11-07 06:33:06 +000078}
79
80} // namespace __sanitizer