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 char* DEBUG_LastCmdLine
= NULL
;
34 static DBG_PROCESS
* DEBUG_ProcessList
= NULL
;
35 DBG_INTVAR DEBUG_IntVars
[DBG_IV_LAST
];
37 void DEBUG_Output(int chn
, const char* buffer
, int len
)
39 if (DBG_IVAR(ConChannelMask
) & chn
)
40 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
41 if (DBG_IVAR(StdChannelMask
) & chn
)
42 fwrite(buffer
, len
, 1, stderr
);
45 int DEBUG_Printf(int chn
, const char* format
, ...)
51 va_start(valist
, format
);
52 len
= vsnprintf(buf
, sizeof(buf
), format
, valist
);
56 len
= sizeof(buf
) - 1;
58 buf
[len
- 1] = buf
[len
- 2] = buf
[len
- 3] = '.';
60 DEBUG_Output(chn
, buf
, len
);
64 static BOOL
DEBUG_IntVarsRW(int read
)
67 DWORD type
= REG_DWORD
;
69 DWORD count
= sizeof(val
);
71 DBG_INTVAR
* div
= DEBUG_IntVars
;
74 /* initializes internal vars table */
75 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
76 div->val = _val; div->name = #_var; div->pval = _ref; \
77 div->type = _typ; div++;
82 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
83 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
85 fprintf(stderr
, "Cannot create WineDbg key in registry\n");
89 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
91 if (!DEBUG_IntVars
[i
].pval
) {
92 if (!RegQueryValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
93 &type
, (LPSTR
)&val
, &count
))
94 DEBUG_IntVars
[i
].val
= val
;
95 DEBUG_IntVars
[i
].pval
= &DEBUG_IntVars
[i
].val
;
97 *DEBUG_IntVars
[i
].pval
= 0;
100 /* FIXME: type should be infered from basic type -if any- of intvar */
101 if (DEBUG_IntVars
[i
].pval
== &DEBUG_IntVars
[i
].val
)
102 RegSetValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
103 type
, (LPCVOID
)DEBUG_IntVars
[i
].pval
, count
);
110 DBG_INTVAR
* DEBUG_GetIntVar(const char* name
)
114 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
115 if (!strcmp(DEBUG_IntVars
[i
].name
, name
))
116 return &DEBUG_IntVars
[i
];
121 static WINE_EXCEPTION_FILTER(wine_dbg
)
123 DEBUG_Printf(DBG_CHN_MESG
, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
124 DEBUG_ExternalDebugger();
125 return EXCEPTION_EXECUTE_HANDLER
;
128 static DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
132 for (p
= DEBUG_ProcessList
; p
; p
= p
->next
)
133 if (p
->pid
== pid
) break;
137 static DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
)
139 DBG_PROCESS
* p
= DBG_alloc(sizeof(DBG_PROCESS
));
146 p
->continue_on_first_exception
= FALSE
;
152 p
->next
= DEBUG_ProcessList
;
154 if (DEBUG_ProcessList
) DEBUG_ProcessList
->prev
= p
;
155 DEBUG_ProcessList
= p
;
159 static void DEBUG_DelThread(DBG_THREAD
* p
);
161 static void DEBUG_DelProcess(DBG_PROCESS
* p
)
163 if (p
->threads
!= NULL
) {
164 DEBUG_Printf(DBG_CHN_ERR
, "Shouldn't happen\n");
165 while (p
->threads
) DEBUG_DelThread(p
->threads
);
167 if (p
->prev
) p
->prev
->next
= p
->next
;
168 if (p
->next
) p
->next
->prev
= p
->prev
;
169 if (p
== DEBUG_ProcessList
) DEBUG_ProcessList
= p
->next
;
170 if (p
== DEBUG_CurrProcess
) DEBUG_CurrProcess
= NULL
;
174 static void DEBUG_InitCurrProcess(void)
178 static BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
182 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
185 static BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
191 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
194 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
200 static DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
204 for (t
= p
->threads
; t
; t
= t
->next
)
205 if (t
->tid
== tid
) break;
209 static DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
210 HANDLE h
, LPVOID start
, LPVOID teb
)
212 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
221 t
->wait_for_first_exception
= 0;
222 t
->dbg_exec_mode
= EXEC_CONT
;
223 t
->dbg_exec_count
= 0;
224 sprintf(t
->name
, "%08lx", tid
);
227 t
->next
= p
->threads
;
229 if (p
->threads
) p
->threads
->prev
= t
;
235 static void DEBUG_InitCurrThread(void)
237 if (DEBUG_CurrThread
->start
) {
238 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
239 DBG_IVAR(BreakAllThreadsStartup
)) {
242 DEBUG_SetBreakpoints(FALSE
);
244 value
.cookie
= DV_TARGET
;
246 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
247 DEBUG_AddBreakpoint(&value
, NULL
);
248 DEBUG_SetBreakpoints(TRUE
);
251 DEBUG_CurrThread
->wait_for_first_exception
= 1;
255 static void DEBUG_DelThread(DBG_THREAD
* t
)
257 if (t
->prev
) t
->prev
->next
= t
->next
;
258 if (t
->next
) t
->next
->prev
= t
->prev
;
259 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
260 t
->process
->num_threads
--;
261 if (t
== DEBUG_CurrThread
) DEBUG_CurrThread
= NULL
;
265 BOOL
DEBUG_Attach(DWORD pid
, BOOL cofe
)
267 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0))) return FALSE
;
269 if (!DebugActiveProcess(pid
)) {
270 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
271 pid
, GetLastError());
274 DEBUG_CurrProcess
->continue_on_first_exception
= cofe
;
278 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
283 DEBUG_GetCurrentAddress(&addr
);
284 DEBUG_SuspendExecution();
288 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx).\n", addr
.off
);
290 switch(DEBUG_GetSelectorType(addr
.seg
))
293 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (%04lx:%08lx).\n", addr
.seg
, addr
.off
);
296 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04lx:%04lx).\n", addr
.seg
, addr
.off
);
299 DEBUG_Printf(DBG_CHN_MESG
, " in vm86 code (%04lx:%04lx).\n", addr
.seg
, addr
.off
);
302 DEBUG_Printf(DBG_CHN_MESG
, "bad CS (%lx)\n", addr
.seg
);
307 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
309 if (!force
&& is_debug
&&
310 DEBUG_ShouldContinue(&addr
,
312 DEBUG_CurrThread
->dbg_exec_mode
,
313 &DEBUG_CurrThread
->dbg_exec_count
))
316 if ((newmode
= DEBUG_GetSelectorType(addr
.seg
)) == MODE_INVALID
) newmode
= MODE_32
;
317 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
319 static const char * const names
[] = { "???", "16-bit", "32-bit", "vm86" };
320 DEBUG_Printf(DBG_CHN_MESG
,"In %s mode.\n", names
[newmode
] );
321 DEBUG_CurrThread
->dbg_mode
= newmode
;
326 if (is_debug
|| force
) {
328 * Do a quiet backtrace so that we have an idea of what the situation
329 * is WRT the source files.
331 DEBUG_BackTrace(FALSE
);
333 /* This is a real crash, dump some info */
334 DEBUG_InfoRegisters();
337 if (DEBUG_CurrThread
->dbg_mode
== MODE_16
) {
338 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
339 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
340 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
342 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
344 DEBUG_BackTrace(TRUE
);
348 (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_STEPI_OVER
) ||
349 (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_STEPI_INSTR
)) {
353 /* Show where we crashed */
355 DEBUG_DisassembleInstruction(&addr
);
357 /* resets list internal arguments so we can look at source code when needed */
358 DEBUG_FindNearestSymbol(&addr
, TRUE
, NULL
, 0, &list
);
359 if (list
.sourcefile
) DEBUG_List(&list
, NULL
, 0);
364 static DWORD
DEBUG_ExceptionEpilog(void)
366 DEBUG_CurrThread
->dbg_exec_mode
= DEBUG_RestartExecution(DEBUG_CurrThread
->dbg_exec_mode
,
367 DEBUG_CurrThread
->dbg_exec_count
);
369 * This will have gotten absorbed into the breakpoint info
370 * if it was used. Otherwise it would have been ignored.
371 * In any case, we don't mess with it any more.
373 if (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_CONT
|| DEBUG_CurrThread
->dbg_exec_mode
== EXEC_PASS
)
374 DEBUG_CurrThread
->dbg_exec_count
= 0;
376 return (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_PASS
) ? DBG_EXCEPTION_NOT_HANDLED
: DBG_CONTINUE
;
379 static BOOL
DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
, LPDWORD cont
)
381 BOOL is_debug
= FALSE
;
383 THREADNAME_INFO
*pThreadName
;
387 *cont
= DBG_CONTINUE
;
389 switch (rec
->ExceptionCode
)
391 case EXCEPTION_BREAKPOINT
:
392 case EXCEPTION_SINGLE_STEP
:
395 case EXCEPTION_NAME_THREAD
:
396 pThreadName
= (THREADNAME_INFO
*)(rec
->ExceptionInformation
);
397 if (pThreadName
->dwThreadID
== -1)
398 pThread
= DEBUG_CurrThread
;
400 pThread
= DEBUG_GetThread(DEBUG_CurrProcess
, pThreadName
->dwThreadID
);
402 if (ReadProcessMemory(DEBUG_CurrThread
->process
->handle
, pThreadName
->szName
,
403 pThread
->name
, 9, NULL
))
404 DEBUG_Printf (DBG_CHN_MESG
,
405 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
406 pThread
->tid
, pThread
->name
);
410 if (first_chance
&& !force
&& !DBG_IVAR(BreakOnFirstChance
))
412 /* pass exception to program except for debug exceptions */
413 *cont
= is_debug
? DBG_CONTINUE
: DBG_EXCEPTION_NOT_HANDLED
;
419 /* print some infos */
420 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
421 first_chance
? "First chance exception" : "Unhandled exception");
422 switch (rec
->ExceptionCode
)
424 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
425 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
427 case EXCEPTION_INT_OVERFLOW
:
428 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
430 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
431 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
433 case EXCEPTION_ILLEGAL_INSTRUCTION
:
434 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
436 case EXCEPTION_STACK_OVERFLOW
:
437 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
439 case EXCEPTION_PRIV_INSTRUCTION
:
440 DEBUG_Printf(DBG_CHN_MESG
, "priviledged instruction");
442 case EXCEPTION_ACCESS_VIOLATION
:
443 if (rec
->NumberParameters
== 2)
444 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
445 rec
->ExceptionInformation
[0] ? "write" : "read",
446 rec
->ExceptionInformation
[1]);
448 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
450 case EXCEPTION_DATATYPE_MISALIGNMENT
:
451 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
454 DEBUG_Printf(DBG_CHN_MESG
, "^C");
456 case EXCEPTION_CRITICAL_SECTION_WAIT
:
457 DEBUG_Printf(DBG_CHN_MESG
, "critical section %08lx wait failed",
458 rec
->ExceptionInformation
[0]);
459 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
461 DEBUG_Printf(DBG_CHN_MESG
, "\n");
465 case EXCEPTION_WINE_STUB
:
467 char dll
[32], name
[64];
468 DEBUG_ProcessGetString( dll
, sizeof(dll
), DEBUG_CurrThread
->process
->handle
,
469 (char *)rec
->ExceptionInformation
[0] );
470 DEBUG_ProcessGetString( name
, sizeof(name
), DEBUG_CurrThread
->process
->handle
,
471 (char *)rec
->ExceptionInformation
[1] );
472 DEBUG_Printf(DBG_CHN_MESG
, "unimplemented function %s.%s called", dll
, name
);
475 case EXCEPTION_VM86_INTx
:
476 DEBUG_Printf(DBG_CHN_MESG
, "interrupt %02lx in vm86 mode",
477 rec
->ExceptionInformation
[0]);
479 case EXCEPTION_VM86_STI
:
480 DEBUG_Printf(DBG_CHN_MESG
, "sti in vm86 mode");
482 case EXCEPTION_VM86_PICRETURN
:
483 DEBUG_Printf(DBG_CHN_MESG
, "PIC return in vm86 mode");
486 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
489 DEBUG_Printf(DBG_CHN_MESG
, "\n");
492 DEBUG_Printf(DBG_CHN_TRACE
,
493 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
495 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
499 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
501 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
)) {
502 while ((ret
= DEBUG_Parser())) {
503 if (DEBUG_ValidateRegisters()) {
504 if (DEBUG_CurrThread
->dbg_exec_mode
!= EXEC_PASS
|| first_chance
)
506 DEBUG_Printf(DBG_CHN_MESG
, "Cannot pass on last chance exception. You must use cont\n");
510 *cont
= DEBUG_ExceptionEpilog();
512 DEBUG_Printf(DBG_CHN_TRACE
,
513 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
515 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
519 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
524 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
, LPDWORD cont
)
529 DEBUG_CurrPid
= de
->dwProcessId
;
530 DEBUG_CurrTid
= de
->dwThreadId
;
536 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
537 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
539 DEBUG_CurrThread
= NULL
;
541 switch (de
->dwDebugEventCode
) {
542 case EXCEPTION_DEBUG_EVENT
:
543 if (!DEBUG_CurrThread
) {
544 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
545 de
->dwProcessId
, de
->dwThreadId
);
549 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
550 de
->dwProcessId
, de
->dwThreadId
,
551 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
553 if (DEBUG_CurrProcess
->continue_on_first_exception
) {
554 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
555 if (!DBG_IVAR(BreakOnAttach
)) {
556 *cont
= DBG_CONTINUE
;
561 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
563 #ifdef CONTEXT_SEGMENTS
566 #ifdef CONTEXT_DEBUG_REGISTERS
567 | CONTEXT_DEBUG_REGISTERS
571 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
)) {
572 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
576 ret
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
577 de
->u
.Exception
.dwFirstChance
,
578 DEBUG_CurrThread
->wait_for_first_exception
,
580 if (DEBUG_CurrThread
) {
581 DEBUG_CurrThread
->wait_for_first_exception
= 0;
582 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
586 case CREATE_THREAD_DEBUG_EVENT
:
587 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
588 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
590 if (DEBUG_CurrProcess
== NULL
) {
591 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
594 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
) {
595 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
599 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
601 de
->u
.CreateThread
.hThread
,
602 de
->u
.CreateThread
.lpStartAddress
,
603 de
->u
.CreateThread
.lpThreadLocalBase
);
604 if (!DEBUG_CurrThread
) {
605 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
608 DEBUG_InitCurrThread();
611 case CREATE_PROCESS_DEBUG_EVENT
:
612 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
613 de
->u
.CreateProcessInfo
.hProcess
,
614 de
->u
.CreateProcessInfo
.lpImageName
);
616 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
617 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
618 de
->dwProcessId
, de
->dwThreadId
,
620 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
621 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
622 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
624 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
) {
625 if (DEBUG_CurrProcess
->handle
) {
626 DEBUG_Printf(DBG_CHN_ERR
, "Skipping already defined process\n");
629 DEBUG_CurrProcess
->handle
= de
->u
.CreateProcessInfo
.hProcess
;
631 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
632 de
->u
.CreateProcessInfo
.hProcess
);
633 if (DEBUG_CurrProcess
== NULL
) {
634 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
639 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
640 de
->dwProcessId
, de
->dwThreadId
,
641 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
643 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
645 de
->u
.CreateProcessInfo
.hThread
,
646 de
->u
.CreateProcessInfo
.lpStartAddress
,
647 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
648 if (!DEBUG_CurrThread
) {
649 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
653 DEBUG_InitCurrProcess();
654 DEBUG_InitCurrThread();
656 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
657 DEBUG_CurrThread
->process
->handle
,
658 de
->u
.CreateProcessInfo
.lpImageName
);
659 DEBUG_LoadModule32(buffer
[0] ? buffer
: "<Debugged process>",
660 de
->u
.CreateProcessInfo
.hFile
,
661 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
663 if (buffer
[0]) /* we got a process name */
666 if (!GetBinaryTypeA( buffer
, &type
))
668 /* not a Windows binary, assume it's a Unix executable then */
669 char unixname
[MAX_PATH
];
670 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
671 if (wine_get_unix_file_name( buffer
, unixname
, sizeof(unixname
) ))
673 DEBUG_ReadExecutableDbgInfo( unixname
);
678 /* if it is a Windows binary, or an invalid or missing file name,
679 * we use wine itself as the main executable */
680 DEBUG_ReadExecutableDbgInfo( "wine" );
683 case EXIT_THREAD_DEBUG_EVENT
:
684 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
685 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
687 if (DEBUG_CurrThread
== NULL
) {
688 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
691 /* FIXME: remove break point set on thread startup */
692 DEBUG_DelThread(DEBUG_CurrThread
);
695 case EXIT_PROCESS_DEBUG_EVENT
:
696 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
697 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
699 if (DEBUG_CurrProcess
== NULL
) {
700 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
704 DEBUG_SetBreakpoints(FALSE
);
705 /* kill last thread */
706 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
707 DEBUG_DelProcess(DEBUG_CurrProcess
);
709 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
712 case LOAD_DLL_DEBUG_EVENT
:
713 if (DEBUG_CurrThread
== NULL
) {
714 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
717 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
718 DEBUG_CurrThread
->process
->handle
,
719 de
->u
.LoadDll
.lpImageName
);
721 /* FIXME unicode: de->u.LoadDll.fUnicode */
722 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
723 de
->dwProcessId
, de
->dwThreadId
,
724 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
725 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
726 de
->u
.LoadDll
.nDebugInfoSize
);
728 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
729 if (DBG_IVAR(BreakOnDllLoad
)) {
730 DEBUG_Printf(DBG_CHN_MESG
, "Stopping on DLL %s loading at %08lx\n",
731 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
);
732 ret
= DEBUG_Parser();
736 case UNLOAD_DLL_DEBUG_EVENT
:
737 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
738 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
741 case OUTPUT_DEBUG_STRING_EVENT
:
742 if (DEBUG_CurrThread
== NULL
) {
743 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
747 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
748 DEBUG_CurrThread
->process
->handle
,
749 de
->u
.DebugString
.lpDebugStringData
);
751 /* fixme unicode de->u.DebugString.fUnicode ? */
752 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
753 de
->dwProcessId
, de
->dwThreadId
, buffer
);
757 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
758 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
759 de
->u
.RipInfo
.dwType
);
763 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
764 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
767 } __EXCEPT(wine_dbg
) {
775 static DWORD
DEBUG_MainLoop(void)
781 DEBUG_Printf(DBG_CHN_MESG
, " on pid %lx\n", DEBUG_CurrPid
);
783 for (ret
= TRUE
; ret
; ) {
784 /* wait until we get at least one loaded process */
785 while (!DEBUG_ProcessList
&& (ret
= DEBUG_Parser()));
788 while (ret
&& DEBUG_ProcessList
&& WaitForDebugEvent(&de
, INFINITE
)) {
789 ret
= DEBUG_HandleDebugEvent(&de
, &cont
);
790 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, cont
);
794 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
799 static BOOL
DEBUG_Start(LPSTR cmdLine
)
801 PROCESS_INFORMATION info
;
802 STARTUPINFOA startup
;
804 memset(&startup
, 0, sizeof(startup
));
805 startup
.cb
= sizeof(startup
);
806 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
807 startup
.wShowWindow
= SW_SHOWNORMAL
;
809 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
810 FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &startup
, &info
)) {
811 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
814 DEBUG_CurrPid
= info
.dwProcessId
;
815 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0))) return FALSE
;
820 void DEBUG_Run(const char* args
)
822 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
823 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
826 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
828 if (!DEBUG_LastCmdLine
) {
829 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
832 DEBUG_Start(DEBUG_LastCmdLine
);
836 int DEBUG_main(int argc
, char** argv
)
841 /* Initialize the debugger heap. */
842 dbg_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0x1000, 0x8000000); /* 128MB */
845 /* Initialize the type handling stuff. */
847 DEBUG_InitCVDataTypes();
849 /* Initialize internal vars (types must be initialized before) */
850 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
852 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
853 * stdout and stderr streams
855 if (DBG_IVAR(UseXTerm
)) {
858 /* This is a hack: it forces creation of an xterm, not done by default */
859 pos
.X
= 0; pos
.Y
= 1;
860 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
), pos
);
863 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg starting... ");
869 if ((pid
= atoi(argv
[1])) != 0 && (hEvent
= (HANDLE
)atoi(argv
[2])) != 0) {
870 if (!DEBUG_Attach(pid
, TRUE
)) {
871 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
872 DEBUG_CurrPid
, GetLastError());
873 /* don't care about result */
877 if (!SetEvent(hEvent
)) {
878 DEBUG_Printf(DBG_CHN_ERR
, "Invalid event handle: %p\n", hEvent
);
886 if (DEBUG_CurrPid
== 0 && argc
> 1) {
890 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
893 for (i
= 1; i
< argc
; i
++) {
894 len
+= strlen(argv
[i
]) + 1;
895 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
896 strcat(cmdLine
, argv
[i
]);
897 cmdLine
[len
- 2] = ' ';
898 cmdLine
[len
- 1] = '\0';
901 if (!DEBUG_Start(cmdLine
)) {
902 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
905 DBG_free(DEBUG_LastCmdLine
);
906 DEBUG_LastCmdLine
= cmdLine
;
909 retv
= DEBUG_MainLoop();
911 /* saves modified variables */
912 DEBUG_IntVarsRW(FALSE
);
917 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");