Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / programs / winedbg / winedbg.c
blob6f58c027697fe141caf635961d9512945af4d288
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
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "debugger.h"
28 #include "wincon.h"
29 #include "winreg.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winternl.h"
33 #include "excpt.h"
34 #include "wine/library.h"
35 #include "winnls.h"
37 DBG_PROCESS* DEBUG_CurrProcess = NULL;
38 DBG_THREAD* DEBUG_CurrThread = NULL;
39 DWORD DEBUG_CurrTid;
40 DWORD DEBUG_CurrPid;
41 CONTEXT DEBUG_context;
42 BOOL DEBUG_InteractiveP = FALSE;
43 static BOOL DEBUG_InException = FALSE;
44 int curr_frame = 0;
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 char* ansi = NULL;
63 int newlen;
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);
68 if(newlen)
70 ansi = (char*)DBG_alloc(newlen);
71 if(ansi)
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 */
78 if(!ansi)
80 ansi = DBG_alloc(len);
81 if(ansi)
83 int i;
84 for(i = 0; i < len; i++)
86 ansi[i] = (char)buffer[i];
89 newlen = len;
91 /* else we are having REALLY bad luck today */
94 if(ansi)
96 DEBUG_OutputA(chn, ansi, newlen);
97 DBG_free(ansi);
101 int DEBUG_Printf(int chn, const char* format, ...)
103 static char buf[4*1024];
104 va_list valist;
105 int len;
107 va_start(valist, format);
108 len = vsnprintf(buf, sizeof(buf), format, valist);
109 va_end(valist);
111 if (len <= -1 || len >= sizeof(buf)) {
112 len = sizeof(buf) - 1;
113 buf[len] = 0;
114 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
116 DEBUG_OutputA(chn, buf, len);
117 return len;
120 static BOOL DEBUG_IntVarsRW(int read)
122 HKEY hkey;
123 DWORD type = REG_DWORD;
124 DWORD val;
125 DWORD count = sizeof(val);
126 int i;
127 DBG_INTVAR* div = DEBUG_IntVars;
129 if (read) {
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++;
134 #include "intvar.h"
135 #undef INTERNAL_VAR
138 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
139 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
140 * so don't use it */
141 fprintf(stderr, "Cannot create WineDbg key in registry\n");
142 return FALSE;
145 for (i = 0; i < DBG_IV_LAST; i++) {
146 if (read) {
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;
152 } else {
153 *DEBUG_IntVars[i].pval = 0;
155 } else {
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);
162 RegCloseKey(hkey);
163 return TRUE;
166 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
168 int i;
170 for (i = 0; i < DBG_IV_LAST; i++) {
171 if (!strcmp(DEBUG_IntVars[i].name, name))
172 return &DEBUG_IntVars[i];
174 return NULL;
177 DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
179 DBG_PROCESS* p;
181 for (p = DEBUG_ProcessList; p; p = p->next)
182 if (p->pid == pid) break;
183 return p;
186 DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName)
188 DBG_PROCESS* p;
190 if ((p = DEBUG_GetProcess(pid)))
192 if (p->handle != 0)
194 DEBUG_Printf(DBG_CHN_ERR, "Process (%lu) is already defined\n", pid);
196 else
198 p->handle = h;
199 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
201 return p;
204 if (!(p = DBG_alloc(sizeof(DBG_PROCESS)))) return NULL;
205 p->handle = h;
206 p->pid = pid;
207 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
208 p->threads = NULL;
209 p->num_threads = 0;
210 p->continue_on_first_exception = FALSE;
211 p->modules = NULL;
212 p->num_modules = 0;
213 p->next_index = 0;
214 p->dbg_hdr_addr = 0;
215 p->delayed_bp = NULL;
216 p->num_delayed_bp = 0;
218 p->next = DEBUG_ProcessList;
219 p->prev = NULL;
220 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
221 DEBUG_ProcessList = p;
222 return p;
225 void DEBUG_DelProcess(DBG_PROCESS* p)
227 int i;
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);
241 DBG_free(p);
244 static void DEBUG_InitCurrProcess(void)
248 BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
250 DWORD sz;
251 *(WCHAR*)buffer = 0;
252 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
255 BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
257 LPVOID ad;
258 DWORD sz;
260 if ( addr
261 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
262 && sz == sizeof(ad)
263 && ad
264 && ReadProcessMemory(hp, ad, buffer, size, &sz))
265 return TRUE;
266 *(WCHAR*)buffer = 0;
267 return FALSE;
270 DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
272 DBG_THREAD* t;
274 if (!p) return NULL;
275 for (t = p->threads; t; t = t->next)
276 if (t->tid == tid) break;
277 return t;
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));
284 if (!t)
285 return NULL;
287 t->handle = h;
288 t->tid = tid;
289 t->start = start;
290 t->teb = teb;
291 t->process = p;
292 t->wait_for_first_exception = 0;
293 t->exec_mode = EXEC_CONT;
294 t->exec_count = 0;
296 snprintf(t->name, sizeof(t->name), "%08lx", tid);
298 p->num_threads++;
299 t->next = p->threads;
300 t->prev = NULL;
301 if (p->threads) p->threads->prev = t;
302 p->threads = t;
304 return t;
307 static void DEBUG_InitCurrThread(void)
309 if (DEBUG_CurrThread->start) {
310 if (DEBUG_CurrThread->process->num_threads == 1 ||
311 DBG_IVAR(BreakAllThreadsStartup)) {
312 DBG_VALUE value;
314 DEBUG_SetBreakpoints(FALSE);
315 value.type = NULL;
316 value.cookie = DV_TARGET;
317 value.addr.seg = 0;
318 value.addr.off = (DWORD)DEBUG_CurrThread->start;
319 DEBUG_AddBreakpointFromValue(&value);
320 DEBUG_SetBreakpoints(TRUE);
322 } else {
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;
334 DBG_free(t);
337 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de);
339 /******************************************************************
340 * DEBUG_Attach
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)
350 DEBUG_EVENT de;
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);
357 return FALSE;
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;
370 return 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 ??? */
379 #ifdef __i386__
380 DEBUG_context.EFlags &= ~STEP_FLAG;
381 #endif
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 */
386 return TRUE;
389 static BOOL DEBUG_FetchContext(void)
391 DEBUG_context.ContextFlags = CONTEXT_CONTROL
392 | CONTEXT_INTEGER
393 #ifdef CONTEXT_SEGMENTS
394 | CONTEXT_SEGMENTS
395 #endif
396 #ifdef CONTEXT_DEBUG_REGISTERS
397 | CONTEXT_DEBUG_REGISTERS
398 #endif
400 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context))
402 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
403 return FALSE;
405 return TRUE;
408 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
410 DBG_ADDR addr;
411 int newmode;
413 DEBUG_InException = TRUE;
414 DEBUG_GetCurrentAddress(&addr);
415 DEBUG_SuspendExecution();
417 if (!is_debug)
419 if (!addr.seg)
420 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
421 else
422 switch(DEBUG_GetSelectorType(addr.seg))
424 case MODE_32:
425 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
426 break;
427 case MODE_16:
428 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
429 break;
430 case MODE_VM86:
431 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
432 break;
433 case MODE_INVALID:
434 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
435 break;
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))
445 return FALSE;
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;
455 DEBUG_DoDisplay();
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);
463 } else {
464 /* This is a real crash, dump some info */
465 DEBUG_InfoRegisters(&DEBUG_context);
466 DEBUG_InfoStack();
467 #ifdef __i386__
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);
474 #endif
475 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
478 if (!is_debug ||
479 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_OVER) ||
480 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_INSTR)) {
482 struct list_id list;
484 /* Show where we crashed */
485 curr_frame = 0;
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);
492 return TRUE;
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;
512 DBG_THREAD *pThread;
514 assert(DEBUG_CurrThread);
516 switch (rec->ExceptionCode)
518 case EXCEPTION_BREAKPOINT:
519 case EXCEPTION_SINGLE_STEP:
520 is_debug = TRUE;
521 break;
522 case EXCEPTION_NAME_THREAD:
523 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
524 if (pThreadName->dwThreadID == -1)
525 pThread = DEBUG_CurrThread;
526 else
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);
534 return DBG_CONTINUE;
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;
543 if (!is_debug)
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");
552 break;
553 case EXCEPTION_INT_OVERFLOW:
554 DEBUG_Printf(DBG_CHN_MESG, "overflow");
555 break;
556 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
557 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
558 break;
559 case EXCEPTION_ILLEGAL_INSTRUCTION:
560 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
561 break;
562 case EXCEPTION_STACK_OVERFLOW:
563 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
564 break;
565 case EXCEPTION_PRIV_INSTRUCTION:
566 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
567 break;
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]);
573 else
574 DEBUG_Printf(DBG_CHN_MESG, "page fault");
575 break;
576 case EXCEPTION_DATATYPE_MISALIGNMENT:
577 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
578 break;
579 case DBG_CONTROL_C:
580 DEBUG_Printf(DBG_CHN_MESG, "^C");
581 break;
582 case CONTROL_C_EXIT:
583 DEBUG_Printf(DBG_CHN_MESG, "^C");
584 break;
585 case STATUS_POSSIBLE_DEADLOCK:
587 DBG_ADDR addr;
589 addr.seg = 0;
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;
600 break;
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 );
610 break;
611 case EXCEPTION_WINE_ASSERTION:
612 DEBUG_Printf(DBG_CHN_MESG, "assertion failed");
613 break;
614 case EXCEPTION_VM86_INTx:
615 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
616 rec->ExceptionInformation[0]);
617 break;
618 case EXCEPTION_VM86_STI:
619 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
620 break;
621 case EXCEPTION_VM86_PICRETURN:
622 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
623 break;
624 case EXCEPTION_FLT_DENORMAL_OPERAND:
625 DEBUG_Printf(DBG_CHN_MESG, "denormal float operand");
626 break;
627 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
628 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
629 break;
630 case EXCEPTION_FLT_INEXACT_RESULT:
631 DEBUG_Printf(DBG_CHN_MESG, "inexact float result");
632 break;
633 case EXCEPTION_FLT_INVALID_OPERATION:
634 DEBUG_Printf(DBG_CHN_MESG, "invalid float operation");
635 break;
636 case EXCEPTION_FLT_OVERFLOW:
637 DEBUG_Printf(DBG_CHN_MESG, "floating pointer overflow");
638 break;
639 case EXCEPTION_FLT_UNDERFLOW:
640 DEBUG_Printf(DBG_CHN_MESG, "floating pointer underflow");
641 break;
642 case EXCEPTION_FLT_STACK_CHECK:
643 DEBUG_Printf(DBG_CHN_MESG, "floating point stack check");
644 break;
645 default:
646 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
647 break;
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;
661 return 0;
663 DEBUG_ExceptionEpilog();
665 return DBG_CONTINUE;
668 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de)
670 char buffer[256];
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);
678 else
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);
688 break;
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);
712 break;
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");
721 break;
723 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL)
725 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
726 break;
729 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
730 de->dwThreadId,
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");
737 break;
739 DEBUG_InitCurrThread();
740 break;
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");
761 break;
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,
769 de->dwThreadId,
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");
776 break;
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" );
791 } while (0);
793 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
794 de->u.CreateProcessInfo.lpBaseOfImage);
796 break;
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");
805 break;
807 /* FIXME: remove break point set on thread startup */
808 DEBUG_DelThread(DEBUG_CurrThread);
809 break;
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");
818 break;
820 /* just in case */
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);
827 break;
829 case LOAD_DLL_DEBUG_EVENT:
830 if (DEBUG_CurrThread == NULL)
832 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
833 break;
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);
845 _strupr(buffer);
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;
854 break;
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);
859 break;
861 case OUTPUT_DEBUG_STRING_EVENT:
862 if (DEBUG_CurrThread == NULL)
864 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
865 break;
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);
875 break;
877 case RIP_EVENT:
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);
881 break;
883 default:
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();
897 #if 1
898 DEBUG_Printf(DBG_CHN_TRACE,
899 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
900 #ifdef __i386__
901 DEBUG_context.Eip, DEBUG_context.EFlags,
902 #else
903 0L, 0L,
904 #endif
905 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
906 #endif
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)
922 DEBUG_EVENT de;
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;
937 #if 1
938 DEBUG_Printf(DBG_CHN_TRACE,
939 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
940 #ifdef __i386__
941 DEBUG_context.Eip, DEBUG_context.EFlags,
942 #else
943 0L, 0L,
944 #endif
945 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
946 #endif
949 static DWORD DEBUG_MainLoop(void)
951 DEBUG_EVENT de;
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");
964 DEBUG_WalkModules();
965 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
966 DEBUG_WalkThreads();
968 else
970 DEBUG_InteractiveP = TRUE;
971 DEBUG_Parser(NULL);
973 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
975 return 0;
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
989 * while GUI:s don't
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);
996 return FALSE;
998 DEBUG_CurrPid = info.dwProcessId;
999 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
1001 return TRUE;
1004 void DEBUG_Run(const char* args)
1006 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
1007 const char* pgm = (wmod) ? wmod->module_name : "none";
1009 if (args) {
1010 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
1011 } else {
1012 if (!DEBUG_LastCmdLine) {
1013 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
1014 return;
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
1024 * in list
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();
1035 return FALSE;
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" );
1050 return 1;
1053 int main(int argc, char** argv)
1055 DWORD retv = 0;
1056 unsigned gdb_flags = 0;
1058 /* Initialize the type handling stuff. */
1059 DEBUG_InitTypes();
1060 DEBUG_InitCVDataTypes();
1062 /* Initialize internal vars (types must have been initialized before) */
1063 if (!DEBUG_IntVarsRW(TRUE)) return -1;
1065 /* parse options */
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;
1076 argc--; argv++;
1077 continue;
1079 if (!strcmp( argv[1], "--gdb" ))
1081 if (local_mode != none_mode) return DEBUG_Usage();
1082 local_mode = gdb_mode;
1083 argc--; argv++;
1084 continue;
1086 if (strcmp( argv[1], "--no-start") == 0 && local_mode == gdb_mode)
1088 gdb_flags |= 1;
1089 argc--; argv++; /* as we don't use argv[0] */
1090 continue;
1092 if (strcmp(argv[1], "--with-xterm") == 0 && local_mode == gdb_mode)
1094 gdb_flags |= 2;
1095 argc--; argv++; /* as we don't use argv[0] */
1096 continue;
1098 if (!strcmp( argv[1], "--debugmsg" ) && argv[2])
1100 wine_dbg_parse_options( argv[2] );
1101 argc -= 2;
1102 argv += 2;
1103 continue;
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)
1113 char* ptr;
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))
1118 DEBUG_CurrPid = 0;
1121 /* try the form <myself> pid evt (Win32 JIT debugger) */
1122 if (DEBUG_CurrPid == 0 && argc == 3)
1124 HANDLE hEvent;
1125 DWORD pid;
1126 char* ptr;
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 */
1134 SetEvent(hEvent);
1135 goto leave;
1137 if (!SetEvent(hEvent))
1139 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
1140 goto leave;
1142 CloseHandle(hEvent);
1143 DEBUG_CurrPid = pid;
1147 if (DEBUG_CurrPid == 0 && argc > 1)
1149 int i, len;
1150 LPSTR cmdLine;
1152 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
1153 cmdLine[0] = '\0';
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);
1167 goto leave;
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);
1182 leave:
1183 return retv;
1185 oom_leave:
1186 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
1187 goto leave;