memprof_stack.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef MEMPROF_STACK_H
  14. #define MEMPROF_STACK_H
  15. #include "memprof_flags.h"
  16. #include "memprof_thread.h"
  17. #include "sanitizer_common/sanitizer_flags.h"
  18. #include "sanitizer_common/sanitizer_stacktrace.h"
  19. namespace __memprof {
  20. static const u32 kDefaultMallocContextSize = 30;
  21. void SetMallocContextSize(u32 size);
  22. u32 GetMallocContextSize();
  23. } // namespace __memprof
  24. // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
  25. // as early as possible (in functions exposed to the user), as we generally
  26. // don't want stack trace to contain functions from MemProf internals.
  27. #define GET_STACK_TRACE(max_size, fast) \
  28. BufferedStackTrace stack; \
  29. if (max_size <= 2) { \
  30. stack.size = max_size; \
  31. if (max_size > 0) { \
  32. stack.top_frame_bp = GET_CURRENT_FRAME(); \
  33. stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
  34. if (max_size > 1) \
  35. stack.trace_buffer[1] = GET_CALLER_PC(); \
  36. } \
  37. } else { \
  38. stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, \
  39. fast, max_size); \
  40. }
  41. #define GET_STACK_TRACE_FATAL_HERE \
  42. GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
  43. #define GET_STACK_TRACE_THREAD GET_STACK_TRACE(kStackTraceMax, true)
  44. #define GET_STACK_TRACE_MALLOC \
  45. GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
  46. #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
  47. #define PRINT_CURRENT_STACK() \
  48. { \
  49. GET_STACK_TRACE_FATAL_HERE; \
  50. stack.Print(); \
  51. }
  52. #endif // MEMPROF_STACK_H