blob: 522fbeb9ac6ed97cce266741933bfb968bee5131 [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);
Kostya Serebryanye31e7782016-05-28 01:25:44 +000028 InternalScopedString dedup_token(GetPageSizeCached());
29 int dedup_frames = common_flags()->dedup_token_length;
Alexey Samsonovca7a2132013-12-25 09:29:54 +000030 uptr frame_num = 0;
Alexey Samsonov9c859272014-10-26 03:35:14 +000031 for (uptr i = 0; i < size && trace[i]; i++) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000032 // PCs in stack traces are actually the return addresses, that is,
33 // addresses of the next instructions after the call.
Alexey Samsonov9c859272014-10-26 03:35:14 +000034 uptr pc = GetPreviousInstructionPc(trace[i]);
Alexey Samsonov0e906682014-12-02 19:48:40 +000035 SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc);
36 CHECK(frames);
37 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
Alexey Samsonovca7a2132013-12-25 09:29:54 +000038 frame_desc.clear();
Alexey Samsonovfbaaed62014-11-06 18:43:45 +000039 RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
Filipe Cabecinhas215046b2015-06-04 01:20:06 +000040 cur->info, common_flags()->symbolize_vs_style,
41 common_flags()->strip_path_prefix);
Alexey Samsonovca7a2132013-12-25 09:29:54 +000042 Printf("%s\n", frame_desc.data());
Kostya Serebryanye31e7782016-05-28 01:25:44 +000043 if (dedup_frames-- > 0) {
44 if (dedup_token.length())
45 dedup_token.append("--");
46 dedup_token.append(cur->info.function);
47 }
Alexey Samsonovca7a2132013-12-25 09:29:54 +000048 }
Alexey Samsonov0e906682014-12-02 19:48:40 +000049 frames->ClearAll();
Alexey Samsonovca7a2132013-12-25 09:29:54 +000050 }
51 // Always print a trailing empty line after stack trace.
52 Printf("\n");
Kostya Serebryanye31e7782016-05-28 01:25:44 +000053 if (dedup_token.length())
54 Printf("DEDUP_TOKEN: %s\n", dedup_token.data());
Alexey Samsonovca7a2132013-12-25 09:29:54 +000055}
56
Evgeniy Stepanovd38af302015-01-22 13:33:16 +000057void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context,
Alexey Samsonov9c859272014-10-26 03:35:14 +000058 uptr stack_top, uptr stack_bottom,
59 bool request_fast_unwind) {
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000060 top_frame_bp = (max_depth > 0) ? bp : 0;
61 // Avoid doing any work for small max_depth.
62 if (max_depth == 0) {
63 size = 0;
64 return;
65 }
66 if (max_depth == 1) {
67 size = 1;
Alexey Samsonov9c859272014-10-26 03:35:14 +000068 trace_buffer[0] = pc;
Alexey Samsonov3e8467b2014-03-04 12:21:28 +000069 return;
70 }
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000071 if (!WillUseFastUnwind(request_fast_unwind)) {
Kuba Brecka3d2ba472015-07-02 13:56:37 +000072#if SANITIZER_CAN_SLOW_UNWIND
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000073 if (context)
74 SlowUnwindStackWithContext(pc, context, max_depth);
75 else
76 SlowUnwindStack(pc, max_depth);
Kuba Brecka3d2ba472015-07-02 13:56:37 +000077#else
78 UNREACHABLE("slow unwind requested but not available");
79#endif
Evgeniy Stepanov2629e572014-02-11 13:45:01 +000080 } else {
Alexey Samsonove13f7752013-11-07 06:33:06 +000081 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
Evgeniy Stepanov769d46f2014-02-11 13:38:57 +000082 }
Alexey Samsonove13f7752013-11-07 06:33:06 +000083}
84
85} // namespace __sanitizer
Kostya Serebryany9aab75f2016-08-25 21:35:29 +000086
87extern "C" {
88SANITIZER_INTERFACE_ATTRIBUTE
89void __sanitizer_symbolize_pc(uptr pc, const char *fmt, char *out_buf,
90 uptr out_buf_size) {
Kostya Serebryanyd77e8c02016-09-09 02:13:27 +000091 if (!out_buf_size) return;
Kostya Serebryany9aab75f2016-08-25 21:35:29 +000092 using namespace __sanitizer;
93 pc = StackTrace::GetPreviousInstructionPc(pc);
94 SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc);
95 if (!frame) {
96 internal_strncpy(out_buf, "<can't symbolize>", out_buf_size);
Kostya Serebryanyd77e8c02016-09-09 02:13:27 +000097 out_buf[out_buf_size - 1] = 0;
Kostya Serebryany9aab75f2016-08-25 21:35:29 +000098 return;
99 }
100 InternalScopedString frame_desc(GetPageSizeCached());
101 RenderFrame(&frame_desc, fmt, 0, frame->info,
102 common_flags()->symbolize_vs_style,
103 common_flags()->strip_path_prefix);
104 internal_strncpy(out_buf, frame_desc.data(), out_buf_size);
Kostya Serebryanyd77e8c02016-09-09 02:13:27 +0000105 out_buf[out_buf_size - 1] = 0;
Kostya Serebryany9aab75f2016-08-25 21:35:29 +0000106}
107} // extern "C"