xref: /aosp_15_r20/external/cronet/base/profiler/suspendable_thread_delegate_win.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_WIN_H_
6 #define BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_WIN_H_
7 
8 #include <windows.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/base_export.h"
14 #include "base/profiler/sampling_profiler_thread_token.h"
15 #include "base/profiler/suspendable_thread_delegate.h"
16 #include "base/threading/platform_thread.h"
17 #include "base/win/scoped_handle.h"
18 
19 namespace base {
20 
21 // Platform- and thread-specific implementation in support of stack sampling on
22 // Windows.
23 class BASE_EXPORT SuspendableThreadDelegateWin
24     : public SuspendableThreadDelegate {
25  public:
26   class ScopedSuspendThread
27       : public SuspendableThreadDelegate::ScopedSuspendThread {
28    public:
29     explicit ScopedSuspendThread(HANDLE thread_handle);
30 
31     ScopedSuspendThread(const ScopedSuspendThread&) = delete;
32     ScopedSuspendThread& operator=(const ScopedSuspendThread&) = delete;
33 
34     ~ScopedSuspendThread() override;
35 
36     bool WasSuccessful() const override;
37 
38    private:
39     HANDLE thread_handle_;
40     bool was_successful_;
41   };
42 
43   explicit SuspendableThreadDelegateWin(
44       SamplingProfilerThreadToken thread_token);
45   ~SuspendableThreadDelegateWin() override;
46 
47   SuspendableThreadDelegateWin(const SuspendableThreadDelegateWin&) = delete;
48   SuspendableThreadDelegateWin& operator=(const SuspendableThreadDelegateWin&) =
49       delete;
50 
51   // SuspendableThreadDelegate
52   std::unique_ptr<SuspendableThreadDelegate::ScopedSuspendThread>
53   CreateScopedSuspendThread() override;
54   bool GetThreadContext(CONTEXT* thread_context) override;
55   PlatformThreadId GetThreadId() const override;
56   uintptr_t GetStackBaseAddress() const override;
57   bool CanCopyStack(uintptr_t stack_pointer) override;
58   std::vector<uintptr_t*> GetRegistersToRewrite(
59       CONTEXT* thread_context) override;
60 
61  private:
62   const PlatformThreadId thread_id_;
63   win::ScopedHandle thread_handle_;
64   const uintptr_t thread_stack_base_address_;
65 };
66 
67 }  // namespace base
68 
69 #endif  // BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_WIN_H_
70