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 re-enable parser usage)
40 * + we always assume the stack grows as on i386 (i.e. downwards)
42 * + re-enable 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
59 * + set a better fix for gdb (proxy mode) than the step-mode hack
60 * + implement function call in debuggee
61 * + trampoline management is broken when getting 16 <=> 32 thunk destination
63 * + thunking of delayed imports doesn't work as expected (ie, when stepping,
64 * it currently stops at first insn with line number during the library
65 * loading). We should identify this (__wine_delay_import) and set a
66 * breakpoint instead of single stepping the library loading.
67 * + it's wrong to copy thread->step_over_bp into process->bp[0] (when
68 * we have a multi-thread debuggee). complete fix must include storing all
69 * thread's step-over bp in process-wide bp array, and not to handle bp
70 * when we have the wrong thread running into that bp
71 * + code in CREATE_PROCESS debug event doesn't work on Windows, as we cannot
72 * get the name of the main module this way. We should rewrite all this code
73 * and store in struct dbg_process as early as possible (before process
74 * creation or attachment), the name of the main module
76 * + define a better way to enable the wine extensions (either DBG SDK function
77 * in dbghelp, or TLS variable, or environment variable or ...)
78 * + audit all files to ensure that we check all potential return values from
79 * every function call to catch the errors
80 * + BTW check also whether the exception mechanism is the best way to return
81 * errors (or find a proper fix for MinGW port)
84 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
86 struct dbg_process
* dbg_curr_process
= NULL
;
87 struct dbg_thread
* dbg_curr_thread
= NULL
;
88 DWORD_PTR dbg_curr_tid
= 0;
89 DWORD_PTR dbg_curr_pid
= 0;
90 dbg_ctx_t dbg_context
;
91 BOOL dbg_interactiveP
= FALSE
;
92 HANDLE dbg_houtput
= 0;
94 static struct list dbg_process_list
= LIST_INIT(dbg_process_list
);
96 struct dbg_internal_var dbg_internal_vars
[DBG_IV_LAST
];
98 static void dbg_outputA(const char* buffer
, int len
)
100 static char line_buff
[4096];
101 static unsigned int line_pos
;
107 unsigned int count
= min( len
, sizeof(line_buff
) - line_pos
);
108 memcpy( line_buff
+ line_pos
, buffer
, count
);
112 for (i
= line_pos
; i
> 0; i
--) if (line_buff
[i
-1] == '\n') break;
113 if (!i
) /* no newline found */
115 if (len
> 0) i
= line_pos
; /* buffer is full, flush anyway */
118 WriteFile(dbg_houtput
, line_buff
, i
, &w
, NULL
);
119 memmove( line_buff
, line_buff
+ i
, line_pos
- i
);
124 const char* dbg_W2A(const WCHAR
* buffer
, unsigned len
)
126 static unsigned ansilen
;
130 newlen
= WideCharToMultiByte(CP_ACP
, 0, buffer
, len
, NULL
, 0, NULL
, NULL
);
131 if (newlen
> ansilen
)
133 static char* newansi
;
135 newansi
= HeapReAlloc(GetProcessHeap(), 0, ansi
, newlen
);
137 newansi
= HeapAlloc(GetProcessHeap(), 0, newlen
);
138 if (!newansi
) return NULL
;
142 WideCharToMultiByte(CP_ACP
, 0, buffer
, len
, ansi
, newlen
, NULL
, NULL
);
146 void dbg_outputW(const WCHAR
* buffer
, int len
)
148 const char* ansi
= dbg_W2A(buffer
, len
);
149 if (ansi
) dbg_outputA(ansi
, strlen(ansi
));
150 /* FIXME: should CP_ACP be GetConsoleCP()? */
153 int dbg_printf(const char* format
, ...)
155 static char buf
[4*1024];
159 va_start(valist
, format
);
160 len
= vsnprintf(buf
, sizeof(buf
), format
, valist
);
163 if (len
<= -1 || len
>= sizeof(buf
))
165 len
= sizeof(buf
) - 1;
167 buf
[len
- 1] = buf
[len
- 2] = buf
[len
- 3] = '.';
169 dbg_outputA(buf
, len
);
173 static unsigned dbg_load_internal_vars(void)
176 DWORD type
= REG_DWORD
;
178 DWORD count
= sizeof(val
);
180 struct dbg_internal_var
* div
= dbg_internal_vars
;
182 /* initializes internal vars table */
183 #define INTERNAL_VAR(_var,_val,_ref,_tid) \
184 div->val = _val; div->name = #_var; div->pval = _ref; \
185 div->typeid = _tid; div++;
189 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
190 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
))
192 WINE_ERR("Cannot create WineDbg key in registry\n");
196 for (i
= 0; i
< DBG_IV_LAST
; i
++)
198 if (!dbg_internal_vars
[i
].pval
)
200 if (!RegQueryValueExA(hkey
, dbg_internal_vars
[i
].name
, 0,
201 &type
, (LPBYTE
)&val
, &count
))
202 dbg_internal_vars
[i
].val
= val
;
203 dbg_internal_vars
[i
].pval
= &dbg_internal_vars
[i
].val
;
211 static unsigned dbg_save_internal_vars(void)
216 /* @@ Wine registry key: HKCU\Software\Wine\WineDbg */
217 if (RegCreateKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\WineDbg", &hkey
))
219 WINE_ERR("Cannot create WineDbg key in registry\n");
223 for (i
= 0; i
< DBG_IV_LAST
; i
++)
225 /* FIXME: type should be inferred from basic type -if any- of intvar */
226 if (dbg_internal_vars
[i
].pval
== &dbg_internal_vars
[i
].val
)
228 DWORD val
= dbg_internal_vars
[i
].val
;
229 RegSetValueExA(hkey
, dbg_internal_vars
[i
].name
, 0, REG_DWORD
, (BYTE
*)&val
, sizeof(val
));
236 const struct dbg_internal_var
* dbg_get_internal_var(const char* name
)
238 const struct dbg_internal_var
* div
;
240 for (div
= &dbg_internal_vars
[DBG_IV_LAST
- 1]; div
>= dbg_internal_vars
; div
--)
242 if (!strcmp(div
->name
, name
)) return div
;
244 for (div
= dbg_curr_process
->be_cpu
->context_vars
; div
->name
; div
++)
246 if (!strcasecmp(div
->name
, name
))
248 struct dbg_internal_var
* ret
= (void*)lexeme_alloc_size(sizeof(*ret
));
249 /* relocate register's field against current context */
251 ret
->pval
= (DWORD_PTR
*)((char*)&dbg_context
+ (DWORD_PTR
)div
->pval
);
259 unsigned dbg_num_processes(void)
261 return list_count(&dbg_process_list
);
264 struct dbg_process
* dbg_get_process(DWORD pid
)
266 struct dbg_process
* p
;
268 LIST_FOR_EACH_ENTRY(p
, &dbg_process_list
, struct dbg_process
, entry
)
269 if (p
->pid
== pid
) return p
;
273 struct dbg_process
* dbg_get_process_h(HANDLE h
)
275 struct dbg_process
* p
;
277 LIST_FOR_EACH_ENTRY(p
, &dbg_process_list
, struct dbg_process
, entry
)
278 if (p
->handle
== h
) return p
;
283 extern struct backend_cpu be_i386
;
284 #elif defined(__powerpc__)
285 extern struct backend_cpu be_ppc
;
286 #elif defined(__x86_64__)
287 extern struct backend_cpu be_i386
;
288 extern struct backend_cpu be_x86_64
;
289 #elif defined(__arm__) && !defined(__ARMEB__)
290 extern struct backend_cpu be_arm
;
291 #elif defined(__aarch64__) && !defined(__AARCH64EB__)
292 extern struct backend_cpu be_arm64
;
297 struct dbg_process
* dbg_add_process(const struct be_process_io
* pio
, DWORD pid
, HANDLE h
)
299 struct dbg_process
* p
;
302 if ((p
= dbg_get_process(pid
)))
306 h
= OpenProcess(PROCESS_ALL_ACCESS
, FALSE
, pid
);
308 if (!(p
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_process
)))) return NULL
;
314 list_init(&p
->threads
);
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 list_add_head(&dbg_process_list
, &p
->entry
);
329 IsWow64Process(h
, &wow64
);
332 p
->be_cpu
= &be_i386
;
333 #elif defined(__powerpc__)
335 #elif defined(__x86_64__)
336 p
->be_cpu
= wow64
? &be_i386
: &be_x86_64
;
337 #elif defined(__arm__) && !defined(__ARMEB__)
339 #elif defined(__aarch64__) && !defined(__AARCH64EB__)
340 p
->be_cpu
= &be_arm64
;
347 void dbg_set_process_name(struct dbg_process
* p
, const WCHAR
* imageName
)
349 assert(p
->imageName
== NULL
);
352 WCHAR
* tmp
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(imageName
) + 1) * sizeof(WCHAR
));
353 if (tmp
) p
->imageName
= lstrcpyW(tmp
, imageName
);
357 void dbg_del_process(struct dbg_process
* p
)
359 struct dbg_thread
* t
;
360 struct dbg_thread
* t2
;
363 LIST_FOR_EACH_ENTRY_SAFE(t
, t2
, &p
->threads
, struct dbg_thread
, entry
)
366 for (i
= 0; i
< p
->num_delayed_bp
; i
++)
367 if (p
->delayed_bp
[i
].is_symbol
)
368 HeapFree(GetProcessHeap(), 0, p
->delayed_bp
[i
].u
.symbol
.name
);
370 HeapFree(GetProcessHeap(), 0, p
->delayed_bp
);
372 source_free_files(p
);
373 list_remove(&p
->entry
);
374 if (p
== dbg_curr_process
) dbg_curr_process
= NULL
;
375 HeapFree(GetProcessHeap(), 0, (char*)p
->imageName
);
376 HeapFree(GetProcessHeap(), 0, p
);
379 /******************************************************************
382 * Initializes the dbghelp library, and also sets the application directory
383 * as a place holder for symbol searches.
385 BOOL
dbg_init(HANDLE hProc
, const WCHAR
* in
, BOOL invade
)
389 ret
= SymInitialize(hProc
, NULL
, invade
);
394 for (last
= in
+ lstrlenW(in
) - 1; last
>= in
; last
--)
396 if (*last
== '/' || *last
== '\\')
399 tmp
= HeapAlloc(GetProcessHeap(), 0, (1024 + 1 + (last
- in
) + 1) * sizeof(WCHAR
));
400 if (tmp
&& SymGetSearchPathW(hProc
, tmp
, 1024))
402 WCHAR
* x
= tmp
+ lstrlenW(tmp
);
405 memcpy(x
, in
, (last
- in
) * sizeof(WCHAR
));
407 ret
= SymSetSearchPathW(hProc
, tmp
);
410 HeapFree(GetProcessHeap(), 0, tmp
);
418 struct mod_loader_info
421 IMAGEHLP_MODULE64
* imh_mod
;
424 static BOOL CALLBACK
mod_loader_cb(PCSTR mod_name
, DWORD64 base
, PVOID ctx
)
426 struct mod_loader_info
* mli
= ctx
;
428 if (!strcmp(mod_name
, "<wine-loader>"))
430 if (SymGetModuleInfo64(mli
->handle
, base
, mli
->imh_mod
))
431 return FALSE
; /* stop enum */
436 BOOL
dbg_get_debuggee_info(HANDLE hProcess
, IMAGEHLP_MODULE64
* imh_mod
)
438 struct mod_loader_info mli
;
441 /* this will resynchronize builtin dbghelp's internal ELF module list */
442 SymLoadModule(hProcess
, 0, 0, 0, 0, 0);
443 mli
.handle
= hProcess
;
444 mli
.imh_mod
= imh_mod
;
445 imh_mod
->SizeOfStruct
= sizeof(*imh_mod
);
446 imh_mod
->BaseOfImage
= 0;
447 /* this is a wine specific options to return also ELF modules in the
450 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
451 SymEnumerateModules64(hProcess
, mod_loader_cb
, &mli
);
454 return imh_mod
->BaseOfImage
!= 0;
457 BOOL
dbg_load_module(HANDLE hProc
, HANDLE hFile
, const WCHAR
* name
, DWORD_PTR base
, DWORD size
)
459 BOOL ret
= SymLoadModuleExW(hProc
, NULL
, name
, NULL
, base
, size
, NULL
, 0);
462 IMAGEHLP_MODULEW64 ihm
;
463 ihm
.SizeOfStruct
= sizeof(ihm
);
464 if (SymGetModuleInfoW64(hProc
, base
, &ihm
) && (ihm
.PdbUnmatched
|| ihm
.DbgUnmatched
))
465 dbg_printf("Loaded unmatched debug information for %s\n", wine_dbgstr_w(name
));
470 struct dbg_thread
* dbg_get_thread(struct dbg_process
* p
, DWORD tid
)
472 struct dbg_thread
* t
;
475 LIST_FOR_EACH_ENTRY(t
, &p
->threads
, struct dbg_thread
, entry
)
476 if (t
->tid
== tid
) return t
;
480 struct dbg_thread
* dbg_add_thread(struct dbg_process
* p
, DWORD tid
,
483 struct dbg_thread
* t
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct dbg_thread
));
492 t
->exec_mode
= dbg_exec_cont
;
494 t
->step_over_bp
.enabled
= FALSE
;
495 t
->step_over_bp
.refcount
= 0;
496 t
->stopped_xpoint
= -1;
497 t
->in_exception
= FALSE
;
501 t
->addr_mode
= AddrModeFlat
;
503 snprintf(t
->name
, sizeof(t
->name
), "%04x", tid
);
505 list_add_head(&p
->threads
, &t
->entry
);
510 void dbg_del_thread(struct dbg_thread
* t
)
512 HeapFree(GetProcessHeap(), 0, t
->frames
);
513 list_remove(&t
->entry
);
514 if (t
== dbg_curr_thread
) dbg_curr_thread
= NULL
;
515 HeapFree(GetProcessHeap(), 0, t
);
518 void dbg_set_option(const char* option
, const char* val
)
520 if (!strcasecmp(option
, "module_load_mismatched"))
522 DWORD opt
= SymGetOptions();
524 dbg_printf("Option: module_load_mismatched %s\n", opt
& SYMOPT_LOAD_ANYTHING
? "true" : "false");
525 else if (!strcasecmp(val
, "true")) opt
|= SYMOPT_LOAD_ANYTHING
;
526 else if (!strcasecmp(val
, "false")) opt
&= ~SYMOPT_LOAD_ANYTHING
;
529 dbg_printf("Syntax: module_load_mismatched [true|false]\n");
534 else if (!strcasecmp(option
, "symbol_picker"))
537 dbg_printf("Option: symbol_picker %s\n",
538 symbol_current_picker
== symbol_picker_interactive
? "interactive" : "scoped");
539 else if (!strcasecmp(val
, "interactive"))
540 symbol_current_picker
= symbol_picker_interactive
;
541 else if (!strcasecmp(val
, "scoped"))
542 symbol_current_picker
= symbol_picker_scoped
;
545 dbg_printf("Syntax: symbol_picker [interactive|scoped]\n");
549 else dbg_printf("Unknown option '%s'\n", option
);
552 BOOL
dbg_interrupt_debuggee(void)
554 struct dbg_process
* p
;
555 if (list_empty(&dbg_process_list
)) return FALSE
;
556 /* FIXME: since we likely have a single process, signal the first process
559 p
= LIST_ENTRY(list_head(&dbg_process_list
), struct dbg_process
, entry
);
560 if (list_next(&dbg_process_list
, &p
->entry
)) dbg_printf("Ctrl-C: only stopping the first process\n");
561 else dbg_printf("Ctrl-C: stopping debuggee\n");
562 p
->continue_on_first_exception
= FALSE
;
563 return DebugBreakProcess(p
->handle
);
566 static BOOL WINAPI
ctrl_c_handler(DWORD dwCtrlType
)
568 if (dwCtrlType
== CTRL_C_EVENT
)
570 return dbg_interrupt_debuggee();
575 void dbg_init_console(void)
577 /* set the output handle */
578 dbg_houtput
= GetStdHandle(STD_OUTPUT_HANDLE
);
580 /* set our control-C handler */
581 SetConsoleCtrlHandler(ctrl_c_handler
, TRUE
);
583 /* set our own title */
584 SetConsoleTitleA("Wine Debugger");
587 static int dbg_winedbg_usage(BOOL advanced
)
591 dbg_printf("Usage:\n"
592 " winedbg <cmdline> launch process <cmdline> (as if you were starting\n"
593 " it with wine) and run WineDbg on it\n"
594 " winedbg <num> attach to running process of wpid <num> and run\n"
596 " winedbg --gdb <cmdline> launch process <cmdline> (as if you were starting\n"
597 " wine) and run gdb (proxied) on it\n"
598 " winedbg --gdb <num> attach to running process of wpid <num> and run\n"
599 " gdb (proxied) on it\n"
600 " winedbg <file.mdmp> reload the minidump <file.mdmp> into memory and run\n"
602 " winedbg --help prints advanced options\n");
605 dbg_printf("Usage:\n\twinedbg [ [ --gdb ] [ <prog-name> [ <prog-args> ] | <num> | <file.mdmp> | --help ]\n");
609 void dbg_start_interactive(HANDLE hFile
)
611 struct dbg_process
* p
;
612 struct dbg_process
* p2
;
614 if (dbg_curr_process
)
616 dbg_printf("WineDbg starting on pid %04lx\n", dbg_curr_pid
);
617 if (dbg_curr_process
->active_debuggee
) dbg_active_wait_for_first_exception();
620 dbg_interactiveP
= TRUE
;
621 parser_handle(hFile
);
623 LIST_FOR_EACH_ENTRY_SAFE(p
, p2
, &dbg_process_list
, struct dbg_process
, entry
)
624 p
->process_io
->close_process(p
, FALSE
);
626 dbg_save_internal_vars();
629 static LONG CALLBACK
top_filter( EXCEPTION_POINTERS
*ptr
)
631 dbg_printf( "winedbg: Internal crash at %p\n", ptr
->ExceptionRecord
->ExceptionAddress
);
632 return EXCEPTION_EXECUTE_HANDLER
;
635 static void restart_if_wow64(void)
639 if (IsWow64Process( GetCurrentProcess(), &is_wow64
) && is_wow64
)
642 PROCESS_INFORMATION pi
;
643 WCHAR filename
[MAX_PATH
];
647 memset( &si
, 0, sizeof(si
) );
649 GetModuleFileNameW( 0, filename
, MAX_PATH
);
651 Wow64DisableWow64FsRedirection( &redir
);
652 if (CreateProcessW( filename
, GetCommandLineW(), NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
654 WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename
) );
655 WaitForSingleObject( pi
.hProcess
, INFINITE
);
656 GetExitCodeProcess( pi
.hProcess
, &exit_code
);
657 ExitProcess( exit_code
);
659 else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename
), GetLastError() );
660 Wow64RevertWow64FsRedirection( redir
);
664 int main(int argc
, char** argv
)
667 HANDLE hFile
= INVALID_HANDLE_VALUE
;
670 /* Initialize the output */
671 dbg_houtput
= GetStdHandle(STD_OUTPUT_HANDLE
);
673 SetUnhandledExceptionFilter( top_filter
);
675 /* Initialize internal vars */
676 if (!dbg_load_internal_vars()) return -1;
678 /* as we don't care about exec name */
681 if (argc
&& !strcmp(argv
[0], "--help"))
682 return dbg_winedbg_usage(TRUE
);
684 if (argc
&& !strcmp(argv
[0], "--gdb"))
687 retv
= gdb_main(argc
, argv
);
688 if (retv
== -1) dbg_winedbg_usage(FALSE
);
693 SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME
)) |
694 SYMOPT_LOAD_LINES
| SYMOPT_DEFERRED_LOADS
| SYMOPT_AUTO_PUBLICS
);
696 if (argc
&& !strcmp(argv
[0], "--auto"))
698 switch (dbg_active_auto(argc
, argv
))
700 case start_ok
: return 0;
701 case start_error_parse
: return dbg_winedbg_usage(FALSE
);
702 case start_error_init
: return -1;
705 if (argc
&& !strcmp(argv
[0], "--minidump"))
707 switch (dbg_active_minidump(argc
, argv
))
709 case start_ok
: return 0;
710 case start_error_parse
: return dbg_winedbg_usage(FALSE
);
711 case start_error_init
: return -1;
715 while (argc
> 0 && argv
[0][0] == '-')
717 if (!strcmp(argv
[0], "--command"))
720 hFile
= parser_generate_command_file(argv
[0], NULL
);
721 if (hFile
== INVALID_HANDLE_VALUE
)
723 dbg_printf("Couldn't open temp file (%u)\n", GetLastError());
729 if (!strcmp(argv
[0], "--file"))
732 hFile
= CreateFileA(argv
[0], GENERIC_READ
|DELETE
, 0,
733 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
734 if (hFile
== INVALID_HANDLE_VALUE
)
736 dbg_printf("Couldn't open file %s (%u)\n", argv
[0], GetLastError());
742 if (!strcmp(argv
[0], "--"))
747 return dbg_winedbg_usage(FALSE
);
749 if (!argc
) ds
= start_ok
;
750 else if ((ds
= dbg_active_attach(argc
, argv
)) == start_error_parse
&&
751 (ds
= minidump_reload(argc
, argv
)) == start_error_parse
)
752 ds
= dbg_active_launch(argc
, argv
);
755 case start_ok
: break;
756 case start_error_parse
: return dbg_winedbg_usage(FALSE
);
757 case start_error_init
: return -1;
762 dbg_start_interactive(hFile
);