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