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