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 (pid
== GetCurrentProcessId())
75 dbg_printf("WineDbg can't debug its own process. Please use another process ID.\n");
78 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, pid
, 0))) return FALSE
;
80 if (!DebugActiveProcess(pid
))
82 dbg_printf("Can't attach process %04lx: error %lu\n", pid
, GetLastError());
83 dbg_del_process(dbg_curr_process
);
87 SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL
);
89 dbg_curr_process
->active_debuggee
= TRUE
;
93 static unsigned dbg_fetch_context(void)
95 if (!dbg_curr_process
->be_cpu
->get_context(dbg_curr_thread
->handle
, &dbg_context
))
97 WINE_WARN("Can't get thread's context\n");
103 BOOL
dbg_set_curr_thread(DWORD tid
)
105 struct dbg_thread
* thread
;
107 if (!dbg_curr_process
)
109 dbg_printf("No process loaded\n");
113 thread
= dbg_get_thread(dbg_curr_process
, tid
);
116 dbg_curr_thread
= thread
;
118 stack_fetch_frames(&dbg_context
);
122 dbg_printf("No such thread\n");
123 return thread
!= NULL
;
126 /***********************************************************************
127 * dbg_exception_prolog
129 * Examine exception and decide if interactive mode is entered(return TRUE)
130 * or exception is silently continued(return FALSE)
131 * is_debug means the exception is a breakpoint or single step exception
133 static BOOL
dbg_exception_prolog(BOOL is_debug
, const EXCEPTION_RECORD
* rec
)
138 memory_get_current_pc(&addr
);
139 break_suspend_execution();
141 /* this will resynchronize builtin dbghelp's internal ELF module list */
142 SymLoadModule(dbg_curr_process
->handle
, 0, 0, 0, 0, 0);
144 if (is_debug
) break_adjust_pc(&addr
, rec
->ExceptionCode
, dbg_curr_thread
->first_chance
, &is_break
);
146 * Do a quiet backtrace so that we have an idea of what the situation
147 * is WRT the source files.
149 stack_fetch_frames(&dbg_context
);
151 if (is_debug
&& !is_break
&& break_should_continue(&addr
, rec
->ExceptionCode
))
154 if (addr
.Mode
!= dbg_curr_thread
->addr_mode
)
160 case AddrMode1616
: name
= "16 bit"; break;
161 case AddrMode1632
: name
= "segmented 32 bit"; break;
162 case AddrModeReal
: name
= "vm86"; break;
163 case AddrModeFlat
: name
= dbg_curr_process
->be_cpu
->pointer_size
== 4
164 ? "32 bit" : "64 bit"; break;
165 default: return FALSE
;
167 dbg_printf("In %s mode.\n", name
);
168 dbg_curr_thread
->addr_mode
= addr
.Mode
;
174 /* This is a real crash, dump some info */
175 dbg_curr_process
->be_cpu
->print_context(dbg_curr_thread
->handle
, &dbg_context
, 0);
177 dbg_curr_process
->be_cpu
->print_segment_info(dbg_curr_thread
->handle
, &dbg_context
);
178 stack_backtrace(dbg_curr_tid
);
182 static char* last_name
;
183 static char* last_file
;
185 char buffer
[sizeof(SYMBOL_INFO
) + 256];
186 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
187 void* lin
= memory_to_linear_addr(&addr
);
192 si
->SizeOfStruct
= sizeof(*si
);
193 si
->MaxNameLen
= 256;
194 il
.SizeOfStruct
= sizeof(il
);
195 if (SymFromAddr(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp64
, si
) &&
196 SymGetLineFromAddr64(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp
, &il
))
198 if ((!last_name
|| strcmp(last_name
, si
->Name
)) ||
199 (!last_file
|| strcmp(last_file
, il
.FileName
)))
203 last_name
= strdup(si
->Name
);
204 last_file
= strdup(il
.FileName
);
205 dbg_printf("%s () at %s:%lu\n", last_name
, last_file
, il
.LineNumber
);
209 if (!is_debug
|| is_break
||
210 dbg_curr_thread
->exec_mode
== dbg_exec_step_over_insn
||
211 dbg_curr_thread
->exec_mode
== dbg_exec_step_into_insn
)
213 ADDRESS64 tmp
= addr
;
214 /* Show where we crashed */
215 memory_disasm_one_insn(&tmp
);
217 source_list_from_addr(&addr
, 0);
222 static void dbg_exception_epilog(void)
224 break_restart_execution(dbg_curr_thread
->exec_count
);
226 * This will have gotten absorbed into the breakpoint info
227 * if it was used. Otherwise it would have been ignored.
228 * In any case, we don't mess with it any more.
230 if (dbg_curr_thread
->exec_mode
== dbg_exec_cont
)
231 dbg_curr_thread
->exec_count
= 0;
232 dbg_curr_thread
->in_exception
= FALSE
;
235 static DWORD
dbg_handle_exception(const EXCEPTION_RECORD
* rec
, BOOL first_chance
)
237 BOOL is_debug
= FALSE
;
238 const THREADNAME_INFO
* pThreadName
;
239 struct dbg_thread
* pThread
;
241 assert(dbg_curr_thread
);
243 WINE_TRACE("exception=%lx first_chance=%c\n",
244 rec
->ExceptionCode
, first_chance
? 'Y' : 'N');
246 switch (rec
->ExceptionCode
)
248 case EXCEPTION_BREAKPOINT
:
249 case EXCEPTION_SINGLE_STEP
:
252 case EXCEPTION_WINE_NAME_THREAD
:
253 pThreadName
= (const THREADNAME_INFO
*)(rec
->ExceptionInformation
);
255 if (pThreadName
->dwType
!= 0x1000)
256 return DBG_EXCEPTION_NOT_HANDLED
;
257 if (pThreadName
->dwThreadID
== -1)
258 pThread
= dbg_curr_thread
;
260 pThread
= dbg_get_thread(dbg_curr_process
, pThreadName
->dwThreadID
);
263 dbg_printf("Thread ID=%04lx not in our list of threads -> can't rename\n", pThreadName
->dwThreadID
);
266 if (dbg_read_memory(pThreadName
->szName
, pThread
->name
, sizeof(pThread
->name
)))
268 pThread
->name
[sizeof(pThread
->name
) - 1] = '\0';
269 dbg_printf("Thread ID=%04lx renamed using MSVC extension (name==\"%s\")\n",
270 pThread
->tid
, pThread
->name
);
273 case EXCEPTION_INVALID_HANDLE
:
277 if (first_chance
&& !is_debug
&& !DBG_IVAR(BreakOnFirstChance
) &&
278 !(rec
->ExceptionFlags
& EH_STACK_INVALID
))
280 /* pass exception to program except for debug exceptions */
281 return DBG_EXCEPTION_NOT_HANDLED
;
284 dbg_curr_thread
->excpt_record
= *rec
;
285 dbg_curr_thread
->in_exception
= TRUE
;
286 dbg_curr_thread
->first_chance
= first_chance
;
288 if (!is_debug
) info_win32_exception();
290 if (rec
->ExceptionCode
== STATUS_POSSIBLE_DEADLOCK
&& !DBG_IVAR(BreakOnCritSectTimeOut
))
292 dbg_curr_thread
->in_exception
= FALSE
;
293 return DBG_EXCEPTION_NOT_HANDLED
;
296 if (dbg_exception_prolog(is_debug
, rec
))
298 dbg_interactiveP
= TRUE
;
301 dbg_exception_epilog();
306 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
);
308 void fetch_module_name(void* name_addr
, void* mod_addr
, WCHAR
* buffer
, size_t bufsz
)
310 memory_get_string_indirect(dbg_curr_process
, name_addr
, TRUE
, buffer
, bufsz
);
311 if (!buffer
[0] && !GetModuleFileNameExW(dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
313 if (GetMappedFileNameW( dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
315 /* FIXME: proper NT->Dos conversion */
316 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
318 if (!wcsncmp( buffer
, nt_prefixW
, 4 ))
319 memmove( buffer
, buffer
+ 4, (lstrlenW(buffer
+ 4) + 1) * sizeof(WCHAR
) );
322 swprintf(buffer
, bufsz
, L
"DLL_%08lx", (ULONG_PTR
)mod_addr
);
326 static unsigned dbg_handle_debug_event(DEBUG_EVENT
* de
)
332 DWORD size
, cont
= DBG_CONTINUE
;
334 dbg_curr_pid
= de
->dwProcessId
;
335 dbg_curr_tid
= de
->dwThreadId
;
337 if ((dbg_curr_process
= dbg_get_process(de
->dwProcessId
)) != NULL
)
338 dbg_curr_thread
= dbg_get_thread(dbg_curr_process
, de
->dwThreadId
);
340 dbg_curr_thread
= NULL
;
342 switch (de
->dwDebugEventCode
)
344 case EXCEPTION_DEBUG_EVENT
:
345 if (!dbg_curr_thread
)
347 WINE_ERR("%04lx:%04lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
348 de
->dwProcessId
, de
->dwThreadId
);
352 WINE_TRACE("%04lx:%04lx: exception code=%08lx\n",
353 de
->dwProcessId
, de
->dwThreadId
,
354 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
356 if (dbg_curr_process
->event_on_first_exception
)
358 SetEvent(dbg_curr_process
->event_on_first_exception
);
359 CloseHandle(dbg_curr_process
->event_on_first_exception
);
360 dbg_curr_process
->event_on_first_exception
= NULL
;
361 if (!DBG_IVAR(BreakOnAttach
)) break;
363 if (dbg_fetch_context())
365 cont
= dbg_handle_exception(&de
->u
.Exception
.ExceptionRecord
,
366 de
->u
.Exception
.dwFirstChance
);
367 if (cont
&& dbg_curr_thread
)
369 dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
);
374 case CREATE_PROCESS_DEBUG_EVENT
:
375 dbg_curr_process
= dbg_add_process(&be_process_active_io
, de
->dwProcessId
,
376 de
->u
.CreateProcessInfo
.hProcess
);
377 if (dbg_curr_process
== NULL
)
379 WINE_ERR("Couldn't create process\n");
382 size
= ARRAY_SIZE(u
.buffer
);
383 if (!QueryFullProcessImageNameW( dbg_curr_process
->handle
, 0, u
.buffer
, &size
))
385 swprintf(u
.buffer
, ARRAY_SIZE(u
.buffer
), L
"Process_%08x", dbg_curr_pid
);
388 WINE_TRACE("%04lx:%04lx: create process '%s'/%p @%p (%lu<%lu>)\n",
389 de
->dwProcessId
, de
->dwThreadId
,
390 wine_dbgstr_w(u
.buffer
),
391 de
->u
.CreateProcessInfo
.lpImageName
,
392 de
->u
.CreateProcessInfo
.lpStartAddress
,
393 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
394 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
395 dbg_set_process_name(dbg_curr_process
, u
.buffer
);
397 if (!dbg_init(dbg_curr_process
->handle
, u
.buffer
, FALSE
))
398 dbg_printf("Couldn't initiate DbgHelp\n");
399 if (!dbg_load_module(dbg_curr_process
->handle
, de
->u
.CreateProcessInfo
.hFile
, u
.buffer
,
400 (DWORD_PTR
)de
->u
.CreateProcessInfo
.lpBaseOfImage
, 0))
401 dbg_printf("couldn't load main module (%lu)\n", GetLastError());
403 WINE_TRACE("%04lx:%04lx: create thread I @%p\n",
404 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateProcessInfo
.lpStartAddress
);
406 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
408 de
->u
.CreateProcessInfo
.hThread
,
409 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
410 if (!dbg_curr_thread
)
412 WINE_ERR("Couldn't create thread\n");
415 dbg_init_current_process();
416 dbg_init_current_thread(de
->u
.CreateProcessInfo
.lpStartAddress
);
419 case EXIT_PROCESS_DEBUG_EVENT
:
420 WINE_TRACE("%04lx:%04lx: exit process (%ld)\n",
421 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
423 if (dbg_curr_process
== NULL
)
425 WINE_ERR("Unknown process\n");
428 tgt_process_active_close_process(dbg_curr_process
, FALSE
);
429 dbg_printf("Process of pid=%04lx has terminated\n", de
->dwProcessId
);
432 case CREATE_THREAD_DEBUG_EVENT
:
433 WINE_TRACE("%04lx:%04lx: create thread D @%p\n",
434 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateThread
.lpStartAddress
);
436 if (dbg_curr_process
== NULL
)
438 WINE_ERR("Unknown process\n");
441 if (dbg_get_thread(dbg_curr_process
, de
->dwThreadId
) != NULL
)
443 WINE_TRACE("Thread already listed, skipping\n");
447 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
449 de
->u
.CreateThread
.hThread
,
450 de
->u
.CreateThread
.lpThreadLocalBase
);
451 if (!dbg_curr_thread
)
453 WINE_ERR("Couldn't create thread\n");
456 dbg_init_current_thread(de
->u
.CreateThread
.lpStartAddress
);
459 case EXIT_THREAD_DEBUG_EVENT
:
460 WINE_TRACE("%04lx:%04lx: exit thread (%ld)\n",
461 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
463 if (dbg_curr_thread
== NULL
)
465 WINE_ERR("Unknown thread\n");
468 /* FIXME: remove break point set on thread startup */
469 dbg_del_thread(dbg_curr_thread
);
472 case LOAD_DLL_DEBUG_EVENT
:
473 if (dbg_curr_thread
== NULL
)
475 WINE_ERR("Unknown thread\n");
478 fetch_module_name(de
->u
.LoadDll
.lpImageName
, de
->u
.LoadDll
.lpBaseOfDll
,
479 u
.buffer
, ARRAY_SIZE(u
.buffer
));
481 WINE_TRACE("%04lx:%04lx: loads DLL %s @%p (%lu<%lu>)\n",
482 de
->dwProcessId
, de
->dwThreadId
,
483 wine_dbgstr_w(u
.buffer
), de
->u
.LoadDll
.lpBaseOfDll
,
484 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
485 de
->u
.LoadDll
.nDebugInfoSize
);
486 dbg_load_module(dbg_curr_process
->handle
, de
->u
.LoadDll
.hFile
, u
.buffer
,
487 (DWORD_PTR
)de
->u
.LoadDll
.lpBaseOfDll
, 0);
488 break_set_xpoints(FALSE
);
489 break_check_delayed_bp();
490 break_set_xpoints(TRUE
);
491 if (DBG_IVAR(BreakOnDllLoad
))
493 dbg_printf("Stopping on DLL %ls loading at %p\n",
494 u
.buffer
, de
->u
.LoadDll
.lpBaseOfDll
);
495 if (dbg_fetch_context()) cont
= 0;
499 case UNLOAD_DLL_DEBUG_EVENT
:
500 WINE_TRACE("%04lx:%04lx: unload DLL @%p\n",
501 de
->dwProcessId
, de
->dwThreadId
,
502 de
->u
.UnloadDll
.lpBaseOfDll
);
503 break_delete_xpoints_from_module((DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
504 dbg_unload_module(dbg_curr_process
, (DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
507 case OUTPUT_DEBUG_STRING_EVENT
:
508 if (dbg_curr_thread
== NULL
)
510 WINE_ERR("Unknown thread\n");
514 memory_get_string(dbg_curr_process
,
515 de
->u
.DebugString
.lpDebugStringData
, TRUE
,
516 de
->u
.DebugString
.fUnicode
, u
.bufferA
, sizeof(u
.bufferA
));
517 WINE_TRACE("%04lx:%04lx: output debug string (%s)\n",
518 de
->dwProcessId
, de
->dwThreadId
, u
.bufferA
);
522 WINE_TRACE("%04lx:%04lx: rip error=%lu type=%lu\n",
523 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
524 de
->u
.RipInfo
.dwType
);
528 WINE_TRACE("%04lx:%04lx: unknown event (%lx)\n",
529 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
531 if (!cont
) return TRUE
; /* stop execution */
532 ContinueDebugEvent(de
->dwProcessId
, de
->dwThreadId
, cont
);
533 return FALSE
; /* continue execution */
536 static void dbg_resume_debuggee(DWORD cont
)
538 if (dbg_curr_thread
->in_exception
)
541 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
543 dbg_exception_epilog();
544 memory_get_current_pc(&addr
);
545 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
546 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
547 dbg_curr_thread
->exec_mode
,
548 dbg_curr_thread
->exec_count
);
551 if (!dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
))
552 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid
);
555 dbg_interactiveP
= FALSE
;
556 if (!ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, cont
))
557 dbg_printf("Cannot continue on %04lx (%08lx)\n", dbg_curr_tid
, cont
);
560 static void wait_exception(void)
564 while (dbg_num_processes() && WaitForDebugEvent(&de
, INFINITE
))
566 if (dbg_handle_debug_event(&de
)) break;
568 dbg_interactiveP
= TRUE
;
571 void dbg_wait_next_exception(DWORD cont
, int count
, int mode
)
574 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
576 if (cont
== DBG_CONTINUE
)
578 dbg_curr_thread
->exec_count
= count
;
579 dbg_curr_thread
->exec_mode
= mode
;
581 dbg_resume_debuggee(cont
);
584 if (!dbg_curr_process
) return;
586 memory_get_current_pc(&addr
);
587 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
588 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
589 dbg_curr_thread
->exec_mode
,
590 dbg_curr_thread
->exec_count
);
593 void dbg_active_wait_for_first_exception(void)
595 dbg_interactiveP
= FALSE
;
596 /* wait for first exception */
600 static BOOL
dbg_start_debuggee(LPSTR cmdLine
)
602 PROCESS_INFORMATION info
;
603 STARTUPINFOA startup
, current
;
606 GetStartupInfoA(¤t
);
608 memset(&startup
, 0, sizeof(startup
));
609 startup
.cb
= sizeof(startup
);
610 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
612 startup
.wShowWindow
= (current
.dwFlags
& STARTF_USESHOWWINDOW
) ?
613 current
.wShowWindow
: SW_SHOWNORMAL
;
615 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUIs need it
618 flags
= DEBUG_PROCESS
| CREATE_NEW_CONSOLE
;
619 if (!DBG_IVAR(AlsoDebugProcChild
)) flags
|= DEBUG_ONLY_THIS_PROCESS
;
621 if (!CreateProcessA(NULL
, cmdLine
, NULL
, NULL
, FALSE
, flags
,
622 NULL
, NULL
, &startup
, &info
))
624 dbg_printf("Couldn't start process '%s'\n", cmdLine
);
627 if (!info
.dwProcessId
)
629 /* this happens when the program being run is not a Wine binary
630 * (for example, a shell wrapper around a WineLib app)
632 /* Current fix: list running processes and let the user attach
633 * to one of them (sic)
634 * FIXME: implement a real fix => grab the process (from the
635 * running processes) from its name
637 dbg_printf("Debuggee has been started (%s)\n"
638 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
639 "Try to attach to one of those processes:\n", cmdLine
);
640 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
642 info_win32_processes();
645 dbg_curr_pid
= info
.dwProcessId
;
646 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, dbg_curr_pid
, 0))) return FALSE
;
647 dbg_curr_process
->active_debuggee
= TRUE
;
648 if (cmdLine
!= dbg_last_cmd_line
)
650 free(dbg_last_cmd_line
);
651 dbg_last_cmd_line
= cmdLine
;
657 /***********************************************************************
658 * dbg_build_command_line
660 * (converted from dlls/ntdll/unix/env.c)
662 * Build the command line of a process from the argv array.
664 * We must quote and escape characters so that the argv array can be rebuilt
665 * from the command line:
666 * - spaces and tabs must be quoted
668 * - quotes must be escaped
670 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
671 * resulting in an odd number of '\' followed by a '"'
674 * - '\'s are followed by the closing '"' must be doubled,
675 * resulting in an even number of '\' followed by a '"'
678 * - '\'s that are not followed by a '"' can be left as is
682 static char *dbg_build_command_line( char **argv
)
689 for (arg
= argv
; *arg
; arg
++) len
+= 3 + 2 * strlen( *arg
);
690 if (!(ret
= malloc( len
))) return NULL
;
693 for (arg
= argv
; *arg
; arg
++)
695 BOOL has_space
, has_quote
;
699 /* check for quotes and spaces in this argument (first arg is always quoted) */
700 has_space
= (arg
== argv
) || !**arg
|| strchr( *arg
, ' ' ) || strchr( *arg
, '\t' );
701 has_quote
= strchr( *arg
, '"' ) != NULL
;
703 /* now transfer it to the command line */
704 if (has_space
) *p
++ = '"';
705 if (has_quote
|| has_space
)
708 for (a
= *arg
; *a
; a
++)
710 if (*a
== '\\') bcount
++;
713 if (*a
== '"') /* double all the '\\' preceding this '"', plus one */
714 for (i
= 0; i
<= bcount
; i
++) *p
++ = '\\';
727 /* Double all the '\' preceding the closing quote */
728 for (i
= 0; i
< bcount
; i
++) *p
++ = '\\';
733 if (p
> ret
) p
--; /* remove last space */
739 void dbg_run_debuggee(struct list_string
* ls
)
741 if (dbg_curr_process
)
743 dbg_printf("Already attached to a process. Use 'detach' or 'kill' before using 'run'\n");
748 dbg_printf("No active target to be restarted\n");
755 unsigned argc
= 2, i
;
756 struct list_string
* cls
;
758 for (cls
= ls
; cls
; cls
= cls
->next
) argc
++;
759 if (!(argv
= malloc(argc
* sizeof(argv
[0])))) return;
760 argv
[0] = dbg_executable
;
761 for (i
= 1, cls
= ls
; cls
; cls
= cls
->next
, i
++) argv
[i
] = cls
->string
;
763 cl
= dbg_build_command_line(argv
);
766 if (!cl
|| !dbg_start_debuggee(cl
))
774 if (!dbg_last_cmd_line
) dbg_last_cmd_line
= strdup(dbg_executable
);
775 dbg_start_debuggee(dbg_last_cmd_line
);
777 dbg_active_wait_for_first_exception();
778 source_list_from_addr(NULL
, 0);
781 static BOOL
str2int(const char* str
, DWORD_PTR
* val
)
785 *val
= strtol(str
, &ptr
, 0);
786 return str
< ptr
&& !*ptr
;
789 static HANDLE
create_temp_file(void)
791 WCHAR path
[MAX_PATH
], name
[MAX_PATH
];
793 if (!GetTempPathW( MAX_PATH
, path
) || !GetTempFileNameW( path
, L
"wdb", 0, name
))
794 return INVALID_HANDLE_VALUE
;
795 return CreateFileW( name
, GENERIC_READ
|GENERIC_WRITE
|DELETE
, FILE_SHARE_DELETE
,
796 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_DELETE_ON_CLOSE
, 0 );
809 { 0, VER_PLATFORM_WIN32s
, 2, 0, "2.0" },
810 { 0, VER_PLATFORM_WIN32s
, 3, 0, "3.0" },
811 { 0, VER_PLATFORM_WIN32s
, 3, 10, "3.1" },
812 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 0, "95" },
813 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 10, "98" },
814 { 0, VER_PLATFORM_WIN32_WINDOWS
, 4, 90, "ME" },
815 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 3, 51, "NT 3.51" },
816 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 4, 0, "NT 4.0" },
817 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 0, "2000" },
818 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 1, "XP" },
819 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 5, 2, "XP" },
820 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 5, 2, "Server 2003" },
821 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 0, "Vista" },
822 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 0, "Server 2008" },
823 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 1, "7" },
824 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 1, "Server 2008 R2" },
825 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 2, "8" },
826 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 2, "Server 2012" },
827 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 6, 3, "8.1" },
828 { VER_NT_SERVER
, VER_PLATFORM_WIN32_NT
, 6, 3, "Server 2012 R2" },
829 { VER_NT_WORKSTATION
, VER_PLATFORM_WIN32_NT
, 10, 0, "10" },
832 static const char *get_windows_version(void)
834 RTL_OSVERSIONINFOEXW info
= { sizeof(RTL_OSVERSIONINFOEXW
) };
838 RtlGetVersion( &info
);
840 for (i
= 0; i
< ARRAY_SIZE(version_table
); i
++)
842 if (version_table
[i
].type
== info
.wProductType
&&
843 version_table
[i
].platform
== info
.dwPlatformId
&&
844 version_table
[i
].major
== info
.dwMajorVersion
&&
845 version_table
[i
].minor
== info
.dwMinorVersion
)
847 return version_table
[i
].str
;
851 snprintf( str
, sizeof(str
), "%ld.%ld (%d)", info
.dwMajorVersion
,
852 info
.dwMinorVersion
, info
.wProductType
);
856 static void output_system_info(void)
859 static const char platform
[] = "i386";
860 #elif defined(__x86_64__)
861 static const char platform
[] = "x86_64";
862 #elif defined(__arm__)
863 static const char platform
[] = "arm";
864 #elif defined(__aarch64__)
865 static const char platform
[] = "arm64";
870 const char *(CDECL
*wine_get_build_id
)(void);
871 void (CDECL
*wine_get_host_version
)( const char **sysname
, const char **release
);
874 wine_get_build_id
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
875 wine_get_host_version
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
876 if (!IsWow64Process( dbg_curr_process
->handle
, &is_wow64
)) is_wow64
= FALSE
;
878 dbg_printf( "System information:\n" );
879 if (wine_get_build_id
) dbg_printf( " Wine build: %s\n", wine_get_build_id() );
880 dbg_printf( " Platform: %s%s\n", platform
, is_wow64
? " (WOW64)" : "" );
881 dbg_printf( " Version: Windows %s\n", get_windows_version() );
882 if (wine_get_host_version
)
884 const char *sysname
, *release
;
885 wine_get_host_version( &sysname
, &release
);
886 dbg_printf( " Host system: %s\n", sysname
);
887 dbg_printf( " Host version: %s\n", release
);
891 /******************************************************************
894 * Tries to attach to a running process
895 * Handles the <pid> or <pid> <evt> forms
897 enum dbg_start
dbg_active_attach(int argc
, char* argv
[])
901 /* try the form <myself> pid */
902 if (argc
== 1 && str2int(argv
[0], &pid
) && pid
!= 0)
904 if (!dbg_attach_debuggee(pid
))
905 return start_error_init
;
907 /* try the form <myself> pid evt (Win32 JIT debugger) */
908 else if (argc
== 2 && str2int(argv
[0], &pid
) && pid
!= 0 &&
909 str2int(argv
[1], &evt
) && evt
!= 0)
911 if (!dbg_attach_debuggee(pid
))
913 /* don't care about result */
914 SetEvent((HANDLE
)evt
);
915 return start_error_init
;
917 dbg_curr_process
->event_on_first_exception
= (HANDLE
)evt
;
919 else return start_error_parse
;
925 /******************************************************************
928 * Launches a debuggee (with its arguments) from argc/argv
930 enum dbg_start
dbg_active_launch(int argc
, char* argv
[])
934 if (argc
== 0) return start_error_parse
;
936 dbg_executable
= strdup(argv
[0]);
937 cmd_line
= dbg_build_command_line(argv
);
939 if (!dbg_start_debuggee(cmd_line
))
942 return start_error_init
;
948 /******************************************************************
951 * Starts (<pid> or <pid> <evt>) in automatic mode
953 enum dbg_start
dbg_active_auto(int argc
, char* argv
[])
955 HANDLE thread
= 0, event
= 0, input
, output
= INVALID_HANDLE_VALUE
;
956 enum dbg_start ds
= start_error_parse
;
958 DBG_IVAR(BreakOnDllLoad
) = 0;
962 ds
= dbg_active_attach(argc
, argv
);
963 if (ds
!= start_ok
) {
964 msgbox_res_id(NULL
, IDS_INVALID_PARAMS
, IDS_AUTO_CAPTION
, MB_OK
);
968 switch (display_crash_dialog())
973 dbg_start_interactive(NULL
, INVALID_HANDLE_VALUE
);
976 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
977 if (event
) thread
= display_crash_details( event
);
978 if (thread
) dbg_houtput
= output
= create_temp_file();
982 input
= parser_generate_command_file("echo Modules:", "info share",
983 "echo Threads:", "info threads", NULL
);
984 if (input
== INVALID_HANDLE_VALUE
) return start_error_parse
;
986 if (dbg_curr_process
->active_debuggee
)
987 dbg_active_wait_for_first_exception();
989 dbg_interactiveP
= TRUE
;
990 parser_handle(NULL
, input
);
991 output_system_info();
993 if (output
!= INVALID_HANDLE_VALUE
)
996 WaitForSingleObject( thread
, INFINITE
);
997 CloseHandle( output
);
998 CloseHandle( thread
);
999 CloseHandle( event
);
1002 CloseHandle( input
);
1003 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, TRUE
);
1007 /******************************************************************
1008 * dbg_active_minidump
1010 * Starts (<pid> or <pid> <evt>) in minidump mode
1012 enum dbg_start
dbg_active_minidump(int argc
, char* argv
[])
1015 enum dbg_start ds
= start_error_parse
;
1016 const char* file
= NULL
;
1017 char tmp
[8 + 1 + 2 + MAX_PATH
]; /* minidump "<file>" */
1019 dbg_houtput
= GetStdHandle(STD_ERROR_HANDLE
);
1020 DBG_IVAR(BreakOnDllLoad
) = 0;
1023 /* hard stuff now ; we can get things like:
1024 * --minidump <pid> 1 arg
1025 * --minidump <pid> <evt> 2 args
1026 * --minidump <file> <pid> 2 args
1027 * --minidump <file> <pid> <evt> 3 args
1032 ds
= dbg_active_attach(argc
, argv
);
1035 if ((ds
= dbg_active_attach(argc
, argv
)) != start_ok
)
1038 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
1043 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
1046 return start_error_parse
;
1048 if (ds
!= start_ok
) return ds
;
1049 memcpy(tmp
, "minidump \"", 10);
1052 char path
[MAX_PATH
];
1054 GetTempPathA(sizeof(path
), path
);
1055 GetTempFileNameA(path
, "WD", 0, tmp
+ 10);
1057 else strcpy(tmp
+ 10, file
);
1061 /* FIXME: should generate unix name as well */
1062 dbg_printf("Capturing program state in %s\n", tmp
+ 9);
1064 hFile
= parser_generate_command_file(tmp
, "detach", NULL
);
1065 if (hFile
== INVALID_HANDLE_VALUE
) return start_error_parse
;
1067 if (dbg_curr_process
->active_debuggee
)
1068 dbg_active_wait_for_first_exception();
1070 dbg_interactiveP
= TRUE
;
1071 parser_handle(NULL
, hFile
);
1076 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
)
1080 DWORD exit_code
= 0;
1082 if (pcs
== dbg_curr_process
&& dbg_curr_thread
->in_exception
)
1083 exit_code
= dbg_curr_thread
->excpt_record
.ExceptionCode
;
1085 TerminateProcess(pcs
->handle
, exit_code
);
1087 else if (pcs
== dbg_curr_process
)
1089 /* remove all set breakpoints in debuggee code */
1090 break_set_xpoints(FALSE
);
1091 /* needed for single stepping (ugly).
1092 * should this be handled inside the server ???
1094 dbg_curr_process
->be_cpu
->single_step(&dbg_context
, FALSE
);
1095 if (dbg_curr_thread
->in_exception
)
1097 dbg_curr_process
->be_cpu
->set_context(dbg_curr_thread
->handle
, &dbg_context
);
1098 ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, DBG_CONTINUE
);
1104 if (!DebugActiveProcessStop(pcs
->pid
)) return FALSE
;
1106 SymCleanup(pcs
->handle
);
1107 dbg_del_process(pcs
);
1112 static BOOL
tgt_process_active_read(HANDLE hProcess
, const void* addr
,
1113 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
1115 return ReadProcessMemory( hProcess
, addr
, buffer
, len
, rlen
);
1118 static BOOL
tgt_process_active_write(HANDLE hProcess
, void* addr
,
1119 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
1121 return WriteProcessMemory( hProcess
, addr
, buffer
, len
, wlen
);
1124 static BOOL
tgt_process_active_get_selector(HANDLE hThread
, DWORD sel
, LDT_ENTRY
* le
)
1126 return GetThreadSelectorEntry( hThread
, sel
, le
);
1129 static struct be_process_io be_process_active_io
=
1131 tgt_process_active_close_process
,
1132 tgt_process_active_read
,
1133 tgt_process_active_write
,
1134 tgt_process_active_get_selector