OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client 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 DEBUGGER_CORE_DEBUGGEE_PROCESS_H_ | |
5 #define DEBUGGER_CORE_DEBUGGEE_PROCESS_H_ | |
6 #include <windows.h> | |
7 #include <deque> | |
8 #include <map> | |
9 #include "debugger/core/debug_event.h" | |
10 #include "debugger/core/debuggee_iprocess.h" | |
11 #include "debugger/core/debuggee_thread.h" | |
12 | |
13 /// \brief This namespace groups classes related to OOP (out-of-process) | |
14 /// Windows debugger. | |
15 namespace debug { | |
16 class Breakpoint; | |
17 class DebugAPI; | |
18 | |
19 /// This class represents a process in debugged application. | |
20 | |
21 /// Class diagram (and more) is here: | |
22 /// https://docs.google.com/a/google.com/document/d/1lTN-IYqDd_oy9XQg9-zlNc_vbg-
qyr4q2MKNEjhSA84/edit?hl=en&authkey=CJyJlOgF# | |
23 /// | |
24 /// Note: most methods shall be called from one thread, this is limitation | |
25 /// of Windows debug API, here's links to Microsoft documentation: | |
26 /// http://msdn.microsoft.com/en-us/library/ms681423%28v=VS.85%29.aspx | |
27 /// http://msdn.microsoft.com/en-us/library/ms681675%28v=vs.85%29.aspx | |
28 /// Simple accessors can be called from any thread. | |
29 /// | |
30 /// Note: not thread-safe. | |
31 class DebuggeeProcess : public IDebuggeeProcess { | |
32 public: | |
33 /// Creates a DebuggeeProcess object. There's no need to close | |
34 /// |handle|, system will close handle when thread terminates. | |
35 /// |file_handle| shall be closed. | |
36 /// @param[in] id process id | |
37 /// @param[in] handle a handle to the process | |
38 /// @param[in] file_handle a handle to the process's image file | |
39 /// @param[in] debug_api pointer to DebugAPI object, | |
40 /// system debug API (or mock) is called though 'debug_api'. | |
41 /// DebuggeeProcess don't not takes ownership of |debug_api|. | |
42 /// If |debug_api| is NULL, default DebugAPI is used, | |
43 /// the one that passes calls to system calls. | |
44 DebuggeeProcess(int id, | |
45 HANDLE handle, | |
46 HANDLE file_handle, | |
47 DebugAPI* debug_api); | |
48 virtual ~DebuggeeProcess(); | |
49 | |
50 /// @return id of the process | |
51 virtual int id() const { return id_; } | |
52 | |
53 /// @return handle of the process | |
54 virtual HANDLE handle() const { return handle_; } | |
55 | |
56 /// @return handle of the process image file | |
57 virtual HANDLE file_handle() const { return file_handle_; } | |
58 | |
59 virtual State state() const { return state_; } | |
60 virtual bool IsHalted() const { return kHalted == state(); } | |
61 | |
62 /// Shall be called only on dead processes (i.e. state_ == kDead). | |
63 /// @return exit code or exception number, if process is terminated | |
64 /// by exception. | |
65 virtual int return_code() const { return exit_code_; } | |
66 | |
67 /// @return reference to last received debug event | |
68 virtual const DebugEvent& last_debug_event() const { | |
69 return last_debug_event_; | |
70 } | |
71 | |
72 virtual DebugAPI& debug_api() { return debug_api_; } | |
73 | |
74 /// @return address of memory region where nexe is loaded. | |
75 virtual void* nexe_mem_base() const { return nexe_mem_base_; } | |
76 | |
77 virtual void set_nexe_mem_base(void* addr) { nexe_mem_base_ = addr; } | |
78 | |
79 /// @return code address of nexe _start() routine. | |
80 virtual void* nexe_entry_point() const { return nexe_entry_point_; } | |
81 | |
82 virtual void set_nexe_entry_point(void* addr) { nexe_entry_point_ = addr; } | |
83 | |
84 /// @return word size of the debuggee process (32 or 64). | |
85 virtual int GetWordSizeInBits(); | |
86 | |
87 /// @return true for WoW (windows-on-windows) processes - | |
88 /// i.e. 32-bit processes running on 64-bit windows. | |
89 virtual bool IsWoW(); | |
90 | |
91 /// Allows process execution to continue (i.e. it calls | |
92 /// ContinueDebugEvent() for halted thread). | |
93 /// Shall be called only on halted process, and only from the thread that | |
94 /// started the debuggee. | |
95 virtual bool Continue(); | |
96 | |
97 /// Allows process execution to continue. If thread was halted due | |
98 /// to exception, that exception is passed to the debugee thread. | |
99 /// Shall be called only on halted process, and only from the thread that | |
100 /// started the debuggee. | |
101 virtual bool ContinueAndPassExceptionToDebuggee(); | |
102 | |
103 /// Cause halted thread to execute single CPU instruction. | |
104 /// Shall be called only on halted process, and only from the thread that | |
105 /// started the debuggee. | |
106 /// Does not block waiting for SINGLE_STEP exception. | |
107 /// Other events can arrive before SINGLE_STEP due to activity of | |
108 /// other threads. | |
109 virtual bool SingleStep(); | |
110 | |
111 /// Cause running process to break (calls | |
112 /// debug::DebugApi::DebugBreakProcess). | |
113 /// Shall not be called on halted process, and only from the thread that | |
114 /// started the debuggee. | |
115 /// Returns before process is stopped, it just initiates breaking debuggee | |
116 /// process. As a result, system will create another thread in debuggee | |
117 /// process, and then that tread executs a trap instruction, causing | |
118 /// delivery of breakpoint exception to the debugger. | |
119 virtual bool Break(); | |
120 | |
121 /// Initiates termination of all threads of the process. | |
122 /// Event loop should process exiting debug event before DebuggeeProcess | |
123 /// object gets into kDead state and can be safely deleted. | |
124 /// TODO(garianov): verify that |Kill| can be called from any thread. | |
125 virtual bool Kill(); | |
126 | |
127 /// Detaches debugger fom the process. Debuggee process is not killed, | |
128 /// it runs freely after debugger is detached. | |
129 /// TODO(garianov): verify that |Detach| can be called from any thread. | |
130 virtual bool Detach(); | |
131 | |
132 /// @return a pointer to the thread object, or NULL if there's | |
133 /// no thread with such |id|. | |
134 /// Thread object is owned by the process. | |
135 virtual DebuggeeThread* GetThread(int id); | |
136 | |
137 /// @return a poiner to the halted thread object, or NULL | |
138 /// if process is not halted. | |
139 /// Thread object is owned by the process. | |
140 virtual DebuggeeThread* GetHaltedThread(); | |
141 | |
142 /// @return all thread ids. | |
143 virtual void GetThreadIds(std::deque<int>* threads) const; | |
144 | |
145 /// Copies memory from debuggee process to debugger buffer. | |
146 /// Shall be called only on halted process. There's no harm though if you | |
147 /// call it on running process. | |
148 /// @param[in] addr address (in debugger address space) from where to read. | |
149 /// @param[in] size number of bytes to read. | |
150 /// @param[out] destination destination buffer (in debugger address space). | |
151 /// TODO(garianov): verify that |ReadMemory| can be called from any thread. | |
152 virtual bool ReadMemory(const void* addr, size_t size, void* destination); | |
153 | |
154 /// Copies memory from debugger to debuggee process. | |
155 /// Shall be called only on halted process. | |
156 /// @param[in] addr address (in debugger address space) where to write. | |
157 /// @param[in] size number of bytes to write. | |
158 /// @param[in] source address of source buffer. | |
159 /// TODO(garianov): verify that |WriteMemory| can be called from any thread. | |
160 virtual bool WriteMemory(const void* addr, size_t size, const void* source); | |
161 | |
162 /// Sets breakpoint at specified address |addr|. | |
163 /// Shall be called only on halted process. | |
164 /// Note: for NaCl threads, breakpoints are supported only in nexe code, | |
165 /// i.e. breakpoints in TCB won't work. | |
166 /// TODO(garianov): add support for breakpoints in TCB. | |
167 /// @param[in] addr address where breakpoint shall be. | |
168 /// @return false if process is not able to access memory at |addr|, | |
169 /// or process is not halted, or if there is breakpoint with the same |addr|. | |
170 virtual bool SetBreakpoint(void* addr); | |
171 | |
172 /// Removes breakpoint at specified address |addr|. | |
173 /// Shall be called only on halted process. | |
174 /// @param[in] addr address of breakpoint. | |
175 /// @return false if process is not halted. | |
176 virtual bool RemoveBreakpoint(void* addr); | |
177 | |
178 /// @return breakpoint object, or NULL if there's no breakpoint | |
179 /// set at |addr|. | |
180 /// @param[in] addr address of breakpoint. | |
181 virtual Breakpoint* GetBreakpoint(void* addr); | |
182 | |
183 /// Return all breakpoints. | |
184 /// @param[out] breakpoints | |
185 virtual void GetBreakpoints(std::deque<Breakpoint*>* breakpoints); | |
186 | |
187 /// Converts relative pointer to flat(aka linear) process address. | |
188 /// Calling this function makes sense only for nexe threads, | |
189 /// it's safe to call for any thread. | |
190 /// @param[in] addr relative pointer | |
191 /// @return flat address | |
192 virtual void* FromNexeToFlatAddress(void* addr) const; | |
193 | |
194 protected: | |
195 friend class ExecutionEngine; | |
196 | |
197 /// Handler of debug events. | |
198 /// @param[in] debug_event debug event received from debuggee process | |
199 virtual void OnDebugEvent(DebugEvent* debug_event); | |
200 | |
201 /// Adds DebuggeeThread object with specified |id| and |handle|. | |
202 /// @param id thread id | |
203 /// @param handle handle to the thread object, should come from | |
204 /// CREATE_THREAD_DEBUG_EVENT or CREATE_PROCESS_DEBUG_EVENT debug events | |
205 /// Don't call ::CloseHandle on it, system releases it when thread is gone. | |
206 /// @return pointer to new thread object, DebuggeeProcess owns it. | |
207 /// Don't delete returned object. | |
208 /// If there's already thread with |id|, no new object is created, | |
209 /// pointer to existing one is returned. | |
210 virtual DebuggeeThread* AddThread(int id, HANDLE handle); | |
211 | |
212 virtual bool ContinueHaltedThread(DebuggeeThread::ContinueOption option); | |
213 | |
214 /// Removes DebuggeeThread object with specified |id|. System thread | |
215 /// structures are not affected. | |
216 /// Called by DebuggeeProcess when thread is gone. | |
217 /// @param id thread id | |
218 virtual void RemoveThread(int id); | |
219 | |
220 /// Delets all thread objects. System structures are not affected. | |
221 virtual void DeleteThreads(); | |
222 | |
223 DebugAPI& debug_api_; | |
224 int id_; | |
225 HANDLE handle_; | |
226 HANDLE file_handle_; | |
227 State state_; | |
228 int exit_code_; | |
229 DebugEvent last_debug_event_; | |
230 std::deque<DebuggeeThread*> threads_; | |
231 std::map<void*, Breakpoint*> breakpoints_; | |
232 void* nexe_mem_base_; | |
233 void* nexe_entry_point_; | |
234 | |
235 private: | |
236 DebuggeeProcess(const DebuggeeProcess&); // DISALLOW_COPY_AND_ASSIGN | |
237 void operator=(const DebuggeeProcess&); | |
238 }; | |
239 } // namespace debug | |
240 #endif // DEBUGGER_CORE_DEBUGGEE_PROCESS_H_ | |
241 | |
OLD | NEW |