1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
19 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(winedbg);
28 DBG_PROCESS* DEBUG_CurrProcess = NULL;
29 DBG_THREAD* DEBUG_CurrThread = NULL;
30 CONTEXT DEBUG_context;
32 static DBG_PROCESS* proc = NULL;
33 static BOOL bBreakAllThreads = FALSE;
35 static BOOL DEBUG_Init(void)
40 DWORD count = sizeof(val);
42 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
43 if (!RegQueryValueExA(hkey, "BreakAllThreadsStartup", 0, &type, (LPSTR)&val, &count)) {
44 bBreakAllThreads = val;
51 static WINE_EXCEPTION_FILTER(wine_dbg)
53 DEBUG_ExternalDebugger();
54 fprintf(stderr, "\nwine_dbg: Exception %lx\n", GetExceptionCode());
55 return EXCEPTION_EXECUTE_HANDLER;
58 static DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
62 for (p = proc; p; p = p->next)
63 if (p->pid == pid) break;
67 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h)
69 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
81 if (proc) proc->prev = p;
86 static void DEBUG_DelThread(DBG_THREAD* p);
88 static void DEBUG_DelProcess(DBG_PROCESS* p)
90 if (p->threads != NULL) {
91 ERR("Shouldn't happen\n");
92 while (p->threads) DEBUG_DelThread(p->threads);
94 if (p->prev) p->prev->next = p->next;
95 if (p->next) p->next->prev = p->prev;
96 if (p == proc) proc = p->next;
100 static void DEBUG_InitCurrProcess(void)
104 * Initialize the debugger heap.
106 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
110 * Initialize the type handling stuff.
113 DEBUG_InitCVDataTypes();
116 * In some cases we can read the stabs information directly
117 * from the executable. If this is the case, we don't need
118 * to bother with trying to read a symbol file, as the stabs
119 * also have line number and local variable information.
120 * As long as gcc is used for the compiler, stabs will
121 * be the default. On SVr4, DWARF could be used, but we
122 * don't grok that yet, and in this case we fall back to using
125 if( DEBUG_ReadExecutableDbgInfo() == FALSE )
127 char* symfilename = "wine.sym";
129 HKEY hWineConf, hkey;
131 char symbolTableFile[256];
133 if (-1 == stat(symfilename, &statbuf) )
134 symfilename = LIBDIR "wine.sym";
136 strcpy(symbolTableFile, symfilename);
137 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config", &hWineConf)) {
138 if (!RegOpenKeyA(hWineConf, "wine", &hkey)) {
139 count = sizeof(symbolTableFile);
140 RegQueryValueA(hkey, "SymbolTableFile", symbolTableFile, &count);
143 RegCloseKey(hWineConf);
145 DEBUG_ReadSymbolTable(symbolTableFile);
147 DEBUG_LoadEntryPoints(NULL);
148 DEBUG_ProcessDeferredDebug();
151 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
155 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
158 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
164 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
167 && ReadProcessMemory(hp, ad, buffer, size, &sz))
173 static DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
177 for (t = p->threads; t; t = t->next)
178 if (t->tid == tid) break;
182 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
183 HANDLE h, LPVOID start, LPVOID teb)
185 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
194 t->wait_for_first_exception = 0;
195 t->dbg_exec_mode = EXEC_CONT;
196 t->dbg_exec_count = 0;
199 t->next = p->threads;
201 if (p->threads) p->threads->prev = t;
207 static void DEBUG_InitCurrThread(void)
209 if (!Options.debug) return;
211 if (DEBUG_CurrThread->start) {
212 if (DEBUG_CurrThread->process->num_threads == 1 || bBreakAllThreads) {
215 DEBUG_SetBreakpoints(FALSE);
217 value.cookie = DV_TARGET;
219 value.addr.off = (DWORD)DEBUG_CurrThread->start;
220 DEBUG_AddBreakpoint(&value);
221 DEBUG_SetBreakpoints(TRUE);
224 DEBUG_CurrThread->wait_for_first_exception = 1;
228 static void DEBUG_DelThread(DBG_THREAD* t)
230 if (t->prev) t->prev->next = t->next;
231 if (t->next) t->next->prev = t->prev;
232 if (t == t->process->threads) t->process->threads = t->next;
233 t->process->num_threads--;
237 static BOOL DEBUG_HandleException( EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force )
239 BOOL is_debug = FALSE;
242 if (first_chance && !Options.debug && !force ) return 0; /* pass to app first */
244 switch (rec->ExceptionCode)
246 case EXCEPTION_BREAKPOINT:
247 case EXCEPTION_SINGLE_STEP:
251 if (!Options.debug) DEBUG_Exit(0);
257 /* print some infos */
258 fprintf( stderr, "%s: ",
259 first_chance ? "First chance exception" : "Unhandled exception" );
260 switch(rec->ExceptionCode)
262 case EXCEPTION_INT_DIVIDE_BY_ZERO:
263 fprintf( stderr, "divide by zero" );
265 case EXCEPTION_INT_OVERFLOW:
266 fprintf( stderr, "overflow" );
268 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
269 fprintf( stderr, "array bounds " );
271 case EXCEPTION_ILLEGAL_INSTRUCTION:
272 fprintf( stderr, "illegal instruction" );
274 case EXCEPTION_STACK_OVERFLOW:
275 fprintf( stderr, "stack overflow" );
277 case EXCEPTION_PRIV_INSTRUCTION:
278 fprintf( stderr, "priviledged instruction" );
280 case EXCEPTION_ACCESS_VIOLATION:
281 if (rec->NumberParameters == 2)
282 fprintf( stderr, "page fault on %s access to 0x%08lx",
283 rec->ExceptionInformation[0] ? "write" : "read",
284 rec->ExceptionInformation[1] );
286 fprintf( stderr, "page fault" );
288 case EXCEPTION_DATATYPE_MISALIGNMENT:
289 fprintf( stderr, "Alignment" );
292 fprintf( stderr, "^C" );
294 case EXCEPTION_CRITICAL_SECTION_WAIT:
295 fprintf( stderr, "critical section %08lx wait failed",
296 rec->ExceptionInformation[0] );
299 fprintf( stderr, "%08lx", rec->ExceptionCode );
305 fprintf(stderr, "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
306 DEBUG_context.Eip, DEBUG_context.EFlags,
307 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
310 ret = DEBUG_Main( is_debug, force, rec->ExceptionCode );
312 fprintf(stderr, "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
313 DEBUG_context.Eip, DEBUG_context.EFlags,
314 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
320 static DWORD DEBUG_HandleDebugEvent(DEBUG_EVENT* de)
328 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
329 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
331 DEBUG_CurrThread = NULL;
333 switch (de->dwDebugEventCode) {
334 case EXCEPTION_DEBUG_EVENT:
335 if (!DEBUG_CurrThread) break;
337 TRACE("%08lx:%08lx: exception code=%08lx %d\n",
338 de->dwProcessId, de->dwThreadId,
339 de->u.Exception.ExceptionRecord.ExceptionCode,
340 DEBUG_CurrThread->wait_for_first_exception);
342 DEBUG_context.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_SEGMENTS|CONTEXT_DEBUG_REGISTERS;
343 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
344 WARN("Can't get thread's context\n");
348 TRACE("%p:%p\n", de->u.Exception.ExceptionRecord.ExceptionAddress,
349 (void*)DEBUG_context.Eip);
351 cont = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
352 de->u.Exception.dwFirstChance,
353 DEBUG_CurrThread->wait_for_first_exception);
355 if (DEBUG_CurrThread->wait_for_first_exception) {
356 DEBUG_CurrThread->wait_for_first_exception = 0;
361 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
364 case CREATE_THREAD_DEBUG_EVENT:
365 TRACE("%08lx:%08lx: create thread D @%p\n", de->dwProcessId, de->dwThreadId,
366 de->u.CreateThread.lpStartAddress);
368 if (DEBUG_CurrProcess == NULL) {
369 ERR("Unknown process\n");
372 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
373 TRACE("Thread already listed, skipping\n");
377 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
379 de->u.CreateThread.hThread,
380 de->u.CreateThread.lpStartAddress,
381 de->u.CreateThread.lpThreadLocalBase);
382 if (!DEBUG_CurrThread) {
383 ERR("Couldn't create thread\n");
386 DEBUG_InitCurrThread();
389 case CREATE_PROCESS_DEBUG_EVENT:
390 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
391 de->u.CreateProcessInfo.hProcess,
392 de->u.LoadDll.lpImageName);
394 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
395 TRACE("%08lx:%08lx: create process %s @%p\n",
396 de->dwProcessId, de->dwThreadId,
398 de->u.CreateProcessInfo.lpStartAddress);
400 if (DEBUG_GetProcess(de->dwProcessId) != NULL) {
401 TRACE("Skipping already defined process\n");
404 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
405 de->u.CreateProcessInfo.hProcess);
406 if (DEBUG_CurrProcess == NULL) {
407 ERR("Unknown process\n");
411 TRACE("%08lx:%08lx: create thread I @%p\n", de->dwProcessId, de->dwThreadId,
412 de->u.CreateProcessInfo.lpStartAddress);
414 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
416 de->u.CreateProcessInfo.hThread,
417 de->u.CreateProcessInfo.lpStartAddress,
418 de->u.CreateProcessInfo.lpThreadLocalBase);
419 if (!DEBUG_CurrThread) {
420 ERR("Couldn't create thread\n");
424 DEBUG_InitCurrProcess();
425 DEBUG_InitCurrThread();
426 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
427 /* so far, process name is not set */
428 DEBUG_RegisterDebugInfo((DWORD)de->u.CreateProcessInfo.lpBaseOfImage,
433 case EXIT_THREAD_DEBUG_EVENT:
434 TRACE("%08lx:%08lx: exit thread (%ld)\n",
435 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
437 if (DEBUG_CurrThread == NULL) {
438 ERR("Unknown thread\n");
441 /* FIXME: remove break point set on thread startup */
442 DEBUG_DelThread(DEBUG_CurrThread);
445 case EXIT_PROCESS_DEBUG_EVENT:
446 TRACE("%08lx:%08lx: exit process (%ld)\n",
447 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
449 if (DEBUG_CurrProcess == NULL) {
450 ERR("Unknown process\n");
453 /* kill last thread */
454 DEBUG_DelThread(DEBUG_CurrProcess->threads);
455 /* FIXME: remove break point set on thread startup */
456 DEBUG_DelProcess(DEBUG_CurrProcess);
459 case LOAD_DLL_DEBUG_EVENT:
460 if (DEBUG_CurrThread == NULL) {
461 ERR("Unknown thread\n");
464 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
465 DEBUG_CurrThread->process->handle,
466 de->u.LoadDll.lpImageName);
468 /* FIXME unicode: de->u.LoadDll.fUnicode */
469 TRACE("%08lx:%08lx: loads DLL %s @%p\n", de->dwProcessId, de->dwThreadId,
470 buffer, de->u.LoadDll.lpBaseOfDll);
472 DEBUG_LoadModule32( buffer, (DWORD)de->u.LoadDll.lpBaseOfDll);
475 case UNLOAD_DLL_DEBUG_EVENT:
476 TRACE("%08lx:%08lx: unload DLL @%p\n", de->dwProcessId, de->dwThreadId,
477 de->u.UnloadDll.lpBaseOfDll);
480 case OUTPUT_DEBUG_STRING_EVENT:
481 if (DEBUG_CurrThread == NULL) {
482 ERR("Unknown thread\n");
486 DEBUG_ProcessGetString(buffer, sizeof(buffer),
487 DEBUG_CurrThread->process->handle,
488 de->u.DebugString.lpDebugStringData);
490 /* fixme unicode de->u.DebugString.fUnicode ? */
491 TRACE("%08lx:%08lx: output debug string (%s)\n",
492 de->dwProcessId, de->dwThreadId,
497 TRACE("%08lx:%08lx: rip error=%ld type=%ld\n",
498 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
499 de->u.RipInfo.dwType);
503 TRACE("%08lx:%08lx: unknown event (%ld)\n",
504 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
507 } __EXCEPT(wine_dbg) {
515 static DWORD CALLBACK DEBUG_MainLoop(DWORD pid)
522 while (WaitForDebugEvent(&de, INFINITE)) {
523 cont = DEBUG_HandleDebugEvent(&de);
524 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
527 TRACE("WineDbg terminated on pid %ld\n", pid);
532 static DWORD CALLBACK DEBUG_StarterFromPID(LPVOID pid)
534 TRACE("WineDbg started on pid %ld\n", (DWORD)pid);
536 if (!DebugActiveProcess((DWORD)pid)) {
537 TRACE("Can't debug process %ld: %ld\n", (DWORD)pid, GetLastError());
540 return DEBUG_MainLoop((DWORD)pid);
543 void DEBUG_Attach(DWORD pid)
545 CreateThread(NULL, 0, DEBUG_StarterFromPID, (LPVOID)pid, 0, NULL);
555 static DWORD CALLBACK DEBUG_StarterFromCmdLine(LPVOID p)
557 PROCESS_INFORMATION info;
558 STARTUPINFOA startup;
561 memset(&startup, 0, sizeof(startup));
562 startup.cb = sizeof(startup);
563 startup.dwFlags = STARTF_USESHOWWINDOW;
564 startup.wShowWindow = ((struct dsfcl*)p)->showWindow;
566 /* any value >= 32 will do, simulate a correct handle value */
567 ((struct dsfcl*)p)->error = 0xFFFFFFFF;
568 if (!CreateProcessA(NULL, ((struct dsfcl*)p)->lpCmdLine, NULL, NULL,
569 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
570 ((struct dsfcl*)p)->error = GetLastError();
573 SetEvent(((struct dsfcl*)p)->hEvent);
574 if (ok) DEBUG_MainLoop(info.dwProcessId);
579 DWORD DEBUG_WinExec(LPSTR lpCmdLine, int sw)
584 if ((s.hEvent = CreateEventA(NULL, FALSE, FALSE, NULL))) {
585 s.lpCmdLine = lpCmdLine;
587 if (CreateThread(NULL, 0, DEBUG_StarterFromCmdLine, (LPVOID)&s, 0, NULL)) {
588 WaitForSingleObject(s.hEvent, INFINITE);
591 ret = 3; /* (dummy) error value for non created thread */
593 CloseHandle(s.hEvent);
595 ret = 1; /* (dummy) error value for non created event */