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
;
29 CONTEXT DEBUG_context
;
31 static DBG_PROCESS
* proc
= NULL
;
32 DBG_INTVAR DEBUG_IntVars
[DBG_IV_LAST
];
34 void DEBUG_Output(int chn
, const char* buffer
, int len
)
36 if (DBG_IVAR(ConChannelMask
) & chn
)
37 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
38 if (DBG_IVAR(StdChannelMask
) & chn
)
39 fwrite(buffer
, len
, 1, stderr
);
42 int DEBUG_Printf(int chn
, const char* format
, ...)
48 va_start(valist
, format
);
49 len
= wvsnprintf(buf
, sizeof(buf
), format
, valist
);
53 len
= sizeof(buf
) - 1;
55 buf
[len
- 1] = buf
[len
- 2] = buf
[len
- 3] = '.';
57 DEBUG_Output(chn
, buf
, len
);
61 static BOOL
DEBUG_IntVarsRW(int read
)
64 DWORD type
= REG_DWORD
;
66 DWORD count
= sizeof(val
);
68 DBG_INTVAR
* div
= DEBUG_IntVars
;
71 /* initializes internal vars table */
72 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
73 div->val = _val; div->name = #_var; div->pval = _ref; \
74 div->type = _typ; div++;
79 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
80 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
82 fprintf(stderr
, "Cannot create WineDbg key in registry\n");
86 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
88 if (!DEBUG_IntVars
[i
].pval
) {
89 if (!RegQueryValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
90 &type
, (LPSTR
)&val
, &count
))
91 DEBUG_IntVars
[i
].val
= val
;
92 DEBUG_IntVars
[i
].pval
= &DEBUG_IntVars
[i
].val
;
94 *DEBUG_IntVars
[i
].pval
= 0;
97 /* FIXME: type should be infered from basic type -if any- of intvar */
98 if (DEBUG_IntVars
[i
].pval
== &DEBUG_IntVars
[i
].val
)
99 RegSetValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
100 type
, (LPCVOID
)DEBUG_IntVars
[i
].pval
, count
);
107 DBG_INTVAR
* DEBUG_GetIntVar(const char* name
)
111 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
112 if (!strcmp(DEBUG_IntVars
[i
].name
, name
))
113 return &DEBUG_IntVars
[i
];
118 static WINE_EXCEPTION_FILTER(wine_dbg
)
120 DEBUG_Printf(DBG_CHN_MESG
, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
121 DEBUG_ExternalDebugger();
122 return EXCEPTION_EXECUTE_HANDLER
;
125 static DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
129 for (p
= proc
; p
; p
= p
->next
)
130 if (p
->pid
== pid
) break;
134 static DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
)
136 DBG_PROCESS
* p
= DBG_alloc(sizeof(DBG_PROCESS
));
143 p
->continue_on_first_exception
= FALSE
;
150 if (proc
) proc
->prev
= p
;
155 static void DEBUG_DelThread(DBG_THREAD
* p
);
157 static void DEBUG_DelProcess(DBG_PROCESS
* p
)
159 if (p
->threads
!= NULL
) {
160 DEBUG_Printf(DBG_CHN_ERR
, "Shouldn't happen\n");
161 while (p
->threads
) DEBUG_DelThread(p
->threads
);
163 if (p
->prev
) p
->prev
->next
= p
->next
;
164 if (p
->next
) p
->next
->prev
= p
->prev
;
165 if (p
== proc
) proc
= p
->next
;
169 static void DEBUG_InitCurrProcess(void)
173 static BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
177 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
180 static BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
186 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
189 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
195 static DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
199 for (t
= p
->threads
; t
; t
= t
->next
)
200 if (t
->tid
== tid
) break;
204 static DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
205 HANDLE h
, LPVOID start
, LPVOID teb
)
207 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
216 t
->wait_for_first_exception
= 0;
217 t
->dbg_exec_mode
= EXEC_CONT
;
218 t
->dbg_exec_count
= 0;
221 t
->next
= p
->threads
;
223 if (p
->threads
) p
->threads
->prev
= t
;
229 static void DEBUG_InitCurrThread(void)
231 if (DEBUG_CurrThread
->start
) {
232 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
233 DBG_IVAR(BreakAllThreadsStartup
)) {
236 DEBUG_SetBreakpoints(FALSE
);
238 value
.cookie
= DV_TARGET
;
240 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
241 DEBUG_AddBreakpoint(&value
, NULL
);
242 DEBUG_SetBreakpoints(TRUE
);
245 DEBUG_CurrThread
->wait_for_first_exception
= 1;
249 static void DEBUG_DelThread(DBG_THREAD
* t
)
251 if (t
->prev
) t
->prev
->next
= t
->next
;
252 if (t
->next
) t
->next
->prev
= t
->prev
;
253 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
254 t
->process
->num_threads
--;
258 static BOOL
DEBUG_HandleException( EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
260 BOOL is_debug
= FALSE
;
263 /* FIXME: need for a configuration var ? */
264 /* pass to app first ??? */
265 /* if (first_chance && !force) return 0; */
267 switch (rec
->ExceptionCode
)
269 case EXCEPTION_BREAKPOINT
:
270 case EXCEPTION_SINGLE_STEP
:
277 /* print some infos */
278 DEBUG_Printf( DBG_CHN_MESG
, "%s: ",
279 first_chance
? "First chance exception" : "Unhandled exception" );
280 switch (rec
->ExceptionCode
)
282 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
283 DEBUG_Printf( DBG_CHN_MESG
, "divide by zero" );
285 case EXCEPTION_INT_OVERFLOW
:
286 DEBUG_Printf( DBG_CHN_MESG
, "overflow" );
288 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
289 DEBUG_Printf( DBG_CHN_MESG
, "array bounds " );
291 case EXCEPTION_ILLEGAL_INSTRUCTION
:
292 DEBUG_Printf( DBG_CHN_MESG
, "illegal instruction" );
294 case EXCEPTION_STACK_OVERFLOW
:
295 DEBUG_Printf( DBG_CHN_MESG
, "stack overflow" );
297 case EXCEPTION_PRIV_INSTRUCTION
:
298 DEBUG_Printf( DBG_CHN_MESG
, "priviledged instruction" );
300 case EXCEPTION_ACCESS_VIOLATION
:
301 if (rec
->NumberParameters
== 2)
302 DEBUG_Printf( DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
303 rec
->ExceptionInformation
[0] ? "write" : "read",
304 rec
->ExceptionInformation
[1] );
306 DEBUG_Printf( DBG_CHN_MESG
, "page fault" );
308 case EXCEPTION_DATATYPE_MISALIGNMENT
:
309 DEBUG_Printf( DBG_CHN_MESG
, "Alignment" );
312 DEBUG_Printf( DBG_CHN_MESG
, "^C" );
314 case EXCEPTION_CRITICAL_SECTION_WAIT
:
315 DEBUG_Printf( DBG_CHN_MESG
, "critical section %08lx wait failed",
316 rec
->ExceptionInformation
[0] );
317 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
321 DEBUG_Printf( DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
324 DEBUG_Printf(DBG_CHN_MESG
, "\n");
327 DEBUG_Printf(DBG_CHN_TRACE
,
328 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
329 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
330 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
332 ret
= DEBUG_Main( is_debug
, force
, rec
->ExceptionCode
);
334 DEBUG_Printf(DBG_CHN_TRACE
,
335 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
336 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
337 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
342 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
, LPDWORD cont
)
347 DEBUG_CurrPid
= de
->dwProcessId
;
348 DEBUG_CurrTid
= de
->dwThreadId
;
354 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
355 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
357 DEBUG_CurrThread
= NULL
;
359 switch (de
->dwDebugEventCode
) {
360 case EXCEPTION_DEBUG_EVENT
:
361 if (!DEBUG_CurrThread
) {
362 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
363 de
->dwProcessId
, de
->dwThreadId
);
367 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
368 de
->dwProcessId
, de
->dwThreadId
,
369 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
371 if (DEBUG_CurrProcess
->continue_on_first_exception
) {
372 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
373 if (!DBG_IVAR(BreakOnAttach
)) {
374 *cont
= DBG_CONTINUE
;
379 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
|CONTEXT_INTEGER
|CONTEXT_SEGMENTS
|CONTEXT_DEBUG_REGISTERS
;
380 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
)) {
381 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
385 *cont
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
386 de
->u
.Exception
.dwFirstChance
,
387 DEBUG_CurrThread
->wait_for_first_exception
);
388 if (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_KILL
) {
391 DEBUG_CurrThread
->wait_for_first_exception
= 0;
392 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
396 case CREATE_THREAD_DEBUG_EVENT
:
397 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
398 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
400 if (DEBUG_CurrProcess
== NULL
) {
401 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
404 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
) {
405 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
409 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
411 de
->u
.CreateThread
.hThread
,
412 de
->u
.CreateThread
.lpStartAddress
,
413 de
->u
.CreateThread
.lpThreadLocalBase
);
414 if (!DEBUG_CurrThread
) {
415 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
418 DEBUG_InitCurrThread();
421 case CREATE_PROCESS_DEBUG_EVENT
:
422 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
423 de
->u
.CreateProcessInfo
.hProcess
,
424 de
->u
.CreateProcessInfo
.lpImageName
);
426 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
427 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
428 de
->dwProcessId
, de
->dwThreadId
,
430 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
431 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
432 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
434 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
) {
435 if (DEBUG_CurrProcess
->handle
) {
436 DEBUG_Printf(DBG_CHN_ERR
, "Skipping already defined process\n");
439 DEBUG_CurrProcess
->handle
= de
->u
.CreateProcessInfo
.hProcess
;
441 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
442 de
->u
.CreateProcessInfo
.hProcess
);
443 if (DEBUG_CurrProcess
== NULL
) {
444 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
449 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
450 de
->dwProcessId
, de
->dwThreadId
,
451 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
453 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
455 de
->u
.CreateProcessInfo
.hThread
,
456 de
->u
.CreateProcessInfo
.lpStartAddress
,
457 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
458 if (!DEBUG_CurrThread
) {
459 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
463 DEBUG_InitCurrProcess();
464 DEBUG_InitCurrThread();
465 /* so far, process name is not set */
466 DEBUG_LoadModule32("<Debugged process>", de
->u
.CreateProcessInfo
.hFile
,
467 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
470 case EXIT_THREAD_DEBUG_EVENT
:
471 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
472 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
474 if (DEBUG_CurrThread
== NULL
) {
475 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
478 /* FIXME: remove break point set on thread startup */
479 DEBUG_DelThread(DEBUG_CurrThread
);
482 case EXIT_PROCESS_DEBUG_EVENT
:
483 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
484 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
486 if (DEBUG_CurrProcess
== NULL
) {
487 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
491 DEBUG_SetBreakpoints(FALSE
);
492 /* kill last thread */
493 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
494 DEBUG_DelProcess(DEBUG_CurrProcess
);
498 case LOAD_DLL_DEBUG_EVENT
:
499 if (DEBUG_CurrThread
== NULL
) {
500 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
503 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
504 DEBUG_CurrThread
->process
->handle
,
505 de
->u
.LoadDll
.lpImageName
);
507 /* FIXME unicode: de->u.LoadDll.fUnicode */
508 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
509 de
->dwProcessId
, de
->dwThreadId
,
510 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
511 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
512 de
->u
.LoadDll
.nDebugInfoSize
);
514 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
517 case UNLOAD_DLL_DEBUG_EVENT
:
518 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
519 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
522 case OUTPUT_DEBUG_STRING_EVENT
:
523 if (DEBUG_CurrThread
== NULL
) {
524 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
528 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
529 DEBUG_CurrThread
->process
->handle
,
530 de
->u
.DebugString
.lpDebugStringData
);
532 /* fixme unicode de->u.DebugString.fUnicode ? */
533 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
534 de
->dwProcessId
, de
->dwThreadId
, buffer
);
538 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
539 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
540 de
->u
.RipInfo
.dwType
);
544 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
545 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
548 } __EXCEPT(wine_dbg
) {
557 static DWORD
DEBUG_MainLoop(DWORD pid
)
563 DEBUG_Printf(DBG_CHN_MESG
, " on pid %ld\n", pid
);
565 while (ret
&& WaitForDebugEvent(&de
, INFINITE
)) {
566 ret
= DEBUG_HandleDebugEvent(&de
, &cont
);
567 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, cont
);
570 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %ld\n", pid
);
575 int DEBUG_main(int argc
, char** argv
)
577 DWORD pid
= 0, retv
= 0;
580 /* Initialize the debugger heap. */
581 dbg_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0x1000, 0x8000000); /* 128MB */
584 /* Initialize the type handling stuff. */
586 DEBUG_InitCVDataTypes();
588 /* Initialize internal vars (types must be initialized before) */
589 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
591 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
592 * stdout and stderr streams
594 if (DBG_IVAR(UseXTerm
)) {
597 /* This is a hack: it forces creation of an xterm, not done by default */
598 pos
.X
= 0; pos
.Y
= 1;
599 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
), pos
);
602 DEBUG_Printf(DBG_CHN_MESG
, "Starting WineDbg... ");
606 if ((pid
= atoi(argv
[1])) != 0 && (hEvent
= atoi(argv
[2])) != 0) {
607 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0))) goto leave
;
608 DEBUG_CurrProcess
->continue_on_first_exception
= TRUE
;
610 if (!DebugActiveProcess(pid
)) {
611 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
612 pid
, GetLastError());
624 DEBUG_Printf(DBG_CHN_MESG
, "\n");
626 pid
= strtol(org
= readline("Enter pid to debug: "), &ptr
, 0);
627 if (pid
&& ptr
&& ptr
!= org
&& *ptr
== '\0') {
628 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0))) goto leave
;
630 if (!DebugActiveProcess(pid
)) {
631 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
632 pid
, GetLastError());
641 PROCESS_INFORMATION info
;
642 STARTUPINFOA startup
;
644 memset(&startup
, 0, sizeof(startup
));
645 startup
.cb
= sizeof(startup
);
646 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
647 startup
.wShowWindow
= SW_SHOWNORMAL
;
649 if (!CreateProcess(NULL
, argv
[1], NULL
, NULL
,
650 FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &startup
, &info
)) {
651 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", argv
[1]);
654 pid
= info
.dwProcessId
;
657 if (pid
) retv
= DEBUG_MainLoop(pid
);
659 /* saves modified variables */
660 DEBUG_IntVarsRW(FALSE
);