GetMenu might be used to get child window id.
[wine.git] / debugger / winedbg.c
blob8825b7041e4b02f4c39f7fbe02ba84032db4fc2f
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->next_index = 0;
149 p->dbg_hdr_addr = 0;
151 p->next = DEBUG_ProcessList;
152 p->prev = NULL;
153 if (DEBUG_ProcessList) DEBUG_ProcessList->prev = p;
154 DEBUG_ProcessList = p;
155 return p;
158 static void DEBUG_DelThread(DBG_THREAD* p);
160 static void DEBUG_DelProcess(DBG_PROCESS* p)
162 if (p->threads != NULL) {
163 DEBUG_Printf(DBG_CHN_ERR, "Shouldn't happen\n");
164 while (p->threads) DEBUG_DelThread(p->threads);
166 if (p->prev) p->prev->next = p->next;
167 if (p->next) p->next->prev = p->prev;
168 if (p == DEBUG_ProcessList) DEBUG_ProcessList = p->next;
169 if (p == DEBUG_CurrProcess) DEBUG_CurrProcess = NULL;
170 DBG_free(p);
173 static void DEBUG_InitCurrProcess(void)
177 static BOOL DEBUG_ProcessGetString(char* buffer, int size, HANDLE hp, LPSTR addr)
179 DWORD sz;
180 *(WCHAR*)buffer = 0;
181 return (addr && ReadProcessMemory(hp, addr, buffer, size, &sz));
184 static BOOL DEBUG_ProcessGetStringIndirect(char* buffer, int size, HANDLE hp, LPVOID addr)
186 LPVOID ad;
187 DWORD sz;
189 if ( addr
190 && ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz)
191 && sz == sizeof(ad)
192 && ad
193 && ReadProcessMemory(hp, ad, buffer, size, &sz))
194 return TRUE;
195 *(WCHAR*)buffer = 0;
196 return FALSE;
199 static DBG_THREAD* DEBUG_GetThread(DBG_PROCESS* p, DWORD tid)
201 DBG_THREAD* t;
203 for (t = p->threads; t; t = t->next)
204 if (t->tid == tid) break;
205 return t;
208 static DBG_THREAD* DEBUG_AddThread(DBG_PROCESS* p, DWORD tid,
209 HANDLE h, LPVOID start, LPVOID teb)
211 DBG_THREAD* t = DBG_alloc(sizeof(DBG_THREAD));
212 if (!t)
213 return NULL;
215 t->handle = h;
216 t->tid = tid;
217 t->start = start;
218 t->teb = teb;
219 t->process = p;
220 t->wait_for_first_exception = 0;
221 t->dbg_exec_mode = EXEC_CONT;
222 t->dbg_exec_count = 0;
224 p->num_threads++;
225 t->next = p->threads;
226 t->prev = NULL;
227 if (p->threads) p->threads->prev = t;
228 p->threads = t;
230 return t;
233 static void DEBUG_InitCurrThread(void)
235 if (DEBUG_CurrThread->start) {
236 if (DEBUG_CurrThread->process->num_threads == 1 ||
237 DBG_IVAR(BreakAllThreadsStartup)) {
238 DBG_VALUE value;
240 DEBUG_SetBreakpoints(FALSE);
241 value.type = NULL;
242 value.cookie = DV_TARGET;
243 value.addr.seg = 0;
244 value.addr.off = (DWORD)DEBUG_CurrThread->start;
245 DEBUG_AddBreakpoint(&value, NULL);
246 DEBUG_SetBreakpoints(TRUE);
248 } else {
249 DEBUG_CurrThread->wait_for_first_exception = 1;
253 static void DEBUG_DelThread(DBG_THREAD* t)
255 if (t->prev) t->prev->next = t->next;
256 if (t->next) t->next->prev = t->prev;
257 if (t == t->process->threads) t->process->threads = t->next;
258 t->process->num_threads--;
259 if (t == DEBUG_CurrThread) DEBUG_CurrThread = NULL;
260 DBG_free(t);
263 BOOL DEBUG_Attach(DWORD pid, BOOL cofe)
265 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(pid, 0))) return FALSE;
267 if (!DebugActiveProcess(pid)) {
268 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
269 pid, GetLastError());
270 return FALSE;
272 DEBUG_CurrProcess->continue_on_first_exception = cofe;
273 return TRUE;
276 static BOOL DEBUG_ExceptionProlog(BOOL is_debug, BOOL force, DWORD code)
278 DBG_ADDR addr;
279 int newmode;
281 DEBUG_GetCurrentAddress(&addr);
282 DEBUG_SuspendExecution();
284 if (!is_debug) {
285 #ifdef __i386__
286 if (DEBUG_IsSelectorSystem(DEBUG_context.SegCs))
287 DEBUG_Printf(DBG_CHN_MESG, " in 32-bit code (0x%08lx).\n", addr.off);
288 else
289 DEBUG_Printf(DBG_CHN_MESG, " in 16-bit code (%04x:%04lx).\n",
290 LOWORD(addr.seg), addr.off);
291 #else
292 DEBUG_Printf(DBG_CHN_MESG, " (0x%08lx).\n", addr.off);
293 #endif
296 DEBUG_LoadEntryPoints("Loading new modules symbols:\n");
298 if (!force && is_debug &&
299 DEBUG_ShouldContinue(&addr,
300 code,
301 DEBUG_CurrThread->dbg_exec_mode,
302 &DEBUG_CurrThread->dbg_exec_count))
303 return FALSE;
305 #ifdef __i386__
306 switch (newmode = DEBUG_GetSelectorType(addr.seg)) {
307 case 16: case 32: break;
308 default: DEBUG_Printf(DBG_CHN_MESG, "Bad CS (%ld)\n", addr.seg); newmode = 32;
310 #else
311 newmode = 32;
312 #endif
313 if (newmode != DEBUG_CurrThread->dbg_mode)
314 DEBUG_Printf(DBG_CHN_MESG,"In %d bit mode.\n", DEBUG_CurrThread->dbg_mode = newmode);
316 DEBUG_DoDisplay();
318 if (is_debug || force) {
320 * Do a quiet backtrace so that we have an idea of what the situation
321 * is WRT the source files.
323 DEBUG_BackTrace(FALSE);
324 } else {
325 /* This is a real crash, dump some info */
326 DEBUG_InfoRegisters();
327 DEBUG_InfoStack();
328 #ifdef __i386__
329 if (DEBUG_CurrThread->dbg_mode == 16) {
330 DEBUG_InfoSegments(DEBUG_context.SegDs >> 3, 1);
331 if (DEBUG_context.SegEs != DEBUG_context.SegDs)
332 DEBUG_InfoSegments(DEBUG_context.SegEs >> 3, 1);
334 DEBUG_InfoSegments(DEBUG_context.SegFs >> 3, 1);
335 #endif
336 DEBUG_BackTrace(TRUE);
339 if (!is_debug ||
340 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_OVER) ||
341 (DEBUG_CurrThread->dbg_exec_mode == EXEC_STEPI_INSTR)) {
343 struct list_id list;
345 /* Show where we crashed */
346 curr_frame = 0;
347 DEBUG_DisassembleInstruction(&addr);
349 /* resets list internal arguments so we can look at source code when needed */
350 DEBUG_FindNearestSymbol(&addr, TRUE, NULL, 0, &list);
351 if (list.sourcefile) DEBUG_List(&list, NULL, 0);
353 return TRUE;
356 static DWORD DEBUG_ExceptionEpilog(void)
358 DEBUG_CurrThread->dbg_exec_mode = DEBUG_RestartExecution(DEBUG_CurrThread->dbg_exec_mode,
359 DEBUG_CurrThread->dbg_exec_count);
361 * This will have gotten absorbed into the breakpoint info
362 * if it was used. Otherwise it would have been ignored.
363 * In any case, we don't mess with it any more.
365 if (DEBUG_CurrThread->dbg_exec_mode == EXEC_CONT || DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS)
366 DEBUG_CurrThread->dbg_exec_count = 0;
368 return (DEBUG_CurrThread->dbg_exec_mode == EXEC_PASS) ? DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE;
371 static BOOL DEBUG_HandleException(EXCEPTION_RECORD *rec, BOOL first_chance, BOOL force, LPDWORD cont)
373 BOOL is_debug = FALSE;
374 BOOL ret = TRUE;
376 *cont = DBG_CONTINUE;
377 if (first_chance && !force && !DBG_IVAR(BreakOnFirstChance)) return TRUE;
379 switch (rec->ExceptionCode)
381 case EXCEPTION_BREAKPOINT:
382 case EXCEPTION_SINGLE_STEP:
383 is_debug = TRUE;
384 break;
387 if (!is_debug)
389 /* print some infos */
390 DEBUG_Printf(DBG_CHN_MESG, "%s: ",
391 first_chance ? "First chance exception" : "Unhandled exception");
392 switch (rec->ExceptionCode)
394 case EXCEPTION_INT_DIVIDE_BY_ZERO:
395 DEBUG_Printf(DBG_CHN_MESG, "divide by zero");
396 break;
397 case EXCEPTION_INT_OVERFLOW:
398 DEBUG_Printf(DBG_CHN_MESG, "overflow");
399 break;
400 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
401 DEBUG_Printf(DBG_CHN_MESG, "array bounds ");
402 break;
403 case EXCEPTION_ILLEGAL_INSTRUCTION:
404 DEBUG_Printf(DBG_CHN_MESG, "illegal instruction");
405 break;
406 case EXCEPTION_STACK_OVERFLOW:
407 DEBUG_Printf(DBG_CHN_MESG, "stack overflow");
408 break;
409 case EXCEPTION_PRIV_INSTRUCTION:
410 DEBUG_Printf(DBG_CHN_MESG, "priviledged instruction");
411 break;
412 case EXCEPTION_ACCESS_VIOLATION:
413 if (rec->NumberParameters == 2)
414 DEBUG_Printf(DBG_CHN_MESG, "page fault on %s access to 0x%08lx",
415 rec->ExceptionInformation[0] ? "write" : "read",
416 rec->ExceptionInformation[1]);
417 else
418 DEBUG_Printf(DBG_CHN_MESG, "page fault");
419 break;
420 case EXCEPTION_DATATYPE_MISALIGNMENT:
421 DEBUG_Printf(DBG_CHN_MESG, "Alignment");
422 break;
423 case CONTROL_C_EXIT:
424 DEBUG_Printf(DBG_CHN_MESG, "^C");
425 break;
426 case EXCEPTION_CRITICAL_SECTION_WAIT:
427 DEBUG_Printf(DBG_CHN_MESG, "critical section %08lx wait failed",
428 rec->ExceptionInformation[0]);
429 if (!DBG_IVAR(BreakOnCritSectTimeOut))
430 return TRUE;
431 break;
432 default:
433 DEBUG_Printf(DBG_CHN_MESG, "%08lx", rec->ExceptionCode);
434 break;
436 DEBUG_Printf(DBG_CHN_MESG, "\n");
439 DEBUG_Printf(DBG_CHN_TRACE,
440 "Entering debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
441 #ifdef __i386__
442 DEBUG_context.Eip, DEBUG_context.EFlags,
443 #else
444 0L, 0L,
445 #endif
446 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
448 if (DEBUG_ExceptionProlog(is_debug, force, rec->ExceptionCode)) {
449 while ((ret = DEBUG_Parser())) {
450 if (DEBUG_ValidateRegisters()) {
451 if (DEBUG_CurrThread->dbg_exec_mode != EXEC_PASS || first_chance)
452 break;
453 DEBUG_Printf(DBG_CHN_MESG, "Cannot pass on last chance exception. You must use cont\n");
457 *cont = DEBUG_ExceptionEpilog();
459 DEBUG_Printf(DBG_CHN_TRACE,
460 "Exiting debugger PC=%lx EFL=%08lx mode=%d count=%d\n",
461 #ifdef __i386__
462 DEBUG_context.Eip, DEBUG_context.EFlags,
463 #else
464 0L, 0L,
465 #endif
466 DEBUG_CurrThread->dbg_exec_mode, DEBUG_CurrThread->dbg_exec_count);
468 return ret;
471 static BOOL DEBUG_HandleDebugEvent(DEBUG_EVENT* de, LPDWORD cont)
473 char buffer[256];
474 BOOL ret;
476 DEBUG_CurrPid = de->dwProcessId;
477 DEBUG_CurrTid = de->dwThreadId;
479 __TRY {
480 ret = TRUE;
481 *cont = 0L;
483 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL)
484 DEBUG_CurrThread = DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId);
485 else
486 DEBUG_CurrThread = NULL;
488 switch (de->dwDebugEventCode) {
489 case EXCEPTION_DEBUG_EVENT:
490 if (!DEBUG_CurrThread) {
491 DEBUG_Printf(DBG_CHN_ERR, "%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
492 de->dwProcessId, de->dwThreadId);
493 break;
496 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exception code=%08lx\n",
497 de->dwProcessId, de->dwThreadId,
498 de->u.Exception.ExceptionRecord.ExceptionCode);
500 if (DEBUG_CurrProcess->continue_on_first_exception) {
501 DEBUG_CurrProcess->continue_on_first_exception = FALSE;
502 if (!DBG_IVAR(BreakOnAttach)) {
503 *cont = DBG_CONTINUE;
504 break;
508 DEBUG_context.ContextFlags = CONTEXT_CONTROL
509 | CONTEXT_INTEGER
510 #ifdef CONTEXT_SEGMENTS
511 | CONTEXT_SEGMENTS
512 #endif
513 #ifdef CONTEXT_DEBUG_REGISTERS
514 | CONTEXT_DEBUG_REGISTERS
515 #endif
518 if (!GetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context)) {
519 DEBUG_Printf(DBG_CHN_WARN, "Can't get thread's context\n");
520 break;
523 ret = DEBUG_HandleException(&de->u.Exception.ExceptionRecord,
524 de->u.Exception.dwFirstChance,
525 DEBUG_CurrThread->wait_for_first_exception,
526 cont);
527 if (DEBUG_CurrThread) {
528 DEBUG_CurrThread->wait_for_first_exception = 0;
529 SetThreadContext(DEBUG_CurrThread->handle, &DEBUG_context);
531 break;
533 case CREATE_THREAD_DEBUG_EVENT:
534 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread D @%08lx\n", de->dwProcessId, de->dwThreadId,
535 (unsigned long)(LPVOID)de->u.CreateThread.lpStartAddress);
537 if (DEBUG_CurrProcess == NULL) {
538 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
539 break;
541 if (DEBUG_GetThread(DEBUG_CurrProcess, de->dwThreadId) != NULL) {
542 DEBUG_Printf(DBG_CHN_TRACE, "Thread already listed, skipping\n");
543 break;
546 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
547 de->dwThreadId,
548 de->u.CreateThread.hThread,
549 de->u.CreateThread.lpStartAddress,
550 de->u.CreateThread.lpThreadLocalBase);
551 if (!DEBUG_CurrThread) {
552 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
553 break;
555 DEBUG_InitCurrThread();
556 break;
558 case CREATE_PROCESS_DEBUG_EVENT:
559 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
560 de->u.CreateProcessInfo.hProcess,
561 de->u.CreateProcessInfo.lpImageName);
563 /* FIXME unicode ? de->u.CreateProcessInfo.fUnicode */
564 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create process %s @%08lx (%ld<%ld>)\n",
565 de->dwProcessId, de->dwThreadId,
566 buffer,
567 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress,
568 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
569 de->u.CreateProcessInfo.nDebugInfoSize);
571 if ((DEBUG_CurrProcess = DEBUG_GetProcess(de->dwProcessId)) != NULL) {
572 if (DEBUG_CurrProcess->handle) {
573 DEBUG_Printf(DBG_CHN_ERR, "Skipping already defined process\n");
574 break;
576 DEBUG_CurrProcess->handle = de->u.CreateProcessInfo.hProcess;
577 } else {
578 DEBUG_CurrProcess = DEBUG_AddProcess(de->dwProcessId,
579 de->u.CreateProcessInfo.hProcess);
580 if (DEBUG_CurrProcess == NULL) {
581 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
582 break;
586 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: create thread I @%08lx\n",
587 de->dwProcessId, de->dwThreadId,
588 (unsigned long)(LPVOID)de->u.CreateProcessInfo.lpStartAddress);
590 DEBUG_CurrThread = DEBUG_AddThread(DEBUG_CurrProcess,
591 de->dwThreadId,
592 de->u.CreateProcessInfo.hThread,
593 de->u.CreateProcessInfo.lpStartAddress,
594 de->u.CreateProcessInfo.lpThreadLocalBase);
595 if (!DEBUG_CurrThread) {
596 DEBUG_Printf(DBG_CHN_ERR, "Couldn't create thread\n");
597 break;
600 DEBUG_InitCurrProcess();
601 DEBUG_InitCurrThread();
603 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
604 DEBUG_CurrThread->process->handle,
605 de->u.CreateProcessInfo.lpImageName);
606 DEBUG_LoadModule32(buffer[0] ? buffer : "<Debugged process>",
607 de->u.CreateProcessInfo.hFile,
608 (DWORD)de->u.CreateProcessInfo.lpBaseOfImage);
610 if (buffer[0]) /* we got a process name */
612 DWORD type;
613 if (!GetBinaryTypeA( buffer, &type ))
615 /* not a Windows binary, assume it's a Unix executable then */
616 DOS_FULL_NAME fullname;
617 /* HACK!! should fix DEBUG_ReadExecutableDbgInfo to accept DOS filenames */
618 if (DOSFS_GetFullName( buffer, TRUE, &fullname ))
620 DEBUG_ReadExecutableDbgInfo( fullname.long_name );
621 break;
625 /* if it is a Windows binary, or an invalid or missing file name,
626 * we use wine itself as the main executable */
627 DEBUG_ReadExecutableDbgInfo( "wine" );
628 break;
630 case EXIT_THREAD_DEBUG_EVENT:
631 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit thread (%ld)\n",
632 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
634 if (DEBUG_CurrThread == NULL) {
635 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
636 break;
638 /* FIXME: remove break point set on thread startup */
639 DEBUG_DelThread(DEBUG_CurrThread);
640 break;
642 case EXIT_PROCESS_DEBUG_EVENT:
643 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: exit process (%ld)\n",
644 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
646 if (DEBUG_CurrProcess == NULL) {
647 DEBUG_Printf(DBG_CHN_ERR, "Unknown process\n");
648 break;
650 /* just in case */
651 DEBUG_SetBreakpoints(FALSE);
652 /* kill last thread */
653 DEBUG_DelThread(DEBUG_CurrProcess->threads);
654 DEBUG_DelProcess(DEBUG_CurrProcess);
656 DEBUG_Printf(DBG_CHN_MESG, "Process of pid=%08lx has terminated\n", DEBUG_CurrPid);
657 break;
659 case LOAD_DLL_DEBUG_EVENT:
660 if (DEBUG_CurrThread == NULL) {
661 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
662 break;
664 DEBUG_ProcessGetStringIndirect(buffer, sizeof(buffer),
665 DEBUG_CurrThread->process->handle,
666 de->u.LoadDll.lpImageName);
668 /* FIXME unicode: de->u.LoadDll.fUnicode */
669 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
670 de->dwProcessId, de->dwThreadId,
671 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
672 de->u.LoadDll.dwDebugInfoFileOffset,
673 de->u.LoadDll.nDebugInfoSize);
674 CharUpper(buffer);
675 DEBUG_LoadModule32(buffer, de->u.LoadDll.hFile, (DWORD)de->u.LoadDll.lpBaseOfDll);
676 if (DBG_IVAR(BreakOnDllLoad)) {
677 DEBUG_Printf(DBG_CHN_MESG, "Stopping on DLL %s loading at %08lx\n",
678 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
679 ret = DEBUG_Parser();
681 break;
683 case UNLOAD_DLL_DEBUG_EVENT:
684 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unload DLL @%08lx\n", de->dwProcessId, de->dwThreadId,
685 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
686 break;
688 case OUTPUT_DEBUG_STRING_EVENT:
689 if (DEBUG_CurrThread == NULL) {
690 DEBUG_Printf(DBG_CHN_ERR, "Unknown thread\n");
691 break;
694 DEBUG_ProcessGetString(buffer, sizeof(buffer),
695 DEBUG_CurrThread->process->handle,
696 de->u.DebugString.lpDebugStringData);
698 /* fixme unicode de->u.DebugString.fUnicode ? */
699 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: output debug string (%s)\n",
700 de->dwProcessId, de->dwThreadId, buffer);
701 break;
703 case RIP_EVENT:
704 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: rip error=%ld type=%ld\n",
705 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
706 de->u.RipInfo.dwType);
707 break;
709 default:
710 DEBUG_Printf(DBG_CHN_TRACE, "%08lx:%08lx: unknown event (%ld)\n",
711 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
714 } __EXCEPT(wine_dbg) {
715 *cont = 0;
716 ret = TRUE;
718 __ENDTRY;
719 return ret;
722 static DWORD DEBUG_MainLoop(void)
724 DEBUG_EVENT de;
725 DWORD cont;
726 BOOL ret;
728 DEBUG_Printf(DBG_CHN_MESG, " on pid %ld\n", DEBUG_CurrPid);
730 for (ret = TRUE; ret; ) {
731 /* wait until we get at least one loaded process */
732 while (!DEBUG_ProcessList && (ret = DEBUG_Parser()));
733 if (!ret) break;
735 while (ret && DEBUG_ProcessList && WaitForDebugEvent(&de, INFINITE)) {
736 ret = DEBUG_HandleDebugEvent(&de, &cont);
737 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, cont);
741 DEBUG_Printf(DBG_CHN_MESG, "WineDbg terminated on pid %ld\n", DEBUG_CurrPid);
743 return 0;
746 static BOOL DEBUG_Start(LPSTR cmdLine)
748 PROCESS_INFORMATION info;
749 STARTUPINFOA startup;
751 memset(&startup, 0, sizeof(startup));
752 startup.cb = sizeof(startup);
753 startup.dwFlags = STARTF_USESHOWWINDOW;
754 startup.wShowWindow = SW_SHOWNORMAL;
756 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
757 FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info)) {
758 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
759 return FALSE;
761 DEBUG_CurrPid = info.dwProcessId;
762 if (!(DEBUG_CurrProcess = DEBUG_AddProcess(DEBUG_CurrPid, 0))) return FALSE;
764 return TRUE;
767 void DEBUG_Run(const char* args)
769 DBG_MODULE* wmod = DEBUG_GetProcessMainModule(DEBUG_CurrProcess);
770 const char* pgm = (wmod) ? wmod->module_name : "none";
772 if (args) {
773 DEBUG_Printf(DBG_CHN_MESG, "Run (%s) with '%s'\n", pgm, args);
774 } else {
775 if (!DEBUG_LastCmdLine) {
776 DEBUG_Printf(DBG_CHN_MESG, "Cannot find previously used command line.\n");
777 return;
779 DEBUG_Start(DEBUG_LastCmdLine);
783 int DEBUG_main(int argc, char** argv)
785 DWORD retv = 0;
787 #ifdef DBG_need_heap
788 /* Initialize the debugger heap. */
789 dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
790 #endif
792 /* Initialize the type handling stuff. */
793 DEBUG_InitTypes();
794 DEBUG_InitCVDataTypes();
796 /* Initialize internal vars (types must be initialized before) */
797 if (!DEBUG_IntVarsRW(TRUE)) return -1;
799 /* keep it as a guiexe for now, so that Wine won't touch the Unix stdin,
800 * stdout and stderr streams
802 if (DBG_IVAR(UseXTerm)) {
803 COORD pos;
805 /* This is a hack: it forces creation of an xterm, not done by default */
806 pos.X = 0; pos.Y = 1;
807 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
810 DEBUG_Printf(DBG_CHN_MESG, "Starting WineDbg... ");
812 if (argc == 3) {
813 HANDLE hEvent;
814 DWORD pid;
816 if ((pid = atoi(argv[1])) != 0 && (hEvent = atoi(argv[2])) != 0) {
817 BOOL ret = DEBUG_Attach(pid, TRUE);
819 SetEvent(hEvent);
820 CloseHandle(hEvent);
821 if (!ret) {
822 DEBUG_Printf(DBG_CHN_ERR, "Can't attach process %ld: %ld\n",
823 DEBUG_CurrPid, GetLastError());
824 goto leave;
826 DEBUG_CurrPid = pid;
830 if (DEBUG_CurrPid == 0 && argc > 1) {
831 int i, len;
832 LPSTR cmdLine;
834 if (!(cmdLine = DBG_alloc(len = 1))) goto oom_leave;
835 cmdLine[0] = '\0';
837 for (i = 1; i < argc; i++) {
838 len += strlen(argv[i]) + 1;
839 if (!(cmdLine = DBG_realloc(cmdLine, len))) goto oom_leave;
840 strcat(cmdLine, argv[i]);
841 cmdLine[len - 2] = ' ';
842 cmdLine[len - 1] = '\0';
845 if (!DEBUG_Start(cmdLine)) {
846 DEBUG_Printf(DBG_CHN_MESG, "Couldn't start process '%s'\n", cmdLine);
847 goto leave;
849 DBG_free(DEBUG_LastCmdLine);
850 DEBUG_LastCmdLine = cmdLine;
853 retv = DEBUG_MainLoop();
854 leave:
855 /* saves modified variables */
856 DEBUG_IntVarsRW(FALSE);
858 return retv;
860 oom_leave:
861 DEBUG_Printf(DBG_CHN_MESG, "Out of memory\n");
862 goto leave;