xref: /netbsd-src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_stacktrace.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
11 #ifndef SANITIZER_STACKTRACE_H
12 #define SANITIZER_STACKTRACE_H
13 
14 #include "sanitizer_internal_defs.h"
15 
16 namespace __sanitizer {
17 
18 static const uptr kStackTraceMax = 256;
19 
20 struct StackTrace {
21   typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
22                                      int out_size);
23   uptr size;
24   uptr max_size;
25   uptr trace[kStackTraceMax];
26   static void PrintStack(const uptr *addr, uptr size,
27                          bool symbolize, const char *strip_file_prefix,
28                          SymbolizeCallback symbolize_callback);
29   void CopyTo(uptr *dst, uptr dst_size) {
30     for (uptr i = 0; i < size && i < dst_size; i++)
31       dst[i] = trace[i];
32     for (uptr i = size; i < dst_size; i++)
33       dst[i] = 0;
34   }
35 
36   void CopyFrom(uptr *src, uptr src_size) {
37     size = src_size;
38     if (size > kStackTraceMax) size = kStackTraceMax;
39     for (uptr i = 0; i < size; i++) {
40       trace[i] = src[i];
41     }
42   }
43 
44   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom);
45   void SlowUnwindStack(uptr pc, uptr max_depth);
46 
47   void PopStackFrames(uptr count);
48 
49   static uptr GetCurrentPc();
50   static uptr GetPreviousInstructionPc(uptr pc);
51 
52   static uptr CompressStack(StackTrace *stack,
53                             u32 *compressed, uptr size);
54   static void UncompressStack(StackTrace *stack,
55                               u32 *compressed, uptr size);
56 };
57 
58 
59 const char *StripPathPrefix(const char *filepath,
60                             const char *strip_file_prefix);
61 
62 }  // namespace __sanitizer
63 
64 // Use this macro if you want to print stack trace with the caller
65 // of the current function in the top frame.
66 #define GET_CALLER_PC_BP_SP \
67   uptr bp = GET_CURRENT_FRAME();              \
68   uptr pc = GET_CALLER_PC();                  \
69   uptr local_stack;                           \
70   uptr sp = (uptr)&local_stack
71 
72 // Use this macro if you want to print stack trace with the current
73 // function in the top frame.
74 #define GET_CURRENT_PC_BP_SP \
75   uptr bp = GET_CURRENT_FRAME();              \
76   uptr pc = StackTrace::GetCurrentPc();   \
77   uptr local_stack;                           \
78   uptr sp = (uptr)&local_stack
79 
80 
81 #endif  // SANITIZER_STACKTRACE_H
82