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;
32 static DBG_PROCESS* proc = NULL;
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, ...)
49 va_start(valist, format);
50 len = wvsnprintf(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 = _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 static DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
130 for (p = proc; p; p = p->next)
131 if (p->pid == pid) break;
135 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h)
137 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
144 p->continue_on_first_exception = FALSE;
151 if (proc) proc->prev = p;
156 static void DEBUG_DelThread(DBG_THREAD* p);
158 static void DEBUG_DelProcess(DBG_PROCESS* p)
160 if (p->threads != NULL) {
161 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
162 while (p->threads) DEBUG_DelThread(p->threads);
164 if (p->prev) p->prev->next = p->next;
165 if (p->next) p->next->prev = p->prev;
166 if (p == proc) proc = p->next;
170 static void DEBUG_InitCurrProcess(void)
174 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
178 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
181 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
187 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
190 && ReadProcessMemory(hp, ad, buffer, size, &sz))
196 static DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
200 for (t = p->threads; t; t = t->next)
201 if (t->tid == tid) break;
205 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
206 HANDLE h, LPVOID start, LPVOID teb)
208 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
217 t->wait_for_first_exception = 0;
218 t->dbg_exec_mode = EXEC_CONT;
219 t->dbg_exec_count = 0;
222 t->next = p->threads;
224 if (p->threads) p->threads->prev = t;
230 static void DEBUG_InitCurrThread(void)
232 if (DEBUG_CurrThread->start) {
233 if (DEBUG_CurrThread->process->num_threads == 1 ||
234 DBG_IVAR(BreakAllThreadsStartup)) {
237 DEBUG_SetBreakpoints(FALSE);
239 value.cookie = DV_TARGET;
241 value.addr.off = (DWORD)DEBUG_CurrThread->start;
242 DEBUG_AddBreakpoint(&value, NULL);
243 DEBUG_SetBreakpoints(TRUE);
246 DEBUG_CurrThread->wait_for_first_exception = 1;
250 static void DEBUG_DelThread(DBG_THREAD* t)
252 if (t->prev) t->prev->next = t->next;
253 if (t->next) t->next->prev = t->prev;
254 if (t == t->process->threads) t->process->threads = t->next;
255 t->process->num_threads--;
259 static BOOL DEBUG_HandleException( EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force )
261 BOOL is_debug = FALSE;
264 /* FIXME: need for a configuration var ? */
265 /* pass to app first ??? */
266 /* if (first_chance && !force) return 0; */
268 switch (rec->ExceptionCode)
270 case EXCEPTION_BREAKPOINT:
271 case EXCEPTION_SINGLE_STEP:
278 /* print some infos */
279 DEBUG_Printf( DBG_CHN_MESG, "%s: ",
280 first_chance ? "First chance exception" : "Unhandled exception" );
281 switch (rec->ExceptionCode)
283 case EXCEPTION_INT_DIVIDE_BY_ZERO:
284 DEBUG_Printf( DBG_CHN_MESG, "divide by zero" );
286 case EXCEPTION_INT_OVERFLOW:
287 DEBUG_Printf( DBG_CHN_MESG, "overflow" );
289 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
290 DEBUG_Printf( DBG_CHN_MESG, "array bounds " );
292 case EXCEPTION_ILLEGAL_INSTRUCTION:
293 DEBUG_Printf( DBG_CHN_MESG, "illegal instruction" );
295 case EXCEPTION_STACK_OVERFLOW:
296 DEBUG_Printf( DBG_CHN_MESG, "stack overflow" );
298 case EXCEPTION_PRIV_INSTRUCTION:
299 DEBUG_Printf( DBG_CHN_MESG, "priviledged instruction" );
301 case EXCEPTION_ACCESS_VIOLATION:
302 if (rec->NumberParameters == 2)
303 DEBUG_Printf( DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
304 rec->ExceptionInformation[0] ? "write" : "read",
305 rec->ExceptionInformation[1] );
307 DEBUG_Printf( DBG_CHN_MESG, "page fault" );
309 case EXCEPTION_DATATYPE_MISALIGNMENT:
310 DEBUG_Printf( DBG_CHN_MESG, "Alignment" );
313 DEBUG_Printf( DBG_CHN_MESG, "^C" );
315 case EXCEPTION_CRITICAL_SECTION_WAIT:
316 DEBUG_Printf( DBG_CHN_MESG, "critical section %08lx wait failed",
317 rec->ExceptionInformation[0] );
318 if (!DBG_IVAR(BreakOnCritSectTimeOut))
322 DEBUG_Printf( DBG_CHN_MESG, "%08lx", rec->ExceptionCode );
325 DEBUG_Printf(DBG_CHN_MESG, "\n");
328 DEBUG_Printf(DBG_CHN_TRACE,
329 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
331 DEBUG_context.Eip, DEBUG_context.EFlags,
335 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
337 ret = DEBUG_Main( is_debug, force, rec->ExceptionCode );
339 DEBUG_Printf(DBG_CHN_TRACE,
340 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
342 DEBUG_context.Eip, DEBUG_context.EFlags,
346 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
351 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
356 DEBUG_CurrPid = de->dwProcessId;
357 DEBUG_CurrTid = de->dwThreadId;
363 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
364 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
366 DEBUG_CurrThread = NULL;
368 switch (de->dwDebugEventCode) {
369 case EXCEPTION_DEBUG_EVENT:
370 if (!DEBUG_CurrThread) {
371 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
372 de->dwProcessId, de->dwThreadId);
376 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
377 de->dwProcessId, de->dwThreadId,
378 de->u.Exception.ExceptionRecord.ExceptionCode);
380 if (DEBUG_CurrProcess->continue_on_first_exception) {
381 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
382 if (!DBG_IVAR(BreakOnAttach)) {
383 *cont = DBG_CONTINUE;
388 DEBUG_context.ContextFlags = CONTEXT_CONTROL
390 #ifdef CONTEXT_SEGMENTS
393 #ifdef CONTEXT_DEBUG_REGISTERS
394 | CONTEXT_DEBUG_REGISTERS
398 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
399 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
403 *cont = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
404 de->u.Exception.dwFirstChance,
405 DEBUG_CurrThread->wait_for_first_exception);
406 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_KILL) {
409 DEBUG_CurrThread->wait_for_first_exception = 0;
410 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
414 case CREATE_THREAD_DEBUG_EVENT:
415 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
416 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
418 if (DEBUG_CurrProcess == NULL) {
419 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
422 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
423 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
427 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
429 de->u.CreateThread.hThread,
430 de->u.CreateThread.lpStartAddress,
431 de->u.CreateThread.lpThreadLocalBase);
432 if (!DEBUG_CurrThread) {
433 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
436 DEBUG_InitCurrThread();
439 case CREATE_PROCESS_DEBUG_EVENT:
440 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
441 de->u.CreateProcessInfo.hProcess,
442 de->u.CreateProcessInfo.lpImageName);
444 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
445 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
446 de->dwProcessId, de->dwThreadId,
448 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
449 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
450 de->u.CreateProcessInfo.nDebugInfoSize);
452 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
453 if (DEBUG_CurrProcess->handle) {
454 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
457 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
459 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
460 de->u.CreateProcessInfo.hProcess);
461 if (DEBUG_CurrProcess == NULL) {
462 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
467 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
468 de->dwProcessId, de->dwThreadId,
469 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
471 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
473 de->u.CreateProcessInfo.hThread,
474 de->u.CreateProcessInfo.lpStartAddress,
475 de->u.CreateProcessInfo.lpThreadLocalBase);
476 if (!DEBUG_CurrThread) {
477 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
481 DEBUG_InitCurrProcess();
482 DEBUG_InitCurrThread();
484 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
485 DEBUG_CurrThread->process->handle,
486 de->u.CreateProcessInfo.lpImageName);
487 DEBUG_LoadModule32(buffer[0] ? buffer : "<Debugged process>",
488 de->u.CreateProcessInfo.hFile,
489 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
491 if (buffer[0]) /* we got a process name */
494 if (!GetBinaryTypeA( buffer, &type ))
496 /* not a Windows binary, assume it's a Unix executable then */
497 DOS_FULL_NAME fullname;
498 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
499 if (DOSFS_GetFullName( buffer, TRUE, &fullname ))
501 DEBUG_ReadExecutableDbgInfo( fullname.long_name );
506 /* if it is a Windows binary, or an invalid or missing file name,
507 * we use wine itself as the main executable */
508 DEBUG_ReadExecutableDbgInfo( "wine" );
511 case EXIT_THREAD_DEBUG_EVENT:
512 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
513 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
515 if (DEBUG_CurrThread == NULL) {
516 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
519 /* FIXME: remove break point set on thread startup */
520 DEBUG_DelThread(DEBUG_CurrThread);
523 case EXIT_PROCESS_DEBUG_EVENT:
524 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
525 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
527 if (DEBUG_CurrProcess == NULL) {
528 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
532 DEBUG_SetBreakpoints(FALSE);
533 /* kill last thread */
534 DEBUG_DelThread(DEBUG_CurrProcess->threads);
535 DEBUG_DelProcess(DEBUG_CurrProcess);
539 case LOAD_DLL_DEBUG_EVENT:
540 if (DEBUG_CurrThread == NULL) {
541 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
544 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
545 DEBUG_CurrThread->process->handle,
546 de->u.LoadDll.lpImageName);
548 /* FIXME unicode: de->u.LoadDll.fUnicode */
549 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
550 de->dwProcessId, de->dwThreadId,
551 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
552 de->u.LoadDll.dwDebugInfoFileOffset,
553 de->u.LoadDll.nDebugInfoSize);
555 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
558 case UNLOAD_DLL_DEBUG_EVENT:
559 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
560 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
563 case OUTPUT_DEBUG_STRING_EVENT:
564 if (DEBUG_CurrThread == NULL) {
565 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
569 DEBUG_ProcessGetString(buffer, sizeof(buffer),
570 DEBUG_CurrThread->process->handle,
571 de->u.DebugString.lpDebugStringData);
573 /* fixme unicode de->u.DebugString.fUnicode ? */
574 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
575 de->dwProcessId, de->dwThreadId, buffer);
579 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
580 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
581 de->u.RipInfo.dwType);
585 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
586 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
589 } __EXCEPT(wine_dbg) {
598 static DWORD DEBUG_MainLoop(DWORD pid)
604 DEBUG_Printf(DBG_CHN_MESG, " on pid %ld\n", pid);
606 while (ret && WaitForDebugEvent(&de, INFINITE)) {
607 ret = DEBUG_HandleDebugEvent(&de, &cont);
608 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
611 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %ld\n", pid);
616 int DEBUG_main(int argc, char** argv)
618 DWORD pid = 0, retv = 0;
621 /* Initialize the debugger heap. */
622 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
625 /* Initialize the type handling stuff. */
627 DEBUG_InitCVDataTypes();
629 /* Initialize internal vars (types must be initialized before) */
630 if (!DEBUG_IntVarsRW(TRUE)) return -1;
632 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
633 * stdout and stderr streams
635 if (DBG_IVAR(UseXTerm)) {
638 /* This is a hack: it forces creation of an xterm, not done by default */
639 pos.X = 0; pos.Y = 1;
640 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
643 DEBUG_Printf(DBG_CHN_MESG, "Starting WineDbg... ");
647 if ((pid = atoi(argv[1])) != 0 && (hEvent = atoi(argv[2])) != 0) {
648 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) goto leave;
649 DEBUG_CurrProcess->continue_on_first_exception = TRUE;
651 if (!DebugActiveProcess(pid)) {
652 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
653 pid, GetLastError());
665 DEBUG_Printf(DBG_CHN_MESG, "\n");
667 pid = strtol(org = readline("Enter pid to debug: "), &ptr, 0);
668 if (pid && ptr && ptr != org && *ptr == '\0') {
669 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) goto leave;
671 if (!DebugActiveProcess(pid)) {
672 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
673 pid, GetLastError());
682 PROCESS_INFORMATION info;
683 STARTUPINFOA startup;
685 memset(&startup, 0, sizeof(startup));
686 startup.cb = sizeof(startup);
687 startup.dwFlags = STARTF_USESHOWWINDOW;
688 startup.wShowWindow = SW_SHOWNORMAL;
690 if (!CreateProcess(NULL, argv[1], NULL, NULL,
691 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
692 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", argv[1]);
695 pid = info.dwProcessId;
698 if (pid) retv = DEBUG_MainLoop(pid);
700 /* saves modified variables */
701 DEBUG_IntVarsRW(FALSE);