stack_sampler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef BASE_PROFILER_STACK_SAMPLER_H_
  5. #define BASE_PROFILER_STACK_SAMPLER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/base_export.h"
  9. #include "base/callback.h"
  10. #include "base/profiler/sampling_profiler_thread_token.h"
  11. #include "base/threading/platform_thread.h"
  12. namespace base {
  13. class Unwinder;
  14. class ModuleCache;
  15. class ProfileBuilder;
  16. class StackBuffer;
  17. class StackSamplerTestDelegate;
  18. // StackSampler is an implementation detail of StackSamplingProfiler. It
  19. // abstracts the native implementation required to record a set of stack frames
  20. // for a given thread.
  21. class BASE_EXPORT StackSampler {
  22. public:
  23. // Factory for generating a set of Unwinders for use by the profiler.
  24. using UnwindersFactory =
  25. OnceCallback<std::vector<std::unique_ptr<Unwinder>>()>;
  26. StackSampler(const StackSampler&) = delete;
  27. StackSampler& operator=(const StackSampler&) = delete;
  28. virtual ~StackSampler();
  29. // Creates a stack sampler that records samples for thread with
  30. // |thread_token|. Unwinders in |unwinders| must be stored in increasing
  31. // priority to guide unwind attempts. Only the unwinder with the lowest
  32. // priority is allowed to return with UnwindResult::kCompleted. Returns null
  33. // if this platform does not support stack sampling.
  34. static std::unique_ptr<StackSampler> Create(
  35. SamplingProfilerThreadToken thread_token,
  36. ModuleCache* module_cache,
  37. UnwindersFactory core_unwinders_factory,
  38. RepeatingClosure record_sample_callback,
  39. StackSamplerTestDelegate* test_delegate);
  40. // Gets the required size of the stack buffer.
  41. static size_t GetStackBufferSize();
  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. // The following functions are all called on the SamplingThread (not the
  46. // thread being sampled).
  47. // Performs post-construction initialization on the SamplingThread.
  48. virtual void Initialize() {}
  49. // Adds an auxiliary unwinder to handle additional, non-native-code unwind
  50. // scenarios. Unwinders must be inserted in increasing priority, following
  51. // |unwinders| provided in Create(), to guide unwind attempts.
  52. virtual void AddAuxUnwinder(std::unique_ptr<Unwinder> unwinder) = 0;
  53. // Records a set of frames and returns them.
  54. virtual void RecordStackFrames(StackBuffer* stackbuffer,
  55. ProfileBuilder* profile_builder,
  56. PlatformThreadId thread_id) = 0;
  57. protected:
  58. StackSampler();
  59. };
  60. // StackSamplerTestDelegate provides seams for test code to execute during stack
  61. // collection.
  62. class BASE_EXPORT StackSamplerTestDelegate {
  63. public:
  64. StackSamplerTestDelegate(const StackSamplerTestDelegate&) = delete;
  65. StackSamplerTestDelegate& operator=(const StackSamplerTestDelegate&) = delete;
  66. virtual ~StackSamplerTestDelegate();
  67. // Called after copying the stack and resuming the target thread, but prior to
  68. // walking the stack. Invoked on the SamplingThread.
  69. virtual void OnPreStackWalk() = 0;
  70. protected:
  71. StackSamplerTestDelegate();
  72. };
  73. } // namespace base
  74. #endif // BASE_PROFILER_STACK_SAMPLER_H_