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 "wine/library.h"
36 DBG_PROCESS* DEBUG_CurrProcess = NULL;
37 DBG_THREAD* DEBUG_CurrThread = NULL;
40 CONTEXT DEBUG_context;
41 BOOL DEBUG_InteractiveP = FALSE;
42 static BOOL DEBUG_InException = 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 if (p->delayed_bp[i].is_symbol)
201 DBG_free(p->delayed_bp[i].u.symbol.name);
203 DBG_free(p->delayed_bp);
204 if (p->prev) p->prev->next = p->next;
205 if (p->next) p->next->prev = p->prev;
206 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
207 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
208 DBG_free((char*)p->imageName);
212 static void DEBUG_InitCurrProcess(void)
216 BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
220 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
223 BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
229 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
232 && ReadProcessMemory(hp, ad, buffer, size, &sz))
238 DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
243 for (t = p->threads; t; t = t->next)
244 if (t->tid == tid) break;
248 DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
249 HANDLE h, LPVOID start, LPVOID teb)
251 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
260 t->wait_for_first_exception = 0;
261 t->exec_mode = EXEC_CONT;
264 snprintf(t->name, sizeof(t->name), "%08lx", tid);
267 t->next = p->threads;
269 if (p->threads) p->threads->prev = t;
275 static void DEBUG_InitCurrThread(void)
277 if (DEBUG_CurrThread->start) {
278 if (DEBUG_CurrThread->process->num_threads == 1 ||
279 DBG_IVAR(BreakAllThreadsStartup)) {
282 DEBUG_SetBreakpoints(FALSE);
284 value.cookie = DV_TARGET;
286 value.addr.off = (DWORD)DEBUG_CurrThread->start;
287 DEBUG_AddBreakpointFromValue(&value);
288 DEBUG_SetBreakpoints(TRUE);
291 DEBUG_CurrThread->wait_for_first_exception = 1;
295 void DEBUG_DelThread(DBG_THREAD* t)
297 if (t->prev) t->prev->next = t->next;
298 if (t->next) t->next->prev = t->prev;
299 if (t == t->process->threads) t->process->threads = t->next;
300 t->process->num_threads--;
301 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
305 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de);
307 /******************************************************************
310 * Sets the debuggee to <pid>
311 * cofe instructs winedbg what to do when first exception is received
312 * (break=FALSE, continue=TRUE)
313 * wfe is set to TRUE if DEBUG_Attach should also proceed with all debug events
314 * until the first exception is received (aka: attach to an already running process)
316 BOOL DEBUG_Attach(DWORD pid, BOOL cofe, BOOL wfe)
320 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE;
322 if (!DebugActiveProcess(pid)) {
323 DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError());
324 DEBUG_DelProcess(DEBUG_CurrProcess);
327 DEBUG_CurrProcess->continue_on_first_exception = cofe;
329 if (wfe) /* shall we proceed all debug events until we get an exception ? */
331 DEBUG_InteractiveP = FALSE;
332 while (DEBUG_CurrProcess && WaitForDebugEvent(&de, INFINITE))
334 if (DEBUG_HandleDebugEvent(&de)) break;
335 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
337 if (DEBUG_CurrProcess) DEBUG_InteractiveP = TRUE;
342 BOOL DEBUG_Detach(void)
344 /* remove all set breakpoints in debuggee code */
345 DEBUG_SetBreakpoints(FALSE);
346 /* needed for single stepping (ugly).
347 * should this be handled inside the server ??? */
349 DEBUG_context.EFlags &= ~STEP_FLAG;
351 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
352 DebugActiveProcessStop(DEBUG_CurrProcess->pid);
353 DEBUG_DelProcess(DEBUG_CurrProcess);
354 /* FIXME: should zero out the symbol table too */
358 static BOOL DEBUG_FetchContext(void)
360 DEBUG_context.ContextFlags = CONTEXT_CONTROL
362 #ifdef CONTEXT_SEGMENTS
365 #ifdef CONTEXT_DEBUG_REGISTERS
366 | CONTEXT_DEBUG_REGISTERS
369 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context))
371 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
377 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
382 DEBUG_InException = TRUE;
383 DEBUG_GetCurrentAddress(&addr);
384 DEBUG_SuspendExecution();
389 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
391 switch(DEBUG_GetSelectorType(addr.seg))
394 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
397 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
400 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
403 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
406 DEBUG_Printf(DBG_CHN_MESG, ".\n");
409 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
411 if (!force && is_debug &&
412 DEBUG_ShouldContinue(&addr, code,
413 &DEBUG_CurrThread->exec_count))
416 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
417 if (newmode != DEBUG_CurrThread->dbg_mode)
419 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
420 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
421 DEBUG_CurrThread->dbg_mode = newmode;
426 if (is_debug || force) {
428 * Do a quiet backtrace so that we have an idea of what the situation
429 * is WRT the source files.
431 DEBUG_BackTrace(DEBUG_CurrTid, FALSE);
433 /* This is a real crash, dump some info */
434 DEBUG_InfoRegisters(&DEBUG_context);
437 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
438 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
439 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
440 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
442 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
444 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
448 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_OVER) ||
449 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_INSTR)) {
453 /* Show where we crashed */
455 DEBUG_DisassembleInstruction(&addr);
457 /* resets list internal arguments so we can look at source code when needed */
458 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
459 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
464 static void DEBUG_ExceptionEpilog(void)
466 DEBUG_RestartExecution(DEBUG_CurrThread->exec_count);
468 * This will have gotten absorbed into the breakpoint info
469 * if it was used. Otherwise it would have been ignored.
470 * In any case, we don't mess with it any more.
472 if (DEBUG_CurrThread->exec_mode == EXEC_CONT)
473 DEBUG_CurrThread->exec_count = 0;
474 DEBUG_InException = FALSE;
477 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force)
479 BOOL is_debug = FALSE;
480 THREADNAME_INFO *pThreadName;
483 assert(DEBUG_CurrThread);
485 switch (rec->ExceptionCode)
487 case EXCEPTION_BREAKPOINT:
488 case EXCEPTION_SINGLE_STEP:
491 case EXCEPTION_NAME_THREAD:
492 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
493 if (pThreadName->dwThreadID == -1)
494 pThread = DEBUG_CurrThread;
496 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
498 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
499 pThread->name, 9, NULL))
500 DEBUG_Printf (DBG_CHN_MESG,
501 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
502 pThread->tid, pThread->name);
506 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
508 /* pass exception to program except for debug exceptions */
514 /* print some infos */
515 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
516 first_chance ? "First chance exception" : "Unhandled exception");
517 switch (rec->ExceptionCode)
519 case EXCEPTION_INT_DIVIDE_BY_ZERO:
520 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
522 case EXCEPTION_INT_OVERFLOW:
523 DEBUG_Printf(DBG_CHN_MESG, "overflow");
525 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
526 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
528 case EXCEPTION_ILLEGAL_INSTRUCTION:
529 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
531 case EXCEPTION_STACK_OVERFLOW:
532 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
534 case EXCEPTION_PRIV_INSTRUCTION:
535 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
537 case EXCEPTION_ACCESS_VIOLATION:
538 if (rec->NumberParameters == 2)
539 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
540 rec->ExceptionInformation[0] ? "write" : "read",
541 rec->ExceptionInformation[1]);
543 DEBUG_Printf(DBG_CHN_MESG, "page fault");
545 case EXCEPTION_DATATYPE_MISALIGNMENT:
546 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
549 DEBUG_Printf(DBG_CHN_MESG, "^C");
552 DEBUG_Printf(DBG_CHN_MESG, "^C");
554 case STATUS_POSSIBLE_DEADLOCK:
559 addr.off = rec->ExceptionInformation[0];
561 DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section ");
562 DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE);
564 if (!DBG_IVAR(BreakOnCritSectTimeOut))
566 DEBUG_Printf(DBG_CHN_MESG, "\n");
570 case EXCEPTION_WINE_STUB:
572 char dll[32], name[64];
573 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
574 (char *)rec->ExceptionInformation[0] );
575 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
576 (char *)rec->ExceptionInformation[1] );
577 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
580 case EXCEPTION_WINE_ASSERTION:
581 DEBUG_Printf(DBG_CHN_MESG, "assertion failed");
583 case EXCEPTION_VM86_INTx:
584 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
585 rec->ExceptionInformation[0]);
587 case EXCEPTION_VM86_STI:
588 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
590 case EXCEPTION_VM86_PICRETURN:
591 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
593 case EXCEPTION_FLT_DENORMAL_OPERAND:
594 DEBUG_Printf(DBG_CHN_MESG, "denormal float operand");
596 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
597 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
599 case EXCEPTION_FLT_INEXACT_RESULT:
600 DEBUG_Printf(DBG_CHN_MESG, "inexact float result");
602 case EXCEPTION_FLT_INVALID_OPERATION:
603 DEBUG_Printf(DBG_CHN_MESG, "invalid float operation");
605 case EXCEPTION_FLT_OVERFLOW:
606 DEBUG_Printf(DBG_CHN_MESG, "floating pointer overflow");
608 case EXCEPTION_FLT_UNDERFLOW:
609 DEBUG_Printf(DBG_CHN_MESG, "floating pointer underflow");
611 case EXCEPTION_FLT_STACK_CHECK:
612 DEBUG_Printf(DBG_CHN_MESG, "floating point stack check");
615 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
620 if (local_mode == automatic_mode)
622 DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode);
623 DEBUG_ExceptionEpilog();
624 return TRUE; /* terminate execution */
627 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode))
629 DEBUG_InteractiveP = TRUE;
632 DEBUG_ExceptionEpilog();
637 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de)
642 DEBUG_CurrPid = de->dwProcessId;
643 DEBUG_CurrTid = de->dwThreadId;
645 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
646 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
648 DEBUG_CurrThread = NULL;
650 switch (de->dwDebugEventCode)
652 case EXCEPTION_DEBUG_EVENT:
653 if (!DEBUG_CurrThread)
655 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
656 de->dwProcessId, de->dwThreadId);
660 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
661 de->dwProcessId, de->dwThreadId,
662 de->u.Exception.ExceptionRecord.ExceptionCode);
664 if (DEBUG_CurrProcess->continue_on_first_exception)
666 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
667 if (!DBG_IVAR(BreakOnAttach)) break;
670 if (DEBUG_FetchContext())
672 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
673 de->u.Exception.dwFirstChance,
674 DEBUG_CurrThread->wait_for_first_exception);
675 if (!ret && DEBUG_CurrThread)
677 DEBUG_CurrThread->wait_for_first_exception = 0;
678 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
683 case CREATE_THREAD_DEBUG_EVENT:
684 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
685 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
687 if (DEBUG_CurrProcess == NULL)
689 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
692 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL)
694 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
698 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
700 de->u.CreateThread.hThread,
701 de->u.CreateThread.lpStartAddress,
702 de->u.CreateThread.lpThreadLocalBase);
703 if (!DEBUG_CurrThread)
705 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
708 DEBUG_InitCurrThread();
711 case CREATE_PROCESS_DEBUG_EVENT:
712 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
713 de->u.CreateProcessInfo.hProcess,
714 de->u.CreateProcessInfo.lpImageName);
716 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
717 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
718 de->dwProcessId, de->dwThreadId,
719 buffer, de->u.CreateProcessInfo.lpImageName,
720 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
721 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
722 de->u.CreateProcessInfo.nDebugInfoSize);
724 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
725 de->u.CreateProcessInfo.hProcess,
726 buffer[0] ? buffer : "<Debugged Process>");
727 if (DEBUG_CurrProcess == NULL)
729 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create process\n");
733 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
734 de->dwProcessId, de->dwThreadId,
735 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
737 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
739 de->u.CreateProcessInfo.hThread,
740 de->u.CreateProcessInfo.lpStartAddress,
741 de->u.CreateProcessInfo.lpThreadLocalBase);
742 if (!DEBUG_CurrThread)
744 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
748 DEBUG_InitCurrProcess();
749 DEBUG_InitCurrThread();
751 /* module is either PE, NE or ELF module (for WineLib), but all
752 * are loaded with wine, so load its symbols, then the main module
756 char* ptr = getenv("WINELOADER");
758 if (!ptr || DEBUG_ReadExecutableDbgInfo( ptr ) == DIL_ERROR)
759 DEBUG_ReadExecutableDbgInfo( "wine" );
762 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
763 de->u.CreateProcessInfo.lpBaseOfImage);
767 case EXIT_THREAD_DEBUG_EVENT:
768 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
769 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
771 if (DEBUG_CurrThread == NULL)
773 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
776 /* FIXME: remove break point set on thread startup */
777 DEBUG_DelThread(DEBUG_CurrThread);
780 case EXIT_PROCESS_DEBUG_EVENT:
781 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
782 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
784 if (DEBUG_CurrProcess == NULL)
786 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
790 DEBUG_SetBreakpoints(FALSE);
791 /* kill last thread */
792 DEBUG_DelThread(DEBUG_CurrProcess->threads);
793 DEBUG_DelProcess(DEBUG_CurrProcess);
795 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
798 case LOAD_DLL_DEBUG_EVENT:
799 if (DEBUG_CurrThread == NULL)
801 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
804 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
805 DEBUG_CurrThread->process->handle,
806 de->u.LoadDll.lpImageName);
808 /* FIXME unicode: de->u.LoadDll.fUnicode */
809 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
810 de->dwProcessId, de->dwThreadId,
811 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
812 de->u.LoadDll.dwDebugInfoFileOffset,
813 de->u.LoadDll.nDebugInfoSize);
815 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, de->u.LoadDll.lpBaseOfDll);
816 DEBUG_CheckDelayedBP();
817 if (DBG_IVAR(BreakOnDllLoad))
819 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
820 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
821 ret = DEBUG_FetchContext();
825 case UNLOAD_DLL_DEBUG_EVENT:
826 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
827 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
830 case OUTPUT_DEBUG_STRING_EVENT:
831 if (DEBUG_CurrThread == NULL)
833 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
837 DEBUG_ProcessGetString(buffer, sizeof(buffer),
838 DEBUG_CurrThread->process->handle,
839 de->u.DebugString.lpDebugStringData);
841 /* FIXME unicode de->u.DebugString.fUnicode ? */
842 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
843 de->dwProcessId, de->dwThreadId, buffer);
847 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
848 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
849 de->u.RipInfo.dwType);
853 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
854 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
860 static void DEBUG_ResumeDebuggee(DWORD cont)
862 if (DEBUG_InException)
864 DEBUG_ExceptionEpilog();
866 DEBUG_Printf(DBG_CHN_TRACE,
867 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
869 DEBUG_context.Eip, DEBUG_context.EFlags,
873 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
875 if (DEBUG_CurrThread)
877 if (!SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context))
878 DEBUG_Printf(DBG_CHN_MESG, "Cannot set ctx on %lu\n", DEBUG_CurrTid);
879 DEBUG_CurrThread->wait_for_first_exception = 0;
882 DEBUG_InteractiveP = FALSE;
883 if (!ContinueDebugEvent(DEBUG_CurrPid, DEBUG_CurrTid, cont))
884 DEBUG_Printf(DBG_CHN_MESG, "Cannot continue on %lu (%lu)\n",
885 DEBUG_CurrTid, cont);
888 void DEBUG_WaitNextException(DWORD cont, int count, int mode)
892 if (cont == DBG_CONTINUE)
894 DEBUG_CurrThread->exec_count = count;
895 DEBUG_CurrThread->exec_mode = mode;
897 DEBUG_ResumeDebuggee(cont);
899 while (DEBUG_CurrProcess && WaitForDebugEvent(&de, INFINITE))
901 if (DEBUG_HandleDebugEvent(&de)) break;
902 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
904 if (!DEBUG_CurrProcess) return;
905 DEBUG_InteractiveP = TRUE;
907 DEBUG_Printf(DBG_CHN_TRACE,
908 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
910 DEBUG_context.Eip, DEBUG_context.EFlags,
914 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
918 static DWORD DEBUG_MainLoop(void)
922 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting on pid %lx\n", DEBUG_CurrPid);
924 /* wait for first exception */
925 while (WaitForDebugEvent(&de, INFINITE))
927 if (DEBUG_HandleDebugEvent(&de)) break;
928 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
930 if (local_mode == automatic_mode)
932 /* print some extra information */
933 DEBUG_Printf(DBG_CHN_MESG, "Modules:\n");
935 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
940 DEBUG_InteractiveP = TRUE;
943 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
948 static BOOL DEBUG_Start(LPSTR cmdLine)
950 PROCESS_INFORMATION info;
951 STARTUPINFOA startup;
953 memset(&startup, 0, sizeof(startup));
954 startup.cb = sizeof(startup);
955 startup.dwFlags = STARTF_USESHOWWINDOW;
956 startup.wShowWindow = SW_SHOWNORMAL;
958 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
961 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
962 FALSE, DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,
963 NULL, NULL, &startup, &info))
965 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
968 DEBUG_CurrPid = info.dwProcessId;
969 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
974 void DEBUG_Run(const char* args)
976 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
977 const char* pgm = (wmod) ? wmod->module_name : "none";
980 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
982 if (!DEBUG_LastCmdLine) {
983 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
986 DEBUG_Start(DEBUG_LastCmdLine);
990 BOOL DEBUG_InterruptDebuggee(void)
992 DEBUG_Printf(DBG_CHN_MESG, "Ctrl-C: stopping debuggee\n");
993 /* FIXME: since we likely have a single process, signal the first process
996 return DEBUG_ProcessList && DebugBreakProcess(DEBUG_ProcessList->handle);
999 static BOOL WINAPI DEBUG_CtrlCHandler(DWORD dwCtrlType)
1001 if (dwCtrlType == CTRL_C_EVENT)
1003 return DEBUG_InterruptDebuggee();
1008 static void DEBUG_InitConsole(void)
1010 /* set our control-C handler */
1011 SetConsoleCtrlHandler(DEBUG_CtrlCHandler, TRUE);
1013 /* set our own title */
1014 SetConsoleTitle("Wine Debugger");
1017 static int DEBUG_Usage(void)
1019 DEBUG_Printf(DBG_CHN_MESG, "Usage: winedbg [--debugmsg dbgoptions] [--auto] [--gdb] cmdline\n" );
1023 int main(int argc, char** argv)
1026 unsigned gdb_flags = 0;
1028 /* Initialize the type handling stuff. */
1030 DEBUG_InitCVDataTypes();
1032 /* Initialize internal vars (types must have been initialized before) */
1033 if (!DEBUG_IntVarsRW(TRUE)) return -1;
1036 while (argc > 1 && argv[1][0] == '-')
1038 if (!strcmp( argv[1], "--auto" ))
1040 if (local_mode != none_mode) return DEBUG_Usage();
1041 local_mode = automatic_mode;
1042 /* force some internal variables */
1043 DBG_IVAR(BreakOnDllLoad) = 0;
1044 DBG_IVAR(ConChannelMask) = 0;
1045 DBG_IVAR(StdChannelMask) = DBG_CHN_MESG;
1049 if (!strcmp( argv[1], "--gdb" ))
1051 if (local_mode != none_mode) return DEBUG_Usage();
1052 local_mode = gdb_mode;
1056 if (strcmp( argv[1], "--no-start") == 0 && local_mode == gdb_mode)
1059 argc--; argv++; /* as we don't use argv[0] */
1062 if (strcmp(argv[1], "--with-xterm") == 0 && local_mode == gdb_mode)
1065 argc--; argv++; /* as we don't use argv[0] */
1068 if (!strcmp( argv[1], "--debugmsg" ) && argv[2])
1070 wine_dbg_parse_options( argv[2] );
1075 return DEBUG_Usage();
1078 if (local_mode == none_mode) local_mode = winedbg_mode;
1080 /* try the form <myself> pid */
1081 if (DEBUG_CurrPid == 0 && argc == 2)
1085 DEBUG_CurrPid = strtol(argv[1], &ptr, 10);
1086 if (DEBUG_CurrPid == 0 || ptr == NULL ||
1087 !DEBUG_Attach(DEBUG_CurrPid, local_mode != gdb_mode, FALSE))
1091 /* try the form <myself> pid evt (Win32 JIT debugger) */
1092 if (DEBUG_CurrPid == 0 && argc == 3)
1098 if ((pid = strtol(argv[1], &ptr, 10)) != 0 && ptr != NULL &&
1099 (hEvent = (HANDLE)strtol(argv[2], &ptr, 10)) != 0 && ptr != NULL)
1101 if (!DEBUG_Attach(pid, TRUE, FALSE))
1103 /* don't care about result */
1107 if (!SetEvent(hEvent))
1109 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
1112 CloseHandle(hEvent);
1113 DEBUG_CurrPid = pid;
1117 if (DEBUG_CurrPid == 0 && argc > 1)
1122 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
1125 for (i = 1; i < argc; i++)
1127 len += strlen(argv[i]) + 1;
1128 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
1129 strcat(cmdLine, argv[i]);
1130 cmdLine[len - 2] = ' ';
1131 cmdLine[len - 1] = '\0';
1134 if (!DEBUG_Start(cmdLine))
1136 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
1139 DBG_free(DEBUG_LastCmdLine);
1140 DEBUG_LastCmdLine = cmdLine;
1142 /* don't save local vars in gdb mode */
1143 if (local_mode == gdb_mode && DEBUG_CurrPid)
1144 return DEBUG_GdbRemote(gdb_flags);
1146 DEBUG_InitConsole();
1148 retv = DEBUG_MainLoop();
1149 /* don't save modified variables in auto mode */
1150 if (local_mode != automatic_mode) DEBUG_IntVarsRW(FALSE);
1156 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");