2 * Wine debugger - back-end for an active target
4 * Copyright 2000-2006 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
34 static char* dbg_executable
;
35 static char* dbg_last_cmd_line
;
36 static struct be_process_io be_process_active_io
;
38 static void dbg_init_current_process(void)
42 static void dbg_init_current_thread(void* start
)
46 if (list_count(&dbg_curr_process
->threads
) == 1 /* first thread ? */ &&
47 DBG_IVAR(BreakAllThreadsStartup
))
51 break_set_xpoints(FALSE
);
52 addr
.Mode
= AddrModeFlat
;
53 addr
.Offset
= (DWORD_PTR
)start
;
54 break_add_break(&addr
, TRUE
, TRUE
);
55 break_set_xpoints(TRUE
);
60 static unsigned dbg_handle_debug_event(DEBUG_EVENT
* de
);
62 /******************************************************************
65 * Sets the debuggee to <pid>
66 * cofe instructs winedbg what to do when first exception is received
67 * (break=FALSE, continue=TRUE)
68 * wfe is set to TRUE if dbg_attach_debuggee should also proceed with all debug events
69 * until the first exception is received (aka: attach to an already running process)
71 BOOL
dbg_attach_debuggee(DWORD pid
)
73 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, pid
, 0))) return FALSE
;
75 if (!DebugActiveProcess(pid
))
77 dbg_printf("Can't attach process %04lx: error %lu\n", pid
, GetLastError());
78 dbg_del_process(dbg_curr_process
);
82 SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL
);
84 dbg_curr_process
->active_debuggee
= TRUE
;
88 static unsigned dbg_fetch_context(void)
90 if (!dbg_curr_process
->be_cpu
->get_context(dbg_curr_thread
->handle
, &dbg_context
))
92 WINE_WARN("Can't get thread's context\n");
98 BOOL
dbg_set_curr_thread(DWORD tid
)
100 struct dbg_thread
* thread
;
102 if (!dbg_curr_process
)
104 dbg_printf("No process loaded\n");
108 thread
= dbg_get_thread(dbg_curr_process
, tid
);
111 dbg_curr_thread
= thread
;
113 stack_fetch_frames(&dbg_context
);
117 dbg_printf("No such thread\n");
118 return thread
!= NULL
;
121 /***********************************************************************
122 * dbg_exception_prolog
124 * Examine exception and decide if interactive mode is entered(return TRUE)
125 * or exception is silently continued(return FALSE)
126 * is_debug means the exception is a breakpoint or single step exception
128 static BOOL
dbg_exception_prolog(BOOL is_debug
, const EXCEPTION_RECORD
* rec
)
133 memory_get_current_pc(&addr
);
134 break_suspend_execution();
136 /* this will resynchronize builtin dbghelp's internal ELF module list */
137 SymLoadModule(dbg_curr_process
->handle
, 0, 0, 0, 0, 0);
139 if (is_debug
) break_adjust_pc(&addr
, rec
->ExceptionCode
, dbg_curr_thread
->first_chance
, &is_break
);
141 * Do a quiet backtrace so that we have an idea of what the situation
142 * is WRT the source files.
144 stack_fetch_frames(&dbg_context
);
146 if (is_debug
&& !is_break
&& break_should_continue(&addr
, rec
->ExceptionCode
))
149 if (addr
.Mode
!= dbg_curr_thread
->addr_mode
)
151 const char* name
= NULL
;
155 case AddrMode1616
: name
= "16 bit"; break;
156 case AddrMode1632
: name
= "segmented 32 bit"; break;
157 case AddrModeReal
: name
= "vm86"; break;
158 case AddrModeFlat
: name
= dbg_curr_process
->be_cpu
->pointer_size
== 4
159 ? "32 bit" : "64 bit"; break;
161 dbg_printf("In %s mode.\n", name
);
162 dbg_curr_thread
->addr_mode
= addr
.Mode
;
168 /* This is a real crash, dump some info */
169 dbg_curr_process
->be_cpu
->print_context(dbg_curr_thread
->handle
, &dbg_context
, 0);
171 dbg_curr_process
->be_cpu
->print_segment_info(dbg_curr_thread
->handle
, &dbg_context
);
172 stack_backtrace(dbg_curr_tid
);
176 static char* last_name
;
177 static char* last_file
;
179 char buffer
[sizeof(SYMBOL_INFO
) + 256];
180 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
181 void* lin
= memory_to_linear_addr(&addr
);
186 si
->SizeOfStruct
= sizeof(*si
);
187 si
->MaxNameLen
= 256;
188 il
.SizeOfStruct
= sizeof(il
);
189 if (SymFromAddr(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp64
, si
) &&
190 SymGetLineFromAddr64(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp
, &il
))
192 if ((!last_name
|| strcmp(last_name
, si
->Name
)) ||
193 (!last_file
|| strcmp(last_file
, il
.FileName
)))
195 HeapFree(GetProcessHeap(), 0, last_name
);
196 HeapFree(GetProcessHeap(), 0, last_file
);
197 last_name
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si
->Name
) + 1), si
->Name
);
198 last_file
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il
.FileName
) + 1), il
.FileName
);
199 dbg_printf("%s () at %s:%lu\n", last_name
, last_file
, il
.LineNumber
);
203 if (!is_debug
|| is_break
||
204 dbg_curr_thread
->exec_mode
== dbg_exec_step_over_insn
||
205 dbg_curr_thread
->exec_mode
== dbg_exec_step_into_insn
)
207 ADDRESS64 tmp
= addr
;
208 /* Show where we crashed */
209 memory_disasm_one_insn(&tmp
);
211 source_list_from_addr(&addr
, 0);
216 static void dbg_exception_epilog(void)
218 break_restart_execution(dbg_curr_thread
->exec_count
);
220 * This will have gotten absorbed into the breakpoint info
221 * if it was used. Otherwise it would have been ignored.
222 * In any case, we don't mess with it any more.
224 if (dbg_curr_thread
->exec_mode
== dbg_exec_cont
)
225 dbg_curr_thread
->exec_count
= 0;
226 dbg_curr_thread
->in_exception
= FALSE
;
229 static DWORD
dbg_handle_exception(const EXCEPTION_RECORD
* rec
, BOOL first_chance
)
231 BOOL is_debug
= FALSE
;
232 const THREADNAME_INFO
* pThreadName
;
233 struct dbg_thread
* pThread
;
235 assert(dbg_curr_thread
);
237 WINE_TRACE("exception=%lx first_chance=%c\n",
238 rec
->ExceptionCode
, first_chance
? 'Y' : 'N');
240 switch (rec
->ExceptionCode
)
242 case EXCEPTION_BREAKPOINT
:
243 case EXCEPTION_SINGLE_STEP
:
246 case EXCEPTION_WINE_NAME_THREAD
:
247 pThreadName
= (const THREADNAME_INFO
*)(rec
->ExceptionInformation
);
249 if (pThreadName
->dwType
!= 0x1000)
250 return DBG_EXCEPTION_NOT_HANDLED
;
251 if (pThreadName
->dwThreadID
== -1)
252 pThread
= dbg_curr_thread
;
254 pThread
= dbg_get_thread(dbg_curr_process
, pThreadName
->dwThreadID
);
257 dbg_printf("Thread ID=%04lx not in our list of threads -> can't rename\n", pThreadName
->dwThreadID
);
260 if (dbg_read_memory(pThreadName
->szName
, pThread
->name
, sizeof(pThread
->name
)))
262 pThread
->name
[sizeof(pThread
->name
) - 1] = '\0';
263 dbg_printf("Thread ID=%04lx renamed using MSVC extension (name==\"%s\")\n",
264 pThread
->tid
, pThread
->name
);
267 case EXCEPTION_INVALID_HANDLE
:
271 if (first_chance
&& !is_debug
&& !DBG_IVAR(BreakOnFirstChance
) &&
272 !(rec
->ExceptionFlags
& EH_STACK_INVALID
))
274 /* pass exception to program except for debug exceptions */
275 return DBG_EXCEPTION_NOT_HANDLED
;
278 dbg_curr_thread
->excpt_record
= *rec
;
279 dbg_curr_thread
->in_exception
= TRUE
;
280 dbg_curr_thread
->first_chance
= first_chance
;
282 if (!is_debug
) info_win32_exception();
284 if (rec
->ExceptionCode
== STATUS_POSSIBLE_DEADLOCK
&& !DBG_IVAR(BreakOnCritSectTimeOut
))
286 dbg_curr_thread
->in_exception
= FALSE
;
287 return DBG_EXCEPTION_NOT_HANDLED
;
290 if (dbg_exception_prolog(is_debug
, rec
))
292 dbg_interactiveP
= TRUE
;
295 dbg_exception_epilog();
300 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
);
302 void fetch_module_name(void* name_addr
, void* mod_addr
, WCHAR
* buffer
, size_t bufsz
)
304 memory_get_string_indirect(dbg_curr_process
, name_addr
, TRUE
, buffer
, bufsz
);
305 if (!buffer
[0] && !GetModuleFileNameExW(dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
307 if (GetMappedFileNameW( dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
309 /* FIXME: proper NT->Dos conversion */
310 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
312 if (!wcsncmp( buffer
, nt_prefixW
, 4 ))
313 memmove( buffer
, buffer
+ 4, (lstrlenW(buffer
+ 4) + 1) * sizeof(WCHAR
) );
316 swprintf(buffer
, bufsz
, L
"DLL_%08lx", (ULONG_PTR
)mod_addr
);
320 static unsigned dbg_handle_debug_event(DEBUG_EVENT
* de
)
326 DWORD size
, cont
= DBG_CONTINUE
;
328 dbg_curr_pid
= de
->dwProcessId
;
329 dbg_curr_tid
= de
->dwThreadId
;
331 if ((dbg_curr_process
= dbg_get_process(de
->dwProcessId
)) != NULL
)
332 dbg_curr_thread
= dbg_get_thread(dbg_curr_process
, de
->dwThreadId
);
334 dbg_curr_thread
= NULL
;
336 switch (de
->dwDebugEventCode
)
338 case EXCEPTION_DEBUG_EVENT
:
339 if (!dbg_curr_thread
)
341 WINE_ERR("%04lx:%04lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
342 de
->dwProcessId
, de
->dwThreadId
);
346 WINE_TRACE("%04lx:%04lx: exception code=%08lx\n",
347 de
->dwProcessId
, de
->dwThreadId
,
348 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
350 if (dbg_curr_process
->event_on_first_exception
)
352 SetEvent(dbg_curr_process
->event_on_first_exception
);
353 CloseHandle(dbg_curr_process
->event_on_first_exception
);
354 dbg_curr_process
->event_on_first_exception
= NULL
;
355 if (!DBG_IVAR(BreakOnAttach
)) break;
357 if (dbg_fetch_context())
359 cont
= dbg_handle_exception(&de
->u
.Exception
.ExceptionRecord
,
360 de
->u
.Exception
.dwFirstChance
);
361 if (cont
&& dbg_curr_thread
)
363 dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
);
368 case CREATE_PROCESS_DEBUG_EVENT
:
369 dbg_curr_process
= dbg_add_process(&be_process_active_io
, de
->dwProcessId
,
370 de
->u
.CreateProcessInfo
.hProcess
);
371 if (dbg_curr_process
== NULL
)
373 WINE_ERR("Couldn't create process\n");
376 size
= ARRAY_SIZE(u
.buffer
);
377 if (!QueryFullProcessImageNameW( dbg_curr_process
->handle
, 0, u
.buffer
, &size
))
379 swprintf(u
.buffer
, ARRAY_SIZE(u
.buffer
), L
"Process_%08x", dbg_curr_pid
);
382 WINE_TRACE("%04lx:%04lx: create process '%s'/%p @%p (%lu<%lu>)\n",
383 de
->dwProcessId
, de
->dwThreadId
,
384 wine_dbgstr_w(u
.buffer
),
385 de
->u
.CreateProcessInfo
.lpImageName
,
386 de
->u
.CreateProcessInfo
.lpStartAddress
,
387 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
388 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
389 dbg_set_process_name(dbg_curr_process
, u
.buffer
);
391 if (!dbg_init(dbg_curr_process
->handle
, u
.buffer
, FALSE
))
392 dbg_printf("Couldn't initiate DbgHelp\n");
393 if (!dbg_load_module(dbg_curr_process
->handle
, de
->u
.CreateProcessInfo
.hFile
, u
.buffer
,
394 (DWORD_PTR
)de
->u
.CreateProcessInfo
.lpBaseOfImage
, 0))
395 dbg_printf("couldn't load main module (%lu)\n", GetLastError());
397 WINE_TRACE("%04lx:%04lx: create thread I @%p\n",
398 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateProcessInfo
.lpStartAddress
);
400 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
402 de
->u
.CreateProcessInfo
.hThread
,
403 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
404 if (!dbg_curr_thread
)
406 WINE_ERR("Couldn't create thread\n");
409 dbg_init_current_process();
410 dbg_init_current_thread(de
->u
.CreateProcessInfo
.lpStartAddress
);
413 case EXIT_PROCESS_DEBUG_EVENT
:
414 WINE_TRACE("%04lx:%04lx: exit process (%ld)\n",
415 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
417 if (dbg_curr_process
== NULL
)
419 WINE_ERR("Unknown process\n");
422 tgt_process_active_close_process(dbg_curr_process
, FALSE
);
423 dbg_printf("Process of pid=%04lx has terminated\n", de
->dwProcessId
);
426 case CREATE_THREAD_DEBUG_EVENT
:
427 WINE_TRACE("%04lx:%04lx: create thread D @%p\n",
428 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateThread
.lpStartAddress
);
430 if (dbg_curr_process
== NULL
)
432 WINE_ERR("Unknown process\n");
435 if (dbg_get_thread(dbg_curr_process
, de
->dwThreadId
) != NULL
)
437 WINE_TRACE("Thread already listed, skipping\n");
441 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
443 de
->u
.CreateThread
.hThread
,
444 de
->u
.CreateThread
.lpThreadLocalBase
);
445 if (!dbg_curr_thread
)
447 WINE_ERR("Couldn't create thread\n");
450 dbg_init_current_thread(de
->u
.CreateThread
.lpStartAddress
);
453 case EXIT_THREAD_DEBUG_EVENT
:
454 WINE_TRACE("%04lx:%04lx: exit thread (%ld)\n",
455 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
457 if (dbg_curr_thread
== NULL
)
459 WINE_ERR("Unknown thread\n");
462 /* FIXME: remove break point set on thread startup */
463 dbg_del_thread(dbg_curr_thread
);
466 case LOAD_DLL_DEBUG_EVENT
:
467 if (dbg_curr_thread
== NULL
)
469 WINE_ERR("Unknown thread\n");
472 fetch_module_name(de
->u
.LoadDll
.lpImageName
, de
->u
.LoadDll
.lpBaseOfDll
,
473 u
.buffer
, ARRAY_SIZE(u
.buffer
));
475 WINE_TRACE("%04lx:%04lx: loads DLL %s @%p (%lu<%lu>)\n",
476 de
->dwProcessId
, de
->dwThreadId
,
477 wine_dbgstr_w(u
.buffer
), de
->u
.LoadDll
.lpBaseOfDll
,
478 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
479 de
->u
.LoadDll
.nDebugInfoSize
);
480 dbg_load_module(dbg_curr_process
->handle
, de
->u
.LoadDll
.hFile
, u
.buffer
,
481 (DWORD_PTR
)de
->u
.LoadDll
.lpBaseOfDll
, 0);
482 break_set_xpoints(FALSE
);
483 break_check_delayed_bp();
484 break_set_xpoints(TRUE
);
485 if (DBG_IVAR(BreakOnDllLoad
))
487 dbg_printf("Stopping on DLL %ls loading at %p\n",
488 u
.buffer
, de
->u
.LoadDll
.lpBaseOfDll
);
489 if (dbg_fetch_context()) cont
= 0;
493 case UNLOAD_DLL_DEBUG_EVENT
:
494 WINE_TRACE("%04lx:%04lx: unload DLL @%p\n",
495 de
->dwProcessId
, de
->dwThreadId
,
496 de
->u
.UnloadDll
.lpBaseOfDll
);
497 break_delete_xpoints_from_module((DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
498 SymUnloadModule64(dbg_curr_process
->handle
, (DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
501 case OUTPUT_DEBUG_STRING_EVENT
:
502 if (dbg_curr_thread
== NULL
)
504 WINE_ERR("Unknown thread\n");
508 memory_get_string(dbg_curr_process
,
509 de
->u
.DebugString
.lpDebugStringData
, TRUE
,
510 de
->u
.DebugString
.fUnicode
, u
.bufferA
, sizeof(u
.bufferA
));
511 WINE_TRACE("%04lx:%04lx: output debug string (%s)\n",
512 de
->dwProcessId
, de
->dwThreadId
, u
.bufferA
);
516 WINE_TRACE("%04lx:%04lx: rip error=%lu type=%lu\n",
517 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
518 de
->u
.RipInfo
.dwType
);
522 WINE_TRACE("%04lx:%04lx: unknown event (%lx)\n",
523 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
525 if (!cont
) return TRUE
; /* stop execution */
526 ContinueDebugEvent(de
->dwProcessId
, de
->dwThreadId
, cont
);
527 return FALSE
; /* continue execution */
530 static void dbg_resume_debuggee(DWORD cont
)
532 if (dbg_curr_thread
->in_exception
)
535 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
537 dbg_exception_epilog();
538 memory_get_current_pc(&addr
);
539 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
540 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
541 dbg_curr_thread
->exec_mode
,
542 dbg_curr_thread
->exec_count
);
545 if (!dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
))
546 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid
);
549 dbg_interactiveP
= FALSE
;
550 if (!ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, cont
))
551 dbg_printf("Cannot continue on %04lx (%08lx)\n", dbg_curr_tid
, cont
);
554 static void wait_exception(void)
558 while (dbg_num_processes() && WaitForDebugEvent(&de
, INFINITE
))
560 if (dbg_handle_debug_event(&de
)) break;
562 dbg_interactiveP
= TRUE
;
565 void dbg_wait_next_exception(DWORD cont
, int count
, int mode
)
568 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
570 if (cont
== DBG_CONTINUE
)
572 dbg_curr_thread
->exec_count
= count
;
573 dbg_curr_thread
->exec_mode
= mode
;
575 dbg_resume_debuggee(cont
);
578 if (!dbg_curr_process
) return;
580 memory_get_current_pc(&addr
);
581 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
582 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
583 dbg_curr_thread
->exec_mode
,
584 dbg_curr_thread
->exec_count
);
587 void dbg_active_wait_for_first_exception(void)
589 dbg_interactiveP
= FALSE
;
590 /* wait for first exception */
594 static BOOL
dbg_start_debuggee(LPSTR cmdLine
)
596 PROCESS_INFORMATION info
;
597 STARTUPINFOA startup
, current
;
600 GetStartupInfoA(¤t
);
602 memset(&startup
, 0, sizeof(startup
));
603 startup
.cb
= sizeof(startup
);
604 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
606 startup
.wShowWindow
= (current
.dwFlags
& STARTF_USESHOWWINDOW
) ?
607 current
.wShowWindow
: SW_SHOWNORMAL
;
609 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUIs need it
612 flags
= DEBUG_PROCESS
| CREATE_NEW_CONSOLE
;
613 if (!DBG_IVAR(AlsoDebugProcChild
)) flags
|= DEBUG_ONLY_THIS_PROCESS
;
615 if (!CreateProcessA(NULL
, cmdLine
, NULL
, NULL
, FALSE
, flags
,
616 NULL
, NULL
, &startup
, &info
))
618 dbg_printf("Couldn't start process '%s'\n", cmdLine
);
621 if (!info
.dwProcessId
)
623 /* this happens when the program being run is not a Wine binary
624 * (for example, a shell wrapper around a WineLib app)
626 /* Current fix: list running processes and let the user attach
627 * to one of them (sic)
628 * FIXME: implement a real fix => grab the process (from the
629 * running processes) from its name
631 dbg_printf("Debuggee has been started (%s)\n"
632 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
633 "Try to attach to one of those processes:\n", cmdLine
);
634 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
636 info_win32_processes();
639 dbg_curr_pid
= info
.dwProcessId
;
640 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, dbg_curr_pid
, 0))) return FALSE
;
641 dbg_curr_process
->active_debuggee
= TRUE
;
642 if (cmdLine
!= dbg_last_cmd_line
)
644 free(dbg_last_cmd_line
);
645 dbg_last_cmd_line
= cmdLine
;
651 /***********************************************************************
652 * dbg_build_command_line
654 * (converted from dlls/ntdll/unix/env.c)
656 * Build the command line of a process from the argv array.
658 * We must quote and escape characters so that the argv array can be rebuilt
659 * from the command line:
660 * - spaces and tabs must be quoted
662 * - quotes must be escaped
664 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
665 * resulting in an odd number of '\' followed by a '"'
668 * - '\'s are followed by the closing '"' must be doubled,
669 * resulting in an even number of '\' followed by a '"'
672 * - '\'s that are not followed by a '"' can be left as is
676 static char *dbg_build_command_line( char **argv
)
683 for (arg
= argv
; *arg
; arg
++) len
+= 3 + 2 * strlen( *arg
);
684 if (!(ret
= malloc( len
))) return NULL
;
687 for (arg
= argv
; *arg
; arg
++)
689 BOOL has_space
, has_quote
;
693 /* check for quotes and spaces in this argument (first arg is always quoted) */
694 has_space
= (arg
== argv
) || !**arg
|| strchr( *arg
, ' ' ) || strchr( *arg
, '\t' );
695 has_quote
= strchr( *arg
, '"' ) != NULL
;
697 /* now transfer it to the command line */
698 if (has_space
) *p
++ = '"';
699 if (has_quote
|| has_space
)
702 for (a
= *arg
; *a
; a
++)
704 if (*a
== '\\') bcount
++;
707 if (*a
== '"') /* double all the '\\' preceding this '"', plus one */
708 for (i
= 0; i
<= bcount
; i
++) *p
++ = '\\';
721 /* Double all the '\' preceding the closing quote */
722 for (i
= 0; i
< bcount
; i
++) *p
++ = '\\';
727 if (p
> ret
) p
--; /* remove last space */
733 void dbg_run_debuggee(struct list_string
* ls
)
735 if (dbg_curr_process
)
737 dbg_printf("Already attached to a process. Use 'detach' or 'kill' before using 'run'\n");
742 dbg_printf("No active target to be restarted\n");
749 unsigned argc
= 2, i
;
750 struct list_string
* cls
;
752 for (cls
= ls
; cls
; cls
= cls
->next
) argc
++;
753 if (!(argv
= malloc(argc
* sizeof(argv
[0])))) return;
754 argv
[0] = dbg_executable
;
755 for (i
= 1, cls
= ls
; cls
; cls
= cls
->next
, i
++) argv
[i
] = cls
->string
;
757 cl
= dbg_build_command_line(argv
);
760 if (!cl
|| !dbg_start_debuggee(cl
))
768 if (!dbg_last_cmd_line
) dbg_last_cmd_line
= strdup(dbg_executable
);
769 dbg_start_debuggee(dbg_last_cmd_line
);
771 dbg_active_wait_for_first_exception();
772 source_list_from_addr(NULL
, 0);
775 static BOOL
str2int(const char* str
, DWORD_PTR
* val
)
779 *val
= strtol(str
, &ptr
, 0);
780 return str
< ptr
&& !*ptr
;
783 static HANDLE
create_temp_file(void)
785 WCHAR path
[MAX_PATH
], name
[MAX_PATH
];
787 if (!GetTempPathW( MAX_PATH
, path
) || !GetTempFileNameW( path
, L
"wdb", 0, name
))
788 return INVALID_HANDLE_VALUE
;
789 return CreateFileW( name
, GENERIC_READ
|GENERIC_WRITE
|DELETE
, FILE_SHARE_DELETE
,
790 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_DELETE_ON_CLOSE
, 0 );
803 { 0, VER_PLATFORM_WIN32s
, 2, 0, "2.0" },
804 { 0, VER_PLATFORM_WIN32s
, 3, 0, "3.0" },
805 { 0, VER_PLATFORM_WIN32s
, 3, 10, "3.1" },
806 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 0, "95" },
807 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 10, "98" },
808 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 90, "ME" },
809 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 3, 51, "NT 3.51" },
810 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 4, 0, "NT 4.0" },
811 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 0, "2000" },
812 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 1, "XP" },
813 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 2, "XP" },
814 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 5, 2, "Server 2003" },
815 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 0, "Vista" },
816 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 0, "Server 2008" },
817 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 1, "7" },
818 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 1, "Server 2008 R2" },
819 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 2, "8" },
820 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 2, "Server 2012" },
821 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 3, "8.1" },
822 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 3, "Server 2012 R2" },
823 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 10, 0, "10" },
826 static const char *get_windows_version(void)
828 RTL_OSVERSIONINFOEXW info
= { sizeof(RTL_OSVERSIONINFOEXW
) };
832 RtlGetVersion( &info
);
834 for (i
= 0; i
< ARRAY_SIZE(version_table
); i
++)
836 if (version_table
[i
].type
== info
.wProductType
&&
837 version_table
[i
].platform
== info
.dwPlatformId
&&
838 version_table
[i
].major
== info
.dwMajorVersion
&&
839 version_table
[i
].minor
== info
.dwMinorVersion
)
841 return version_table
[i
].str
;
845 snprintf( str
, sizeof(str
), "%ld.%ld (%d)", info
.dwMajorVersion
,
846 info
.dwMinorVersion
, info
.wProductType
);
850 static void output_system_info(void)
853 static const char platform
[] = "i386";
854 #elif defined(__x86_64__)
855 static const char platform
[] = "x86_64";
856 #elif defined(__arm__)
857 static const char platform
[] = "arm";
858 #elif defined(__aarch64__)
859 static const char platform
[] = "arm64";
864 const char *(CDECL
*wine_get_build_id
)(void);
865 void (CDECL
*wine_get_host_version
)( const char **sysname
, const char **release
);
868 wine_get_build_id
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
869 wine_get_host_version
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
870 if (!IsWow64Process( dbg_curr_process
->handle
, &is_wow64
)) is_wow64
= FALSE
;
872 dbg_printf( "System information:\n" );
873 if (wine_get_build_id
) dbg_printf( " Wine build: %s\n", wine_get_build_id() );
874 dbg_printf( " Platform: %s%s\n", platform
, is_wow64
? " (WOW64)" : "" );
875 dbg_printf( " Version: Windows %s\n", get_windows_version() );
876 if (wine_get_host_version
)
878 const char *sysname
, *release
;
879 wine_get_host_version( &sysname
, &release
);
880 dbg_printf( " Host system: %s\n", sysname
);
881 dbg_printf( " Host version: %s\n", release
);
885 /******************************************************************
888 * Tries to attach to a running process
889 * Handles the <pid> or <pid> <evt> forms
891 enum dbg_start
dbg_active_attach(int argc
, char* argv
[])
895 /* try the form <myself> pid */
896 if (argc
== 1 && str2int(argv
[0], &pid
) && pid
!= 0)
898 if (!dbg_attach_debuggee(pid
))
899 return start_error_init
;
901 /* try the form <myself> pid evt (Win32 JIT debugger) */
902 else if (argc
== 2 && str2int(argv
[0], &pid
) && pid
!= 0 &&
903 str2int(argv
[1], &evt
) && evt
!= 0)
905 if (!dbg_attach_debuggee(pid
))
907 /* don't care about result */
908 SetEvent((HANDLE
)evt
);
909 return start_error_init
;
911 dbg_curr_process
->event_on_first_exception
= (HANDLE
)evt
;
913 else return start_error_parse
;
919 /******************************************************************
922 * Launches a debuggee (with its arguments) from argc/argv
924 enum dbg_start
dbg_active_launch(int argc
, char* argv
[])
928 if (argc
== 0) return start_error_parse
;
930 dbg_executable
= strdup(argv
[0]);
931 cmd_line
= dbg_build_command_line(argv
);
933 if (!dbg_start_debuggee(cmd_line
))
936 return start_error_init
;
942 /******************************************************************
945 * Starts (<pid> or <pid> <evt>) in automatic mode
947 enum dbg_start
dbg_active_auto(int argc
, char* argv
[])
949 HANDLE thread
= 0, event
= 0, input
, output
= INVALID_HANDLE_VALUE
;
950 enum dbg_start ds
= start_error_parse
;
952 DBG_IVAR(BreakOnDllLoad
) = 0;
956 ds
= dbg_active_attach(argc
, argv
);
957 if (ds
!= start_ok
) {
958 msgbox_res_id(NULL
, IDS_INVALID_PARAMS
, IDS_AUTO_CAPTION
, MB_OK
);
962 switch (display_crash_dialog())
967 dbg_start_interactive(NULL
, INVALID_HANDLE_VALUE
);
970 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
971 if (event
) thread
= display_crash_details( event
);
972 if (thread
) dbg_houtput
= output
= create_temp_file();
976 input
= parser_generate_command_file("echo Modules:", "info share",
977 "echo Threads:", "info threads", NULL
);
978 if (input
== INVALID_HANDLE_VALUE
) return start_error_parse
;
980 if (dbg_curr_process
->active_debuggee
)
981 dbg_active_wait_for_first_exception();
983 dbg_interactiveP
= TRUE
;
984 parser_handle(NULL
, input
);
985 output_system_info();
987 if (output
!= INVALID_HANDLE_VALUE
)
990 WaitForSingleObject( thread
, INFINITE
);
991 CloseHandle( output
);
992 CloseHandle( thread
);
993 CloseHandle( event
);
996 CloseHandle( input
);
997 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, TRUE
);
1001 /******************************************************************
1002 * dbg_active_minidump
1004 * Starts (<pid> or <pid> <evt>) in minidump mode
1006 enum dbg_start
dbg_active_minidump(int argc
, char* argv
[])
1009 enum dbg_start ds
= start_error_parse
;
1010 const char* file
= NULL
;
1011 char tmp
[8 + 1 + 2 + MAX_PATH
]; /* minidump "<file>" */
1013 dbg_houtput
= GetStdHandle(STD_ERROR_HANDLE
);
1014 DBG_IVAR(BreakOnDllLoad
) = 0;
1017 /* hard stuff now ; we can get things like:
1018 * --minidump <pid> 1 arg
1019 * --minidump <pid> <evt> 2 args
1020 * --minidump <file> <pid> 2 args
1021 * --minidump <file> <pid> <evt> 3 args
1026 ds
= dbg_active_attach(argc
, argv
);
1029 if ((ds
= dbg_active_attach(argc
, argv
)) != start_ok
)
1032 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
1037 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
1040 return start_error_parse
;
1042 if (ds
!= start_ok
) return ds
;
1043 memcpy(tmp
, "minidump \"", 10);
1046 char path
[MAX_PATH
];
1048 GetTempPathA(sizeof(path
), path
);
1049 GetTempFileNameA(path
, "WD", 0, tmp
+ 10);
1051 else strcpy(tmp
+ 10, file
);
1055 /* FIXME: should generate unix name as well */
1056 dbg_printf("Capturing program state in %s\n", tmp
+ 9);
1058 hFile
= parser_generate_command_file(tmp
, "detach", NULL
);
1059 if (hFile
== INVALID_HANDLE_VALUE
) return start_error_parse
;
1061 if (dbg_curr_process
->active_debuggee
)
1062 dbg_active_wait_for_first_exception();
1064 dbg_interactiveP
= TRUE
;
1065 parser_handle(NULL
, hFile
);
1070 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
)
1074 DWORD exit_code
= 0;
1076 if (pcs
== dbg_curr_process
&& dbg_curr_thread
->in_exception
)
1077 exit_code
= dbg_curr_thread
->excpt_record
.ExceptionCode
;
1079 TerminateProcess(pcs
->handle
, exit_code
);
1081 else if (pcs
== dbg_curr_process
)
1083 /* remove all set breakpoints in debuggee code */
1084 break_set_xpoints(FALSE
);
1085 /* needed for single stepping (ugly).
1086 * should this be handled inside the server ???
1088 dbg_curr_process
->be_cpu
->single_step(&dbg_context
, FALSE
);
1089 if (dbg_curr_thread
->in_exception
)
1091 dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
);
1092 ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, DBG_CONTINUE
);
1098 if (!DebugActiveProcessStop(pcs
->pid
)) return FALSE
;
1100 SymCleanup(pcs
->handle
);
1101 dbg_del_process(pcs
);
1106 static BOOL
tgt_process_active_read(HANDLE hProcess
, const void* addr
,
1107 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
1109 return ReadProcessMemory( hProcess
, addr
, buffer
, len
, rlen
);
1112 static BOOL
tgt_process_active_write(HANDLE hProcess
, void* addr
,
1113 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
1115 return WriteProcessMemory( hProcess
, addr
, buffer
, len
, wlen
);
1118 static BOOL
tgt_process_active_get_selector(HANDLE hThread
, DWORD sel
, LDT_ENTRY
* le
)
1120 return GetThreadSelectorEntry( hThread
, sel
, le
);
1123 static struct be_process_io be_process_active_io
=
1125 tgt_process_active_close_process
,
1126 tgt_process_active_read
,
1127 tgt_process_active_write
,
1128 tgt_process_active_get_selector