annotate agent/src/os/win32/SwDbgSub.cpp @ 738:53d9bf689e80 jdk7-b58

Added tag jdk7-b57 for changeset f4cbf78110c7
author xdono
date Thu, 30 Apr 2009 15:04:27 -0700
parents a61af66fc99e
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // This is the source code for the subprocess forked by the Simple
a61af66fc99e Initial load
duke
parents:
diff changeset
26 // Windows Debug Server. It assumes most of the responsibility for the
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // debug session, and processes all of the commands sent by clients.
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 // Disable too-long symbol warnings
a61af66fc99e Initial load
duke
parents:
diff changeset
30 #pragma warning ( disable : 4786 )
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #include <iostream>
a61af66fc99e Initial load
duke
parents:
diff changeset
33 #include <vector>
a61af66fc99e Initial load
duke
parents:
diff changeset
34 #include <stdlib.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
35 #include <assert.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // Must come before windows.h
a61af66fc99e Initial load
duke
parents:
diff changeset
37 #include <winsock2.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
38 #include <windows.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #include "IOBuf.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
40 #include "libInfo.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
41 #include "LockableList.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
42 #include "Message.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
43 #include "Monitor.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
44 #include "nt4internals.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
45
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // Uncomment the #define below to get messages on stderr
a61af66fc99e Initial load
duke
parents:
diff changeset
47 // #define DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 using namespace std;
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 DWORD pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 HANDLE procHandle;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 IOBuf* ioBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // State flags indicating whether the attach to the remote process
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // definitively succeeded or failed
a61af66fc99e Initial load
duke
parents:
diff changeset
57 volatile bool attachFailed = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
58 volatile bool attachSucceeded = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // State flag indicating whether the target process is suspended.
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // Modified by suspend()/resume(), viewed by debug thread, but only
a61af66fc99e Initial load
duke
parents:
diff changeset
62 // under cover of the threads lock.
a61af66fc99e Initial load
duke
parents:
diff changeset
63 volatile bool suspended = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 // State flags indicating whether we are considered to be attached to
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // the target process and are therefore queuing up events to be sent
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // back to the debug server. These flags are only accessed and
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // modified under the cover of the eventLock.
a61af66fc99e Initial load
duke
parents:
diff changeset
69 Monitor* eventLock;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // The following is set to true when a client is attached to this process
a61af66fc99e Initial load
duke
parents:
diff changeset
71 volatile bool generateDebugEvents = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 // Pointer to current debug event; non-NULL indicates a debug event is
a61af66fc99e Initial load
duke
parents:
diff changeset
73 // waiting to be sent to the client. Main thread sets this to NULL to
a61af66fc99e Initial load
duke
parents:
diff changeset
74 // indicate that the event has been consumed; also sets
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // passEventToClient, below.
a61af66fc99e Initial load
duke
parents:
diff changeset
76 volatile DEBUG_EVENT* curDebugEvent = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // Set by main thread to indicate whether the most recently posted
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // debug event should be passed on to the target process.
a61af66fc99e Initial load
duke
parents:
diff changeset
79 volatile bool passEventToClient = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
80
a61af66fc99e Initial load
duke
parents:
diff changeset
81 void conditionalPostDebugEvent(DEBUG_EVENT* ev, DWORD* continueOrNotHandledFlag) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // FIXME: make it possible for the client to enable and disable
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // certain types of events (have to do so in a platform-independent
a61af66fc99e Initial load
duke
parents:
diff changeset
84 // manner)
a61af66fc99e Initial load
duke
parents:
diff changeset
85 switch (ev->dwDebugEventCode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 case EXCEPTION_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
87 switch (ev->u.Exception.ExceptionRecord.ExceptionCode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 case EXCEPTION_BREAKPOINT: break;
a61af66fc99e Initial load
duke
parents:
diff changeset
89 case EXCEPTION_SINGLE_STEP: break;
a61af66fc99e Initial load
duke
parents:
diff changeset
90 case EXCEPTION_ACCESS_VIOLATION: break;
a61af66fc99e Initial load
duke
parents:
diff changeset
91 default: return;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94 eventLock->lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
95 if (generateDebugEvents) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 curDebugEvent = ev;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 while (curDebugEvent != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 eventLock->wait();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 if (passEventToClient) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 *continueOrNotHandledFlag = DBG_EXCEPTION_NOT_HANDLED;
a61af66fc99e Initial load
duke
parents:
diff changeset
102 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 *continueOrNotHandledFlag = DBG_CONTINUE;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 eventLock->unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
111 // Module list
a61af66fc99e Initial load
duke
parents:
diff changeset
112 //
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 vector<LibInfo> libs;
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // Thread list
a61af66fc99e Initial load
duke
parents:
diff changeset
118 //
a61af66fc99e Initial load
duke
parents:
diff changeset
119
a61af66fc99e Initial load
duke
parents:
diff changeset
120 struct ThreadInfo {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 DWORD tid;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 HANDLE thread;
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 ThreadInfo(DWORD tid, HANDLE thread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 this->tid = tid;
a61af66fc99e Initial load
duke
parents:
diff changeset
126 this->thread = thread;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128 };
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 class ThreadList : public LockableList<ThreadInfo> {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
132 bool removeByThreadID(DWORD tid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 for (InternalListType::iterator iter = internalList.begin();
a61af66fc99e Initial load
duke
parents:
diff changeset
134 iter != internalList.end(); iter++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 if ((*iter).tid == tid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 internalList.erase(iter);
a61af66fc99e Initial load
duke
parents:
diff changeset
137 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142 HANDLE threadIDToHandle(DWORD tid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 for (InternalListType::iterator iter = internalList.begin();
a61af66fc99e Initial load
duke
parents:
diff changeset
144 iter != internalList.end(); iter++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 if ((*iter).tid == tid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
146 return (*iter).thread;
a61af66fc99e Initial load
duke
parents:
diff changeset
147 }
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151 };
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 ThreadList threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // INITIALIZATION AND TERMINATION
a61af66fc99e Initial load
duke
parents:
diff changeset
157 //
a61af66fc99e Initial load
duke
parents:
diff changeset
158
a61af66fc99e Initial load
duke
parents:
diff changeset
159 void
a61af66fc99e Initial load
duke
parents:
diff changeset
160 printError(const char* prefix) {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 DWORD detail = GetLastError();
a61af66fc99e Initial load
duke
parents:
diff changeset
162 LPTSTR message;
a61af66fc99e Initial load
duke
parents:
diff changeset
163 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
a61af66fc99e Initial load
duke
parents:
diff changeset
164 FORMAT_MESSAGE_FROM_SYSTEM,
a61af66fc99e Initial load
duke
parents:
diff changeset
165 0,
a61af66fc99e Initial load
duke
parents:
diff changeset
166 detail,
a61af66fc99e Initial load
duke
parents:
diff changeset
167 0,
a61af66fc99e Initial load
duke
parents:
diff changeset
168 (LPTSTR) &message,
a61af66fc99e Initial load
duke
parents:
diff changeset
169 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
170 NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
171 // FIXME: This is signaling an error: "The handle is invalid." ?
a61af66fc99e Initial load
duke
parents:
diff changeset
172 // Do I have to do all of my WaitForDebugEvent calls from the same thread?
a61af66fc99e Initial load
duke
parents:
diff changeset
173 cerr << prefix << ": " << message << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
174 LocalFree(message);
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176
a61af66fc99e Initial load
duke
parents:
diff changeset
177 void
a61af66fc99e Initial load
duke
parents:
diff changeset
178 endProcess(bool waitForProcess = true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 NT4::unloadNTDLL();
a61af66fc99e Initial load
duke
parents:
diff changeset
180 if (waitForProcess) {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 // Though we're exiting because of an error, do not tear down the
a61af66fc99e Initial load
duke
parents:
diff changeset
182 // target process.
a61af66fc99e Initial load
duke
parents:
diff changeset
183 WaitForSingleObject(procHandle, INFINITE);
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185 CloseHandle(procHandle);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 exit(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 DWORD WINAPI
a61af66fc99e Initial load
duke
parents:
diff changeset
190 debugThreadEntry(void*) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
192 DWORD lastMsgId = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
193 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
195
a61af66fc99e Initial load
duke
parents:
diff changeset
196 if (!DebugActiveProcess(pid)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 attachFailed = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
199 }
a61af66fc99e Initial load
duke
parents:
diff changeset
200
a61af66fc99e Initial load
duke
parents:
diff changeset
201 // Wait for debug events. We keep the information from some of these
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // on the side in anticipation of later queries by the client. NOTE
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // that we leave the process running. The main thread is responsible
a61af66fc99e Initial load
duke
parents:
diff changeset
204 // for suspending and resuming all currently-active threads upon
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // client attach and detach.
a61af66fc99e Initial load
duke
parents:
diff changeset
206
a61af66fc99e Initial load
duke
parents:
diff changeset
207 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 DEBUG_EVENT ev;
a61af66fc99e Initial load
duke
parents:
diff changeset
209 if (!WaitForDebugEvent(&ev, INFINITE)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
210 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
211 if (++count < 10) {
a61af66fc99e Initial load
duke
parents:
diff changeset
212 // FIXME: This is signaling an error: "The handle is invalid." ?
a61af66fc99e Initial load
duke
parents:
diff changeset
213 // Do I have to do all of my WaitForDebugEvent calls from the same thread?
a61af66fc99e Initial load
duke
parents:
diff changeset
214 printError("WaitForDebugEvent failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
217 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
218
a61af66fc99e Initial load
duke
parents:
diff changeset
219 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
220 if (ev.dwDebugEventCode != lastMsgId) {
a61af66fc99e Initial load
duke
parents:
diff changeset
221 lastMsgId = ev.dwDebugEventCode;
a61af66fc99e Initial load
duke
parents:
diff changeset
222 count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
223 cerr << "Debug thread received event " << ev.dwDebugEventCode << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
224 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 if (++count < 10) {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 cerr << "Debug thread received event " << ev.dwDebugEventCode << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
227 }
a61af66fc99e Initial load
duke
parents:
diff changeset
228 }
a61af66fc99e Initial load
duke
parents:
diff changeset
229 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 DWORD dbgContinueMode = DBG_CONTINUE;
a61af66fc99e Initial load
duke
parents:
diff changeset
232
a61af66fc99e Initial load
duke
parents:
diff changeset
233 switch (ev.dwDebugEventCode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
234 case LOAD_DLL_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
235 conditionalPostDebugEvent(&ev, &dbgContinueMode);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
237
a61af66fc99e Initial load
duke
parents:
diff changeset
238 case UNLOAD_DLL_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
239 conditionalPostDebugEvent(&ev, &dbgContinueMode);
a61af66fc99e Initial load
duke
parents:
diff changeset
240 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
241
a61af66fc99e Initial load
duke
parents:
diff changeset
242 case CREATE_PROCESS_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
243 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // FIXME: will this deal properly with child processes? If
a61af66fc99e Initial load
duke
parents:
diff changeset
245 // not, is it possible to make it do so?
a61af66fc99e Initial load
duke
parents:
diff changeset
246 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
247 cerr << "CREATE_PROCESS_DEBUG_EVENT " << ev.dwThreadId
a61af66fc99e Initial load
duke
parents:
diff changeset
248 << " " << ev.u.CreateProcessInfo.hThread << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
249 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
250 if (ev.u.CreateProcessInfo.hThread != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
251 threads.add(ThreadInfo(ev.dwThreadId, ev.u.CreateProcessInfo.hThread));
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
254 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 case CREATE_THREAD_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
257 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
258 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
259 cerr << "CREATE_THREAD_DEBUG_EVENT " << ev.dwThreadId
a61af66fc99e Initial load
duke
parents:
diff changeset
260 << " " << ev.u.CreateThread.hThread << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
261 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
262 if (suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 // Suspend this thread before adding it to the thread list
a61af66fc99e Initial load
duke
parents:
diff changeset
264 SuspendThread(ev.u.CreateThread.hThread);
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266 threads.add(ThreadInfo(ev.dwThreadId, ev.u.CreateThread.hThread));
a61af66fc99e Initial load
duke
parents:
diff changeset
267 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
268 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 case EXIT_THREAD_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
271 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
272 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
273 cerr << "EXIT_THREAD_DEBUG_EVENT " << ev.dwThreadId << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
274 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
275 threads.removeByThreadID(ev.dwThreadId);
a61af66fc99e Initial load
duke
parents:
diff changeset
276 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
277 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
278
a61af66fc99e Initial load
duke
parents:
diff changeset
279 case EXCEPTION_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
280 // cerr << "EXCEPTION_DEBUG_EVENT" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
281 switch (ev.u.Exception.ExceptionRecord.ExceptionCode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 case EXCEPTION_BREAKPOINT:
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // cerr << "EXCEPTION_BREAKPOINT" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
284 if (!attachSucceeded && !attachFailed) {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 attachSucceeded = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
288
a61af66fc99e Initial load
duke
parents:
diff changeset
289 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
290 dbgContinueMode = DBG_EXCEPTION_NOT_HANDLED;
a61af66fc99e Initial load
duke
parents:
diff changeset
291 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293 conditionalPostDebugEvent(&ev, &dbgContinueMode);
a61af66fc99e Initial load
duke
parents:
diff changeset
294 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
295
a61af66fc99e Initial load
duke
parents:
diff changeset
296 case EXIT_PROCESS_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
297 endProcess(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
298 // NOT REACHED
a61af66fc99e Initial load
duke
parents:
diff changeset
299 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
300
a61af66fc99e Initial load
duke
parents:
diff changeset
301 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
302 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
303 cerr << "Received debug event " << ev.dwDebugEventCode << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
304 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
305 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307
a61af66fc99e Initial load
duke
parents:
diff changeset
308 ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, dbgContinueMode);
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311 }
a61af66fc99e Initial load
duke
parents:
diff changeset
312
a61af66fc99e Initial load
duke
parents:
diff changeset
313 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
314 attachToProcess() {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 // Create event lock
a61af66fc99e Initial load
duke
parents:
diff changeset
316 eventLock = new Monitor();
a61af66fc99e Initial load
duke
parents:
diff changeset
317
a61af66fc99e Initial load
duke
parents:
diff changeset
318 // Get a process handle for later
a61af66fc99e Initial load
duke
parents:
diff changeset
319 procHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
320 if (procHandle == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 }
a61af66fc99e Initial load
duke
parents:
diff changeset
323
a61af66fc99e Initial load
duke
parents:
diff changeset
324 // Start up the debug thread
a61af66fc99e Initial load
duke
parents:
diff changeset
325 DWORD debugThreadId;
a61af66fc99e Initial load
duke
parents:
diff changeset
326 if (CreateThread(NULL, 0, &debugThreadEntry, NULL, 0, &debugThreadId) == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 // Failed to make background debug thread. Fail.
a61af66fc99e Initial load
duke
parents:
diff changeset
328 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330
a61af66fc99e Initial load
duke
parents:
diff changeset
331 while ((!attachSucceeded) && (!attachFailed)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
332 Sleep(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
333 }
a61af66fc99e Initial load
duke
parents:
diff changeset
334
a61af66fc99e Initial load
duke
parents:
diff changeset
335 if (attachFailed) {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
337 }
a61af66fc99e Initial load
duke
parents:
diff changeset
338
a61af66fc99e Initial load
duke
parents:
diff changeset
339 assert(attachSucceeded);
a61af66fc99e Initial load
duke
parents:
diff changeset
340
a61af66fc99e Initial load
duke
parents:
diff changeset
341 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 }
a61af66fc99e Initial load
duke
parents:
diff changeset
343
a61af66fc99e Initial load
duke
parents:
diff changeset
344 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
345 readMessage(Message* msg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
346 DWORD numRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
347 if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE),
a61af66fc99e Initial load
duke
parents:
diff changeset
348 msg,
a61af66fc99e Initial load
duke
parents:
diff changeset
349 sizeof(Message),
a61af66fc99e Initial load
duke
parents:
diff changeset
350 &numRead,
a61af66fc99e Initial load
duke
parents:
diff changeset
351 NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
354 if (numRead != sizeof(Message)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
355 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357 // For "poke" messages, must follow up by reading raw data
a61af66fc99e Initial load
duke
parents:
diff changeset
358 if (msg->type == Message::POKE) {
a61af66fc99e Initial load
duke
parents:
diff changeset
359 char* dataBuf = new char[msg->pokeArg.numBytes];
a61af66fc99e Initial load
duke
parents:
diff changeset
360 if (dataBuf == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
362 }
a61af66fc99e Initial load
duke
parents:
diff changeset
363 if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE),
a61af66fc99e Initial load
duke
parents:
diff changeset
364 dataBuf,
a61af66fc99e Initial load
duke
parents:
diff changeset
365 msg->pokeArg.numBytes,
a61af66fc99e Initial load
duke
parents:
diff changeset
366 &numRead,
a61af66fc99e Initial load
duke
parents:
diff changeset
367 NULL)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
368 delete[] dataBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
370 }
a61af66fc99e Initial load
duke
parents:
diff changeset
371 if (numRead != msg->pokeArg.numBytes) {
a61af66fc99e Initial load
duke
parents:
diff changeset
372 delete[] dataBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
373 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
374 }
a61af66fc99e Initial load
duke
parents:
diff changeset
375 msg->pokeArg.data = (void *) dataBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
378 }
a61af66fc99e Initial load
duke
parents:
diff changeset
379
a61af66fc99e Initial load
duke
parents:
diff changeset
380 void
a61af66fc99e Initial load
duke
parents:
diff changeset
381 handlePeek(Message* msg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
382 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
383 cerr << "Entering handlePeek()" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
384 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
385
a61af66fc99e Initial load
duke
parents:
diff changeset
386 char* memBuf = new char[msg->peekArg.numBytes];
a61af66fc99e Initial load
duke
parents:
diff changeset
387 if (memBuf == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
388 ioBuf->writeString("B");
a61af66fc99e Initial load
duke
parents:
diff changeset
389 ioBuf->writeBinChar(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
390 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
391 delete[] memBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
392 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
393 }
a61af66fc99e Initial load
duke
parents:
diff changeset
394
a61af66fc99e Initial load
duke
parents:
diff changeset
395 // Try fast case first
a61af66fc99e Initial load
duke
parents:
diff changeset
396 DWORD numRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
397 BOOL res = ReadProcessMemory(procHandle,
a61af66fc99e Initial load
duke
parents:
diff changeset
398 (LPCVOID) msg->peekArg.address,
a61af66fc99e Initial load
duke
parents:
diff changeset
399 memBuf,
a61af66fc99e Initial load
duke
parents:
diff changeset
400 msg->peekArg.numBytes,
a61af66fc99e Initial load
duke
parents:
diff changeset
401 &numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
402 if (res && (numRead == msg->peekArg.numBytes)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
403
a61af66fc99e Initial load
duke
parents:
diff changeset
404 // OK, complete success. Phew.
a61af66fc99e Initial load
duke
parents:
diff changeset
405 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
406 cerr << "Peek success case" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
408 ioBuf->writeString("B");
a61af66fc99e Initial load
duke
parents:
diff changeset
409 ioBuf->writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
410 ioBuf->writeBinUnsignedInt(numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
411 ioBuf->writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
412 ioBuf->writeBinBuf(memBuf, numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
413 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
414 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
415 cerr << "*** Peek slow case ***" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
416 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
417
a61af66fc99e Initial load
duke
parents:
diff changeset
418 ioBuf->writeString("B");
a61af66fc99e Initial load
duke
parents:
diff changeset
419 ioBuf->writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
420
a61af66fc99e Initial load
duke
parents:
diff changeset
421 // Use VirtualQuery to speed things up a bit
a61af66fc99e Initial load
duke
parents:
diff changeset
422 DWORD numLeft = msg->peekArg.numBytes;
a61af66fc99e Initial load
duke
parents:
diff changeset
423 char* curAddr = (char*) msg->peekArg.address;
a61af66fc99e Initial load
duke
parents:
diff changeset
424 while (numLeft > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 MEMORY_BASIC_INFORMATION memInfo;
a61af66fc99e Initial load
duke
parents:
diff changeset
426 VirtualQueryEx(procHandle, curAddr, &memInfo, sizeof(memInfo));
a61af66fc99e Initial load
duke
parents:
diff changeset
427 DWORD numToRead = memInfo.RegionSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 if (numToRead > numLeft) {
a61af66fc99e Initial load
duke
parents:
diff changeset
429 numToRead = numLeft;
a61af66fc99e Initial load
duke
parents:
diff changeset
430 }
a61af66fc99e Initial load
duke
parents:
diff changeset
431 DWORD numRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
432 if (memInfo.State == MEM_COMMIT) {
a61af66fc99e Initial load
duke
parents:
diff changeset
433 // Read the process memory at this address for this length
a61af66fc99e Initial load
duke
parents:
diff changeset
434 // FIXME: should check the result of this read
a61af66fc99e Initial load
duke
parents:
diff changeset
435 ReadProcessMemory(procHandle, curAddr, memBuf,
a61af66fc99e Initial load
duke
parents:
diff changeset
436 numToRead, &numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
437 // Write this out
a61af66fc99e Initial load
duke
parents:
diff changeset
438 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
439 cerr << "*** Writing " << numToRead << " bytes as mapped ***" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
440 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
441 ioBuf->writeBinUnsignedInt(numToRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
442 ioBuf->writeBinChar(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
443 ioBuf->writeBinBuf(memBuf, numToRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
444 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
445 // Indicate region is free
a61af66fc99e Initial load
duke
parents:
diff changeset
446 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
447 cerr << "*** Writing " << numToRead << " bytes as unmapped ***" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
448 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
449 ioBuf->writeBinUnsignedInt(numToRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
450 ioBuf->writeBinChar(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
451 }
a61af66fc99e Initial load
duke
parents:
diff changeset
452 curAddr += numToRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
453 numLeft -= numToRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
454 }
a61af66fc99e Initial load
duke
parents:
diff changeset
455 }
a61af66fc99e Initial load
duke
parents:
diff changeset
456
a61af66fc99e Initial load
duke
parents:
diff changeset
457 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
458 delete[] memBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
459 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
460 cerr << "Exiting handlePeek()" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
461 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
462 }
a61af66fc99e Initial load
duke
parents:
diff changeset
463
a61af66fc99e Initial load
duke
parents:
diff changeset
464 void
a61af66fc99e Initial load
duke
parents:
diff changeset
465 handlePoke(Message* msg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
466 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
467 cerr << "Entering handlePoke()" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
469 DWORD numWritten;
a61af66fc99e Initial load
duke
parents:
diff changeset
470 BOOL res = WriteProcessMemory(procHandle,
a61af66fc99e Initial load
duke
parents:
diff changeset
471 (LPVOID) msg->pokeArg.address,
a61af66fc99e Initial load
duke
parents:
diff changeset
472 msg->pokeArg.data,
a61af66fc99e Initial load
duke
parents:
diff changeset
473 msg->pokeArg.numBytes,
a61af66fc99e Initial load
duke
parents:
diff changeset
474 &numWritten);
a61af66fc99e Initial load
duke
parents:
diff changeset
475 if (res && (numWritten == msg->pokeArg.numBytes)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
476 // Success
a61af66fc99e Initial load
duke
parents:
diff changeset
477 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
478 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
479 cerr << " (Succeeded)" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
481 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 // Failure
a61af66fc99e Initial load
duke
parents:
diff changeset
483 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
484 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
485 cerr << " (Failed)" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
486 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
487 }
a61af66fc99e Initial load
duke
parents:
diff changeset
488 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
489 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
490 // We clean up the data
a61af66fc99e Initial load
duke
parents:
diff changeset
491 char* dataBuf = (char*) msg->pokeArg.data;
a61af66fc99e Initial load
duke
parents:
diff changeset
492 delete[] dataBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
493 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
494 cerr << "Exiting handlePoke()" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
495 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
496 }
a61af66fc99e Initial load
duke
parents:
diff changeset
497
a61af66fc99e Initial load
duke
parents:
diff changeset
498 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
499 suspend() {
a61af66fc99e Initial load
duke
parents:
diff changeset
500 if (suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
502 }
a61af66fc99e Initial load
duke
parents:
diff changeset
503 // Before we suspend, we must take a snapshot of the loaded module
a61af66fc99e Initial load
duke
parents:
diff changeset
504 // names and base addresses, since acquiring this snapshot requires
a61af66fc99e Initial load
duke
parents:
diff changeset
505 // starting and exiting a thread in the remote process (at least on
a61af66fc99e Initial load
duke
parents:
diff changeset
506 // NT 4).
a61af66fc99e Initial load
duke
parents:
diff changeset
507 libs.clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
508 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
509 cerr << "Starting suspension" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
510 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
511 libInfo(pid, libs);
a61af66fc99e Initial load
duke
parents:
diff changeset
512 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
513 cerr << " Got lib info" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
514 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
515 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
516 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
517 cerr << " Got thread lock" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
518 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
519 suspended = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
520 int j = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
521 for (int i = 0; i < threads.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
522 j++;
a61af66fc99e Initial load
duke
parents:
diff changeset
523 SuspendThread(threads.get(i).thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
524 }
a61af66fc99e Initial load
duke
parents:
diff changeset
525 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
526 cerr << "Suspended " << j << " threads" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
527 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
528 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
529 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
530 }
a61af66fc99e Initial load
duke
parents:
diff changeset
531
a61af66fc99e Initial load
duke
parents:
diff changeset
532 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
533 resume() {
a61af66fc99e Initial load
duke
parents:
diff changeset
534 if (!suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
535 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
536 }
a61af66fc99e Initial load
duke
parents:
diff changeset
537 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
538 suspended = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
539 for (int i = 0; i < threads.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
540 ResumeThread(threads.get(i).thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
541 }
a61af66fc99e Initial load
duke
parents:
diff changeset
542 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
543 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
544 cerr << "Resumed process" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
545 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
546 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
547 }
a61af66fc99e Initial load
duke
parents:
diff changeset
548
a61af66fc99e Initial load
duke
parents:
diff changeset
549 int
a61af66fc99e Initial load
duke
parents:
diff changeset
550 main(int argc, char **argv)
a61af66fc99e Initial load
duke
parents:
diff changeset
551 {
a61af66fc99e Initial load
duke
parents:
diff changeset
552 if (argc != 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // Should only be used by performing CreateProcess within SwDbgSrv
a61af66fc99e Initial load
duke
parents:
diff changeset
554 exit(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
555 }
a61af66fc99e Initial load
duke
parents:
diff changeset
556
a61af66fc99e Initial load
duke
parents:
diff changeset
557 if (sscanf(argv[1], "%u", &pid) != 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
558 exit(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
559 }
a61af66fc99e Initial load
duke
parents:
diff changeset
560
a61af66fc99e Initial load
duke
parents:
diff changeset
561 // Try to attach to process
a61af66fc99e Initial load
duke
parents:
diff changeset
562 if (!attachToProcess()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
563 // Attach failed. Notify parent by writing result to stdout file
a61af66fc99e Initial load
duke
parents:
diff changeset
564 // handle.
a61af66fc99e Initial load
duke
parents:
diff changeset
565 char res = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
566 DWORD numBytes;
a61af66fc99e Initial load
duke
parents:
diff changeset
567 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), &res, sizeof(res),
a61af66fc99e Initial load
duke
parents:
diff changeset
568 &numBytes, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
569 exit(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
570 }
a61af66fc99e Initial load
duke
parents:
diff changeset
571
a61af66fc99e Initial load
duke
parents:
diff changeset
572 // Server is expecting success result back.
a61af66fc99e Initial load
duke
parents:
diff changeset
573 char res = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
574 DWORD numBytes;
a61af66fc99e Initial load
duke
parents:
diff changeset
575 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), &res, sizeof(res),
a61af66fc99e Initial load
duke
parents:
diff changeset
576 &numBytes, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
577
a61af66fc99e Initial load
duke
parents:
diff changeset
578 // Initialize our I/O buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
579 ioBuf = new IOBuf(32768, 131072);
a61af66fc99e Initial load
duke
parents:
diff changeset
580 ioBuf->setOutputFileHandle(GetStdHandle(STD_OUTPUT_HANDLE));
a61af66fc99e Initial load
duke
parents:
diff changeset
581
a61af66fc99e Initial load
duke
parents:
diff changeset
582 // At this point we are attached. Enter our main loop which services
a61af66fc99e Initial load
duke
parents:
diff changeset
583 // requests from the server. Note that in order to handle attach/
a61af66fc99e Initial load
duke
parents:
diff changeset
584 // detach properly (i.e., resumption of process upon "detach") we
a61af66fc99e Initial load
duke
parents:
diff changeset
585 // will need another thread which handles debug events.
a61af66fc99e Initial load
duke
parents:
diff changeset
586 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
587 // Read a message from the server
a61af66fc99e Initial load
duke
parents:
diff changeset
588 Message msg;
a61af66fc99e Initial load
duke
parents:
diff changeset
589 if (!readMessage(&msg)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
590 endProcess();
a61af66fc99e Initial load
duke
parents:
diff changeset
591 }
a61af66fc99e Initial load
duke
parents:
diff changeset
592
a61af66fc99e Initial load
duke
parents:
diff changeset
593 #ifdef DEBUGGING
a61af66fc99e Initial load
duke
parents:
diff changeset
594 cerr << "Main thread read message: " << msg.type << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
595 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
596
a61af66fc99e Initial load
duke
parents:
diff changeset
597 switch (msg.type) {
a61af66fc99e Initial load
duke
parents:
diff changeset
598 // ATTACH and DETACH messages MUST come in pairs
a61af66fc99e Initial load
duke
parents:
diff changeset
599 case Message::ATTACH:
a61af66fc99e Initial load
duke
parents:
diff changeset
600 suspend();
a61af66fc99e Initial load
duke
parents:
diff changeset
601 eventLock->lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
602 generateDebugEvents = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
603 eventLock->unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
604 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
605
a61af66fc99e Initial load
duke
parents:
diff changeset
606 case Message::DETACH:
a61af66fc99e Initial load
duke
parents:
diff changeset
607 eventLock->lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
608 generateDebugEvents = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
609 // Flush remaining event if any
a61af66fc99e Initial load
duke
parents:
diff changeset
610 if (curDebugEvent != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
611 curDebugEvent = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
612 eventLock->notifyAll();
a61af66fc99e Initial load
duke
parents:
diff changeset
613 }
a61af66fc99e Initial load
duke
parents:
diff changeset
614 eventLock->unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
615 resume();
a61af66fc99e Initial load
duke
parents:
diff changeset
616 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
617
a61af66fc99e Initial load
duke
parents:
diff changeset
618 case Message::LIBINFO:
a61af66fc99e Initial load
duke
parents:
diff changeset
619 {
a61af66fc99e Initial load
duke
parents:
diff changeset
620 if (!suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
621 ioBuf->writeInt(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
622 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
623 // Send back formatted text
a61af66fc99e Initial load
duke
parents:
diff changeset
624 ioBuf->writeInt(libs.size());
a61af66fc99e Initial load
duke
parents:
diff changeset
625 for (int i = 0; i < libs.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
626 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
627 ioBuf->writeInt(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
628 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
629 ioBuf->writeInt(libs[i].name.size());
a61af66fc99e Initial load
duke
parents:
diff changeset
630 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
631 ioBuf->writeString(libs[i].name.c_str());
a61af66fc99e Initial load
duke
parents:
diff changeset
632 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
633 ioBuf->writeAddress(libs[i].base);
a61af66fc99e Initial load
duke
parents:
diff changeset
634 }
a61af66fc99e Initial load
duke
parents:
diff changeset
635 }
a61af66fc99e Initial load
duke
parents:
diff changeset
636 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
637 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
638 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
639 }
a61af66fc99e Initial load
duke
parents:
diff changeset
640
a61af66fc99e Initial load
duke
parents:
diff changeset
641 case Message::PEEK:
a61af66fc99e Initial load
duke
parents:
diff changeset
642 handlePeek(&msg);
a61af66fc99e Initial load
duke
parents:
diff changeset
643 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
644
a61af66fc99e Initial load
duke
parents:
diff changeset
645 case Message::POKE:
a61af66fc99e Initial load
duke
parents:
diff changeset
646 handlePoke(&msg);
a61af66fc99e Initial load
duke
parents:
diff changeset
647 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
648
a61af66fc99e Initial load
duke
parents:
diff changeset
649 case Message::THREADLIST:
a61af66fc99e Initial load
duke
parents:
diff changeset
650 {
a61af66fc99e Initial load
duke
parents:
diff changeset
651 if (!suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
652 ioBuf->writeInt(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
653 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
654 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
655 ioBuf->writeInt(threads.size());
a61af66fc99e Initial load
duke
parents:
diff changeset
656 for (int i = 0; i < threads.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
657 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
658 ioBuf->writeAddress((void*) threads.get(i).thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
659 }
a61af66fc99e Initial load
duke
parents:
diff changeset
660 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
661 }
a61af66fc99e Initial load
duke
parents:
diff changeset
662 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
663 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
664 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
665 }
a61af66fc99e Initial load
duke
parents:
diff changeset
666
a61af66fc99e Initial load
duke
parents:
diff changeset
667 case Message::DUPHANDLE:
a61af66fc99e Initial load
duke
parents:
diff changeset
668 {
a61af66fc99e Initial load
duke
parents:
diff changeset
669 HANDLE dup;
a61af66fc99e Initial load
duke
parents:
diff changeset
670 if (DuplicateHandle(procHandle,
a61af66fc99e Initial load
duke
parents:
diff changeset
671 msg.handleArg.handle,
a61af66fc99e Initial load
duke
parents:
diff changeset
672 GetCurrentProcess(),
a61af66fc99e Initial load
duke
parents:
diff changeset
673 &dup,
a61af66fc99e Initial load
duke
parents:
diff changeset
674 0,
a61af66fc99e Initial load
duke
parents:
diff changeset
675 FALSE,
a61af66fc99e Initial load
duke
parents:
diff changeset
676 DUPLICATE_SAME_ACCESS)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
677 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
678 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
679 ioBuf->writeAddress((void*) dup);
a61af66fc99e Initial load
duke
parents:
diff changeset
680 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
681 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
682 }
a61af66fc99e Initial load
duke
parents:
diff changeset
683 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
684 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
685 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
686 }
a61af66fc99e Initial load
duke
parents:
diff changeset
687
a61af66fc99e Initial load
duke
parents:
diff changeset
688 case Message::CLOSEHANDLE:
a61af66fc99e Initial load
duke
parents:
diff changeset
689 {
a61af66fc99e Initial load
duke
parents:
diff changeset
690 CloseHandle(msg.handleArg.handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
691 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
692 }
a61af66fc99e Initial load
duke
parents:
diff changeset
693
a61af66fc99e Initial load
duke
parents:
diff changeset
694 case Message::GETCONTEXT:
a61af66fc99e Initial load
duke
parents:
diff changeset
695 {
a61af66fc99e Initial load
duke
parents:
diff changeset
696 if (!suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
697 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
698 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
699 CONTEXT context;
a61af66fc99e Initial load
duke
parents:
diff changeset
700 context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
a61af66fc99e Initial load
duke
parents:
diff changeset
701 if (GetThreadContext(msg.handleArg.handle, &context)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
702 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
703 // EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP, EIP, DS, ES, FS, GS,
a61af66fc99e Initial load
duke
parents:
diff changeset
704 // CS, SS, EFLAGS, DR0, DR1, DR2, DR3, DR6, DR7
a61af66fc99e Initial load
duke
parents:
diff changeset
705 // See README-commands.txt
a61af66fc99e Initial load
duke
parents:
diff changeset
706 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Eax);
a61af66fc99e Initial load
duke
parents:
diff changeset
707 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Ebx);
a61af66fc99e Initial load
duke
parents:
diff changeset
708 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Ecx);
a61af66fc99e Initial load
duke
parents:
diff changeset
709 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Edx);
a61af66fc99e Initial load
duke
parents:
diff changeset
710 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Esi);
a61af66fc99e Initial load
duke
parents:
diff changeset
711 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Edi);
a61af66fc99e Initial load
duke
parents:
diff changeset
712 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Ebp);
a61af66fc99e Initial load
duke
parents:
diff changeset
713 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Esp);
a61af66fc99e Initial load
duke
parents:
diff changeset
714 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Eip);
a61af66fc99e Initial load
duke
parents:
diff changeset
715 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.SegDs);
a61af66fc99e Initial load
duke
parents:
diff changeset
716 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.SegEs);
a61af66fc99e Initial load
duke
parents:
diff changeset
717 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.SegFs);
a61af66fc99e Initial load
duke
parents:
diff changeset
718 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.SegGs);
a61af66fc99e Initial load
duke
parents:
diff changeset
719 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.SegCs);
a61af66fc99e Initial load
duke
parents:
diff changeset
720 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.SegSs);
a61af66fc99e Initial load
duke
parents:
diff changeset
721 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.EFlags);
a61af66fc99e Initial load
duke
parents:
diff changeset
722 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Dr0);
a61af66fc99e Initial load
duke
parents:
diff changeset
723 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Dr1);
a61af66fc99e Initial load
duke
parents:
diff changeset
724 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Dr2);
a61af66fc99e Initial load
duke
parents:
diff changeset
725 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Dr3);
a61af66fc99e Initial load
duke
parents:
diff changeset
726 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Dr6);
a61af66fc99e Initial load
duke
parents:
diff changeset
727 ioBuf->writeSpace(); ioBuf->writeAddress((void*) context.Dr7);
a61af66fc99e Initial load
duke
parents:
diff changeset
728 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
729 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
730 }
a61af66fc99e Initial load
duke
parents:
diff changeset
731 }
a61af66fc99e Initial load
duke
parents:
diff changeset
732 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
733 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
734 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
735 }
a61af66fc99e Initial load
duke
parents:
diff changeset
736
a61af66fc99e Initial load
duke
parents:
diff changeset
737 case Message::SETCONTEXT:
a61af66fc99e Initial load
duke
parents:
diff changeset
738 {
a61af66fc99e Initial load
duke
parents:
diff changeset
739 if (!suspended) {
a61af66fc99e Initial load
duke
parents:
diff changeset
740 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
741 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
742 CONTEXT context;
a61af66fc99e Initial load
duke
parents:
diff changeset
743 context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
a61af66fc99e Initial load
duke
parents:
diff changeset
744 context.Eax = msg.setContextArg.Eax;
a61af66fc99e Initial load
duke
parents:
diff changeset
745 context.Ebx = msg.setContextArg.Ebx;
a61af66fc99e Initial load
duke
parents:
diff changeset
746 context.Ecx = msg.setContextArg.Ecx;
a61af66fc99e Initial load
duke
parents:
diff changeset
747 context.Edx = msg.setContextArg.Edx;
a61af66fc99e Initial load
duke
parents:
diff changeset
748 context.Esi = msg.setContextArg.Esi;
a61af66fc99e Initial load
duke
parents:
diff changeset
749 context.Edi = msg.setContextArg.Edi;
a61af66fc99e Initial load
duke
parents:
diff changeset
750 context.Ebp = msg.setContextArg.Ebp;
a61af66fc99e Initial load
duke
parents:
diff changeset
751 context.Esp = msg.setContextArg.Esp;
a61af66fc99e Initial load
duke
parents:
diff changeset
752 context.Eip = msg.setContextArg.Eip;
a61af66fc99e Initial load
duke
parents:
diff changeset
753 context.SegDs = msg.setContextArg.Ds;
a61af66fc99e Initial load
duke
parents:
diff changeset
754 context.SegEs = msg.setContextArg.Es;
a61af66fc99e Initial load
duke
parents:
diff changeset
755 context.SegFs = msg.setContextArg.Fs;
a61af66fc99e Initial load
duke
parents:
diff changeset
756 context.SegGs = msg.setContextArg.Gs;
a61af66fc99e Initial load
duke
parents:
diff changeset
757 context.SegCs = msg.setContextArg.Cs;
a61af66fc99e Initial load
duke
parents:
diff changeset
758 context.SegSs = msg.setContextArg.Ss;
a61af66fc99e Initial load
duke
parents:
diff changeset
759 context.EFlags = msg.setContextArg.EFlags;
a61af66fc99e Initial load
duke
parents:
diff changeset
760 context.Dr0 = msg.setContextArg.Dr0;
a61af66fc99e Initial load
duke
parents:
diff changeset
761 context.Dr1 = msg.setContextArg.Dr1;
a61af66fc99e Initial load
duke
parents:
diff changeset
762 context.Dr2 = msg.setContextArg.Dr2;
a61af66fc99e Initial load
duke
parents:
diff changeset
763 context.Dr3 = msg.setContextArg.Dr3;
a61af66fc99e Initial load
duke
parents:
diff changeset
764 context.Dr6 = msg.setContextArg.Dr6;
a61af66fc99e Initial load
duke
parents:
diff changeset
765 context.Dr7 = msg.setContextArg.Dr7;
a61af66fc99e Initial load
duke
parents:
diff changeset
766 if (SetThreadContext(msg.setContextArg.handle, &context)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
767 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
768 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
769 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
770 }
a61af66fc99e Initial load
duke
parents:
diff changeset
771 }
a61af66fc99e Initial load
duke
parents:
diff changeset
772 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
773 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
774 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
775 }
a61af66fc99e Initial load
duke
parents:
diff changeset
776
a61af66fc99e Initial load
duke
parents:
diff changeset
777 case Message::SELECTORENTRY:
a61af66fc99e Initial load
duke
parents:
diff changeset
778 {
a61af66fc99e Initial load
duke
parents:
diff changeset
779 LDT_ENTRY entry;
a61af66fc99e Initial load
duke
parents:
diff changeset
780
a61af66fc99e Initial load
duke
parents:
diff changeset
781 if (GetThreadSelectorEntry(msg.selectorArg.handle,
a61af66fc99e Initial load
duke
parents:
diff changeset
782 msg.selectorArg.selector,
a61af66fc99e Initial load
duke
parents:
diff changeset
783 &entry)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
784 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
785 ioBuf->writeSpace(); ioBuf->writeAddress((void*) entry.LimitLow);
a61af66fc99e Initial load
duke
parents:
diff changeset
786 ioBuf->writeSpace(); ioBuf->writeAddress((void*) entry.BaseLow);
a61af66fc99e Initial load
duke
parents:
diff changeset
787 ioBuf->writeSpace(); ioBuf->writeAddress((void*) entry.HighWord.Bytes.BaseMid);
a61af66fc99e Initial load
duke
parents:
diff changeset
788 ioBuf->writeSpace(); ioBuf->writeAddress((void*) entry.HighWord.Bytes.Flags1);
a61af66fc99e Initial load
duke
parents:
diff changeset
789 ioBuf->writeSpace(); ioBuf->writeAddress((void*) entry.HighWord.Bytes.Flags2);
a61af66fc99e Initial load
duke
parents:
diff changeset
790 ioBuf->writeSpace(); ioBuf->writeAddress((void*) entry.HighWord.Bytes.BaseHi);
a61af66fc99e Initial load
duke
parents:
diff changeset
791 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
792 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
793 }
a61af66fc99e Initial load
duke
parents:
diff changeset
794
a61af66fc99e Initial load
duke
parents:
diff changeset
795 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
796 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
797 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
798 }
a61af66fc99e Initial load
duke
parents:
diff changeset
799
a61af66fc99e Initial load
duke
parents:
diff changeset
800 case Message::SUSPEND:
a61af66fc99e Initial load
duke
parents:
diff changeset
801 suspend();
a61af66fc99e Initial load
duke
parents:
diff changeset
802 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
803
a61af66fc99e Initial load
duke
parents:
diff changeset
804 case Message::RESUME:
a61af66fc99e Initial load
duke
parents:
diff changeset
805 resume();
a61af66fc99e Initial load
duke
parents:
diff changeset
806 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
807
a61af66fc99e Initial load
duke
parents:
diff changeset
808 case Message::POLLEVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
809 eventLock->lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
810 if (curDebugEvent == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
811 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
812 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
813 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
814 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
815 threads.lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
816 ioBuf->writeAddress((void*) threads.threadIDToHandle(curDebugEvent->dwThreadId));
a61af66fc99e Initial load
duke
parents:
diff changeset
817 threads.unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
818 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
819 ioBuf->writeUnsignedInt(curDebugEvent->dwDebugEventCode);
a61af66fc99e Initial load
duke
parents:
diff changeset
820 // Figure out what else to write
a61af66fc99e Initial load
duke
parents:
diff changeset
821 switch (curDebugEvent->dwDebugEventCode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
822 case LOAD_DLL_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
823 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
824 ioBuf->writeAddress(curDebugEvent->u.LoadDll.lpBaseOfDll);
a61af66fc99e Initial load
duke
parents:
diff changeset
825 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
826
a61af66fc99e Initial load
duke
parents:
diff changeset
827 case UNLOAD_DLL_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
828 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
829 ioBuf->writeAddress(curDebugEvent->u.UnloadDll.lpBaseOfDll);
a61af66fc99e Initial load
duke
parents:
diff changeset
830 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
831
a61af66fc99e Initial load
duke
parents:
diff changeset
832 case EXCEPTION_DEBUG_EVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
833 {
a61af66fc99e Initial load
duke
parents:
diff changeset
834 DWORD code = curDebugEvent->u.Exception.ExceptionRecord.ExceptionCode;
a61af66fc99e Initial load
duke
parents:
diff changeset
835 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
836 ioBuf->writeUnsignedInt(code);
a61af66fc99e Initial load
duke
parents:
diff changeset
837 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
838 ioBuf->writeAddress(curDebugEvent->u.Exception.ExceptionRecord.ExceptionAddress);
a61af66fc99e Initial load
duke
parents:
diff changeset
839 switch (curDebugEvent->u.Exception.ExceptionRecord.ExceptionCode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
840 case EXCEPTION_ACCESS_VIOLATION:
a61af66fc99e Initial load
duke
parents:
diff changeset
841 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
842 ioBuf->writeBoolAsInt(curDebugEvent->u.Exception.ExceptionRecord.ExceptionInformation[0] != 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
843 ioBuf->writeSpace();
a61af66fc99e Initial load
duke
parents:
diff changeset
844 ioBuf->writeAddress((void*) curDebugEvent->u.Exception.ExceptionRecord.ExceptionInformation[1]);
a61af66fc99e Initial load
duke
parents:
diff changeset
845 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
846
a61af66fc99e Initial load
duke
parents:
diff changeset
847 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
848 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
849 }
a61af66fc99e Initial load
duke
parents:
diff changeset
850 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
851 }
a61af66fc99e Initial load
duke
parents:
diff changeset
852
a61af66fc99e Initial load
duke
parents:
diff changeset
853 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
854 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
855 }
a61af66fc99e Initial load
duke
parents:
diff changeset
856 }
a61af66fc99e Initial load
duke
parents:
diff changeset
857 eventLock->unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
858 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
859 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
860 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
861
a61af66fc99e Initial load
duke
parents:
diff changeset
862 case Message::CONTINUEEVENT:
a61af66fc99e Initial load
duke
parents:
diff changeset
863 eventLock->lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
864 if (curDebugEvent == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
865 ioBuf->writeBoolAsInt(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
866 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
867 curDebugEvent = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
868 passEventToClient = msg.boolArg.val;
a61af66fc99e Initial load
duke
parents:
diff changeset
869 ioBuf->writeBoolAsInt(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
870 eventLock->notify();
a61af66fc99e Initial load
duke
parents:
diff changeset
871 }
a61af66fc99e Initial load
duke
parents:
diff changeset
872 eventLock->unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
873 ioBuf->writeEOL();
a61af66fc99e Initial load
duke
parents:
diff changeset
874 ioBuf->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
875 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
876 }
a61af66fc99e Initial load
duke
parents:
diff changeset
877 }
a61af66fc99e Initial load
duke
parents:
diff changeset
878
a61af66fc99e Initial load
duke
parents:
diff changeset
879 endProcess();
a61af66fc99e Initial load
duke
parents:
diff changeset
880
a61af66fc99e Initial load
duke
parents:
diff changeset
881 // NOT REACHED
a61af66fc99e Initial load
duke
parents:
diff changeset
882 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
883 }