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
;
44 static char* DEBUG_LastCmdLine
= NULL
;
46 static DBG_PROCESS
* DEBUG_ProcessList
= NULL
;
47 static enum {none_mode
= 0, winedbg_mode
, automatic_mode
, gdb_mode
} local_mode
;
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 DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
149 for (p
= DEBUG_ProcessList
; p
; p
= p
->next
)
150 if (p
->pid
== pid
) break;
154 DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
, const char* imageName
)
158 if ((p
= DEBUG_GetProcess(pid
)))
162 DEBUG_Printf(DBG_CHN_ERR
, "Process (%lu) is already defined\n", pid
);
167 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
172 if (!(p
= DBG_alloc(sizeof(DBG_PROCESS
)))) return NULL
;
175 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
178 p
->continue_on_first_exception
= FALSE
;
183 p
->delayed_bp
= NULL
;
184 p
->num_delayed_bp
= 0;
186 p
->next
= DEBUG_ProcessList
;
188 if (DEBUG_ProcessList
) DEBUG_ProcessList
->prev
= p
;
189 DEBUG_ProcessList
= p
;
193 void DEBUG_DelProcess(DBG_PROCESS
* p
)
197 while (p
->threads
) DEBUG_DelThread(p
->threads
);
199 for (i
= 0; i
< p
->num_delayed_bp
; i
++)
200 if (p
->delayed_bp
[i
].is_symbol
)
201 DBG_free(p
->delayed_bp
[i
].u
.symbol
.name
);
203 DBG_free(p
->delayed_bp
);
204 if (p
->prev
) p
->prev
->next
= p
->next
;
205 if (p
->next
) p
->next
->prev
= p
->prev
;
206 if (p
== DEBUG_ProcessList
) DEBUG_ProcessList
= p
->next
;
207 if (p
== DEBUG_CurrProcess
) DEBUG_CurrProcess
= NULL
;
208 DBG_free((char*)p
->imageName
);
212 static void DEBUG_InitCurrProcess(void)
216 BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
220 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
223 BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
229 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
232 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
238 DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
243 for (t
= p
->threads
; t
; t
= t
->next
)
244 if (t
->tid
== tid
) break;
248 DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
249 HANDLE h
, LPVOID start
, LPVOID teb
)
251 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
260 t
->wait_for_first_exception
= 0;
261 t
->exec_mode
= EXEC_CONT
;
264 sprintf(t
->name
, "%08lx", tid
);
267 t
->next
= p
->threads
;
269 if (p
->threads
) p
->threads
->prev
= t
;
275 static void DEBUG_InitCurrThread(void)
277 if (DEBUG_CurrThread
->start
) {
278 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
279 DBG_IVAR(BreakAllThreadsStartup
)) {
282 DEBUG_SetBreakpoints(FALSE
);
284 value
.cookie
= DV_TARGET
;
286 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
287 DEBUG_AddBreakpointFromValue(&value
);
288 DEBUG_SetBreakpoints(TRUE
);
291 DEBUG_CurrThread
->wait_for_first_exception
= 1;
295 void DEBUG_DelThread(DBG_THREAD
* t
)
297 if (t
->prev
) t
->prev
->next
= t
->next
;
298 if (t
->next
) t
->next
->prev
= t
->prev
;
299 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
300 t
->process
->num_threads
--;
301 if (t
== DEBUG_CurrThread
) DEBUG_CurrThread
= NULL
;
305 BOOL
DEBUG_Attach(DWORD pid
, BOOL cofe
)
307 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0, NULL
))) return FALSE
;
309 if (!DebugActiveProcess(pid
)) {
310 DEBUG_Printf(DBG_CHN_MESG
, "Can't attach process %lx: error %ld\n", pid
, GetLastError());
311 DEBUG_DelProcess(DEBUG_CurrProcess
);
312 DEBUG_CurrProcess
= NULL
;
315 DEBUG_CurrProcess
->continue_on_first_exception
= cofe
;
319 BOOL
DEBUG_Detach(void)
321 /* remove all set breakpoints in debuggee code */
322 DEBUG_SetBreakpoints(FALSE
);
323 /* needed for single stepping (ugly).
324 * should this be handled inside the server ??? */
326 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
328 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
329 DebugActiveProcessStop(DEBUG_CurrProcess
->pid
);
330 DEBUG_DelProcess(DEBUG_CurrProcess
);
331 DEBUG_CurrProcess
= NULL
;
332 /* FIXME: should zero out the symbol table too */
336 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
341 DEBUG_GetCurrentAddress(&addr
);
342 DEBUG_SuspendExecution();
347 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx)", addr
.off
);
349 switch(DEBUG_GetSelectorType(addr
.seg
))
352 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (%04lx:%08lx)", addr
.seg
, addr
.off
);
355 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04lx:%04lx)", addr
.seg
, addr
.off
);
358 DEBUG_Printf(DBG_CHN_MESG
, " in vm86 code (%04lx:%04lx)", addr
.seg
, addr
.off
);
361 DEBUG_Printf(DBG_CHN_MESG
, " bad CS (%lx)", addr
.seg
);
364 DEBUG_Printf(DBG_CHN_MESG
, ".\n");
367 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
369 if (!force
&& is_debug
&&
370 DEBUG_ShouldContinue(&addr
, code
,
371 &DEBUG_CurrThread
->exec_count
))
374 if ((newmode
= DEBUG_GetSelectorType(addr
.seg
)) == MODE_INVALID
) newmode
= MODE_32
;
375 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
377 static const char * const names
[] = { "???", "16-bit", "32-bit", "vm86" };
378 DEBUG_Printf(DBG_CHN_MESG
,"In %s mode.\n", names
[newmode
] );
379 DEBUG_CurrThread
->dbg_mode
= newmode
;
384 if (is_debug
|| force
) {
386 * Do a quiet backtrace so that we have an idea of what the situation
387 * is WRT the source files.
389 DEBUG_BackTrace(DEBUG_CurrTid
, FALSE
);
391 /* This is a real crash, dump some info */
392 DEBUG_InfoRegisters(&DEBUG_context
);
395 if (DEBUG_CurrThread
->dbg_mode
== MODE_16
) {
396 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
397 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
398 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
400 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
402 DEBUG_BackTrace(DEBUG_CurrTid
, TRUE
);
406 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_OVER
) ||
407 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_INSTR
)) {
411 /* Show where we crashed */
413 DEBUG_DisassembleInstruction(&addr
);
415 /* resets list internal arguments so we can look at source code when needed */
416 DEBUG_FindNearestSymbol(&addr
, TRUE
, NULL
, 0, &list
);
417 if (list
.sourcefile
) DEBUG_List(&list
, NULL
, 0);
422 static void DEBUG_ExceptionEpilog(void)
424 DEBUG_RestartExecution(DEBUG_CurrThread
->exec_count
);
426 * This will have gotten absorbed into the breakpoint info
427 * if it was used. Otherwise it would have been ignored.
428 * In any case, we don't mess with it any more.
430 if (DEBUG_CurrThread
->exec_mode
== EXEC_CONT
)
431 DEBUG_CurrThread
->exec_count
= 0;
434 static BOOL
DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
436 BOOL is_debug
= FALSE
;
437 THREADNAME_INFO
*pThreadName
;
440 assert(DEBUG_CurrThread
);
442 switch (rec
->ExceptionCode
)
444 case EXCEPTION_BREAKPOINT
:
445 case EXCEPTION_SINGLE_STEP
:
448 case EXCEPTION_NAME_THREAD
:
449 pThreadName
= (THREADNAME_INFO
*)(rec
->ExceptionInformation
);
450 if (pThreadName
->dwThreadID
== -1)
451 pThread
= DEBUG_CurrThread
;
453 pThread
= DEBUG_GetThread(DEBUG_CurrProcess
, pThreadName
->dwThreadID
);
455 if (ReadProcessMemory(DEBUG_CurrThread
->process
->handle
, pThreadName
->szName
,
456 pThread
->name
, 9, NULL
))
457 DEBUG_Printf (DBG_CHN_MESG
,
458 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
459 pThread
->tid
, pThread
->name
);
463 if (first_chance
&& !is_debug
&& !force
&& !DBG_IVAR(BreakOnFirstChance
))
465 /* pass exception to program except for debug exceptions */
471 /* print some infos */
472 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
473 first_chance
? "First chance exception" : "Unhandled exception");
474 switch (rec
->ExceptionCode
)
476 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
477 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
479 case EXCEPTION_INT_OVERFLOW
:
480 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
482 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
483 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
485 case EXCEPTION_ILLEGAL_INSTRUCTION
:
486 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
488 case EXCEPTION_STACK_OVERFLOW
:
489 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
491 case EXCEPTION_PRIV_INSTRUCTION
:
492 DEBUG_Printf(DBG_CHN_MESG
, "privileged instruction");
494 case EXCEPTION_ACCESS_VIOLATION
:
495 if (rec
->NumberParameters
== 2)
496 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
497 rec
->ExceptionInformation
[0] ? "write" : "read",
498 rec
->ExceptionInformation
[1]);
500 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
502 case EXCEPTION_DATATYPE_MISALIGNMENT
:
503 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
506 DEBUG_Printf(DBG_CHN_MESG
, "^C");
509 DEBUG_Printf(DBG_CHN_MESG
, "^C");
511 case EXCEPTION_CRITICAL_SECTION_WAIT
:
516 addr
.off
= rec
->ExceptionInformation
[0];
518 DEBUG_Printf(DBG_CHN_MESG
, "wait failed on critical section ");
519 DEBUG_PrintAddress(&addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
521 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
523 DEBUG_Printf(DBG_CHN_MESG
, "\n");
527 case EXCEPTION_WINE_STUB
:
529 char dll
[32], name
[64];
530 DEBUG_ProcessGetString( dll
, sizeof(dll
), DEBUG_CurrThread
->process
->handle
,
531 (char *)rec
->ExceptionInformation
[0] );
532 DEBUG_ProcessGetString( name
, sizeof(name
), DEBUG_CurrThread
->process
->handle
,
533 (char *)rec
->ExceptionInformation
[1] );
534 DEBUG_Printf(DBG_CHN_MESG
, "unimplemented function %s.%s called", dll
, name
);
537 case EXCEPTION_VM86_INTx
:
538 DEBUG_Printf(DBG_CHN_MESG
, "interrupt %02lx in vm86 mode",
539 rec
->ExceptionInformation
[0]);
541 case EXCEPTION_VM86_STI
:
542 DEBUG_Printf(DBG_CHN_MESG
, "sti in vm86 mode");
544 case EXCEPTION_VM86_PICRETURN
:
545 DEBUG_Printf(DBG_CHN_MESG
, "PIC return in vm86 mode");
548 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
553 if (local_mode
== automatic_mode
)
555 DEBUG_ExceptionProlog(is_debug
, FALSE
, rec
->ExceptionCode
);
556 DEBUG_ExceptionEpilog();
557 return TRUE
; /* terminate execution */
560 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
))
562 DEBUG_InteractiveP
= TRUE
;
565 DEBUG_ExceptionEpilog();
570 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
)
575 DEBUG_CurrPid
= de
->dwProcessId
;
576 DEBUG_CurrTid
= de
->dwThreadId
;
578 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
579 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
581 DEBUG_CurrThread
= NULL
;
583 switch (de
->dwDebugEventCode
)
585 case EXCEPTION_DEBUG_EVENT
:
586 if (!DEBUG_CurrThread
)
588 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
589 de
->dwProcessId
, de
->dwThreadId
);
593 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
594 de
->dwProcessId
, de
->dwThreadId
,
595 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
597 if (DEBUG_CurrProcess
->continue_on_first_exception
)
599 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
600 if (!DBG_IVAR(BreakOnAttach
)) break;
603 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
605 #ifdef CONTEXT_SEGMENTS
608 #ifdef CONTEXT_DEBUG_REGISTERS
609 | CONTEXT_DEBUG_REGISTERS
613 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
615 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
619 ret
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
620 de
->u
.Exception
.dwFirstChance
,
621 DEBUG_CurrThread
->wait_for_first_exception
);
622 if (DEBUG_CurrThread
)
624 DEBUG_CurrThread
->wait_for_first_exception
= 0;
625 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
629 case CREATE_THREAD_DEBUG_EVENT
:
630 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
631 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
633 if (DEBUG_CurrProcess
== NULL
)
635 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
638 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
)
640 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
644 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
646 de
->u
.CreateThread
.hThread
,
647 de
->u
.CreateThread
.lpStartAddress
,
648 de
->u
.CreateThread
.lpThreadLocalBase
);
649 if (!DEBUG_CurrThread
)
651 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
654 DEBUG_InitCurrThread();
657 case CREATE_PROCESS_DEBUG_EVENT
:
658 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
659 de
->u
.CreateProcessInfo
.hProcess
,
660 de
->u
.CreateProcessInfo
.lpImageName
);
662 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
663 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
664 de
->dwProcessId
, de
->dwThreadId
,
665 buffer
, de
->u
.CreateProcessInfo
.lpImageName
,
666 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
667 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
668 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
670 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
671 de
->u
.CreateProcessInfo
.hProcess
,
672 buffer
[0] ? buffer
: "<Debugged Process>");
673 if (DEBUG_CurrProcess
== NULL
)
675 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create process\n");
679 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
680 de
->dwProcessId
, de
->dwThreadId
,
681 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
683 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
685 de
->u
.CreateProcessInfo
.hThread
,
686 de
->u
.CreateProcessInfo
.lpStartAddress
,
687 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
688 if (!DEBUG_CurrThread
)
690 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
694 DEBUG_InitCurrProcess();
695 DEBUG_InitCurrThread();
697 /* module is either PE, NE or ELF module (for WineLib), but all
698 * are loaded with wine, so load its symbols, then the main module
702 char* ptr
= getenv("WINELOADER");
704 if (!ptr
|| DEBUG_ReadExecutableDbgInfo( ptr
) == DIL_ERROR
)
705 DEBUG_ReadExecutableDbgInfo( "wine" );
708 DEBUG_LoadModule32(DEBUG_CurrProcess
->imageName
, de
->u
.CreateProcessInfo
.hFile
,
709 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
713 case EXIT_THREAD_DEBUG_EVENT
:
714 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
715 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
717 if (DEBUG_CurrThread
== NULL
)
719 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
722 /* FIXME: remove break point set on thread startup */
723 DEBUG_DelThread(DEBUG_CurrThread
);
726 case EXIT_PROCESS_DEBUG_EVENT
:
727 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
728 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
730 if (DEBUG_CurrProcess
== NULL
)
732 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
736 DEBUG_SetBreakpoints(FALSE
);
737 /* kill last thread */
738 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
739 DEBUG_DelProcess(DEBUG_CurrProcess
);
741 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
744 case LOAD_DLL_DEBUG_EVENT
:
745 if (DEBUG_CurrThread
== NULL
)
747 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
750 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
751 DEBUG_CurrThread
->process
->handle
,
752 de
->u
.LoadDll
.lpImageName
);
754 /* FIXME unicode: de->u.LoadDll.fUnicode */
755 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
756 de
->dwProcessId
, de
->dwThreadId
,
757 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
758 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
759 de
->u
.LoadDll
.nDebugInfoSize
);
761 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
762 DEBUG_CheckDelayedBP();
763 if (DBG_IVAR(BreakOnDllLoad
))
765 DEBUG_Printf(DBG_CHN_MESG
, "Stopping on DLL %s loading at %08lx\n",
766 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
);
771 case UNLOAD_DLL_DEBUG_EVENT
:
772 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
773 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
776 case OUTPUT_DEBUG_STRING_EVENT
:
777 if (DEBUG_CurrThread
== NULL
)
779 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
783 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
784 DEBUG_CurrThread
->process
->handle
,
785 de
->u
.DebugString
.lpDebugStringData
);
787 /* FIXME unicode de->u.DebugString.fUnicode ? */
788 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
789 de
->dwProcessId
, de
->dwThreadId
, buffer
);
793 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
794 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
795 de
->u
.RipInfo
.dwType
);
799 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
800 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
806 static void DEBUG_ResumeDebuggee(DWORD cont
)
808 DEBUG_ExceptionEpilog();
810 DEBUG_Printf(DBG_CHN_TRACE
,
811 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
813 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
817 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
819 DEBUG_InteractiveP
= FALSE
;
820 if (DEBUG_CurrThread
)
822 if (!SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
823 DEBUG_Printf(DBG_CHN_MESG
, "Cannot set ctx on %lu\n", DEBUG_CurrTid
);
824 DEBUG_CurrThread
->wait_for_first_exception
= 0;
826 if (!ContinueDebugEvent(DEBUG_CurrPid
, DEBUG_CurrTid
, cont
))
827 DEBUG_Printf(DBG_CHN_MESG
, "Cannot continue on %lu (%lu)\n",
828 DEBUG_CurrTid
, cont
);
831 void DEBUG_WaitNextException(DWORD cont
, int count
, int mode
)
835 if (cont
== DBG_CONTINUE
)
837 DEBUG_CurrThread
->exec_count
= count
;
838 DEBUG_CurrThread
->exec_mode
= mode
;
840 DEBUG_ResumeDebuggee(cont
);
842 while (DEBUG_CurrProcess
&& WaitForDebugEvent(&de
, INFINITE
))
844 if (DEBUG_HandleDebugEvent(&de
)) break;
845 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
847 if (!DEBUG_CurrProcess
) return;
848 DEBUG_InteractiveP
= TRUE
;
850 DEBUG_Printf(DBG_CHN_TRACE
,
851 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
853 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
857 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
861 static DWORD
DEBUG_MainLoop(void)
865 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg starting on pid %lx\n", DEBUG_CurrPid
);
867 /* wait for first exception */
868 while (WaitForDebugEvent(&de
, INFINITE
))
870 if (DEBUG_HandleDebugEvent(&de
)) break;
871 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
873 if (local_mode
== automatic_mode
)
875 /* print some extra information */
876 DEBUG_Printf(DBG_CHN_MESG
, "Modules:\n");
878 DEBUG_Printf(DBG_CHN_MESG
, "Threads:\n");
883 DEBUG_InteractiveP
= TRUE
;
886 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
891 static BOOL
DEBUG_Start(LPSTR cmdLine
)
893 PROCESS_INFORMATION info
;
894 STARTUPINFOA startup
;
896 memset(&startup
, 0, sizeof(startup
));
897 startup
.cb
= sizeof(startup
);
898 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
899 startup
.wShowWindow
= SW_SHOWNORMAL
;
901 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
904 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
905 FALSE
, DEBUG_PROCESS
|DEBUG_ONLY_THIS_PROCESS
|CREATE_NEW_CONSOLE
,
906 NULL
, NULL
, &startup
, &info
))
908 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
911 DEBUG_CurrPid
= info
.dwProcessId
;
912 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0, NULL
))) return FALSE
;
917 void DEBUG_Run(const char* args
)
919 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
920 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
923 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
925 if (!DEBUG_LastCmdLine
) {
926 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
929 DEBUG_Start(DEBUG_LastCmdLine
);
933 static BOOL WINAPI
DEBUG_CtrlCHandler(DWORD dwCtrlType
)
935 if (dwCtrlType
== CTRL_C_EVENT
)
937 DEBUG_Printf(DBG_CHN_MESG
, "Ctrl-C: stopping debuggee\n");
938 /* FIXME: since we likely have a single process, signal the first process
941 return DEBUG_ProcessList
&& DebugBreakProcess(DEBUG_ProcessList
->handle
);
946 static void DEBUG_InitConsole(void)
952 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
953 * stdout and stderr streams
955 if (DBG_IVAR(UseXTerm
))
961 /* this would be nicer for output */
964 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
), c
);
966 /* sets the console's window width accordingly */
971 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE
), TRUE
, &sr
);
973 /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */
974 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), &mode
))
975 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), mode
| WINE_ENABLE_LINE_INPUT_EMACS
);
977 /* set our control-C handler */
978 SetConsoleCtrlHandler(DEBUG_CtrlCHandler
, TRUE
);
981 static int DEBUG_Usage(void)
983 DEBUG_Printf(DBG_CHN_MESG
, "Usage: winedbg [--debugmsg dbgoptions] [--auto] [--gdb] cmdline\n" );
987 int main(int argc
, char** argv
)
990 unsigned gdb_flags
= 0;
992 /* Initialize the type handling stuff. */
994 DEBUG_InitCVDataTypes();
996 /* Initialize internal vars (types must have been initialized before) */
997 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
1000 while (argc
> 1 && argv
[1][0] == '-')
1002 if (!strcmp( argv
[1], "--auto" ))
1004 if (local_mode
!= none_mode
) return DEBUG_Usage();
1005 local_mode
= automatic_mode
;
1006 /* force some internal variables */
1007 DBG_IVAR(UseXTerm
) = 0;
1008 DBG_IVAR(BreakOnDllLoad
) = 0;
1009 DBG_IVAR(ConChannelMask
) = 0;
1010 DBG_IVAR(StdChannelMask
) = DBG_CHN_MESG
;
1014 if (!strcmp( argv
[1], "--gdb" ))
1016 if (local_mode
!= none_mode
) return DEBUG_Usage();
1017 local_mode
= gdb_mode
;
1021 if (strcmp( argv
[1], "--no-start") == 0 && local_mode
== gdb_mode
)
1024 argc
--; argv
++; /* as we don't use argv[0] */
1027 if (strcmp(argv
[1], "--with-xterm") == 0 && local_mode
== gdb_mode
)
1030 argc
--; argv
++; /* as we don't use argv[0] */
1033 if (!strcmp( argv
[1], "--debugmsg" ) && argv
[2])
1035 wine_dbg_parse_options( argv
[2] );
1040 return DEBUG_Usage();
1043 if (local_mode
== none_mode
) local_mode
= winedbg_mode
;
1045 /* try the from <myself> pid */
1046 if (DEBUG_CurrPid
== 0 && argc
== 2)
1050 DEBUG_CurrPid
= strtol(argv
[1], &ptr
, 10);
1051 if (DEBUG_CurrPid
== 0 || ptr
== NULL
||
1052 !DEBUG_Attach(DEBUG_CurrPid
, local_mode
!= gdb_mode
))
1056 /* try the from <myself> pid evt (Win32 JIT debugger) */
1057 if (DEBUG_CurrPid
== 0 && argc
== 3)
1063 if ((pid
= strtol(argv
[1], &ptr
, 10)) != 0 && ptr
!= NULL
&&
1064 (hEvent
= (HANDLE
)strtol(argv
[2], &ptr
, 10)) != 0 && ptr
!= NULL
)
1066 if (!DEBUG_Attach(pid
, TRUE
))
1068 /* don't care about result */
1072 if (!SetEvent(hEvent
))
1074 DEBUG_Printf(DBG_CHN_ERR
, "Invalid event handle: %p\n", hEvent
);
1077 CloseHandle(hEvent
);
1078 DEBUG_CurrPid
= pid
;
1082 if (DEBUG_CurrPid
== 0 && argc
> 1)
1087 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
1090 for (i
= 1; i
< argc
; i
++)
1092 len
+= strlen(argv
[i
]) + 1;
1093 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
1094 strcat(cmdLine
, argv
[i
]);
1095 cmdLine
[len
- 2] = ' ';
1096 cmdLine
[len
- 1] = '\0';
1099 if (!DEBUG_Start(cmdLine
))
1101 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
1104 DBG_free(DEBUG_LastCmdLine
);
1105 DEBUG_LastCmdLine
= cmdLine
;
1107 /* don't save local vars in gdb mode */
1108 if (local_mode
== gdb_mode
&& DEBUG_CurrPid
)
1109 return DEBUG_GdbRemote(gdb_flags
);
1111 DEBUG_InitConsole();
1113 retv
= DEBUG_MainLoop();
1114 /* don't save modified variables in auto mode */
1115 if (local_mode
!= automatic_mode
) DEBUG_IntVarsRW(FALSE
);
1121 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");