RegSetValueExA/W: fixed REG_SZ string length handling for Win95.
[wine/multimedia.git] / debugger / winedbg.c
blob7488a83b49cbf66e21902ab56ff846e7eb2063bc
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /* Wine internal debugger
4 * Interface to Windows debugger API
5 * Eric Pouech (c) 2000
6 */
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "debugger.h"
14 #include "thread.h"
15 #include "file.h"
16 #include "wincon.h"
17 #include "wingdi.h"
18 #include "winuser.h"
20 #include "winreg.h"
22 #ifdef DBG_need_heap
23 HANDLE dbg_heap = 0;
24 #endif
26 DBG_PROCESS* DEBUG_CurrProcess = NULL;
27 DBG_THREAD* DEBUG_CurrThread = NULL;
28 DWORD DEBUG_CurrTid;
29 DWORD DEBUG_CurrPid;
30 CONTEXT DEBUG_context;
31 int curr_frame = 0;
32 static char* DEBUG_LastCmdLine = NULL;
34 static DBG_PROCESS* DEBUG_ProcessList = NULL;
35 DBG_INTVAR DEBUG_IntVars[DBG_IV_LAST];
37 void DEBUG_Output(int chn, const char* buffer, int len)
39 if (DBG_IVAR(ConChannelMask) & chn)
40 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL);
41 if (DBG_IVAR(StdChannelMask) & chn)
42 fwrite(buffer, len, 1, stderr);
45 int DEBUG_Printf(int chn, const char* format, ...)
47 char buf[1024];
48 va_list valist;
49 int len;
51 va_start(valist, format);
52 len = vsnprintf(buf, sizeof(buf), format, valist);
53 va_end(valist);
55 if (len <= -1) {
56 len = sizeof(buf) - 1;
57 buf[len] = 0;
58 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
60 DEBUG_Output(chn, buf, len);
61 return len;
64 static BOOL DEBUG_IntVarsRW(int read)
66 HKEY hkey;
67 DWORD type = REG_DWORD;
68 DWORD val;
69 DWORD count = sizeof(val);
70 int i;
71 DBG_INTVAR* div = DEBUG_IntVars;
73 if (read) {
74 /* initializes internal vars table */
75 #define INTERNAL_VAR(_var,_val,_ref,_typ) \
76 div->val = _val; div->name = #_var; div->pval = _ref; \
77 div->type = _typ; div++;
78 #include "intvar.h"
79 #undef INTERNAL_VAR
82 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey)) {
83 /* since the IVars are not yet setup, DEBUG_Printf doesn't work,
84 * so don't use it */
85 fprintf(stderr, "Cannot create WineDbg key in registry\n");
86 return FALSE;
89 for (i = 0; i < DBG_IV_LAST; i++) {
90 if (read) {
91 if (!DEBUG_IntVars[i].pval) {
92 if (!RegQueryValueEx(hkey, DEBUG_IntVars[i].name, 0,
93 &type, (LPSTR)&val, &count))
94 DEBUG_IntVars[i].val = val;
95 DEBUG_IntVars[i].pval = &DEBUG_IntVars[i].val;
96 } else {
97 *DEBUG_IntVars[i].pval = 0;
99 } else {
100 /* FIXME: type should be infered from basic type -if any- of intvar */
101 if (DEBUG_IntVars[i].pval == &DEBUG_IntVars[i].val)
102 RegSetValueEx(hkey, DEBUG_IntVars[i].name, 0,
103 type, (LPCVOID)DEBUG_IntVars[i].pval, count);
106 RegCloseKey(hkey);
107 return TRUE;
110 DBG_INTVAR* DEBUG_GetIntVar(const char* name)
112 int i;
114 for (i = 0; i < DBG_IV_LAST; i++) {
115 if (!strcmp(DEBUG_IntVars[i].name, name))
116 return &DEBUG_IntVars[i];
118 return NULL;
121 static WINE_EXCEPTION_FILTER(wine_dbg)
123 DEBUG_Printf(DBG_CHN_MESG, "\nwine_dbg: Exception (%lx) inside debugger, continuing...\n", GetExceptionCode());
124 DEBUG_ExternalDebugger();
125 return EXCEPTION_EXECUTE_HANDLER;
128 static DBG_PROCESS* DEBUG_GetProcess(DWORD pid)
130 DBG_PROCESS* p;
132 for (p = DEBUG_ProcessList; p; p = p->next)
133 if (p->pid == pid) break;
134 return p;
137 static DBG_PROCESS* DEBUG_AddProcess(DWORD pid, HANDLE h)
139 DBG_PROCESS* p = DBG_alloc(sizeof(DBG_PROCESS));
140 if (!p)
141 return NULL;
142 p->handle = h;
143 p->pid = pid;
144 p->threads = NULL;
145 p->num_threads = 0;
146 p->continue_on_first_exception = FALSE;
147 p->modules = NULL;
148 p->num_modules = 0;
149 p->next_index = 0;
150 p->dbg_hdr_addr = 0;
152 p->next = DEBUG_ProcessList;
153 p->prev = NULL;
154 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
155 DEBUG_ProcessList = p;
156 return p;
159 static void DEBUG_DelThread(DBG_THREAD* p);
161 static void DEBUG_DelProcess(DBG_PROCESS* p)
163 if (p->threads != NULL) {
164 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
165 while (p->threads) DEBUG_DelThread(p->threads);
167 if (p->prev) p->prev->next = p->next;
168 if (p->next) p->next->prev = p->prev;
169 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
170 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
171 DBG_free(p);
174 static void DEBUG_InitCurrProcess(void)
178 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
180 DWORD sz;
181 *(WCHAR*)buffer = 0;
182 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
185 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
187 LPVOID ad;
188 DWORD sz;
190 if ( addr
191 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
192 && sz == sizeof(ad)
193 && ad
194 && ReadProcessMemory(hp, ad, buffer, size, &sz))
195 return TRUE;
196 *(WCHAR*)buffer = 0;
197 return FALSE;
200 static DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
202 DBG_THREAD* t;
204 for (t = p->threads; t; t = t->next)
205 if (t->tid == tid) break;
206 return t;
209 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
210 HANDLE h, LPVOID start, LPVOID teb)
212 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
213 if (!t)
214 return NULL;
216 t->handle = h;
217 t->tid = tid;
218 t->start = start;
219 t->teb = teb;
220 t->process = p;
221 t->wait_for_first_exception = 0;
222 t->dbg_exec_mode = EXEC_CONT;
223 t->dbg_exec_count = 0;
225 p->num_threads++;
226 t->next = p->threads;
227 t->prev = NULL;
228 if (p->threads) p->threads->prev = t;
229 p->threads = t;
231 return t;
234 static void DEBUG_InitCurrThread(void)
236 if (DEBUG_CurrThread->start) {
237 if (DEBUG_CurrThread->process->num_threads == 1 ||
238 DBG_IVAR(BreakAllThreadsStartup)) {
239 DBG_VALUE value;
241 DEBUG_SetBreakpoints(FALSE);
242 value.type = NULL;
243 value.cookie = DV_TARGET;
244 value.addr.seg = 0;
245 value.addr.off = (DWORD)DEBUG_CurrThread->start;
246 DEBUG_AddBreakpoint(&value, NULL);
247 DEBUG_SetBreakpoints(TRUE);
249 } else {
250 DEBUG_CurrThread->wait_for_first_exception = 1;
254 static void DEBUG_DelThread(DBG_THREAD* t)
256 if (t->prev) t->prev->next = t->next;
257 if (t->next) t->next->prev = t->prev;
258 if (t == t->process->threads) t->process->threads = t->next;
259 t->process->num_threads--;
260 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
261 DBG_free(t);
264 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
266 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) return FALSE;
268 if (!DebugActiveProcess(pid)) {
269 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
270 pid, GetLastError());
271 return FALSE;
273 DEBUG_CurrProcess->continue_on_first_exception = cofe;
274 return TRUE;
277 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
279 DBG_ADDR addr;
280 int newmode;
282 DEBUG_GetCurrentAddress(&addr);
283 DEBUG_SuspendExecution();
285 if (!is_debug) {
286 if (!addr.seg)
287 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx).\n", addr.off);
288 else
289 switch(DEBUG_GetSelectorType(addr.seg))
291 case MODE_32:
292 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (%04lx:%08lx).\n", addr.seg, addr.off);
293 break;
294 case MODE_16:
295 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04lx:%04lx).\n", addr.seg, addr.off);
296 break;
297 case MODE_VM86:
298 DEBUG_Printf(DBG_CHN_MESG, " in vm86 code (%04lx:%04lx).\n", addr.seg, addr.off);
299 break;
300 case MODE_INVALID:
301 DEBUG_Printf(DBG_CHN_MESG, "bad CS (%lx)\n", addr.seg);
302 break;
306 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
308 if (!force && is_debug &&
309 DEBUG_ShouldContinue(&addr,
310 code,
311 DEBUG_CurrThread->dbg_exec_mode,
312 &DEBUG_CurrThread->dbg_exec_count))
313 return FALSE;
315 if ((newmode = DEBUG_GetSelectorType(addr.seg)) == MODE_INVALID) newmode = MODE_32;
316 if (newmode != DEBUG_CurrThread->dbg_mode)
318 static const char * const names[] = { "???", "16-bit", "32-bit", "vm86" };
319 DEBUG_Printf(DBG_CHN_MESG,"In %s mode.\n", names[newmode] );
320 DEBUG_CurrThread->dbg_mode = newmode;
323 DEBUG_DoDisplay();
325 if (is_debug || force) {
327 * Do a quiet backtrace so that we have an idea of what the situation
328 * is WRT the source files.
330 DEBUG_BackTrace(FALSE);
331 } else {
332 /* This is a real crash, dump some info */
333 DEBUG_InfoRegisters();
334 DEBUG_InfoStack();
335 #ifdef __i386__
336 if (DEBUG_CurrThread->dbg_mode == MODE_16) {
337 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
338 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
339 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
341 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
342 #endif
343 DEBUG_BackTrace(TRUE);
346 if (!is_debug ||
347 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) ||
348 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) {
350 struct list_id list;
352 /* Show where we crashed */
353 curr_frame = 0;
354 DEBUG_DisassembleInstruction(&addr);
356 /* resets list internal arguments so we can look at source code when needed */
357 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
358 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
360 return TRUE;
363 static DWORD DEBUG_ExceptionEpilog(void)
365 DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode,
366 DEBUG_CurrThread->dbg_exec_count);
368 * This will have gotten absorbed into the breakpoint info
369 * if it was used. Otherwise it would have been ignored.
370 * In any case, we don't mess with it any more.
372 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS)
373 DEBUG_CurrThread->dbg_exec_count = 0;
375 return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
378 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont)
380 BOOL is_debug = FALSE;
381 BOOL ret = TRUE;
382 THREADNAME_INFO *pThreadName;
383 DBG_THREAD *pThread;
386 *cont = DBG_CONTINUE;
388 switch (rec->ExceptionCode)
390 case EXCEPTION_BREAKPOINT:
391 case EXCEPTION_SINGLE_STEP:
392 is_debug = TRUE;
393 break;
394 case EXCEPTION_NAME_THREAD:
395 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
396 if (pThreadName->dwThreadID == -1)
397 pThread = DEBUG_CurrThread;
398 else
399 pThread = DEBUG_GetThread(DEBUG_CurrProcess, pThreadName->dwThreadID);
401 if (ReadProcessMemory(DEBUG_CurrThread->process->handle, pThreadName->szName,
402 pThread->name, 9, NULL))
403 DEBUG_Printf (DBG_CHN_MESG,
404 "Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
405 pThread->tid, pThread->name);
406 return TRUE;
409 if (first_chance && !force && !DBG_IVAR(BreakOnFirstChance))
411 /* pass exception to program except for debug exceptions */
412 *cont = is_debug ? DBG_CONTINUE : DBG_EXCEPTION_NOT_HANDLED;
413 return TRUE;
416 if (!is_debug)
418 /* print some infos */
419 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
420 first_chance ? "First chance exception" : "Unhandled exception");
421 switch (rec->ExceptionCode)
423 case EXCEPTION_INT_DIVIDE_BY_ZERO:
424 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
425 break;
426 case EXCEPTION_INT_OVERFLOW:
427 DEBUG_Printf(DBG_CHN_MESG, "overflow");
428 break;
429 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
430 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
431 break;
432 case EXCEPTION_ILLEGAL_INSTRUCTION:
433 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
434 break;
435 case EXCEPTION_STACK_OVERFLOW:
436 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
437 break;
438 case EXCEPTION_PRIV_INSTRUCTION:
439 DEBUG_Printf(DBG_CHN_MESG, "priviledged instruction");
440 break;
441 case EXCEPTION_ACCESS_VIOLATION:
442 if (rec->NumberParameters == 2)
443 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
444 rec->ExceptionInformation[0] ? "write" : "read",
445 rec->ExceptionInformation[1]);
446 else
447 DEBUG_Printf(DBG_CHN_MESG, "page fault");
448 break;
449 case EXCEPTION_DATATYPE_MISALIGNMENT:
450 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
451 break;
452 case CONTROL_C_EXIT:
453 DEBUG_Printf(DBG_CHN_MESG, "^C");
454 break;
455 case EXCEPTION_CRITICAL_SECTION_WAIT:
456 DEBUG_Printf(DBG_CHN_MESG, "critical section %08lx wait failed",
457 rec->ExceptionInformation[0]);
458 if (!DBG_IVAR(BreakOnCritSectTimeOut))
460 DEBUG_Printf(DBG_CHN_MESG, "\n");
461 return TRUE;
463 break;
464 case EXCEPTION_WINE_STUB:
466 char dll[32], name[64];
467 DEBUG_ProcessGetString( dll, sizeof(dll), DEBUG_CurrThread->process->handle,
468 (char *)rec->ExceptionInformation[0] );
469 DEBUG_ProcessGetString( name, sizeof(name), DEBUG_CurrThread->process->handle,
470 (char *)rec->ExceptionInformation[1] );
471 DEBUG_Printf(DBG_CHN_MESG, "unimplemented function %s.%s called", dll, name );
473 break;
474 case EXCEPTION_VM86_INTx:
475 DEBUG_Printf(DBG_CHN_MESG, "interrupt %02lx in vm86 mode",
476 rec->ExceptionInformation[0]);
477 break;
478 case EXCEPTION_VM86_STI:
479 DEBUG_Printf(DBG_CHN_MESG, "sti in vm86 mode");
480 break;
481 case EXCEPTION_VM86_PICRETURN:
482 DEBUG_Printf(DBG_CHN_MESG, "PIC return in vm86 mode");
483 break;
484 default:
485 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
486 break;
488 DEBUG_Printf(DBG_CHN_MESG, "\n");
491 DEBUG_Printf(DBG_CHN_TRACE,
492 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
493 #ifdef __i386__
494 DEBUG_context.Eip, DEBUG_context.EFlags,
495 #else
496 0L, 0L,
497 #endif
498 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
500 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
501 while ((ret = DEBUG_Parser())) {
502 if (DEBUG_ValidateRegisters()) {
503 if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance)
504 break;
505 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
509 *cont = DEBUG_ExceptionEpilog();
511 DEBUG_Printf(DBG_CHN_TRACE,
512 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
513 #ifdef __i386__
514 DEBUG_context.Eip, DEBUG_context.EFlags,
515 #else
516 0L, 0L,
517 #endif
518 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
520 return ret;
523 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
525 char buffer[256];
526 BOOL ret;
528 DEBUG_CurrPid = de->dwProcessId;
529 DEBUG_CurrTid = de->dwThreadId;
531 __TRY {
532 ret = TRUE;
533 *cont = 0L;
535 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
536 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
537 else
538 DEBUG_CurrThread = NULL;
540 switch (de->dwDebugEventCode) {
541 case EXCEPTION_DEBUG_EVENT:
542 if (!DEBUG_CurrThread) {
543 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
544 de->dwProcessId, de->dwThreadId);
545 break;
548 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
549 de->dwProcessId, de->dwThreadId,
550 de->u.Exception.ExceptionRecord.ExceptionCode);
552 if (DEBUG_CurrProcess->continue_on_first_exception) {
553 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
554 if (!DBG_IVAR(BreakOnAttach)) {
555 *cont = DBG_CONTINUE;
556 break;
560 DEBUG_context.ContextFlags = CONTEXT_CONTROL
561 | CONTEXT_INTEGER
562 #ifdef CONTEXT_SEGMENTS
563 | CONTEXT_SEGMENTS
564 #endif
565 #ifdef CONTEXT_DEBUG_REGISTERS
566 | CONTEXT_DEBUG_REGISTERS
567 #endif
570 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
571 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
572 break;
575 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
576 de->u.Exception.dwFirstChance,
577 DEBUG_CurrThread->wait_for_first_exception,
578 cont);
579 if (DEBUG_CurrThread) {
580 DEBUG_CurrThread->wait_for_first_exception = 0;
581 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
583 break;
585 case CREATE_THREAD_DEBUG_EVENT:
586 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
587 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
589 if (DEBUG_CurrProcess == NULL) {
590 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
591 break;
593 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
594 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
595 break;
598 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
599 de->dwThreadId,
600 de->u.CreateThread.hThread,
601 de->u.CreateThread.lpStartAddress,
602 de->u.CreateThread.lpThreadLocalBase);
603 if (!DEBUG_CurrThread) {
604 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
605 break;
607 DEBUG_InitCurrThread();
608 break;
610 case CREATE_PROCESS_DEBUG_EVENT:
611 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
612 de->u.CreateProcessInfo.hProcess,
613 de->u.CreateProcessInfo.lpImageName);
615 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
616 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
617 de->dwProcessId, de->dwThreadId,
618 buffer,
619 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
620 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
621 de->u.CreateProcessInfo.nDebugInfoSize);
623 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
624 if (DEBUG_CurrProcess->handle) {
625 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
626 break;
628 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
629 } else {
630 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
631 de->u.CreateProcessInfo.hProcess);
632 if (DEBUG_CurrProcess == NULL) {
633 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
634 break;
638 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
639 de->dwProcessId, de->dwThreadId,
640 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
642 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
643 de->dwThreadId,
644 de->u.CreateProcessInfo.hThread,
645 de->u.CreateProcessInfo.lpStartAddress,
646 de->u.CreateProcessInfo.lpThreadLocalBase);
647 if (!DEBUG_CurrThread) {
648 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
649 break;
652 DEBUG_InitCurrProcess();
653 DEBUG_InitCurrThread();
655 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
656 DEBUG_CurrThread->process->handle,
657 de->u.CreateProcessInfo.lpImageName);
658 DEBUG_LoadModule32(buffer[0] ? buffer : "<Debugged process>",
659 de->u.CreateProcessInfo.hFile,
660 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
662 if (buffer[0]) /* we got a process name */
664 DWORD type;
665 if (!GetBinaryTypeA( buffer, &type ))
667 /* not a Windows binary, assume it's a Unix executable then */
668 char unixname[MAX_PATH];
669 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
670 if (wine_get_unix_file_name( buffer, unixname, sizeof(unixname) ))
672 DEBUG_ReadExecutableDbgInfo( unixname );
673 break;
677 /* if it is a Windows binary, or an invalid or missing file name,
678 * we use wine itself as the main executable */
679 DEBUG_ReadExecutableDbgInfo( "wine" );
680 break;
682 case EXIT_THREAD_DEBUG_EVENT:
683 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
684 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
686 if (DEBUG_CurrThread == NULL) {
687 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
688 break;
690 /* FIXME: remove break point set on thread startup */
691 DEBUG_DelThread(DEBUG_CurrThread);
692 break;
694 case EXIT_PROCESS_DEBUG_EVENT:
695 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
696 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
698 if (DEBUG_CurrProcess == NULL) {
699 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
700 break;
702 /* just in case */
703 DEBUG_SetBreakpoints(FALSE);
704 /* kill last thread */
705 DEBUG_DelThread(DEBUG_CurrProcess->threads);
706 DEBUG_DelProcess(DEBUG_CurrProcess);
708 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
709 break;
711 case LOAD_DLL_DEBUG_EVENT:
712 if (DEBUG_CurrThread == NULL) {
713 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
714 break;
716 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
717 DEBUG_CurrThread->process->handle,
718 de->u.LoadDll.lpImageName);
720 /* FIXME unicode: de->u.LoadDll.fUnicode */
721 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
722 de->dwProcessId, de->dwThreadId,
723 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
724 de->u.LoadDll.dwDebugInfoFileOffset,
725 de->u.LoadDll.nDebugInfoSize);
726 CharUpper(buffer);
727 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
728 if (DBG_IVAR(BreakOnDllLoad)) {
729 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
730 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
731 ret = DEBUG_Parser();
733 break;
735 case UNLOAD_DLL_DEBUG_EVENT:
736 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
737 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
738 break;
740 case OUTPUT_DEBUG_STRING_EVENT:
741 if (DEBUG_CurrThread == NULL) {
742 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
743 break;
746 DEBUG_ProcessGetString(buffer, sizeof(buffer),
747 DEBUG_CurrThread->process->handle,
748 de->u.DebugString.lpDebugStringData);
750 /* fixme unicode de->u.DebugString.fUnicode ? */
751 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
752 de->dwProcessId, de->dwThreadId, buffer);
753 break;
755 case RIP_EVENT:
756 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
757 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
758 de->u.RipInfo.dwType);
759 break;
761 default:
762 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
763 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
766 } __EXCEPT(wine_dbg) {
767 *cont = 0;
768 ret = TRUE;
770 __ENDTRY;
771 return ret;
774 static DWORD DEBUG_MainLoop(void)
776 DEBUG_EVENT de;
777 DWORD cont;
778 BOOL ret;
780 DEBUG_Printf(DBG_CHN_MESG, " on pid %lx\n", DEBUG_CurrPid);
782 for (ret = TRUE; ret; ) {
783 /* wait until we get at least one loaded process */
784 while (!DEBUG_ProcessList && (ret = DEBUG_Parser()));
785 if (!ret) break;
787 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) {
788 ret = DEBUG_HandleDebugEvent(&de, &cont);
789 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
793 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %lx\n", DEBUG_CurrPid);
795 return 0;
798 static BOOL DEBUG_Start(LPSTR cmdLine)
800 PROCESS_INFORMATION info;
801 STARTUPINFOA startup;
803 memset(&startup, 0, sizeof(startup));
804 startup.cb = sizeof(startup);
805 startup.dwFlags = STARTF_USESHOWWINDOW;
806 startup.wShowWindow = SW_SHOWNORMAL;
808 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
809 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
810 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
811 return FALSE;
813 DEBUG_CurrPid = info.dwProcessId;
814 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0))) return FALSE;
816 return TRUE;
819 void DEBUG_Run(const char* args)
821 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
822 const char* pgm = (wmod) ? wmod->module_name : "none";
824 if (args) {
825 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
826 } else {
827 if (!DEBUG_LastCmdLine) {
828 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
829 return;
831 DEBUG_Start(DEBUG_LastCmdLine);
835 int DEBUG_main(int argc, char** argv)
837 DWORD retv = 0;
839 #ifdef DBG_need_heap
840 /* Initialize the debugger heap. */
841 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
842 #endif
844 /* Initialize the type handling stuff. */
845 DEBUG_InitTypes();
846 DEBUG_InitCVDataTypes();
848 /* Initialize internal vars (types must be initialized before) */
849 if (!DEBUG_IntVarsRW(TRUE)) return -1;
851 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
852 * stdout and stderr streams
854 if (DBG_IVAR(UseXTerm)) {
855 COORD pos;
857 /* This is a hack: it forces creation of an xterm, not done by default */
858 pos.X = 0; pos.Y = 1;
859 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
862 DEBUG_Printf(DBG_CHN_MESG, "WineDbg starting... ");
864 if (argc == 3) {
865 HANDLE hEvent;
866 DWORD pid;
868 if ((pid = atoi(argv[1])) != 0 && (hEvent = (HANDLE)atoi(argv[2])) != 0) {
869 BOOL ret = DEBUG_Attach(pid, TRUE);
871 SetEvent(hEvent);
872 CloseHandle(hEvent);
873 if (!ret) {
874 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
875 DEBUG_CurrPid, GetLastError());
876 goto leave;
878 DEBUG_CurrPid = pid;
882 if (DEBUG_CurrPid == 0 && argc > 1) {
883 int i, len;
884 LPSTR cmdLine;
886 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
887 cmdLine[0] = '\0';
889 for (i = 1; i < argc; i++) {
890 len += strlen(argv[i]) + 1;
891 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
892 strcat(cmdLine, argv[i]);
893 cmdLine[len - 2] = ' ';
894 cmdLine[len - 1] = '\0';
897 if (!DEBUG_Start(cmdLine)) {
898 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
899 goto leave;
901 DBG_free(DEBUG_LastCmdLine);
902 DEBUG_LastCmdLine = cmdLine;
905 retv = DEBUG_MainLoop();
906 leave:
907 /* saves modified variables */
908 DEBUG_IntVarsRW(FALSE);
910 return retv;
912 oom_leave:
913 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
914 goto leave;