1 //===-- memprof_stack.cpp ------------------------------------------------===//
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 // Code for MemProf stack trace.
12 //===----------------------------------------------------------------------===//
13 #include "memprof_stack.h"
14 #include "memprof_internal.h"
15 #include "sanitizer_common/sanitizer_atomic.h"
16 
17 namespace __memprof {
18 
19 static atomic_uint32_t malloc_context_size;
20 
21 void SetMallocContextSize(u32 size) {
22   atomic_store(&malloc_context_size, size, memory_order_release);
23 }
24 
25 u32 GetMallocContextSize() {
26   return atomic_load(&malloc_context_size, memory_order_acquire);
27 }
28 
29 } // namespace __memprof
30 
31 void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
32                                                  void *context,
33                                                  bool request_fast,
34                                                  u32 max_depth) {
35   using namespace __memprof;
36   size = 0;
37   if (UNLIKELY(!memprof_inited))
38     return;
39   request_fast = StackTrace::WillUseFastUnwind(request_fast);
40   MemprofThread *t = GetCurrentThread();
41   if (request_fast) {
42     if (t) {
43       Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(),
44              true);
45     }
46     return;
47   }
48   Unwind(max_depth, pc, bp, context, 0, 0, false);
49 }
50 
51 // ------------------ Interface -------------- {{{1
52 
53 extern "C" {
54 SANITIZER_INTERFACE_ATTRIBUTE
55 void __sanitizer_print_stack_trace() {
56   using namespace __memprof;
57   PRINT_CURRENT_STACK();
58 }
59 } // extern "C"
60