2014-09-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / libsanitizer / asan / asan_stack.h
blobd31c0afa83bcfd5dcdec9c9a06173baeddd76550
1 //===-- asan_stack.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 a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan-private header for asan_stack.cc.
11 //===----------------------------------------------------------------------===//
12 #ifndef ASAN_STACK_H
13 #define ASAN_STACK_H
15 #include "asan_flags.h"
16 #include "asan_thread.h"
17 #include "sanitizer_common/sanitizer_flags.h"
18 #include "sanitizer_common/sanitizer_stacktrace.h"
20 namespace __asan {
22 // Get the stack trace with the given pc and bp.
23 // The pc will be in the position 0 of the resulting stack trace.
24 // The bp may refer to the current frame or to the caller's frame.
25 ALWAYS_INLINE
26 void GetStackTraceWithPcBpAndContext(StackTrace *stack, uptr max_depth, uptr pc,
27 uptr bp, void *context, bool fast) {
28 #if SANITIZER_WINDOWS
29 stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
30 #else
31 AsanThread *t;
32 stack->size = 0;
33 if (LIKELY(asan_inited)) {
34 if ((t = GetCurrentThread()) && !t->isUnwinding()) {
35 uptr stack_top = t->stack_top();
36 uptr stack_bottom = t->stack_bottom();
37 ScopedUnwinding unwind_scope(t);
38 stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
39 } else if (t == 0 && !fast) {
40 /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
41 stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
44 #endif // SANITIZER_WINDOWS
47 } // namespace __asan
49 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
50 // as early as possible (in functions exposed to the user), as we generally
51 // don't want stack trace to contain functions from ASan internals.
53 #define GET_STACK_TRACE(max_size, fast) \
54 StackTrace stack; \
55 if (max_size <= 2) { \
56 stack.size = max_size; \
57 if (max_size > 0) { \
58 stack.top_frame_bp = GET_CURRENT_FRAME(); \
59 stack.trace[0] = StackTrace::GetCurrentPc(); \
60 if (max_size > 1) \
61 stack.trace[1] = GET_CALLER_PC(); \
62 } \
63 } else { \
64 GetStackTraceWithPcBpAndContext(&stack, max_size, \
65 StackTrace::GetCurrentPc(), \
66 GET_CURRENT_FRAME(), 0, fast); \
69 #define GET_STACK_TRACE_FATAL(pc, bp) \
70 StackTrace stack; \
71 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
72 common_flags()->fast_unwind_on_fatal)
74 #define GET_STACK_TRACE_SIGNAL(pc, bp, context) \
75 StackTrace stack; \
76 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context, \
77 common_flags()->fast_unwind_on_fatal)
79 #define GET_STACK_TRACE_FATAL_HERE \
80 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
82 #define GET_STACK_TRACE_THREAD \
83 GET_STACK_TRACE(kStackTraceMax, true)
85 #define GET_STACK_TRACE_MALLOC \
86 GET_STACK_TRACE(common_flags()->malloc_context_size, \
87 common_flags()->fast_unwind_on_malloc)
89 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
91 #define PRINT_CURRENT_STACK() \
92 { \
93 GET_STACK_TRACE_FATAL_HERE; \
94 stack.Print(); \
97 #endif // ASAN_STACK_H