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
);
248 if (pThreadName
->dwThreadID
== -1)
249 pThread
= dbg_curr_thread
;
251 pThread
= dbg_get_thread(dbg_curr_process
, pThreadName
->dwThreadID
);
254 dbg_printf("Thread ID=%04lx not in our list of threads -> can't rename\n", pThreadName
->dwThreadID
);
257 if (dbg_read_memory(pThreadName
->szName
, pThread
->name
, 9))
258 dbg_printf("Thread ID=%04lx renamed using MS VC6 extension (name==\"%.9s\")\n",
259 pThread
->tid
, pThread
->name
);
261 case EXCEPTION_INVALID_HANDLE
:
265 if (first_chance
&& !is_debug
&& !DBG_IVAR(BreakOnFirstChance
) &&
266 !(rec
->ExceptionFlags
& EH_STACK_INVALID
))
268 /* pass exception to program except for debug exceptions */
269 return DBG_EXCEPTION_NOT_HANDLED
;
272 dbg_curr_thread
->excpt_record
= *rec
;
273 dbg_curr_thread
->in_exception
= TRUE
;
274 dbg_curr_thread
->first_chance
= first_chance
;
276 if (!is_debug
) info_win32_exception();
278 if (rec
->ExceptionCode
== STATUS_POSSIBLE_DEADLOCK
&& !DBG_IVAR(BreakOnCritSectTimeOut
))
280 dbg_curr_thread
->in_exception
= FALSE
;
281 return DBG_EXCEPTION_NOT_HANDLED
;
284 if (dbg_exception_prolog(is_debug
, rec
))
286 dbg_interactiveP
= TRUE
;
289 dbg_exception_epilog();
294 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
);
296 void fetch_module_name(void* name_addr
, void* mod_addr
, WCHAR
* buffer
, size_t bufsz
)
298 memory_get_string_indirect(dbg_curr_process
, name_addr
, TRUE
, buffer
, bufsz
);
299 if (!buffer
[0] && !GetModuleFileNameExW(dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
301 if (GetMappedFileNameW( dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
303 /* FIXME: proper NT->Dos conversion */
304 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
306 if (!wcsncmp( buffer
, nt_prefixW
, 4 ))
307 memmove( buffer
, buffer
+ 4, (lstrlenW(buffer
+ 4) + 1) * sizeof(WCHAR
) );
310 swprintf(buffer
, bufsz
, L
"DLL_%08lx", (ULONG_PTR
)mod_addr
);
314 static unsigned dbg_handle_debug_event(DEBUG_EVENT
* de
)
320 DWORD size
, cont
= DBG_CONTINUE
;
322 dbg_curr_pid
= de
->dwProcessId
;
323 dbg_curr_tid
= de
->dwThreadId
;
325 if ((dbg_curr_process
= dbg_get_process(de
->dwProcessId
)) != NULL
)
326 dbg_curr_thread
= dbg_get_thread(dbg_curr_process
, de
->dwThreadId
);
328 dbg_curr_thread
= NULL
;
330 switch (de
->dwDebugEventCode
)
332 case EXCEPTION_DEBUG_EVENT
:
333 if (!dbg_curr_thread
)
335 WINE_ERR("%04lx:%04lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
336 de
->dwProcessId
, de
->dwThreadId
);
340 WINE_TRACE("%04lx:%04lx: exception code=%08lx\n",
341 de
->dwProcessId
, de
->dwThreadId
,
342 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
344 if (dbg_curr_process
->event_on_first_exception
)
346 SetEvent(dbg_curr_process
->event_on_first_exception
);
347 CloseHandle(dbg_curr_process
->event_on_first_exception
);
348 dbg_curr_process
->event_on_first_exception
= NULL
;
349 if (!DBG_IVAR(BreakOnAttach
)) break;
351 if (dbg_fetch_context())
353 cont
= dbg_handle_exception(&de
->u
.Exception
.ExceptionRecord
,
354 de
->u
.Exception
.dwFirstChance
);
355 if (cont
&& dbg_curr_thread
)
357 dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
);
362 case CREATE_PROCESS_DEBUG_EVENT
:
363 dbg_curr_process
= dbg_add_process(&be_process_active_io
, de
->dwProcessId
,
364 de
->u
.CreateProcessInfo
.hProcess
);
365 if (dbg_curr_process
== NULL
)
367 WINE_ERR("Couldn't create process\n");
370 size
= ARRAY_SIZE(u
.buffer
);
371 if (!QueryFullProcessImageNameW( dbg_curr_process
->handle
, 0, u
.buffer
, &size
))
373 swprintf(u
.buffer
, ARRAY_SIZE(u
.buffer
), L
"Process_%08x", dbg_curr_pid
);
376 WINE_TRACE("%04lx:%04lx: create process '%s'/%p @%p (%lu<%lu>)\n",
377 de
->dwProcessId
, de
->dwThreadId
,
378 wine_dbgstr_w(u
.buffer
),
379 de
->u
.CreateProcessInfo
.lpImageName
,
380 de
->u
.CreateProcessInfo
.lpStartAddress
,
381 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
382 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
383 dbg_set_process_name(dbg_curr_process
, u
.buffer
);
385 if (!dbg_init(dbg_curr_process
->handle
, u
.buffer
, FALSE
))
386 dbg_printf("Couldn't initiate DbgHelp\n");
387 if (!dbg_load_module(dbg_curr_process
->handle
, de
->u
.CreateProcessInfo
.hFile
, u
.buffer
,
388 (DWORD_PTR
)de
->u
.CreateProcessInfo
.lpBaseOfImage
, 0))
389 dbg_printf("couldn't load main module (%lu)\n", GetLastError());
391 WINE_TRACE("%04lx:%04lx: create thread I @%p\n",
392 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateProcessInfo
.lpStartAddress
);
394 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
396 de
->u
.CreateProcessInfo
.hThread
,
397 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
398 if (!dbg_curr_thread
)
400 WINE_ERR("Couldn't create thread\n");
403 dbg_init_current_process();
404 dbg_init_current_thread(de
->u
.CreateProcessInfo
.lpStartAddress
);
407 case EXIT_PROCESS_DEBUG_EVENT
:
408 WINE_TRACE("%04lx:%04lx: exit process (%ld)\n",
409 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
411 if (dbg_curr_process
== NULL
)
413 WINE_ERR("Unknown process\n");
416 tgt_process_active_close_process(dbg_curr_process
, FALSE
);
417 dbg_printf("Process of pid=%04lx has terminated\n", de
->dwProcessId
);
420 case CREATE_THREAD_DEBUG_EVENT
:
421 WINE_TRACE("%04lx:%04lx: create thread D @%p\n",
422 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateThread
.lpStartAddress
);
424 if (dbg_curr_process
== NULL
)
426 WINE_ERR("Unknown process\n");
429 if (dbg_get_thread(dbg_curr_process
, de
->dwThreadId
) != NULL
)
431 WINE_TRACE("Thread already listed, skipping\n");
435 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
437 de
->u
.CreateThread
.hThread
,
438 de
->u
.CreateThread
.lpThreadLocalBase
);
439 if (!dbg_curr_thread
)
441 WINE_ERR("Couldn't create thread\n");
444 dbg_init_current_thread(de
->u
.CreateThread
.lpStartAddress
);
447 case EXIT_THREAD_DEBUG_EVENT
:
448 WINE_TRACE("%04lx:%04lx: exit thread (%ld)\n",
449 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
451 if (dbg_curr_thread
== NULL
)
453 WINE_ERR("Unknown thread\n");
456 /* FIXME: remove break point set on thread startup */
457 dbg_del_thread(dbg_curr_thread
);
460 case LOAD_DLL_DEBUG_EVENT
:
461 if (dbg_curr_thread
== NULL
)
463 WINE_ERR("Unknown thread\n");
466 fetch_module_name(de
->u
.LoadDll
.lpImageName
, de
->u
.LoadDll
.lpBaseOfDll
,
467 u
.buffer
, ARRAY_SIZE(u
.buffer
));
469 WINE_TRACE("%04lx:%04lx: loads DLL %s @%p (%lu<%lu>)\n",
470 de
->dwProcessId
, de
->dwThreadId
,
471 wine_dbgstr_w(u
.buffer
), de
->u
.LoadDll
.lpBaseOfDll
,
472 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
473 de
->u
.LoadDll
.nDebugInfoSize
);
474 dbg_load_module(dbg_curr_process
->handle
, de
->u
.LoadDll
.hFile
, u
.buffer
,
475 (DWORD_PTR
)de
->u
.LoadDll
.lpBaseOfDll
, 0);
476 break_set_xpoints(FALSE
);
477 break_check_delayed_bp();
478 break_set_xpoints(TRUE
);
479 if (DBG_IVAR(BreakOnDllLoad
))
481 dbg_printf("Stopping on DLL %s loading at %p\n",
482 dbg_W2A(u
.buffer
, -1), de
->u
.LoadDll
.lpBaseOfDll
);
483 if (dbg_fetch_context()) cont
= 0;
487 case UNLOAD_DLL_DEBUG_EVENT
:
488 WINE_TRACE("%04lx:%04lx: unload DLL @%p\n",
489 de
->dwProcessId
, de
->dwThreadId
,
490 de
->u
.UnloadDll
.lpBaseOfDll
);
491 break_delete_xpoints_from_module((DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
492 SymUnloadModule64(dbg_curr_process
->handle
, (DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
495 case OUTPUT_DEBUG_STRING_EVENT
:
496 if (dbg_curr_thread
== NULL
)
498 WINE_ERR("Unknown thread\n");
502 memory_get_string(dbg_curr_process
,
503 de
->u
.DebugString
.lpDebugStringData
, TRUE
,
504 de
->u
.DebugString
.fUnicode
, u
.bufferA
, sizeof(u
.bufferA
));
505 WINE_TRACE("%04lx:%04lx: output debug string (%s)\n",
506 de
->dwProcessId
, de
->dwThreadId
, u
.bufferA
);
510 WINE_TRACE("%04lx:%04lx: rip error=%lu type=%lu\n",
511 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
512 de
->u
.RipInfo
.dwType
);
516 WINE_TRACE("%04lx:%04lx: unknown event (%lx)\n",
517 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
519 if (!cont
) return TRUE
; /* stop execution */
520 ContinueDebugEvent(de
->dwProcessId
, de
->dwThreadId
, cont
);
521 return FALSE
; /* continue execution */
524 static void dbg_resume_debuggee(DWORD cont
)
526 if (dbg_curr_thread
->in_exception
)
529 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
531 dbg_exception_epilog();
532 memory_get_current_pc(&addr
);
533 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
534 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
535 dbg_curr_thread
->exec_mode
,
536 dbg_curr_thread
->exec_count
);
539 if (!dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
))
540 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid
);
543 dbg_interactiveP
= FALSE
;
544 if (!ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, cont
))
545 dbg_printf("Cannot continue on %04lx (%08lx)\n", dbg_curr_tid
, cont
);
548 static void wait_exception(void)
552 while (dbg_num_processes() && WaitForDebugEvent(&de
, INFINITE
))
554 if (dbg_handle_debug_event(&de
)) break;
556 dbg_interactiveP
= TRUE
;
559 void dbg_wait_next_exception(DWORD cont
, int count
, int mode
)
562 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
564 if (cont
== DBG_CONTINUE
)
566 dbg_curr_thread
->exec_count
= count
;
567 dbg_curr_thread
->exec_mode
= mode
;
569 dbg_resume_debuggee(cont
);
572 if (!dbg_curr_process
) return;
574 memory_get_current_pc(&addr
);
575 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
576 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
577 dbg_curr_thread
->exec_mode
,
578 dbg_curr_thread
->exec_count
);
581 void dbg_active_wait_for_first_exception(void)
583 dbg_interactiveP
= FALSE
;
584 /* wait for first exception */
588 static BOOL
dbg_start_debuggee(LPSTR cmdLine
)
590 PROCESS_INFORMATION info
;
591 STARTUPINFOA startup
, current
;
594 GetStartupInfoA(¤t
);
596 memset(&startup
, 0, sizeof(startup
));
597 startup
.cb
= sizeof(startup
);
598 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
600 startup
.wShowWindow
= (current
.dwFlags
& STARTF_USESHOWWINDOW
) ?
601 current
.wShowWindow
: SW_SHOWNORMAL
;
603 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUIs need it
606 flags
= DEBUG_PROCESS
| CREATE_NEW_CONSOLE
;
607 if (!DBG_IVAR(AlsoDebugProcChild
)) flags
|= DEBUG_ONLY_THIS_PROCESS
;
609 if (!CreateProcessA(NULL
, cmdLine
, NULL
, NULL
, FALSE
, flags
,
610 NULL
, NULL
, &startup
, &info
))
612 dbg_printf("Couldn't start process '%s'\n", cmdLine
);
615 if (!info
.dwProcessId
)
617 /* this happens when the program being run is not a Wine binary
618 * (for example, a shell wrapper around a WineLib app)
620 /* Current fix: list running processes and let the user attach
621 * to one of them (sic)
622 * FIXME: implement a real fix => grab the process (from the
623 * running processes) from its name
625 dbg_printf("Debuggee has been started (%s)\n"
626 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
627 "Try to attach to one of those processes:\n", cmdLine
);
628 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
630 info_win32_processes();
633 dbg_curr_pid
= info
.dwProcessId
;
634 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, dbg_curr_pid
, 0))) return FALSE
;
635 dbg_curr_process
->active_debuggee
= TRUE
;
636 if (cmdLine
!= dbg_last_cmd_line
)
638 free(dbg_last_cmd_line
);
639 dbg_last_cmd_line
= cmdLine
;
645 /***********************************************************************
646 * dbg_build_command_line
648 * (converted from dlls/ntdll/unix/env.c)
650 * Build the command line of a process from the argv array.
652 * We must quote and escape characters so that the argv array can be rebuilt
653 * from the command line:
654 * - spaces and tabs must be quoted
656 * - quotes must be escaped
658 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
659 * resulting in an odd number of '\' followed by a '"'
662 * - '\'s are followed by the closing '"' must be doubled,
663 * resulting in an even number of '\' followed by a '"'
666 * - '\'s that are not followed by a '"' can be left as is
670 static char *dbg_build_command_line( char **argv
)
677 for (arg
= argv
; *arg
; arg
++) len
+= 3 + 2 * strlen( *arg
);
678 if (!(ret
= malloc( len
))) return NULL
;
681 for (arg
= argv
; *arg
; arg
++)
683 BOOL has_space
, has_quote
;
687 /* check for quotes and spaces in this argument (first arg is always quoted) */
688 has_space
= (arg
== argv
) || !**arg
|| strchr( *arg
, ' ' ) || strchr( *arg
, '\t' );
689 has_quote
= strchr( *arg
, '"' ) != NULL
;
691 /* now transfer it to the command line */
692 if (has_space
) *p
++ = '"';
693 if (has_quote
|| has_space
)
696 for (a
= *arg
; *a
; a
++)
698 if (*a
== '\\') bcount
++;
701 if (*a
== '"') /* double all the '\\' preceding this '"', plus one */
702 for (i
= 0; i
<= bcount
; i
++) *p
++ = '\\';
715 /* Double all the '\' preceding the closing quote */
716 for (i
= 0; i
< bcount
; i
++) *p
++ = '\\';
721 if (p
> ret
) p
--; /* remove last space */
727 void dbg_run_debuggee(struct list_string
* ls
)
729 if (dbg_curr_process
)
731 dbg_printf("Already attached to a process. Use 'detach' or 'kill' before using 'run'\n");
736 dbg_printf("No active target to be restarted\n");
743 unsigned argc
= 2, i
;
744 struct list_string
* cls
;
746 for (cls
= ls
; cls
; cls
= cls
->next
) argc
++;
747 if (!(argv
= malloc(argc
* sizeof(argv
[0])))) return;
748 argv
[0] = dbg_executable
;
749 for (i
= 1, cls
= ls
; cls
; cls
= cls
->next
, i
++) argv
[i
] = cls
->string
;
751 cl
= dbg_build_command_line(argv
);
754 if (!cl
|| !dbg_start_debuggee(cl
))
762 if (!dbg_last_cmd_line
) dbg_last_cmd_line
= strdup(dbg_executable
);
763 dbg_start_debuggee(dbg_last_cmd_line
);
765 dbg_active_wait_for_first_exception();
766 source_list_from_addr(NULL
, 0);
769 static BOOL
str2int(const char* str
, DWORD_PTR
* val
)
773 *val
= strtol(str
, &ptr
, 0);
774 return str
< ptr
&& !*ptr
;
777 static HANDLE
create_temp_file(void)
779 WCHAR path
[MAX_PATH
], name
[MAX_PATH
];
781 if (!GetTempPathW( MAX_PATH
, path
) || !GetTempFileNameW( path
, L
"wdb", 0, name
))
782 return INVALID_HANDLE_VALUE
;
783 return CreateFileW( name
, GENERIC_READ
|GENERIC_WRITE
|DELETE
, FILE_SHARE_DELETE
,
784 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_DELETE_ON_CLOSE
, 0 );
797 { 0, VER_PLATFORM_WIN32s
, 2, 0, "2.0" },
798 { 0, VER_PLATFORM_WIN32s
, 3, 0, "3.0" },
799 { 0, VER_PLATFORM_WIN32s
, 3, 10, "3.1" },
800 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 0, "95" },
801 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 10, "98" },
802 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 90, "ME" },
803 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 3, 51, "NT 3.51" },
804 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 4, 0, "NT 4.0" },
805 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 0, "2000" },
806 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 1, "XP" },
807 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 2, "XP" },
808 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 5, 2, "Server 2003" },
809 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 0, "Vista" },
810 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 0, "Server 2008" },
811 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 1, "7" },
812 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 1, "Server 2008 R2" },
813 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 2, "8" },
814 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 2, "Server 2012" },
815 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 3, "8.1" },
816 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 3, "Server 2012 R2" },
817 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 10, 0, "10" },
820 static const char *get_windows_version(void)
822 RTL_OSVERSIONINFOEXW info
= { sizeof(RTL_OSVERSIONINFOEXW
) };
826 RtlGetVersion( &info
);
828 for (i
= 0; i
< ARRAY_SIZE(version_table
); i
++)
830 if (version_table
[i
].type
== info
.wProductType
&&
831 version_table
[i
].platform
== info
.dwPlatformId
&&
832 version_table
[i
].major
== info
.dwMajorVersion
&&
833 version_table
[i
].minor
== info
.dwMinorVersion
)
835 return version_table
[i
].str
;
839 snprintf( str
, sizeof(str
), "%ld.%ld (%d)", info
.dwMajorVersion
,
840 info
.dwMinorVersion
, info
.wProductType
);
844 static void output_system_info(void)
847 static const char platform
[] = "i386";
848 #elif defined(__x86_64__)
849 static const char platform
[] = "x86_64";
850 #elif defined(__arm__)
851 static const char platform
[] = "arm";
852 #elif defined(__aarch64__)
853 static const char platform
[] = "arm64";
858 const char *(CDECL
*wine_get_build_id
)(void);
859 void (CDECL
*wine_get_host_version
)( const char **sysname
, const char **release
);
862 wine_get_build_id
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
863 wine_get_host_version
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
864 if (!IsWow64Process( dbg_curr_process
->handle
, &is_wow64
)) is_wow64
= FALSE
;
866 dbg_printf( "System information:\n" );
867 if (wine_get_build_id
) dbg_printf( " Wine build: %s\n", wine_get_build_id() );
868 dbg_printf( " Platform: %s%s\n", platform
, is_wow64
? " (WOW64)" : "" );
869 dbg_printf( " Version: Windows %s\n", get_windows_version() );
870 if (wine_get_host_version
)
872 const char *sysname
, *release
;
873 wine_get_host_version( &sysname
, &release
);
874 dbg_printf( " Host system: %s\n", sysname
);
875 dbg_printf( " Host version: %s\n", release
);
879 /******************************************************************
882 * Tries to attach to a running process
883 * Handles the <pid> or <pid> <evt> forms
885 enum dbg_start
dbg_active_attach(int argc
, char* argv
[])
889 /* try the form <myself> pid */
890 if (argc
== 1 && str2int(argv
[0], &pid
) && pid
!= 0)
892 if (!dbg_attach_debuggee(pid
))
893 return start_error_init
;
895 /* try the form <myself> pid evt (Win32 JIT debugger) */
896 else if (argc
== 2 && str2int(argv
[0], &pid
) && pid
!= 0 &&
897 str2int(argv
[1], &evt
) && evt
!= 0)
899 if (!dbg_attach_debuggee(pid
))
901 /* don't care about result */
902 SetEvent((HANDLE
)evt
);
903 return start_error_init
;
905 dbg_curr_process
->event_on_first_exception
= (HANDLE
)evt
;
907 else return start_error_parse
;
913 /******************************************************************
916 * Launches a debuggee (with its arguments) from argc/argv
918 enum dbg_start
dbg_active_launch(int argc
, char* argv
[])
922 if (argc
== 0) return start_error_parse
;
924 dbg_executable
= strdup(argv
[0]);
925 cmd_line
= dbg_build_command_line(argv
);
927 if (!dbg_start_debuggee(cmd_line
))
930 return start_error_init
;
936 /******************************************************************
939 * Starts (<pid> or <pid> <evt>) in automatic mode
941 enum dbg_start
dbg_active_auto(int argc
, char* argv
[])
943 HANDLE thread
= 0, event
= 0, input
, output
= INVALID_HANDLE_VALUE
;
944 enum dbg_start ds
= start_error_parse
;
946 DBG_IVAR(BreakOnDllLoad
) = 0;
950 ds
= dbg_active_attach(argc
, argv
);
951 if (ds
!= start_ok
) {
952 msgbox_res_id(NULL
, IDS_INVALID_PARAMS
, IDS_AUTO_CAPTION
, MB_OK
);
956 switch (display_crash_dialog())
961 dbg_start_interactive(NULL
, INVALID_HANDLE_VALUE
);
964 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
965 if (event
) thread
= display_crash_details( event
);
966 if (thread
) dbg_houtput
= output
= create_temp_file();
970 input
= parser_generate_command_file("echo Modules:", "info share",
971 "echo Threads:", "info threads", NULL
);
972 if (input
== INVALID_HANDLE_VALUE
) return start_error_parse
;
974 if (dbg_curr_process
->active_debuggee
)
975 dbg_active_wait_for_first_exception();
977 dbg_interactiveP
= TRUE
;
978 parser_handle(NULL
, input
);
979 output_system_info();
981 if (output
!= INVALID_HANDLE_VALUE
)
984 WaitForSingleObject( thread
, INFINITE
);
985 CloseHandle( output
);
986 CloseHandle( thread
);
987 CloseHandle( event
);
990 CloseHandle( input
);
991 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, TRUE
);
995 /******************************************************************
996 * dbg_active_minidump
998 * Starts (<pid> or <pid> <evt>) in minidump mode
1000 enum dbg_start
dbg_active_minidump(int argc
, char* argv
[])
1003 enum dbg_start ds
= start_error_parse
;
1004 const char* file
= NULL
;
1005 char tmp
[8 + 1 + 2 + MAX_PATH
]; /* minidump "<file>" */
1007 dbg_houtput
= GetStdHandle(STD_ERROR_HANDLE
);
1008 DBG_IVAR(BreakOnDllLoad
) = 0;
1011 /* hard stuff now ; we can get things like:
1012 * --minidump <pid> 1 arg
1013 * --minidump <pid> <evt> 2 args
1014 * --minidump <file> <pid> 2 args
1015 * --minidump <file> <pid> <evt> 3 args
1020 ds
= dbg_active_attach(argc
, argv
);
1023 if ((ds
= dbg_active_attach(argc
, argv
)) != start_ok
)
1026 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
1031 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
1034 return start_error_parse
;
1036 if (ds
!= start_ok
) return ds
;
1037 memcpy(tmp
, "minidump \"", 10);
1040 char path
[MAX_PATH
];
1042 GetTempPathA(sizeof(path
), path
);
1043 GetTempFileNameA(path
, "WD", 0, tmp
+ 10);
1045 else strcpy(tmp
+ 10, file
);
1049 /* FIXME: should generate unix name as well */
1050 dbg_printf("Capturing program state in %s\n", tmp
+ 9);
1052 hFile
= parser_generate_command_file(tmp
, "detach", NULL
);
1053 if (hFile
== INVALID_HANDLE_VALUE
) return start_error_parse
;
1055 if (dbg_curr_process
->active_debuggee
)
1056 dbg_active_wait_for_first_exception();
1058 dbg_interactiveP
= TRUE
;
1059 parser_handle(NULL
, hFile
);
1064 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
)
1068 DWORD exit_code
= 0;
1070 if (pcs
== dbg_curr_process
&& dbg_curr_thread
->in_exception
)
1071 exit_code
= dbg_curr_thread
->excpt_record
.ExceptionCode
;
1073 TerminateProcess(pcs
->handle
, exit_code
);
1075 else if (pcs
== dbg_curr_process
)
1077 /* remove all set breakpoints in debuggee code */
1078 break_set_xpoints(FALSE
);
1079 /* needed for single stepping (ugly).
1080 * should this be handled inside the server ???
1082 dbg_curr_process
->be_cpu
->single_step(&dbg_context
, FALSE
);
1083 if (dbg_curr_thread
->in_exception
)
1085 dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
);
1086 ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, DBG_CONTINUE
);
1092 if (!DebugActiveProcessStop(pcs
->pid
)) return FALSE
;
1094 SymCleanup(pcs
->handle
);
1095 dbg_del_process(pcs
);
1100 static BOOL
tgt_process_active_read(HANDLE hProcess
, const void* addr
,
1101 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
1103 return ReadProcessMemory( hProcess
, addr
, buffer
, len
, rlen
);
1106 static BOOL
tgt_process_active_write(HANDLE hProcess
, void* addr
,
1107 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
1109 return WriteProcessMemory( hProcess
, addr
, buffer
, len
, wlen
);
1112 static BOOL
tgt_process_active_get_selector(HANDLE hThread
, DWORD sel
, LDT_ENTRY
* le
)
1114 return GetThreadSelectorEntry( hThread
, sel
, le
);
1117 static struct be_process_io be_process_active_io
=
1119 tgt_process_active_close_process
,
1120 tgt_process_active_read
,
1121 tgt_process_active_write
,
1122 tgt_process_active_get_selector