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 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
)
62 /* FIXME: this won't work is std output isn't attached to a console */
63 if (DBG_IVAR(ConChannelMask
) & chn
)
64 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, len
, NULL
, NULL
);
65 /* simplistic Unicode to ANSI conversion */
66 if (DBG_IVAR(StdChannelMask
) & chn
)
67 while (len
--) fputc((char)*buffer
++, stderr
);
70 int DEBUG_Printf(int chn
, const char* format
, ...)
72 static char buf
[4*1024];
76 va_start(valist
, format
);
77 len
= vsnprintf(buf
, sizeof(buf
), format
, valist
);
80 if (len
<= -1 || len
>= sizeof(buf
)) {
81 len
= sizeof(buf
) - 1;
83 buf
[len
- 1] = buf
[len
- 2] = buf
[len
- 3] = '.';
85 DEBUG_OutputA(chn
, buf
, len
);
89 static BOOL
DEBUG_IntVarsRW(int read
)
92 DWORD type
= REG_DWORD
;
94 DWORD count
= sizeof(val
);
96 DBG_INTVAR
* div
= DEBUG_IntVars
;
99 /* initializes internal vars table */
100 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
101 div->val = _val; div->name = #_var; div->pval = _ref; \
102 div->type = DEBUG_GetBasicType(_typ); div++;
107 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
)) {
108 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
110 fprintf(stderr
, "Cannot create WineDbg key in registry\n");
114 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
116 if (!DEBUG_IntVars
[i
].pval
) {
117 if (!RegQueryValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
118 &type
, (LPSTR
)&val
, &count
))
119 DEBUG_IntVars
[i
].val
= val
;
120 DEBUG_IntVars
[i
].pval
= &DEBUG_IntVars
[i
].val
;
122 *DEBUG_IntVars
[i
].pval
= 0;
125 /* FIXME: type should be infered from basic type -if any- of intvar */
126 if (DEBUG_IntVars
[i
].pval
== &DEBUG_IntVars
[i
].val
)
127 RegSetValueEx(hkey
, DEBUG_IntVars
[i
].name
, 0,
128 type
, (LPCVOID
)DEBUG_IntVars
[i
].pval
, count
);
135 DBG_INTVAR
* DEBUG_GetIntVar(const char* name
)
139 for (i
= 0; i
< DBG_IV_LAST
; i
++) {
140 if (!strcmp(DEBUG_IntVars
[i
].name
, name
))
141 return &DEBUG_IntVars
[i
];
146 DBG_PROCESS
* DEBUG_GetProcess(DWORD pid
)
150 for (p
= DEBUG_ProcessList
; p
; p
= p
->next
)
151 if (p
->pid
== pid
) break;
155 DBG_PROCESS
* DEBUG_AddProcess(DWORD pid
, HANDLE h
, const char* imageName
)
159 if ((p
= DEBUG_GetProcess(pid
)))
163 DEBUG_Printf(DBG_CHN_ERR
, "Process (%lu) is already defined\n", pid
);
168 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
173 if (!(p
= DBG_alloc(sizeof(DBG_PROCESS
)))) return NULL
;
176 p
->imageName
= imageName
? DBG_strdup(imageName
) : NULL
;
179 p
->continue_on_first_exception
= FALSE
;
184 p
->delayed_bp
= NULL
;
185 p
->num_delayed_bp
= 0;
187 p
->next
= DEBUG_ProcessList
;
189 if (DEBUG_ProcessList
) DEBUG_ProcessList
->prev
= p
;
190 DEBUG_ProcessList
= p
;
194 void DEBUG_DelProcess(DBG_PROCESS
* p
)
198 while (p
->threads
) DEBUG_DelThread(p
->threads
);
200 for (i
= 0; i
< p
->num_delayed_bp
; i
++)
201 if (p
->delayed_bp
[i
].is_symbol
)
202 DBG_free(p
->delayed_bp
[i
].u
.symbol
.name
);
204 DBG_free(p
->delayed_bp
);
205 if (p
->prev
) p
->prev
->next
= p
->next
;
206 if (p
->next
) p
->next
->prev
= p
->prev
;
207 if (p
== DEBUG_ProcessList
) DEBUG_ProcessList
= p
->next
;
208 if (p
== DEBUG_CurrProcess
) DEBUG_CurrProcess
= NULL
;
209 DBG_free((char*)p
->imageName
);
213 static void DEBUG_InitCurrProcess(void)
217 BOOL
DEBUG_ProcessGetString(char* buffer
, int size
, HANDLE hp
, LPSTR addr
)
221 return (addr
&& ReadProcessMemory(hp
, addr
, buffer
, size
, &sz
));
224 BOOL
DEBUG_ProcessGetStringIndirect(char* buffer
, int size
, HANDLE hp
, LPVOID addr
)
230 && ReadProcessMemory(hp
, addr
, &ad
, sizeof(ad
), &sz
)
233 && ReadProcessMemory(hp
, ad
, buffer
, size
, &sz
))
239 DBG_THREAD
* DEBUG_GetThread(DBG_PROCESS
* p
, DWORD tid
)
244 for (t
= p
->threads
; t
; t
= t
->next
)
245 if (t
->tid
== tid
) break;
249 DBG_THREAD
* DEBUG_AddThread(DBG_PROCESS
* p
, DWORD tid
,
250 HANDLE h
, LPVOID start
, LPVOID teb
)
252 DBG_THREAD
* t
= DBG_alloc(sizeof(DBG_THREAD
));
261 t
->wait_for_first_exception
= 0;
262 t
->exec_mode
= EXEC_CONT
;
265 sprintf(t
->name
, "%08lx", tid
);
268 t
->next
= p
->threads
;
270 if (p
->threads
) p
->threads
->prev
= t
;
276 static void DEBUG_InitCurrThread(void)
278 if (DEBUG_CurrThread
->start
) {
279 if (DEBUG_CurrThread
->process
->num_threads
== 1 ||
280 DBG_IVAR(BreakAllThreadsStartup
)) {
283 DEBUG_SetBreakpoints(FALSE
);
285 value
.cookie
= DV_TARGET
;
287 value
.addr
.off
= (DWORD
)DEBUG_CurrThread
->start
;
288 DEBUG_AddBreakpointFromValue(&value
);
289 DEBUG_SetBreakpoints(TRUE
);
292 DEBUG_CurrThread
->wait_for_first_exception
= 1;
296 void DEBUG_DelThread(DBG_THREAD
* t
)
298 if (t
->prev
) t
->prev
->next
= t
->next
;
299 if (t
->next
) t
->next
->prev
= t
->prev
;
300 if (t
== t
->process
->threads
) t
->process
->threads
= t
->next
;
301 t
->process
->num_threads
--;
302 if (t
== DEBUG_CurrThread
) DEBUG_CurrThread
= NULL
;
306 BOOL
DEBUG_Attach(DWORD pid
, BOOL cofe
)
308 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(pid
, 0, NULL
))) return FALSE
;
310 if (!DebugActiveProcess(pid
)) {
311 DEBUG_Printf(DBG_CHN_MESG
, "Can't attach process %lx: error %ld\n", pid
, GetLastError());
312 DEBUG_DelProcess(DEBUG_CurrProcess
);
313 DEBUG_CurrProcess
= NULL
;
316 DEBUG_CurrProcess
->continue_on_first_exception
= cofe
;
320 BOOL
DEBUG_Detach(void)
322 /* remove all set breakpoints in debuggee code */
323 DEBUG_SetBreakpoints(FALSE
);
324 /* needed for single stepping (ugly).
325 * should this be handled inside the server ??? */
327 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
329 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
330 DebugActiveProcessStop(DEBUG_CurrProcess
->pid
);
331 DEBUG_DelProcess(DEBUG_CurrProcess
);
332 DEBUG_CurrProcess
= NULL
;
333 /* FIXME: should zero out the symbol table too */
337 static BOOL
DEBUG_FetchContext(void)
339 DEBUG_context
.ContextFlags
= CONTEXT_CONTROL
341 #ifdef CONTEXT_SEGMENTS
344 #ifdef CONTEXT_DEBUG_REGISTERS
345 | CONTEXT_DEBUG_REGISTERS
348 if (!GetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
350 DEBUG_Printf(DBG_CHN_WARN
, "Can't get thread's context\n");
356 static BOOL
DEBUG_ExceptionProlog(BOOL is_debug
, BOOL force
, DWORD code
)
361 DEBUG_InException
= TRUE
;
362 DEBUG_GetCurrentAddress(&addr
);
363 DEBUG_SuspendExecution();
368 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (0x%08lx)", addr
.off
);
370 switch(DEBUG_GetSelectorType(addr
.seg
))
373 DEBUG_Printf(DBG_CHN_MESG
, " in 32-bit code (%04lx:%08lx)", addr
.seg
, addr
.off
);
376 DEBUG_Printf(DBG_CHN_MESG
, " in 16-bit code (%04lx:%04lx)", addr
.seg
, addr
.off
);
379 DEBUG_Printf(DBG_CHN_MESG
, " in vm86 code (%04lx:%04lx)", addr
.seg
, addr
.off
);
382 DEBUG_Printf(DBG_CHN_MESG
, " bad CS (%lx)", addr
.seg
);
385 DEBUG_Printf(DBG_CHN_MESG
, ".\n");
388 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
390 if (!force
&& is_debug
&&
391 DEBUG_ShouldContinue(&addr
, code
,
392 &DEBUG_CurrThread
->exec_count
))
395 if ((newmode
= DEBUG_GetSelectorType(addr
.seg
)) == MODE_INVALID
) newmode
= MODE_32
;
396 if (newmode
!= DEBUG_CurrThread
->dbg_mode
)
398 static const char * const names
[] = { "???", "16-bit", "32-bit", "vm86" };
399 DEBUG_Printf(DBG_CHN_MESG
,"In %s mode.\n", names
[newmode
] );
400 DEBUG_CurrThread
->dbg_mode
= newmode
;
405 if (is_debug
|| force
) {
407 * Do a quiet backtrace so that we have an idea of what the situation
408 * is WRT the source files.
410 DEBUG_BackTrace(DEBUG_CurrTid
, FALSE
);
412 /* This is a real crash, dump some info */
413 DEBUG_InfoRegisters(&DEBUG_context
);
416 if (DEBUG_CurrThread
->dbg_mode
== MODE_16
) {
417 DEBUG_InfoSegments(DEBUG_context
.SegDs
>> 3, 1);
418 if (DEBUG_context
.SegEs
!= DEBUG_context
.SegDs
)
419 DEBUG_InfoSegments(DEBUG_context
.SegEs
>> 3, 1);
421 DEBUG_InfoSegments(DEBUG_context
.SegFs
>> 3, 1);
423 DEBUG_BackTrace(DEBUG_CurrTid
, TRUE
);
427 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_OVER
) ||
428 (DEBUG_CurrThread
->exec_mode
== EXEC_STEPI_INSTR
)) {
432 /* Show where we crashed */
434 DEBUG_DisassembleInstruction(&addr
);
436 /* resets list internal arguments so we can look at source code when needed */
437 DEBUG_FindNearestSymbol(&addr
, TRUE
, NULL
, 0, &list
);
438 if (list
.sourcefile
) DEBUG_List(&list
, NULL
, 0);
443 static void DEBUG_ExceptionEpilog(void)
445 DEBUG_RestartExecution(DEBUG_CurrThread
->exec_count
);
447 * This will have gotten absorbed into the breakpoint info
448 * if it was used. Otherwise it would have been ignored.
449 * In any case, we don't mess with it any more.
451 if (DEBUG_CurrThread
->exec_mode
== EXEC_CONT
)
452 DEBUG_CurrThread
->exec_count
= 0;
453 DEBUG_InException
= FALSE
;
456 static BOOL
DEBUG_HandleException(EXCEPTION_RECORD
*rec
, BOOL first_chance
, BOOL force
)
458 BOOL is_debug
= FALSE
;
459 THREADNAME_INFO
*pThreadName
;
462 assert(DEBUG_CurrThread
);
464 switch (rec
->ExceptionCode
)
466 case EXCEPTION_BREAKPOINT
:
467 case EXCEPTION_SINGLE_STEP
:
470 case EXCEPTION_NAME_THREAD
:
471 pThreadName
= (THREADNAME_INFO
*)(rec
->ExceptionInformation
);
472 if (pThreadName
->dwThreadID
== -1)
473 pThread
= DEBUG_CurrThread
;
475 pThread
= DEBUG_GetThread(DEBUG_CurrProcess
, pThreadName
->dwThreadID
);
477 if (ReadProcessMemory(DEBUG_CurrThread
->process
->handle
, pThreadName
->szName
,
478 pThread
->name
, 9, NULL
))
479 DEBUG_Printf (DBG_CHN_MESG
,
480 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
481 pThread
->tid
, pThread
->name
);
485 if (first_chance
&& !is_debug
&& !force
&& !DBG_IVAR(BreakOnFirstChance
))
487 /* pass exception to program except for debug exceptions */
493 /* print some infos */
494 DEBUG_Printf(DBG_CHN_MESG
, "%s: ",
495 first_chance
? "First chance exception" : "Unhandled exception");
496 switch (rec
->ExceptionCode
)
498 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
499 DEBUG_Printf(DBG_CHN_MESG
, "divide by zero");
501 case EXCEPTION_INT_OVERFLOW
:
502 DEBUG_Printf(DBG_CHN_MESG
, "overflow");
504 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
505 DEBUG_Printf(DBG_CHN_MESG
, "array bounds ");
507 case EXCEPTION_ILLEGAL_INSTRUCTION
:
508 DEBUG_Printf(DBG_CHN_MESG
, "illegal instruction");
510 case EXCEPTION_STACK_OVERFLOW
:
511 DEBUG_Printf(DBG_CHN_MESG
, "stack overflow");
513 case EXCEPTION_PRIV_INSTRUCTION
:
514 DEBUG_Printf(DBG_CHN_MESG
, "privileged instruction");
516 case EXCEPTION_ACCESS_VIOLATION
:
517 if (rec
->NumberParameters
== 2)
518 DEBUG_Printf(DBG_CHN_MESG
, "page fault on %s access to 0x%08lx",
519 rec
->ExceptionInformation
[0] ? "write" : "read",
520 rec
->ExceptionInformation
[1]);
522 DEBUG_Printf(DBG_CHN_MESG
, "page fault");
524 case EXCEPTION_DATATYPE_MISALIGNMENT
:
525 DEBUG_Printf(DBG_CHN_MESG
, "Alignment");
528 DEBUG_Printf(DBG_CHN_MESG
, "^C");
531 DEBUG_Printf(DBG_CHN_MESG
, "^C");
533 case EXCEPTION_CRITICAL_SECTION_WAIT
:
538 addr
.off
= rec
->ExceptionInformation
[0];
540 DEBUG_Printf(DBG_CHN_MESG
, "wait failed on critical section ");
541 DEBUG_PrintAddress(&addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
543 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
545 DEBUG_Printf(DBG_CHN_MESG
, "\n");
549 case EXCEPTION_WINE_STUB
:
551 char dll
[32], name
[64];
552 DEBUG_ProcessGetString( dll
, sizeof(dll
), DEBUG_CurrThread
->process
->handle
,
553 (char *)rec
->ExceptionInformation
[0] );
554 DEBUG_ProcessGetString( name
, sizeof(name
), DEBUG_CurrThread
->process
->handle
,
555 (char *)rec
->ExceptionInformation
[1] );
556 DEBUG_Printf(DBG_CHN_MESG
, "unimplemented function %s.%s called", dll
, name
);
559 case EXCEPTION_WINE_ASSERTION
:
560 DEBUG_Printf(DBG_CHN_MESG
, "assertion failed");
562 case EXCEPTION_VM86_INTx
:
563 DEBUG_Printf(DBG_CHN_MESG
, "interrupt %02lx in vm86 mode",
564 rec
->ExceptionInformation
[0]);
566 case EXCEPTION_VM86_STI
:
567 DEBUG_Printf(DBG_CHN_MESG
, "sti in vm86 mode");
569 case EXCEPTION_VM86_PICRETURN
:
570 DEBUG_Printf(DBG_CHN_MESG
, "PIC return in vm86 mode");
573 DEBUG_Printf(DBG_CHN_MESG
, "%08lx", rec
->ExceptionCode
);
578 if (local_mode
== automatic_mode
)
580 DEBUG_ExceptionProlog(is_debug
, FALSE
, rec
->ExceptionCode
);
581 DEBUG_ExceptionEpilog();
582 return TRUE
; /* terminate execution */
585 if (DEBUG_ExceptionProlog(is_debug
, force
, rec
->ExceptionCode
))
587 DEBUG_InteractiveP
= TRUE
;
590 DEBUG_ExceptionEpilog();
595 static BOOL
DEBUG_HandleDebugEvent(DEBUG_EVENT
* de
)
600 DEBUG_CurrPid
= de
->dwProcessId
;
601 DEBUG_CurrTid
= de
->dwThreadId
;
603 if ((DEBUG_CurrProcess
= DEBUG_GetProcess(de
->dwProcessId
)) != NULL
)
604 DEBUG_CurrThread
= DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
);
606 DEBUG_CurrThread
= NULL
;
608 switch (de
->dwDebugEventCode
)
610 case EXCEPTION_DEBUG_EVENT
:
611 if (!DEBUG_CurrThread
)
613 DEBUG_Printf(DBG_CHN_ERR
, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
614 de
->dwProcessId
, de
->dwThreadId
);
618 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exception code=%08lx\n",
619 de
->dwProcessId
, de
->dwThreadId
,
620 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
622 if (DEBUG_CurrProcess
->continue_on_first_exception
)
624 DEBUG_CurrProcess
->continue_on_first_exception
= FALSE
;
625 if (!DBG_IVAR(BreakOnAttach
)) break;
628 if (DEBUG_FetchContext())
630 ret
= DEBUG_HandleException(&de
->u
.Exception
.ExceptionRecord
,
631 de
->u
.Exception
.dwFirstChance
,
632 DEBUG_CurrThread
->wait_for_first_exception
);
633 if (!ret
&& DEBUG_CurrThread
)
635 DEBUG_CurrThread
->wait_for_first_exception
= 0;
636 SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
);
641 case CREATE_THREAD_DEBUG_EVENT
:
642 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread D @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
643 (unsigned long)(LPVOID
)de
->u
.CreateThread
.lpStartAddress
);
645 if (DEBUG_CurrProcess
== NULL
)
647 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
650 if (DEBUG_GetThread(DEBUG_CurrProcess
, de
->dwThreadId
) != NULL
)
652 DEBUG_Printf(DBG_CHN_TRACE
, "Thread already listed, skipping\n");
656 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
658 de
->u
.CreateThread
.hThread
,
659 de
->u
.CreateThread
.lpStartAddress
,
660 de
->u
.CreateThread
.lpThreadLocalBase
);
661 if (!DEBUG_CurrThread
)
663 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
666 DEBUG_InitCurrThread();
669 case CREATE_PROCESS_DEBUG_EVENT
:
670 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
671 de
->u
.CreateProcessInfo
.hProcess
,
672 de
->u
.CreateProcessInfo
.lpImageName
);
674 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
675 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
676 de
->dwProcessId
, de
->dwThreadId
,
677 buffer
, de
->u
.CreateProcessInfo
.lpImageName
,
678 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
,
679 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
680 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
682 DEBUG_CurrProcess
= DEBUG_AddProcess(de
->dwProcessId
,
683 de
->u
.CreateProcessInfo
.hProcess
,
684 buffer
[0] ? buffer
: "<Debugged Process>");
685 if (DEBUG_CurrProcess
== NULL
)
687 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create process\n");
691 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: create thread I @%08lx\n",
692 de
->dwProcessId
, de
->dwThreadId
,
693 (unsigned long)(LPVOID
)de
->u
.CreateProcessInfo
.lpStartAddress
);
695 DEBUG_CurrThread
= DEBUG_AddThread(DEBUG_CurrProcess
,
697 de
->u
.CreateProcessInfo
.hThread
,
698 de
->u
.CreateProcessInfo
.lpStartAddress
,
699 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
700 if (!DEBUG_CurrThread
)
702 DEBUG_Printf(DBG_CHN_ERR
, "Couldn't create thread\n");
706 DEBUG_InitCurrProcess();
707 DEBUG_InitCurrThread();
709 /* module is either PE, NE or ELF module (for WineLib), but all
710 * are loaded with wine, so load its symbols, then the main module
714 char* ptr
= getenv("WINELOADER");
716 if (!ptr
|| DEBUG_ReadExecutableDbgInfo( ptr
) == DIL_ERROR
)
717 DEBUG_ReadExecutableDbgInfo( "wine" );
720 DEBUG_LoadModule32(DEBUG_CurrProcess
->imageName
, de
->u
.CreateProcessInfo
.hFile
,
721 (DWORD
)de
->u
.CreateProcessInfo
.lpBaseOfImage
);
725 case EXIT_THREAD_DEBUG_EVENT
:
726 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit thread (%ld)\n",
727 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
729 if (DEBUG_CurrThread
== NULL
)
731 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
734 /* FIXME: remove break point set on thread startup */
735 DEBUG_DelThread(DEBUG_CurrThread
);
738 case EXIT_PROCESS_DEBUG_EVENT
:
739 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: exit process (%ld)\n",
740 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
742 if (DEBUG_CurrProcess
== NULL
)
744 DEBUG_Printf(DBG_CHN_ERR
, "Unknown process\n");
748 DEBUG_SetBreakpoints(FALSE
);
749 /* kill last thread */
750 DEBUG_DelThread(DEBUG_CurrProcess
->threads
);
751 DEBUG_DelProcess(DEBUG_CurrProcess
);
753 DEBUG_Printf(DBG_CHN_MESG
, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid
);
756 case LOAD_DLL_DEBUG_EVENT
:
757 if (DEBUG_CurrThread
== NULL
)
759 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
762 DEBUG_ProcessGetStringIndirect(buffer
, sizeof(buffer
),
763 DEBUG_CurrThread
->process
->handle
,
764 de
->u
.LoadDll
.lpImageName
);
766 /* FIXME unicode: de->u.LoadDll.fUnicode */
767 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
768 de
->dwProcessId
, de
->dwThreadId
,
769 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
,
770 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
771 de
->u
.LoadDll
.nDebugInfoSize
);
773 DEBUG_LoadModule32(buffer
, de
->u
.LoadDll
.hFile
, (DWORD
)de
->u
.LoadDll
.lpBaseOfDll
);
774 DEBUG_CheckDelayedBP();
775 if (DBG_IVAR(BreakOnDllLoad
))
777 DEBUG_Printf(DBG_CHN_MESG
, "Stopping on DLL %s loading at %08lx\n",
778 buffer
, (unsigned long)de
->u
.LoadDll
.lpBaseOfDll
);
779 ret
= DEBUG_FetchContext();
783 case UNLOAD_DLL_DEBUG_EVENT
:
784 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unload DLL @%08lx\n", de
->dwProcessId
, de
->dwThreadId
,
785 (unsigned long)de
->u
.UnloadDll
.lpBaseOfDll
);
788 case OUTPUT_DEBUG_STRING_EVENT
:
789 if (DEBUG_CurrThread
== NULL
)
791 DEBUG_Printf(DBG_CHN_ERR
, "Unknown thread\n");
795 DEBUG_ProcessGetString(buffer
, sizeof(buffer
),
796 DEBUG_CurrThread
->process
->handle
,
797 de
->u
.DebugString
.lpDebugStringData
);
799 /* FIXME unicode de->u.DebugString.fUnicode ? */
800 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: output debug string (%s)\n",
801 de
->dwProcessId
, de
->dwThreadId
, buffer
);
805 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: rip error=%ld type=%ld\n",
806 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
807 de
->u
.RipInfo
.dwType
);
811 DEBUG_Printf(DBG_CHN_TRACE
, "%08lx:%08lx: unknown event (%ld)\n",
812 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
818 static void DEBUG_ResumeDebuggee(DWORD cont
)
820 if (DEBUG_InException
)
822 DEBUG_ExceptionEpilog();
824 DEBUG_Printf(DBG_CHN_TRACE
,
825 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
827 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
831 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
833 if (DEBUG_CurrThread
)
835 if (!SetThreadContext(DEBUG_CurrThread
->handle
, &DEBUG_context
))
836 DEBUG_Printf(DBG_CHN_MESG
, "Cannot set ctx on %lu\n", DEBUG_CurrTid
);
837 DEBUG_CurrThread
->wait_for_first_exception
= 0;
840 DEBUG_InteractiveP
= FALSE
;
841 if (!ContinueDebugEvent(DEBUG_CurrPid
, DEBUG_CurrTid
, cont
))
842 DEBUG_Printf(DBG_CHN_MESG
, "Cannot continue on %lu (%lu)\n",
843 DEBUG_CurrTid
, cont
);
846 void DEBUG_WaitNextException(DWORD cont
, int count
, int mode
)
850 if (cont
== DBG_CONTINUE
)
852 DEBUG_CurrThread
->exec_count
= count
;
853 DEBUG_CurrThread
->exec_mode
= mode
;
855 DEBUG_ResumeDebuggee(cont
);
857 while (DEBUG_CurrProcess
&& WaitForDebugEvent(&de
, INFINITE
))
859 if (DEBUG_HandleDebugEvent(&de
)) break;
860 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
862 if (!DEBUG_CurrProcess
) return;
863 DEBUG_InteractiveP
= TRUE
;
865 DEBUG_Printf(DBG_CHN_TRACE
,
866 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
868 DEBUG_context
.Eip
, DEBUG_context
.EFlags
,
872 DEBUG_CurrThread
->exec_mode
, DEBUG_CurrThread
->exec_count
);
876 static DWORD
DEBUG_MainLoop(void)
880 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg starting on pid %lx\n", DEBUG_CurrPid
);
882 /* wait for first exception */
883 while (WaitForDebugEvent(&de
, INFINITE
))
885 if (DEBUG_HandleDebugEvent(&de
)) break;
886 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
888 if (local_mode
== automatic_mode
)
890 /* print some extra information */
891 DEBUG_Printf(DBG_CHN_MESG
, "Modules:\n");
893 DEBUG_Printf(DBG_CHN_MESG
, "Threads:\n");
898 DEBUG_InteractiveP
= TRUE
;
901 DEBUG_Printf(DBG_CHN_MESG
, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid
);
906 static BOOL
DEBUG_Start(LPSTR cmdLine
)
908 PROCESS_INFORMATION info
;
909 STARTUPINFOA startup
;
911 memset(&startup
, 0, sizeof(startup
));
912 startup
.cb
= sizeof(startup
);
913 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
914 startup
.wShowWindow
= SW_SHOWNORMAL
;
916 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
919 if (!CreateProcess(NULL
, cmdLine
, NULL
, NULL
,
920 FALSE
, DEBUG_PROCESS
|DEBUG_ONLY_THIS_PROCESS
|CREATE_NEW_CONSOLE
,
921 NULL
, NULL
, &startup
, &info
))
923 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
926 DEBUG_CurrPid
= info
.dwProcessId
;
927 if (!(DEBUG_CurrProcess
= DEBUG_AddProcess(DEBUG_CurrPid
, 0, NULL
))) return FALSE
;
932 void DEBUG_Run(const char* args
)
934 DBG_MODULE
* wmod
= DEBUG_GetProcessMainModule(DEBUG_CurrProcess
);
935 const char* pgm
= (wmod
) ? wmod
->module_name
: "none";
938 DEBUG_Printf(DBG_CHN_MESG
, "Run (%s) with '%s'\n", pgm
, args
);
940 if (!DEBUG_LastCmdLine
) {
941 DEBUG_Printf(DBG_CHN_MESG
, "Cannot find previously used command line.\n");
944 DEBUG_Start(DEBUG_LastCmdLine
);
948 static BOOL WINAPI
DEBUG_CtrlCHandler(DWORD dwCtrlType
)
950 if (dwCtrlType
== CTRL_C_EVENT
)
952 DEBUG_Printf(DBG_CHN_MESG
, "Ctrl-C: stopping debuggee\n");
953 /* FIXME: since we likely have a single process, signal the first process
956 return DEBUG_ProcessList
&& DebugBreakProcess(DEBUG_ProcessList
->handle
);
961 static void DEBUG_InitConsole(void)
965 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
966 * stdout and stderr streams
968 if (DBG_IVAR(UseXTerm
))
974 /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */
975 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), &mode
))
976 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), mode
| WINE_ENABLE_LINE_INPUT_EMACS
);
978 /* set our control-C handler */
979 SetConsoleCtrlHandler(DEBUG_CtrlCHandler
, TRUE
);
981 /* set our own title */
982 SetConsoleTitle("Wine Debugger");
985 static int DEBUG_Usage(void)
987 DEBUG_Printf(DBG_CHN_MESG
, "Usage: winedbg [--debugmsg dbgoptions] [--auto] [--gdb] cmdline\n" );
991 int main(int argc
, char** argv
)
994 unsigned gdb_flags
= 0;
996 /* Initialize the type handling stuff. */
998 DEBUG_InitCVDataTypes();
1000 /* Initialize internal vars (types must have been initialized before) */
1001 if (!DEBUG_IntVarsRW(TRUE
)) return -1;
1004 while (argc
> 1 && argv
[1][0] == '-')
1006 if (!strcmp( argv
[1], "--auto" ))
1008 if (local_mode
!= none_mode
) return DEBUG_Usage();
1009 local_mode
= automatic_mode
;
1010 /* force some internal variables */
1011 DBG_IVAR(UseXTerm
) = 0;
1012 DBG_IVAR(BreakOnDllLoad
) = 0;
1013 DBG_IVAR(ConChannelMask
) = 0;
1014 DBG_IVAR(StdChannelMask
) = DBG_CHN_MESG
;
1018 if (!strcmp( argv
[1], "--gdb" ))
1020 if (local_mode
!= none_mode
) return DEBUG_Usage();
1021 local_mode
= gdb_mode
;
1025 if (strcmp( argv
[1], "--no-start") == 0 && local_mode
== gdb_mode
)
1028 argc
--; argv
++; /* as we don't use argv[0] */
1031 if (strcmp(argv
[1], "--with-xterm") == 0 && local_mode
== gdb_mode
)
1034 argc
--; argv
++; /* as we don't use argv[0] */
1037 if (!strcmp( argv
[1], "--debugmsg" ) && argv
[2])
1039 wine_dbg_parse_options( argv
[2] );
1044 return DEBUG_Usage();
1047 if (local_mode
== none_mode
) local_mode
= winedbg_mode
;
1049 /* try the from <myself> pid */
1050 if (DEBUG_CurrPid
== 0 && argc
== 2)
1054 DEBUG_CurrPid
= strtol(argv
[1], &ptr
, 10);
1055 if (DEBUG_CurrPid
== 0 || ptr
== NULL
||
1056 !DEBUG_Attach(DEBUG_CurrPid
, local_mode
!= gdb_mode
))
1060 /* try the from <myself> pid evt (Win32 JIT debugger) */
1061 if (DEBUG_CurrPid
== 0 && argc
== 3)
1067 if ((pid
= strtol(argv
[1], &ptr
, 10)) != 0 && ptr
!= NULL
&&
1068 (hEvent
= (HANDLE
)strtol(argv
[2], &ptr
, 10)) != 0 && ptr
!= NULL
)
1070 if (!DEBUG_Attach(pid
, TRUE
))
1072 /* don't care about result */
1076 if (!SetEvent(hEvent
))
1078 DEBUG_Printf(DBG_CHN_ERR
, "Invalid event handle: %p\n", hEvent
);
1081 CloseHandle(hEvent
);
1082 DEBUG_CurrPid
= pid
;
1086 if (DEBUG_CurrPid
== 0 && argc
> 1)
1091 if (!(cmdLine
= DBG_alloc(len
= 1))) goto oom_leave
;
1094 for (i
= 1; i
< argc
; i
++)
1096 len
+= strlen(argv
[i
]) + 1;
1097 if (!(cmdLine
= DBG_realloc(cmdLine
, len
))) goto oom_leave
;
1098 strcat(cmdLine
, argv
[i
]);
1099 cmdLine
[len
- 2] = ' ';
1100 cmdLine
[len
- 1] = '\0';
1103 if (!DEBUG_Start(cmdLine
))
1105 DEBUG_Printf(DBG_CHN_MESG
, "Couldn't start process '%s'\n", cmdLine
);
1108 DBG_free(DEBUG_LastCmdLine
);
1109 DEBUG_LastCmdLine
= cmdLine
;
1111 /* don't save local vars in gdb mode */
1112 if (local_mode
== gdb_mode
&& DEBUG_CurrPid
)
1113 return DEBUG_GdbRemote(gdb_flags
);
1115 DEBUG_InitConsole();
1117 retv
= DEBUG_MainLoop();
1118 /* don't save modified variables in auto mode */
1119 if (local_mode
!= automatic_mode
) DEBUG_IntVarsRW(FALSE
);
1125 DEBUG_Printf(DBG_CHN_MESG
, "Out of memory\n");