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
= wvsnprintf(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
;
151 p
->next
= DEBUG_ProcessList
;
153 if (DEBUG_ProcessList
) DEBUG_ProcessList
->prev
= p
;
154 DEBUG_ProcessList
= p
;
158 static void DEBUG_DelThread(DBG_THREAD
* p
);
160 static void DEBUG_DelProcess(DBG_PROCESS
* p
)
162 if (p
->threads
!= NULL
) {
163 DEBUG_Printf(DBG_CHN_ERR
, "Shouldn't happen\n");
164 while (p
->threads
) DEBUG_DelThread(p
->threads
);
166 if (p
->prev
) p
->prev
->next
= p
->next
;
167 if (p
->next
) p
->next
->prev
= p
->prev
;
168 if (p
== DEBUG_ProcessList
) DEBUG_ProcessList
= p
->next
;
169 if (p
== DEBUG_CurrProcess
) DEBUG_CurrProcess
= NULL
;
173 static void DEBUG_InitCurrProcess(void)
177 static BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
181 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
184 static BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
190 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
193 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
199 static DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
203 for (t
= p
->threads
; t
; t
= t
->next
)
204 if (t
->tid
== tid
) break;
208 static DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
209 HANDLE h
, LPVOID start
, LPVOID teb
)
211 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
220 t
->wait_for_first_exception
= 0;
221 t
->dbg_exec_mode
= EXEC_CONT
;
222 t
->dbg_exec_count
= 0;
225 t
->next
= p
->threads
;
227 if (p
->threads
) p
->threads
->prev
= t
;
233 static void DEBUG_InitCurrThread(void)
235 if (DEBUG_CurrThread
->start
) {
236 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
237 DBG_IVAR(BreakAllThreadsStartup
)) {
240 DEBUG_SetBreakpoints(FALSE
);
242 value
.cookie
= DV_TARGET
;
244 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
245 DEBUG_AddBreakpoint(&value
, NULL
);
246 DEBUG_SetBreakpoints(TRUE
);
249 DEBUG_CurrThread
->wait_for_first_exception
= 1;
253 static void DEBUG_DelThread(DBG_THREAD
* t
)
255 if (t
->prev
) t
->prev
->next
= t
->next
;
256 if (t
->next
) t
->next
->prev
= t
->prev
;
257 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
258 t
->process
->num_threads
--;
259 if (t
== DEBUG_CurrThread
) DEBUG_CurrThread
= NULL
;
263 BOOL
DEBUG_Attach(DWORD pid
, BOOL cofe
)
265 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0))) return FALSE
;
267 if (!DebugActiveProcess(pid
)) {
268 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
269 pid
, GetLastError());
272 DEBUG_CurrProcess
->continue_on_first_exception
= cofe
;
276 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
281 DEBUG_GetCurrentAddress(&addr
);
282 DEBUG_SuspendExecution();
286 if (DEBUG_IsSelectorSystem(DEBUG_context
.SegCs
))
287 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx).\n", addr
.off
);
289 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04x:%04lx).\n",
290 LOWORD(addr
.seg
), addr
.off
);
292 DEBUG_Printf(DBG_CHN_MESG
, " (0x%08lx).\n", addr
.off
);
296 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
298 if (!force
&& is_debug
&&
299 DEBUG_ShouldContinue(code
,
300 DEBUG_CurrThread
->dbg_exec_mode
,
301 &DEBUG_CurrThread
->dbg_exec_count
))
305 switch (newmode
= DEBUG_GetSelectorType(addr
.seg
)) {
306 case 16: case 32: break;
307 default: DEBUG_Printf(DBG_CHN_MESG
, "Bad CS (%ld)\n", addr
.seg
); newmode
= 32;
312 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
313 DEBUG_Printf(DBG_CHN_MESG
,"In %d bit mode.\n", DEBUG_CurrThread
->dbg_mode
= newmode
);
317 if (is_debug
|| force
) {
319 * Do a quiet backtrace so that we have an idea of what the situation
320 * is WRT the source files.
322 DEBUG_BackTrace(FALSE
);
324 /* This is a real crash, dump some info */
325 DEBUG_InfoRegisters();
328 if (DEBUG_CurrThread
->dbg_mode
== 16) {
329 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
330 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
331 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
333 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
335 DEBUG_BackTrace(TRUE
);
339 (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_STEPI_OVER
) ||
340 (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_STEPI_INSTR
)) {
341 /* Show where we crashed */
343 DEBUG_PrintAddress( &addr
, DEBUG_CurrThread
->dbg_mode
, TRUE
);
344 DEBUG_Printf(DBG_CHN_MESG
,": ");
345 DEBUG_Disasm( &addr
, TRUE
);
346 DEBUG_Printf( DBG_CHN_MESG
, "\n" );
351 static DWORD
DEBUG_ExceptionEpilog(void)
353 DEBUG_CurrThread
->dbg_exec_mode
= DEBUG_RestartExecution(DEBUG_CurrThread
->dbg_exec_mode
,
354 DEBUG_CurrThread
->dbg_exec_count
);
356 * This will have gotten absorbed into the breakpoint info
357 * if it was used. Otherwise it would have been ignored.
358 * In any case, we don't mess with it any more.
360 if (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_CONT
|| DEBUG_CurrThread
->dbg_exec_mode
== EXEC_PASS
)
361 DEBUG_CurrThread
->dbg_exec_count
= 0;
363 return (DEBUG_CurrThread
->dbg_exec_mode
== EXEC_PASS
) ? DBG_EXCEPTION_NOT_HANDLED
: DBG_CONTINUE
;
366 static BOOL
DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
, LPDWORD cont
)
368 BOOL is_debug
= FALSE
;
371 *cont
= DBG_CONTINUE
;
372 if (first_chance
&& !force
&& !DBG_IVAR(BreakOnFirstChance
)) return TRUE
;
374 switch (rec
->ExceptionCode
)
376 case EXCEPTION_BREAKPOINT
:
377 case EXCEPTION_SINGLE_STEP
:
384 /* print some infos */
385 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
386 first_chance
? "First chance exception" : "Unhandled exception");
387 switch (rec
->ExceptionCode
)
389 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
390 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
392 case EXCEPTION_INT_OVERFLOW
:
393 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
395 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
396 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
398 case EXCEPTION_ILLEGAL_INSTRUCTION
:
399 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
401 case EXCEPTION_STACK_OVERFLOW
:
402 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
404 case EXCEPTION_PRIV_INSTRUCTION
:
405 DEBUG_Printf(DBG_CHN_MESG
, "priviledged instruction");
407 case EXCEPTION_ACCESS_VIOLATION
:
408 if (rec
->NumberParameters
== 2)
409 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
410 rec
->ExceptionInformation
[0] ? "write" : "read",
411 rec
->ExceptionInformation
[1]);
413 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
415 case EXCEPTION_DATATYPE_MISALIGNMENT
:
416 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
419 DEBUG_Printf(DBG_CHN_MESG
, "^C");
421 case EXCEPTION_CRITICAL_SECTION_WAIT
:
422 DEBUG_Printf(DBG_CHN_MESG
, "critical section %08lx wait failed",
423 rec
->ExceptionInformation
[0]);
424 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
428 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
431 DEBUG_Printf(DBG_CHN_MESG
, "\n");
434 DEBUG_Printf(DBG_CHN_TRACE
,
435 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
437 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
441 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
443 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
)) {
444 while ((ret
= DEBUG_Parser())) {
445 if (DEBUG_ValidateRegisters()) {
446 if (DEBUG_CurrThread
->dbg_exec_mode
!= EXEC_PASS
|| first_chance
)
448 DEBUG_Printf(DBG_CHN_MESG
, "Cannot pass on last chance exception. You must use cont\n");
452 *cont
= DEBUG_ExceptionEpilog();
454 DEBUG_Printf(DBG_CHN_TRACE
,
455 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
457 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
461 DEBUG_CurrThread
->dbg_exec_mode
, DEBUG_CurrThread
->dbg_exec_count
);
466 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
, LPDWORD cont
)
471 DEBUG_CurrPid
= de
->dwProcessId
;
472 DEBUG_CurrTid
= de
->dwThreadId
;
478 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
479 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
481 DEBUG_CurrThread
= NULL
;
483 switch (de
->dwDebugEventCode
) {
484 case EXCEPTION_DEBUG_EVENT
:
485 if (!DEBUG_CurrThread
) {
486 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
487 de
->dwProcessId
, de
->dwThreadId
);
491 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
492 de
->dwProcessId
, de
->dwThreadId
,
493 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
495 if (DEBUG_CurrProcess
->continue_on_first_exception
) {
496 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
497 if (!DBG_IVAR(BreakOnAttach
)) {
498 *cont
= DBG_CONTINUE
;
503 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
505 #ifdef CONTEXT_SEGMENTS
508 #ifdef CONTEXT_DEBUG_REGISTERS
509 | CONTEXT_DEBUG_REGISTERS
513 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
)) {
514 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
518 ret
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
519 de
->u
.Exception
.dwFirstChance
,
520 DEBUG_CurrThread
->wait_for_first_exception
,
522 if (DEBUG_CurrThread
) {
523 DEBUG_CurrThread
->wait_for_first_exception
= 0;
524 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
528 case CREATE_THREAD_DEBUG_EVENT
:
529 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
530 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
532 if (DEBUG_CurrProcess
== NULL
) {
533 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
536 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
) {
537 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
541 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
543 de
->u
.CreateThread
.hThread
,
544 de
->u
.CreateThread
.lpStartAddress
,
545 de
->u
.CreateThread
.lpThreadLocalBase
);
546 if (!DEBUG_CurrThread
) {
547 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
550 DEBUG_InitCurrThread();
553 case CREATE_PROCESS_DEBUG_EVENT
:
554 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
555 de
->u
.CreateProcessInfo
.hProcess
,
556 de
->u
.CreateProcessInfo
.lpImageName
);
558 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
559 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
560 de
->dwProcessId
, de
->dwThreadId
,
562 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
563 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
564 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
566 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
) {
567 if (DEBUG_CurrProcess
->handle
) {
568 DEBUG_Printf(DBG_CHN_ERR
, "Skipping already defined process\n");
571 DEBUG_CurrProcess
->handle
= de
->u
.CreateProcessInfo
.hProcess
;
573 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
574 de
->u
.CreateProcessInfo
.hProcess
);
575 if (DEBUG_CurrProcess
== NULL
) {
576 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
581 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
582 de
->dwProcessId
, de
->dwThreadId
,
583 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
585 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
587 de
->u
.CreateProcessInfo
.hThread
,
588 de
->u
.CreateProcessInfo
.lpStartAddress
,
589 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
590 if (!DEBUG_CurrThread
) {
591 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
595 DEBUG_InitCurrProcess();
596 DEBUG_InitCurrThread();
598 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
599 DEBUG_CurrThread
->process
->handle
,
600 de
->u
.CreateProcessInfo
.lpImageName
);
601 DEBUG_LoadModule32(buffer
[0] ? buffer
: "<Debugged process>",
602 de
->u
.CreateProcessInfo
.hFile
,
603 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
605 if (buffer
[0]) /* we got a process name */
608 if (!GetBinaryTypeA( buffer
, &type
))
610 /* not a Windows binary, assume it's a Unix executable then */
611 DOS_FULL_NAME fullname
;
612 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
613 if (DOSFS_GetFullName( buffer
, TRUE
, &fullname
))
615 DEBUG_ReadExecutableDbgInfo( fullname
.long_name
);
620 /* if it is a Windows binary, or an invalid or missing file name,
621 * we use wine itself as the main executable */
622 DEBUG_ReadExecutableDbgInfo( "wine" );
625 case EXIT_THREAD_DEBUG_EVENT
:
626 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
627 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
629 if (DEBUG_CurrThread
== NULL
) {
630 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
633 /* FIXME: remove break point set on thread startup */
634 DEBUG_DelThread(DEBUG_CurrThread
);
637 case EXIT_PROCESS_DEBUG_EVENT
:
638 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
639 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
641 if (DEBUG_CurrProcess
== NULL
) {
642 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
646 DEBUG_SetBreakpoints(FALSE
);
647 /* kill last thread */
648 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
649 DEBUG_DelProcess(DEBUG_CurrProcess
);
651 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
654 case LOAD_DLL_DEBUG_EVENT
:
655 if (DEBUG_CurrThread
== NULL
) {
656 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
659 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
660 DEBUG_CurrThread
->process
->handle
,
661 de
->u
.LoadDll
.lpImageName
);
663 /* FIXME unicode: de->u.LoadDll.fUnicode */
664 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
665 de
->dwProcessId
, de
->dwThreadId
,
666 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
667 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
668 de
->u
.LoadDll
.nDebugInfoSize
);
670 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
671 if (DBG_IVAR(BreakOnDllLoad
)) ret
= DEBUG_Parser();
674 case UNLOAD_DLL_DEBUG_EVENT
:
675 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
676 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
679 case OUTPUT_DEBUG_STRING_EVENT
:
680 if (DEBUG_CurrThread
== NULL
) {
681 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
685 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
686 DEBUG_CurrThread
->process
->handle
,
687 de
->u
.DebugString
.lpDebugStringData
);
689 /* fixme unicode de->u.DebugString.fUnicode ? */
690 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
691 de
->dwProcessId
, de
->dwThreadId
, buffer
);
695 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
696 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
697 de
->u
.RipInfo
.dwType
);
701 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
702 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
705 } __EXCEPT(wine_dbg
) {
713 static DWORD
DEBUG_MainLoop(void)
719 DEBUG_Printf(DBG_CHN_MESG
, " on pid %ld\n", DEBUG_CurrPid
);
721 for (ret
= TRUE
; ret
; ) {
722 /* wait until we get at least one loaded process */
723 while (!DEBUG_ProcessList
&& (ret
= DEBUG_Parser()));
726 while (ret
&& DEBUG_ProcessList
&& WaitForDebugEvent(&de
, INFINITE
)) {
727 ret
= DEBUG_HandleDebugEvent(&de
, &cont
);
728 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, cont
);
732 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %ld\n", DEBUG_CurrPid
);
737 static BOOL
DEBUG_Start(LPSTR cmdLine
)
739 PROCESS_INFORMATION info
;
740 STARTUPINFOA startup
;
742 memset(&startup
, 0, sizeof(startup
));
743 startup
.cb
= sizeof(startup
);
744 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
745 startup
.wShowWindow
= SW_SHOWNORMAL
;
747 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
748 FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &startup
, &info
)) {
749 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
752 DEBUG_CurrPid
= info
.dwProcessId
;
753 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0))) return FALSE
;
758 void DEBUG_Run(const char* args
)
760 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
761 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
764 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
766 if (!DEBUG_LastCmdLine
) {
767 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
770 DEBUG_Start(DEBUG_LastCmdLine
);
774 int DEBUG_main(int argc
, char** argv
)
779 /* Initialize the debugger heap. */
780 dbg_heap
= HeapCreate(HEAP_NO_SERIALIZE
, 0x1000, 0x8000000); /* 128MB */
783 /* Initialize the type handling stuff. */
785 DEBUG_InitCVDataTypes();
787 /* Initialize internal vars (types must be initialized before) */
788 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
790 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
791 * stdout and stderr streams
793 if (DBG_IVAR(UseXTerm
)) {
796 /* This is a hack: it forces creation of an xterm, not done by default */
797 pos
.X
= 0; pos
.Y
= 1;
798 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
), pos
);
801 DEBUG_Printf(DBG_CHN_MESG
, "Starting WineDbg... ");
807 if ((pid
= atoi(argv
[1])) != 0 && (hEvent
= atoi(argv
[2])) != 0) {
808 BOOL ret
= DEBUG_Attach(pid
, TRUE
);
812 DEBUG_Printf(DBG_CHN_ERR
, "Can't attach process %ld: %ld\n",
813 DEBUG_CurrPid
, GetLastError());
820 if (DEBUG_CurrPid
== 0 && argc
> 1) {
824 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
827 for (i
= 1; i
< argc
; i
++) {
828 len
+= strlen(argv
[i
]) + 1;
829 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
830 strcat(cmdLine
, argv
[i
]);
831 cmdLine
[len
- 2] = ' ';
832 cmdLine
[len
- 1] = '\0';
835 if (!DEBUG_Start(cmdLine
)) {
836 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
839 DBG_free(DEBUG_LastCmdLine
);
840 DEBUG_LastCmdLine
= cmdLine
;
843 retv
= DEBUG_MainLoop();
845 /* saves modified variables */
846 DEBUG_IntVarsRW(FALSE
);
851 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");