blob: f66fa79f19a5e68f9cfccf1b87c74a0be0bca343 [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++,
Filipe Cabecinhas215046b2015-06-04 01:20:06 +000038 cur->info, common_flags()->symbolize_vs_style,
39 common_flags()->strip_path_prefix);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000040 Printf("%s\n", frame_desc.data());
Alexey Samsonovca7a2132013-12-25 09:29:54 +000041 }
Alexey Samsonov0e906682014-12-02 19:48:40 +000042 frames->ClearAll();
Alexey Samsonovca7a2132013-12-25 09:29:54 +000043 }
44 // Always print a trailing empty line after stack trace.
45 Printf("\n");
46}
47
Evgeniy Stepanovd38af302015-01-22 13:33:16 +000048void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context,
Alexey Samsonov9c859272014-10-26 03:35:14 +000049 uptr stack_top, uptr stack_bottom,
50 bool request_fast_unwind) {
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000051 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;
Alexey Samsonov9c859272014-10-26 03:35:14 +000059 trace_buffer[0] = pc;
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000060 return;
61 }
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000062 if (!WillUseFastUnwind(request_fast_unwind)) {
Kuba Brecka3d2ba472015-07-02 13:56:37 +000063#if SANITIZER_CAN_SLOW_UNWIND
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000064 if (context)
65 SlowUnwindStackWithContext(pc, context, max_depth);
66 else
67 SlowUnwindStack(pc, max_depth);
Kuba Brecka3d2ba472015-07-02 13:56:37 +000068#else
69 UNREACHABLE("slow unwind requested but not available");
70#endif
Evgeniy Stepanov2629e572014-02-11 13:45:01 +000071 } else {
Alexey Samsonove13f7752013-11-07 06:33:06 +000072 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000073 }
Alexey Samsonove13f7752013-11-07 06:33:06 +000074}
75
76} // namespace __sanitizer