1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
24 DBG_PROCESS
* DEBUG_CurrProcess
= NULL
;
25 DBG_THREAD
* DEBUG_CurrThread
= NULL
;
26 CONTEXT DEBUG_context
;
28 static DBG_PROCESS
* proc
= NULL
;
30 /* build internal vars table */
31 #define INTERNAL_VAR(_var,_val) int DBG_IVAR(_var) = _val;
35 int DEBUG_Printf(int chn
, const char* format
, ...)
40 va_start(valist
, format
);
41 vsprintf(buf
, format
, valist
);
44 if (DBG_IVAR(ChannelMask
) & chn
)
45 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, strlen(buf
), NULL
, NULL
);
46 if (chn
== DBG_CHN_MESG
) fwrite(buf
, strlen(buf
), 1, stderr
);
48 if (DBG_IVAR(ChannelMask
) & chn
) fwrite(buf
, strlen(buf
), 1, stderr
);
53 static BOOL
DEBUG_Init(void)
58 DWORD count
= sizeof(val
);
60 if (!RegOpenKey(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
61 #define INTERNAL_VAR(_var,_val) \
62 if (!RegQueryValueEx(hkey, #_var, 0, &type, (LPSTR)&val, &count)) \
71 static WINE_EXCEPTION_FILTER(wine_dbg
)
73 DEBUG_ExternalDebugger();
74 DEBUG_Printf(DBG_CHN_MESG
, "\nwine_dbg: Exception %lx\n", GetExceptionCode());
75 return EXCEPTION_EXECUTE_HANDLER
;
78 static DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
82 for (p
= proc
; p
; p
= p
->next
)
83 if (p
->pid
== pid
) break;
87 static DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
)
89 DBG_PROCESS
* p
= DBG_alloc(sizeof(DBG_PROCESS
));
101 if (proc
) proc
->prev
= p
;
106 static void DEBUG_DelThread(DBG_THREAD
* p
);
108 static void DEBUG_DelProcess(DBG_PROCESS
* p
)
110 if (p
->threads
!= NULL
) {
111 DEBUG_Printf(DBG_CHN_ERR
, "Shouldn't happen\n");
112 while (p
->threads
) DEBUG_DelThread(p
->threads
);
114 if (p
->prev
) p
->prev
->next
= p
->next
;
115 if (p
->next
) p
->next
->prev
= p
->prev
;
116 if (p
== proc
) proc
= p
->next
;
120 static void DEBUG_InitCurrProcess(void)
124 * Initialize the debugger heap.
126 dbg_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0x1000, 0x8000000); /* 128MB */
130 * Initialize the type handling stuff.
133 DEBUG_InitCVDataTypes();
136 static BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
140 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
143 static BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
149 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
152 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
158 static DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
162 for (t
= p
->threads
; t
; t
= t
->next
)
163 if (t
->tid
== tid
) break;
167 static DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
168 HANDLE h
, LPVOID start
, LPVOID teb
)
170 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
179 t
->wait_for_first_exception
= 0;
180 t
->dbg_exec_mode
= EXEC_CONT
;
181 t
->dbg_exec_count
= 0;
184 t
->next
= p
->threads
;
186 if (p
->threads
) p
->threads
->prev
= t
;
192 static void DEBUG_InitCurrThread(void)
194 if (DEBUG_CurrThread
->start
) {
195 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
196 DBG_IVAR(BreakAllThreadsStartup
)) {
199 DEBUG_SetBreakpoints(FALSE
);
201 value
.cookie
= DV_TARGET
;
203 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
204 DEBUG_AddBreakpoint(&value
);
205 DEBUG_SetBreakpoints(TRUE
);
208 DEBUG_CurrThread
->wait_for_first_exception
= 1;
212 static void DEBUG_DelThread(DBG_THREAD
* t
)
214 if (t
->prev
) t
->prev
->next
= t
->next
;
215 if (t
->next
) t
->next
->prev
= t
->prev
;
216 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
217 t
->process
->num_threads
--;
221 static BOOL
DEBUG_HandleException( EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
223 BOOL is_debug
= FALSE
;
226 /* FIXME: need for a configuration var ? */
227 /* pass to app first ??? */
228 /* if (first_chance && !force) return 0; */
230 switch (rec
->ExceptionCode
)
232 case EXCEPTION_BREAKPOINT
:
233 case EXCEPTION_SINGLE_STEP
:
240 /* print some infos */
241 DEBUG_Printf( DBG_CHN_MESG
, "%s: ",
242 first_chance
? "First chance exception" : "Unhandled exception" );
243 switch(rec
->ExceptionCode
)
245 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
246 DEBUG_Printf( DBG_CHN_MESG
, "divide by zero" );
248 case EXCEPTION_INT_OVERFLOW
:
249 DEBUG_Printf( DBG_CHN_MESG
, "overflow" );
251 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
252 DEBUG_Printf( DBG_CHN_MESG
, "array bounds " );
254 case EXCEPTION_ILLEGAL_INSTRUCTION
:
255 DEBUG_Printf( DBG_CHN_MESG
, "illegal instruction" );
257 case EXCEPTION_STACK_OVERFLOW
:
258 DEBUG_Printf( DBG_CHN_MESG
, "stack overflow" );
260 case EXCEPTION_PRIV_INSTRUCTION
:
261 DEBUG_Printf( DBG_CHN_MESG
, "priviledged instruction" );
263 case EXCEPTION_ACCESS_VIOLATION
:
264 if (rec
->NumberParameters
== 2)
265 DEBUG_Printf( DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
266 rec
->ExceptionInformation
[0] ? "write" : "read",
267 rec
->ExceptionInformation
[1] );
269 DEBUG_Printf( DBG_CHN_MESG
, "page fault" );
271 case EXCEPTION_DATATYPE_MISALIGNMENT
:
272 DEBUG_Printf( DBG_CHN_MESG
, "Alignment" );
275 DEBUG_Printf( DBG_CHN_MESG
, "^C" );
277 case EXCEPTION_CRITICAL_SECTION_WAIT
:
278 DEBUG_Printf( DBG_CHN_MESG
, "critical section %08lx wait failed",
279 rec
->ExceptionInformation
[0] );
282 DEBUG_Printf( DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
287 DEBUG_Printf(DBG_CHN_TRACE
,
288 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
289 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
290 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
292 ret
= DEBUG_Main( is_debug
, force
, rec
->ExceptionCode
);
294 DEBUG_Printf(DBG_CHN_TRACE
,
295 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
296 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
297 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
302 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
, LPDWORD cont
)
311 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
312 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
314 DEBUG_CurrThread
= NULL
;
316 switch (de
->dwDebugEventCode
) {
317 case EXCEPTION_DEBUG_EVENT
:
318 if (!DEBUG_CurrThread
) {
319 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
320 de
->dwProcessId
, de
->dwThreadId
);
324 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx %d\n",
325 de
->dwProcessId
, de
->dwThreadId
,
326 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
,
327 DEBUG_CurrThread
->wait_for_first_exception
);
329 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
|CONTEXT_INTEGER
|CONTEXT_SEGMENTS
|CONTEXT_DEBUG_REGISTERS
;
330 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
)) {
331 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
335 DEBUG_Printf(DBG_CHN_TRACE
, "%p:%p\n", de
->u
.Exception
.ExceptionRecord
.ExceptionAddress
,
336 (void*)DEBUG_context
.Eip
);
338 *cont
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
339 de
->u
.Exception
.dwFirstChance
,
340 DEBUG_CurrThread
->wait_for_first_exception
);
341 if (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_KILL
) {
344 DEBUG_CurrThread
->wait_for_first_exception
= 0;
345 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
349 case CREATE_THREAD_DEBUG_EVENT
:
350 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%p\n", de
->dwProcessId
, de
->dwThreadId
,
351 de
->u
.CreateThread
.lpStartAddress
);
353 if (DEBUG_CurrProcess
== NULL
) {
354 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
357 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
) {
358 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
362 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
364 de
->u
.CreateThread
.hThread
,
365 de
->u
.CreateThread
.lpStartAddress
,
366 de
->u
.CreateThread
.lpThreadLocalBase
);
367 if (!DEBUG_CurrThread
) {
368 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
371 DEBUG_InitCurrThread();
374 case CREATE_PROCESS_DEBUG_EVENT
:
375 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
376 de
->u
.CreateProcessInfo
.hProcess
,
377 de
->u
.CreateProcessInfo
.lpImageName
);
379 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
380 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process %s @%p (%ld<%ld>)\n",
381 de
->dwProcessId
, de
->dwThreadId
,
383 de
->u
.CreateProcessInfo
.lpStartAddress
,
384 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
385 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
387 if (DEBUG_GetProcess(de
->dwProcessId
) != NULL
) {
388 DEBUG_Printf(DBG_CHN_TRACE
, "Skipping already defined process\n");
391 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
392 de
->u
.CreateProcessInfo
.hProcess
);
393 if (DEBUG_CurrProcess
== NULL
) {
394 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
398 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%p\n",
399 de
->dwProcessId
, de
->dwThreadId
,
400 de
->u
.CreateProcessInfo
.lpStartAddress
);
402 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
404 de
->u
.CreateProcessInfo
.hThread
,
405 de
->u
.CreateProcessInfo
.lpStartAddress
,
406 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
407 if (!DEBUG_CurrThread
) {
408 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
412 DEBUG_InitCurrProcess();
413 DEBUG_InitCurrThread();
414 /* so far, process name is not set */
415 DEBUG_LoadModule32("<Debugged process>", de
->u
.CreateProcessInfo
.hFile
,
416 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
419 case EXIT_THREAD_DEBUG_EVENT
:
420 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
421 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
423 if (DEBUG_CurrThread
== NULL
) {
424 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
427 /* FIXME: remove break point set on thread startup */
428 DEBUG_DelThread(DEBUG_CurrThread
);
431 case EXIT_PROCESS_DEBUG_EVENT
:
432 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
433 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
435 if (DEBUG_CurrProcess
== NULL
) {
436 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
440 DEBUG_SetBreakpoints(FALSE
);
441 /* kill last thread */
442 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
443 DEBUG_DelProcess(DEBUG_CurrProcess
);
447 case LOAD_DLL_DEBUG_EVENT
:
448 if (DEBUG_CurrThread
== NULL
) {
449 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
452 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
453 DEBUG_CurrThread
->process
->handle
,
454 de
->u
.LoadDll
.lpImageName
);
456 /* FIXME unicode: de->u.LoadDll.fUnicode */
457 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%p (%ld<%ld>)\n",
458 de
->dwProcessId
, de
->dwThreadId
,
459 buffer
, de
->u
.LoadDll
.lpBaseOfDll
,
460 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
461 de
->u
.LoadDll
.nDebugInfoSize
);
463 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
466 case UNLOAD_DLL_DEBUG_EVENT
:
467 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%p\n", de
->dwProcessId
, de
->dwThreadId
,
468 de
->u
.UnloadDll
.lpBaseOfDll
);
471 case OUTPUT_DEBUG_STRING_EVENT
:
472 if (DEBUG_CurrThread
== NULL
) {
473 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
477 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
478 DEBUG_CurrThread
->process
->handle
,
479 de
->u
.DebugString
.lpDebugStringData
);
481 /* fixme unicode de->u.DebugString.fUnicode ? */
482 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
483 de
->dwProcessId
, de
->dwThreadId
, buffer
);
487 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
488 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
489 de
->u
.RipInfo
.dwType
);
493 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
494 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
497 } __EXCEPT(wine_dbg
) {
506 static DWORD CALLBACK
DEBUG_MainLoop(DWORD pid
)
512 DEBUG_Printf(DBG_CHN_MESG
, " on pid %ld\n", pid
);
516 while (ret
&& WaitForDebugEvent(&de
, INFINITE
)) {
517 ret
= DEBUG_HandleDebugEvent(&de
, &cont
);
518 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, cont
);
521 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %ld\n", pid
);
526 int PASCAL
WinMain(HINSTANCE hInst
, HINSTANCE prev
, LPSTR _cmdline
, int show
)
529 char* cmdline
= strdup(_cmdline
);
534 while ((*ptr
== ' ' || *ptr
== '\t') && *ptr
!= 0) ptr
++;
536 for (; *ptr
; ptr
++) {
537 if ((*ptr
== ' ' || *ptr
== '\t') && !instr
) {
539 while (*ptr
== ' ' || *ptr
== '\t') ptr
++;
540 if (*ptr
) argv
[argc
++] = ptr
;
541 if (argc
>= sizeof(argv
) / sizeof(argv
[0])) return 0;
542 } else if (*ptr
== '"') {
548 /* would require to change .spec with a cuiexe type */
549 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
550 * stdout and stderr streams
552 if (1 /*DBG_IVAR(UseXterm)*/) {
555 /* This is a hack: it forces creation of an xterm, not done by default */
556 pos
.x
= 0; pos
.y
= 1;
557 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
), pos
);
561 DEBUG_Printf(DBG_CHN_MESG
, "Starting WineDbg... ");
563 DWORD pid
= atoi(argv
[0]);
564 HANDLE hEvent
= atoi(argv
[1]);
566 if (pid
!= 0 && hEvent
!= 0) {
569 if (!DebugActiveProcess(pid
)) {
570 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
571 pid
, GetLastError());
575 return DEBUG_MainLoop(pid
);
579 PROCESS_INFORMATION info
;
580 STARTUPINFOA startup
;
584 memset(&startup
, 0, sizeof(startup
));
585 startup
.cb
= sizeof(startup
);
586 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
587 startup
.wShowWindow
= SW_SHOWNORMAL
;
589 if (CreateProcess(NULL
, _cmdline
, NULL
, NULL
,
590 FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &startup
, &info
)) {
591 return DEBUG_MainLoop(info
.dwProcessId
);
593 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", _cmdline
);