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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/port.h"
29 #include "wine/exception.h"
30 #include "wine/library.h"
32 #include "wine/debug.h"
37 * + ensure that all commands work as expected in minidump reload function
38 * (and reenable parser usager)
40 * + we always assume the stack grows as on i386 (ie downwards)
42 * + enable back the limited output (depth of structure printing and number of
44 * + make the output as close as possible to what gdb does
45 * - symbol management:
46 * + symbol table loading is broken
47 * + in symbol_get_lvalue, we don't do any scoping (as C does) between local and
48 * global vars (we may need this to force some display for example). A solution
49 * would be always to return arrays with: local vars, global vars, thunks
51 * + some bits of internal types are missing (like type casts and the address
53 * + the type for an enum's value is always inferred as int (winedbg & dbghelp)
54 * + most of the code implies that sizeof(void*) = sizeof(int)
55 * + all computations should be made on long long
56 * o expr computations are in int:s
57 * o bitfield size is on a 4-bytes
58 * + array_index and deref should be the same function (or should share the same
61 * + set a better fix for gdb (proxy mode) than the step-mode hack
62 * + implement function call in debuggee
63 * + trampoline management is broken when getting 16 <=> 32 thunk destination
65 * + thunking of delayed imports doesn't work as expected (ie, when stepping,
66 * it currently stops at first insn with line number during the library
67 * loading). We should identify this (__wine_delay_import) and set a
68 * breakpoint instead of single stepping the library loading.
69 * + it's wrong to copy thread->step_over_bp into process->bp[0] (when
70 * we have a multi-thread debuggee). complete fix must include storing all
71 * thread's step-over bp in process-wide bp array, and not to handle bp
72 * when we have the wrong thread running into that bp
73 * + code in CREATE_PROCESS debug event doesn't work on Windows, as we cannot
74 * get the name of the main module this way. We should rewrite all this code
75 * and store in struct dbg_process as early as possible (before process
76 * creation or attachment), the name of the main module
78 * + define a better way to enable the wine extensions (either DBG SDK function
79 * in dbghelp, or TLS variable, or environment variable or ...)
80 * + audit all files to ensure that we check all potential return values from
81 * every function call to catch the errors
82 * + BTW check also whether the exception mechanism is the best way to return
83 * errors (or find a proper fix for MinGW port)
84 * + use Wine standard list mechanism for all list handling
87 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
89 struct dbg_process* dbg_curr_process = NULL;
90 struct dbg_thread* dbg_curr_thread = NULL;
91 DWORD_PTR dbg_curr_tid = 0;
92 DWORD_PTR dbg_curr_pid = 0;
94 BOOL dbg_interactiveP = FALSE;
96 static struct dbg_process* dbg_process_list = NULL;
98 struct dbg_internal_var dbg_internal_vars[DBG_IV_LAST];
99 static HANDLE dbg_houtput;
101 static void dbg_outputA(const char* buffer, int len)
103 static char line_buff[4096];
104 static unsigned int line_pos;
110 unsigned int count = min( len, sizeof(line_buff) - line_pos );
111 memcpy( line_buff + line_pos, buffer, count );
115 for (i = line_pos; i > 0; i--) if (line_buff[i-1] == '\n') break;
116 if (!i) /* no newline found */
118 if (len > 0) i = line_pos; /* buffer is full, flush anyway */
121 WriteFile(dbg_houtput, line_buff, i, &w, NULL);
122 memmove( line_buff, line_buff + i, line_pos - i );
127 const char* dbg_W2A(const WCHAR* buffer, unsigned len)
129 static unsigned ansilen;
133 newlen = WideCharToMultiByte(CP_ACP, 0, buffer, len, NULL, 0, NULL, NULL);
134 if (newlen > ansilen)
136 static char* newansi;
138 newansi = HeapReAlloc(GetProcessHeap(), 0, ansi, newlen);
140 newansi = HeapAlloc(GetProcessHeap(), 0, newlen);
141 if (!newansi) return NULL;
145 WideCharToMultiByte(CP_ACP, 0, buffer, len, ansi, newlen, NULL, NULL);
149 void dbg_outputW(const WCHAR* buffer, int len)
151 const char* ansi = dbg_W2A(buffer, len);
152 if (ansi) dbg_outputA(ansi, strlen(ansi));
153 /* FIXME: should CP_ACP be GetConsoleCP()? */
156 int dbg_printf(const char* format, ...)
158 static char buf[4*1024];
162 va_start(valist, format);
163 len = vsnprintf(buf, sizeof(buf), format, valist);
166 if (len <= -1 || len >= sizeof(buf))
168 len = sizeof(buf) - 1;
170 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
172 dbg_outputA(buf, len);
176 static unsigned dbg_load_internal_vars(void)
179 DWORD type = REG_DWORD;
181 DWORD count = sizeof(val);
183 struct dbg_internal_var* div = dbg_internal_vars;
185 /* initializes internal vars table */
186 #define INTERNAL_VAR(_var,_val,_ref,_tid) \
187 div->val = _val; div->name = #_var; div->pval = _ref; \
188 div->typeid = _tid; div++;
192 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
193 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
195 WINE_ERR("Cannot create WineDbg key in registry\n");
199 for (i = 0; i < DBG_IV_LAST; i++)
201 if (!dbg_internal_vars[i].pval)
203 if (!RegQueryValueExA(hkey, dbg_internal_vars[i].name, 0,
204 &type, (LPBYTE)&val, &count))
205 dbg_internal_vars[i].val = val;
206 dbg_internal_vars[i].pval = &dbg_internal_vars[i].val;
214 static unsigned dbg_save_internal_vars(void)
219 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
220 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
222 WINE_ERR("Cannot create WineDbg key in registry\n");
226 for (i = 0; i < DBG_IV_LAST; i++)
228 /* FIXME: type should be inferred from basic type -if any- of intvar */
229 if (dbg_internal_vars[i].pval == &dbg_internal_vars[i].val)
230 RegSetValueExA(hkey, dbg_internal_vars[i].name, 0,
231 REG_DWORD, (const void*)dbg_internal_vars[i].pval,
232 sizeof(*dbg_internal_vars[i].pval));
238 const struct dbg_internal_var* dbg_get_internal_var(const char* name)
240 const struct dbg_internal_var* div;
242 for (div = &dbg_internal_vars[DBG_IV_LAST - 1]; div >= dbg_internal_vars; div--)
244 if (!strcmp(div->name, name)) return div;
246 for (div = be_cpu->context_vars; div->name; div++)
248 if (!strcasecmp(div->name, name))
250 struct dbg_internal_var* ret = (void*)lexeme_alloc_size(sizeof(*ret));
251 /* relocate register's field against current context */
253 ret->pval = (DWORD_PTR*)((char*)&dbg_context + (DWORD_PTR)div->pval);
261 unsigned dbg_num_processes(void)
263 struct dbg_process* p;
266 for (p = dbg_process_list; p; p = p->next)
271 struct dbg_process* dbg_get_process(DWORD pid)
273 struct dbg_process* p;
275 for (p = dbg_process_list; p; p = p->next)
276 if (p->pid == pid) break;
280 struct dbg_process* dbg_get_process_h(HANDLE h)
282 struct dbg_process* p;
284 for (p = dbg_process_list; p; p = p->next)
285 if (p->handle == h) break;
289 struct dbg_process* dbg_add_process(const struct be_process_io* pio, DWORD pid, HANDLE h)
291 struct dbg_process* p;
293 if ((p = dbg_get_process(pid)))
297 WINE_ERR("Process (%04x) is already defined\n", pid);
308 if (!(p = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_process)))) return NULL;
315 p->continue_on_first_exception = FALSE;
316 p->active_debuggee = FALSE;
317 p->next_bp = 1; /* breakpoint 0 is reserved for step-over */
318 memset(p->bp, 0, sizeof(p->bp));
319 p->delayed_bp = NULL;
320 p->num_delayed_bp = 0;
321 p->source_ofiles = NULL;
322 p->search_path = NULL;
323 p->source_current_file[0] = '\0';
324 p->source_start_line = -1;
325 p->source_end_line = -1;
327 p->next = dbg_process_list;
329 if (dbg_process_list) dbg_process_list->prev = p;
330 dbg_process_list = p;
334 void dbg_set_process_name(struct dbg_process* p, const WCHAR* imageName)
336 assert(p->imageName == NULL);
339 WCHAR* tmp = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(imageName) + 1) * sizeof(WCHAR));
340 if (tmp) p->imageName = lstrcpyW(tmp, imageName);
344 void dbg_del_process(struct dbg_process* p)
348 while (p->threads) dbg_del_thread(p->threads);
350 for (i = 0; i < p->num_delayed_bp; i++)
351 if (p->delayed_bp[i].is_symbol)
352 HeapFree(GetProcessHeap(), 0, p->delayed_bp[i].u.symbol.name);
354 HeapFree(GetProcessHeap(), 0, p->delayed_bp);
356 source_free_files(p);
357 if (p->prev) p->prev->next = p->next;
358 if (p->next) p->next->prev = p->prev;
359 if (p == dbg_process_list) dbg_process_list = p->next;
360 if (p == dbg_curr_process) dbg_curr_process = NULL;
361 HeapFree(GetProcessHeap(), 0, (char*)p->imageName);
362 HeapFree(GetProcessHeap(), 0, p);
365 /******************************************************************
368 * Initializes the dbghelp library, and also sets the application directory
369 * as a place holder for symbol searches.
371 BOOL dbg_init(HANDLE hProc, const WCHAR* in, BOOL invade)
375 ret = SymInitialize(hProc, NULL, invade);
380 for (last = in + lstrlenW(in) - 1; last >= in; last--)
382 if (*last == '/' || *last == '\\')
385 tmp = HeapAlloc(GetProcessHeap(), 0, (1024 + 1 + (last - in) + 1) * sizeof(WCHAR));
386 if (tmp && SymGetSearchPathW(hProc, tmp, 1024))
388 WCHAR* x = tmp + lstrlenW(tmp);
391 memcpy(x, in, (last - in) * sizeof(WCHAR));
393 ret = SymSetSearchPathW(hProc, tmp);
396 HeapFree(GetProcessHeap(), 0, tmp);
404 struct mod_loader_info
407 IMAGEHLP_MODULE64* imh_mod;
410 static BOOL CALLBACK mod_loader_cb(PCSTR mod_name, DWORD64 base, PVOID ctx)
412 struct mod_loader_info* mli = ctx;
414 if (!strcmp(mod_name, "<wine-loader>"))
416 if (SymGetModuleInfo64(mli->handle, base, mli->imh_mod))
417 return FALSE; /* stop enum */
422 BOOL dbg_get_debuggee_info(HANDLE hProcess, IMAGEHLP_MODULE64* imh_mod)
424 struct mod_loader_info mli;
427 /* this will resynchronize builtin dbghelp's internal ELF module list */
428 SymLoadModule(hProcess, 0, 0, 0, 0, 0);
429 mli.handle = hProcess;
430 mli.imh_mod = imh_mod;
431 imh_mod->SizeOfStruct = sizeof(*imh_mod);
432 imh_mod->BaseOfImage = 0;
433 /* this is a wine specific options to return also ELF modules in the
436 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
437 SymEnumerateModules64(hProcess, mod_loader_cb, (void*)&mli);
440 return imh_mod->BaseOfImage != 0;
443 BOOL dbg_load_module(HANDLE hProc, HANDLE hFile, const WCHAR* name, DWORD_PTR base, DWORD size)
445 BOOL ret = SymLoadModuleExW(hProc, NULL, name, NULL, base, size, NULL, 0);
448 IMAGEHLP_MODULEW64 ihm;
449 ihm.SizeOfStruct = sizeof(ihm);
450 if (SymGetModuleInfoW64(hProc, base, &ihm) && (ihm.PdbUnmatched || ihm.DbgUnmatched))
451 dbg_printf("Loaded unmatched debug information for %s\n", wine_dbgstr_w(name));
456 struct dbg_thread* dbg_get_thread(struct dbg_process* p, DWORD tid)
458 struct dbg_thread* t;
461 for (t = p->threads; t; t = t->next)
462 if (t->tid == tid) break;
466 struct dbg_thread* dbg_add_thread(struct dbg_process* p, DWORD tid,
469 struct dbg_thread* t = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_thread));
478 t->exec_mode = dbg_exec_cont;
480 t->step_over_bp.enabled = FALSE;
481 t->step_over_bp.refcount = 0;
482 t->stopped_xpoint = -1;
483 t->in_exception = FALSE;
487 t->addr_mode = AddrModeFlat;
489 snprintf(t->name, sizeof(t->name), "%04x", tid);
491 t->next = p->threads;
493 if (p->threads) p->threads->prev = t;
499 void dbg_del_thread(struct dbg_thread* t)
501 HeapFree(GetProcessHeap(), 0, t->frames);
502 if (t->prev) t->prev->next = t->next;
503 if (t->next) t->next->prev = t->prev;
504 if (t == t->process->threads) t->process->threads = t->next;
505 if (t == dbg_curr_thread) dbg_curr_thread = NULL;
506 HeapFree(GetProcessHeap(), 0, t);
509 void dbg_set_option(const char* option, const char* val)
511 if (!strcasecmp(option, "module_load_mismatched"))
513 DWORD opt = SymGetOptions();
515 dbg_printf("Option: module_load_mismatched %s\n", opt & SYMOPT_LOAD_ANYTHING ? "true" : "false");
516 else if (!strcasecmp(val, "true")) opt |= SYMOPT_LOAD_ANYTHING;
517 else if (!strcasecmp(val, "false")) opt &= ~SYMOPT_LOAD_ANYTHING;
520 dbg_printf("Syntax: module_load_mismatched [true|false]\n");
525 else if (!strcasecmp(option, "symbol_picker"))
528 dbg_printf("Option: symbol_picker %s\n",
529 symbol_current_picker == symbol_picker_interactive ? "interactive" : "scoped");
530 else if (!strcasecmp(val, "interactive"))
531 symbol_current_picker = symbol_picker_interactive;
532 else if (!strcasecmp(val, "scoped"))
533 symbol_current_picker = symbol_picker_scoped;
536 dbg_printf("Syntax: symbol_picker [interactive|scoped]\n");
540 else dbg_printf("Unknown option '%s'\n", option);
543 BOOL dbg_interrupt_debuggee(void)
545 if (!dbg_process_list) return FALSE;
546 /* FIXME: since we likely have a single process, signal the first process
549 if (dbg_process_list->next) dbg_printf("Ctrl-C: only stopping the first process\n");
550 else dbg_printf("Ctrl-C: stopping debuggee\n");
551 dbg_process_list->continue_on_first_exception = FALSE;
552 return DebugBreakProcess(dbg_process_list->handle);
555 static BOOL WINAPI ctrl_c_handler(DWORD dwCtrlType)
557 if (dwCtrlType == CTRL_C_EVENT)
559 return dbg_interrupt_debuggee();
564 void dbg_init_console(void)
566 /* set the output handle */
567 dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
569 /* set our control-C handler */
570 SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
572 /* set our own title */
573 SetConsoleTitleA("Wine Debugger");
576 static int dbg_winedbg_usage(BOOL advanced)
580 dbg_printf("Usage:\n"
581 " winedbg cmdline launch process 'cmdline' (as if you were starting\n"
582 " it with wine) and run WineDbg on it\n"
583 " winedbg <num> attach to running process of pid <num> and run\n"
585 " winedbg --gdb cmdline launch process 'cmdline' (as if you were starting\n"
586 " wine) and run gdb (proxied) on it\n"
587 " winedbg --gdb <num> attach to running process of pid <num> and run\n"
588 " gdb (proxied) on it\n"
589 " winedbg file.mdmp reload the minidump file.mdmp into memory and run\n"
591 " winedbg --help prints advanced options\n");
594 dbg_printf("Usage:\n\twinedbg [ [ --gdb ] [ prog-name [ prog-args ] | <num> | file.mdmp | --help ]\n");
598 void dbg_start_interactive(HANDLE hFile)
600 if (dbg_curr_process)
602 dbg_printf("WineDbg starting on pid %04lx\n", dbg_curr_pid);
603 if (dbg_curr_process->active_debuggee) dbg_active_wait_for_first_exception();
606 dbg_interactiveP = TRUE;
607 parser_handle(hFile);
609 while (dbg_process_list)
610 dbg_process_list->process_io->close_process(dbg_process_list, FALSE);
612 dbg_save_internal_vars();
615 struct backend_cpu* be_cpu;
617 extern struct backend_cpu be_i386;
618 #elif defined(__powerpc__)
619 extern struct backend_cpu be_ppc;
620 #elif defined(__ALPHA__)
621 extern struct backend_cpu be_alpha;
622 #elif defined(__x86_64__)
623 extern struct backend_cpu be_x86_64;
628 int main(int argc, char** argv)
631 HANDLE hFile = INVALID_HANDLE_VALUE;
636 #elif defined(__powerpc__)
638 #elif defined(__ALPHA__)
640 #elif defined(__x86_64__)
645 /* Initialize the output */
646 dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
648 /* Initialize internal vars */
649 if (!dbg_load_internal_vars()) return -1;
651 /* as we don't care about exec name */
654 if (argc && !strcmp(argv[0], "--help"))
655 return dbg_winedbg_usage(TRUE);
657 if (argc && !strcmp(argv[0], "--gdb"))
659 retv = gdb_main(argc, argv);
660 if (retv == -1) dbg_winedbg_usage(FALSE);
665 SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME)) |
666 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS);
668 if (argc && (!strcmp(argv[0], "--auto") || !strcmp(argv[0], "--minidump")))
670 /* force some internal variables */
671 DBG_IVAR(BreakOnDllLoad) = 0;
672 dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
673 switch (dbg_active_auto(argc, argv))
675 case start_ok: return 0;
676 case start_error_parse: return dbg_winedbg_usage(FALSE);
677 case start_error_init: return -1;
681 while (argc > 0 && argv[0][0] == '-')
683 if (!strcmp(argv[0], "--command"))
686 hFile = parser_generate_command_file(argv[0], NULL);
687 if (hFile == INVALID_HANDLE_VALUE)
689 dbg_printf("Couldn't open temp file (%u)\n", GetLastError());
695 if (!strcmp(argv[0], "--file"))
698 hFile = CreateFileA(argv[0], GENERIC_READ|DELETE, 0,
699 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
700 if (hFile == INVALID_HANDLE_VALUE)
702 dbg_printf("Couldn't open file %s (%u)\n", argv[0], GetLastError());
708 if (!strcmp(argv[0], "--"))
713 return dbg_winedbg_usage(FALSE);
715 if (!argc) ds = start_ok;
716 else if ((ds = dbg_active_attach(argc, argv)) == start_error_parse &&
717 (ds = minidump_reload(argc, argv)) == start_error_parse)
718 ds = dbg_active_launch(argc, argv);
721 case start_ok: break;
722 case start_error_parse: return dbg_winedbg_usage(FALSE);
723 case start_error_init: return -1;
726 dbg_start_interactive(hFile);