- Minor API file update.
[wine.git] / debugger / winedbg.c
blob26b29111deb8b4fda718939625b360c174e5aaf3
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 "ntddk.h"
29 #include "thread.h"
30 #include "wincon.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "msvcrt/excpt.h"
35 #include "winreg.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 enum exit_mode DEBUG_ExitMode = EXIT_CONTINUE;
44 int curr_frame = 0;
45 static char* DEBUG_LastCmdLine = NULL;
47 static DBG_PROCESS* DEBUG_ProcessList = NULL;
48 static int automatic_mode;
49 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
51 void DEBUG_Output(int chn, const char* buffer, int len)
53 if (DBG_IVAR(ConChannelMask) & chn)
54 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
55 if (DBG_IVAR(StdChannelMask) & chn)
56 fwrite(buffer, len, 1, stderr);
59 int DEBUG_Printf(int chn, const char* format, ...)
61 static char buf[4*1024];
62 va_list valist;
63 int len;
65 va_start(valist, format);
66 len = vsnprintf(buf, sizeof(buf), format, valist);
67 va_end(valist);
69 if (len <= -1 || len >= sizeof(buf)) {
70 len = sizeof(buf) - 1;
71 buf[len] = 0;
72 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
74 DEBUG_Output(chn, buf, len);
75 return len;
78 static BOOL DEBUG_IntVarsRW(int read)
80 HKEY hkey;
81 DWORD type = REG_DWORD;
82 DWORD val;
83 DWORD count = sizeof(val);
84 int i;
85 DBG_INTVAR* div = DEBUG_IntVars;
87 if (read) {
88 /* initializes internal vars table */
89 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
90 div->val = _val; div->name = #_var; div->pval = _ref; \
91 div->type = DEBUG_GetBasicType(_typ); div++;
92 #include "intvar.h"
93 #undef INTERNAL_VAR
96 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
97 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
98 * so don't use it */
99 fprintf(stderr, "Cannot create WineDbg key in registry\n");
100 return FALSE;
103 for (i = 0; i < DBG_IV_LAST; i++) {
104 if (read) {
105 if (!DEBUG_IntVars[i].pval) {
106 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
107 &type, (LPSTR)&val, &count))
108 DEBUG_IntVars[i].val = val;
109 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
110 } else {
111 *DEBUG_IntVars[i].pval = 0;
113 } else {
114 /* FIXME: type should be infered from basic type -if any- of intvar */
115 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
116 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
117 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
120 RegCloseKey(hkey);
121 return TRUE;
124 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
126 int i;
128 for (i = 0; i < DBG_IV_LAST; i++) {
129 if (!strcmp(DEBUG_IntVars[i].name, name))
130 return &DEBUG_IntVars[i];
132 return NULL;
135 static WINE_EXCEPTION_FILTER(wine_dbg)
137 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
138 DEBUG_ExternalDebugger();
139 return EXCEPTION_EXECUTE_HANDLER;
142 DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
144 DBG_PROCESS* p;
146 for (p = DEBUG_ProcessList; p; p = p->next)
147 if (p->pid == pid) break;
148 return p;
151 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h, const char* imageName)
153 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
154 if (!p)
155 return NULL;
156 p->handle = h;
157 p->pid = pid;
158 p->imageName = imageName ? DBG_strdup(imageName) : NULL;
159 p->threads = NULL;
160 p->num_threads = 0;
161 p->continue_on_first_exception = FALSE;
162 p->modules = NULL;
163 p->num_modules = 0;
164 p->next_index = 0;
165 p->dbg_hdr_addr = 0;
166 p->delayed_bp = NULL;
167 p->num_delayed_bp = 0;
169 p->next = DEBUG_ProcessList;
170 p->prev = NULL;
171 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
172 DEBUG_ProcessList = p;
173 return p;
176 static void DEBUG_DelThread(DBG_THREAD* p);
178 static void DEBUG_DelProcess(DBG_PROCESS* p)
180 int i;
182 if (p->threads != NULL) {
183 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
184 while (p->threads) DEBUG_DelThread(p->threads);
186 for (i = 0; i < p->num_delayed_bp; i++) {
187 DBG_free(p->delayed_bp[i].name);
189 DBG_free(p->delayed_bp);
190 if (p->prev) p->prev->next = p->next;
191 if (p->next) p->next->prev = p->prev;
192 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
193 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
194 DBG_free((char*)p->imageName);
195 DBG_free(p);
198 static void DEBUG_InitCurrProcess(void)
202 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
204 DWORD sz;
205 *(WCHAR*)buffer = 0;
206 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
209 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
211 LPVOID ad;
212 DWORD sz;
214 if ( addr
215 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
216 && sz == sizeof(ad)
217 && ad
218 && ReadProcessMemory(hp, ad, buffer, size, &sz))
219 return TRUE;
220 *(WCHAR*)buffer = 0;
221 return FALSE;
224 DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
226 DBG_THREAD* t;
228 for (t = p->threads; t; t = t->next)
229 if (t->tid == tid) break;
230 return t;
233 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
234 HANDLE h, LPVOID start, LPVOID teb)
236 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
237 if (!t)
238 return NULL;
240 t->handle = h;
241 t->tid = tid;
242 t->start = start;
243 t->teb = teb;
244 t->process = p;
245 t->wait_for_first_exception = 0;
246 t->exec_mode = EXEC_CONT;
247 t->exec_count = 0;
249 sprintf(t->name, "%08lx", tid);
251 p->num_threads++;
252 t->next = p->threads;
253 t->prev = NULL;
254 if (p->threads) p->threads->prev = t;
255 p->threads = t;
257 return t;
260 static void DEBUG_InitCurrThread(void)
262 if (DEBUG_CurrThread->start) {
263 if (DEBUG_CurrThread->process->num_threads == 1 ||
264 DBG_IVAR(BreakAllThreadsStartup)) {
265 DBG_VALUE value;
267 DEBUG_SetBreakpoints(FALSE);
268 value.type = NULL;
269 value.cookie = DV_TARGET;
270 value.addr.seg = 0;
271 value.addr.off = (DWORD)DEBUG_CurrThread->start;
272 DEBUG_AddBreakpoint(&value, NULL);
273 DEBUG_SetBreakpoints(TRUE);
275 } else {
276 DEBUG_CurrThread->wait_for_first_exception = 1;
280 static void DEBUG_DelThread(DBG_THREAD* t)
282 if (t->prev) t->prev->next = t->next;
283 if (t->next) t->next->prev = t->prev;
284 if (t == t->process->threads) t->process->threads = t->next;
285 t->process->num_threads--;
286 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
287 DBG_free(t);
290 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
292 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0, NULL))) return FALSE;
294 if (!DebugActiveProcess(pid)) {
295 DEBUG_Printf(DBG_CHN_MESG, "Can't attach process %lx: error %ld\n", pid, GetLastError());
296 DEBUG_DelProcess(DEBUG_CurrProcess);
297 DEBUG_CurrProcess = NULL;
298 return FALSE;
300 DEBUG_CurrProcess->continue_on_first_exception = cofe;
301 return TRUE;
304 BOOL DEBUG_Detach(void)
306 /* remove all set breakpoints in debuggee code */
307 DEBUG_SetBreakpoints(FALSE);
308 /* needed for single stepping (ugly).
309 * should this be handled inside the server ??? */
310 #ifdef __i386__
311 DEBUG_context.EFlags &= ~STEP_FLAG;
312 #endif
313 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
314 DebugActiveProcessStop(DEBUG_CurrProcess->pid);
315 DEBUG_DelProcess(DEBUG_CurrProcess);
316 DEBUG_CurrProcess = NULL;
317 /* FIXME: should zero out the symbol table too */
318 return TRUE;
321 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
323 DBG_ADDR addr;
324 int newmode;
326 DEBUG_GetCurrentAddress(&addr);
327 DEBUG_SuspendExecution();
329 if (!is_debug) {
330 if (!addr.seg)
331 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx)", addr.off);
332 else
333 switch(DEBUG_GetSelectorType(addr.seg))
335 case MODE_32:
336 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx)", addr.seg, addr.off);
337 break;
338 case MODE_16:
339 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx)", addr.seg, addr.off);
340 break;
341 case MODE_VM86:
342 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx)", addr.seg, addr.off);
343 break;
344 case MODE_INVALID:
345 DEBUG_Printf(DBG_CHN_MESG, " bad CS (%lx)", addr.seg);
346 break;
348 DEBUG_Printf(DBG_CHN_MESG, ".\n");
351 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
353 if (!force && is_debug &&
354 DEBUG_ShouldContinue(&addr, code,
355 &DEBUG_CurrThread->exec_count))
356 return FALSE;
358 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
359 if (newmode != DEBUG_CurrThread->dbg_mode)
361 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
362 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
363 DEBUG_CurrThread->dbg_mode = newmode;
366 DEBUG_DoDisplay();
368 if (is_debug || force) {
370 * Do a quiet backtrace so that we have an idea of what the situation
371 * is WRT the source files.
373 DEBUG_BackTrace(DEBUG_CurrTid, FALSE);
374 } else {
375 /* This is a real crash, dump some info */
376 DEBUG_InfoRegisters();
377 DEBUG_InfoStack();
378 #ifdef __i386__
379 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
380 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
381 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
382 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
384 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
385 #endif
386 DEBUG_BackTrace(DEBUG_CurrTid, TRUE);
389 if (!is_debug ||
390 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_OVER) ||
391 (DEBUG_CurrThread->exec_mode == EXEC_STEPI_INSTR)) {
393 struct list_id list;
395 /* Show where we crashed */
396 curr_frame = 0;
397 DEBUG_DisassembleInstruction(&addr);
399 /* resets list internal arguments so we can look at source code when needed */
400 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
401 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
403 return TRUE;
406 static void DEBUG_ExceptionEpilog(void)
408 DEBUG_RestartExecution(DEBUG_CurrThread->exec_count);
410 * This will have gotten absorbed into the breakpoint info
411 * if it was used. Otherwise it would have been ignored.
412 * In any case, we don't mess with it any more.
414 if (DEBUG_CurrThread->exec_mode == EXEC_CONT)
415 DEBUG_CurrThread->exec_count = 0;
418 static void DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force)
420 BOOL is_debug = FALSE;
421 THREADNAME_INFO *pThreadName;
422 DBG_THREAD *pThread;
424 assert(DEBUG_CurrThread);
426 DEBUG_ExitMode = EXIT_CONTINUE;
428 switch (rec->ExceptionCode)
430 case EXCEPTION_BREAKPOINT:
431 case EXCEPTION_SINGLE_STEP:
432 is_debug = TRUE;
433 break;
434 case EXCEPTION_NAME_THREAD:
435 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
436 if (pThreadName->dwThreadID == -1)
437 pThread = DEBUG_CurrThread;
438 else
439 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
441 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
442 pThread->name, 9, NULL))
443 DEBUG_Printf (DBG_CHN_MESG,
444 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
445 pThread->tid, pThread->name);
446 return;
449 if (first_chance && !is_debug && !force && !DBG_IVAR(BreakOnFirstChance))
451 /* pass exception to program except for debug exceptions */
452 if (!is_debug) DEBUG_ExitMode = EXIT_PASS;
453 return;
456 if (!is_debug)
458 /* print some infos */
459 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
460 first_chance ? "First chance exception" : "Unhandled exception");
461 switch (rec->ExceptionCode)
463 case EXCEPTION_INT_DIVIDE_BY_ZERO:
464 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
465 break;
466 case EXCEPTION_INT_OVERFLOW:
467 DEBUG_Printf(DBG_CHN_MESG, "overflow");
468 break;
469 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
470 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
471 break;
472 case EXCEPTION_ILLEGAL_INSTRUCTION:
473 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
474 break;
475 case EXCEPTION_STACK_OVERFLOW:
476 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
477 break;
478 case EXCEPTION_PRIV_INSTRUCTION:
479 DEBUG_Printf(DBG_CHN_MESG, "privileged instruction");
480 break;
481 case EXCEPTION_ACCESS_VIOLATION:
482 if (rec->NumberParameters == 2)
483 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
484 rec->ExceptionInformation[0] ? "write" : "read",
485 rec->ExceptionInformation[1]);
486 else
487 DEBUG_Printf(DBG_CHN_MESG, "page fault");
488 break;
489 case EXCEPTION_DATATYPE_MISALIGNMENT:
490 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
491 break;
492 case DBG_CONTROL_C:
493 DEBUG_Printf(DBG_CHN_MESG, "^C");
494 break;
495 case CONTROL_C_EXIT:
496 DEBUG_Printf(DBG_CHN_MESG, "^C");
497 break;
498 case EXCEPTION_CRITICAL_SECTION_WAIT:
500 DBG_ADDR addr;
502 addr.seg = 0;
503 addr.off = rec->ExceptionInformation[0];
505 DEBUG_Printf(DBG_CHN_MESG, "wait failed on critical section ");
506 DEBUG_PrintAddress(&addr, DEBUG_CurrThread->dbg_mode, FALSE);
508 if (!DBG_IVAR(BreakOnCritSectTimeOut))
510 DEBUG_Printf(DBG_CHN_MESG, "\n");
511 return;
513 break;
514 case EXCEPTION_WINE_STUB:
516 char dll[32], name[64];
517 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
518 (char *)rec->ExceptionInformation[0] );
519 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
520 (char *)rec->ExceptionInformation[1] );
521 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
523 break;
524 case EXCEPTION_VM86_INTx:
525 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
526 rec->ExceptionInformation[0]);
527 break;
528 case EXCEPTION_VM86_STI:
529 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
530 break;
531 case EXCEPTION_VM86_PICRETURN:
532 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
533 break;
534 default:
535 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
536 break;
540 #if 0
541 DEBUG_Printf(DBG_CHN_TRACE,
542 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
543 #ifdef __i386__
544 DEBUG_context.Eip, DEBUG_context.EFlags,
545 #else
546 0L, 0L,
547 #endif
548 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
549 #endif
551 if (automatic_mode)
553 DEBUG_ExceptionProlog(is_debug, FALSE, rec->ExceptionCode);
554 DEBUG_ExitMode = EXIT_QUIT;
555 return; /* terminate execution */
558 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
559 DEBUG_interactiveP = TRUE;
560 for (;;)
562 DEBUG_Parser();
563 if (DEBUG_ExitMode == EXIT_QUIT || DEBUG_ExitMode == EXIT_DETACH)
564 break;
565 if (DEBUG_ValidateRegisters()) {
566 if (DEBUG_ExitMode == EXIT_PASS || first_chance)
567 break;
568 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
571 DEBUG_interactiveP = FALSE;
573 DEBUG_ExceptionEpilog();
575 #if 0
576 DEBUG_Printf(DBG_CHN_TRACE,
577 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
578 #ifdef __i386__
579 DEBUG_context.Eip, DEBUG_context.EFlags,
580 #else
581 0L, 0L,
582 #endif
583 DEBUG_CurrThread->exec_mode, DEBUG_CurrThread->exec_count);
584 #endif
587 static void DEBUG_HandleDebugEvent(DEBUG_EVENT* de)
589 char buffer[256];
591 DEBUG_CurrPid = de->dwProcessId;
592 DEBUG_CurrTid = de->dwThreadId;
594 DEBUG_ExitMode = EXIT_CONTINUE;
595 __TRY {
596 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
597 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
598 else
599 DEBUG_CurrThread = NULL;
601 switch (de->dwDebugEventCode) {
602 case EXCEPTION_DEBUG_EVENT:
603 if (!DEBUG_CurrThread) {
604 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
605 de->dwProcessId, de->dwThreadId);
606 break;
609 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
610 de->dwProcessId, de->dwThreadId,
611 de->u.Exception.ExceptionRecord.ExceptionCode);
613 if (DEBUG_CurrProcess->continue_on_first_exception) {
614 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
615 if (!DBG_IVAR(BreakOnAttach)) {
616 break;
620 DEBUG_context.ContextFlags = CONTEXT_CONTROL
621 | CONTEXT_INTEGER
622 #ifdef CONTEXT_SEGMENTS
623 | CONTEXT_SEGMENTS
624 #endif
625 #ifdef CONTEXT_DEBUG_REGISTERS
626 | CONTEXT_DEBUG_REGISTERS
627 #endif
630 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
631 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
632 break;
635 DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
636 de->u.Exception.dwFirstChance,
637 DEBUG_CurrThread->wait_for_first_exception);
638 if (DEBUG_CurrThread) {
639 DEBUG_CurrThread->wait_for_first_exception = 0;
640 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
642 break;
644 case CREATE_THREAD_DEBUG_EVENT:
645 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
646 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
648 if (DEBUG_CurrProcess == NULL) {
649 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
650 break;
652 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
653 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
654 break;
657 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
658 de->dwThreadId,
659 de->u.CreateThread.hThread,
660 de->u.CreateThread.lpStartAddress,
661 de->u.CreateThread.lpThreadLocalBase);
662 if (!DEBUG_CurrThread) {
663 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
664 break;
666 DEBUG_InitCurrThread();
667 break;
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 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
683 if (DEBUG_CurrProcess->handle) {
684 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
685 break;
687 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
688 if (DEBUG_CurrProcess->imageName == NULL)
689 DEBUG_CurrProcess->imageName = DBG_strdup(buffer[0] ? buffer : "<Debugged Process>");
691 } else {
692 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
693 de->u.CreateProcessInfo.hProcess,
694 buffer[0] ? buffer : "<Debugged Process>");
695 if (DEBUG_CurrProcess == NULL) {
696 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
697 break;
701 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
702 de->dwProcessId, de->dwThreadId,
703 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
705 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
706 de->dwThreadId,
707 de->u.CreateProcessInfo.hThread,
708 de->u.CreateProcessInfo.lpStartAddress,
709 de->u.CreateProcessInfo.lpThreadLocalBase);
710 if (!DEBUG_CurrThread) {
711 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
712 break;
715 DEBUG_InitCurrProcess();
716 DEBUG_InitCurrThread();
718 DEBUG_LoadModule32(DEBUG_CurrProcess->imageName, de->u.CreateProcessInfo.hFile,
719 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
721 if (buffer[0]) /* we got a process name */
723 DWORD type;
724 if (!GetBinaryTypeA( buffer, &type ))
726 /* not a Windows binary, assume it's a Unix executable then */
727 char unixname[MAX_PATH];
728 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
729 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
731 DEBUG_ReadExecutableDbgInfo( unixname );
732 break;
736 /* if it is a Windows binary, or an invalid or missing file name,
737 * we use wine itself as the main executable */
738 DEBUG_ReadExecutableDbgInfo( "wine" );
739 break;
741 case EXIT_THREAD_DEBUG_EVENT:
742 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
743 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
745 if (DEBUG_CurrThread == NULL) {
746 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
747 break;
749 /* FIXME: remove break point set on thread startup */
750 DEBUG_DelThread(DEBUG_CurrThread);
751 break;
753 case EXIT_PROCESS_DEBUG_EVENT:
754 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
755 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
757 if (DEBUG_CurrProcess == NULL) {
758 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
759 break;
761 /* just in case */
762 DEBUG_SetBreakpoints(FALSE);
763 /* kill last thread */
764 DEBUG_DelThread(DEBUG_CurrProcess->threads);
765 DEBUG_DelProcess(DEBUG_CurrProcess);
767 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
768 break;
770 case LOAD_DLL_DEBUG_EVENT:
771 if (DEBUG_CurrThread == NULL) {
772 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
773 break;
775 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
776 DEBUG_CurrThread->process->handle,
777 de->u.LoadDll.lpImageName);
779 /* FIXME unicode: de->u.LoadDll.fUnicode */
780 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
781 de->dwProcessId, de->dwThreadId,
782 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
783 de->u.LoadDll.dwDebugInfoFileOffset,
784 de->u.LoadDll.nDebugInfoSize);
785 _strupr(buffer);
786 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
787 DEBUG_CheckDelayedBP();
788 if (DBG_IVAR(BreakOnDllLoad)) {
789 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
790 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
791 DEBUG_Parser();
793 break;
795 case UNLOAD_DLL_DEBUG_EVENT:
796 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
797 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
798 break;
800 case OUTPUT_DEBUG_STRING_EVENT:
801 if (DEBUG_CurrThread == NULL) {
802 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
803 break;
806 DEBUG_ProcessGetString(buffer, sizeof(buffer),
807 DEBUG_CurrThread->process->handle,
808 de->u.DebugString.lpDebugStringData);
810 /* FIXME unicode de->u.DebugString.fUnicode ? */
811 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
812 de->dwProcessId, de->dwThreadId, buffer);
813 break;
815 case RIP_EVENT:
816 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
817 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
818 de->u.RipInfo.dwType);
819 break;
821 default:
822 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
823 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
826 } __EXCEPT(wine_dbg) {
827 DEBUG_ExitMode = EXIT_CONTINUE;
829 __ENDTRY;
832 static DWORD DEBUG_MainLoop(void)
834 DEBUG_EVENT de;
836 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
838 while (DEBUG_ExitMode == EXIT_CONTINUE)
840 /* wait until we get at least one loaded process */
841 while (!DEBUG_ProcessList)
843 DEBUG_Parser();
844 if (DEBUG_ExitMode == EXIT_CONTINUE || DEBUG_ExitMode == EXIT_QUIT) break;
846 if (DEBUG_ExitMode != EXIT_CONTINUE) break;
848 while ((DEBUG_ExitMode == EXIT_CONTINUE || DEBUG_ExitMode == EXIT_PASS) &&
849 DEBUG_ProcessList &&
850 WaitForDebugEvent(&de, INFINITE))
852 DEBUG_HandleDebugEvent(&de);
853 ContinueDebugEvent(de.dwProcessId, de.dwThreadId,
854 (DEBUG_ExitMode == EXIT_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE);
856 if (DEBUG_ExitMode == EXIT_DETACH && DEBUG_Detach())
858 /* DEBUG_ExitMode = EXIT_CONTINUE; */
859 /* FIXME: as we don't have a simple way to zero out the process symbol table
860 * we simply quit the debugger on detach...
862 DEBUG_ExitMode = EXIT_QUIT;
866 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
868 return 0;
871 static DWORD DEBUG_AutoMode(void)
873 DEBUG_EVENT de;
875 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
877 while ((DEBUG_ExitMode == EXIT_CONTINUE || DEBUG_ExitMode == EXIT_PASS) &&
878 DEBUG_ProcessList &&
879 WaitForDebugEvent(&de, INFINITE))
881 DEBUG_HandleDebugEvent(&de);
882 ContinueDebugEvent(de.dwProcessId, de.dwThreadId,
883 (DEBUG_ExitMode == EXIT_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE);
885 /* print some extra information */
886 DEBUG_Printf(DBG_CHN_MESG, "Modules:\n");
887 DEBUG_WalkModules();
888 DEBUG_Printf(DBG_CHN_MESG, "Threads:\n");
889 DEBUG_WalkThreads();
891 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
892 return 0;
895 static BOOL DEBUG_Start(LPSTR cmdLine)
897 PROCESS_INFORMATION info;
898 STARTUPINFOA startup;
900 memset(&startup, 0, sizeof(startup));
901 startup.cb = sizeof(startup);
902 startup.dwFlags = STARTF_USESHOWWINDOW;
903 startup.wShowWindow = SW_SHOWNORMAL;
905 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
906 FALSE, DEBUG_PROCESS|DETACHED_PROCESS, NULL, NULL, &startup, &info)) {
907 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
908 return FALSE;
910 DEBUG_CurrPid = info.dwProcessId;
911 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0, NULL))) return FALSE;
913 return TRUE;
916 void DEBUG_Run(const char* args)
918 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
919 const char* pgm = (wmod) ? wmod->module_name : "none";
921 if (args) {
922 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
923 } else {
924 if (!DEBUG_LastCmdLine) {
925 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
926 return;
928 DEBUG_Start(DEBUG_LastCmdLine);
932 static void DEBUG_InitConsole(void)
934 COORD c;
935 SMALL_RECT sr;
936 DWORD mode;
938 /* keep it as a cuiexe for now, so that Wine won't touch the Unix stdin,
939 * stdout and stderr streams
941 if (DBG_IVAR(UseXTerm))
943 FreeConsole();
944 AllocConsole();
947 /* this would be nicer for output */
948 c.X = 132;
949 c.Y = 500;
950 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c);
952 /* sets the console's window width accordingly */
953 sr.Left = 0;
954 sr.Top = 0;
955 sr.Right = c.X - 1;
956 sr.Bottom = 50;
957 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &sr);
959 /* put the line editing mode with the nice emacs features (FIXME: could be triggered by a IVAR) */
960 if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode))
961 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode | WINE_ENABLE_LINE_INPUT_EMACS);
964 int DEBUG_main(int argc, char** argv)
966 DWORD retv = 0;
968 /* Initialize the type handling stuff. */
969 DEBUG_InitTypes();
970 DEBUG_InitCVDataTypes();
972 /* Initialize internal vars (types must have been initialized before) */
973 if (!DEBUG_IntVarsRW(TRUE)) return -1;
975 if (argc > 1 && !strcmp( argv[1], "--auto" ))
977 argc--;
978 argv++;
979 automatic_mode = 1;
980 /* force some internal variables */
981 DBG_IVAR(UseXTerm) = 0;
982 DBG_IVAR(BreakOnDllLoad) = 0;
983 DBG_IVAR(ConChannelMask) = 0;
984 DBG_IVAR(StdChannelMask) = DBG_CHN_MESG;
987 DEBUG_InitConsole();
989 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... ");
991 if (argc == 3) {
992 HANDLE hEvent;
993 DWORD pid;
995 if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) {
996 if (!DEBUG_Attach(pid, TRUE)) {
997 /* don't care about result */
998 SetEvent(hEvent);
999 goto leave;
1001 if (!SetEvent(hEvent)) {
1002 DEBUG_Printf(DBG_CHN_ERR, "Invalid event handle: %p\n", hEvent);
1003 goto leave;
1005 CloseHandle(hEvent);
1006 DEBUG_CurrPid = pid;
1010 if (DEBUG_CurrPid == 0 && argc > 1) {
1011 int i, len;
1012 LPSTR cmdLine;
1014 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
1015 cmdLine[0] = '\0';
1017 for (i = 1; i < argc; i++) {
1018 len += strlen(argv[i]) + 1;
1019 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
1020 strcat(cmdLine, argv[i]);
1021 cmdLine[len - 2] = ' ';
1022 cmdLine[len - 1] = '\0';
1025 if (!DEBUG_Start(cmdLine)) {
1026 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
1027 goto leave;
1029 DBG_free(DEBUG_LastCmdLine);
1030 DEBUG_LastCmdLine = cmdLine;
1033 if (automatic_mode)
1035 retv = DEBUG_AutoMode();
1036 /* don't save modified variables in auto mode */
1038 else
1040 retv = DEBUG_MainLoop();
1041 /* saves modified variables */
1042 DEBUG_IntVarsRW(FALSE);
1045 leave:
1046 return retv;
1048 oom_leave:
1049 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
1050 goto leave;