1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
26 DBG_PROCESS* DEBUG_CurrProcess = NULL;
27 DBG_THREAD* DEBUG_CurrThread = NULL;
30 CONTEXT DEBUG_context;
31 BOOL DEBUG_interactiveP = FALSE;
33 static char* DEBUG_LastCmdLine = NULL;
35 static DBG_PROCESS* DEBUG_ProcessList = NULL;
36 static int automatic_mode;
37 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
39 void DEBUG_Output(int chn, const char* buffer, int len)
41 if (DBG_IVAR(ConChannelMask) & chn)
42 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
43 if (DBG_IVAR(StdChannelMask) & chn)
44 fwrite(buffer, len, 1, stderr);
47 int DEBUG_Printf(int chn, const char* format, ...)
49 static char buf[4*1024];
53 va_start(valist, format);
54 len = vsnprintf(buf, sizeof(buf), format, valist);
58 len = sizeof(buf) - 1;
60 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
62 DEBUG_Output(chn, buf, len);
66 static BOOL DEBUG_IntVarsRW(int read)
69 DWORD type = REG_DWORD;
71 DWORD count = sizeof(val);
73 DBG_INTVAR* div = DEBUG_IntVars;
76 /* initializes internal vars table */
77 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
78 div->val = _val; div->name = #_var; div->pval = _ref; \
79 div->type = _typ; div++;
84 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
85 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
87 fprintf(stderr, "Cannot create WineDbg key in registry\n");
91 for (i = 0; i < DBG_IV_LAST; i++) {
93 if (!DEBUG_IntVars[i].pval) {
94 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
95 &type, (LPSTR)&val, &count))
96 DEBUG_IntVars[i].val = val;
97 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
99 *DEBUG_IntVars[i].pval = 0;
102 /* FIXME: type should be infered from basic type -if any- of intvar */
103 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
104 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
105 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
112 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
116 for (i = 0; i < DBG_IV_LAST; i++) {
117 if (!strcmp(DEBUG_IntVars[i].name, name))
118 return &DEBUG_IntVars[i];
123 static WINE_EXCEPTION_FILTER(wine_dbg)
125 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
126 DEBUG_ExternalDebugger();
127 return EXCEPTION_EXECUTE_HANDLER;
130 DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
134 for (p = DEBUG_ProcessList; p; p = p->next)
135 if (p->pid == pid) break;
139 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName)
141 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
146 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
149 p->continue_on_first_exception = FALSE;
154 p->delayed_bp = NULL;
155 p->num_delayed_bp = 0;
157 p->next = DEBUG_ProcessList;
159 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
160 DEBUG_ProcessList = p;
164 static void DEBUG_DelThread(DBG_THREAD* p);
166 static void DEBUG_DelProcess(DBG_PROCESS* p)
170 if (p->threads != NULL) {
171 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
172 while (p->threads) DEBUG_DelThread(p->threads);
174 for (i = 0; i < p->num_delayed_bp; i++) {
175 DBG_free(p->delayed_bp[i].name);
177 DBG_free(p->delayed_bp);
178 if (p->prev) p->prev->next = p->next;
179 if (p->next) p->next->prev = p->prev;
180 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
181 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
182 DBG_free((char*)p->imageName);
186 static void DEBUG_InitCurrProcess(void)
190 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
194 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
197 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
203 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
206 && ReadProcessMemory(hp, ad, buffer, size, &sz))
212 DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
216 for (t = p->threads; t; t = t->next)
217 if (t->tid == tid) break;
221 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
222 HANDLE h, LPVOID start, LPVOID teb)
224 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
233 t->wait_for_first_exception = 0;
234 t->dbg_exec_mode = EXEC_CONT;
235 t->dbg_exec_count = 0;
236 sprintf(t->name, "%08lx", tid);
239 t->next = p->threads;
241 if (p->threads) p->threads->prev = t;
247 static void DEBUG_InitCurrThread(void)
249 if (DEBUG_CurrThread->start) {
250 if (DEBUG_CurrThread->process->num_threads == 1 ||
251 DBG_IVAR(BreakAllThreadsStartup)) {
254 DEBUG_SetBreakpoints(FALSE);
256 value.cookie = DV_TARGET;
258 value.addr.off = (DWORD)DEBUG_CurrThread->start;
259 DEBUG_AddBreakpoint(&value, NULL);
260 DEBUG_SetBreakpoints(TRUE);
263 DEBUG_CurrThread->wait_for_first_exception = 1;
267 static void DEBUG_DelThread(DBG_THREAD* t)
269 if (t->prev) t->prev->next = t->next;
270 if (t->next) t->next->prev = t->prev;
271 if (t == t->process->threads) t->process->threads = t->next;
272 t->process->num_threads--;
273 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
277 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
279 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE;
281 if (!DebugActiveProcess(pid)) {
282 DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError());
283 DEBUG_DelProcess(DEBUG_CurrProcess);
284 DEBUG_CurrProcess = NULL;
287 DEBUG_CurrProcess->continue_on_first_exception = cofe;
291 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
296 DEBUG_GetCurrentAddress(&addr);
297 DEBUG_SuspendExecution();
301 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
303 switch(DEBUG_GetSelectorType(addr.seg))
306 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
309 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
312 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
315 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
318 DEBUG_Printf(DBG_CHN_MESG, ".\n");
321 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
323 if (!force && is_debug &&
324 DEBUG_ShouldContinue(&addr,
326 DEBUG_CurrThread->dbg_exec_mode,
327 &DEBUG_CurrThread->dbg_exec_count))
330 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
331 if (newmode != DEBUG_CurrThread->dbg_mode)
333 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
334 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
335 DEBUG_CurrThread->dbg_mode = newmode;
340 if (is_debug || force) {
342 * Do a quiet backtrace so that we have an idea of what the situation
343 * is WRT the source files.
345 DEBUG_BackTrace(DEBUG_CurrTid, FALSE);
347 /* This is a real crash, dump some info */
348 DEBUG_InfoRegisters();
351 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
352 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
353 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
354 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
356 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
358 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
362 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) ||
363 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) {
367 /* Show where we crashed */
369 DEBUG_DisassembleInstruction(&addr);
371 /* resets list internal arguments so we can look at source code when needed */
372 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
373 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
378 static DWORD DEBUG_ExceptionEpilog(void)
380 DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode,
381 DEBUG_CurrThread->dbg_exec_count);
383 * This will have gotten absorbed into the breakpoint info
384 * if it was used. Otherwise it would have been ignored.
385 * In any case, we don't mess with it any more.
387 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS)
388 DEBUG_CurrThread->dbg_exec_count = 0;
390 return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
393 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont)
395 BOOL is_debug = FALSE;
397 THREADNAME_INFO *pThreadName;
401 *cont = DBG_CONTINUE;
403 switch (rec->ExceptionCode)
405 case EXCEPTION_BREAKPOINT:
406 case EXCEPTION_SINGLE_STEP:
409 case EXCEPTION_NAME_THREAD:
410 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
411 if (pThreadName->dwThreadID == -1)
412 pThread = DEBUG_CurrThread;
414 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
416 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
417 pThread->name, 9, NULL))
418 DEBUG_Printf (DBG_CHN_MESG,
419 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
420 pThread->tid, pThread->name);
424 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
426 /* pass exception to program except for debug exceptions */
427 *cont = is_debug ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED;
433 /* print some infos */
434 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
435 first_chance ? "First chance exception" : "Unhandled exception");
436 switch (rec->ExceptionCode)
438 case EXCEPTION_INT_DIVIDE_BY_ZERO:
439 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
441 case EXCEPTION_INT_OVERFLOW:
442 DEBUG_Printf(DBG_CHN_MESG, "overflow");
444 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
445 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
447 case EXCEPTION_ILLEGAL_INSTRUCTION:
448 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
450 case EXCEPTION_STACK_OVERFLOW:
451 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
453 case EXCEPTION_PRIV_INSTRUCTION:
454 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
456 case EXCEPTION_ACCESS_VIOLATION:
457 if (rec->NumberParameters == 2)
458 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
459 rec->ExceptionInformation[0] ? "write" : "read",
460 rec->ExceptionInformation[1]);
462 DEBUG_Printf(DBG_CHN_MESG, "page fault");
464 case EXCEPTION_DATATYPE_MISALIGNMENT:
465 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
468 DEBUG_Printf(DBG_CHN_MESG, "^C");
470 case EXCEPTION_CRITICAL_SECTION_WAIT:
475 addr.off = rec->ExceptionInformation[0];
477 DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section ");
478 DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE);
480 if (!DBG_IVAR(BreakOnCritSectTimeOut))
482 DEBUG_Printf(DBG_CHN_MESG, "\n");
486 case EXCEPTION_WINE_STUB:
488 char dll[32], name[64];
489 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
490 (char *)rec->ExceptionInformation[0] );
491 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
492 (char *)rec->ExceptionInformation[1] );
493 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
496 case EXCEPTION_VM86_INTx:
497 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
498 rec->ExceptionInformation[0]);
500 case EXCEPTION_VM86_STI:
501 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
503 case EXCEPTION_VM86_PICRETURN:
504 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
507 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
513 DEBUG_Printf(DBG_CHN_TRACE,
514 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
516 DEBUG_context.Eip, DEBUG_context.EFlags,
520 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
525 DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode);
526 return FALSE; /* terminate execution */
529 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
530 DEBUG_interactiveP = TRUE;
531 while ((ret = DEBUG_Parser())) {
532 if (DEBUG_ValidateRegisters()) {
533 if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance)
535 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
538 DEBUG_interactiveP = FALSE;
540 *cont = DEBUG_ExceptionEpilog();
543 DEBUG_Printf(DBG_CHN_TRACE,
544 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
546 DEBUG_context.Eip, DEBUG_context.EFlags,
550 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
556 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
561 DEBUG_CurrPid = de->dwProcessId;
562 DEBUG_CurrTid = de->dwThreadId;
568 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
569 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
571 DEBUG_CurrThread = NULL;
573 switch (de->dwDebugEventCode) {
574 case EXCEPTION_DEBUG_EVENT:
575 if (!DEBUG_CurrThread) {
576 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
577 de->dwProcessId, de->dwThreadId);
581 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
582 de->dwProcessId, de->dwThreadId,
583 de->u.Exception.ExceptionRecord.ExceptionCode);
585 if (DEBUG_CurrProcess->continue_on_first_exception) {
586 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
587 if (!DBG_IVAR(BreakOnAttach)) {
588 *cont = DBG_CONTINUE;
593 DEBUG_context.ContextFlags = CONTEXT_CONTROL
595 #ifdef CONTEXT_SEGMENTS
598 #ifdef CONTEXT_DEBUG_REGISTERS
599 | CONTEXT_DEBUG_REGISTERS
603 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
604 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
608 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
609 de->u.Exception.dwFirstChance,
610 DEBUG_CurrThread->wait_for_first_exception,
612 if (DEBUG_CurrThread) {
613 DEBUG_CurrThread->wait_for_first_exception = 0;
614 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
618 case CREATE_THREAD_DEBUG_EVENT:
619 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
620 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
622 if (DEBUG_CurrProcess == NULL) {
623 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
626 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
627 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
631 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
633 de->u.CreateThread.hThread,
634 de->u.CreateThread.lpStartAddress,
635 de->u.CreateThread.lpThreadLocalBase);
636 if (!DEBUG_CurrThread) {
637 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
640 DEBUG_InitCurrThread();
643 case CREATE_PROCESS_DEBUG_EVENT:
644 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
645 de->u.CreateProcessInfo.hProcess,
646 de->u.CreateProcessInfo.lpImageName);
648 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
649 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
650 de->dwProcessId, de->dwThreadId,
651 buffer, de->u.CreateProcessInfo.lpImageName,
652 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
653 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
654 de->u.CreateProcessInfo.nDebugInfoSize);
656 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
657 if (DEBUG_CurrProcess->handle) {
658 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
661 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
662 if (DEBUG_CurrProcess->imageName == NULL)
663 DEBUG_CurrProcess->imageName = DBG_strdup(buffer[0] ? buffer : "<Debugged Process>");
666 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
667 de->u.CreateProcessInfo.hProcess,
668 buffer[0] ? buffer : "<Debugged Process>");
669 if (DEBUG_CurrProcess == NULL) {
670 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
675 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
676 de->dwProcessId, de->dwThreadId,
677 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
679 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
681 de->u.CreateProcessInfo.hThread,
682 de->u.CreateProcessInfo.lpStartAddress,
683 de->u.CreateProcessInfo.lpThreadLocalBase);
684 if (!DEBUG_CurrThread) {
685 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
689 DEBUG_InitCurrProcess();
690 DEBUG_InitCurrThread();
692 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
693 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
695 if (buffer[0]) /* we got a process name */
698 if (!GetBinaryTypeA( buffer, &type ))
700 /* not a Windows binary, assume it's a Unix executable then */
701 char unixname[MAX_PATH];
702 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
703 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
705 DEBUG_ReadExecutableDbgInfo( unixname );
710 /* if it is a Windows binary, or an invalid or missing file name,
711 * we use wine itself as the main executable */
712 DEBUG_ReadExecutableDbgInfo( "wine" );
715 case EXIT_THREAD_DEBUG_EVENT:
716 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
717 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
719 if (DEBUG_CurrThread == NULL) {
720 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
723 /* FIXME: remove break point set on thread startup */
724 DEBUG_DelThread(DEBUG_CurrThread);
727 case EXIT_PROCESS_DEBUG_EVENT:
728 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
729 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
731 if (DEBUG_CurrProcess == NULL) {
732 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
736 DEBUG_SetBreakpoints(FALSE);
737 /* kill last thread */
738 DEBUG_DelThread(DEBUG_CurrProcess->threads);
739 DEBUG_DelProcess(DEBUG_CurrProcess);
741 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
744 case LOAD_DLL_DEBUG_EVENT:
745 if (DEBUG_CurrThread == NULL) {
746 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
749 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
750 DEBUG_CurrThread->process->handle,
751 de->u.LoadDll.lpImageName);
753 /* FIXME unicode: de->u.LoadDll.fUnicode */
754 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
755 de->dwProcessId, de->dwThreadId,
756 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
757 de->u.LoadDll.dwDebugInfoFileOffset,
758 de->u.LoadDll.nDebugInfoSize);
760 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
761 DEBUG_CheckDelayedBP();
762 if (DBG_IVAR(BreakOnDllLoad)) {
763 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
764 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
765 ret = DEBUG_Parser();
769 case UNLOAD_DLL_DEBUG_EVENT:
770 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
771 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
774 case OUTPUT_DEBUG_STRING_EVENT:
775 if (DEBUG_CurrThread == NULL) {
776 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
780 DEBUG_ProcessGetString(buffer, sizeof(buffer),
781 DEBUG_CurrThread->process->handle,
782 de->u.DebugString.lpDebugStringData);
784 /* fixme unicode de->u.DebugString.fUnicode ? */
785 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
786 de->dwProcessId, de->dwThreadId, buffer);
790 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
791 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
792 de->u.RipInfo.dwType);
796 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
797 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
800 } __EXCEPT(wine_dbg) {
808 static DWORD DEBUG_MainLoop(void)
814 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
816 for (ret = TRUE; ret; ) {
817 /* wait until we get at least one loaded process */
818 while (!DEBUG_ProcessList && (ret = DEBUG_Parser()));
821 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) {
822 ret = DEBUG_HandleDebugEvent(&de, &cont);
823 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
827 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
832 static DWORD DEBUG_AutoMode(void)
838 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
840 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE))
842 ret = DEBUG_HandleDebugEvent(&de, &cont);
843 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
845 /* print some extra information */
846 DEBUG_Printf(DBG_CHN_MESG, "Modules:\n");
848 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
851 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
855 static BOOL DEBUG_Start(LPSTR cmdLine)
857 PROCESS_INFORMATION info;
858 STARTUPINFOA startup;
860 memset(&startup, 0, sizeof(startup));
861 startup.cb = sizeof(startup);
862 startup.dwFlags = STARTF_USESHOWWINDOW;
863 startup.wShowWindow = SW_SHOWNORMAL;
865 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
866 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
867 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
870 DEBUG_CurrPid = info.dwProcessId;
871 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
876 void DEBUG_Run(const char* args)
878 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
879 const char* pgm = (wmod) ? wmod->module_name : "none";
882 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
884 if (!DEBUG_LastCmdLine) {
885 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
888 DEBUG_Start(DEBUG_LastCmdLine);
892 int DEBUG_main(int argc, char** argv)
897 /* Initialize the debugger heap. */
898 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
901 /* Initialize the type handling stuff. */
903 DEBUG_InitCVDataTypes();
905 /* Initialize internal vars (types must have been initialized before) */
906 if (!DEBUG_IntVarsRW(TRUE)) return -1;
908 if (argc > 1 && !strcmp( argv[1], "--auto" ))
913 /* force some internal variables */
914 DBG_IVAR(UseXTerm) = 0;
915 DBG_IVAR(BreakOnDllLoad) = 0;
916 DBG_IVAR(ConChannelMask) = 0;
917 DBG_IVAR(StdChannelMask) = DBG_CHN_MESG;
920 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
921 * stdout and stderr streams
923 if (DBG_IVAR(UseXTerm)) {
926 /* This is a hack: it forces creation of an xterm, not done by default */
927 pos.X = 0; pos.Y = 1;
928 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
931 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... ");
937 if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) {
938 if (!DEBUG_Attach(pid, TRUE)) {
939 /* don't care about result */
943 if (!SetEvent(hEvent)) {
944 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
952 if (DEBUG_CurrPid == 0 && argc > 1) {
956 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
959 for (i = 1; i < argc; i++) {
960 len += strlen(argv[i]) + 1;
961 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
962 strcat(cmdLine, argv[i]);
963 cmdLine[len - 2] = ' ';
964 cmdLine[len - 1] = '\0';
967 if (!DEBUG_Start(cmdLine)) {
968 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
971 DBG_free(DEBUG_LastCmdLine);
972 DEBUG_LastCmdLine = cmdLine;
977 retv = DEBUG_AutoMode();
978 /* don't save modified variables in auto mode */
982 retv = DEBUG_MainLoop();
983 /* saves modified variables */
984 DEBUG_IntVarsRW(FALSE);
991 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");