blob: b8c2b5d2109fe9221a92a1210589e138fa00e4ae [file] [log] [blame]
Eric Pouech527eea92000-03-08 16:44:54 +00001/* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3/* Wine internal debugger
4 * Interface to Windows debugger API
5 * Eric Pouech (c) 2000
6 */
7
8#include <stdlib.h>
Alexandre Julliard58d5a0a2000-07-29 21:58:17 +00009#include <stdarg.h>
Eric Pouech527eea92000-03-08 16:44:54 +000010#include <stdio.h>
Eric Poueche5efa0c2000-04-13 19:31:58 +000011#include <string.h>
Eric Pouech527eea92000-03-08 16:44:54 +000012#include "debugger.h"
Eric Pouechac11a682000-03-26 13:37:39 +000013
Alexandre Julliard09d78222001-05-05 00:44:48 +000014#include "ntddk.h"
Eric Pouechac11a682000-03-26 13:37:39 +000015#include "thread.h"
Alexandre Julliardf818d422000-05-03 17:48:21 +000016#include "wincon.h"
Eric Pouechac11a682000-03-26 13:37:39 +000017#include "wingdi.h"
18#include "winuser.h"
19
Eric Pouech527eea92000-03-08 16:44:54 +000020#include "winreg.h"
Eric Pouech527eea92000-03-08 16:44:54 +000021
Eric Pouechac11a682000-03-26 13:37:39 +000022DBG_PROCESS* DEBUG_CurrProcess = NULL;
23DBG_THREAD* DEBUG_CurrThread = NULL;
Eric Pouechf1822352000-05-11 21:43:43 +000024DWORD DEBUG_CurrTid;
25DWORD DEBUG_CurrPid;
Eric Pouechac11a682000-03-26 13:37:39 +000026CONTEXT DEBUG_context;
Eric Pouech52c75342001-05-03 18:32:47 +000027BOOL DEBUG_interactiveP = FALSE;
Eric Pouech911436b2000-06-18 19:30:24 +000028int curr_frame = 0;
29static char* DEBUG_LastCmdLine = NULL;
Eric Pouech527eea92000-03-08 16:44:54 +000030
Eric Pouech7c43b222000-06-24 12:52:13 +000031static DBG_PROCESS* DEBUG_ProcessList = NULL;
Alexandre Julliardf5a8b962001-10-22 19:00:34 +000032static int automatic_mode;
Eric Pouech04c16b82000-04-30 12:21:15 +000033DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
Eric Poueche5efa0c2000-04-13 19:31:58 +000034
Eric Pouech04c16b82000-04-30 12:21:15 +000035void DEBUG_Output(int chn, const char* buffer, int len)
36{
37 if (DBG_IVAR(ConChannelMask) & chn)
38 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
39 if (DBG_IVAR(StdChannelMask) & chn)
40 fwrite(buffer, len, 1, stderr);
41}
Eric Poueche5efa0c2000-04-13 19:31:58 +000042
43int DEBUG_Printf(int chn, const char* format, ...)
44{
Eric Pouech52c75342001-05-03 18:32:47 +000045static char buf[4*1024];
Eric Poueche5efa0c2000-04-13 19:31:58 +000046 va_list valist;
Eric Pouech04c16b82000-04-30 12:21:15 +000047 int len;
Eric Poueche5efa0c2000-04-13 19:31:58 +000048
49 va_start(valist, format);
Alexandre Julliard58d5a0a2000-07-29 21:58:17 +000050 len = vsnprintf(buf, sizeof(buf), format, valist);
Eric Poueche5efa0c2000-04-13 19:31:58 +000051 va_end(valist);
Eric Pouechb9717452000-05-05 18:14:34 +000052
53 if (len <= -1) {
54 len = sizeof(buf) - 1;
55 buf[len] = 0;
56 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
57 }
Eric Pouech04c16b82000-04-30 12:21:15 +000058 DEBUG_Output(chn, buf, len);
59 return len;
Eric Poueche5efa0c2000-04-13 19:31:58 +000060}
Eric Pouech527eea92000-03-08 16:44:54 +000061
Eric Pouech04c16b82000-04-30 12:21:15 +000062static BOOL DEBUG_IntVarsRW(int read)
Eric Pouech527eea92000-03-08 16:44:54 +000063{
Eric Pouechac11a682000-03-26 13:37:39 +000064 HKEY hkey;
Eric Pouech04c16b82000-04-30 12:21:15 +000065 DWORD type = REG_DWORD;
Eric Pouechac11a682000-03-26 13:37:39 +000066 DWORD val;
67 DWORD count = sizeof(val);
Eric Pouech04c16b82000-04-30 12:21:15 +000068 int i;
69 DBG_INTVAR* div = DEBUG_IntVars;
Eric Pouechac11a682000-03-26 13:37:39 +000070
Eric Pouech04c16b82000-04-30 12:21:15 +000071 if (read) {
72/* initializes internal vars table */
73#define INTERNAL_VAR(_var,_val,_ref,_typ) \
74 div->val = _val; div->name = #_var; div->pval = _ref; \
75 div->type = _typ; div++;
Eric Poueche5efa0c2000-04-13 19:31:58 +000076#include "intvar.h"
77#undef INTERNAL_VAR
Eric Pouech04c16b82000-04-30 12:21:15 +000078 }
79
Eric Pouechf1822352000-05-11 21:43:43 +000080 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
Eric Pouech26342c92000-05-01 14:22:02 +000081 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
82 * so don't use it */
83 fprintf(stderr, "Cannot create WineDbg key in registry\n");
84 return FALSE;
Eric Pouechac11a682000-03-26 13:37:39 +000085 }
Eric Pouech26342c92000-05-01 14:22:02 +000086
87 for (i = 0; i < DBG_IV_LAST; i++) {
88 if (read) {
89 if (!DEBUG_IntVars[i].pval) {
90 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
91 &type, (LPSTR)&val, &count))
92 DEBUG_IntVars[i].val = val;
93 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
94 } else {
95 *DEBUG_IntVars[i].pval = 0;
96 }
97 } else {
98 /* FIXME: type should be infered from basic type -if any- of intvar */
99 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
100 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
101 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
102 }
103 }
104 RegCloseKey(hkey);
Eric Pouechac11a682000-03-26 13:37:39 +0000105 return TRUE;
106}
Eric Pouech04c16b82000-04-30 12:21:15 +0000107
108DBG_INTVAR* DEBUG_GetIntVar(const char* name)
109{
110 int i;
111
112 for (i = 0; i < DBG_IV_LAST; i++) {
113 if (!strcmp(DEBUG_IntVars[i].name, name))
114 return &DEBUG_IntVars[i];
115 }
116 return NULL;
117}
Eric Pouechac11a682000-03-26 13:37:39 +0000118
119static WINE_EXCEPTION_FILTER(wine_dbg)
120{
Eric Pouechb9717452000-05-05 18:14:34 +0000121 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
Eric Pouechac11a682000-03-26 13:37:39 +0000122 DEBUG_ExternalDebugger();
Eric Pouechac11a682000-03-26 13:37:39 +0000123 return EXCEPTION_EXECUTE_HANDLER;
124}
125
Eric Pouech800773f2001-08-06 17:51:52 +0000126DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
Eric Pouechac11a682000-03-26 13:37:39 +0000127{
128 DBG_PROCESS* p;
Eric Pouech527eea92000-03-08 16:44:54 +0000129
Eric Pouech7c43b222000-06-24 12:52:13 +0000130 for (p = DEBUG_ProcessList; p; p = p->next)
Eric Pouech527eea92000-03-08 16:44:54 +0000131 if (p->pid == pid) break;
132 return p;
133}
134
Eric Pouech800773f2001-08-06 17:51:52 +0000135static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName)
Eric Pouech527eea92000-03-08 16:44:54 +0000136{
Eric Pouechac11a682000-03-26 13:37:39 +0000137 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
Eric Pouech527eea92000-03-08 16:44:54 +0000138 if (!p)
139 return NULL;
140 p->handle = h;
141 p->pid = pid;
Eric Pouech800773f2001-08-06 17:51:52 +0000142 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
Eric Pouech527eea92000-03-08 16:44:54 +0000143 p->threads = NULL;
Eric Pouechac11a682000-03-26 13:37:39 +0000144 p->num_threads = 0;
Eric Pouechb9717452000-05-05 18:14:34 +0000145 p->continue_on_first_exception = FALSE;
Eric Pouechac11a682000-03-26 13:37:39 +0000146 p->modules = NULL;
Eric Poueche9392062000-11-15 22:16:26 +0000147 p->num_modules = 0;
Eric Pouechac11a682000-03-26 13:37:39 +0000148 p->next_index = 0;
Eric Pouechb9717452000-05-05 18:14:34 +0000149 p->dbg_hdr_addr = 0;
Eric Pouecheaafb732001-05-21 18:33:15 +0000150 p->delayed_bp = NULL;
151 p->num_delayed_bp = 0;
Eric Pouech527eea92000-03-08 16:44:54 +0000152
Eric Pouech7c43b222000-06-24 12:52:13 +0000153 p->next = DEBUG_ProcessList;
Eric Pouech527eea92000-03-08 16:44:54 +0000154 p->prev = NULL;
Eric Pouech7c43b222000-06-24 12:52:13 +0000155 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
156 DEBUG_ProcessList = p;
Eric Pouech527eea92000-03-08 16:44:54 +0000157 return p;
158}
159
Eric Pouechac11a682000-03-26 13:37:39 +0000160static void DEBUG_DelThread(DBG_THREAD* p);
Eric Pouech527eea92000-03-08 16:44:54 +0000161
Eric Pouechac11a682000-03-26 13:37:39 +0000162static void DEBUG_DelProcess(DBG_PROCESS* p)
Eric Pouech527eea92000-03-08 16:44:54 +0000163{
Eric Pouecheaafb732001-05-21 18:33:15 +0000164 int i;
165
Eric Pouech527eea92000-03-08 16:44:54 +0000166 if (p->threads != NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000167 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000168 while (p->threads) DEBUG_DelThread(p->threads);
169 }
Eric Pouecheaafb732001-05-21 18:33:15 +0000170 for (i = 0; i < p->num_delayed_bp; i++) {
171 DBG_free(p->delayed_bp[i].name);
172 }
173 DBG_free(p->delayed_bp);
Eric Pouech527eea92000-03-08 16:44:54 +0000174 if (p->prev) p->prev->next = p->next;
175 if (p->next) p->next->prev = p->prev;
Eric Pouech7c43b222000-06-24 12:52:13 +0000176 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
Eric Pouech911436b2000-06-18 19:30:24 +0000177 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
Eric Pouech800773f2001-08-06 17:51:52 +0000178 DBG_free((char*)p->imageName);
Eric Pouech527eea92000-03-08 16:44:54 +0000179 DBG_free(p);
180}
181
182static void DEBUG_InitCurrProcess(void)
183{
Eric Pouech527eea92000-03-08 16:44:54 +0000184}
185
Alexandre Julliard0966a6d2000-03-10 22:25:07 +0000186static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
187{
188 DWORD sz;
189 *(WCHAR*)buffer = 0;
190 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
191}
192
193static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
Eric Pouech527eea92000-03-08 16:44:54 +0000194{
195 LPVOID ad;
196 DWORD sz;
197
198 if ( addr
199 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
200 && sz == sizeof(ad)
201 && ad
202 && ReadProcessMemory(hp, ad, buffer, size, &sz))
203 return TRUE;
204 *(WCHAR*)buffer = 0;
205 return FALSE;
206}
207
Eric Pouech800773f2001-08-06 17:51:52 +0000208DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
Eric Pouech527eea92000-03-08 16:44:54 +0000209{
Eric Pouechac11a682000-03-26 13:37:39 +0000210 DBG_THREAD* t;
Eric Pouech527eea92000-03-08 16:44:54 +0000211
212 for (t = p->threads; t; t = t->next)
213 if (t->tid == tid) break;
214 return t;
215}
216
Eric Pouechac11a682000-03-26 13:37:39 +0000217static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
218 HANDLE h, LPVOID start, LPVOID teb)
Eric Pouech527eea92000-03-08 16:44:54 +0000219{
Eric Pouechac11a682000-03-26 13:37:39 +0000220 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
Eric Pouech527eea92000-03-08 16:44:54 +0000221 if (!t)
222 return NULL;
223
224 t->handle = h;
225 t->tid = tid;
226 t->start = start;
227 t->teb = teb;
228 t->process = p;
229 t->wait_for_first_exception = 0;
230 t->dbg_exec_mode = EXEC_CONT;
231 t->dbg_exec_count = 0;
Eric Pouech2b713ba2001-04-13 22:26:23 +0000232 sprintf(t->name, "%08lx", tid);
Eric Pouechac11a682000-03-26 13:37:39 +0000233
234 p->num_threads++;
Eric Pouech527eea92000-03-08 16:44:54 +0000235 t->next = p->threads;
236 t->prev = NULL;
237 if (p->threads) p->threads->prev = t;
238 p->threads = t;
239
240 return t;
241}
242
243static void DEBUG_InitCurrThread(void)
244{
Eric Pouech527eea92000-03-08 16:44:54 +0000245 if (DEBUG_CurrThread->start) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000246 if (DEBUG_CurrThread->process->num_threads == 1 ||
247 DBG_IVAR(BreakAllThreadsStartup)) {
Eric Pouechac11a682000-03-26 13:37:39 +0000248 DBG_VALUE value;
249
250 DEBUG_SetBreakpoints(FALSE);
251 value.type = NULL;
252 value.cookie = DV_TARGET;
253 value.addr.seg = 0;
254 value.addr.off = (DWORD)DEBUG_CurrThread->start;
Eric Pouechb9717452000-05-05 18:14:34 +0000255 DEBUG_AddBreakpoint(&value, NULL);
Eric Pouechac11a682000-03-26 13:37:39 +0000256 DEBUG_SetBreakpoints(TRUE);
257 }
Eric Pouech527eea92000-03-08 16:44:54 +0000258 } else {
259 DEBUG_CurrThread->wait_for_first_exception = 1;
260 }
261}
262
Eric Pouechac11a682000-03-26 13:37:39 +0000263static void DEBUG_DelThread(DBG_THREAD* t)
Eric Pouech527eea92000-03-08 16:44:54 +0000264{
265 if (t->prev) t->prev->next = t->next;
266 if (t->next) t->next->prev = t->prev;
267 if (t == t->process->threads) t->process->threads = t->next;
Eric Pouechac11a682000-03-26 13:37:39 +0000268 t->process->num_threads--;
Eric Pouech911436b2000-06-18 19:30:24 +0000269 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
Eric Pouech527eea92000-03-08 16:44:54 +0000270 DBG_free(t);
271}
272
Eric Pouech911436b2000-06-18 19:30:24 +0000273BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
274{
Eric Pouech800773f2001-08-06 17:51:52 +0000275 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE;
Eric Pouech911436b2000-06-18 19:30:24 +0000276
277 if (!DebugActiveProcess(pid)) {
Alexandre Julliard1ce1bef2001-07-11 17:33:08 +0000278 DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError());
279 DEBUG_DelProcess(DEBUG_CurrProcess);
280 DEBUG_CurrProcess = NULL;
Eric Pouech911436b2000-06-18 19:30:24 +0000281 return FALSE;
282 }
283 DEBUG_CurrProcess->continue_on_first_exception = cofe;
284 return TRUE;
285}
286
287static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
288{
289 DBG_ADDR addr;
290 int newmode;
291
292 DEBUG_GetCurrentAddress(&addr);
293 DEBUG_SuspendExecution();
294
295 if (!is_debug) {
Alexandre Julliard954a4132000-09-24 03:15:50 +0000296 if (!addr.seg)
Eric Pouech38f2be42001-08-15 17:40:31 +0000297 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
Eric Pouech911436b2000-06-18 19:30:24 +0000298 else
Alexandre Julliard954a4132000-09-24 03:15:50 +0000299 switch(DEBUG_GetSelectorType(addr.seg))
300 {
301 case MODE_32:
Eric Pouech38f2be42001-08-15 17:40:31 +0000302 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
Alexandre Julliard954a4132000-09-24 03:15:50 +0000303 break;
304 case MODE_16:
Eric Pouech38f2be42001-08-15 17:40:31 +0000305 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
Alexandre Julliard954a4132000-09-24 03:15:50 +0000306 break;
307 case MODE_VM86:
Eric Pouech38f2be42001-08-15 17:40:31 +0000308 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
Alexandre Julliard954a4132000-09-24 03:15:50 +0000309 break;
310 case MODE_INVALID:
Eric Pouech38f2be42001-08-15 17:40:31 +0000311 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
Alexandre Julliard954a4132000-09-24 03:15:50 +0000312 break;
313 }
Eric Pouech38f2be42001-08-15 17:40:31 +0000314 DEBUG_Printf(DBG_CHN_MESG, ".\n");
Eric Pouech911436b2000-06-18 19:30:24 +0000315 }
Eric Pouech38f2be42001-08-15 17:40:31 +0000316
Eric Pouech911436b2000-06-18 19:30:24 +0000317 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
318
319 if (!force && is_debug &&
James Abbatielloebef9a92000-09-01 01:22:05 +0000320 DEBUG_ShouldContinue(&addr,
321 code,
Eric Pouech911436b2000-06-18 19:30:24 +0000322 DEBUG_CurrThread->dbg_exec_mode,
323 &DEBUG_CurrThread->dbg_exec_count))
324 return FALSE;
325
Alexandre Julliard954a4132000-09-24 03:15:50 +0000326 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
Eric Pouech911436b2000-06-18 19:30:24 +0000327 if (newmode != DEBUG_CurrThread->dbg_mode)
Alexandre Julliard954a4132000-09-24 03:15:50 +0000328 {
329 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
330 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
331 DEBUG_CurrThread->dbg_mode = newmode;
332 }
Eric Pouech911436b2000-06-18 19:30:24 +0000333
334 DEBUG_DoDisplay();
335
336 if (is_debug || force) {
337 /*
338 * Do a quiet backtrace so that we have an idea of what the situation
339 * is WRT the source files.
340 */
Eric Pouech800773f2001-08-06 17:51:52 +0000341 DEBUG_BackTrace(DEBUG_CurrTid, FALSE);
Eric Pouech911436b2000-06-18 19:30:24 +0000342 } else {
343 /* This is a real crash, dump some info */
344 DEBUG_InfoRegisters();
345 DEBUG_InfoStack();
346#ifdef __i386__
Alexandre Julliard954a4132000-09-24 03:15:50 +0000347 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
Eric Pouech911436b2000-06-18 19:30:24 +0000348 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
349 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
350 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
351 }
352 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
353#endif
Eric Pouech800773f2001-08-06 17:51:52 +0000354 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
Eric Pouech911436b2000-06-18 19:30:24 +0000355 }
356
357 if (!is_debug ||
358 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) ||
359 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) {
Eric Pouech71189b52000-07-25 12:51:56 +0000360
361 struct list_id list;
362
Eric Pouech911436b2000-06-18 19:30:24 +0000363 /* Show where we crashed */
364 curr_frame = 0;
Eric Pouech71189b52000-07-25 12:51:56 +0000365 DEBUG_DisassembleInstruction(&addr);
366
367 /* resets list internal arguments so we can look at source code when needed */
368 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
369 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
Eric Pouech911436b2000-06-18 19:30:24 +0000370 }
371 return TRUE;
372}
373
374static DWORD DEBUG_ExceptionEpilog(void)
375{
376 DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode,
377 DEBUG_CurrThread->dbg_exec_count);
378 /*
379 * This will have gotten absorbed into the breakpoint info
380 * if it was used. Otherwise it would have been ignored.
381 * In any case, we don't mess with it any more.
382 */
383 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS)
384 DEBUG_CurrThread->dbg_exec_count = 0;
385
386 return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
387}
388
389static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont)
Eric Pouech527eea92000-03-08 16:44:54 +0000390{
James Hathewayf5523592001-04-09 18:31:47 +0000391 BOOL is_debug = FALSE;
392 BOOL ret = TRUE;
393 THREADNAME_INFO *pThreadName;
394 DBG_THREAD *pThread;
395
Eric Pouech527eea92000-03-08 16:44:54 +0000396
Eric Pouech911436b2000-06-18 19:30:24 +0000397 *cont = DBG_CONTINUE;
Eric Pouech527eea92000-03-08 16:44:54 +0000398
399 switch (rec->ExceptionCode)
400 {
401 case EXCEPTION_BREAKPOINT:
402 case EXCEPTION_SINGLE_STEP:
403 is_debug = TRUE;
404 break;
James Hathewayf5523592001-04-09 18:31:47 +0000405 case EXCEPTION_NAME_THREAD:
406 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
407 if (pThreadName->dwThreadID == -1)
408 pThread = DEBUG_CurrThread;
409 else
410 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
411
412 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
413 pThread->name, 9, NULL))
414 DEBUG_Printf (DBG_CHN_MESG,
415 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
416 pThread->tid, pThread->name);
417 return TRUE;
Eric Pouech527eea92000-03-08 16:44:54 +0000418 }
419
Alexandre Julliard840434a2001-10-04 16:17:16 +0000420 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
Alexandre Julliard954a4132000-09-24 03:15:50 +0000421 {
422 /* pass exception to program except for debug exceptions */
423 *cont = is_debug ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED;
424 return TRUE;
425 }
426
Eric Pouech527eea92000-03-08 16:44:54 +0000427 if (!is_debug)
428 {
429 /* print some infos */
Eric Pouech911436b2000-06-18 19:30:24 +0000430 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
431 first_chance ? "First chance exception" : "Unhandled exception");
Eric Pouechb9717452000-05-05 18:14:34 +0000432 switch (rec->ExceptionCode)
Eric Pouech527eea92000-03-08 16:44:54 +0000433 {
434 case EXCEPTION_INT_DIVIDE_BY_ZERO:
Eric Pouech911436b2000-06-18 19:30:24 +0000435 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
Eric Pouech527eea92000-03-08 16:44:54 +0000436 break;
437 case EXCEPTION_INT_OVERFLOW:
Eric Pouech911436b2000-06-18 19:30:24 +0000438 DEBUG_Printf(DBG_CHN_MESG, "overflow");
Eric Pouech527eea92000-03-08 16:44:54 +0000439 break;
440 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
Eric Pouech911436b2000-06-18 19:30:24 +0000441 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
Eric Pouech527eea92000-03-08 16:44:54 +0000442 break;
443 case EXCEPTION_ILLEGAL_INSTRUCTION:
Eric Pouech911436b2000-06-18 19:30:24 +0000444 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
Eric Pouech527eea92000-03-08 16:44:54 +0000445 break;
446 case EXCEPTION_STACK_OVERFLOW:
Eric Pouech911436b2000-06-18 19:30:24 +0000447 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
Eric Pouech527eea92000-03-08 16:44:54 +0000448 break;
449 case EXCEPTION_PRIV_INSTRUCTION:
Andreas Mohre15badb2001-10-21 15:18:15 +0000450 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
Eric Pouech527eea92000-03-08 16:44:54 +0000451 break;
452 case EXCEPTION_ACCESS_VIOLATION:
453 if (rec->NumberParameters == 2)
Eric Pouech911436b2000-06-18 19:30:24 +0000454 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
Eric Poueche5efa0c2000-04-13 19:31:58 +0000455 rec->ExceptionInformation[0] ? "write" : "read",
Eric Pouech911436b2000-06-18 19:30:24 +0000456 rec->ExceptionInformation[1]);
Eric Pouech527eea92000-03-08 16:44:54 +0000457 else
Eric Pouech911436b2000-06-18 19:30:24 +0000458 DEBUG_Printf(DBG_CHN_MESG, "page fault");
Eric Pouech527eea92000-03-08 16:44:54 +0000459 break;
460 case EXCEPTION_DATATYPE_MISALIGNMENT:
Eric Pouech911436b2000-06-18 19:30:24 +0000461 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
Eric Pouech527eea92000-03-08 16:44:54 +0000462 break;
Eric Pouechd0a04932001-11-23 23:10:08 +0000463 case DBG_CONTROL_C:
464 DEBUG_Printf(DBG_CHN_MESG, "^C");
465 break;
Eric Pouech527eea92000-03-08 16:44:54 +0000466 case CONTROL_C_EXIT:
Eric Pouech911436b2000-06-18 19:30:24 +0000467 DEBUG_Printf(DBG_CHN_MESG, "^C");
Eric Pouech527eea92000-03-08 16:44:54 +0000468 break;
469 case EXCEPTION_CRITICAL_SECTION_WAIT:
Eric Pouechc2b5cb32001-08-07 19:32:14 +0000470 {
471 DBG_ADDR addr;
472
473 addr.seg = 0;
474 addr.off = rec->ExceptionInformation[0];
475
476 DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section ");
477 DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE);
478 }
Eric Pouech04c16b82000-04-30 12:21:15 +0000479 if (!DBG_IVAR(BreakOnCritSectTimeOut))
Andreas Mohr20cd9352000-09-12 23:40:40 +0000480 {
481 DEBUG_Printf(DBG_CHN_MESG, "\n");
Eric Pouech911436b2000-06-18 19:30:24 +0000482 return TRUE;
Andreas Mohr20cd9352000-09-12 23:40:40 +0000483 }
Eric Pouech527eea92000-03-08 16:44:54 +0000484 break;
Alexandre Julliard634824b2000-10-26 22:03:34 +0000485 case EXCEPTION_WINE_STUB:
486 {
487 char dll[32], name[64];
488 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
489 (char *)rec->ExceptionInformation[0] );
490 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
491 (char *)rec->ExceptionInformation[1] );
492 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
493 }
494 break;
Alexandre Julliard954a4132000-09-24 03:15:50 +0000495 case EXCEPTION_VM86_INTx:
496 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
497 rec->ExceptionInformation[0]);
498 break;
499 case EXCEPTION_VM86_STI:
500 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
501 break;
502 case EXCEPTION_VM86_PICRETURN:
503 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
504 break;
Eric Pouech527eea92000-03-08 16:44:54 +0000505 default:
Eric Pouech911436b2000-06-18 19:30:24 +0000506 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
Eric Pouech527eea92000-03-08 16:44:54 +0000507 break;
508 }
509 }
510
Eric Pouech38f2be42001-08-15 17:40:31 +0000511#if 0
Eric Poueche5efa0c2000-04-13 19:31:58 +0000512 DEBUG_Printf(DBG_CHN_TRACE,
513 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000514#ifdef __i386__
Eric Poueche5efa0c2000-04-13 19:31:58 +0000515 DEBUG_context.Eip, DEBUG_context.EFlags,
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000516#else
517 0L, 0L,
518#endif
Eric Poueche5efa0c2000-04-13 19:31:58 +0000519 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
Eric Pouech38f2be42001-08-15 17:40:31 +0000520#endif
Eric Pouech527eea92000-03-08 16:44:54 +0000521
Alexandre Julliardf5a8b962001-10-22 19:00:34 +0000522 if (automatic_mode)
523 {
524 DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode);
525 return FALSE; /* terminate execution */
526 }
527
Eric Pouech911436b2000-06-18 19:30:24 +0000528 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
Eric Pouech52c75342001-05-03 18:32:47 +0000529 DEBUG_interactiveP = TRUE;
Eric Pouech5b471ba2000-06-25 12:49:13 +0000530 while ((ret = DEBUG_Parser())) {
531 if (DEBUG_ValidateRegisters()) {
532 if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance)
533 break;
534 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
535 }
Eric Pouech911436b2000-06-18 19:30:24 +0000536 }
Eric Pouech52c75342001-05-03 18:32:47 +0000537 DEBUG_interactiveP = FALSE;
Eric Pouech911436b2000-06-18 19:30:24 +0000538 }
539 *cont = DEBUG_ExceptionEpilog();
Eric Poueche5efa0c2000-04-13 19:31:58 +0000540
Eric Pouech38f2be42001-08-15 17:40:31 +0000541#if 0
Eric Poueche5efa0c2000-04-13 19:31:58 +0000542 DEBUG_Printf(DBG_CHN_TRACE,
543 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000544#ifdef __i386__
Eric Poueche5efa0c2000-04-13 19:31:58 +0000545 DEBUG_context.Eip, DEBUG_context.EFlags,
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000546#else
547 0L, 0L,
548#endif
Eric Poueche5efa0c2000-04-13 19:31:58 +0000549 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
Eric Pouech38f2be42001-08-15 17:40:31 +0000550#endif
Eric Pouech527eea92000-03-08 16:44:54 +0000551
552 return ret;
553}
554
Eric Poueche5efa0c2000-04-13 19:31:58 +0000555static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
Eric Pouech527eea92000-03-08 16:44:54 +0000556{
Eric Pouech527eea92000-03-08 16:44:54 +0000557 char buffer[256];
Eric Poueche5efa0c2000-04-13 19:31:58 +0000558 BOOL ret;
Eric Pouech911436b2000-06-18 19:30:24 +0000559
Eric Pouechf1822352000-05-11 21:43:43 +0000560 DEBUG_CurrPid = de->dwProcessId;
561 DEBUG_CurrTid = de->dwThreadId;
562
Eric Pouechac11a682000-03-26 13:37:39 +0000563 __TRY {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000564 ret = TRUE;
565 *cont = 0L;
Eric Pouechac11a682000-03-26 13:37:39 +0000566
567 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
568 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
Eric Pouech527eea92000-03-08 16:44:54 +0000569 else
570 DEBUG_CurrThread = NULL;
Eric Pouechac11a682000-03-26 13:37:39 +0000571
572 switch (de->dwDebugEventCode) {
Eric Pouech527eea92000-03-08 16:44:54 +0000573 case EXCEPTION_DEBUG_EVENT:
Eric Poueche5efa0c2000-04-13 19:31:58 +0000574 if (!DEBUG_CurrThread) {
575 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
576 de->dwProcessId, de->dwThreadId);
577 break;
578 }
Eric Pouechac11a682000-03-26 13:37:39 +0000579
Eric Pouechb9717452000-05-05 18:14:34 +0000580 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
Eric Poueche5efa0c2000-04-13 19:31:58 +0000581 de->dwProcessId, de->dwThreadId,
Eric Pouechb9717452000-05-05 18:14:34 +0000582 de->u.Exception.ExceptionRecord.ExceptionCode);
583
584 if (DEBUG_CurrProcess->continue_on_first_exception) {
585 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
586 if (!DBG_IVAR(BreakOnAttach)) {
587 *cont = DBG_CONTINUE;
588 break;
589 }
590 }
591
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000592 DEBUG_context.ContextFlags = CONTEXT_CONTROL
593 | CONTEXT_INTEGER
594#ifdef CONTEXT_SEGMENTS
595 | CONTEXT_SEGMENTS
596#endif
597#ifdef CONTEXT_DEBUG_REGISTERS
598 | CONTEXT_DEBUG_REGISTERS
599#endif
600 ;
601
Eric Pouech527eea92000-03-08 16:44:54 +0000602 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000603 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000604 break;
605 }
Eric Pouechac11a682000-03-26 13:37:39 +0000606
Eric Pouech911436b2000-06-18 19:30:24 +0000607 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
608 de->u.Exception.dwFirstChance,
609 DEBUG_CurrThread->wait_for_first_exception,
610 cont);
611 if (DEBUG_CurrThread) {
Eric Pouech527eea92000-03-08 16:44:54 +0000612 DEBUG_CurrThread->wait_for_first_exception = 0;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000613 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
Eric Pouech527eea92000-03-08 16:44:54 +0000614 }
Eric Pouech527eea92000-03-08 16:44:54 +0000615 break;
616
617 case CREATE_THREAD_DEBUG_EVENT:
Eric Pouechf1822352000-05-11 21:43:43 +0000618 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
619 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
Eric Pouech527eea92000-03-08 16:44:54 +0000620
621 if (DEBUG_CurrProcess == NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000622 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000623 break;
624 }
Eric Pouechac11a682000-03-26 13:37:39 +0000625 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000626 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000627 break;
628 }
Eric Pouechac11a682000-03-26 13:37:39 +0000629
Eric Pouech527eea92000-03-08 16:44:54 +0000630 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
Eric Pouechac11a682000-03-26 13:37:39 +0000631 de->dwThreadId,
632 de->u.CreateThread.hThread,
633 de->u.CreateThread.lpStartAddress,
634 de->u.CreateThread.lpThreadLocalBase);
Eric Pouech527eea92000-03-08 16:44:54 +0000635 if (!DEBUG_CurrThread) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000636 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000637 break;
638 }
639 DEBUG_InitCurrThread();
640 break;
641
642 case CREATE_PROCESS_DEBUG_EVENT:
Alexandre Julliard0966a6d2000-03-10 22:25:07 +0000643 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
Eric Pouechac11a682000-03-26 13:37:39 +0000644 de->u.CreateProcessInfo.hProcess,
Eric Poueche5efa0c2000-04-13 19:31:58 +0000645 de->u.CreateProcessInfo.lpImageName);
Eric Pouech800773f2001-08-06 17:51:52 +0000646
Eric Pouechac11a682000-03-26 13:37:39 +0000647 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
Eric Pouech800773f2001-08-06 17:51:52 +0000648 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
Eric Poueche5efa0c2000-04-13 19:31:58 +0000649 de->dwProcessId, de->dwThreadId,
Eric Pouech800773f2001-08-06 17:51:52 +0000650 buffer, de->u.CreateProcessInfo.lpImageName,
Eric Pouechf1822352000-05-11 21:43:43 +0000651 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
Eric Poueche5efa0c2000-04-13 19:31:58 +0000652 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
653 de->u.CreateProcessInfo.nDebugInfoSize);
Eric Pouech527eea92000-03-08 16:44:54 +0000654
Eric Pouechb9717452000-05-05 18:14:34 +0000655 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
656 if (DEBUG_CurrProcess->handle) {
657 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
658 break;
659 }
660 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
Eric Pouech800773f2001-08-06 17:51:52 +0000661 if (DEBUG_CurrProcess->imageName == NULL)
662 DEBUG_CurrProcess->imageName = DBG_strdup(buffer[0] ? buffer : "<Debugged Process>");
663
Eric Pouechb9717452000-05-05 18:14:34 +0000664 } else {
665 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
Eric Pouech800773f2001-08-06 17:51:52 +0000666 de->u.CreateProcessInfo.hProcess,
667 buffer[0] ? buffer : "<Debugged Process>");
Eric Pouechb9717452000-05-05 18:14:34 +0000668 if (DEBUG_CurrProcess == NULL) {
669 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
670 break;
671 }
Eric Pouech527eea92000-03-08 16:44:54 +0000672 }
673
Eric Pouechf1822352000-05-11 21:43:43 +0000674 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
Eric Poueche5efa0c2000-04-13 19:31:58 +0000675 de->dwProcessId, de->dwThreadId,
Eric Pouechf1822352000-05-11 21:43:43 +0000676 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
Eric Pouech527eea92000-03-08 16:44:54 +0000677
678 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
Eric Pouechac11a682000-03-26 13:37:39 +0000679 de->dwThreadId,
680 de->u.CreateProcessInfo.hThread,
681 de->u.CreateProcessInfo.lpStartAddress,
682 de->u.CreateProcessInfo.lpThreadLocalBase);
Eric Pouech527eea92000-03-08 16:44:54 +0000683 if (!DEBUG_CurrThread) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000684 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000685 break;
686 }
687
688 DEBUG_InitCurrProcess();
689 DEBUG_InitCurrThread();
Alexandre Julliard291fa662000-06-08 05:02:19 +0000690
Eric Pouech800773f2001-08-06 17:51:52 +0000691 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
Eric Poueche5efa0c2000-04-13 19:31:58 +0000692 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
Alexandre Julliard291fa662000-06-08 05:02:19 +0000693
694 if (buffer[0]) /* we got a process name */
695 {
696 DWORD type;
697 if (!GetBinaryTypeA( buffer, &type ))
698 {
699 /* not a Windows binary, assume it's a Unix executable then */
Alexandre Julliard633293f2000-12-06 03:20:47 +0000700 char unixname[MAX_PATH];
Alexandre Julliard291fa662000-06-08 05:02:19 +0000701 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
Alexandre Julliard633293f2000-12-06 03:20:47 +0000702 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
Alexandre Julliard291fa662000-06-08 05:02:19 +0000703 {
Alexandre Julliard633293f2000-12-06 03:20:47 +0000704 DEBUG_ReadExecutableDbgInfo( unixname );
Alexandre Julliard291fa662000-06-08 05:02:19 +0000705 break;
706 }
707 }
708 }
709 /* if it is a Windows binary, or an invalid or missing file name,
710 * we use wine itself as the main executable */
711 DEBUG_ReadExecutableDbgInfo( "wine" );
Eric Pouech527eea92000-03-08 16:44:54 +0000712 break;
713
714 case EXIT_THREAD_DEBUG_EVENT:
Eric Poueche5efa0c2000-04-13 19:31:58 +0000715 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
716 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
Eric Pouechac11a682000-03-26 13:37:39 +0000717
Eric Pouech527eea92000-03-08 16:44:54 +0000718 if (DEBUG_CurrThread == NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000719 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000720 break;
721 }
722 /* FIXME: remove break point set on thread startup */
723 DEBUG_DelThread(DEBUG_CurrThread);
724 break;
725
726 case EXIT_PROCESS_DEBUG_EVENT:
Eric Poueche5efa0c2000-04-13 19:31:58 +0000727 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
728 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
Eric Pouechac11a682000-03-26 13:37:39 +0000729
Eric Pouech527eea92000-03-08 16:44:54 +0000730 if (DEBUG_CurrProcess == NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000731 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000732 break;
733 }
Eric Poueche5efa0c2000-04-13 19:31:58 +0000734 /* just in case */
735 DEBUG_SetBreakpoints(FALSE);
Eric Pouech527eea92000-03-08 16:44:54 +0000736 /* kill last thread */
737 DEBUG_DelThread(DEBUG_CurrProcess->threads);
Eric Pouech527eea92000-03-08 16:44:54 +0000738 DEBUG_DelProcess(DEBUG_CurrProcess);
Eric Pouech911436b2000-06-18 19:30:24 +0000739
740 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
Eric Pouech527eea92000-03-08 16:44:54 +0000741 break;
742
743 case LOAD_DLL_DEBUG_EVENT:
744 if (DEBUG_CurrThread == NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000745 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000746 break;
747 }
Alexandre Julliard0966a6d2000-03-10 22:25:07 +0000748 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
749 DEBUG_CurrThread->process->handle,
Eric Pouechac11a682000-03-26 13:37:39 +0000750 de->u.LoadDll.lpImageName);
Eric Pouech527eea92000-03-08 16:44:54 +0000751
Eric Pouechac11a682000-03-26 13:37:39 +0000752 /* FIXME unicode: de->u.LoadDll.fUnicode */
Eric Pouechf1822352000-05-11 21:43:43 +0000753 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
Eric Poueche5efa0c2000-04-13 19:31:58 +0000754 de->dwProcessId, de->dwThreadId,
Eric Pouechf1822352000-05-11 21:43:43 +0000755 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
Eric Poueche5efa0c2000-04-13 19:31:58 +0000756 de->u.LoadDll.dwDebugInfoFileOffset,
757 de->u.LoadDll.nDebugInfoSize);
Alexandre Julliard09d78222001-05-05 00:44:48 +0000758 _strupr(buffer);
Eric Poueche5efa0c2000-04-13 19:31:58 +0000759 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
Eric Pouecheaafb732001-05-21 18:33:15 +0000760 DEBUG_CheckDelayedBP();
Eric Pouechf516be62000-07-10 13:05:44 +0000761 if (DBG_IVAR(BreakOnDllLoad)) {
762 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
763 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
764 ret = DEBUG_Parser();
765 }
Eric Pouech527eea92000-03-08 16:44:54 +0000766 break;
767
768 case UNLOAD_DLL_DEBUG_EVENT:
Eric Pouechf1822352000-05-11 21:43:43 +0000769 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
770 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
Eric Pouech527eea92000-03-08 16:44:54 +0000771 break;
772
773 case OUTPUT_DEBUG_STRING_EVENT:
774 if (DEBUG_CurrThread == NULL) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000775 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
Eric Pouech527eea92000-03-08 16:44:54 +0000776 break;
777 }
Eric Pouechac11a682000-03-26 13:37:39 +0000778
Eric Pouech527eea92000-03-08 16:44:54 +0000779 DEBUG_ProcessGetString(buffer, sizeof(buffer),
780 DEBUG_CurrThread->process->handle,
Eric Pouechac11a682000-03-26 13:37:39 +0000781 de->u.DebugString.lpDebugStringData);
Eric Pouech527eea92000-03-08 16:44:54 +0000782
Eric Pouechac11a682000-03-26 13:37:39 +0000783 /* fixme unicode de->u.DebugString.fUnicode ? */
Eric Poueche5efa0c2000-04-13 19:31:58 +0000784 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
785 de->dwProcessId, de->dwThreadId, buffer);
Eric Pouech527eea92000-03-08 16:44:54 +0000786 break;
787
788 case RIP_EVENT:
Eric Poueche5efa0c2000-04-13 19:31:58 +0000789 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
790 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
791 de->u.RipInfo.dwType);
Eric Pouech527eea92000-03-08 16:44:54 +0000792 break;
793
794 default:
Eric Poueche5efa0c2000-04-13 19:31:58 +0000795 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
796 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
Eric Pouech527eea92000-03-08 16:44:54 +0000797 }
Eric Pouechac11a682000-03-26 13:37:39 +0000798
799 } __EXCEPT(wine_dbg) {
Eric Poueche5efa0c2000-04-13 19:31:58 +0000800 *cont = 0;
801 ret = TRUE;
Eric Pouechac11a682000-03-26 13:37:39 +0000802 }
803 __ENDTRY;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000804 return ret;
Eric Pouechac11a682000-03-26 13:37:39 +0000805}
806
Eric Pouech911436b2000-06-18 19:30:24 +0000807static DWORD DEBUG_MainLoop(void)
Eric Pouechac11a682000-03-26 13:37:39 +0000808{
809 DEBUG_EVENT de;
810 DWORD cont;
Eric Pouech911436b2000-06-18 19:30:24 +0000811 BOOL ret;
Eric Pouechac11a682000-03-26 13:37:39 +0000812
Alexandre Julliard954a4132000-09-24 03:15:50 +0000813 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
Eric Poueche5efa0c2000-04-13 19:31:58 +0000814
Eric Pouech911436b2000-06-18 19:30:24 +0000815 for (ret = TRUE; ret; ) {
816 /* wait until we get at least one loaded process */
Eric Pouech7c43b222000-06-24 12:52:13 +0000817 while (!DEBUG_ProcessList && (ret = DEBUG_Parser()));
Eric Pouech911436b2000-06-18 19:30:24 +0000818 if (!ret) break;
819
Eric Pouech7c43b222000-06-24 12:52:13 +0000820 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) {
Eric Pouech911436b2000-06-18 19:30:24 +0000821 ret = DEBUG_HandleDebugEvent(&de, &cont);
822 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
823 }
824 };
Eric Pouech527eea92000-03-08 16:44:54 +0000825
Alexandre Julliard954a4132000-09-24 03:15:50 +0000826 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
Eric Pouech04c16b82000-04-30 12:21:15 +0000827
828 return 0;
Eric Pouech527eea92000-03-08 16:44:54 +0000829}
830
Alexandre Julliardf5a8b962001-10-22 19:00:34 +0000831static DWORD DEBUG_AutoMode(void)
832{
833 DEBUG_EVENT de;
834 DWORD cont;
835 BOOL ret = TRUE;
836
837 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
838
839 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE))
840 {
841 ret = DEBUG_HandleDebugEvent(&de, &cont);
842 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
843 }
844 /* print some extra information */
845 DEBUG_Printf(DBG_CHN_MESG, "Modules:\n");
846 DEBUG_WalkModules();
847 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
848 DEBUG_WalkThreads();
849
850 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
851 return 0;
852}
853
Eric Pouech911436b2000-06-18 19:30:24 +0000854static BOOL DEBUG_Start(LPSTR cmdLine)
855{
856 PROCESS_INFORMATION info;
857 STARTUPINFOA startup;
858
859 memset(&startup, 0, sizeof(startup));
860 startup.cb = sizeof(startup);
861 startup.dwFlags = STARTF_USESHOWWINDOW;
862 startup.wShowWindow = SW_SHOWNORMAL;
863
864 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
Eric Pouechd0a04932001-11-23 23:10:08 +0000865 FALSE, DEBUG_PROCESS|DETACHED_PROCESS, NULL, NULL, &startup, &info)) {
Eric Pouech911436b2000-06-18 19:30:24 +0000866 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
867 return FALSE;
868 }
869 DEBUG_CurrPid = info.dwProcessId;
Eric Pouech800773f2001-08-06 17:51:52 +0000870 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
Eric Pouech911436b2000-06-18 19:30:24 +0000871
872 return TRUE;
873}
874
875void DEBUG_Run(const char* args)
876{
877 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
878 const char* pgm = (wmod) ? wmod->module_name : "none";
879
880 if (args) {
881 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
882 } else {
883 if (!DEBUG_LastCmdLine) {
884 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
885 return;
886 }
887 DEBUG_Start(DEBUG_LastCmdLine);
888 }
889}
890
Eric Pouechd0a04932001-11-23 23:10:08 +0000891static void DEBUG_InitConsole(void)
892{
893 COORD c;
894 SMALL_RECT sr;
895 DWORD mode;
896
897 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
898 * stdout and stderr streams
899 */
900 if (DBG_IVAR(UseXTerm))
901 {
902 FreeConsole();
903 AllocConsole();
904 }
905 /* this would be nicer for output */
906 c.X = 132;
907 c.Y = 500;
908 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c);
909
910 /* sets the console's window width accordingly */
911 sr.Left = 0;
912 sr.Top = 0;
913 sr.Right = c.X - 1;
914 sr.Bottom = 50;
915 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &sr);
916
917 /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */
918 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode))
919 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode | WINE_ENABLE_LINE_INPUT_EMACS);
920}
921
Eric Pouech04c16b82000-04-30 12:21:15 +0000922int DEBUG_main(int argc, char** argv)
Eric Pouech527eea92000-03-08 16:44:54 +0000923{
Eric Pouech911436b2000-06-18 19:30:24 +0000924 DWORD retv = 0;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000925
Eric Pouech04c16b82000-04-30 12:21:15 +0000926 /* Initialize the type handling stuff. */
927 DEBUG_InitTypes();
928 DEBUG_InitCVDataTypes();
Eric Pouech527eea92000-03-08 16:44:54 +0000929
Eric Pouech800773f2001-08-06 17:51:52 +0000930 /* Initialize internal vars (types must have been initialized before) */
Eric Pouech26342c92000-05-01 14:22:02 +0000931 if (!DEBUG_IntVarsRW(TRUE)) return -1;
Eric Pouech04c16b82000-04-30 12:21:15 +0000932
Alexandre Julliardf5a8b962001-10-22 19:00:34 +0000933 if (argc > 1 && !strcmp( argv[1], "--auto" ))
934 {
935 argc--;
936 argv++;
937 automatic_mode = 1;
938 /* force some internal variables */
939 DBG_IVAR(UseXTerm) = 0;
940 DBG_IVAR(BreakOnDllLoad) = 0;
941 DBG_IVAR(ConChannelMask) = 0;
942 DBG_IVAR(StdChannelMask) = DBG_CHN_MESG;
943 }
944
Eric Pouechd0a04932001-11-23 23:10:08 +0000945 DEBUG_InitConsole();
946
Andreas Mohra59324d2000-11-25 01:40:10 +0000947 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... ");
Eric Pouech52c75342001-05-03 18:32:47 +0000948
Eric Pouech04c16b82000-04-30 12:21:15 +0000949 if (argc == 3) {
950 HANDLE hEvent;
Eric Pouech911436b2000-06-18 19:30:24 +0000951 DWORD pid;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000952
François Gougetd5042c42000-12-29 05:38:00 +0000953 if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) {
Eric Pouech2b713ba2001-04-13 22:26:23 +0000954 if (!DEBUG_Attach(pid, TRUE)) {
Eric Pouech2b713ba2001-04-13 22:26:23 +0000955 /* don't care about result */
956 SetEvent(hEvent);
Eric Pouechf1822352000-05-11 21:43:43 +0000957 goto leave;
958 }
Eric Pouech2b713ba2001-04-13 22:26:23 +0000959 if (!SetEvent(hEvent)) {
960 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
961 goto leave;
962 }
963 CloseHandle(hEvent);
Eric Pouech911436b2000-06-18 19:30:24 +0000964 DEBUG_CurrPid = pid;
Eric Pouechf1822352000-05-11 21:43:43 +0000965 }
966 }
967
Eric Pouech911436b2000-06-18 19:30:24 +0000968 if (DEBUG_CurrPid == 0 && argc > 1) {
969 int i, len;
970 LPSTR cmdLine;
971
972 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
973 cmdLine[0] = '\0';
Eric Poueche5efa0c2000-04-13 19:31:58 +0000974
Eric Pouech911436b2000-06-18 19:30:24 +0000975 for (i = 1; i < argc; i++) {
976 len += strlen(argv[i]) + 1;
977 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
978 strcat(cmdLine, argv[i]);
979 cmdLine[len - 2] = ' ';
980 cmdLine[len - 1] = '\0';
981 }
Eric Poueche5efa0c2000-04-13 19:31:58 +0000982
Eric Pouech911436b2000-06-18 19:30:24 +0000983 if (!DEBUG_Start(cmdLine)) {
984 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
Eric Pouech04c16b82000-04-30 12:21:15 +0000985 goto leave;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000986 }
Eric Pouech911436b2000-06-18 19:30:24 +0000987 DBG_free(DEBUG_LastCmdLine);
988 DEBUG_LastCmdLine = cmdLine;
Eric Pouech04c16b82000-04-30 12:21:15 +0000989 }
990
Alexandre Julliardf5a8b962001-10-22 19:00:34 +0000991 if (automatic_mode)
992 {
993 retv = DEBUG_AutoMode();
994 /* don't save modified variables in auto mode */
995 }
996 else
997 {
998 retv = DEBUG_MainLoop();
999 /* saves modified variables */
1000 DEBUG_IntVarsRW(FALSE);
1001 }
Eric Pouech04c16b82000-04-30 12:21:15 +00001002
Eric Pouech800773f2001-08-06 17:51:52 +00001003 leave:
Eric Pouech04c16b82000-04-30 12:21:15 +00001004 return retv;
Eric Pouech911436b2000-06-18 19:30:24 +00001005
1006 oom_leave:
1007 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
1008 goto leave;
Eric Pouechcbb7a172000-03-09 18:45:43 +00001009}