| 6 | #define BASE_PROFILER_STACK_SAMPLER_H_ |
| 7 |
| 8 | #include <memory> |
| 9 |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "base/profiler/sampling_profiler_thread_token.h" |
| 13 | #include "base/threading/platform_thread.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | class Unwinder; |
| 18 | class ModuleCache; |
| 19 | class ProfileBuilder; |
| 20 | class StackBuffer; |
| 21 | class StackSamplerTestDelegate; |
| 22 |
| 23 | // StackSampler is an implementation detail of StackSamplingProfiler. It |
| 24 | // abstracts the native implementation required to record a set of stack frames |
| 25 | // for a given thread. |
| 26 | class BASE_EXPORT StackSampler { |
| 27 | public: |
| 28 | virtual ~StackSampler(); |
| 29 |
| 30 | // Creates a stack sampler that records samples for thread with |
| 31 | // |thread_token|. Returns null if this platform does not support stack |
| 32 | // sampling. |
| 33 | static std::unique_ptr<StackSampler> Create( |
| 34 | SamplingProfilerThreadToken thread_token, |
| 35 | ModuleCache* module_cache, |
| 36 | StackSamplerTestDelegate* test_delegate, |
| 37 | std::unique_ptr<Unwinder> native_unwinder = nullptr); |
| 38 |
| 39 | // Gets the required size of the stack buffer. |
| 40 | static size_t GetStackBufferSize(); |
| 41 |
| 42 | // Creates an instance of the a stack buffer that can be used for calls to |
| 43 | // any StackSampler object. |
| 44 | static std::unique_ptr<StackBuffer> CreateStackBuffer(); |
| 45 |
| 46 | // The following functions are all called on the SamplingThread (not the |
| 47 | // thread being sampled). |
| 48 |
| 49 | // Adds an auxiliary unwinder to handle additional, non-native-code unwind |
| 50 | // scenarios. |
| 51 | virtual void AddAuxUnwinder(std::unique_ptr<Unwinder> unwinder) = 0; |
| 52 |
| 53 | // Records a set of frames and returns them. |
| 54 | virtual void RecordStackFrames(StackBuffer* stackbuffer, |
| 55 | ProfileBuilder* profile_builder) = 0; |
| 56 |
| 57 | protected: |
| 58 | StackSampler(); |
| 59 |
| 60 | private: |
| 61 | DISALLOW_COPY_AND_ASSIGN(StackSampler); |
| 62 | }; |
| 63 |
| 64 | // StackSamplerTestDelegate provides seams for test code to execute during stack |
| 65 | // collection. |
| 66 | class BASE_EXPORT StackSamplerTestDelegate { |
| 67 | public: |
| 68 | virtual ~StackSamplerTestDelegate(); |
| 69 |
| 70 | // Called after copying the stack and resuming the target thread, but prior to |
| 71 | // walking the stack. Invoked on the SamplingThread. |
| 72 | virtual void OnPreStackWalk() = 0; |
| 73 |
| 74 | protected: |
| 75 | StackSamplerTestDelegate(); |
| 76 |
| 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(StackSamplerTestDelegate); |
| 79 | }; |
| 80 |
| 81 | } // namespace base |
| 82 |
| 83 | #endif // BASE_PROFILER_STACK_SAMPLER_H_ |
| 84 |