1 //===-- memprof_stack.h ----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of MemProfiler, a memory profiler. 10 // 11 // MemProf-private header for memprof_stack.cpp. 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MEMPROF_STACK_H 15 #define MEMPROF_STACK_H 16 17 #include "memprof_flags.h" 18 #include "memprof_thread.h" 19 #include "sanitizer_common/sanitizer_flags.h" 20 #include "sanitizer_common/sanitizer_stacktrace.h" 21 22 namespace __memprof { 23 24 static const u32 kDefaultMallocContextSize = 30; 25 26 void SetMallocContextSize(u32 size); 27 u32 GetMallocContextSize(); 28 29 } // namespace __memprof 30 31 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors 32 // as early as possible (in functions exposed to the user), as we generally 33 // don't want stack trace to contain functions from MemProf internals. 34 35 #define GET_STACK_TRACE(max_size, fast) \ 36 BufferedStackTrace stack; \ 37 if (max_size <= 2) { \ 38 stack.size = max_size; \ 39 if (max_size > 0) { \ 40 stack.top_frame_bp = GET_CURRENT_FRAME(); \ 41 stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \ 42 if (max_size > 1) \ 43 stack.trace_buffer[1] = GET_CALLER_PC(); \ 44 } \ 45 } else { \ 46 stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, \ 47 fast, max_size); \ 48 } 49 50 #define GET_STACK_TRACE_FATAL_HERE \ 51 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal) 52 53 #define GET_STACK_TRACE_CHECK_HERE \ 54 GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check) 55 56 #define GET_STACK_TRACE_THREAD GET_STACK_TRACE(kStackTraceMax, true) 57 58 #define GET_STACK_TRACE_MALLOC \ 59 GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc) 60 61 #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC 62 63 #define PRINT_CURRENT_STACK() \ 64 { \ 65 GET_STACK_TRACE_FATAL_HERE; \ 66 stack.Print(); \ 67 } 68 69 #define PRINT_CURRENT_STACK_CHECK() \ 70 { \ 71 GET_STACK_TRACE_CHECK_HERE; \ 72 stack.Print(); \ 73 } 74 75 #endif // MEMPROF_STACK_H 76