Some spelling fixes.
[wine/testsucceed.git] / programs / winedbg / winedbg.c
blob962d4c89bc7be4446b6ff5db68b1fd6fef46ddfe
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 "winternl.h"
29 #include "wincon.h"
30 #include "winreg.h"
31 #include "wingdi.h"
32 #include "winuser.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;
367 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
369 if (DEBUG_CurrProcess) DEBUG_InteractiveP = TRUE;
371 return 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 ??? */
380 #ifdef __i386__
381 DEBUG_context.EFlags &= ~STEP_FLAG;
382 #endif
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 */
387 return TRUE;
390 static BOOL DEBUG_FetchContext(void)
392 DEBUG_context.ContextFlags = CONTEXT_CONTROL
393 | CONTEXT_INTEGER
394 #ifdef CONTEXT_SEGMENTS
395 | CONTEXT_SEGMENTS
396 #endif
397 #ifdef CONTEXT_DEBUG_REGISTERS
398 | CONTEXT_DEBUG_REGISTERS
399 #endif
401 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context))
403 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
404 return FALSE;
406 return TRUE;
409 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
411 DBG_ADDR addr;
412 int newmode;
414 DEBUG_InException = TRUE;
415 DEBUG_GetCurrentAddress(&addr);
416 DEBUG_SuspendExecution();
418 if (!is_debug)
420 if (!addr.seg)
421 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
422 else
423 switch(DEBUG_GetSelectorType(addr.seg))
425 case MODE_32:
426 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
427 break;
428 case MODE_16:
429 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
430 break;
431 case MODE_VM86:
432 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
433 break;
434 case MODE_INVALID:
435 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
436 break;
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))
446 return FALSE;
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;
456 DEBUG_DoDisplay();
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);
464 } else {
465 /* This is a real crash, dump some info */
466 DEBUG_InfoRegisters(&DEBUG_context);
467 DEBUG_InfoStack();
468 #ifdef __i386__
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);
475 #endif
476 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
479 if (!is_debug ||
480 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_OVER) ||
481 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_INSTR)) {
483 struct list_id list;
485 /* Show where we crashed */
486 curr_frame = 0;
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);
493 return TRUE;
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;
513 DBG_THREAD *pThread;
515 assert(DEBUG_CurrThread);
517 switch (rec->ExceptionCode)
519 case EXCEPTION_BREAKPOINT:
520 case EXCEPTION_SINGLE_STEP:
521 is_debug = TRUE;
522 break;
523 case EXCEPTION_NAME_THREAD:
524 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
525 if (pThreadName->dwThreadID == -1)
526 pThread = DEBUG_CurrThread;
527 else
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);
535 return FALSE;
538 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
540 /* pass exception to program except for debug exceptions */
541 return FALSE;
544 if (!is_debug)
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");
553 break;
554 case EXCEPTION_INT_OVERFLOW:
555 DEBUG_Printf(DBG_CHN_MESG, "overflow");
556 break;
557 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
558 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
559 break;
560 case EXCEPTION_ILLEGAL_INSTRUCTION:
561 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
562 break;
563 case EXCEPTION_STACK_OVERFLOW:
564 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
565 break;
566 case EXCEPTION_PRIV_INSTRUCTION:
567 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
568 break;
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]);
574 else
575 DEBUG_Printf(DBG_CHN_MESG, "page fault");
576 break;
577 case EXCEPTION_DATATYPE_MISALIGNMENT:
578 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
579 break;
580 case DBG_CONTROL_C:
581 DEBUG_Printf(DBG_CHN_MESG, "^C");
582 break;
583 case CONTROL_C_EXIT:
584 DEBUG_Printf(DBG_CHN_MESG, "^C");
585 break;
586 case STATUS_POSSIBLE_DEADLOCK:
588 DBG_ADDR addr;
590 addr.seg = 0;
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");
599 return FALSE;
601 break;
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 );
611 break;
612 case EXCEPTION_WINE_ASSERTION:
613 DEBUG_Printf(DBG_CHN_MESG, "assertion failed");
614 break;
615 case EXCEPTION_VM86_INTx:
616 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
617 rec->ExceptionInformation[0]);
618 break;
619 case EXCEPTION_VM86_STI:
620 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
621 break;
622 case EXCEPTION_VM86_PICRETURN:
623 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
624 break;
625 case EXCEPTION_FLT_DENORMAL_OPERAND:
626 DEBUG_Printf(DBG_CHN_MESG, "denormal float operand");
627 break;
628 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
629 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
630 break;
631 case EXCEPTION_FLT_INEXACT_RESULT:
632 DEBUG_Printf(DBG_CHN_MESG, "inexact float result");
633 break;
634 case EXCEPTION_FLT_INVALID_OPERATION:
635 DEBUG_Printf(DBG_CHN_MESG, "invalid float operation");
636 break;
637 case EXCEPTION_FLT_OVERFLOW:
638 DEBUG_Printf(DBG_CHN_MESG, "floating pointer overflow");
639 break;
640 case EXCEPTION_FLT_UNDERFLOW:
641 DEBUG_Printf(DBG_CHN_MESG, "floating pointer underflow");
642 break;
643 case EXCEPTION_FLT_STACK_CHECK:
644 DEBUG_Printf(DBG_CHN_MESG, "floating point stack check");
645 break;
646 default:
647 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
648 break;
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;
662 return TRUE;
664 DEBUG_ExceptionEpilog();
666 return FALSE;
669 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de)
671 char buffer[256];
672 BOOL ret = FALSE;
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);
679 else
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);
689 break;
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);
713 break;
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");
722 break;
724 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL)
726 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
727 break;
730 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
731 de->dwThreadId,
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");
738 break;
740 DEBUG_InitCurrThread();
741 break;
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");
762 break;
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,
770 de->dwThreadId,
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");
777 break;
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" );
792 } while (0);
794 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
795 de->u.CreateProcessInfo.lpBaseOfImage);
797 break;
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");
806 break;
808 /* FIXME: remove break point set on thread startup */
809 DEBUG_DelThread(DEBUG_CurrThread);
810 break;
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");
819 break;
821 /* just in case */
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);
828 break;
830 case LOAD_DLL_DEBUG_EVENT:
831 if (DEBUG_CurrThread == NULL)
833 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
834 break;
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);
846 _strupr(buffer);
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();
855 break;
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);
860 break;
862 case OUTPUT_DEBUG_STRING_EVENT:
863 if (DEBUG_CurrThread == NULL)
865 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
866 break;
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);
876 break;
878 case RIP_EVENT:
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);
882 break;
884 default:
885 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
886 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
889 return ret;
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;
934 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
936 if (!DEBUG_CurrProcess) return;
937 DEBUG_InteractiveP = TRUE;
938 #if 1
939 DEBUG_Printf(DBG_CHN_TRACE,
940 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
941 #ifdef __i386__
942 DEBUG_context.Eip, DEBUG_context.EFlags,
943 #else
944 0L, 0L,
945 #endif
946 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
947 #endif
950 static DWORD DEBUG_MainLoop(void)
952 DEBUG_EVENT de;
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");
966 DEBUG_WalkModules();
967 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
968 DEBUG_WalkThreads();
970 else
972 DEBUG_InteractiveP = TRUE;
973 DEBUG_Parser(NULL);
975 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
977 return 0;
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
991 * while GUI:s don't
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);
998 return FALSE;
1000 DEBUG_CurrPid = info.dwProcessId;
1001 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
1003 return TRUE;
1006 void DEBUG_Run(const char* args)
1008 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
1009 const char* pgm = (wmod) ? wmod->module_name : "none";
1011 if (args) {
1012 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
1013 } else {
1014 if (!DEBUG_LastCmdLine) {
1015 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
1016 return;
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
1026 * in list
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();
1037 return FALSE;
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" );
1052 return 1;
1055 int main(int argc, char** argv)
1057 DWORD retv = 0;
1058 unsigned gdb_flags = 0;
1060 /* Initialize the type handling stuff. */
1061 DEBUG_InitTypes();
1062 DEBUG_InitCVDataTypes();
1064 /* Initialize internal vars (types must have been initialized before) */
1065 if (!DEBUG_IntVarsRW(TRUE)) return -1;
1067 /* parse options */
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;
1078 argc--; argv++;
1079 continue;
1081 if (!strcmp( argv[1], "--gdb" ))
1083 if (local_mode != none_mode) return DEBUG_Usage();
1084 local_mode = gdb_mode;
1085 argc--; argv++;
1086 continue;
1088 if (strcmp( argv[1], "--no-start") == 0 && local_mode == gdb_mode)
1090 gdb_flags |= 1;
1091 argc--; argv++; /* as we don't use argv[0] */
1092 continue;
1094 if (strcmp(argv[1], "--with-xterm") == 0 && local_mode == gdb_mode)
1096 gdb_flags |= 2;
1097 argc--; argv++; /* as we don't use argv[0] */
1098 continue;
1100 if (!strcmp( argv[1], "--debugmsg" ) && argv[2])
1102 wine_dbg_parse_options( argv[2] );
1103 argc -= 2;
1104 argv += 2;
1105 continue;
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)
1115 char* ptr;
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))
1120 DEBUG_CurrPid = 0;
1123 /* try the form <myself> pid evt (Win32 JIT debugger) */
1124 if (DEBUG_CurrPid == 0 && argc == 3)
1126 HANDLE hEvent;
1127 DWORD pid;
1128 char* ptr;
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 */
1136 SetEvent(hEvent);
1137 goto leave;
1139 if (!SetEvent(hEvent))
1141 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
1142 goto leave;
1144 CloseHandle(hEvent);
1145 DEBUG_CurrPid = pid;
1149 if (DEBUG_CurrPid == 0 && argc > 1)
1151 int i, len;
1152 LPSTR cmdLine;
1154 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
1155 cmdLine[0] = '\0';
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);
1169 goto leave;
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);
1184 leave:
1185 return retv;
1187 oom_leave:
1188 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
1189 goto leave;