blob: 04da09317cb348f9d8881e9c2be0ee9c40115eed [file] [log] [blame]
Zachary Turner02862bc2014-11-07 23:44:13 +00001//===-- DebuggerThread.h ----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turner02862bc2014-11-07 23:44:13 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_Plugins_Process_Windows_DebuggerThread_H_
10#define liblldb_Plugins_Process_Windows_DebuggerThread_H_
11
Adrian McCarthya59a7212015-06-19 18:26:53 +000012#include <atomic>
Zachary Turnerfb6c34942014-12-10 23:25:10 +000013#include <memory>
14
Zachary Turner02862bc2014-11-07 23:44:13 +000015#include "ForwardDecl.h"
16#include "lldb/Host/HostProcess.h"
17#include "lldb/Host/HostThread.h"
18#include "lldb/Host/windows/windows.h"
Raphael Isemann7fae4932018-08-30 17:51:10 +000019#include "lldb/Utility/Predicate.h"
Zachary Turner02862bc2014-11-07 23:44:13 +000020
Kate Stoneb9c1b512016-09-06 20:57:50 +000021namespace lldb_private {
Zachary Turner02862bc2014-11-07 23:44:13 +000022
23//----------------------------------------------------------------------
24// DebuggerThread
25//
26// Debugs a single process, notifying listeners as appropriate when interesting
27// things occur.
28//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000029class DebuggerThread : public std::enable_shared_from_this<DebuggerThread> {
30public:
31 DebuggerThread(DebugDelegateSP debug_delegate);
32 virtual ~DebuggerThread();
Zachary Turner02862bc2014-11-07 23:44:13 +000033
Zachary Turner97206d52017-05-12 04:51:55 +000034 Status DebugLaunch(const ProcessLaunchInfo &launch_info);
35 Status DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info);
Zachary Turner3985f892014-11-10 22:32:18 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 HostProcess GetProcess() const { return m_process; }
38 HostThread GetMainThread() const { return m_main_thread; }
39 std::weak_ptr<ExceptionRecord> GetActiveException() {
40 return m_active_exception;
41 }
Zachary Turnerc6a66532014-12-03 22:04:18 +000042
Zachary Turner97206d52017-05-12 04:51:55 +000043 Status StopDebugging(bool terminate);
Zachary Turner02862bc2014-11-07 23:44:13 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 void ContinueAsyncException(ExceptionResult result);
Zachary Turnerdcd80372014-11-11 00:00:14 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047private:
48 void FreeProcessHandles();
49 void DebugLoop();
50 ExceptionResult HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
51 DWORD thread_id);
52 DWORD HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info,
53 DWORD thread_id);
54 DWORD HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info,
55 DWORD thread_id);
56 DWORD HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info,
57 DWORD thread_id);
58 DWORD HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info,
59 DWORD thread_id);
60 DWORD HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id);
61 DWORD HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info,
62 DWORD thread_id);
63 DWORD HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id);
64 DWORD HandleRipEvent(const RIP_INFO &info, DWORD thread_id);
Zachary Turner02862bc2014-11-07 23:44:13 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 DebugDelegateSP m_debug_delegate;
Zachary Turner02862bc2014-11-07 23:44:13 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 HostProcess m_process; // The process being debugged.
69 HostThread m_main_thread; // The main thread of the inferior.
Zachary Turner02862bc2014-11-07 23:44:13 +000070
Zachary Turner5a8ad4592016-10-05 17:07:34 +000071 // The image file of the process being debugged.
72 HANDLE m_image_file = nullptr;
Zachary Turnerc6a66532014-12-03 22:04:18 +000073
Zachary Turner5a8ad4592016-10-05 17:07:34 +000074 // The current exception waiting to be handled
75 ExceptionRecordSP m_active_exception;
Zachary Turnerdcd80372014-11-11 00:00:14 +000076
Zachary Turner5a8ad4592016-10-05 17:07:34 +000077 // A predicate which gets signalled when an exception is finished processing
78 // and the debug loop can be continued.
79 Predicate<ExceptionResult> m_exception_pred;
Zachary Turner3c1c5b92015-05-21 19:56:26 +000080
Zachary Turner5a8ad4592016-10-05 17:07:34 +000081 // An event which gets signalled by the debugger thread when it exits the
82 // debugger loop and is detached from the inferior.
83 HANDLE m_debugging_ended_event = nullptr;
84
85 // Signals the loop to detach from the process (specified by pid).
86 std::atomic<DWORD> m_pid_to_detach;
87
88 // Signals the debug loop to stop processing certain types of events that
89 // block shutdown.
90 std::atomic<bool> m_is_shutting_down;
91
92 // Indicates we've detached from the inferior process and the debug loop can
93 // exit.
94 bool m_detached = false;
Adrian McCarthya59a7212015-06-19 18:26:53 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 static lldb::thread_result_t DebuggerThreadLaunchRoutine(void *data);
97 lldb::thread_result_t
98 DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info);
99 static lldb::thread_result_t DebuggerThreadAttachRoutine(void *data);
100 lldb::thread_result_t
101 DebuggerThreadAttachRoutine(lldb::pid_t pid,
102 const ProcessAttachInfo &launch_info);
Zachary Turner02862bc2014-11-07 23:44:13 +0000103};
104}
Zachary Turner02862bc2014-11-07 23:44:13 +0000105#endif