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 #include <memory> 10 #include <vector> 11 12 #include "base/base_export.h" 13 #include "base/profiler/sampling_profiler_thread_token.h" 14 #include "base/profiler/suspendable_thread_delegate.h" 15 #include "base/threading/platform_thread.h" 16 #include "base/win/scoped_handle.h" 17 18 namespace base { 19 20 // Platform- and thread-specific implementation in support of stack sampling on 21 // Windows. 22 class BASE_EXPORT SuspendableThreadDelegateWin 23 : public SuspendableThreadDelegate { 24 public: 25 class ScopedSuspendThread 26 : public SuspendableThreadDelegate::ScopedSuspendThread { 27 public: 28 explicit ScopedSuspendThread(HANDLE thread_handle); 29 30 ScopedSuspendThread(const ScopedSuspendThread&) = delete; 31 ScopedSuspendThread& operator=(const ScopedSuspendThread&) = delete; 32 33 ~ScopedSuspendThread() override; 34 35 bool WasSuccessful() const override; 36 37 private: 38 HANDLE thread_handle_; 39 bool was_successful_; 40 }; 41 42 explicit SuspendableThreadDelegateWin( 43 SamplingProfilerThreadToken thread_token); 44 ~SuspendableThreadDelegateWin() override; 45 46 SuspendableThreadDelegateWin(const SuspendableThreadDelegateWin&) = delete; 47 SuspendableThreadDelegateWin& operator=(const SuspendableThreadDelegateWin&) = 48 delete; 49 50 // SuspendableThreadDelegate 51 std::unique_ptr<SuspendableThreadDelegate::ScopedSuspendThread> 52 CreateScopedSuspendThread() override; 53 bool GetThreadContext(CONTEXT* thread_context) override; 54 PlatformThreadId GetThreadId() const override; 55 uintptr_t GetStackBaseAddress() const override; 56 bool CanCopyStack(uintptr_t stack_pointer) override; 57 std::vector<uintptr_t*> GetRegistersToRewrite( 58 CONTEXT* thread_context) override; 59 60 private: 61 const PlatformThreadId thread_id_; 62 win::ScopedHandle thread_handle_; 63 const uintptr_t thread_stack_base_address_; 64 }; 65 66 } // namespace base 67 68 #endif // BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_WIN_H_ 69