1 //===-- ProcessDebugger.h ---------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_ProcessDebugger_h_
10 #define liblldb_ProcessDebugger_h_
11 
12 #include "lldb/Host/windows/windows.h"
13 
14 #include "lldb/Utility/Status.h"
15 #include "lldb/lldb-forward.h"
16 #include "lldb/lldb-types.h"
17 #include "llvm/Support/Mutex.h"
18 
19 #include "ForwardDecl.h"
20 #include <map>
21 #include <set>
22 
23 namespace lldb_private {
24 
25 class HostProcess;
26 class HostThread;
27 class ProcessLaunchInfo;
28 class ProcessAttachInfo;
29 
30 class ProcessWindowsData {
31 public:
32   ProcessWindowsData(bool stop_at_entry) : m_stop_at_entry(stop_at_entry) {
33     m_initial_stop_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
34   }
35 
36   ~ProcessWindowsData() { ::CloseHandle(m_initial_stop_event); }
37 
38   Status m_launch_error;
39   DebuggerThreadSP m_debugger;
40   // StopInfoSP m_pending_stop_info;
41   HANDLE m_initial_stop_event = nullptr;
42   bool m_initial_stop_received = false;
43   bool m_stop_at_entry;
44   std::map<lldb::tid_t, HostThread> m_new_threads;
45   std::set<lldb::tid_t> m_exited_threads;
46 };
47 
48 class ProcessDebugger {
49 
50 public:
51   virtual void OnExitProcess(uint32_t exit_code);
52   virtual void OnDebuggerConnected(lldb::addr_t image_base);
53   virtual ExceptionResult OnDebugException(bool first_chance,
54                                            const ExceptionRecord &record);
55   virtual void OnCreateThread(const HostThread &thread);
56   virtual void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code);
57   virtual void OnLoadDll(const ModuleSpec &module_spec,
58                          lldb::addr_t module_addr);
59   virtual void OnUnloadDll(lldb::addr_t module_addr);
60   virtual void OnDebugString(const std::string &string);
61   virtual void OnDebuggerError(const Status &error, uint32_t type);
62 
63 protected:
64   Status DetachProcess();
65 
66   Status LaunchProcess(ProcessLaunchInfo &launch_info,
67                        DebugDelegateSP delegate);
68 
69   Status AttachProcess(lldb::pid_t pid, const ProcessAttachInfo &attach_info,
70                        DebugDelegateSP delegate);
71 
72   Status DestroyProcess(lldb::StateType process_state);
73 
74   Status HaltProcess(bool &caused_stop);
75 
76   Status GetMemoryRegionInfo(lldb::addr_t load_addr,
77                              MemoryRegionInfo &range_info);
78 
79   Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
80                     size_t &bytes_read);
81 
82   Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
83                      size_t &bytes_written);
84 
85   Status AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr);
86 
87   Status DeallocateMemory(lldb::addr_t addr);
88 
89   lldb::pid_t GetDebuggedProcessId() const;
90 
91   Status WaitForDebuggerConnection(DebuggerThreadSP debugger,
92                                    HostProcess &process);
93 
94 protected:
95   llvm::sys::Mutex m_mutex;
96   std::unique_ptr<ProcessWindowsData> m_session_data;
97 };
98 
99 } // namespace lldb_private
100 
101 #endif // #ifndef liblldb_ProcessDebugger_h_
102