1 /* Wine internal debugger
2 * Interface to Windows debugger API
3 * Copyright 2000-2004 Eric Pouech
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/port.h"
29 #include "wine/exception.h"
30 #include "wine/library.h"
32 #include "wine/debug.h"
37 * + allow winedbg in automatic mode to create a minidump (or add another option
39 * + set a mode where winedbg would start (postmortem debugging) from a minidump
41 * + we always assume the stack grows has an i386 (ie downwards)
43 * + enable back the limited output (depth of structure printing and number of
45 * + make the output as close as possible to what gdb does
46 * - symbol management:
47 * + symbol table loading is broken
48 * + in symbol_get_lvalue, we don't do any scoping (as C does) between local and
49 * global vars (we may need this to force some display for example). A solution
50 * would be always to return arrays with: local vars, global vars, thunks
52 * + some bits of internal types are missing (like type casts and the address
54 * + the type for an enum's value is always inferred as int (winedbg & dbghelp)
55 * + most of the code implies that sizeof(void*) = sizeof(int)
56 * + all computations should be made on long long
57 * o expr computations are in int:s
58 * o bitfield size is on a 4-bytes
60 * + set a better fix for gdb (proxy mode) than the step-mode hack
61 * + implement function call in debuggee
62 * + trampoline management is broken when getting 16 <=> 32 thunk destination
64 * + thunking of delayed imports doesn't work as expected (ie, when stepping,
65 * it currently stops at first insn with line number during the library
66 * loading). We should identify this (__wine_delay_import) and set a
67 * breakpoint instead of single stepping the library loading.
68 * + it's wrong to copy thread->step_over_bp into process->bp[0] (when
69 * we have a multi-thread debuggee). complete fix must include storing all
70 * thread's step-over bp in process-wide bp array, and not to handle bp
71 * when we have the wrong thread running into that bp
74 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
76 struct dbg_process* dbg_curr_process = NULL;
77 struct dbg_thread* dbg_curr_thread = NULL;
81 int dbg_curr_frame = 0;
82 BOOL dbg_interactiveP = FALSE;
83 static char* dbg_last_cmd_line = NULL;
85 static struct dbg_process* dbg_process_list = NULL;
86 static enum {none_mode = 0, winedbg_mode, automatic_mode, gdb_mode} dbg_action_mode;
88 struct dbg_internal_var dbg_internal_vars[DBG_IV_LAST];
89 const struct dbg_internal_var* dbg_context_vars;
90 static HANDLE dbg_houtput;
92 void dbg_outputA(const char* buffer, int len)
94 static char line_buff[4096];
95 static unsigned int line_pos;
101 unsigned int count = min( len, sizeof(line_buff) - line_pos );
102 memcpy( line_buff + line_pos, buffer, count );
106 for (i = line_pos; i > 0; i--) if (line_buff[i-1] == '\n') break;
107 if (!i) /* no newline found */
109 if (len > 0) i = line_pos; /* buffer is full, flush anyway */
112 WriteFile(dbg_houtput, line_buff, i, &w, NULL);
113 memmove( line_buff, line_buff + i, line_pos - i );
118 void dbg_outputW(const WCHAR* buffer, int len)
123 /* do a serious Unicode to ANSI conversion
124 * FIXME: should CP_ACP be GetConsoleCP()?
126 newlen = WideCharToMultiByte(CP_ACP, 0, buffer, len, NULL, 0, NULL, NULL);
129 if (!(ansi = HeapAlloc(GetProcessHeap(), 0, newlen))) return;
130 WideCharToMultiByte(CP_ACP, 0, buffer, len, ansi, newlen, NULL, NULL);
131 dbg_outputA(ansi, newlen);
132 HeapFree(GetProcessHeap(), 0, ansi);
136 int dbg_printf(const char* format, ...)
138 static char buf[4*1024];
142 va_start(valist, format);
143 len = vsnprintf(buf, sizeof(buf), format, valist);
146 if (len <= -1 || len >= sizeof(buf))
148 len = sizeof(buf) - 1;
150 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
152 dbg_outputA(buf, len);
156 static unsigned dbg_load_internal_vars(void)
159 DWORD type = REG_DWORD;
161 DWORD count = sizeof(val);
163 struct dbg_internal_var* div = dbg_internal_vars;
165 /* initializes internal vars table */
166 #define INTERNAL_VAR(_var,_val,_ref,_tid) \
167 div->val = _val; div->name = #_var; div->pval = _ref; \
168 div->typeid = _tid; div++;
172 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
174 WINE_ERR("Cannot create WineDbg key in registry\n");
178 for (i = 0; i < DBG_IV_LAST; i++)
180 if (!dbg_internal_vars[i].pval)
182 if (!RegQueryValueEx(hkey, dbg_internal_vars[i].name, 0,
183 &type, (LPSTR)&val, &count))
184 dbg_internal_vars[i].val = val;
185 dbg_internal_vars[i].pval = &dbg_internal_vars[i].val;
189 /* set up the debug variables for the CPU context */
190 dbg_context_vars = be_cpu->init_registers(&dbg_context);
194 static unsigned dbg_save_internal_vars(void)
199 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
201 WINE_ERR("Cannot create WineDbg key in registry\n");
205 for (i = 0; i < DBG_IV_LAST; i++)
207 /* FIXME: type should be infered from basic type -if any- of intvar */
208 if (dbg_internal_vars[i].pval == &dbg_internal_vars[i].val)
209 RegSetValueEx(hkey, dbg_internal_vars[i].name, 0,
210 REG_DWORD, (const void*)dbg_internal_vars[i].pval,
211 sizeof(*dbg_internal_vars[i].pval));
217 const struct dbg_internal_var* dbg_get_internal_var(const char* name)
219 const struct dbg_internal_var* div;
221 for (div = &dbg_internal_vars[DBG_IV_LAST - 1]; div >= dbg_internal_vars; div--)
223 if (!strcmp(div->name, name)) return div;
225 for (div = dbg_context_vars; div->name; div++)
227 if (!strcasecmp(div->name, name)) return div;
233 struct dbg_process* dbg_get_process(DWORD pid)
235 struct dbg_process* p;
237 for (p = dbg_process_list; p; p = p->next)
238 if (p->pid == pid) break;
242 struct dbg_process* dbg_add_process(DWORD pid, HANDLE h, const char* imageName)
244 struct dbg_process* p;
246 if ((p = dbg_get_process(pid)))
250 WINE_ERR("Process (%lu) is already defined\n", pid);
255 p->imageName = imageName ? strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(imageName) + 1), imageName) : NULL;
260 if (!(p = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_process)))) return NULL;
263 p->imageName = imageName ? strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(imageName) + 1), imageName) : NULL;
265 p->continue_on_first_exception = FALSE;
266 p->next_bp = 1; /* breakpoint 0 is reserved for step-over */
267 memset(p->bp, 0, sizeof(p->bp));
268 p->delayed_bp = NULL;
269 p->num_delayed_bp = 0;
271 p->next = dbg_process_list;
273 if (dbg_process_list) dbg_process_list->prev = p;
274 dbg_process_list = p;
278 void dbg_del_process(struct dbg_process* p)
282 while (p->threads) dbg_del_thread(p->threads);
284 for (i = 0; i < p->num_delayed_bp; i++)
285 if (p->delayed_bp[i].is_symbol)
286 HeapFree(GetProcessHeap(), 0, p->delayed_bp[i].u.symbol.name);
288 HeapFree(GetProcessHeap(), 0, p->delayed_bp);
289 if (p->prev) p->prev->next = p->next;
290 if (p->next) p->next->prev = p->prev;
291 if (p == dbg_process_list) dbg_process_list = p->next;
292 if (p == dbg_curr_process) dbg_curr_process = NULL;
293 HeapFree(GetProcessHeap(), 0, (char*)p->imageName);
294 HeapFree(GetProcessHeap(), 0, p);
297 static void dbg_init_current_process(void)
301 struct mod_loader_info
304 IMAGEHLP_MODULE* imh_mod;
307 static BOOL CALLBACK mod_loader_cb(PSTR mod_name, DWORD base, void* ctx)
309 struct mod_loader_info* mli = (struct mod_loader_info*)ctx;
311 if (!strcmp(mod_name, "<wine-loader>"))
313 if (SymGetModuleInfo(mli->handle, base, mli->imh_mod))
314 return FALSE; /* stop enum */
319 BOOL dbg_get_debuggee_info(HANDLE hProcess, IMAGEHLP_MODULE* imh_mod)
321 struct mod_loader_info mli;
324 /* this will resynchronize builtin dbghelp's internal ELF module list */
325 SymLoadModule(hProcess, 0, 0, 0, 0, 0);
326 mli.handle = hProcess;
327 mli.imh_mod = imh_mod;
328 imh_mod->SizeOfStruct = sizeof(*imh_mod);
329 imh_mod->BaseOfImage = 0;
330 /* this is a wine specific options to return also ELF modules in the
333 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
334 SymEnumerateModules(hProcess, mod_loader_cb, (void*)&mli);
337 return imh_mod->BaseOfImage != 0;
340 struct dbg_thread* dbg_get_thread(struct dbg_process* p, DWORD tid)
342 struct dbg_thread* t;
345 for (t = p->threads; t; t = t->next)
346 if (t->tid == tid) break;
350 struct dbg_thread* dbg_add_thread(struct dbg_process* p, DWORD tid,
353 struct dbg_thread* t = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_thread));
362 t->exec_mode = dbg_exec_cont;
364 t->step_over_bp.enabled = FALSE;
365 t->step_over_bp.refcount = 0;
366 t->in_exception = FALSE;
368 snprintf(t->name, sizeof(t->name), "0x%08lx", tid);
370 t->next = p->threads;
372 if (p->threads) p->threads->prev = t;
378 static void dbg_init_current_thread(void* start)
382 if (dbg_curr_process->threads &&
383 !dbg_curr_process->threads->next && /* first thread ? */
384 DBG_IVAR(BreakAllThreadsStartup))
388 break_set_xpoints(FALSE);
389 addr.Mode = AddrModeFlat;
390 addr.Offset = (DWORD)start;
391 break_add_break(&addr, TRUE);
392 break_set_xpoints(TRUE);
397 void dbg_del_thread(struct dbg_thread* t)
399 if (t->prev) t->prev->next = t->next;
400 if (t->next) t->next->prev = t->prev;
401 if (t == t->process->threads) t->process->threads = t->next;
402 if (t == dbg_curr_thread) dbg_curr_thread = NULL;
403 HeapFree(GetProcessHeap(), 0, t);
406 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de);
408 /******************************************************************
409 * dbg_attach_debuggee
411 * Sets the debuggee to <pid>
412 * cofe instructs winedbg what to do when first exception is received
413 * (break=FALSE, continue=TRUE)
414 * wfe is set to TRUE if dbg_attach_debuggee should also proceed with all debug events
415 * until the first exception is received (aka: attach to an already running process)
417 BOOL dbg_attach_debuggee(DWORD pid, BOOL cofe, BOOL wfe)
421 if (!(dbg_curr_process = dbg_add_process(pid, 0, NULL))) return FALSE;
423 if (!DebugActiveProcess(pid))
425 dbg_printf("Can't attach process %lx: error %ld\n", pid, GetLastError());
426 dbg_del_process(dbg_curr_process);
429 dbg_curr_process->continue_on_first_exception = cofe;
431 if (wfe) /* shall we proceed all debug events until we get an exception ? */
433 dbg_interactiveP = FALSE;
434 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
436 if (dbg_handle_debug_event(&de)) break;
438 if (dbg_curr_process) dbg_interactiveP = TRUE;
443 BOOL dbg_detach_debuggee(void)
445 /* remove all set breakpoints in debuggee code */
446 break_set_xpoints(FALSE);
447 /* needed for single stepping (ugly).
448 * should this be handled inside the server ???
450 be_cpu->single_step(&dbg_context, FALSE);
451 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
452 if (dbg_curr_thread->in_exception)
453 ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, DBG_CONTINUE);
454 if (!DebugActiveProcessStop(dbg_curr_pid)) return FALSE;
455 dbg_del_process(dbg_curr_process);
460 static unsigned dbg_fetch_context(void)
462 dbg_context.ContextFlags = CONTEXT_CONTROL
464 #ifdef CONTEXT_SEGMENTS
467 #ifdef CONTEXT_DEBUG_REGISTERS
468 | CONTEXT_DEBUG_REGISTERS
471 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
473 WINE_WARN("Can't get thread's context\n");
479 static unsigned dbg_exception_prolog(BOOL is_debug, const EXCEPTION_RECORD* rec)
484 memory_get_current_pc(&addr);
485 break_suspend_execution();
486 dbg_curr_thread->excpt_record = *rec;
487 dbg_curr_thread->in_exception = TRUE;
493 case AddrModeFlat: dbg_printf(" in 32-bit code (0x%08lx)", addr.Offset); break;
494 case AddrModeReal: dbg_printf(" in vm86 code (%04x:%04lx)", addr.Segment, addr.Offset); break;
495 case AddrMode1616: dbg_printf(" in 16-bit code (%04x:%04lx)", addr.Segment, addr.Offset); break;
496 case AddrMode1632: dbg_printf(" in 32-bit code (%04x:%08lx)", addr.Segment, addr.Offset); break;
497 default: dbg_printf(" bad address");
502 /* this will resynchronize builtin dbghelp's internal ELF module list */
503 SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0);
506 * Do a quiet backtrace so that we have an idea of what the situation
507 * is WRT the source files.
509 stack_backtrace(dbg_curr_tid, FALSE);
511 break_should_continue(&addr, rec->ExceptionCode, &dbg_curr_thread->exec_count, &is_break))
514 if (addr.Mode != dbg_curr_thread->addr_mode)
516 const char* name = NULL;
520 case AddrMode1616: name = "16 bit"; break;
521 case AddrMode1632: name = "32 bit"; break;
522 case AddrModeReal: name = "vm86"; break;
523 case AddrModeFlat: name = "32 bit"; break;
526 dbg_printf("In %s mode.\n", name);
527 dbg_curr_thread->addr_mode = addr.Mode;
533 /* This is a real crash, dump some info */
534 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context);
536 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
537 stack_backtrace(dbg_curr_tid, TRUE);
539 if (!is_debug || is_break ||
540 dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
541 dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
544 /* Show where we crashed */
546 memory_disasm_one_insn(&tmp);
548 source_list_from_addr(&addr, 0);
553 static void dbg_exception_epilog(void)
555 break_restart_execution(dbg_curr_thread->exec_count);
557 * This will have gotten absorbed into the breakpoint info
558 * if it was used. Otherwise it would have been ignored.
559 * In any case, we don't mess with it any more.
561 if (dbg_curr_thread->exec_mode == dbg_exec_cont)
562 dbg_curr_thread->exec_count = 0;
563 dbg_curr_thread->in_exception = FALSE;
566 static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
568 BOOL is_debug = FALSE;
569 THREADNAME_INFO* pThreadName;
570 struct dbg_thread* pThread;
572 assert(dbg_curr_thread);
574 WINE_TRACE("exception=%lx first_chance=%c\n",
575 rec->ExceptionCode, first_chance ? 'Y' : 'N');
577 switch (rec->ExceptionCode)
579 case EXCEPTION_BREAKPOINT:
580 case EXCEPTION_SINGLE_STEP:
583 case EXCEPTION_NAME_THREAD:
584 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
585 if (pThreadName->dwThreadID == -1)
586 pThread = dbg_curr_thread;
588 pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
590 if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
591 dbg_printf("Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
592 pThread->tid, pThread->name);
596 if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance))
598 /* pass exception to program except for debug exceptions */
599 return DBG_EXCEPTION_NOT_HANDLED;
604 /* print some infos */
606 first_chance ? "First chance exception" : "Unhandled exception");
607 switch (rec->ExceptionCode)
609 case EXCEPTION_INT_DIVIDE_BY_ZERO:
610 dbg_printf("divide by zero");
612 case EXCEPTION_INT_OVERFLOW:
613 dbg_printf("overflow");
615 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
616 dbg_printf("array bounds");
618 case EXCEPTION_ILLEGAL_INSTRUCTION:
619 dbg_printf("illegal instruction");
621 case EXCEPTION_STACK_OVERFLOW:
622 dbg_printf("stack overflow");
624 case EXCEPTION_PRIV_INSTRUCTION:
625 dbg_printf("privileged instruction");
627 case EXCEPTION_ACCESS_VIOLATION:
628 if (rec->NumberParameters == 2)
629 dbg_printf("page fault on %s access to 0x%08lx",
630 rec->ExceptionInformation[0] ? "write" : "read",
631 rec->ExceptionInformation[1]);
633 dbg_printf("page fault");
635 case EXCEPTION_DATATYPE_MISALIGNMENT:
636 dbg_printf("Alignment");
644 case STATUS_POSSIBLE_DEADLOCK:
648 addr.Mode = AddrModeFlat;
649 addr.Offset = rec->ExceptionInformation[0];
651 dbg_printf("wait failed on critical section ");
652 print_address(&addr, FALSE);
654 if (!DBG_IVAR(BreakOnCritSectTimeOut))
657 return DBG_EXCEPTION_NOT_HANDLED;
660 case EXCEPTION_WINE_STUB:
662 char dll[32], name[64];
663 memory_get_string(dbg_curr_process->handle,
664 (void*)rec->ExceptionInformation[0], TRUE, FALSE,
666 if (HIWORD(rec->ExceptionInformation[1]))
667 memory_get_string(dbg_curr_process->handle,
668 (void*)rec->ExceptionInformation[1], TRUE, FALSE,
671 sprintf( name, "%ld", rec->ExceptionInformation[1] );
672 dbg_printf("unimplemented function %s.%s called", dll, name);
675 case EXCEPTION_WINE_ASSERTION:
676 dbg_printf("assertion failed");
678 case EXCEPTION_VM86_INTx:
679 dbg_printf("interrupt %02lx in vm86 mode", rec->ExceptionInformation[0]);
681 case EXCEPTION_VM86_STI:
682 dbg_printf("sti in vm86 mode");
684 case EXCEPTION_VM86_PICRETURN:
685 dbg_printf("PIC return in vm86 mode");
687 case EXCEPTION_FLT_DENORMAL_OPERAND:
688 dbg_printf("denormal float operand");
690 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
691 dbg_printf("divide by zero");
693 case EXCEPTION_FLT_INEXACT_RESULT:
694 dbg_printf("inexact float result");
696 case EXCEPTION_FLT_INVALID_OPERATION:
697 dbg_printf("invalid float operation");
699 case EXCEPTION_FLT_OVERFLOW:
700 dbg_printf("floating pointer overflow");
702 case EXCEPTION_FLT_UNDERFLOW:
703 dbg_printf("floating pointer underflow");
705 case EXCEPTION_FLT_STACK_CHECK:
706 dbg_printf("floating point stack check");
709 dbg_printf("0x%08lx", rec->ExceptionCode);
714 if (dbg_action_mode == automatic_mode)
716 dbg_exception_prolog(is_debug, rec);
717 dbg_exception_epilog();
718 return 0; /* terminate execution */
721 if (dbg_exception_prolog(is_debug, rec))
723 dbg_interactiveP = TRUE;
726 dbg_exception_epilog();
731 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
734 DWORD cont = DBG_CONTINUE;
736 dbg_curr_pid = de->dwProcessId;
737 dbg_curr_tid = de->dwThreadId;
739 if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
740 dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
742 dbg_curr_thread = NULL;
744 switch (de->dwDebugEventCode)
746 case EXCEPTION_DEBUG_EVENT:
747 if (!dbg_curr_thread)
749 WINE_ERR("%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
750 de->dwProcessId, de->dwThreadId);
754 WINE_TRACE("%08lx:%08lx: exception code=%08lx\n",
755 de->dwProcessId, de->dwThreadId,
756 de->u.Exception.ExceptionRecord.ExceptionCode);
758 if (dbg_curr_process->continue_on_first_exception)
760 dbg_curr_process->continue_on_first_exception = FALSE;
761 if (!DBG_IVAR(BreakOnAttach)) break;
763 if (dbg_fetch_context())
765 cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
766 de->u.Exception.dwFirstChance);
767 if (cont && dbg_curr_thread)
769 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
774 case CREATE_PROCESS_DEBUG_EVENT:
775 memory_get_string_indirect(de->u.CreateProcessInfo.hProcess,
776 de->u.CreateProcessInfo.lpImageName,
777 de->u.CreateProcessInfo.fUnicode,
778 buffer, sizeof(buffer));
779 WINE_TRACE("%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
780 de->dwProcessId, de->dwThreadId,
781 buffer, de->u.CreateProcessInfo.lpImageName,
782 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress,
783 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
784 de->u.CreateProcessInfo.nDebugInfoSize);
786 dbg_curr_process = dbg_add_process(de->dwProcessId,
787 de->u.CreateProcessInfo.hProcess,
788 buffer[0] ? buffer : "<Debugged Process>");
789 if (dbg_curr_process == NULL)
791 WINE_ERR("Couldn't create process\n");
794 if (!SymInitialize(dbg_curr_process->handle, NULL, TRUE))
795 dbg_printf("Couldn't initiate DbgHelp\n");
797 WINE_TRACE("%08lx:%08lx: create thread I @%08lx\n",
798 de->dwProcessId, de->dwThreadId,
799 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress);
801 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
803 de->u.CreateProcessInfo.hThread,
804 de->u.CreateProcessInfo.lpThreadLocalBase);
805 if (!dbg_curr_thread)
807 WINE_ERR("Couldn't create thread\n");
810 dbg_init_current_process();
811 dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
814 case EXIT_PROCESS_DEBUG_EVENT:
815 WINE_TRACE("%08lx:%08lx: exit process (%ld)\n",
816 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
818 if (dbg_curr_process == NULL)
820 WINE_ERR("Unknown process\n");
823 if (!SymCleanup(dbg_curr_process->handle))
824 dbg_printf("Couldn't initiate DbgHelp\n");
826 break_set_xpoints(FALSE);
827 /* kill last thread */
828 dbg_del_thread(dbg_curr_process->threads);
829 dbg_del_process(dbg_curr_process);
831 dbg_printf("Process of pid=0x%08lx has terminated\n", dbg_curr_pid);
834 case CREATE_THREAD_DEBUG_EVENT:
835 WINE_TRACE("%08lx:%08lx: create thread D @%08lx\n",
836 de->dwProcessId, de->dwThreadId,
837 (unsigned long)(void*)de->u.CreateThread.lpStartAddress);
839 if (dbg_curr_process == NULL)
841 WINE_ERR("Unknown process\n");
844 if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
846 WINE_TRACE("Thread already listed, skipping\n");
850 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
852 de->u.CreateThread.hThread,
853 de->u.CreateThread.lpThreadLocalBase);
854 if (!dbg_curr_thread)
856 WINE_ERR("Couldn't create thread\n");
859 dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
862 case EXIT_THREAD_DEBUG_EVENT:
863 WINE_TRACE("%08lx:%08lx: exit thread (%ld)\n",
864 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
866 if (dbg_curr_thread == NULL)
868 WINE_ERR("Unknown thread\n");
871 /* FIXME: remove break point set on thread startup */
872 dbg_del_thread(dbg_curr_thread);
875 case LOAD_DLL_DEBUG_EVENT:
876 if (dbg_curr_thread == NULL)
878 WINE_ERR("Unknown thread\n");
881 memory_get_string_indirect(dbg_curr_process->handle,
882 de->u.LoadDll.lpImageName,
883 de->u.LoadDll.fUnicode,
884 buffer, sizeof(buffer));
886 WINE_TRACE("%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
887 de->dwProcessId, de->dwThreadId,
888 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
889 de->u.LoadDll.dwDebugInfoFileOffset,
890 de->u.LoadDll.nDebugInfoSize);
892 SymLoadModule(dbg_curr_process->handle, de->u.LoadDll.hFile, buffer, NULL,
893 (unsigned long)de->u.LoadDll.lpBaseOfDll, 0);
894 break_set_xpoints(FALSE);
895 break_check_delayed_bp();
896 break_set_xpoints(TRUE);
897 if (DBG_IVAR(BreakOnDllLoad))
899 dbg_printf("Stopping on DLL %s loading at 0x%08lx\n",
900 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
901 if (dbg_fetch_context()) cont = 0;
905 case UNLOAD_DLL_DEBUG_EVENT:
906 WINE_TRACE("%08lx:%08lx: unload DLL @%08lx\n",
907 de->dwProcessId, de->dwThreadId,
908 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
909 break_delete_xpoints_from_module((unsigned long)de->u.UnloadDll.lpBaseOfDll);
910 SymUnloadModule(dbg_curr_process->handle,
911 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
914 case OUTPUT_DEBUG_STRING_EVENT:
915 if (dbg_curr_thread == NULL)
917 WINE_ERR("Unknown thread\n");
921 memory_get_string(dbg_curr_process->handle,
922 de->u.DebugString.lpDebugStringData, TRUE,
923 de->u.DebugString.fUnicode, buffer, sizeof(buffer));
924 WINE_TRACE("%08lx:%08lx: output debug string (%s)\n",
925 de->dwProcessId, de->dwThreadId, buffer);
929 WINE_TRACE("%08lx:%08lx: rip error=%ld type=%ld\n",
930 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
931 de->u.RipInfo.dwType);
935 WINE_TRACE("%08lx:%08lx: unknown event (%ld)\n",
936 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
938 if (!cont) return TRUE; /* stop execution */
939 ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
940 return FALSE; /* continue execution */
943 static void dbg_resume_debuggee(DWORD cont)
945 if (dbg_curr_thread->in_exception)
949 dbg_exception_epilog();
950 memory_get_current_pc(&addr);
951 WINE_TRACE("Exiting debugger PC=0x%lx mode=%d count=%d\n",
952 addr.Offset, dbg_curr_thread->exec_mode,
953 dbg_curr_thread->exec_count);
956 if (!SetThreadContext(dbg_curr_thread->handle, &dbg_context))
957 dbg_printf("Cannot set ctx on %lu\n", dbg_curr_tid);
960 dbg_interactiveP = FALSE;
961 if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
962 dbg_printf("Cannot continue on %lu (%lu)\n", dbg_curr_tid, cont);
965 void dbg_wait_next_exception(DWORD cont, int count, int mode)
970 if (cont == DBG_CONTINUE)
972 dbg_curr_thread->exec_count = count;
973 dbg_curr_thread->exec_mode = mode;
975 dbg_resume_debuggee(cont);
977 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
979 if (dbg_handle_debug_event(&de)) break;
981 if (!dbg_curr_process) return;
982 dbg_interactiveP = TRUE;
984 memory_get_current_pc(&addr);
985 WINE_TRACE("Entering debugger PC=0x%lx mode=%d count=%d\n",
986 addr.Offset, dbg_curr_thread->exec_mode,
987 dbg_curr_thread->exec_count);
990 static unsigned dbg_main_loop(void)
994 if (dbg_curr_process)
995 dbg_printf("WineDbg starting on pid 0x%lx\n", dbg_curr_pid);
997 /* wait for first exception */
998 while (WaitForDebugEvent(&de, INFINITE))
1000 if (dbg_handle_debug_event(&de)) break;
1002 switch (dbg_action_mode)
1004 case automatic_mode:
1005 /* print some extra information */
1006 dbg_printf("Modules:\n");
1007 info_win32_module(0); /* print all modules */
1008 dbg_printf("Threads:\n");
1009 info_win32_threads();
1012 dbg_interactiveP = TRUE;
1015 dbg_printf("WineDbg terminated on pid 0x%lx\n", dbg_curr_pid);
1020 static unsigned dbg_start_debuggee(LPSTR cmdLine)
1022 PROCESS_INFORMATION info;
1023 STARTUPINFOA startup;
1025 memset(&startup, 0, sizeof(startup));
1026 startup.cb = sizeof(startup);
1027 startup.dwFlags = STARTF_USESHOWWINDOW;
1028 startup.wShowWindow = SW_SHOWNORMAL;
1030 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
1033 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
1035 DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,
1036 NULL, NULL, &startup, &info))
1038 dbg_printf("Couldn't start process '%s'\n", cmdLine);
1041 if (!info.dwProcessId)
1043 /* this happens when the program being run is not a Wine binary
1044 * (for example, a shell wrapper around a WineLib app)
1046 /* Current fix: list running processes and let the user attach
1047 * to one of them (sic)
1048 * FIXME: implement a real fix => grab the process (from the
1049 * running processes) from its name
1051 dbg_printf("Debuggee has been started (%s)\n"
1052 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
1053 "Try to attach to one of those processes:\n", cmdLine);
1054 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
1056 info_win32_processes();
1059 dbg_curr_pid = info.dwProcessId;
1060 if (!(dbg_curr_process = dbg_add_process(dbg_curr_pid, 0, NULL))) return FALSE;
1065 void dbg_run_debuggee(const char* args)
1069 WINE_FIXME("Re-running current program with %s as args is broken\n", args);
1076 if (!dbg_last_cmd_line)
1078 dbg_printf("Cannot find previously used command line.\n");
1081 dbg_start_debuggee(dbg_last_cmd_line);
1082 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
1084 if (dbg_handle_debug_event(&de)) break;
1086 source_list_from_addr(NULL, 0);
1090 BOOL dbg_interrupt_debuggee(void)
1092 if (!dbg_process_list) return FALSE;
1093 /* FIXME: since we likely have a single process, signal the first process
1096 if (dbg_process_list->next) dbg_printf("Ctrl-C: only stopping the first process\n");
1097 else dbg_printf("Ctrl-C: stopping debuggee\n");
1098 dbg_process_list->continue_on_first_exception = FALSE;
1099 return DebugBreakProcess(dbg_process_list->handle);
1102 static BOOL WINAPI ctrl_c_handler(DWORD dwCtrlType)
1104 if (dwCtrlType == CTRL_C_EVENT)
1106 return dbg_interrupt_debuggee();
1111 static void dbg_init_console(void)
1113 /* set our control-C handler */
1114 SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
1116 /* set our own title */
1117 SetConsoleTitle("Wine Debugger");
1120 static int dbg_winedbg_usage(void)
1122 dbg_printf("Usage: winedbg [--auto] [--gdb] cmdline\n");
1126 struct backend_cpu* be_cpu;
1128 extern struct backend_cpu be_i386;
1130 extern struct backend_cpu be_ppc;
1132 extern struct backend_cpu be_alpha;
1137 int main(int argc, char** argv)
1140 unsigned gdb_flags = 0;
1151 /* Initialize the output */
1152 dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
1154 /* Initialize internal vars */
1155 if (!dbg_load_internal_vars()) return -1;
1158 while (argc > 1 && argv[1][0] == '-')
1160 if (!strcmp(argv[1], "--command"))
1163 arg_command = HeapAlloc(GetProcessHeap(), 0, strlen(argv[1])+2);
1164 strcpy(arg_command, argv[1]);
1165 strcat(arg_command, "\n");
1169 if (!strcmp(argv[1], "--auto"))
1171 if (dbg_action_mode != none_mode) return dbg_winedbg_usage();
1172 dbg_action_mode = automatic_mode;
1173 /* force some internal variables */
1174 DBG_IVAR(BreakOnDllLoad) = 0;
1176 dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
1179 if (!strcmp(argv[1], "--gdb"))
1181 if (dbg_action_mode != none_mode) return dbg_winedbg_usage();
1182 dbg_action_mode = gdb_mode;
1186 if (strcmp(argv[1], "--no-start") == 0 && dbg_action_mode == gdb_mode)
1189 argc--; argv++; /* as we don't use argv[0] */
1192 if (strcmp(argv[1], "--with-xterm") == 0 && dbg_action_mode == gdb_mode)
1195 argc--; argv++; /* as we don't use argv[0] */
1198 return dbg_winedbg_usage();
1201 if (dbg_action_mode == none_mode) dbg_action_mode = winedbg_mode;
1203 /* try the form <myself> pid */
1204 if (dbg_curr_pid == 0 && argc == 2)
1208 dbg_curr_pid = strtol(argv[1], &ptr, 10);
1209 if (dbg_curr_pid == 0 || ptr != argv[1] + strlen(argv[1]) ||
1210 !dbg_attach_debuggee(dbg_curr_pid, dbg_action_mode != gdb_mode, FALSE))
1214 /* try the form <myself> pid evt (Win32 JIT debugger) */
1215 if (dbg_curr_pid == 0 && argc == 3)
1221 if ((pid = strtol(argv[1], &ptr, 10)) != 0 && ptr != NULL &&
1222 (hEvent = (HANDLE)strtol(argv[2], &ptr, 10)) != 0 && ptr != NULL)
1224 if (!dbg_attach_debuggee(pid, TRUE, FALSE))
1226 /* don't care about result */
1230 if (!SetEvent(hEvent))
1232 WINE_ERR("Invalid event handle: %p\n", hEvent);
1235 CloseHandle(hEvent);
1240 if (dbg_curr_pid == 0 && argc > 1)
1245 if (!(cmdLine = HeapAlloc(GetProcessHeap(), 0, len = 1))) goto oom_leave;
1248 for (i = 1; i < argc; i++)
1250 len += strlen(argv[i]) + 1;
1251 if (!(cmdLine = HeapReAlloc(GetProcessHeap(), 0, cmdLine, len)))
1253 strcat(cmdLine, argv[i]);
1254 cmdLine[len - 2] = ' ';
1255 cmdLine[len - 1] = '\0';
1258 if (!dbg_start_debuggee(cmdLine))
1260 dbg_printf("Couldn't start process '%s'\n", cmdLine);
1263 dbg_last_cmd_line = cmdLine;
1265 /* don't save local vars in gdb mode */
1266 if (dbg_action_mode == gdb_mode && dbg_curr_pid)
1267 return gdb_remote(gdb_flags);
1271 SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME)) |
1272 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS);
1274 retv = dbg_main_loop();
1275 /* don't save modified variables in auto mode */
1276 if (dbg_action_mode != automatic_mode) dbg_save_internal_vars();
1282 dbg_printf("Out of memory\n");