* runtime/stop.c: Use C11 _Noreturn.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace_libcdep.cc
blobc3c1045ee04acf31006117cf8830c88597d7f537
1 //===-- sanitizer_stacktrace_libcdep.cc -----------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common.h"
13 #include "sanitizer_stacktrace.h"
14 #include "sanitizer_symbolizer.h"
16 namespace __sanitizer {
18 static void PrintStackFramePrefix(InternalScopedString *buffer, uptr frame_num,
19 uptr pc) {
20 buffer->append(" #%zu 0x%zx", frame_num, pc);
23 void StackTrace::PrintStack(const uptr *addr, uptr size) {
24 if (addr == 0 || size == 0) {
25 Printf(" <empty stack>\n\n");
26 return;
28 InternalScopedBuffer<char> buff(GetPageSizeCached() * 2);
29 InternalScopedBuffer<AddressInfo> addr_frames(64);
30 InternalScopedString frame_desc(GetPageSizeCached() * 2);
31 uptr frame_num = 0;
32 for (uptr i = 0; i < size && addr[i]; i++) {
33 // PCs in stack traces are actually the return addresses, that is,
34 // addresses of the next instructions after the call.
35 uptr pc = GetPreviousInstructionPc(addr[i]);
36 uptr addr_frames_num = Symbolizer::GetOrInit()->SymbolizePC(
37 pc, addr_frames.data(), addr_frames.size());
38 for (uptr j = 0; j < addr_frames_num; j++) {
39 AddressInfo &info = addr_frames[j];
40 frame_desc.clear();
41 PrintStackFramePrefix(&frame_desc, frame_num, pc);
42 if (info.function) {
43 frame_desc.append(" in %s", info.function);
44 // Print offset in function if we don't know the source file.
45 if (!info.file && info.function_offset != AddressInfo::kUnknown)
46 frame_desc.append("+0x%zx", info.function_offset);
48 if (info.file) {
49 frame_desc.append(" ");
50 PrintSourceLocation(&frame_desc, info.file, info.line, info.column);
51 } else if (info.module) {
52 frame_desc.append(" ");
53 PrintModuleAndOffset(&frame_desc, info.module, info.module_offset);
55 Printf("%s\n", frame_desc.data());
56 frame_num++;
57 info.Clear();
60 // Always print a trailing empty line after stack trace.
61 Printf("\n");
64 void StackTrace::Unwind(uptr max_depth, uptr pc, uptr bp, void *context,
65 uptr stack_top, uptr stack_bottom,
66 bool request_fast_unwind) {
67 top_frame_bp = (max_depth > 0) ? bp : 0;
68 // Avoid doing any work for small max_depth.
69 if (max_depth == 0) {
70 size = 0;
71 return;
73 if (max_depth == 1) {
74 size = 1;
75 trace[0] = pc;
76 return;
78 if (!WillUseFastUnwind(request_fast_unwind)) {
79 if (context)
80 SlowUnwindStackWithContext(pc, context, max_depth);
81 else
82 SlowUnwindStack(pc, max_depth);
83 } else {
84 FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
88 } // namespace __sanitizer