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 * + set a mode where winedbg would start (postmortem debugging) from a minidump
39 * + we always assume the stack grows as on i386 (ie downwards)
41 * + enable back the limited output (depth of structure printing and number of
43 * + make the output as close as possible to what gdb does
44 * - symbol management:
45 * + symbol table loading is broken
46 * + in symbol_get_lvalue, we don't do any scoping (as C does) between local and
47 * global vars (we may need this to force some display for example). A solution
48 * would be always to return arrays with: local vars, global vars, thunks
50 * + some bits of internal types are missing (like type casts and the address
52 * + the type for an enum's value is always inferred as int (winedbg & dbghelp)
53 * + most of the code implies that sizeof(void*) = sizeof(int)
54 * + all computations should be made on long long
55 * o expr computations are in int:s
56 * o bitfield size is on a 4-bytes
57 * + array_index and deref should be the same function (or should share the same
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
72 * + code in CREATE_PROCESS debug event doesn't work on Windows, as we cannot
73 * get the name of the main module this way. We should rewrite all this code
74 * and store in struct dbg_process as early as possible (before process
75 * creation or attachment), the name of the main module
77 * + define a better way to enable the wine extensions (either DBG SDK function
78 * in dbghelp, or TLS variable, or environment variable or ...)
79 * + audit all files to ensure that we check all potential return values from
80 * every function call to catch the errors
81 * + BTW check also whether the exception mechanism is the best way to return
82 * errors (or find a proper fix for MinGW port)
83 * + use Wine standard list mechanism for all list handling
86 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
88 struct dbg_process* dbg_curr_process = NULL;
89 struct dbg_thread* dbg_curr_thread = NULL;
93 BOOL dbg_interactiveP = FALSE;
95 static struct dbg_process* dbg_process_list = NULL;
97 struct dbg_internal_var dbg_internal_vars[DBG_IV_LAST];
98 const struct dbg_internal_var* dbg_context_vars;
99 static HANDLE dbg_houtput;
101 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 void dbg_outputW(const WCHAR* buffer, int len)
132 /* do a serious Unicode to ANSI conversion
133 * FIXME: should CP_ACP be GetConsoleCP()?
135 newlen = WideCharToMultiByte(CP_ACP, 0, buffer, len, NULL, 0, NULL, NULL);
138 if (!(ansi = HeapAlloc(GetProcessHeap(), 0, newlen))) return;
139 WideCharToMultiByte(CP_ACP, 0, buffer, len, ansi, newlen, NULL, NULL);
140 dbg_outputA(ansi, newlen);
141 HeapFree(GetProcessHeap(), 0, ansi);
145 int dbg_printf(const char* format, ...)
147 static char buf[4*1024];
151 va_start(valist, format);
152 len = vsnprintf(buf, sizeof(buf), format, valist);
155 if (len <= -1 || len >= sizeof(buf))
157 len = sizeof(buf) - 1;
159 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
161 dbg_outputA(buf, len);
165 static unsigned dbg_load_internal_vars(void)
168 DWORD type = REG_DWORD;
170 DWORD count = sizeof(val);
172 struct dbg_internal_var* div = dbg_internal_vars;
174 /* initializes internal vars table */
175 #define INTERNAL_VAR(_var,_val,_ref,_tid) \
176 div->val = _val; div->name = #_var; div->pval = _ref; \
177 div->typeid = _tid; div++;
181 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
182 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
184 WINE_ERR("Cannot create WineDbg key in registry\n");
188 for (i = 0; i < DBG_IV_LAST; i++)
190 if (!dbg_internal_vars[i].pval)
192 if (!RegQueryValueEx(hkey, dbg_internal_vars[i].name, 0,
193 &type, (LPBYTE)&val, &count))
194 dbg_internal_vars[i].val = val;
195 dbg_internal_vars[i].pval = &dbg_internal_vars[i].val;
199 /* set up the debug variables for the CPU context */
200 dbg_context_vars = be_cpu->init_registers(&dbg_context);
204 static unsigned dbg_save_internal_vars(void)
209 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
210 if (RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\WineDbg", &hkey))
212 WINE_ERR("Cannot create WineDbg key in registry\n");
216 for (i = 0; i < DBG_IV_LAST; i++)
218 /* FIXME: type should be infered from basic type -if any- of intvar */
219 if (dbg_internal_vars[i].pval == &dbg_internal_vars[i].val)
220 RegSetValueEx(hkey, dbg_internal_vars[i].name, 0,
221 REG_DWORD, (const void*)dbg_internal_vars[i].pval,
222 sizeof(*dbg_internal_vars[i].pval));
228 const struct dbg_internal_var* dbg_get_internal_var(const char* name)
230 const struct dbg_internal_var* div;
232 for (div = &dbg_internal_vars[DBG_IV_LAST - 1]; div >= dbg_internal_vars; div--)
234 if (!strcmp(div->name, name)) return div;
236 for (div = dbg_context_vars; div->name; div++)
238 if (!strcasecmp(div->name, name)) return div;
244 struct dbg_process* dbg_get_process(DWORD pid)
246 struct dbg_process* p;
248 for (p = dbg_process_list; p; p = p->next)
249 if (p->pid == pid) break;
253 struct dbg_process* dbg_add_process(const struct be_process_io* pio, DWORD pid, HANDLE h)
255 struct dbg_process* p;
257 if ((p = dbg_get_process(pid)))
261 WINE_ERR("Process (%lu) is already defined\n", pid);
272 if (!(p = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_process)))) return NULL;
278 p->continue_on_first_exception = FALSE;
279 p->next_bp = 1; /* breakpoint 0 is reserved for step-over */
280 memset(p->bp, 0, sizeof(p->bp));
281 p->delayed_bp = NULL;
282 p->num_delayed_bp = 0;
284 p->next = dbg_process_list;
286 if (dbg_process_list) dbg_process_list->prev = p;
287 dbg_process_list = p;
291 void dbg_set_process_name(struct dbg_process* p, const char* imageName)
293 assert(p->imageName == NULL);
296 char* tmp = HeapAlloc(GetProcessHeap(), 0, strlen(imageName) + 1);
297 if (tmp) p->imageName = strcpy(tmp, imageName);
301 void dbg_del_process(struct dbg_process* p)
305 while (p->threads) dbg_del_thread(p->threads);
307 for (i = 0; i < p->num_delayed_bp; i++)
308 if (p->delayed_bp[i].is_symbol)
309 HeapFree(GetProcessHeap(), 0, p->delayed_bp[i].u.symbol.name);
311 HeapFree(GetProcessHeap(), 0, p->delayed_bp);
312 if (p->prev) p->prev->next = p->next;
313 if (p->next) p->next->prev = p->prev;
314 if (p == dbg_process_list) dbg_process_list = p->next;
315 if (p == dbg_curr_process) dbg_curr_process = NULL;
316 HeapFree(GetProcessHeap(), 0, (char*)p->imageName);
317 HeapFree(GetProcessHeap(), 0, p);
320 struct mod_loader_info
323 IMAGEHLP_MODULE* imh_mod;
326 static BOOL CALLBACK mod_loader_cb(PSTR mod_name, DWORD base, void* ctx)
328 struct mod_loader_info* mli = (struct mod_loader_info*)ctx;
330 if (!strcmp(mod_name, "<wine-loader>"))
332 if (SymGetModuleInfo(mli->handle, base, mli->imh_mod))
333 return FALSE; /* stop enum */
338 BOOL dbg_get_debuggee_info(HANDLE hProcess, IMAGEHLP_MODULE* imh_mod)
340 struct mod_loader_info mli;
343 /* this will resynchronize builtin dbghelp's internal ELF module list */
344 SymLoadModule(hProcess, 0, 0, 0, 0, 0);
345 mli.handle = hProcess;
346 mli.imh_mod = imh_mod;
347 imh_mod->SizeOfStruct = sizeof(*imh_mod);
348 imh_mod->BaseOfImage = 0;
349 /* this is a wine specific options to return also ELF modules in the
352 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
353 SymEnumerateModules(hProcess, mod_loader_cb, (void*)&mli);
356 return imh_mod->BaseOfImage != 0;
359 struct dbg_thread* dbg_get_thread(struct dbg_process* p, DWORD tid)
361 struct dbg_thread* t;
364 for (t = p->threads; t; t = t->next)
365 if (t->tid == tid) break;
369 struct dbg_thread* dbg_add_thread(struct dbg_process* p, DWORD tid,
372 struct dbg_thread* t = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_thread));
381 t->exec_mode = dbg_exec_cont;
383 t->step_over_bp.enabled = FALSE;
384 t->step_over_bp.refcount = 0;
385 t->stopped_xpoint = -1;
386 t->in_exception = FALSE;
390 t->addr_mode = AddrModeFlat;
392 snprintf(t->name, sizeof(t->name), "0x%08lx", tid);
394 t->next = p->threads;
396 if (p->threads) p->threads->prev = t;
402 void dbg_del_thread(struct dbg_thread* t)
404 HeapFree(GetProcessHeap(), 0, t->frames);
405 if (t->prev) t->prev->next = t->next;
406 if (t->next) t->next->prev = t->prev;
407 if (t == t->process->threads) t->process->threads = t->next;
408 if (t == dbg_curr_thread) dbg_curr_thread = NULL;
409 HeapFree(GetProcessHeap(), 0, t);
412 BOOL dbg_interrupt_debuggee(void)
414 if (!dbg_process_list) return FALSE;
415 /* FIXME: since we likely have a single process, signal the first process
418 if (dbg_process_list->next) dbg_printf("Ctrl-C: only stopping the first process\n");
419 else dbg_printf("Ctrl-C: stopping debuggee\n");
420 dbg_process_list->continue_on_first_exception = FALSE;
421 return DebugBreakProcess(dbg_process_list->handle);
424 static BOOL WINAPI ctrl_c_handler(DWORD dwCtrlType)
426 if (dwCtrlType == CTRL_C_EVENT)
428 return dbg_interrupt_debuggee();
433 static void dbg_init_console(void)
435 /* set our control-C handler */
436 SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
438 /* set our own title */
439 SetConsoleTitle("Wine Debugger");
442 static int dbg_winedbg_usage(void)
444 dbg_printf("Usage: winedbg [--command cmd|--file file|--auto] [--gdb [--no-start] [--with-xterm]] cmdline\n");
448 struct backend_cpu* be_cpu;
450 extern struct backend_cpu be_i386;
452 extern struct backend_cpu be_ppc;
454 extern struct backend_cpu be_alpha;
459 int main(int argc, char** argv)
462 HANDLE hFile = INVALID_HANDLE_VALUE;
474 /* Initialize the output */
475 dbg_houtput = GetStdHandle(STD_OUTPUT_HANDLE);
477 /* Initialize internal vars */
478 if (!dbg_load_internal_vars()) return -1;
480 /* as we don't care about exec name */
483 if (argc && !strcmp(argv[0], "--gdb"))
485 retv = gdb_main(argc, argv);
486 if (retv == -1) dbg_winedbg_usage();
491 SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME)) |
492 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS);
494 if (argc && (!strcmp(argv[0], "--auto") || !strcmp(argv[0], "--minidump")))
496 /* force some internal variables */
497 DBG_IVAR(BreakOnDllLoad) = 0;
498 dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
499 switch (dbg_active_auto(argc, argv))
501 case start_ok: return 0;
502 case start_error_parse: return dbg_winedbg_usage();
503 case start_error_init: return -1;
507 while (argc > 0 && argv[0][0] == '-')
509 if (!strcmp(argv[0], "--command"))
512 hFile = parser_generate_command_file(argv[0], NULL);
513 if (hFile == INVALID_HANDLE_VALUE)
515 dbg_printf("Couldn't open temp file (%lu)\n", GetLastError());
521 if (!strcmp(argv[0], "--file"))
524 hFile = CreateFileA(argv[0], GENERIC_READ|DELETE, 0,
525 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
526 if (hFile == INVALID_HANDLE_VALUE)
528 dbg_printf("Couldn't open file %s (%lu)\n", argv[0], GetLastError());
534 if (!strcmp(argv[0], "--"))
539 return dbg_winedbg_usage();
541 if (!argc) ds = start_ok;
542 else if ((ds = dbg_active_attach(argc, argv)) == start_error_parse)
543 ds = dbg_active_launch(argc, argv);
546 case start_ok: break;
547 case start_error_parse: return dbg_winedbg_usage();
548 case start_error_init: return -1;
551 dbg_interactiveP = TRUE;
552 parser_handle(hFile);
554 while (dbg_process_list)
555 dbg_process_list->process_io->close_process(dbg_process_list, TRUE);
557 dbg_save_internal_vars();