1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
5 * Copyright 2000 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "msvcrt/excpt.h"
35 #include "wine/library.h"
37 DBG_PROCESS
* DEBUG_CurrProcess
= NULL
;
38 DBG_THREAD
* DEBUG_CurrThread
= NULL
;
41 CONTEXT DEBUG_context
;
42 BOOL DEBUG_interactiveP
= FALSE
;
43 enum exit_mode DEBUG_ExitMode
= EXIT_CONTINUE
;
45 int automatic_mode
= 0;
46 static char* DEBUG_LastCmdLine
= NULL
;
48 static DBG_PROCESS
* DEBUG_ProcessList
= NULL
;
49 DBG_INTVAR DEBUG_IntVars
[DBG_IV_LAST
];
51 void DEBUG_OutputA(int chn
, const char* buffer
, int len
)
53 if (DBG_IVAR(ConChannelMask
) & chn
)
54 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
55 if (DBG_IVAR(StdChannelMask
) & chn
)
56 fwrite(buffer
, len
, 1, stderr
);
59 void DEBUG_OutputW(int chn
, const WCHAR
* buffer
, int len
)
61 /* FIXME: this won't work is std output isn't attached to a console */
62 if (DBG_IVAR(ConChannelMask
) & chn
)
63 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
64 /* simplistic Unicode to ANSI conversion */
65 if (DBG_IVAR(StdChannelMask
) & chn
)
66 while (len
--) fputc((char)*buffer
++, stderr
);
69 int DEBUG_Printf(int chn
, const char* format
, ...)
71 static char buf
[4*1024];
75 va_start(valist
, format
);
76 len
= vsnprintf(buf
, sizeof(buf
), format
, valist
);
79 if (len
<= -1 || len
>= sizeof(buf
)) {
80 len
= sizeof(buf
) - 1;
82 buf
[len
- 1] = buf
[len
- 2] = buf
[len
- 3] = '.';
84 DEBUG_OutputA(chn
, buf
, len
);
88 static BOOL
DEBUG_IntVarsRW(int read
)
91 DWORD type
= REG_DWORD
;
93 DWORD count
= sizeof(val
);
95 DBG_INTVAR
* div
= DEBUG_IntVars
;
98 /* initializes internal vars table */
99 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
100 div->val = _val; div->name = #_var; div->pval = _ref; \
101 div->type = DEBUG_GetBasicType(_typ); div++;
106 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
107 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
109 fprintf(stderr
, "Cannot create WineDbg key in registry\n");
113 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
115 if (!DEBUG_IntVars
[i
].pval
) {
116 if (!RegQueryValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
117 &type
, (LPSTR
)&val
, &count
))
118 DEBUG_IntVars
[i
].val
= val
;
119 DEBUG_IntVars
[i
].pval
= &DEBUG_IntVars
[i
].val
;
121 *DEBUG_IntVars
[i
].pval
= 0;
124 /* FIXME: type should be infered from basic type -if any- of intvar */
125 if (DEBUG_IntVars
[i
].pval
== &DEBUG_IntVars
[i
].val
)
126 RegSetValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
127 type
, (LPCVOID
)DEBUG_IntVars
[i
].pval
, count
);
134 DBG_INTVAR
* DEBUG_GetIntVar(const char* name
)
138 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
139 if (!strcmp(DEBUG_IntVars
[i
].name
, name
))
140 return &DEBUG_IntVars
[i
];
145 static WINE_EXCEPTION_FILTER(wine_dbg
)
147 DEBUG_Printf(DBG_CHN_MESG
, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
148 DEBUG_ExternalDebugger();
149 return EXCEPTION_EXECUTE_HANDLER
;
152 DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
156 for (p
= DEBUG_ProcessList
; p
; p
= p
->next
)
157 if (p
->pid
== pid
) break;
161 static DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
, const char* imageName
)
163 DBG_PROCESS
* p
= DBG_alloc(sizeof(DBG_PROCESS
));
168 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
171 p
->continue_on_first_exception
= FALSE
;
176 p
->delayed_bp
= NULL
;
177 p
->num_delayed_bp
= 0;
179 p
->next
= DEBUG_ProcessList
;
181 if (DEBUG_ProcessList
) DEBUG_ProcessList
->prev
= p
;
182 DEBUG_ProcessList
= p
;
186 static void DEBUG_DelThread(DBG_THREAD
* p
);
188 static void DEBUG_DelProcess(DBG_PROCESS
* p
)
192 if (p
->threads
!= NULL
) {
193 DEBUG_Printf(DBG_CHN_ERR
, "Shouldn't happen\n");
194 while (p
->threads
) DEBUG_DelThread(p
->threads
);
196 for (i
= 0; i
< p
->num_delayed_bp
; i
++) {
197 DBG_free(p
->delayed_bp
[i
].name
);
199 DBG_free(p
->delayed_bp
);
200 if (p
->prev
) p
->prev
->next
= p
->next
;
201 if (p
->next
) p
->next
->prev
= p
->prev
;
202 if (p
== DEBUG_ProcessList
) DEBUG_ProcessList
= p
->next
;
203 if (p
== DEBUG_CurrProcess
) DEBUG_CurrProcess
= NULL
;
204 DBG_free((char*)p
->imageName
);
208 static void DEBUG_InitCurrProcess(void)
212 static BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
216 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
219 static BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
225 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
228 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
234 DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
238 for (t
= p
->threads
; t
; t
= t
->next
)
239 if (t
->tid
== tid
) break;
243 static DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
244 HANDLE h
, LPVOID start
, LPVOID teb
)
246 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
255 t
->wait_for_first_exception
= 0;
256 t
->exec_mode
= EXEC_CONT
;
259 sprintf(t
->name
, "%08lx", tid
);
262 t
->next
= p
->threads
;
264 if (p
->threads
) p
->threads
->prev
= t
;
270 static void DEBUG_InitCurrThread(void)
272 if (DEBUG_CurrThread
->start
) {
273 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
274 DBG_IVAR(BreakAllThreadsStartup
)) {
277 DEBUG_SetBreakpoints(FALSE
);
279 value
.cookie
= DV_TARGET
;
281 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
282 DEBUG_AddBreakpoint(&value
, NULL
);
283 DEBUG_SetBreakpoints(TRUE
);
286 DEBUG_CurrThread
->wait_for_first_exception
= 1;
290 static void DEBUG_DelThread(DBG_THREAD
* t
)
292 if (t
->prev
) t
->prev
->next
= t
->next
;
293 if (t
->next
) t
->next
->prev
= t
->prev
;
294 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
295 t
->process
->num_threads
--;
296 if (t
== DEBUG_CurrThread
) DEBUG_CurrThread
= NULL
;
300 BOOL
DEBUG_Attach(DWORD pid
, BOOL cofe
)
302 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0, NULL
))) return FALSE
;
304 if (!DebugActiveProcess(pid
)) {
305 DEBUG_Printf(DBG_CHN_MESG
, "Can't attach process %lx: error %ld\n", pid
, GetLastError());
306 DEBUG_DelProcess(DEBUG_CurrProcess
);
307 DEBUG_CurrProcess
= NULL
;
310 DEBUG_CurrProcess
->continue_on_first_exception
= cofe
;
314 BOOL
DEBUG_Detach(void)
316 /* remove all set breakpoints in debuggee code */
317 DEBUG_SetBreakpoints(FALSE
);
318 /* needed for single stepping (ugly).
319 * should this be handled inside the server ??? */
321 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
323 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
324 DebugActiveProcessStop(DEBUG_CurrProcess
->pid
);
325 DEBUG_DelProcess(DEBUG_CurrProcess
);
326 DEBUG_CurrProcess
= NULL
;
327 /* FIXME: should zero out the symbol table too */
331 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
336 DEBUG_GetCurrentAddress(&addr
);
337 DEBUG_SuspendExecution();
341 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx)", addr
.off
);
343 switch(DEBUG_GetSelectorType(addr
.seg
))
346 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (%04lx:%08lx)", addr
.seg
, addr
.off
);
349 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04lx:%04lx)", addr
.seg
, addr
.off
);
352 DEBUG_Printf(DBG_CHN_MESG
, " in vm86 code (%04lx:%04lx)", addr
.seg
, addr
.off
);
355 DEBUG_Printf(DBG_CHN_MESG
, " bad CS (%lx)", addr
.seg
);
358 DEBUG_Printf(DBG_CHN_MESG
, ".\n");
361 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
363 if (!force
&& is_debug
&&
364 DEBUG_ShouldContinue(&addr
, code
,
365 &DEBUG_CurrThread
->exec_count
))
368 if ((newmode
= DEBUG_GetSelectorType(addr
.seg
)) == MODE_INVALID
) newmode
= MODE_32
;
369 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
371 static const char * const names
[] = { "???", "16-bit", "32-bit", "vm86" };
372 DEBUG_Printf(DBG_CHN_MESG
,"In %s mode.\n", names
[newmode
] );
373 DEBUG_CurrThread
->dbg_mode
= newmode
;
378 if (is_debug
|| force
) {
380 * Do a quiet backtrace so that we have an idea of what the situation
381 * is WRT the source files.
383 DEBUG_BackTrace(DEBUG_CurrTid
, FALSE
);
385 /* This is a real crash, dump some info */
386 DEBUG_InfoRegisters();
389 if (DEBUG_CurrThread
->dbg_mode
== MODE_16
) {
390 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
391 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
392 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
394 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
396 DEBUG_BackTrace(DEBUG_CurrTid
, TRUE
);
400 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_OVER
) ||
401 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_INSTR
)) {
405 /* Show where we crashed */
407 DEBUG_DisassembleInstruction(&addr
);
409 /* resets list internal arguments so we can look at source code when needed */
410 DEBUG_FindNearestSymbol(&addr
, TRUE
, NULL
, 0, &list
);
411 if (list
.sourcefile
) DEBUG_List(&list
, NULL
, 0);
416 static void DEBUG_ExceptionEpilog(void)
418 DEBUG_RestartExecution(DEBUG_CurrThread
->exec_count
);
420 * This will have gotten absorbed into the breakpoint info
421 * if it was used. Otherwise it would have been ignored.
422 * In any case, we don't mess with it any more.
424 if (DEBUG_CurrThread
->exec_mode
== EXEC_CONT
)
425 DEBUG_CurrThread
->exec_count
= 0;
428 static void DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
430 BOOL is_debug
= FALSE
;
431 THREADNAME_INFO
*pThreadName
;
434 assert(DEBUG_CurrThread
);
436 DEBUG_ExitMode
= EXIT_CONTINUE
;
438 switch (rec
->ExceptionCode
)
440 case EXCEPTION_BREAKPOINT
:
441 case EXCEPTION_SINGLE_STEP
:
444 case EXCEPTION_NAME_THREAD
:
445 pThreadName
= (THREADNAME_INFO
*)(rec
->ExceptionInformation
);
446 if (pThreadName
->dwThreadID
== -1)
447 pThread
= DEBUG_CurrThread
;
449 pThread
= DEBUG_GetThread(DEBUG_CurrProcess
, pThreadName
->dwThreadID
);
451 if (ReadProcessMemory(DEBUG_CurrThread
->process
->handle
, pThreadName
->szName
,
452 pThread
->name
, 9, NULL
))
453 DEBUG_Printf (DBG_CHN_MESG
,
454 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
455 pThread
->tid
, pThread
->name
);
459 if (first_chance
&& !is_debug
&& !force
&& !DBG_IVAR(BreakOnFirstChance
))
461 /* pass exception to program except for debug exceptions */
462 if (!is_debug
) DEBUG_ExitMode
= EXIT_PASS
;
468 /* print some infos */
469 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
470 first_chance
? "First chance exception" : "Unhandled exception");
471 switch (rec
->ExceptionCode
)
473 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
474 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
476 case EXCEPTION_INT_OVERFLOW
:
477 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
479 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
480 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
482 case EXCEPTION_ILLEGAL_INSTRUCTION
:
483 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
485 case EXCEPTION_STACK_OVERFLOW
:
486 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
488 case EXCEPTION_PRIV_INSTRUCTION
:
489 DEBUG_Printf(DBG_CHN_MESG
, "privileged instruction");
491 case EXCEPTION_ACCESS_VIOLATION
:
492 if (rec
->NumberParameters
== 2)
493 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
494 rec
->ExceptionInformation
[0] ? "write" : "read",
495 rec
->ExceptionInformation
[1]);
497 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
499 case EXCEPTION_DATATYPE_MISALIGNMENT
:
500 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
503 DEBUG_Printf(DBG_CHN_MESG
, "^C");
506 DEBUG_Printf(DBG_CHN_MESG
, "^C");
508 case EXCEPTION_CRITICAL_SECTION_WAIT
:
513 addr
.off
= rec
->ExceptionInformation
[0];
515 DEBUG_Printf(DBG_CHN_MESG
, "wait failed on critical section ");
516 DEBUG_PrintAddress(&addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
518 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
520 DEBUG_Printf(DBG_CHN_MESG
, "\n");
524 case EXCEPTION_WINE_STUB
:
526 char dll
[32], name
[64];
527 DEBUG_ProcessGetString( dll
, sizeof(dll
), DEBUG_CurrThread
->process
->handle
,
528 (char *)rec
->ExceptionInformation
[0] );
529 DEBUG_ProcessGetString( name
, sizeof(name
), DEBUG_CurrThread
->process
->handle
,
530 (char *)rec
->ExceptionInformation
[1] );
531 DEBUG_Printf(DBG_CHN_MESG
, "unimplemented function %s.%s called", dll
, name
);
534 case EXCEPTION_VM86_INTx
:
535 DEBUG_Printf(DBG_CHN_MESG
, "interrupt %02lx in vm86 mode",
536 rec
->ExceptionInformation
[0]);
538 case EXCEPTION_VM86_STI
:
539 DEBUG_Printf(DBG_CHN_MESG
, "sti in vm86 mode");
541 case EXCEPTION_VM86_PICRETURN
:
542 DEBUG_Printf(DBG_CHN_MESG
, "PIC return in vm86 mode");
545 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
551 DEBUG_Printf(DBG_CHN_TRACE
,
552 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
554 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
558 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
563 DEBUG_ExceptionProlog(is_debug
, FALSE
, rec
->ExceptionCode
);
564 DEBUG_ExitMode
= EXIT_QUIT
;
565 return; /* terminate execution */
568 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
)) {
569 DEBUG_interactiveP
= TRUE
;
573 if (DEBUG_ExitMode
== EXIT_QUIT
|| DEBUG_ExitMode
== EXIT_DETACH
)
575 if (DEBUG_ValidateRegisters()) {
576 if (DEBUG_ExitMode
== EXIT_CONTINUE
|| first_chance
)
578 DEBUG_Printf(DBG_CHN_MESG
, "Cannot pass on last chance exception. You must use cont\n");
581 DEBUG_interactiveP
= FALSE
;
583 DEBUG_ExceptionEpilog();
586 DEBUG_Printf(DBG_CHN_TRACE
,
587 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
589 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
593 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
597 static void DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
)
601 DEBUG_CurrPid
= de
->dwProcessId
;
602 DEBUG_CurrTid
= de
->dwThreadId
;
604 DEBUG_ExitMode
= EXIT_CONTINUE
;
606 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
607 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
609 DEBUG_CurrThread
= NULL
;
611 switch (de
->dwDebugEventCode
) {
612 case EXCEPTION_DEBUG_EVENT
:
613 if (!DEBUG_CurrThread
) {
614 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
615 de
->dwProcessId
, de
->dwThreadId
);
619 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
620 de
->dwProcessId
, de
->dwThreadId
,
621 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
623 if (DEBUG_CurrProcess
->continue_on_first_exception
) {
624 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
625 if (!DBG_IVAR(BreakOnAttach
)) {
630 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
632 #ifdef CONTEXT_SEGMENTS
635 #ifdef CONTEXT_DEBUG_REGISTERS
636 | CONTEXT_DEBUG_REGISTERS
640 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
)) {
641 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
645 DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
646 de
->u
.Exception
.dwFirstChance
,
647 DEBUG_CurrThread
->wait_for_first_exception
);
648 if (DEBUG_CurrThread
) {
649 DEBUG_CurrThread
->wait_for_first_exception
= 0;
650 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
654 case CREATE_THREAD_DEBUG_EVENT
:
655 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
656 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
658 if (DEBUG_CurrProcess
== NULL
) {
659 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
662 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
) {
663 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
667 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
669 de
->u
.CreateThread
.hThread
,
670 de
->u
.CreateThread
.lpStartAddress
,
671 de
->u
.CreateThread
.lpThreadLocalBase
);
672 if (!DEBUG_CurrThread
) {
673 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
676 DEBUG_InitCurrThread();
679 case CREATE_PROCESS_DEBUG_EVENT
:
680 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
681 de
->u
.CreateProcessInfo
.hProcess
,
682 de
->u
.CreateProcessInfo
.lpImageName
);
684 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
685 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
686 de
->dwProcessId
, de
->dwThreadId
,
687 buffer
, de
->u
.CreateProcessInfo
.lpImageName
,
688 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
689 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
690 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
692 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
) {
693 if (DEBUG_CurrProcess
->handle
) {
694 DEBUG_Printf(DBG_CHN_ERR
, "Skipping already defined process\n");
697 DEBUG_CurrProcess
->handle
= de
->u
.CreateProcessInfo
.hProcess
;
698 if (DEBUG_CurrProcess
->imageName
== NULL
)
699 DEBUG_CurrProcess
->imageName
= DBG_strdup(buffer
[0] ? buffer
: "<Debugged Process>");
702 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
703 de
->u
.CreateProcessInfo
.hProcess
,
704 buffer
[0] ? buffer
: "<Debugged Process>");
705 if (DEBUG_CurrProcess
== NULL
) {
706 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
711 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
712 de
->dwProcessId
, de
->dwThreadId
,
713 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
715 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
717 de
->u
.CreateProcessInfo
.hThread
,
718 de
->u
.CreateProcessInfo
.lpStartAddress
,
719 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
720 if (!DEBUG_CurrThread
) {
721 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
725 DEBUG_InitCurrProcess();
726 DEBUG_InitCurrThread();
728 /* module is either PE, NE or ELF module (for WineLib), but all
729 * are loaded with wine, so load its symbols, then the main module
733 char* ptr
= getenv("WINELOADER");
735 if (!ptr
|| DEBUG_ReadExecutableDbgInfo( ptr
) == DIL_ERROR
)
736 DEBUG_ReadExecutableDbgInfo( "wine" );
739 DEBUG_LoadModule32(DEBUG_CurrProcess
->imageName
, de
->u
.CreateProcessInfo
.hFile
,
740 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
744 case EXIT_THREAD_DEBUG_EVENT
:
745 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
746 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
748 if (DEBUG_CurrThread
== NULL
) {
749 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
752 /* FIXME: remove break point set on thread startup */
753 DEBUG_DelThread(DEBUG_CurrThread
);
756 case EXIT_PROCESS_DEBUG_EVENT
:
757 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
758 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
760 if (DEBUG_CurrProcess
== NULL
) {
761 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
765 DEBUG_SetBreakpoints(FALSE
);
766 /* kill last thread */
767 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
768 DEBUG_DelProcess(DEBUG_CurrProcess
);
770 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
773 case LOAD_DLL_DEBUG_EVENT
:
774 if (DEBUG_CurrThread
== NULL
) {
775 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
778 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
779 DEBUG_CurrThread
->process
->handle
,
780 de
->u
.LoadDll
.lpImageName
);
782 /* FIXME unicode: de->u.LoadDll.fUnicode */
783 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
784 de
->dwProcessId
, de
->dwThreadId
,
785 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
786 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
787 de
->u
.LoadDll
.nDebugInfoSize
);
789 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
790 DEBUG_CheckDelayedBP();
791 if (DBG_IVAR(BreakOnDllLoad
)) {
792 DEBUG_Printf(DBG_CHN_MESG
, "Stopping on DLL %s loading at %08lx\n",
793 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
);
798 case UNLOAD_DLL_DEBUG_EVENT
:
799 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
800 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
803 case OUTPUT_DEBUG_STRING_EVENT
:
804 if (DEBUG_CurrThread
== NULL
) {
805 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
809 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
810 DEBUG_CurrThread
->process
->handle
,
811 de
->u
.DebugString
.lpDebugStringData
);
813 /* FIXME unicode de->u.DebugString.fUnicode ? */
814 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
815 de
->dwProcessId
, de
->dwThreadId
, buffer
);
819 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
820 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
821 de
->u
.RipInfo
.dwType
);
825 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
826 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
829 } __EXCEPT(wine_dbg
) {
830 DEBUG_ExitMode
= EXIT_CONTINUE
;
835 static DWORD
DEBUG_MainLoop(void)
839 DEBUG_Printf(DBG_CHN_MESG
, " on pid %lx\n", DEBUG_CurrPid
);
841 while (DEBUG_ExitMode
== EXIT_CONTINUE
)
843 /* wait until we get at least one loaded process */
844 while (!DEBUG_ProcessList
)
847 if (DEBUG_ExitMode
== EXIT_CONTINUE
|| DEBUG_ExitMode
== EXIT_QUIT
) break;
849 if (DEBUG_ExitMode
!= EXIT_CONTINUE
) break;
851 while ((DEBUG_ExitMode
== EXIT_CONTINUE
|| DEBUG_ExitMode
== EXIT_PASS
) &&
853 WaitForDebugEvent(&de
, INFINITE
))
855 DEBUG_HandleDebugEvent(&de
);
856 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
,
857 (DEBUG_ExitMode
== EXIT_PASS
) ? DBG_EXCEPTION_NOT_HANDLED
: DBG_CONTINUE
);
859 if (DEBUG_ExitMode
== EXIT_DETACH
&& DEBUG_Detach())
861 /* DEBUG_ExitMode = EXIT_CONTINUE; */
862 /* FIXME: as we don't have a simple way to zero out the process symbol table
863 * we simply quit the debugger on detach...
865 DEBUG_ExitMode
= EXIT_QUIT
;
869 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
874 static DWORD
DEBUG_AutoMode(void)
878 DEBUG_Printf(DBG_CHN_MESG
, " on pid %lx\n", DEBUG_CurrPid
);
880 while ((DEBUG_ExitMode
== EXIT_CONTINUE
|| DEBUG_ExitMode
== EXIT_PASS
) &&
882 WaitForDebugEvent(&de
, INFINITE
))
884 DEBUG_HandleDebugEvent(&de
);
885 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
,
886 (DEBUG_ExitMode
== EXIT_PASS
) ? DBG_EXCEPTION_NOT_HANDLED
: DBG_CONTINUE
);
888 /* print some extra information */
889 DEBUG_Printf(DBG_CHN_MESG
, "Modules:\n");
891 DEBUG_Printf(DBG_CHN_MESG
, "Threads:\n");
894 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
898 static BOOL
DEBUG_Start(LPSTR cmdLine
)
900 PROCESS_INFORMATION info
;
901 STARTUPINFOA startup
;
903 memset(&startup
, 0, sizeof(startup
));
904 startup
.cb
= sizeof(startup
);
905 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
906 startup
.wShowWindow
= SW_SHOWNORMAL
;
908 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
909 FALSE
, DEBUG_PROCESS
|DETACHED_PROCESS
, NULL
, NULL
, &startup
, &info
)) {
910 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
913 DEBUG_CurrPid
= info
.dwProcessId
;
914 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0, NULL
))) return FALSE
;
919 void DEBUG_Run(const char* args
)
921 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
922 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
925 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
927 if (!DEBUG_LastCmdLine
) {
928 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
931 DEBUG_Start(DEBUG_LastCmdLine
);
935 static BOOL WINAPI
DEBUG_CtrlCHandler(DWORD dwCtrlType
)
937 if (dwCtrlType
== CTRL_C_EVENT
)
939 DEBUG_Printf(DBG_CHN_MESG
, "Ctrl-C: stopping debuggee\n");
940 /* FIXME: since we likely have a single process, signal the first process
943 return DEBUG_ProcessList
&& DebugBreakProcess(DEBUG_ProcessList
->handle
);
948 static void DEBUG_InitConsole(void)
954 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
955 * stdout and stderr streams
957 if (DBG_IVAR(UseXTerm
))
963 /* this would be nicer for output */
966 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
), c
);
968 /* sets the console's window width accordingly */
973 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE
), TRUE
, &sr
);
975 /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */
976 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), &mode
))
977 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), mode
| WINE_ENABLE_LINE_INPUT_EMACS
);
979 /* set our control-C handler */
980 SetConsoleCtrlHandler(DEBUG_CtrlCHandler
, TRUE
);
983 int main(int argc
, char** argv
)
987 /* Initialize the type handling stuff. */
989 DEBUG_InitCVDataTypes();
991 /* Initialize internal vars (types must have been initialized before) */
992 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
995 while (argc
> 1 && argv
[1][0] == '-')
997 if (!strcmp( argv
[1], "--auto" ))
1000 /* force some internal variables */
1001 DBG_IVAR(UseXTerm
) = 0;
1002 DBG_IVAR(BreakOnDllLoad
) = 0;
1003 DBG_IVAR(ConChannelMask
) = 0;
1004 DBG_IVAR(StdChannelMask
) = DBG_CHN_MESG
;
1009 if (!strcmp( argv
[1], "--debugmsg" ) && argv
[2])
1011 wine_dbg_parse_options( argv
[2] );
1016 DEBUG_Printf(DBG_CHN_MESG
, "Usage: winedbg [--debugmsg dbgoptions] [--auto] cmdline\n" );
1020 DEBUG_InitConsole();
1022 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg starting... ");
1028 if ((pid
= atoi(argv
[1])) != 0 && (hEvent
= (HANDLE
)atoi(argv
[2])) != 0) {
1029 if (!DEBUG_Attach(pid
, TRUE
)) {
1030 /* don't care about result */
1034 if (!SetEvent(hEvent
)) {
1035 DEBUG_Printf(DBG_CHN_ERR
, "Invalid event handle: %p\n", hEvent
);
1038 CloseHandle(hEvent
);
1039 DEBUG_CurrPid
= pid
;
1043 if (DEBUG_CurrPid
== 0 && argc
> 1) {
1047 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
1050 for (i
= 1; i
< argc
; i
++) {
1051 len
+= strlen(argv
[i
]) + 1;
1052 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
1053 strcat(cmdLine
, argv
[i
]);
1054 cmdLine
[len
- 2] = ' ';
1055 cmdLine
[len
- 1] = '\0';
1058 if (!DEBUG_Start(cmdLine
)) {
1059 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
1062 DBG_free(DEBUG_LastCmdLine
);
1063 DEBUG_LastCmdLine
= cmdLine
;
1068 retv
= DEBUG_AutoMode();
1069 /* don't save modified variables in auto mode */
1073 retv
= DEBUG_MainLoop();
1074 /* saves modified variables */
1075 DEBUG_IntVarsRW(FALSE
);
1082 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");