unwinder.h 3.5 KB

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