blob: 0d90980e6a68855abf867f264c6163694cbb9d67 [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 Samsonovca7a2132013-12-25 09:29:54 +000027 InternalScopedString frame_desc(GetPageSizeCached() * 2);
28 uptr frame_num = 0;
Alexey Samsonov9c859272014-10-26 03:35:14 +000029 for (uptr i = 0; i < size && trace[i]; i++) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000030 // PCs in stack traces are actually the return addresses, that is,
31 // addresses of the next instructions after the call.
Alexey Samsonov9c859272014-10-26 03:35:14 +000032 uptr pc = GetPreviousInstructionPc(trace[i]);
Alexey Samsonov0e906682014-12-02 19:48:40 +000033 SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc);
34 CHECK(frames);
35 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000036 frame_desc.clear();
Alexey Samsonovfbaaed62014-11-06 18:43:45 +000037 RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
Alexey Samsonov0e906682014-12-02 19:48:40 +000038 cur->info, common_flags()->strip_path_prefix);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000039 Printf("%s\n", frame_desc.data());
Alexey Samsonovca7a2132013-12-25 09:29:54 +000040 }
Alexey Samsonov0e906682014-12-02 19:48:40 +000041 frames->ClearAll();
Alexey Samsonovca7a2132013-12-25 09:29:54 +000042 }
43 // Always print a trailing empty line after stack trace.
44 Printf("\n");
45}
46
Alexey Samsonov9c859272014-10-26 03:35:14 +000047void BufferedStackTrace::Unwind(uptr max_depth, uptr pc, uptr bp, void *context,
48 uptr stack_top, uptr stack_bottom,
49 bool request_fast_unwind) {
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000050 top_frame_bp = (max_depth > 0) ? bp : 0;
51 // Avoid doing any work for small max_depth.
52 if (max_depth == 0) {
53 size = 0;
54 return;
55 }
56 if (max_depth == 1) {
57 size = 1;
Alexey Samsonov9c859272014-10-26 03:35:14 +000058 trace_buffer[0] = pc;
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000059 return;
60 }
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000061 if (!WillUseFastUnwind(request_fast_unwind)) {
62 if (context)
63 SlowUnwindStackWithContext(pc, context, max_depth);
64 else
65 SlowUnwindStack(pc, max_depth);
Evgeniy Stepanov2629e572014-02-11 13:45:01 +000066 } else {
Alexey Samsonove13f7752013-11-07 06:33:06 +000067 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000068 }
Alexey Samsonove13f7752013-11-07 06:33:06 +000069}
70
71} // namespace __sanitizer