1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
22 DBG_PROCESS* DEBUG_CurrProcess = NULL;
23 DBG_THREAD* DEBUG_CurrThread = NULL;
26 CONTEXT DEBUG_context;
27 BOOL DEBUG_interactiveP = FALSE;
29 static char* DEBUG_LastCmdLine = NULL;
31 static DBG_PROCESS* DEBUG_ProcessList = NULL;
32 static int automatic_mode;
33 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
35 void DEBUG_Output(int chn, const char* buffer, int len)
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);
43 int DEBUG_Printf(int chn, const char* format, ...)
45 static char buf[4*1024];
49 va_start(valist, format);
50 len = vsnprintf(buf, sizeof(buf), format, valist);
54 len = sizeof(buf) - 1;
56 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
58 DEBUG_Output(chn, buf, len);
62 static BOOL DEBUG_IntVarsRW(int read)
65 DWORD type = REG_DWORD;
67 DWORD count = sizeof(val);
69 DBG_INTVAR* div = DEBUG_IntVars;
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 = DEBUG_GetBasicType(_typ); div++;
80 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
81 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
83 fprintf(stderr, "Cannot create WineDbg key in registry\n");
87 for (i = 0; i < DBG_IV_LAST; i++) {
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;
95 *DEBUG_IntVars[i].pval = 0;
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);
108 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
112 for (i = 0; i < DBG_IV_LAST; i++) {
113 if (!strcmp(DEBUG_IntVars[i].name, name))
114 return &DEBUG_IntVars[i];
119 static WINE_EXCEPTION_FILTER(wine_dbg)
121 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
122 DEBUG_ExternalDebugger();
123 return EXCEPTION_EXECUTE_HANDLER;
126 DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
130 for (p = DEBUG_ProcessList; p; p = p->next)
131 if (p->pid == pid) break;
135 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName)
137 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
142 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
145 p->continue_on_first_exception = FALSE;
150 p->delayed_bp = NULL;
151 p->num_delayed_bp = 0;
153 p->next = DEBUG_ProcessList;
155 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
156 DEBUG_ProcessList = p;
160 static void DEBUG_DelThread(DBG_THREAD* p);
162 static void DEBUG_DelProcess(DBG_PROCESS* p)
166 if (p->threads != NULL) {
167 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
168 while (p->threads) DEBUG_DelThread(p->threads);
170 for (i = 0; i < p->num_delayed_bp; i++) {
171 DBG_free(p->delayed_bp[i].name);
173 DBG_free(p->delayed_bp);
174 if (p->prev) p->prev->next = p->next;
175 if (p->next) p->next->prev = p->prev;
176 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
177 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
178 DBG_free((char*)p->imageName);
182 static void DEBUG_InitCurrProcess(void)
186 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
190 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
193 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
199 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
202 && ReadProcessMemory(hp, ad, buffer, size, &sz))
208 DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
212 for (t = p->threads; t; t = t->next)
213 if (t->tid == tid) break;
217 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
218 HANDLE h, LPVOID start, LPVOID teb)
220 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
229 t->wait_for_first_exception = 0;
230 t->dbg_exec_mode = EXEC_CONT;
231 t->dbg_exec_count = 0;
232 sprintf(t->name, "%08lx", tid);
235 t->next = p->threads;
237 if (p->threads) p->threads->prev = t;
243 static void DEBUG_InitCurrThread(void)
245 if (DEBUG_CurrThread->start) {
246 if (DEBUG_CurrThread->process->num_threads == 1 ||
247 DBG_IVAR(BreakAllThreadsStartup)) {
250 DEBUG_SetBreakpoints(FALSE);
252 value.cookie = DV_TARGET;
254 value.addr.off = (DWORD)DEBUG_CurrThread->start;
255 DEBUG_AddBreakpoint(&value, NULL);
256 DEBUG_SetBreakpoints(TRUE);
259 DEBUG_CurrThread->wait_for_first_exception = 1;
263 static void DEBUG_DelThread(DBG_THREAD* t)
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;
268 t->process->num_threads--;
269 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
273 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
275 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE;
277 if (!DebugActiveProcess(pid)) {
278 DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError());
279 DEBUG_DelProcess(DEBUG_CurrProcess);
280 DEBUG_CurrProcess = NULL;
283 DEBUG_CurrProcess->continue_on_first_exception = cofe;
287 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
292 DEBUG_GetCurrentAddress(&addr);
293 DEBUG_SuspendExecution();
297 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
299 switch(DEBUG_GetSelectorType(addr.seg))
302 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
305 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
308 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
311 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
314 DEBUG_Printf(DBG_CHN_MESG, ".\n");
317 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
319 if (!force && is_debug &&
320 DEBUG_ShouldContinue(&addr,
322 DEBUG_CurrThread->dbg_exec_mode,
323 &DEBUG_CurrThread->dbg_exec_count))
326 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
327 if (newmode != DEBUG_CurrThread->dbg_mode)
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;
336 if (is_debug || force) {
338 * Do a quiet backtrace so that we have an idea of what the situation
339 * is WRT the source files.
341 DEBUG_BackTrace(DEBUG_CurrTid, FALSE);
343 /* This is a real crash, dump some info */
344 DEBUG_InfoRegisters();
347 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
348 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
349 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
350 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
352 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
354 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
358 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) ||
359 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) {
363 /* Show where we crashed */
365 DEBUG_DisassembleInstruction(&addr);
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);
374 static DWORD DEBUG_ExceptionEpilog(void)
376 DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode,
377 DEBUG_CurrThread->dbg_exec_count);
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.
383 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS)
384 DEBUG_CurrThread->dbg_exec_count = 0;
386 return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
389 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont)
391 BOOL is_debug = FALSE;
393 THREADNAME_INFO *pThreadName;
397 *cont = DBG_CONTINUE;
399 switch (rec->ExceptionCode)
401 case EXCEPTION_BREAKPOINT:
402 case EXCEPTION_SINGLE_STEP:
405 case EXCEPTION_NAME_THREAD:
406 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
407 if (pThreadName->dwThreadID == -1)
408 pThread = DEBUG_CurrThread;
410 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
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);
420 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
422 /* pass exception to program except for debug exceptions */
423 *cont = is_debug ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED;
429 /* print some infos */
430 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
431 first_chance ? "First chance exception" : "Unhandled exception");
432 switch (rec->ExceptionCode)
434 case EXCEPTION_INT_DIVIDE_BY_ZERO:
435 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
437 case EXCEPTION_INT_OVERFLOW:
438 DEBUG_Printf(DBG_CHN_MESG, "overflow");
440 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
441 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
443 case EXCEPTION_ILLEGAL_INSTRUCTION:
444 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
446 case EXCEPTION_STACK_OVERFLOW:
447 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
449 case EXCEPTION_PRIV_INSTRUCTION:
450 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
452 case EXCEPTION_ACCESS_VIOLATION:
453 if (rec->NumberParameters == 2)
454 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
455 rec->ExceptionInformation[0] ? "write" : "read",
456 rec->ExceptionInformation[1]);
458 DEBUG_Printf(DBG_CHN_MESG, "page fault");
460 case EXCEPTION_DATATYPE_MISALIGNMENT:
461 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
464 DEBUG_Printf(DBG_CHN_MESG, "^C");
467 DEBUG_Printf(DBG_CHN_MESG, "^C");
469 case EXCEPTION_CRITICAL_SECTION_WAIT:
474 addr.off = rec->ExceptionInformation[0];
476 DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section ");
477 DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE);
479 if (!DBG_IVAR(BreakOnCritSectTimeOut))
481 DEBUG_Printf(DBG_CHN_MESG, "\n");
485 case EXCEPTION_WINE_STUB:
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 );
495 case EXCEPTION_VM86_INTx:
496 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
497 rec->ExceptionInformation[0]);
499 case EXCEPTION_VM86_STI:
500 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
502 case EXCEPTION_VM86_PICRETURN:
503 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
506 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
512 DEBUG_Printf(DBG_CHN_TRACE,
513 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
515 DEBUG_context.Eip, DEBUG_context.EFlags,
519 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
524 DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode);
525 return FALSE; /* terminate execution */
528 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
529 DEBUG_interactiveP = TRUE;
530 while ((ret = DEBUG_Parser())) {
531 if (DEBUG_ValidateRegisters()) {
532 if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance)
534 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
537 DEBUG_interactiveP = FALSE;
539 *cont = DEBUG_ExceptionEpilog();
542 DEBUG_Printf(DBG_CHN_TRACE,
543 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
545 DEBUG_context.Eip, DEBUG_context.EFlags,
549 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
555 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
560 DEBUG_CurrPid = de->dwProcessId;
561 DEBUG_CurrTid = de->dwThreadId;
567 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
568 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
570 DEBUG_CurrThread = NULL;
572 switch (de->dwDebugEventCode) {
573 case EXCEPTION_DEBUG_EVENT:
574 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);
580 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
581 de->dwProcessId, de->dwThreadId,
582 de->u.Exception.ExceptionRecord.ExceptionCode);
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;
592 DEBUG_context.ContextFlags = CONTEXT_CONTROL
594 #ifdef CONTEXT_SEGMENTS
597 #ifdef CONTEXT_DEBUG_REGISTERS
598 | CONTEXT_DEBUG_REGISTERS
602 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
603 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
607 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
608 de->u.Exception.dwFirstChance,
609 DEBUG_CurrThread->wait_for_first_exception,
611 if (DEBUG_CurrThread) {
612 DEBUG_CurrThread->wait_for_first_exception = 0;
613 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
617 case CREATE_THREAD_DEBUG_EVENT:
618 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
619 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
621 if (DEBUG_CurrProcess == NULL) {
622 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
625 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
626 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
630 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
632 de->u.CreateThread.hThread,
633 de->u.CreateThread.lpStartAddress,
634 de->u.CreateThread.lpThreadLocalBase);
635 if (!DEBUG_CurrThread) {
636 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
639 DEBUG_InitCurrThread();
642 case CREATE_PROCESS_DEBUG_EVENT:
643 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
644 de->u.CreateProcessInfo.hProcess,
645 de->u.CreateProcessInfo.lpImageName);
647 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
648 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
649 de->dwProcessId, de->dwThreadId,
650 buffer, de->u.CreateProcessInfo.lpImageName,
651 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
652 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
653 de->u.CreateProcessInfo.nDebugInfoSize);
655 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
656 if (DEBUG_CurrProcess->handle) {
657 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
660 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
661 if (DEBUG_CurrProcess->imageName == NULL)
662 DEBUG_CurrProcess->imageName = DBG_strdup(buffer[0] ? buffer : "<Debugged Process>");
665 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
666 de->u.CreateProcessInfo.hProcess,
667 buffer[0] ? buffer : "<Debugged Process>");
668 if (DEBUG_CurrProcess == NULL) {
669 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
674 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
675 de->dwProcessId, de->dwThreadId,
676 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
678 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
680 de->u.CreateProcessInfo.hThread,
681 de->u.CreateProcessInfo.lpStartAddress,
682 de->u.CreateProcessInfo.lpThreadLocalBase);
683 if (!DEBUG_CurrThread) {
684 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
688 DEBUG_InitCurrProcess();
689 DEBUG_InitCurrThread();
691 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
692 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
694 if (buffer[0]) /* we got a process name */
697 if (!GetBinaryTypeA( buffer, &type ))
699 /* not a Windows binary, assume it's a Unix executable then */
700 char unixname[MAX_PATH];
701 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
702 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
704 DEBUG_ReadExecutableDbgInfo( unixname );
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" );
714 case EXIT_THREAD_DEBUG_EVENT:
715 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
716 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
718 if (DEBUG_CurrThread == NULL) {
719 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
722 /* FIXME: remove break point set on thread startup */
723 DEBUG_DelThread(DEBUG_CurrThread);
726 case EXIT_PROCESS_DEBUG_EVENT:
727 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
728 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
730 if (DEBUG_CurrProcess == NULL) {
731 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
735 DEBUG_SetBreakpoints(FALSE);
736 /* kill last thread */
737 DEBUG_DelThread(DEBUG_CurrProcess->threads);
738 DEBUG_DelProcess(DEBUG_CurrProcess);
740 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
743 case LOAD_DLL_DEBUG_EVENT:
744 if (DEBUG_CurrThread == NULL) {
745 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
748 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
749 DEBUG_CurrThread->process->handle,
750 de->u.LoadDll.lpImageName);
752 /* FIXME unicode: de->u.LoadDll.fUnicode */
753 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
754 de->dwProcessId, de->dwThreadId,
755 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
756 de->u.LoadDll.dwDebugInfoFileOffset,
757 de->u.LoadDll.nDebugInfoSize);
759 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
760 DEBUG_CheckDelayedBP();
761 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();
768 case UNLOAD_DLL_DEBUG_EVENT:
769 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
770 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
773 case OUTPUT_DEBUG_STRING_EVENT:
774 if (DEBUG_CurrThread == NULL) {
775 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
779 DEBUG_ProcessGetString(buffer, sizeof(buffer),
780 DEBUG_CurrThread->process->handle,
781 de->u.DebugString.lpDebugStringData);
783 /* fixme unicode de->u.DebugString.fUnicode ? */
784 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
785 de->dwProcessId, de->dwThreadId, buffer);
789 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);
795 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
796 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
799 } __EXCEPT(wine_dbg) {
807 static DWORD DEBUG_MainLoop(void)
813 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
815 for (ret = TRUE; ret; ) {
816 /* wait until we get at least one loaded process */
817 while (!DEBUG_ProcessList && (ret = DEBUG_Parser()));
820 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) {
821 ret = DEBUG_HandleDebugEvent(&de, &cont);
822 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
826 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
831 static DWORD DEBUG_AutoMode(void)
837 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
839 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE))
841 ret = DEBUG_HandleDebugEvent(&de, &cont);
842 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
844 /* print some extra information */
845 DEBUG_Printf(DBG_CHN_MESG, "Modules:\n");
847 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
850 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
854 static BOOL DEBUG_Start(LPSTR cmdLine)
856 PROCESS_INFORMATION info;
857 STARTUPINFOA startup;
859 memset(&startup, 0, sizeof(startup));
860 startup.cb = sizeof(startup);
861 startup.dwFlags = STARTF_USESHOWWINDOW;
862 startup.wShowWindow = SW_SHOWNORMAL;
864 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
865 FALSE, DEBUG_PROCESS|DETACHED_PROCESS, NULL, NULL, &startup, &info)) {
866 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
869 DEBUG_CurrPid = info.dwProcessId;
870 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
875 void DEBUG_Run(const char* args)
877 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
878 const char* pgm = (wmod) ? wmod->module_name : "none";
881 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
883 if (!DEBUG_LastCmdLine) {
884 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
887 DEBUG_Start(DEBUG_LastCmdLine);
891 static void DEBUG_InitConsole(void)
897 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
898 * stdout and stderr streams
900 if (DBG_IVAR(UseXTerm))
905 /* this would be nicer for output */
908 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c);
910 /* sets the console's window width accordingly */
915 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &sr);
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);
922 int DEBUG_main(int argc, char** argv)
926 /* Initialize the type handling stuff. */
928 DEBUG_InitCVDataTypes();
930 /* Initialize internal vars (types must have been initialized before) */
931 if (!DEBUG_IntVarsRW(TRUE)) return -1;
933 if (argc > 1 && !strcmp( argv[1], "--auto" ))
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;
947 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... ");
953 if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) {
954 if (!DEBUG_Attach(pid, TRUE)) {
955 /* don't care about result */
959 if (!SetEvent(hEvent)) {
960 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
968 if (DEBUG_CurrPid == 0 && argc > 1) {
972 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
975 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';
983 if (!DEBUG_Start(cmdLine)) {
984 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
987 DBG_free(DEBUG_LastCmdLine);
988 DEBUG_LastCmdLine = cmdLine;
993 retv = DEBUG_AutoMode();
994 /* don't save modified variables in auto mode */
998 retv = DEBUG_MainLoop();
999 /* saves modified variables */
1000 DEBUG_IntVarsRW(FALSE);
1007 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");