xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/memprof/memprof_stack.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===-- memprof_stack.cpp ------------------------------------------------===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric //
9*e8d8bef9SDimitry Andric // This file is a part of MemProfiler, a memory profiler.
10*e8d8bef9SDimitry Andric //
11*e8d8bef9SDimitry Andric // Code for MemProf stack trace.
12*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13*e8d8bef9SDimitry Andric #include "memprof_stack.h"
14*e8d8bef9SDimitry Andric #include "memprof_internal.h"
15*e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_atomic.h"
16*e8d8bef9SDimitry Andric 
17*e8d8bef9SDimitry Andric namespace __memprof {
18*e8d8bef9SDimitry Andric 
19*e8d8bef9SDimitry Andric static atomic_uint32_t malloc_context_size;
20*e8d8bef9SDimitry Andric 
SetMallocContextSize(u32 size)21*e8d8bef9SDimitry Andric void SetMallocContextSize(u32 size) {
22*e8d8bef9SDimitry Andric   atomic_store(&malloc_context_size, size, memory_order_release);
23*e8d8bef9SDimitry Andric }
24*e8d8bef9SDimitry Andric 
GetMallocContextSize()25*e8d8bef9SDimitry Andric u32 GetMallocContextSize() {
26*e8d8bef9SDimitry Andric   return atomic_load(&malloc_context_size, memory_order_acquire);
27*e8d8bef9SDimitry Andric }
28*e8d8bef9SDimitry Andric 
29*e8d8bef9SDimitry Andric } // namespace __memprof
30*e8d8bef9SDimitry Andric 
UnwindImpl(uptr pc,uptr bp,void * context,bool request_fast,u32 max_depth)31*e8d8bef9SDimitry Andric void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
32*e8d8bef9SDimitry Andric                                                  void *context,
33*e8d8bef9SDimitry Andric                                                  bool request_fast,
34*e8d8bef9SDimitry Andric                                                  u32 max_depth) {
35*e8d8bef9SDimitry Andric   using namespace __memprof;
36*e8d8bef9SDimitry Andric   size = 0;
37*e8d8bef9SDimitry Andric   if (UNLIKELY(!memprof_inited))
38*e8d8bef9SDimitry Andric     return;
39*e8d8bef9SDimitry Andric   request_fast = StackTrace::WillUseFastUnwind(request_fast);
40*e8d8bef9SDimitry Andric   MemprofThread *t = GetCurrentThread();
41*e8d8bef9SDimitry Andric   if (request_fast) {
42*e8d8bef9SDimitry Andric     if (t) {
43*e8d8bef9SDimitry Andric       Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(),
44*e8d8bef9SDimitry Andric              true);
45*e8d8bef9SDimitry Andric     }
46*e8d8bef9SDimitry Andric     return;
47*e8d8bef9SDimitry Andric   }
48*e8d8bef9SDimitry Andric   Unwind(max_depth, pc, bp, context, 0, 0, false);
49*e8d8bef9SDimitry Andric }
50*e8d8bef9SDimitry Andric 
51*e8d8bef9SDimitry Andric // ------------------ Interface -------------- {{{1
52*e8d8bef9SDimitry Andric 
53*e8d8bef9SDimitry Andric extern "C" {
54*e8d8bef9SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_print_stack_trace()55*e8d8bef9SDimitry Andric void __sanitizer_print_stack_trace() {
56*e8d8bef9SDimitry Andric   using namespace __memprof;
57*e8d8bef9SDimitry Andric   PRINT_CURRENT_STACK();
58*e8d8bef9SDimitry Andric }
59*e8d8bef9SDimitry Andric } // extern "C"
60