blob: b55d4dde083f5471728824d5306889b98a85e68f [file] [log] [blame]
Alexey Samsonove74968c2013-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
Stephen Hines2d1fdb22014-05-28 23:58:16 -070014#include "sanitizer_common.h"
Stephen Hines6d186232014-11-26 17:56:19 -080015#include "sanitizer_placement_new.h"
Alexey Samsonove74968c2013-11-07 06:33:06 +000016#include "sanitizer_stacktrace.h"
Stephen Hines6d186232014-11-26 17:56:19 -080017#include "sanitizer_stacktrace_printer.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018#include "sanitizer_symbolizer.h"
Alexey Samsonove74968c2013-11-07 06:33:06 +000019
20namespace __sanitizer {
21
Stephen Hines6d186232014-11-26 17:56:19 -080022void StackTrace::Print() const {
23 if (trace == nullptr || size == 0) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070024 Printf(" <empty stack>\n\n");
25 return;
26 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070027 InternalScopedString frame_desc(GetPageSizeCached() * 2);
28 uptr frame_num = 0;
Stephen Hines6d186232014-11-26 17:56:19 -080029 for (uptr i = 0; i < size && trace[i]; i++) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070030 // PCs in stack traces are actually the return addresses, that is,
31 // addresses of the next instructions after the call.
Stephen Hines6d186232014-11-26 17:56:19 -080032 uptr pc = GetPreviousInstructionPc(trace[i]);
Stephen Hines86277eb2015-03-23 12:06:32 -070033 SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc);
34 CHECK(frames);
35 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070036 frame_desc.clear();
Stephen Hines6d186232014-11-26 17:56:19 -080037 RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070038 cur->info, common_flags()->symbolize_vs_style,
39 common_flags()->strip_path_prefix);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070040 Printf("%s\n", frame_desc.data());
Stephen Hines2d1fdb22014-05-28 23:58:16 -070041 }
Stephen Hines86277eb2015-03-23 12:06:32 -070042 frames->ClearAll();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070043 }
44 // Always print a trailing empty line after stack trace.
45 Printf("\n");
46}
47
Stephen Hines86277eb2015-03-23 12:06:32 -070048void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context,
Stephen Hines6d186232014-11-26 17:56:19 -080049 uptr stack_top, uptr stack_bottom,
50 bool request_fast_unwind) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070051 top_frame_bp = (max_depth > 0) ? bp : 0;
52 // Avoid doing any work for small max_depth.
53 if (max_depth == 0) {
54 size = 0;
55 return;
56 }
57 if (max_depth == 1) {
58 size = 1;
Stephen Hines6d186232014-11-26 17:56:19 -080059 trace_buffer[0] = pc;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070060 return;
61 }
62 if (!WillUseFastUnwind(request_fast_unwind)) {
63 if (context)
64 SlowUnwindStackWithContext(pc, context, max_depth);
65 else
66 SlowUnwindStack(pc, max_depth);
67 } else {
68 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
69 }
Alexey Samsonove74968c2013-11-07 06:33:06 +000070}
71
72} // namespace __sanitizer