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;
367 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
369 if (DEBUG_CurrProcess
) DEBUG_InteractiveP
= TRUE
;
374 BOOL
DEBUG_Detach(void)
376 /* remove all set breakpoints in debuggee code */
377 DEBUG_SetBreakpoints(FALSE
);
378 /* needed for single stepping (ugly).
379 * should this be handled inside the server ??? */
381 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
383 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
384 DebugActiveProcessStop(DEBUG_CurrProcess
->pid
);
385 DEBUG_DelProcess(DEBUG_CurrProcess
);
386 /* FIXME: should zero out the symbol table too */
390 static BOOL
DEBUG_FetchContext(void)
392 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
394 #ifdef CONTEXT_SEGMENTS
397 #ifdef CONTEXT_DEBUG_REGISTERS
398 | CONTEXT_DEBUG_REGISTERS
401 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
403 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
409 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
414 DEBUG_InException
= TRUE
;
415 DEBUG_GetCurrentAddress(&addr
);
416 DEBUG_SuspendExecution();
421 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx)", addr
.off
);
423 switch(DEBUG_GetSelectorType(addr
.seg
))
426 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (%04lx:%08lx)", addr
.seg
, addr
.off
);
429 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04lx:%04lx)", addr
.seg
, addr
.off
);
432 DEBUG_Printf(DBG_CHN_MESG
, " in vm86 code (%04lx:%04lx)", addr
.seg
, addr
.off
);
435 DEBUG_Printf(DBG_CHN_MESG
, " bad CS (%lx)", addr
.seg
);
438 DEBUG_Printf(DBG_CHN_MESG
, ".\n");
441 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
443 if (!force
&& is_debug
&&
444 DEBUG_ShouldContinue(&addr
, code
,
445 &DEBUG_CurrThread
->exec_count
))
448 if ((newmode
= DEBUG_GetSelectorType(addr
.seg
)) == MODE_INVALID
) newmode
= MODE_32
;
449 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
451 static const char * const names
[] = { "???", "16-bit", "32-bit", "vm86" };
452 DEBUG_Printf(DBG_CHN_MESG
,"In %s mode.\n", names
[newmode
] );
453 DEBUG_CurrThread
->dbg_mode
= newmode
;
458 if (is_debug
|| force
) {
460 * Do a quiet backtrace so that we have an idea of what the situation
461 * is WRT the source files.
463 DEBUG_BackTrace(DEBUG_CurrTid
, FALSE
);
465 /* This is a real crash, dump some info */
466 DEBUG_InfoRegisters(&DEBUG_context
);
469 if (DEBUG_CurrThread
->dbg_mode
== MODE_16
) {
470 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
471 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
472 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
474 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
476 DEBUG_BackTrace(DEBUG_CurrTid
, TRUE
);
480 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_OVER
) ||
481 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_INSTR
)) {
485 /* Show where we crashed */
487 DEBUG_DisassembleInstruction(&addr
);
489 /* resets list internal arguments so we can look at source code when needed */
490 DEBUG_FindNearestSymbol(&addr
, TRUE
, NULL
, 0, &list
);
491 if (list
.sourcefile
) DEBUG_List(&list
, NULL
, 0);
496 static void DEBUG_ExceptionEpilog(void)
498 DEBUG_RestartExecution(DEBUG_CurrThread
->exec_count
);
500 * This will have gotten absorbed into the breakpoint info
501 * if it was used. Otherwise it would have been ignored.
502 * In any case, we don't mess with it any more.
504 if (DEBUG_CurrThread
->exec_mode
== EXEC_CONT
)
505 DEBUG_CurrThread
->exec_count
= 0;
506 DEBUG_InException
= FALSE
;
509 static BOOL
DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
511 BOOL is_debug
= FALSE
;
512 THREADNAME_INFO
*pThreadName
;
515 assert(DEBUG_CurrThread
);
517 switch (rec
->ExceptionCode
)
519 case EXCEPTION_BREAKPOINT
:
520 case EXCEPTION_SINGLE_STEP
:
523 case EXCEPTION_NAME_THREAD
:
524 pThreadName
= (THREADNAME_INFO
*)(rec
->ExceptionInformation
);
525 if (pThreadName
->dwThreadID
== -1)
526 pThread
= DEBUG_CurrThread
;
528 pThread
= DEBUG_GetThread(DEBUG_CurrProcess
, pThreadName
->dwThreadID
);
530 if (ReadProcessMemory(DEBUG_CurrThread
->process
->handle
, pThreadName
->szName
,
531 pThread
->name
, 9, NULL
))
532 DEBUG_Printf (DBG_CHN_MESG
,
533 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
534 pThread
->tid
, pThread
->name
);
538 if (first_chance
&& !is_debug
&& !force
&& !DBG_IVAR(BreakOnFirstChance
))
540 /* pass exception to program except for debug exceptions */
546 /* print some infos */
547 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
548 first_chance
? "First chance exception" : "Unhandled exception");
549 switch (rec
->ExceptionCode
)
551 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
552 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
554 case EXCEPTION_INT_OVERFLOW
:
555 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
557 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
558 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
560 case EXCEPTION_ILLEGAL_INSTRUCTION
:
561 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
563 case EXCEPTION_STACK_OVERFLOW
:
564 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
566 case EXCEPTION_PRIV_INSTRUCTION
:
567 DEBUG_Printf(DBG_CHN_MESG
, "privileged instruction");
569 case EXCEPTION_ACCESS_VIOLATION
:
570 if (rec
->NumberParameters
== 2)
571 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
572 rec
->ExceptionInformation
[0] ? "write" : "read",
573 rec
->ExceptionInformation
[1]);
575 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
577 case EXCEPTION_DATATYPE_MISALIGNMENT
:
578 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
581 DEBUG_Printf(DBG_CHN_MESG
, "^C");
584 DEBUG_Printf(DBG_CHN_MESG
, "^C");
586 case STATUS_POSSIBLE_DEADLOCK
:
591 addr
.off
= rec
->ExceptionInformation
[0];
593 DEBUG_Printf(DBG_CHN_MESG
, "wait failed on critical section ");
594 DEBUG_PrintAddress(&addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
596 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
598 DEBUG_Printf(DBG_CHN_MESG
, "\n");
602 case EXCEPTION_WINE_STUB
:
604 char dll
[32], name
[64];
605 DEBUG_ProcessGetString( dll
, sizeof(dll
), DEBUG_CurrThread
->process
->handle
,
606 (char *)rec
->ExceptionInformation
[0] );
607 DEBUG_ProcessGetString( name
, sizeof(name
), DEBUG_CurrThread
->process
->handle
,
608 (char *)rec
->ExceptionInformation
[1] );
609 DEBUG_Printf(DBG_CHN_MESG
, "unimplemented function %s.%s called", dll
, name
);
612 case EXCEPTION_WINE_ASSERTION
:
613 DEBUG_Printf(DBG_CHN_MESG
, "assertion failed");
615 case EXCEPTION_VM86_INTx
:
616 DEBUG_Printf(DBG_CHN_MESG
, "interrupt %02lx in vm86 mode",
617 rec
->ExceptionInformation
[0]);
619 case EXCEPTION_VM86_STI
:
620 DEBUG_Printf(DBG_CHN_MESG
, "sti in vm86 mode");
622 case EXCEPTION_VM86_PICRETURN
:
623 DEBUG_Printf(DBG_CHN_MESG
, "PIC return in vm86 mode");
625 case EXCEPTION_FLT_DENORMAL_OPERAND
:
626 DEBUG_Printf(DBG_CHN_MESG
, "denormal float operand");
628 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
629 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
631 case EXCEPTION_FLT_INEXACT_RESULT
:
632 DEBUG_Printf(DBG_CHN_MESG
, "inexact float result");
634 case EXCEPTION_FLT_INVALID_OPERATION
:
635 DEBUG_Printf(DBG_CHN_MESG
, "invalid float operation");
637 case EXCEPTION_FLT_OVERFLOW
:
638 DEBUG_Printf(DBG_CHN_MESG
, "floating pointer overflow");
640 case EXCEPTION_FLT_UNDERFLOW
:
641 DEBUG_Printf(DBG_CHN_MESG
, "floating pointer underflow");
643 case EXCEPTION_FLT_STACK_CHECK
:
644 DEBUG_Printf(DBG_CHN_MESG
, "floating point stack check");
647 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
652 if (local_mode
== automatic_mode
)
654 DEBUG_ExceptionProlog(is_debug
, FALSE
, rec
->ExceptionCode
);
655 DEBUG_ExceptionEpilog();
656 return TRUE
; /* terminate execution */
659 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
))
661 DEBUG_InteractiveP
= TRUE
;
664 DEBUG_ExceptionEpilog();
669 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
)
674 DEBUG_CurrPid
= de
->dwProcessId
;
675 DEBUG_CurrTid
= de
->dwThreadId
;
677 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
678 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
680 DEBUG_CurrThread
= NULL
;
682 switch (de
->dwDebugEventCode
)
684 case EXCEPTION_DEBUG_EVENT
:
685 if (!DEBUG_CurrThread
)
687 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
688 de
->dwProcessId
, de
->dwThreadId
);
692 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
693 de
->dwProcessId
, de
->dwThreadId
,
694 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
696 if (DEBUG_CurrProcess
->continue_on_first_exception
)
698 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
699 if (!DBG_IVAR(BreakOnAttach
)) break;
702 if (DEBUG_FetchContext())
704 ret
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
705 de
->u
.Exception
.dwFirstChance
,
706 DEBUG_CurrThread
->wait_for_first_exception
);
707 if (!ret
&& DEBUG_CurrThread
)
709 DEBUG_CurrThread
->wait_for_first_exception
= 0;
710 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
715 case CREATE_THREAD_DEBUG_EVENT
:
716 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
717 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
719 if (DEBUG_CurrProcess
== NULL
)
721 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
724 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
)
726 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
730 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
732 de
->u
.CreateThread
.hThread
,
733 de
->u
.CreateThread
.lpStartAddress
,
734 de
->u
.CreateThread
.lpThreadLocalBase
);
735 if (!DEBUG_CurrThread
)
737 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
740 DEBUG_InitCurrThread();
743 case CREATE_PROCESS_DEBUG_EVENT
:
744 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
745 de
->u
.CreateProcessInfo
.hProcess
,
746 de
->u
.CreateProcessInfo
.lpImageName
);
748 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
749 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
750 de
->dwProcessId
, de
->dwThreadId
,
751 buffer
, de
->u
.CreateProcessInfo
.lpImageName
,
752 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
753 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
754 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
756 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
757 de
->u
.CreateProcessInfo
.hProcess
,
758 buffer
[0] ? buffer
: "<Debugged Process>");
759 if (DEBUG_CurrProcess
== NULL
)
761 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create process\n");
765 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
766 de
->dwProcessId
, de
->dwThreadId
,
767 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
769 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
771 de
->u
.CreateProcessInfo
.hThread
,
772 de
->u
.CreateProcessInfo
.lpStartAddress
,
773 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
774 if (!DEBUG_CurrThread
)
776 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
780 DEBUG_InitCurrProcess();
781 DEBUG_InitCurrThread();
783 /* module is either PE, NE or ELF module (for WineLib), but all
784 * are loaded with wine, so load its symbols, then the main module
788 char* ptr
= getenv("WINELOADER");
790 if (!ptr
|| DEBUG_ReadExecutableDbgInfo( ptr
) == DIL_ERROR
)
791 DEBUG_ReadExecutableDbgInfo( "wine" );
794 DEBUG_LoadModule32(DEBUG_CurrProcess
->imageName
, de
->u
.CreateProcessInfo
.hFile
,
795 de
->u
.CreateProcessInfo
.lpBaseOfImage
);
799 case EXIT_THREAD_DEBUG_EVENT
:
800 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
801 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
803 if (DEBUG_CurrThread
== NULL
)
805 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
808 /* FIXME: remove break point set on thread startup */
809 DEBUG_DelThread(DEBUG_CurrThread
);
812 case EXIT_PROCESS_DEBUG_EVENT
:
813 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
814 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
816 if (DEBUG_CurrProcess
== NULL
)
818 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
822 DEBUG_SetBreakpoints(FALSE
);
823 /* kill last thread */
824 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
825 DEBUG_DelProcess(DEBUG_CurrProcess
);
827 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
830 case LOAD_DLL_DEBUG_EVENT
:
831 if (DEBUG_CurrThread
== NULL
)
833 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
836 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
837 DEBUG_CurrThread
->process
->handle
,
838 de
->u
.LoadDll
.lpImageName
);
840 /* FIXME unicode: de->u.LoadDll.fUnicode */
841 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
842 de
->dwProcessId
, de
->dwThreadId
,
843 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
844 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
845 de
->u
.LoadDll
.nDebugInfoSize
);
847 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, de
->u
.LoadDll
.lpBaseOfDll
);
848 DEBUG_CheckDelayedBP();
849 if (DBG_IVAR(BreakOnDllLoad
))
851 DEBUG_Printf(DBG_CHN_MESG
, "Stopping on DLL %s loading at %08lx\n",
852 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
);
853 ret
= DEBUG_FetchContext();
857 case UNLOAD_DLL_DEBUG_EVENT
:
858 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
859 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
862 case OUTPUT_DEBUG_STRING_EVENT
:
863 if (DEBUG_CurrThread
== NULL
)
865 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
869 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
870 DEBUG_CurrThread
->process
->handle
,
871 de
->u
.DebugString
.lpDebugStringData
);
873 /* FIXME unicode de->u.DebugString.fUnicode ? */
874 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
875 de
->dwProcessId
, de
->dwThreadId
, buffer
);
879 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
880 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
881 de
->u
.RipInfo
.dwType
);
885 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
886 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
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;
934 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
936 if (!DEBUG_CurrProcess
) return;
937 DEBUG_InteractiveP
= TRUE
;
939 DEBUG_Printf(DBG_CHN_TRACE
,
940 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
942 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
946 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
950 static DWORD
DEBUG_MainLoop(void)
954 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg starting on pid %lx\n", DEBUG_CurrPid
);
956 /* wait for first exception */
957 while (WaitForDebugEvent(&de
, INFINITE
))
959 if (DEBUG_HandleDebugEvent(&de
)) break;
960 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
962 if (local_mode
== automatic_mode
)
964 /* print some extra information */
965 DEBUG_Printf(DBG_CHN_MESG
, "Modules:\n");
967 DEBUG_Printf(DBG_CHN_MESG
, "Threads:\n");
972 DEBUG_InteractiveP
= TRUE
;
975 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
980 static BOOL
DEBUG_Start(LPSTR cmdLine
)
982 PROCESS_INFORMATION info
;
983 STARTUPINFOA startup
;
985 memset(&startup
, 0, sizeof(startup
));
986 startup
.cb
= sizeof(startup
);
987 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
988 startup
.wShowWindow
= SW_SHOWNORMAL
;
990 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
993 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
994 FALSE
, DEBUG_PROCESS
|DEBUG_ONLY_THIS_PROCESS
|CREATE_NEW_CONSOLE
,
995 NULL
, NULL
, &startup
, &info
))
997 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
1000 DEBUG_CurrPid
= info
.dwProcessId
;
1001 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0, NULL
))) return FALSE
;
1006 void DEBUG_Run(const char* args
)
1008 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
1009 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
1012 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
1014 if (!DEBUG_LastCmdLine
) {
1015 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
1018 DEBUG_Start(DEBUG_LastCmdLine
);
1022 BOOL
DEBUG_InterruptDebuggee(void)
1024 DEBUG_Printf(DBG_CHN_MESG
, "Ctrl-C: stopping debuggee\n");
1025 /* FIXME: since we likely have a single process, signal the first process
1028 return DEBUG_ProcessList
&& DebugBreakProcess(DEBUG_ProcessList
->handle
);
1031 static BOOL WINAPI
DEBUG_CtrlCHandler(DWORD dwCtrlType
)
1033 if (dwCtrlType
== CTRL_C_EVENT
)
1035 return DEBUG_InterruptDebuggee();
1040 static void DEBUG_InitConsole(void)
1042 /* set our control-C handler */
1043 SetConsoleCtrlHandler(DEBUG_CtrlCHandler
, TRUE
);
1045 /* set our own title */
1046 SetConsoleTitle("Wine Debugger");
1049 static int DEBUG_Usage(void)
1051 DEBUG_Printf(DBG_CHN_MESG
, "Usage: winedbg [--debugmsg dbgoptions] [--auto] [--gdb] cmdline\n" );
1055 int main(int argc
, char** argv
)
1058 unsigned gdb_flags
= 0;
1060 /* Initialize the type handling stuff. */
1062 DEBUG_InitCVDataTypes();
1064 /* Initialize internal vars (types must have been initialized before) */
1065 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
1068 while (argc
> 1 && argv
[1][0] == '-')
1070 if (!strcmp( argv
[1], "--auto" ))
1072 if (local_mode
!= none_mode
) return DEBUG_Usage();
1073 local_mode
= automatic_mode
;
1074 /* force some internal variables */
1075 DBG_IVAR(BreakOnDllLoad
) = 0;
1076 DBG_IVAR(ConChannelMask
) = 0;
1077 DBG_IVAR(StdChannelMask
) = DBG_CHN_MESG
;
1081 if (!strcmp( argv
[1], "--gdb" ))
1083 if (local_mode
!= none_mode
) return DEBUG_Usage();
1084 local_mode
= gdb_mode
;
1088 if (strcmp( argv
[1], "--no-start") == 0 && local_mode
== gdb_mode
)
1091 argc
--; argv
++; /* as we don't use argv[0] */
1094 if (strcmp(argv
[1], "--with-xterm") == 0 && local_mode
== gdb_mode
)
1097 argc
--; argv
++; /* as we don't use argv[0] */
1100 if (!strcmp( argv
[1], "--debugmsg" ) && argv
[2])
1102 wine_dbg_parse_options( argv
[2] );
1107 return DEBUG_Usage();
1110 if (local_mode
== none_mode
) local_mode
= winedbg_mode
;
1112 /* try the form <myself> pid */
1113 if (DEBUG_CurrPid
== 0 && argc
== 2)
1117 DEBUG_CurrPid
= strtol(argv
[1], &ptr
, 10);
1118 if (DEBUG_CurrPid
== 0 || ptr
== NULL
||
1119 !DEBUG_Attach(DEBUG_CurrPid
, local_mode
!= gdb_mode
, FALSE
))
1123 /* try the form <myself> pid evt (Win32 JIT debugger) */
1124 if (DEBUG_CurrPid
== 0 && argc
== 3)
1130 if ((pid
= strtol(argv
[1], &ptr
, 10)) != 0 && ptr
!= NULL
&&
1131 (hEvent
= (HANDLE
)strtol(argv
[2], &ptr
, 10)) != 0 && ptr
!= NULL
)
1133 if (!DEBUG_Attach(pid
, TRUE
, FALSE
))
1135 /* don't care about result */
1139 if (!SetEvent(hEvent
))
1141 DEBUG_Printf(DBG_CHN_ERR
, "Invalid event handle: %p\n", hEvent
);
1144 CloseHandle(hEvent
);
1145 DEBUG_CurrPid
= pid
;
1149 if (DEBUG_CurrPid
== 0 && argc
> 1)
1154 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
1157 for (i
= 1; i
< argc
; i
++)
1159 len
+= strlen(argv
[i
]) + 1;
1160 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
1161 strcat(cmdLine
, argv
[i
]);
1162 cmdLine
[len
- 2] = ' ';
1163 cmdLine
[len
- 1] = '\0';
1166 if (!DEBUG_Start(cmdLine
))
1168 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
1171 DBG_free(DEBUG_LastCmdLine
);
1172 DEBUG_LastCmdLine
= cmdLine
;
1174 /* don't save local vars in gdb mode */
1175 if (local_mode
== gdb_mode
&& DEBUG_CurrPid
)
1176 return DEBUG_GdbRemote(gdb_flags
);
1178 DEBUG_InitConsole();
1180 retv
= DEBUG_MainLoop();
1181 /* don't save modified variables in auto mode */
1182 if (local_mode
!= automatic_mode
) DEBUG_IntVarsRW(FALSE
);
1188 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");