1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
5 * Copyright 2000 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "msvcrt/excpt.h"
35 #include "wine/library.h"
37 DBG_PROCESS* DEBUG_CurrProcess = NULL;
38 DBG_THREAD* DEBUG_CurrThread = NULL;
41 CONTEXT DEBUG_context;
42 BOOL DEBUG_InteractiveP = FALSE;
44 static char* DEBUG_LastCmdLine = NULL;
46 static DBG_PROCESS* DEBUG_ProcessList = NULL;
47 static enum {none_mode = 0, winedbg_mode, automatic_mode, gdb_mode} local_mode;
49 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
51 void DEBUG_OutputA(int chn, const char* buffer, int len)
53 if (DBG_IVAR(ConChannelMask) & chn)
54 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
55 if (DBG_IVAR(StdChannelMask) & chn)
56 fwrite(buffer, len, 1, stderr);
59 void DEBUG_OutputW(int chn, const WCHAR* buffer, int len)
61 /* FIXME: this won't work is std output isn't attached to a console */
62 if (DBG_IVAR(ConChannelMask) & chn)
63 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
64 /* simplistic Unicode to ANSI conversion */
65 if (DBG_IVAR(StdChannelMask) & chn)
66 while (len--) fputc((char)*buffer++, stderr);
69 int DEBUG_Printf(int chn, const char* format, ...)
71 static char buf[4*1024];
75 va_start(valist, format);
76 len = vsnprintf(buf, sizeof(buf), format, valist);
79 if (len <= -1 || len >= sizeof(buf)) {
80 len = sizeof(buf) - 1;
82 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
84 DEBUG_OutputA(chn, buf, len);
88 static BOOL DEBUG_IntVarsRW(int read)
91 DWORD type = REG_DWORD;
93 DWORD count = sizeof(val);
95 DBG_INTVAR* div = DEBUG_IntVars;
98 /* initializes internal vars table */
99 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
100 div->val = _val; div->name = #_var; div->pval = _ref; \
101 div->type = DEBUG_GetBasicType(_typ); div++;
106 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
107 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
109 fprintf(stderr, "Cannot create WineDbg key in registry\n");
113 for (i = 0; i < DBG_IV_LAST; i++) {
115 if (!DEBUG_IntVars[i].pval) {
116 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
117 &type, (LPSTR)&val, &count))
118 DEBUG_IntVars[i].val = val;
119 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
121 *DEBUG_IntVars[i].pval = 0;
124 /* FIXME: type should be infered from basic type -if any- of intvar */
125 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
126 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
127 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
134 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
138 for (i = 0; i < DBG_IV_LAST; i++) {
139 if (!strcmp(DEBUG_IntVars[i].name, name))
140 return &DEBUG_IntVars[i];
145 DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
149 for (p = DEBUG_ProcessList; p; p = p->next)
150 if (p->pid == pid) break;
154 DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName)
158 if ((p = DEBUG_GetProcess(pid)))
162 DEBUG_Printf(DBG_CHN_ERR, "Process (%lu) is already defined\n", pid);
167 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
172 if (!(p = DBG_alloc(sizeof(DBG_PROCESS)))) return NULL;
175 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
178 p->continue_on_first_exception = FALSE;
183 p->delayed_bp = NULL;
184 p->num_delayed_bp = 0;
186 p->next = DEBUG_ProcessList;
188 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
189 DEBUG_ProcessList = p;
193 void DEBUG_DelProcess(DBG_PROCESS* p)
197 while (p->threads) DEBUG_DelThread(p->threads);
199 for (i = 0; i < p->num_delayed_bp; i++) {
200 DBG_free(p->delayed_bp[i].name);
202 DBG_free(p->delayed_bp);
203 if (p->prev) p->prev->next = p->next;
204 if (p->next) p->next->prev = p->prev;
205 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
206 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
207 DBG_free((char*)p->imageName);
211 static void DEBUG_InitCurrProcess(void)
215 BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
219 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
222 BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
228 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
231 && ReadProcessMemory(hp, ad, buffer, size, &sz))
237 DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
242 for (t = p->threads; t; t = t->next)
243 if (t->tid == tid) break;
247 DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
248 HANDLE h, LPVOID start, LPVOID teb)
250 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
259 t->wait_for_first_exception = 0;
260 t->exec_mode = EXEC_CONT;
263 sprintf(t->name, "%08lx", tid);
266 t->next = p->threads;
268 if (p->threads) p->threads->prev = t;
274 static void DEBUG_InitCurrThread(void)
276 if (DEBUG_CurrThread->start) {
277 if (DEBUG_CurrThread->process->num_threads == 1 ||
278 DBG_IVAR(BreakAllThreadsStartup)) {
281 DEBUG_SetBreakpoints(FALSE);
283 value.cookie = DV_TARGET;
285 value.addr.off = (DWORD)DEBUG_CurrThread->start;
286 DEBUG_AddBreakpoint(&value, NULL);
287 DEBUG_SetBreakpoints(TRUE);
290 DEBUG_CurrThread->wait_for_first_exception = 1;
294 void DEBUG_DelThread(DBG_THREAD* t)
296 if (t->prev) t->prev->next = t->next;
297 if (t->next) t->next->prev = t->prev;
298 if (t == t->process->threads) t->process->threads = t->next;
299 t->process->num_threads--;
300 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
304 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
306 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE;
308 if (!DebugActiveProcess(pid)) {
309 DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError());
310 DEBUG_DelProcess(DEBUG_CurrProcess);
311 DEBUG_CurrProcess = NULL;
314 DEBUG_CurrProcess->continue_on_first_exception = cofe;
318 BOOL DEBUG_Detach(void)
320 /* remove all set breakpoints in debuggee code */
321 DEBUG_SetBreakpoints(FALSE);
322 /* needed for single stepping (ugly).
323 * should this be handled inside the server ??? */
325 DEBUG_context.EFlags &= ~STEP_FLAG;
327 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
328 DebugActiveProcessStop(DEBUG_CurrProcess->pid);
329 DEBUG_DelProcess(DEBUG_CurrProcess);
330 DEBUG_CurrProcess = NULL;
331 /* FIXME: should zero out the symbol table too */
335 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
340 DEBUG_GetCurrentAddress(&addr);
341 DEBUG_SuspendExecution();
346 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
348 switch(DEBUG_GetSelectorType(addr.seg))
351 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
354 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
357 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
360 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
363 DEBUG_Printf(DBG_CHN_MESG, ".\n");
366 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
368 if (!force && is_debug &&
369 DEBUG_ShouldContinue(&addr, code,
370 &DEBUG_CurrThread->exec_count))
373 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
374 if (newmode != DEBUG_CurrThread->dbg_mode)
376 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
377 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
378 DEBUG_CurrThread->dbg_mode = newmode;
383 if (is_debug || force) {
385 * Do a quiet backtrace so that we have an idea of what the situation
386 * is WRT the source files.
388 DEBUG_BackTrace(DEBUG_CurrTid, FALSE);
390 /* This is a real crash, dump some info */
391 DEBUG_InfoRegisters();
394 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
395 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
396 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
397 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
399 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
401 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
405 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_OVER) ||
406 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_INSTR)) {
410 /* Show where we crashed */
412 DEBUG_DisassembleInstruction(&addr);
414 /* resets list internal arguments so we can look at source code when needed */
415 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
416 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
421 static void DEBUG_ExceptionEpilog(void)
423 DEBUG_RestartExecution(DEBUG_CurrThread->exec_count);
425 * This will have gotten absorbed into the breakpoint info
426 * if it was used. Otherwise it would have been ignored.
427 * In any case, we don't mess with it any more.
429 if (DEBUG_CurrThread->exec_mode == EXEC_CONT)
430 DEBUG_CurrThread->exec_count = 0;
433 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force)
435 BOOL is_debug = FALSE;
436 THREADNAME_INFO *pThreadName;
439 assert(DEBUG_CurrThread);
441 switch (rec->ExceptionCode)
443 case EXCEPTION_BREAKPOINT:
444 case EXCEPTION_SINGLE_STEP:
447 case EXCEPTION_NAME_THREAD:
448 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
449 if (pThreadName->dwThreadID == -1)
450 pThread = DEBUG_CurrThread;
452 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
454 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
455 pThread->name, 9, NULL))
456 DEBUG_Printf (DBG_CHN_MESG,
457 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
458 pThread->tid, pThread->name);
462 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
464 /* pass exception to program except for debug exceptions */
470 /* print some infos */
471 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
472 first_chance ? "First chance exception" : "Unhandled exception");
473 switch (rec->ExceptionCode)
475 case EXCEPTION_INT_DIVIDE_BY_ZERO:
476 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
478 case EXCEPTION_INT_OVERFLOW:
479 DEBUG_Printf(DBG_CHN_MESG, "overflow");
481 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
482 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
484 case EXCEPTION_ILLEGAL_INSTRUCTION:
485 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
487 case EXCEPTION_STACK_OVERFLOW:
488 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
490 case EXCEPTION_PRIV_INSTRUCTION:
491 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
493 case EXCEPTION_ACCESS_VIOLATION:
494 if (rec->NumberParameters == 2)
495 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
496 rec->ExceptionInformation[0] ? "write" : "read",
497 rec->ExceptionInformation[1]);
499 DEBUG_Printf(DBG_CHN_MESG, "page fault");
501 case EXCEPTION_DATATYPE_MISALIGNMENT:
502 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
505 DEBUG_Printf(DBG_CHN_MESG, "^C");
508 DEBUG_Printf(DBG_CHN_MESG, "^C");
510 case EXCEPTION_CRITICAL_SECTION_WAIT:
515 addr.off = rec->ExceptionInformation[0];
517 DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section ");
518 DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE);
520 if (!DBG_IVAR(BreakOnCritSectTimeOut))
522 DEBUG_Printf(DBG_CHN_MESG, "\n");
526 case EXCEPTION_WINE_STUB:
528 char dll[32], name[64];
529 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
530 (char *)rec->ExceptionInformation[0] );
531 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
532 (char *)rec->ExceptionInformation[1] );
533 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
536 case EXCEPTION_VM86_INTx:
537 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
538 rec->ExceptionInformation[0]);
540 case EXCEPTION_VM86_STI:
541 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
543 case EXCEPTION_VM86_PICRETURN:
544 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
547 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
552 if (local_mode == automatic_mode)
554 DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode);
555 DEBUG_ExceptionEpilog();
556 return TRUE; /* terminate execution */
559 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode))
561 DEBUG_InteractiveP = TRUE;
564 DEBUG_ExceptionEpilog();
569 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de)
574 DEBUG_CurrPid = de->dwProcessId;
575 DEBUG_CurrTid = de->dwThreadId;
577 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
578 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
580 DEBUG_CurrThread = NULL;
582 switch (de->dwDebugEventCode)
584 case EXCEPTION_DEBUG_EVENT:
585 if (!DEBUG_CurrThread)
587 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
588 de->dwProcessId, de->dwThreadId);
592 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
593 de->dwProcessId, de->dwThreadId,
594 de->u.Exception.ExceptionRecord.ExceptionCode);
596 if (DEBUG_CurrProcess->continue_on_first_exception)
598 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
599 if (!DBG_IVAR(BreakOnAttach)) break;
602 DEBUG_context.ContextFlags = CONTEXT_CONTROL
604 #ifdef CONTEXT_SEGMENTS
607 #ifdef CONTEXT_DEBUG_REGISTERS
608 | CONTEXT_DEBUG_REGISTERS
612 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context))
614 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
618 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
619 de->u.Exception.dwFirstChance,
620 DEBUG_CurrThread->wait_for_first_exception);
621 if (DEBUG_CurrThread)
623 DEBUG_CurrThread->wait_for_first_exception = 0;
624 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
628 case CREATE_THREAD_DEBUG_EVENT:
629 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
630 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
632 if (DEBUG_CurrProcess == NULL)
634 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
637 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL)
639 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
643 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
645 de->u.CreateThread.hThread,
646 de->u.CreateThread.lpStartAddress,
647 de->u.CreateThread.lpThreadLocalBase);
648 if (!DEBUG_CurrThread)
650 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
653 DEBUG_InitCurrThread();
656 case CREATE_PROCESS_DEBUG_EVENT:
657 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
658 de->u.CreateProcessInfo.hProcess,
659 de->u.CreateProcessInfo.lpImageName);
661 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
662 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
663 de->dwProcessId, de->dwThreadId,
664 buffer, de->u.CreateProcessInfo.lpImageName,
665 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
666 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
667 de->u.CreateProcessInfo.nDebugInfoSize);
669 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
670 de->u.CreateProcessInfo.hProcess,
671 buffer[0] ? buffer : "<Debugged Process>");
672 if (DEBUG_CurrProcess == NULL)
674 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create process\n");
678 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
679 de->dwProcessId, de->dwThreadId,
680 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
682 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
684 de->u.CreateProcessInfo.hThread,
685 de->u.CreateProcessInfo.lpStartAddress,
686 de->u.CreateProcessInfo.lpThreadLocalBase);
687 if (!DEBUG_CurrThread)
689 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
693 DEBUG_InitCurrProcess();
694 DEBUG_InitCurrThread();
696 /* module is either PE, NE or ELF module (for WineLib), but all
697 * are loaded with wine, so load its symbols, then the main module
701 char* ptr = getenv("WINELOADER");
703 if (!ptr || DEBUG_ReadExecutableDbgInfo( ptr ) == DIL_ERROR)
704 DEBUG_ReadExecutableDbgInfo( "wine" );
707 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
708 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
712 case EXIT_THREAD_DEBUG_EVENT:
713 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
714 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
716 if (DEBUG_CurrThread == NULL)
718 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
721 /* FIXME: remove break point set on thread startup */
722 DEBUG_DelThread(DEBUG_CurrThread);
725 case EXIT_PROCESS_DEBUG_EVENT:
726 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
727 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
729 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)
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))
764 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
765 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
770 case UNLOAD_DLL_DEBUG_EVENT:
771 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
772 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
775 case OUTPUT_DEBUG_STRING_EVENT:
776 if (DEBUG_CurrThread == NULL)
778 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
782 DEBUG_ProcessGetString(buffer, sizeof(buffer),
783 DEBUG_CurrThread->process->handle,
784 de->u.DebugString.lpDebugStringData);
786 /* FIXME unicode de->u.DebugString.fUnicode ? */
787 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
788 de->dwProcessId, de->dwThreadId, buffer);
792 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
793 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
794 de->u.RipInfo.dwType);
798 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
799 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
805 static void DEBUG_ResumeDebuggee(DWORD cont)
807 DEBUG_ExceptionEpilog();
809 DEBUG_Printf(DBG_CHN_TRACE,
810 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
812 DEBUG_context.Eip, DEBUG_context.EFlags,
816 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
818 DEBUG_InteractiveP = FALSE;
819 if (DEBUG_CurrThread)
821 if (!SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context))
822 DEBUG_Printf(DBG_CHN_MESG, "Cannot set ctx on %lu\n", DEBUG_CurrTid);
823 DEBUG_CurrThread->wait_for_first_exception = 0;
825 if (!ContinueDebugEvent(DEBUG_CurrPid, DEBUG_CurrTid, cont))
826 DEBUG_Printf(DBG_CHN_MESG, "Cannot continue on %lu (%lu)\n",
827 DEBUG_CurrTid, cont);
830 void DEBUG_WaitNextException(DWORD cont, int count, int mode)
834 if (cont == DBG_CONTINUE)
836 DEBUG_CurrThread->exec_count = count;
837 DEBUG_CurrThread->exec_mode = mode;
839 DEBUG_ResumeDebuggee(cont);
841 while (DEBUG_CurrProcess && WaitForDebugEvent(&de, INFINITE))
843 if (DEBUG_HandleDebugEvent(&de)) break;
844 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
846 if (!DEBUG_CurrProcess) return;
847 DEBUG_InteractiveP = TRUE;
849 DEBUG_Printf(DBG_CHN_TRACE,
850 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
852 DEBUG_context.Eip, DEBUG_context.EFlags,
856 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
860 static DWORD DEBUG_MainLoop(void)
864 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting on pid %lx\n", DEBUG_CurrPid);
866 /* wait for first exception */
867 while (WaitForDebugEvent(&de, INFINITE))
869 if (DEBUG_HandleDebugEvent(&de)) break;
870 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
872 if (local_mode == automatic_mode)
874 /* print some extra information */
875 DEBUG_Printf(DBG_CHN_MESG, "Modules:\n");
877 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
882 DEBUG_InteractiveP = TRUE;
885 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
890 static BOOL DEBUG_Start(LPSTR cmdLine)
892 PROCESS_INFORMATION info;
893 STARTUPINFOA startup;
895 memset(&startup, 0, sizeof(startup));
896 startup.cb = sizeof(startup);
897 startup.dwFlags = STARTF_USESHOWWINDOW;
898 startup.wShowWindow = SW_SHOWNORMAL;
900 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
903 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
904 FALSE, DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,
905 NULL, NULL, &startup, &info))
907 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
910 DEBUG_CurrPid = info.dwProcessId;
911 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
916 void DEBUG_Run(const char* args)
918 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
919 const char* pgm = (wmod) ? wmod->module_name : "none";
922 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
924 if (!DEBUG_LastCmdLine) {
925 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
928 DEBUG_Start(DEBUG_LastCmdLine);
932 static BOOL WINAPI DEBUG_CtrlCHandler(DWORD dwCtrlType)
934 if (dwCtrlType == CTRL_C_EVENT)
936 DEBUG_Printf(DBG_CHN_MESG, "Ctrl-C: stopping debuggee\n");
937 /* FIXME: since we likely have a single process, signal the first process
940 return DEBUG_ProcessList && DebugBreakProcess(DEBUG_ProcessList->handle);
945 static void DEBUG_InitConsole(void)
951 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
952 * stdout and stderr streams
954 if (DBG_IVAR(UseXTerm))
960 /* this would be nicer for output */
963 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c);
965 /* sets the console's window width accordingly */
970 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &sr);
972 /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */
973 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode))
974 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode | WINE_ENABLE_LINE_INPUT_EMACS);
976 /* set our control-C handler */
977 SetConsoleCtrlHandler(DEBUG_CtrlCHandler, TRUE);
980 static int DEBUG_Usage(void)
982 DEBUG_Printf(DBG_CHN_MESG, "Usage: winedbg [--debugmsg dbgoptions] [--auto] [--gdb] cmdline\n" );
986 int main(int argc, char** argv)
989 unsigned gdb_flags = 0;
991 /* Initialize internal vars (types must have been initialized before) */
992 if (!DEBUG_IntVarsRW(TRUE)) return -1;
995 while (argc > 1 && argv[1][0] == '-')
997 if (!strcmp( argv[1], "--auto" ))
999 if (local_mode != none_mode) return DEBUG_Usage();
1000 local_mode = automatic_mode;
1001 /* force some internal variables */
1002 DBG_IVAR(UseXTerm) = 0;
1003 DBG_IVAR(BreakOnDllLoad) = 0;
1004 DBG_IVAR(ConChannelMask) = 0;
1005 DBG_IVAR(StdChannelMask) = DBG_CHN_MESG;
1009 if (!strcmp( argv[1], "--gdb" ))
1011 if (local_mode != none_mode) return DEBUG_Usage();
1012 local_mode = gdb_mode;
1016 if (strcmp( argv[1], "--no-start") == 0 && local_mode == gdb_mode)
1019 argc--; argv++; /* as we don't use argv[0] */
1022 if (strcmp(argv[1], "--with-xterm") == 0 && local_mode == gdb_mode)
1025 argc--; argv++; /* as we don't use argv[0] */
1028 if (!strcmp( argv[1], "--debugmsg" ) && argv[2])
1030 wine_dbg_parse_options( argv[2] );
1035 return DEBUG_Usage();
1038 if (local_mode == none_mode) local_mode = winedbg_mode;
1040 /* try the from <myself> pid */
1041 if (DEBUG_CurrPid == 0 && argc == 2)
1045 DEBUG_CurrPid = strtol(argv[1], &ptr, 10);
1046 if (DEBUG_CurrPid == 0 || ptr == NULL ||
1047 !DEBUG_Attach(DEBUG_CurrPid, local_mode != gdb_mode))
1051 /* try the from <myself> pid evt (Win32 JIT debugger) */
1052 if (DEBUG_CurrPid == 0 && argc == 3)
1058 if ((pid = strtol(argv[1], &ptr, 10)) != 0 && ptr != NULL &&
1059 (hEvent = (HANDLE)strtol(argv[2], &ptr, 10)) != 0 && ptr != NULL)
1061 if (!DEBUG_Attach(pid, TRUE))
1063 /* don't care about result */
1067 if (!SetEvent(hEvent))
1069 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
1072 CloseHandle(hEvent);
1073 DEBUG_CurrPid = pid;
1077 if (DEBUG_CurrPid == 0 && argc > 1)
1082 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
1085 for (i = 1; i < argc; i++)
1087 len += strlen(argv[i]) + 1;
1088 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
1089 strcat(cmdLine, argv[i]);
1090 cmdLine[len - 2] = ' ';
1091 cmdLine[len - 1] = '\0';
1094 if (!DEBUG_Start(cmdLine))
1096 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
1099 DBG_free(DEBUG_LastCmdLine);
1100 DEBUG_LastCmdLine = cmdLine;
1102 /* don't save local vars in gdb mode */
1103 if (local_mode == gdb_mode && DEBUG_CurrPid)
1104 return DEBUG_GdbRemote(gdb_flags);
1106 /* Initialize the type handling stuff. */
1108 DEBUG_InitCVDataTypes();
1110 DEBUG_InitConsole();
1112 retv = DEBUG_MainLoop();
1113 /* don't save modified variables in auto mode */
1114 if (local_mode != automatic_mode) DEBUG_IntVarsRW(FALSE);
1120 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");