| 6 | #define BASE_PROFILER_UNWINDER_H_ |
| 7 |
| 8 | #include <vector> |
| 9 |
| 10 | #include "base/macros.h" |
| 11 | #include "base/profiler/frame.h" |
| 12 | #include "base/profiler/module_cache.h" |
| 13 | #include "base/profiler/register_context.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | // The result of attempting to unwind stack frames. |
| 18 | enum class UnwindResult { |
| 19 | // The end of the stack was reached successfully. |
| 20 | COMPLETED, |
| 21 |
| 22 | // The walk reached a frame that it doesn't know how to unwind, but might be |
| 23 | // unwindable by the other native/aux unwinder. |
| 24 | UNRECOGNIZED_FRAME, |
| 25 |
| 26 | // The walk was aborted and is not resumable. |
| 27 | ABORTED, |
| 28 | }; |
| 29 |
| 30 | // Unwinder provides an interface for stack frame unwinder implementations for |
| 31 | // use with the StackSamplingProfiler. The profiler is expected to call |
| 32 | // CanUnwind() to determine if the Unwinder thinks it can unwind from the frame |
| 33 | // represented by the context values, then TryUnwind() to attempt the |
| 34 | // unwind. |
| 35 | class Unwinder { |
| 36 | public: |
| 37 | virtual ~Unwinder() = default; |
| 38 |
| 39 | // Invoked to allow the unwinder to add any modules it recognizes to the |
| 40 | // ModuleCache. |
| 41 | virtual void AddInitialModules(ModuleCache* module_cache) {} |
| 42 |
| 43 | // Invoked at the time the stack is captured. IMPORTANT NOTE: this function is |
| 44 | // invoked while the target thread is suspended. To avoid deadlock it must not |
| 45 | // invoke any non-reentrant code that is also invoked by the target thread. In |
| 46 | // particular, it may not perform any heap allocation or deallocation, |
| 47 | // including indirectly via use of DCHECK/CHECK or other logging statements. |
| 48 | virtual void OnStackCapture() {} |
| 49 |
| 50 | // Allows the unwinder to update ModuleCache with any modules it's responsible |
| 51 | // for. Invoked for each sample between OnStackCapture() and the initial |
| 52 | // invocations of CanUnwindFrom()/TryUnwind(). |
| 53 | virtual void UpdateModules(ModuleCache* module_cache) {} |
| 54 |
| 55 | // Returns true if the unwinder recognizes the code referenced by |
| 56 | // |current_frame| as code from which it should be able to unwind. When |
| 57 | // multiple unwinders are in use, each should return true for a disjoint set |
| 58 | // of frames. Note that if the unwinder returns true it may still legitmately |
| 59 | // fail to unwind; e.g. in the case of a native unwind for a function that |
| 60 | // doesn't have unwind information. |
| 61 | virtual bool CanUnwindFrom(const Frame& current_frame) const = 0; |
| 62 |
| 63 | // Attempts to unwind the frame represented by the context values. |
| 64 | // Walks the native frames on the stack pointed to by the stack pointer in |
| 65 | // |thread_context|, appending the frames to |stack|. When invoked |
| 66 | // stack->back() contains the frame corresponding to the state in |
| 67 | // |thread_context|. |
| 68 | // Precondition: RegisterContextStackPointer(thread_context) is less than |
| 69 | // |stack_top|. |
| 70 | // Postcondition: If the implementation returns UNRECOGNIZED_FRAME, indicating |
| 71 | // that it successfully unwound, RegisterContextStackPointer(thread_context) |
| 72 | // is greater than the previous value and less than |stack_top|. |
| 73 | virtual UnwindResult TryUnwind(RegisterContext* thread_context, |
| 74 | uintptr_t stack_top, |
| 75 | ModuleCache* module_cache, |
| 76 | std::vector<Frame>* stack) const = 0; |
| 77 |
| 78 | Unwinder(const Unwinder&) = delete; |
| 79 | Unwinder& operator=(const Unwinder&) = delete; |
| 80 |
| 81 | protected: |
| 82 | Unwinder() = default; |
| 83 | }; |
| 84 |
| 85 | } // namespace base |
| 86 |
| 87 | #endif // BASE_PROFILER_UNWINDER_H_ |
| 88 |