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 "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 static BOOL DEBUG_InException
= FALSE
;
45 static char* DEBUG_LastCmdLine
= NULL
;
47 static DBG_PROCESS
* DEBUG_ProcessList
= NULL
;
48 static enum {none_mode
= 0, winedbg_mode
, automatic_mode
, gdb_mode
} local_mode
;
50 DBG_INTVAR DEBUG_IntVars
[DBG_IV_LAST
];
52 void DEBUG_OutputA(int chn
, const char* buffer
, int len
)
54 if (DBG_IVAR(ConChannelMask
) & chn
)
55 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
56 if (DBG_IVAR(StdChannelMask
) & chn
)
57 fwrite(buffer
, len
, 1, stderr
);
60 void DEBUG_OutputW(int chn
, const WCHAR
* buffer
, int len
)
65 /* do a serious Unicode to ANSI conversion
66 FIXME: should CP_ACP be GetConsoleCP()? */
67 newlen
= WideCharToMultiByte(CP_ACP
, 0, buffer
, len
, NULL
, 0, NULL
, NULL
);
70 ansi
= (char*)DBG_alloc(newlen
);
73 WideCharToMultiByte(CP_ACP
, 0, buffer
, len
, ansi
, newlen
, NULL
, NULL
);
77 /* fall back to a simple Unicode to ANSI conversion in case WC2MB failed */
80 ansi
= DBG_alloc(len
);
84 for(i
= 0; i
< len
; i
++)
86 ansi
[i
] = (char)buffer
[i
];
91 /* else we are having REALLY bad luck today */
96 DEBUG_OutputA(chn
, ansi
, newlen
);
101 int DEBUG_Printf(int chn
, const char* format
, ...)
103 static char buf
[4*1024];
107 va_start(valist
, format
);
108 len
= vsnprintf(buf
, sizeof(buf
), format
, valist
);
111 if (len
<= -1 || len
>= sizeof(buf
)) {
112 len
= sizeof(buf
) - 1;
114 buf
[len
- 1] = buf
[len
- 2] = buf
[len
- 3] = '.';
116 DEBUG_OutputA(chn
, buf
, len
);
120 static BOOL
DEBUG_IntVarsRW(int read
)
123 DWORD type
= REG_DWORD
;
125 DWORD count
= sizeof(val
);
127 DBG_INTVAR
* div
= DEBUG_IntVars
;
130 /* initializes internal vars table */
131 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
132 div->val = _val; div->name = #_var; div->pval = _ref; \
133 div->type = DEBUG_GetBasicType(_typ); div++;
138 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
139 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
141 fprintf(stderr
, "Cannot create WineDbg key in registry\n");
145 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
147 if (!DEBUG_IntVars
[i
].pval
) {
148 if (!RegQueryValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
149 &type
, (LPSTR
)&val
, &count
))
150 DEBUG_IntVars
[i
].val
= val
;
151 DEBUG_IntVars
[i
].pval
= &DEBUG_IntVars
[i
].val
;
153 *DEBUG_IntVars
[i
].pval
= 0;
156 /* FIXME: type should be infered from basic type -if any- of intvar */
157 if (DEBUG_IntVars
[i
].pval
== &DEBUG_IntVars
[i
].val
)
158 RegSetValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
159 type
, (LPCVOID
)DEBUG_IntVars
[i
].pval
, count
);
166 DBG_INTVAR
* DEBUG_GetIntVar(const char* name
)
170 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
171 if (!strcmp(DEBUG_IntVars
[i
].name
, name
))
172 return &DEBUG_IntVars
[i
];
177 DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
181 for (p
= DEBUG_ProcessList
; p
; p
= p
->next
)
182 if (p
->pid
== pid
) break;
186 DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
, const char* imageName
)
190 if ((p
= DEBUG_GetProcess(pid
)))
194 DEBUG_Printf(DBG_CHN_ERR
, "Process (%lu) is already defined\n", pid
);
199 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
204 if (!(p
= DBG_alloc(sizeof(DBG_PROCESS
)))) return NULL
;
207 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
210 p
->continue_on_first_exception
= FALSE
;
215 p
->delayed_bp
= NULL
;
216 p
->num_delayed_bp
= 0;
218 p
->next
= DEBUG_ProcessList
;
220 if (DEBUG_ProcessList
) DEBUG_ProcessList
->prev
= p
;
221 DEBUG_ProcessList
= p
;
225 void DEBUG_DelProcess(DBG_PROCESS
* p
)
229 while (p
->threads
) DEBUG_DelThread(p
->threads
);
231 for (i
= 0; i
< p
->num_delayed_bp
; i
++)
232 if (p
->delayed_bp
[i
].is_symbol
)
233 DBG_free(p
->delayed_bp
[i
].u
.symbol
.name
);
235 DBG_free(p
->delayed_bp
);
236 if (p
->prev
) p
->prev
->next
= p
->next
;
237 if (p
->next
) p
->next
->prev
= p
->prev
;
238 if (p
== DEBUG_ProcessList
) DEBUG_ProcessList
= p
->next
;
239 if (p
== DEBUG_CurrProcess
) DEBUG_CurrProcess
= NULL
;
240 DBG_free((char*)p
->imageName
);
244 static void DEBUG_InitCurrProcess(void)
248 BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
252 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
255 BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
261 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
264 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
270 DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
275 for (t
= p
->threads
; t
; t
= t
->next
)
276 if (t
->tid
== tid
) break;
280 DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
281 HANDLE h
, LPVOID start
, LPVOID teb
)
283 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
292 t
->wait_for_first_exception
= 0;
293 t
->exec_mode
= EXEC_CONT
;
296 snprintf(t
->name
, sizeof(t
->name
), "%08lx", tid
);
299 t
->next
= p
->threads
;
301 if (p
->threads
) p
->threads
->prev
= t
;
307 static void DEBUG_InitCurrThread(void)
309 if (DEBUG_CurrThread
->start
) {
310 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
311 DBG_IVAR(BreakAllThreadsStartup
)) {
314 DEBUG_SetBreakpoints(FALSE
);
316 value
.cookie
= DV_TARGET
;
318 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
319 DEBUG_AddBreakpointFromValue(&value
);
320 DEBUG_SetBreakpoints(TRUE
);
323 DEBUG_CurrThread
->wait_for_first_exception
= 1;
327 void DEBUG_DelThread(DBG_THREAD
* t
)
329 if (t
->prev
) t
->prev
->next
= t
->next
;
330 if (t
->next
) t
->next
->prev
= t
->prev
;
331 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
332 t
->process
->num_threads
--;
333 if (t
== DEBUG_CurrThread
) DEBUG_CurrThread
= NULL
;
337 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
);
339 /******************************************************************
342 * Sets the debuggee to <pid>
343 * cofe instructs winedbg what to do when first exception is received
344 * (break=FALSE, continue=TRUE)
345 * wfe is set to TRUE if DEBUG_Attach should also proceed with all debug events
346 * until the first exception is received (aka: attach to an already running process)
348 BOOL
DEBUG_Attach(DWORD pid
, BOOL cofe
, BOOL wfe
)
352 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0, NULL
))) return FALSE
;
354 if (!DebugActiveProcess(pid
)) {
355 DEBUG_Printf(DBG_CHN_MESG
, "Can't attach process %lx: error %ld\n", pid
, GetLastError());
356 DEBUG_DelProcess(DEBUG_CurrProcess
);
359 DEBUG_CurrProcess
->continue_on_first_exception
= cofe
;
361 if (wfe
) /* shall we proceed all debug events until we get an exception ? */
363 DEBUG_InteractiveP
= FALSE
;
364 while (DEBUG_CurrProcess
&& WaitForDebugEvent(&de
, INFINITE
))
366 if (DEBUG_HandleDebugEvent(&de
)) break;
368 if (DEBUG_CurrProcess
) DEBUG_InteractiveP
= TRUE
;
373 BOOL
DEBUG_Detach(void)
375 /* remove all set breakpoints in debuggee code */
376 DEBUG_SetBreakpoints(FALSE
);
377 /* needed for single stepping (ugly).
378 * should this be handled inside the server ??? */
380 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
382 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
383 DebugActiveProcessStop(DEBUG_CurrProcess
->pid
);
384 DEBUG_DelProcess(DEBUG_CurrProcess
);
385 /* FIXME: should zero out the symbol table too */
389 static BOOL
DEBUG_FetchContext(void)
391 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
393 #ifdef CONTEXT_SEGMENTS
396 #ifdef CONTEXT_DEBUG_REGISTERS
397 | CONTEXT_DEBUG_REGISTERS
400 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
402 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
408 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
413 DEBUG_InException
= TRUE
;
414 DEBUG_GetCurrentAddress(&addr
);
415 DEBUG_SuspendExecution();
420 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx)", addr
.off
);
422 switch(DEBUG_GetSelectorType(addr
.seg
))
425 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (%04lx:%08lx)", addr
.seg
, addr
.off
);
428 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04lx:%04lx)", addr
.seg
, addr
.off
);
431 DEBUG_Printf(DBG_CHN_MESG
, " in vm86 code (%04lx:%04lx)", addr
.seg
, addr
.off
);
434 DEBUG_Printf(DBG_CHN_MESG
, " bad CS (%lx)", addr
.seg
);
437 DEBUG_Printf(DBG_CHN_MESG
, ".\n");
440 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
442 if (!force
&& is_debug
&&
443 DEBUG_ShouldContinue(&addr
, code
,
444 &DEBUG_CurrThread
->exec_count
))
447 if ((newmode
= DEBUG_GetSelectorType(addr
.seg
)) == MODE_INVALID
) newmode
= MODE_32
;
448 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
450 static const char * const names
[] = { "???", "16-bit", "32-bit", "vm86" };
451 DEBUG_Printf(DBG_CHN_MESG
,"In %s mode.\n", names
[newmode
] );
452 DEBUG_CurrThread
->dbg_mode
= newmode
;
457 if (is_debug
|| force
) {
459 * Do a quiet backtrace so that we have an idea of what the situation
460 * is WRT the source files.
462 DEBUG_BackTrace(DEBUG_CurrTid
, FALSE
);
464 /* This is a real crash, dump some info */
465 DEBUG_InfoRegisters(&DEBUG_context
);
468 if (DEBUG_CurrThread
->dbg_mode
== MODE_16
) {
469 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
470 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
471 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
473 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
475 DEBUG_BackTrace(DEBUG_CurrTid
, TRUE
);
479 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_OVER
) ||
480 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_INSTR
)) {
484 /* Show where we crashed */
486 DEBUG_DisassembleInstruction(&addr
);
488 /* resets list internal arguments so we can look at source code when needed */
489 DEBUG_FindNearestSymbol(&addr
, TRUE
, NULL
, 0, &list
);
490 if (list
.sourcefile
) DEBUG_List(&list
, NULL
, 0);
495 static void DEBUG_ExceptionEpilog(void)
497 DEBUG_RestartExecution(DEBUG_CurrThread
->exec_count
);
499 * This will have gotten absorbed into the breakpoint info
500 * if it was used. Otherwise it would have been ignored.
501 * In any case, we don't mess with it any more.
503 if (DEBUG_CurrThread
->exec_mode
== EXEC_CONT
)
504 DEBUG_CurrThread
->exec_count
= 0;
505 DEBUG_InException
= FALSE
;
508 static DWORD
DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
510 BOOL is_debug
= FALSE
;
511 THREADNAME_INFO
*pThreadName
;
514 assert(DEBUG_CurrThread
);
516 switch (rec
->ExceptionCode
)
518 case EXCEPTION_BREAKPOINT
:
519 case EXCEPTION_SINGLE_STEP
:
522 case EXCEPTION_NAME_THREAD
:
523 pThreadName
= (THREADNAME_INFO
*)(rec
->ExceptionInformation
);
524 if (pThreadName
->dwThreadID
== -1)
525 pThread
= DEBUG_CurrThread
;
527 pThread
= DEBUG_GetThread(DEBUG_CurrProcess
, pThreadName
->dwThreadID
);
529 if (ReadProcessMemory(DEBUG_CurrThread
->process
->handle
, pThreadName
->szName
,
530 pThread
->name
, 9, NULL
))
531 DEBUG_Printf (DBG_CHN_MESG
,
532 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
533 pThread
->tid
, pThread
->name
);
537 if (first_chance
&& !is_debug
&& !force
&& !DBG_IVAR(BreakOnFirstChance
))
539 /* pass exception to program except for debug exceptions */
540 return DBG_EXCEPTION_NOT_HANDLED
;
545 /* print some infos */
546 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
547 first_chance
? "First chance exception" : "Unhandled exception");
548 switch (rec
->ExceptionCode
)
550 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
551 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
553 case EXCEPTION_INT_OVERFLOW
:
554 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
556 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
557 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
559 case EXCEPTION_ILLEGAL_INSTRUCTION
:
560 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
562 case EXCEPTION_STACK_OVERFLOW
:
563 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
565 case EXCEPTION_PRIV_INSTRUCTION
:
566 DEBUG_Printf(DBG_CHN_MESG
, "privileged instruction");
568 case EXCEPTION_ACCESS_VIOLATION
:
569 if (rec
->NumberParameters
== 2)
570 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
571 rec
->ExceptionInformation
[0] ? "write" : "read",
572 rec
->ExceptionInformation
[1]);
574 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
576 case EXCEPTION_DATATYPE_MISALIGNMENT
:
577 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
580 DEBUG_Printf(DBG_CHN_MESG
, "^C");
583 DEBUG_Printf(DBG_CHN_MESG
, "^C");
585 case STATUS_POSSIBLE_DEADLOCK
:
590 addr
.off
= rec
->ExceptionInformation
[0];
592 DEBUG_Printf(DBG_CHN_MESG
, "wait failed on critical section ");
593 DEBUG_PrintAddress(&addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
595 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
597 DEBUG_Printf(DBG_CHN_MESG
, "\n");
598 return DBG_EXCEPTION_NOT_HANDLED
;
601 case EXCEPTION_WINE_STUB
:
603 char dll
[32], name
[64];
604 DEBUG_ProcessGetString( dll
, sizeof(dll
), DEBUG_CurrThread
->process
->handle
,
605 (char *)rec
->ExceptionInformation
[0] );
606 DEBUG_ProcessGetString( name
, sizeof(name
), DEBUG_CurrThread
->process
->handle
,
607 (char *)rec
->ExceptionInformation
[1] );
608 DEBUG_Printf(DBG_CHN_MESG
, "unimplemented function %s.%s called", dll
, name
);
611 case EXCEPTION_WINE_ASSERTION
:
612 DEBUG_Printf(DBG_CHN_MESG
, "assertion failed");
614 case EXCEPTION_VM86_INTx
:
615 DEBUG_Printf(DBG_CHN_MESG
, "interrupt %02lx in vm86 mode",
616 rec
->ExceptionInformation
[0]);
618 case EXCEPTION_VM86_STI
:
619 DEBUG_Printf(DBG_CHN_MESG
, "sti in vm86 mode");
621 case EXCEPTION_VM86_PICRETURN
:
622 DEBUG_Printf(DBG_CHN_MESG
, "PIC return in vm86 mode");
624 case EXCEPTION_FLT_DENORMAL_OPERAND
:
625 DEBUG_Printf(DBG_CHN_MESG
, "denormal float operand");
627 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
628 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
630 case EXCEPTION_FLT_INEXACT_RESULT
:
631 DEBUG_Printf(DBG_CHN_MESG
, "inexact float result");
633 case EXCEPTION_FLT_INVALID_OPERATION
:
634 DEBUG_Printf(DBG_CHN_MESG
, "invalid float operation");
636 case EXCEPTION_FLT_OVERFLOW
:
637 DEBUG_Printf(DBG_CHN_MESG
, "floating pointer overflow");
639 case EXCEPTION_FLT_UNDERFLOW
:
640 DEBUG_Printf(DBG_CHN_MESG
, "floating pointer underflow");
642 case EXCEPTION_FLT_STACK_CHECK
:
643 DEBUG_Printf(DBG_CHN_MESG
, "floating point stack check");
646 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
651 if (local_mode
== automatic_mode
)
653 DEBUG_ExceptionProlog(is_debug
, FALSE
, rec
->ExceptionCode
);
654 DEBUG_ExceptionEpilog();
655 return 0; /* terminate execution */
658 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
))
660 DEBUG_InteractiveP
= TRUE
;
663 DEBUG_ExceptionEpilog();
668 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
)
671 DWORD cont
= DBG_CONTINUE
;
673 DEBUG_CurrPid
= de
->dwProcessId
;
674 DEBUG_CurrTid
= de
->dwThreadId
;
676 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
677 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
679 DEBUG_CurrThread
= NULL
;
681 switch (de
->dwDebugEventCode
)
683 case EXCEPTION_DEBUG_EVENT
:
684 if (!DEBUG_CurrThread
)
686 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
687 de
->dwProcessId
, de
->dwThreadId
);
691 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
692 de
->dwProcessId
, de
->dwThreadId
,
693 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
695 if (DEBUG_CurrProcess
->continue_on_first_exception
)
697 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
698 if (!DBG_IVAR(BreakOnAttach
)) break;
701 if (DEBUG_FetchContext())
703 cont
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
704 de
->u
.Exception
.dwFirstChance
,
705 DEBUG_CurrThread
->wait_for_first_exception
);
706 if (cont
&& DEBUG_CurrThread
)
708 DEBUG_CurrThread
->wait_for_first_exception
= 0;
709 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
714 case CREATE_THREAD_DEBUG_EVENT
:
715 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
716 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
718 if (DEBUG_CurrProcess
== NULL
)
720 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
723 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
)
725 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
729 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
731 de
->u
.CreateThread
.hThread
,
732 de
->u
.CreateThread
.lpStartAddress
,
733 de
->u
.CreateThread
.lpThreadLocalBase
);
734 if (!DEBUG_CurrThread
)
736 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
739 DEBUG_InitCurrThread();
742 case CREATE_PROCESS_DEBUG_EVENT
:
743 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
744 de
->u
.CreateProcessInfo
.hProcess
,
745 de
->u
.CreateProcessInfo
.lpImageName
);
747 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
748 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
749 de
->dwProcessId
, de
->dwThreadId
,
750 buffer
, de
->u
.CreateProcessInfo
.lpImageName
,
751 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
752 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
753 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
755 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
756 de
->u
.CreateProcessInfo
.hProcess
,
757 buffer
[0] ? buffer
: "<Debugged Process>");
758 if (DEBUG_CurrProcess
== NULL
)
760 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create process\n");
764 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
765 de
->dwProcessId
, de
->dwThreadId
,
766 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
768 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
770 de
->u
.CreateProcessInfo
.hThread
,
771 de
->u
.CreateProcessInfo
.lpStartAddress
,
772 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
773 if (!DEBUG_CurrThread
)
775 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
779 DEBUG_InitCurrProcess();
780 DEBUG_InitCurrThread();
782 /* module is either PE, NE or ELF module (for WineLib), but all
783 * are loaded with wine, so load its symbols, then the main module
787 char* ptr
= getenv("WINELOADER");
789 if (!ptr
|| DEBUG_ReadExecutableDbgInfo( ptr
) == DIL_ERROR
)
790 DEBUG_ReadExecutableDbgInfo( "wine" );
793 DEBUG_LoadModule32(DEBUG_CurrProcess
->imageName
, de
->u
.CreateProcessInfo
.hFile
,
794 de
->u
.CreateProcessInfo
.lpBaseOfImage
);
798 case EXIT_THREAD_DEBUG_EVENT
:
799 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
800 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
802 if (DEBUG_CurrThread
== NULL
)
804 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
807 /* FIXME: remove break point set on thread startup */
808 DEBUG_DelThread(DEBUG_CurrThread
);
811 case EXIT_PROCESS_DEBUG_EVENT
:
812 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
813 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
815 if (DEBUG_CurrProcess
== NULL
)
817 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
821 DEBUG_SetBreakpoints(FALSE
);
822 /* kill last thread */
823 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
824 DEBUG_DelProcess(DEBUG_CurrProcess
);
826 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
829 case LOAD_DLL_DEBUG_EVENT
:
830 if (DEBUG_CurrThread
== NULL
)
832 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
835 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
836 DEBUG_CurrThread
->process
->handle
,
837 de
->u
.LoadDll
.lpImageName
);
839 /* FIXME unicode: de->u.LoadDll.fUnicode */
840 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
841 de
->dwProcessId
, de
->dwThreadId
,
842 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
843 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
844 de
->u
.LoadDll
.nDebugInfoSize
);
846 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, de
->u
.LoadDll
.lpBaseOfDll
);
847 DEBUG_CheckDelayedBP();
848 if (DBG_IVAR(BreakOnDllLoad
))
850 DEBUG_Printf(DBG_CHN_MESG
, "Stopping on DLL %s loading at %08lx\n",
851 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
);
852 if (DEBUG_FetchContext()) cont
= 0;
856 case UNLOAD_DLL_DEBUG_EVENT
:
857 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
858 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
861 case OUTPUT_DEBUG_STRING_EVENT
:
862 if (DEBUG_CurrThread
== NULL
)
864 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
868 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
869 DEBUG_CurrThread
->process
->handle
,
870 de
->u
.DebugString
.lpDebugStringData
);
872 /* FIXME unicode de->u.DebugString.fUnicode ? */
873 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
874 de
->dwProcessId
, de
->dwThreadId
, buffer
);
878 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
879 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
880 de
->u
.RipInfo
.dwType
);
884 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
885 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
887 if (!cont
) return TRUE
; /* stop execution */
888 ContinueDebugEvent(de
->dwProcessId
, de
->dwThreadId
, cont
);
889 return FALSE
; /* continue execution */
892 static void DEBUG_ResumeDebuggee(DWORD cont
)
894 if (DEBUG_InException
)
896 DEBUG_ExceptionEpilog();
898 DEBUG_Printf(DBG_CHN_TRACE
,
899 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
901 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
905 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
907 if (DEBUG_CurrThread
)
909 if (!SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
910 DEBUG_Printf(DBG_CHN_MESG
, "Cannot set ctx on %lu\n", DEBUG_CurrTid
);
911 DEBUG_CurrThread
->wait_for_first_exception
= 0;
914 DEBUG_InteractiveP
= FALSE
;
915 if (!ContinueDebugEvent(DEBUG_CurrPid
, DEBUG_CurrTid
, cont
))
916 DEBUG_Printf(DBG_CHN_MESG
, "Cannot continue on %lu (%lu)\n",
917 DEBUG_CurrTid
, cont
);
920 void DEBUG_WaitNextException(DWORD cont
, int count
, int mode
)
924 if (cont
== DBG_CONTINUE
)
926 DEBUG_CurrThread
->exec_count
= count
;
927 DEBUG_CurrThread
->exec_mode
= mode
;
929 DEBUG_ResumeDebuggee(cont
);
931 while (DEBUG_CurrProcess
&& WaitForDebugEvent(&de
, INFINITE
))
933 if (DEBUG_HandleDebugEvent(&de
)) break;
935 if (!DEBUG_CurrProcess
) return;
936 DEBUG_InteractiveP
= TRUE
;
938 DEBUG_Printf(DBG_CHN_TRACE
,
939 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
941 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
945 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
949 static DWORD
DEBUG_MainLoop(void)
953 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg starting on pid %lx\n", DEBUG_CurrPid
);
955 /* wait for first exception */
956 while (WaitForDebugEvent(&de
, INFINITE
))
958 if (DEBUG_HandleDebugEvent(&de
)) break;
960 if (local_mode
== automatic_mode
)
962 /* print some extra information */
963 DEBUG_Printf(DBG_CHN_MESG
, "Modules:\n");
965 DEBUG_Printf(DBG_CHN_MESG
, "Threads:\n");
970 DEBUG_InteractiveP
= TRUE
;
973 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
978 static BOOL
DEBUG_Start(LPSTR cmdLine
)
980 PROCESS_INFORMATION info
;
981 STARTUPINFOA startup
;
983 memset(&startup
, 0, sizeof(startup
));
984 startup
.cb
= sizeof(startup
);
985 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
986 startup
.wShowWindow
= SW_SHOWNORMAL
;
988 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
991 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
992 FALSE
, DEBUG_PROCESS
|DEBUG_ONLY_THIS_PROCESS
|CREATE_NEW_CONSOLE
,
993 NULL
, NULL
, &startup
, &info
))
995 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
998 DEBUG_CurrPid
= info
.dwProcessId
;
999 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0, NULL
))) return FALSE
;
1004 void DEBUG_Run(const char* args
)
1006 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
1007 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
1010 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
1012 if (!DEBUG_LastCmdLine
) {
1013 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
1016 DEBUG_Start(DEBUG_LastCmdLine
);
1020 BOOL
DEBUG_InterruptDebuggee(void)
1022 DEBUG_Printf(DBG_CHN_MESG
, "Ctrl-C: stopping debuggee\n");
1023 /* FIXME: since we likely have a single process, signal the first process
1026 return DEBUG_ProcessList
&& DebugBreakProcess(DEBUG_ProcessList
->handle
);
1029 static BOOL WINAPI
DEBUG_CtrlCHandler(DWORD dwCtrlType
)
1031 if (dwCtrlType
== CTRL_C_EVENT
)
1033 return DEBUG_InterruptDebuggee();
1038 static void DEBUG_InitConsole(void)
1040 /* set our control-C handler */
1041 SetConsoleCtrlHandler(DEBUG_CtrlCHandler
, TRUE
);
1043 /* set our own title */
1044 SetConsoleTitle("Wine Debugger");
1047 static int DEBUG_Usage(void)
1049 DEBUG_Printf(DBG_CHN_MESG
, "Usage: winedbg [--debugmsg dbgoptions] [--auto] [--gdb] cmdline\n" );
1053 int main(int argc
, char** argv
)
1056 unsigned gdb_flags
= 0;
1058 /* Initialize the type handling stuff. */
1060 DEBUG_InitCVDataTypes();
1062 /* Initialize internal vars (types must have been initialized before) */
1063 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
1066 while (argc
> 1 && argv
[1][0] == '-')
1068 if (!strcmp( argv
[1], "--auto" ))
1070 if (local_mode
!= none_mode
) return DEBUG_Usage();
1071 local_mode
= automatic_mode
;
1072 /* force some internal variables */
1073 DBG_IVAR(BreakOnDllLoad
) = 0;
1074 DBG_IVAR(ConChannelMask
) = 0;
1075 DBG_IVAR(StdChannelMask
) = DBG_CHN_MESG
;
1079 if (!strcmp( argv
[1], "--gdb" ))
1081 if (local_mode
!= none_mode
) return DEBUG_Usage();
1082 local_mode
= gdb_mode
;
1086 if (strcmp( argv
[1], "--no-start") == 0 && local_mode
== gdb_mode
)
1089 argc
--; argv
++; /* as we don't use argv[0] */
1092 if (strcmp(argv
[1], "--with-xterm") == 0 && local_mode
== gdb_mode
)
1095 argc
--; argv
++; /* as we don't use argv[0] */
1098 if (!strcmp( argv
[1], "--debugmsg" ) && argv
[2])
1100 wine_dbg_parse_options( argv
[2] );
1105 return DEBUG_Usage();
1108 if (local_mode
== none_mode
) local_mode
= winedbg_mode
;
1110 /* try the form <myself> pid */
1111 if (DEBUG_CurrPid
== 0 && argc
== 2)
1115 DEBUG_CurrPid
= strtol(argv
[1], &ptr
, 10);
1116 if (DEBUG_CurrPid
== 0 || ptr
== NULL
||
1117 !DEBUG_Attach(DEBUG_CurrPid
, local_mode
!= gdb_mode
, FALSE
))
1121 /* try the form <myself> pid evt (Win32 JIT debugger) */
1122 if (DEBUG_CurrPid
== 0 && argc
== 3)
1128 if ((pid
= strtol(argv
[1], &ptr
, 10)) != 0 && ptr
!= NULL
&&
1129 (hEvent
= (HANDLE
)strtol(argv
[2], &ptr
, 10)) != 0 && ptr
!= NULL
)
1131 if (!DEBUG_Attach(pid
, TRUE
, FALSE
))
1133 /* don't care about result */
1137 if (!SetEvent(hEvent
))
1139 DEBUG_Printf(DBG_CHN_ERR
, "Invalid event handle: %p\n", hEvent
);
1142 CloseHandle(hEvent
);
1143 DEBUG_CurrPid
= pid
;
1147 if (DEBUG_CurrPid
== 0 && argc
> 1)
1152 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
1155 for (i
= 1; i
< argc
; i
++)
1157 len
+= strlen(argv
[i
]) + 1;
1158 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
1159 strcat(cmdLine
, argv
[i
]);
1160 cmdLine
[len
- 2] = ' ';
1161 cmdLine
[len
- 1] = '\0';
1164 if (!DEBUG_Start(cmdLine
))
1166 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
1169 DBG_free(DEBUG_LastCmdLine
);
1170 DEBUG_LastCmdLine
= cmdLine
;
1172 /* don't save local vars in gdb mode */
1173 if (local_mode
== gdb_mode
&& DEBUG_CurrPid
)
1174 return DEBUG_GdbRemote(gdb_flags
);
1176 DEBUG_InitConsole();
1178 retv
= DEBUG_MainLoop();
1179 /* don't save modified variables in auto mode */
1180 if (local_mode
!= automatic_mode
) DEBUG_IntVarsRW(FALSE
);
1186 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");