1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
25 DBG_PROCESS
* DEBUG_CurrProcess
= NULL
;
26 DBG_THREAD
* DEBUG_CurrThread
= NULL
;
27 CONTEXT DEBUG_context
;
29 static DBG_PROCESS
* proc
= NULL
;
30 DBG_INTVAR DEBUG_IntVars
[DBG_IV_LAST
];
32 void DEBUG_Output(int chn
, const char* buffer
, int len
)
34 if (DBG_IVAR(ConChannelMask
) & chn
)
35 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
36 if (DBG_IVAR(StdChannelMask
) & chn
)
37 fwrite(buffer
, len
, 1, stderr
);
40 int DEBUG_Printf(int chn
, const char* format
, ...)
46 va_start(valist
, format
);
47 len
= vsprintf(buf
, format
, valist
);
49 DEBUG_Output(chn
, buf
, len
);
53 static BOOL
DEBUG_IntVarsRW(int read
)
56 DWORD type
= REG_DWORD
;
58 DWORD count
= sizeof(val
);
60 DBG_INTVAR
* div
= DEBUG_IntVars
;
63 /* initializes internal vars table */
64 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
65 div->val = _val; div->name = #_var; div->pval = _ref; \
66 div->type = _typ; div++;
71 if (RegOpenKey(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
) &&
72 RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
73 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
75 fprintf(stderr
, "Cannot create WineDbg key in registry\n");
79 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
81 if (!DEBUG_IntVars
[i
].pval
) {
82 if (!RegQueryValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
83 &type
, (LPSTR
)&val
, &count
))
84 DEBUG_IntVars
[i
].val
= val
;
85 DEBUG_IntVars
[i
].pval
= &DEBUG_IntVars
[i
].val
;
87 *DEBUG_IntVars
[i
].pval
= 0;
90 /* FIXME: type should be infered from basic type -if any- of intvar */
91 if (DEBUG_IntVars
[i
].pval
== &DEBUG_IntVars
[i
].val
)
92 RegSetValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
93 type
, (LPCVOID
)DEBUG_IntVars
[i
].pval
, count
);
100 DBG_INTVAR
* DEBUG_GetIntVar(const char* name
)
104 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
105 if (!strcmp(DEBUG_IntVars
[i
].name
, name
))
106 return &DEBUG_IntVars
[i
];
111 static WINE_EXCEPTION_FILTER(wine_dbg
)
113 DEBUG_ExternalDebugger();
114 DEBUG_Printf(DBG_CHN_MESG
, "\nwine_dbg: Exception %lx\n", GetExceptionCode());
115 return EXCEPTION_EXECUTE_HANDLER
;
118 static DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
122 for (p
= proc
; p
; p
= p
->next
)
123 if (p
->pid
== pid
) break;
127 static DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
)
129 DBG_PROCESS
* p
= DBG_alloc(sizeof(DBG_PROCESS
));
141 if (proc
) proc
->prev
= p
;
146 static void DEBUG_DelThread(DBG_THREAD
* p
);
148 static void DEBUG_DelProcess(DBG_PROCESS
* p
)
150 if (p
->threads
!= NULL
) {
151 DEBUG_Printf(DBG_CHN_ERR
, "Shouldn't happen\n");
152 while (p
->threads
) DEBUG_DelThread(p
->threads
);
154 if (p
->prev
) p
->prev
->next
= p
->next
;
155 if (p
->next
) p
->next
->prev
= p
->prev
;
156 if (p
== proc
) proc
= p
->next
;
160 static void DEBUG_InitCurrProcess(void)
164 static BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
168 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
171 static BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
177 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
180 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
186 static DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
190 for (t
= p
->threads
; t
; t
= t
->next
)
191 if (t
->tid
== tid
) break;
195 static DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
196 HANDLE h
, LPVOID start
, LPVOID teb
)
198 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
207 t
->wait_for_first_exception
= 0;
208 t
->dbg_exec_mode
= EXEC_CONT
;
209 t
->dbg_exec_count
= 0;
212 t
->next
= p
->threads
;
214 if (p
->threads
) p
->threads
->prev
= t
;
220 static void DEBUG_InitCurrThread(void)
222 if (DEBUG_CurrThread
->start
) {
223 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
224 DBG_IVAR(BreakAllThreadsStartup
)) {
227 DEBUG_SetBreakpoints(FALSE
);
229 value
.cookie
= DV_TARGET
;
231 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
232 DEBUG_AddBreakpoint(&value
);
233 DEBUG_SetBreakpoints(TRUE
);
236 DEBUG_CurrThread
->wait_for_first_exception
= 1;
240 static void DEBUG_DelThread(DBG_THREAD
* t
)
242 if (t
->prev
) t
->prev
->next
= t
->next
;
243 if (t
->next
) t
->next
->prev
= t
->prev
;
244 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
245 t
->process
->num_threads
--;
249 static BOOL
DEBUG_HandleException( EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
251 BOOL is_debug
= FALSE
;
254 /* FIXME: need for a configuration var ? */
255 /* pass to app first ??? */
256 /* if (first_chance && !force) return 0; */
258 switch (rec
->ExceptionCode
)
260 case EXCEPTION_BREAKPOINT
:
261 case EXCEPTION_SINGLE_STEP
:
268 /* print some infos */
269 DEBUG_Printf( DBG_CHN_MESG
, "%s: ",
270 first_chance
? "First chance exception" : "Unhandled exception" );
271 switch(rec
->ExceptionCode
)
273 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
274 DEBUG_Printf( DBG_CHN_MESG
, "divide by zero" );
276 case EXCEPTION_INT_OVERFLOW
:
277 DEBUG_Printf( DBG_CHN_MESG
, "overflow" );
279 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
280 DEBUG_Printf( DBG_CHN_MESG
, "array bounds " );
282 case EXCEPTION_ILLEGAL_INSTRUCTION
:
283 DEBUG_Printf( DBG_CHN_MESG
, "illegal instruction" );
285 case EXCEPTION_STACK_OVERFLOW
:
286 DEBUG_Printf( DBG_CHN_MESG
, "stack overflow" );
288 case EXCEPTION_PRIV_INSTRUCTION
:
289 DEBUG_Printf( DBG_CHN_MESG
, "priviledged instruction" );
291 case EXCEPTION_ACCESS_VIOLATION
:
292 if (rec
->NumberParameters
== 2)
293 DEBUG_Printf( DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
294 rec
->ExceptionInformation
[0] ? "write" : "read",
295 rec
->ExceptionInformation
[1] );
297 DEBUG_Printf( DBG_CHN_MESG
, "page fault" );
299 case EXCEPTION_DATATYPE_MISALIGNMENT
:
300 DEBUG_Printf( DBG_CHN_MESG
, "Alignment" );
303 DEBUG_Printf( DBG_CHN_MESG
, "^C" );
305 case EXCEPTION_CRITICAL_SECTION_WAIT
:
306 DEBUG_Printf( DBG_CHN_MESG
, "critical section %08lx wait failed",
307 rec
->ExceptionInformation
[0] );
308 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
312 DEBUG_Printf( DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
317 DEBUG_Printf(DBG_CHN_MESG
, "\n");
319 DEBUG_Printf(DBG_CHN_TRACE
,
320 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
321 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
322 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
324 ret
= DEBUG_Main( is_debug
, force
, rec
->ExceptionCode
);
326 DEBUG_Printf(DBG_CHN_TRACE
,
327 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
328 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
329 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
334 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
, LPDWORD cont
)
343 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
344 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
346 DEBUG_CurrThread
= NULL
;
348 switch (de
->dwDebugEventCode
) {
349 case EXCEPTION_DEBUG_EVENT
:
350 if (!DEBUG_CurrThread
) {
351 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
352 de
->dwProcessId
, de
->dwThreadId
);
356 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx %d\n",
357 de
->dwProcessId
, de
->dwThreadId
,
358 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
,
359 DEBUG_CurrThread
->wait_for_first_exception
);
361 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
|CONTEXT_INTEGER
|CONTEXT_SEGMENTS
|CONTEXT_DEBUG_REGISTERS
;
362 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
)) {
363 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
367 DEBUG_Printf(DBG_CHN_TRACE
, "%p:%p\n", de
->u
.Exception
.ExceptionRecord
.ExceptionAddress
,
368 (void*)DEBUG_context
.Eip
);
370 *cont
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
371 de
->u
.Exception
.dwFirstChance
,
372 DEBUG_CurrThread
->wait_for_first_exception
);
373 if (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_KILL
) {
376 DEBUG_CurrThread
->wait_for_first_exception
= 0;
377 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
381 case CREATE_THREAD_DEBUG_EVENT
:
382 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%p\n", de
->dwProcessId
, de
->dwThreadId
,
383 de
->u
.CreateThread
.lpStartAddress
);
385 if (DEBUG_CurrProcess
== NULL
) {
386 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
389 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
) {
390 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
394 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
396 de
->u
.CreateThread
.hThread
,
397 de
->u
.CreateThread
.lpStartAddress
,
398 de
->u
.CreateThread
.lpThreadLocalBase
);
399 if (!DEBUG_CurrThread
) {
400 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
403 DEBUG_InitCurrThread();
406 case CREATE_PROCESS_DEBUG_EVENT
:
407 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
408 de
->u
.CreateProcessInfo
.hProcess
,
409 de
->u
.CreateProcessInfo
.lpImageName
);
411 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
412 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process %s @%p (%ld<%ld>)\n",
413 de
->dwProcessId
, de
->dwThreadId
,
415 de
->u
.CreateProcessInfo
.lpStartAddress
,
416 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
417 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
419 if (DEBUG_GetProcess(de
->dwProcessId
) != NULL
) {
420 DEBUG_Printf(DBG_CHN_TRACE
, "Skipping already defined process\n");
423 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
424 de
->u
.CreateProcessInfo
.hProcess
);
425 if (DEBUG_CurrProcess
== NULL
) {
426 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
430 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%p\n",
431 de
->dwProcessId
, de
->dwThreadId
,
432 de
->u
.CreateProcessInfo
.lpStartAddress
);
434 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
436 de
->u
.CreateProcessInfo
.hThread
,
437 de
->u
.CreateProcessInfo
.lpStartAddress
,
438 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
439 if (!DEBUG_CurrThread
) {
440 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
444 DEBUG_InitCurrProcess();
445 DEBUG_InitCurrThread();
446 /* so far, process name is not set */
447 DEBUG_LoadModule32("<Debugged process>", de
->u
.CreateProcessInfo
.hFile
,
448 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
451 case EXIT_THREAD_DEBUG_EVENT
:
452 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
453 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
455 if (DEBUG_CurrThread
== NULL
) {
456 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
459 /* FIXME: remove break point set on thread startup */
460 DEBUG_DelThread(DEBUG_CurrThread
);
463 case EXIT_PROCESS_DEBUG_EVENT
:
464 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
465 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
467 if (DEBUG_CurrProcess
== NULL
) {
468 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
472 DEBUG_SetBreakpoints(FALSE
);
473 /* kill last thread */
474 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
475 DEBUG_DelProcess(DEBUG_CurrProcess
);
479 case LOAD_DLL_DEBUG_EVENT
:
480 if (DEBUG_CurrThread
== NULL
) {
481 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
484 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
485 DEBUG_CurrThread
->process
->handle
,
486 de
->u
.LoadDll
.lpImageName
);
488 /* FIXME unicode: de->u.LoadDll.fUnicode */
489 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%p (%ld<%ld>)\n",
490 de
->dwProcessId
, de
->dwThreadId
,
491 buffer
, de
->u
.LoadDll
.lpBaseOfDll
,
492 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
493 de
->u
.LoadDll
.nDebugInfoSize
);
495 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
498 case UNLOAD_DLL_DEBUG_EVENT
:
499 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%p\n", de
->dwProcessId
, de
->dwThreadId
,
500 de
->u
.UnloadDll
.lpBaseOfDll
);
503 case OUTPUT_DEBUG_STRING_EVENT
:
504 if (DEBUG_CurrThread
== NULL
) {
505 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
509 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
510 DEBUG_CurrThread
->process
->handle
,
511 de
->u
.DebugString
.lpDebugStringData
);
513 /* fixme unicode de->u.DebugString.fUnicode ? */
514 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
515 de
->dwProcessId
, de
->dwThreadId
, buffer
);
519 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
520 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
521 de
->u
.RipInfo
.dwType
);
525 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
526 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
529 } __EXCEPT(wine_dbg
) {
538 static DWORD
DEBUG_MainLoop(DWORD pid
)
544 DEBUG_Printf(DBG_CHN_MESG
, " on pid %ld\n", pid
);
546 while (ret
&& WaitForDebugEvent(&de
, INFINITE
)) {
547 ret
= DEBUG_HandleDebugEvent(&de
, &cont
);
548 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, cont
);
551 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %ld\n", pid
);
556 int DEBUG_main(int argc
, char** argv
)
558 DWORD pid
= 0, retv
= 0;
561 /* Initialize the debugger heap. */
562 dbg_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0x1000, 0x8000000); /* 128MB */
565 /* Initialize the type handling stuff. */
567 DEBUG_InitCVDataTypes();
569 /* Initialize internal vars */
570 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
572 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
573 * stdout and stderr streams
575 if (DBG_IVAR(UseXTerm
)) {
578 /* This is a hack: it forces creation of an xterm, not done by default */
579 pos
.X
= 0; pos
.Y
= 1;
580 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
), pos
);
583 DEBUG_Printf(DBG_CHN_MESG
, "Starting WineDbg... ");
587 if ((pid
= atoi(argv
[1])) != 0 && (hEvent
= atoi(argv
[2])) != 0) {
588 if (!DebugActiveProcess(pid
)) {
589 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
590 pid
, GetLastError());
601 PROCESS_INFORMATION info
;
602 STARTUPINFOA startup
;
604 memset(&startup
, 0, sizeof(startup
));
605 startup
.cb
= sizeof(startup
);
606 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
607 startup
.wShowWindow
= SW_SHOWNORMAL
;
609 if (!CreateProcess(NULL
, argv
[1], NULL
, NULL
,
610 FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &startup
, &info
)) {
611 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", argv
[1]);
614 pid
= info
.dwProcessId
;
617 if (pid
) retv
= DEBUG_MainLoop(pid
);
619 /* saves modified variables */
620 DEBUG_IntVarsRW(FALSE
);