stack_sampler.h 2.8 KB

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