Implemented DirectDraw's Hardware Abstraction Layer (HAL) interface.
[wine/hacks.git] / debugger / winedbg.c
blob686b587e945e013600565cf9f0a0f1adc2efddfd
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
5 * Eric Pouech (c) 2000
6 */
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "debugger.h"
14 #include "thread.h"
15 #include "file.h"
16 #include "wincon.h"
17 #include "wingdi.h"
18 #include "winuser.h"
20 #include "winreg.h"
22 #ifdef DBG_need_heap
23 HANDLE dbg_heap = 0;
24 #endif
26 DBG_PROCESS* DEBUG_CurrProcess = NULL;
27 DBG_THREAD* DEBUG_CurrThread = NULL;
28 DWORD DEBUG_CurrTid;
29 DWORD DEBUG_CurrPid;
30 CONTEXT DEBUG_context;
31 int curr_frame = 0;
32 static char* DEBUG_LastCmdLine = NULL;
34 static DBG_PROCESS* DEBUG_ProcessList = NULL;
35 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
37 void DEBUG_Output(int chn, const char* buffer, int len)
39 if (DBG_IVAR(ConChannelMask) & chn)
40 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
41 if (DBG_IVAR(StdChannelMask) & chn)
42 fwrite(buffer, len, 1, stderr);
45 int DEBUG_Printf(int chn, const char* format, ...)
47 char buf[1024];
48 va_list valist;
49 int len;
51 va_start(valist, format);
52 len = vsnprintf(buf, sizeof(buf), format, valist);
53 va_end(valist);
55 if (len <= -1) {
56 len = sizeof(buf) - 1;
57 buf[len] = 0;
58 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
60 DEBUG_Output(chn, buf, len);
61 return len;
64 static BOOL DEBUG_IntVarsRW(int read)
66 HKEY hkey;
67 DWORD type = REG_DWORD;
68 DWORD val;
69 DWORD count = sizeof(val);
70 int i;
71 DBG_INTVAR* div = DEBUG_IntVars;
73 if (read) {
74 /* initializes internal vars table */
75 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
76 div->val = _val; div->name = #_var; div->pval = _ref; \
77 div->type = _typ; div++;
78 #include "intvar.h"
79 #undef INTERNAL_VAR
82 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
83 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
84 * so don't use it */
85 fprintf(stderr, "Cannot create WineDbg key in registry\n");
86 return FALSE;
89 for (i = 0; i < DBG_IV_LAST; i++) {
90 if (read) {
91 if (!DEBUG_IntVars[i].pval) {
92 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
93 &type, (LPSTR)&val, &count))
94 DEBUG_IntVars[i].val = val;
95 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
96 } else {
97 *DEBUG_IntVars[i].pval = 0;
99 } else {
100 /* FIXME: type should be infered from basic type -if any- of intvar */
101 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
102 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
103 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
106 RegCloseKey(hkey);
107 return TRUE;
110 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
112 int i;
114 for (i = 0; i < DBG_IV_LAST; i++) {
115 if (!strcmp(DEBUG_IntVars[i].name, name))
116 return &DEBUG_IntVars[i];
118 return NULL;
121 static WINE_EXCEPTION_FILTER(wine_dbg)
123 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
124 DEBUG_ExternalDebugger();
125 return EXCEPTION_EXECUTE_HANDLER;
128 static DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
130 DBG_PROCESS* p;
132 for (p = DEBUG_ProcessList; p; p = p->next)
133 if (p->pid == pid) break;
134 return p;
137 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h)
139 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
140 if (!p)
141 return NULL;
142 p->handle = h;
143 p->pid = pid;
144 p->threads = NULL;
145 p->num_threads = 0;
146 p->continue_on_first_exception = FALSE;
147 p->modules = NULL;
148 p->num_modules = 0;
149 p->next_index = 0;
150 p->dbg_hdr_addr = 0;
152 p->next = DEBUG_ProcessList;
153 p->prev = NULL;
154 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
155 DEBUG_ProcessList = p;
156 return p;
159 static void DEBUG_DelThread(DBG_THREAD* p);
161 static void DEBUG_DelProcess(DBG_PROCESS* p)
163 if (p->threads != NULL) {
164 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
165 while (p->threads) DEBUG_DelThread(p->threads);
167 if (p->prev) p->prev->next = p->next;
168 if (p->next) p->next->prev = p->prev;
169 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
170 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
171 DBG_free(p);
174 static void DEBUG_InitCurrProcess(void)
178 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
180 DWORD sz;
181 *(WCHAR*)buffer = 0;
182 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
185 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
187 LPVOID ad;
188 DWORD sz;
190 if ( addr
191 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
192 && sz == sizeof(ad)
193 && ad
194 && ReadProcessMemory(hp, ad, buffer, size, &sz))
195 return TRUE;
196 *(WCHAR*)buffer = 0;
197 return FALSE;
200 static DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
202 DBG_THREAD* t;
204 for (t = p->threads; t; t = t->next)
205 if (t->tid == tid) break;
206 return t;
209 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
210 HANDLE h, LPVOID start, LPVOID teb)
212 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
213 if (!t)
214 return NULL;
216 t->handle = h;
217 t->tid = tid;
218 t->start = start;
219 t->teb = teb;
220 t->process = p;
221 t->wait_for_first_exception = 0;
222 t->dbg_exec_mode = EXEC_CONT;
223 t->dbg_exec_count = 0;
224 sprintf(t->name, "%08lx", tid);
226 p->num_threads++;
227 t->next = p->threads;
228 t->prev = NULL;
229 if (p->threads) p->threads->prev = t;
230 p->threads = t;
232 return t;
235 static void DEBUG_InitCurrThread(void)
237 if (DEBUG_CurrThread->start) {
238 if (DEBUG_CurrThread->process->num_threads == 1 ||
239 DBG_IVAR(BreakAllThreadsStartup)) {
240 DBG_VALUE value;
242 DEBUG_SetBreakpoints(FALSE);
243 value.type = NULL;
244 value.cookie = DV_TARGET;
245 value.addr.seg = 0;
246 value.addr.off = (DWORD)DEBUG_CurrThread->start;
247 DEBUG_AddBreakpoint(&value, NULL);
248 DEBUG_SetBreakpoints(TRUE);
250 } else {
251 DEBUG_CurrThread->wait_for_first_exception = 1;
255 static void DEBUG_DelThread(DBG_THREAD* t)
257 if (t->prev) t->prev->next = t->next;
258 if (t->next) t->next->prev = t->prev;
259 if (t == t->process->threads) t->process->threads = t->next;
260 t->process->num_threads--;
261 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
262 DBG_free(t);
265 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
267 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) return FALSE;
269 if (!DebugActiveProcess(pid)) {
270 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
271 pid, GetLastError());
272 return FALSE;
274 DEBUG_CurrProcess->continue_on_first_exception = cofe;
275 return TRUE;
278 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
280 DBG_ADDR addr;
281 int newmode;
283 DEBUG_GetCurrentAddress(&addr);
284 DEBUG_SuspendExecution();
286 if (!is_debug) {
287 if (!addr.seg)
288 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx).\n", addr.off);
289 else
290 switch(DEBUG_GetSelectorType(addr.seg))
292 case MODE_32:
293 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx).\n", addr.seg, addr.off);
294 break;
295 case MODE_16:
296 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx).\n", addr.seg, addr.off);
297 break;
298 case MODE_VM86:
299 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx).\n", addr.seg, addr.off);
300 break;
301 case MODE_INVALID:
302 DEBUG_Printf(DBG_CHN_MESG, "bad CS (%lx)\n", addr.seg);
303 break;
307 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
309 if (!force && is_debug &&
310 DEBUG_ShouldContinue(&addr,
311 code,
312 DEBUG_CurrThread->dbg_exec_mode,
313 &DEBUG_CurrThread->dbg_exec_count))
314 return FALSE;
316 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
317 if (newmode != DEBUG_CurrThread->dbg_mode)
319 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
320 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
321 DEBUG_CurrThread->dbg_mode = newmode;
324 DEBUG_DoDisplay();
326 if (is_debug || force) {
328 * Do a quiet backtrace so that we have an idea of what the situation
329 * is WRT the source files.
331 DEBUG_BackTrace(FALSE);
332 } else {
333 /* This is a real crash, dump some info */
334 DEBUG_InfoRegisters();
335 DEBUG_InfoStack();
336 #ifdef __i386__
337 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
338 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
339 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
340 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
342 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
343 #endif
344 DEBUG_BackTrace(TRUE);
347 if (!is_debug ||
348 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) ||
349 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) {
351 struct list_id list;
353 /* Show where we crashed */
354 curr_frame = 0;
355 DEBUG_DisassembleInstruction(&addr);
357 /* resets list internal arguments so we can look at source code when needed */
358 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
359 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
361 return TRUE;
364 static DWORD DEBUG_ExceptionEpilog(void)
366 DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode,
367 DEBUG_CurrThread->dbg_exec_count);
369 * This will have gotten absorbed into the breakpoint info
370 * if it was used. Otherwise it would have been ignored.
371 * In any case, we don't mess with it any more.
373 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS)
374 DEBUG_CurrThread->dbg_exec_count = 0;
376 return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
379 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont)
381 BOOL is_debug = FALSE;
382 BOOL ret = TRUE;
383 THREADNAME_INFO *pThreadName;
384 DBG_THREAD *pThread;
387 *cont = DBG_CONTINUE;
389 switch (rec->ExceptionCode)
391 case EXCEPTION_BREAKPOINT:
392 case EXCEPTION_SINGLE_STEP:
393 is_debug = TRUE;
394 break;
395 case EXCEPTION_NAME_THREAD:
396 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
397 if (pThreadName->dwThreadID == -1)
398 pThread = DEBUG_CurrThread;
399 else
400 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
402 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
403 pThread->name, 9, NULL))
404 DEBUG_Printf (DBG_CHN_MESG,
405 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
406 pThread->tid, pThread->name);
407 return TRUE;
410 if (first_chance && !force && !DBG_IVAR(BreakOnFirstChance))
412 /* pass exception to program except for debug exceptions */
413 *cont = is_debug ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED;
414 return TRUE;
417 if (!is_debug)
419 /* print some infos */
420 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
421 first_chance ? "First chance exception" : "Unhandled exception");
422 switch (rec->ExceptionCode)
424 case EXCEPTION_INT_DIVIDE_BY_ZERO:
425 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
426 break;
427 case EXCEPTION_INT_OVERFLOW:
428 DEBUG_Printf(DBG_CHN_MESG, "overflow");
429 break;
430 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
431 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
432 break;
433 case EXCEPTION_ILLEGAL_INSTRUCTION:
434 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
435 break;
436 case EXCEPTION_STACK_OVERFLOW:
437 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
438 break;
439 case EXCEPTION_PRIV_INSTRUCTION:
440 DEBUG_Printf(DBG_CHN_MESG, "priviledged instruction");
441 break;
442 case EXCEPTION_ACCESS_VIOLATION:
443 if (rec->NumberParameters == 2)
444 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
445 rec->ExceptionInformation[0] ? "write" : "read",
446 rec->ExceptionInformation[1]);
447 else
448 DEBUG_Printf(DBG_CHN_MESG, "page fault");
449 break;
450 case EXCEPTION_DATATYPE_MISALIGNMENT:
451 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
452 break;
453 case CONTROL_C_EXIT:
454 DEBUG_Printf(DBG_CHN_MESG, "^C");
455 break;
456 case EXCEPTION_CRITICAL_SECTION_WAIT:
457 DEBUG_Printf(DBG_CHN_MESG, "critical section %08lx wait failed",
458 rec->ExceptionInformation[0]);
459 if (!DBG_IVAR(BreakOnCritSectTimeOut))
461 DEBUG_Printf(DBG_CHN_MESG, "\n");
462 return TRUE;
464 break;
465 case EXCEPTION_WINE_STUB:
467 char dll[32], name[64];
468 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
469 (char *)rec->ExceptionInformation[0] );
470 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
471 (char *)rec->ExceptionInformation[1] );
472 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
474 break;
475 case EXCEPTION_VM86_INTx:
476 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
477 rec->ExceptionInformation[0]);
478 break;
479 case EXCEPTION_VM86_STI:
480 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
481 break;
482 case EXCEPTION_VM86_PICRETURN:
483 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
484 break;
485 default:
486 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
487 break;
489 DEBUG_Printf(DBG_CHN_MESG, "\n");
492 DEBUG_Printf(DBG_CHN_TRACE,
493 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
494 #ifdef __i386__
495 DEBUG_context.Eip, DEBUG_context.EFlags,
496 #else
497 0L, 0L,
498 #endif
499 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
501 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
502 while ((ret = DEBUG_Parser())) {
503 if (DEBUG_ValidateRegisters()) {
504 if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance)
505 break;
506 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
510 *cont = DEBUG_ExceptionEpilog();
512 DEBUG_Printf(DBG_CHN_TRACE,
513 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
514 #ifdef __i386__
515 DEBUG_context.Eip, DEBUG_context.EFlags,
516 #else
517 0L, 0L,
518 #endif
519 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
521 return ret;
524 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
526 char buffer[256];
527 BOOL ret;
529 DEBUG_CurrPid = de->dwProcessId;
530 DEBUG_CurrTid = de->dwThreadId;
532 __TRY {
533 ret = TRUE;
534 *cont = 0L;
536 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
537 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
538 else
539 DEBUG_CurrThread = NULL;
541 switch (de->dwDebugEventCode) {
542 case EXCEPTION_DEBUG_EVENT:
543 if (!DEBUG_CurrThread) {
544 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
545 de->dwProcessId, de->dwThreadId);
546 break;
549 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
550 de->dwProcessId, de->dwThreadId,
551 de->u.Exception.ExceptionRecord.ExceptionCode);
553 if (DEBUG_CurrProcess->continue_on_first_exception) {
554 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
555 if (!DBG_IVAR(BreakOnAttach)) {
556 *cont = DBG_CONTINUE;
557 break;
561 DEBUG_context.ContextFlags = CONTEXT_CONTROL
562 | CONTEXT_INTEGER
563 #ifdef CONTEXT_SEGMENTS
564 | CONTEXT_SEGMENTS
565 #endif
566 #ifdef CONTEXT_DEBUG_REGISTERS
567 | CONTEXT_DEBUG_REGISTERS
568 #endif
571 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
572 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
573 break;
576 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
577 de->u.Exception.dwFirstChance,
578 DEBUG_CurrThread->wait_for_first_exception,
579 cont);
580 if (DEBUG_CurrThread) {
581 DEBUG_CurrThread->wait_for_first_exception = 0;
582 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
584 break;
586 case CREATE_THREAD_DEBUG_EVENT:
587 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
588 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
590 if (DEBUG_CurrProcess == NULL) {
591 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
592 break;
594 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
595 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
596 break;
599 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
600 de->dwThreadId,
601 de->u.CreateThread.hThread,
602 de->u.CreateThread.lpStartAddress,
603 de->u.CreateThread.lpThreadLocalBase);
604 if (!DEBUG_CurrThread) {
605 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
606 break;
608 DEBUG_InitCurrThread();
609 break;
611 case CREATE_PROCESS_DEBUG_EVENT:
612 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
613 de->u.CreateProcessInfo.hProcess,
614 de->u.CreateProcessInfo.lpImageName);
616 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
617 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
618 de->dwProcessId, de->dwThreadId,
619 buffer,
620 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
621 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
622 de->u.CreateProcessInfo.nDebugInfoSize);
624 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
625 if (DEBUG_CurrProcess->handle) {
626 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
627 break;
629 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
630 } else {
631 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
632 de->u.CreateProcessInfo.hProcess);
633 if (DEBUG_CurrProcess == NULL) {
634 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
635 break;
639 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
640 de->dwProcessId, de->dwThreadId,
641 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
643 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
644 de->dwThreadId,
645 de->u.CreateProcessInfo.hThread,
646 de->u.CreateProcessInfo.lpStartAddress,
647 de->u.CreateProcessInfo.lpThreadLocalBase);
648 if (!DEBUG_CurrThread) {
649 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
650 break;
653 DEBUG_InitCurrProcess();
654 DEBUG_InitCurrThread();
656 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
657 DEBUG_CurrThread->process->handle,
658 de->u.CreateProcessInfo.lpImageName);
659 DEBUG_LoadModule32(buffer[0] ? buffer : "<Debugged process>",
660 de->u.CreateProcessInfo.hFile,
661 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
663 if (buffer[0]) /* we got a process name */
665 DWORD type;
666 if (!GetBinaryTypeA( buffer, &type ))
668 /* not a Windows binary, assume it's a Unix executable then */
669 char unixname[MAX_PATH];
670 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
671 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
673 DEBUG_ReadExecutableDbgInfo( unixname );
674 break;
678 /* if it is a Windows binary, or an invalid or missing file name,
679 * we use wine itself as the main executable */
680 DEBUG_ReadExecutableDbgInfo( "wine" );
681 break;
683 case EXIT_THREAD_DEBUG_EVENT:
684 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
685 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
687 if (DEBUG_CurrThread == NULL) {
688 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
689 break;
691 /* FIXME: remove break point set on thread startup */
692 DEBUG_DelThread(DEBUG_CurrThread);
693 break;
695 case EXIT_PROCESS_DEBUG_EVENT:
696 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
697 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
699 if (DEBUG_CurrProcess == NULL) {
700 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
701 break;
703 /* just in case */
704 DEBUG_SetBreakpoints(FALSE);
705 /* kill last thread */
706 DEBUG_DelThread(DEBUG_CurrProcess->threads);
707 DEBUG_DelProcess(DEBUG_CurrProcess);
709 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
710 break;
712 case LOAD_DLL_DEBUG_EVENT:
713 if (DEBUG_CurrThread == NULL) {
714 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
715 break;
717 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
718 DEBUG_CurrThread->process->handle,
719 de->u.LoadDll.lpImageName);
721 /* FIXME unicode: de->u.LoadDll.fUnicode */
722 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
723 de->dwProcessId, de->dwThreadId,
724 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
725 de->u.LoadDll.dwDebugInfoFileOffset,
726 de->u.LoadDll.nDebugInfoSize);
727 CharUpper(buffer);
728 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
729 if (DBG_IVAR(BreakOnDllLoad)) {
730 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
731 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
732 ret = DEBUG_Parser();
734 break;
736 case UNLOAD_DLL_DEBUG_EVENT:
737 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
738 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
739 break;
741 case OUTPUT_DEBUG_STRING_EVENT:
742 if (DEBUG_CurrThread == NULL) {
743 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
744 break;
747 DEBUG_ProcessGetString(buffer, sizeof(buffer),
748 DEBUG_CurrThread->process->handle,
749 de->u.DebugString.lpDebugStringData);
751 /* fixme unicode de->u.DebugString.fUnicode ? */
752 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
753 de->dwProcessId, de->dwThreadId, buffer);
754 break;
756 case RIP_EVENT:
757 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
758 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
759 de->u.RipInfo.dwType);
760 break;
762 default:
763 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
764 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
767 } __EXCEPT(wine_dbg) {
768 *cont = 0;
769 ret = TRUE;
771 __ENDTRY;
772 return ret;
775 static DWORD DEBUG_MainLoop(void)
777 DEBUG_EVENT de;
778 DWORD cont;
779 BOOL ret;
781 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
783 for (ret = TRUE; ret; ) {
784 /* wait until we get at least one loaded process */
785 while (!DEBUG_ProcessList && (ret = DEBUG_Parser()));
786 if (!ret) break;
788 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) {
789 ret = DEBUG_HandleDebugEvent(&de, &cont);
790 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
794 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
796 return 0;
799 static BOOL DEBUG_Start(LPSTR cmdLine)
801 PROCESS_INFORMATION info;
802 STARTUPINFOA startup;
804 memset(&startup, 0, sizeof(startup));
805 startup.cb = sizeof(startup);
806 startup.dwFlags = STARTF_USESHOWWINDOW;
807 startup.wShowWindow = SW_SHOWNORMAL;
809 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
810 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
811 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
812 return FALSE;
814 DEBUG_CurrPid = info.dwProcessId;
815 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0))) return FALSE;
817 return TRUE;
820 void DEBUG_Run(const char* args)
822 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
823 const char* pgm = (wmod) ? wmod->module_name : "none";
825 if (args) {
826 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
827 } else {
828 if (!DEBUG_LastCmdLine) {
829 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
830 return;
832 DEBUG_Start(DEBUG_LastCmdLine);
836 int DEBUG_main(int argc, char** argv)
838 DWORD retv = 0;
840 #ifdef DBG_need_heap
841 /* Initialize the debugger heap. */
842 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
843 #endif
845 /* Initialize the type handling stuff. */
846 DEBUG_InitTypes();
847 DEBUG_InitCVDataTypes();
849 /* Initialize internal vars (types must be initialized before) */
850 if (!DEBUG_IntVarsRW(TRUE)) return -1;
852 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
853 * stdout and stderr streams
855 if (DBG_IVAR(UseXTerm)) {
856 COORD pos;
858 /* This is a hack: it forces creation of an xterm, not done by default */
859 pos.X = 0; pos.Y = 1;
860 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
863 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... ");
865 if (argc == 3) {
866 HANDLE hEvent;
867 DWORD pid;
869 if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) {
870 if (!DEBUG_Attach(pid, TRUE)) {
871 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
872 DEBUG_CurrPid, GetLastError());
873 /* don't care about result */
874 SetEvent(hEvent);
875 goto leave;
877 if (!SetEvent(hEvent)) {
878 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
879 goto leave;
881 CloseHandle(hEvent);
882 DEBUG_CurrPid = pid;
886 if (DEBUG_CurrPid == 0 && argc > 1) {
887 int i, len;
888 LPSTR cmdLine;
890 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
891 cmdLine[0] = '\0';
893 for (i = 1; i < argc; i++) {
894 len += strlen(argv[i]) + 1;
895 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
896 strcat(cmdLine, argv[i]);
897 cmdLine[len - 2] = ' ';
898 cmdLine[len - 1] = '\0';
901 if (!DEBUG_Start(cmdLine)) {
902 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
903 goto leave;
905 DBG_free(DEBUG_LastCmdLine);
906 DEBUG_LastCmdLine = cmdLine;
909 retv = DEBUG_MainLoop();
910 leave:
911 /* saves modified variables */
912 DEBUG_IntVarsRW(FALSE);
914 return retv;
916 oom_leave:
917 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
918 goto leave;