xref: /aosp_15_r20/external/libchrome/base/profiler/native_stack_sampler.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_PROFILER_NATIVE_STACK_SAMPLER_H_
6 #define BASE_PROFILER_NATIVE_STACK_SAMPLER_H_
7 
8 #include <memory>
9 
10 #include "base/base_export.h"
11 #include "base/macros.h"
12 #include "base/profiler/stack_sampling_profiler.h"
13 #include "base/threading/platform_thread.h"
14 
15 namespace base {
16 
17 class NativeStackSamplerTestDelegate;
18 
19 // NativeStackSampler is an implementation detail of StackSamplingProfiler. It
20 // abstracts the native implementation required to record a set of stack frames
21 // for a given thread.
22 class NativeStackSampler {
23  public:
24   // This class contains a buffer for stack copies that can be shared across
25   // multiple instances of NativeStackSampler.
26   class StackBuffer {
27    public:
28     StackBuffer(size_t buffer_size);
29     ~StackBuffer();
30 
buffer()31     void* buffer() const { return buffer_.get(); }
size()32     size_t size() const { return size_; }
33 
34    private:
35     // The word-aligned buffer.
36     const std::unique_ptr<uintptr_t[]> buffer_;
37 
38     // The size of the buffer.
39     const size_t size_;
40 
41     DISALLOW_COPY_AND_ASSIGN(StackBuffer);
42   };
43 
44   virtual ~NativeStackSampler();
45 
46   // Creates a stack sampler that records samples for thread with |thread_id|.
47   // Returns null if this platform does not support stack sampling.
48   static std::unique_ptr<NativeStackSampler> Create(
49       PlatformThreadId thread_id,
50       NativeStackSamplerTestDelegate* test_delegate);
51 
52   // Gets the required size of the stack buffer.
53   static size_t GetStackBufferSize();
54 
55   // Creates an instance of the a stack buffer that can be used for calls to
56   // any NativeStackSampler object.
57   static std::unique_ptr<StackBuffer> CreateStackBuffer();
58 
59   // The following functions are all called on the SamplingThread (not the
60   // thread being sampled).
61 
62   // Notifies the sampler that we're starting to record a new profile.
63   virtual void ProfileRecordingStarting() = 0;
64 
65   // Records a set of internal frames and returns them.
66   virtual std::vector<StackSamplingProfiler::InternalFrame> RecordStackFrames(
67       StackBuffer* stackbuffer,
68       StackSamplingProfiler::ProfileBuilder* profile_builder) = 0;
69 
70  protected:
71   NativeStackSampler();
72 
73  private:
74   DISALLOW_COPY_AND_ASSIGN(NativeStackSampler);
75 };
76 
77 // NativeStackSamplerTestDelegate provides seams for test code to execute during
78 // stack collection.
79 class BASE_EXPORT NativeStackSamplerTestDelegate {
80  public:
81   virtual ~NativeStackSamplerTestDelegate();
82 
83   // Called after copying the stack and resuming the target thread, but prior to
84   // walking the stack. Invoked on the SamplingThread.
85   virtual void OnPreStackWalk() = 0;
86 
87  protected:
88   NativeStackSamplerTestDelegate();
89 
90  private:
91   DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerTestDelegate);
92 };
93 
94 }  // namespace base
95 
96 #endif  // BASE_PROFILER_NATIVE_STACK_SAMPLER_H_
97 
98