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
32 #include "wine/debug.h"
33 #include "wine/exception.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
38 static char* dbg_last_cmd_line
;
39 static struct be_process_io be_process_active_io
;
41 static void dbg_init_current_process(void)
45 static void dbg_init_current_thread(void* start
)
49 if (list_count(&dbg_curr_process
->threads
) == 1 /* first thread ? */ &&
50 DBG_IVAR(BreakAllThreadsStartup
))
54 break_set_xpoints(FALSE
);
55 addr
.Mode
= AddrModeFlat
;
56 addr
.Offset
= (DWORD_PTR
)start
;
57 break_add_break(&addr
, TRUE
, TRUE
);
58 break_set_xpoints(TRUE
);
63 static unsigned dbg_handle_debug_event(DEBUG_EVENT
* de
);
65 /******************************************************************
68 * Sets the debuggee to <pid>
69 * cofe instructs winedbg what to do when first exception is received
70 * (break=FALSE, continue=TRUE)
71 * wfe is set to TRUE if dbg_attach_debuggee should also proceed with all debug events
72 * until the first exception is received (aka: attach to an already running process)
74 BOOL
dbg_attach_debuggee(DWORD pid
, BOOL cofe
)
76 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, pid
, 0))) return FALSE
;
78 if (!DebugActiveProcess(pid
))
80 dbg_printf("Can't attach process %04x: error %u\n", pid
, GetLastError());
81 dbg_del_process(dbg_curr_process
);
84 dbg_curr_process
->continue_on_first_exception
= cofe
;
86 SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL
);
88 dbg_curr_process
->active_debuggee
= TRUE
;
92 static unsigned dbg_fetch_context(void)
94 dbg_context
.ContextFlags
= CONTEXT_CONTROL
96 #ifdef CONTEXT_FLOATING_POINT
97 | CONTEXT_FLOATING_POINT
99 #ifdef CONTEXT_SEGMENTS
102 #ifdef CONTEXT_DEBUG_REGISTERS
103 | CONTEXT_DEBUG_REGISTERS
106 if (!GetThreadContext(dbg_curr_thread
->handle
, &dbg_context
))
108 WINE_WARN("Can't get thread's context\n");
114 /***********************************************************************
115 * dbg_exception_prolog
117 * Examine exception and decide if interactive mode is entered(return TRUE)
118 * or exception is silently continued(return FALSE)
119 * is_debug means the exception is a breakpoint or single step exception
121 static unsigned dbg_exception_prolog(BOOL is_debug
, BOOL first_chance
, const EXCEPTION_RECORD
* rec
)
125 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
127 memory_get_current_pc(&addr
);
128 break_suspend_execution();
129 dbg_curr_thread
->excpt_record
= *rec
;
130 dbg_curr_thread
->in_exception
= TRUE
;
137 dbg_printf(" in %d-bit code (%s)",
138 be_cpu
->pointer_size
* 8,
139 memory_offset_to_string(hexbuf
, addr
.Offset
, 0));
142 dbg_printf(" in vm86 code (%04x:%04x)", addr
.Segment
, (unsigned) addr
.Offset
);
145 dbg_printf(" in 16-bit code (%04x:%04x)", addr
.Segment
, (unsigned) addr
.Offset
);
148 dbg_printf(" in segmented 32-bit code (%04x:%08lx)", addr
.Segment
, (unsigned long) addr
.Offset
);
150 default: dbg_printf(" bad address");
155 /* this will resynchronize builtin dbghelp's internal ELF module list */
156 SymLoadModule(dbg_curr_process
->handle
, 0, 0, 0, 0, 0);
158 if (is_debug
) break_adjust_pc(&addr
, rec
->ExceptionCode
, first_chance
, &is_break
);
160 * Do a quiet backtrace so that we have an idea of what the situation
161 * is WRT the source files.
163 stack_fetch_frames(&dbg_context
);
165 if (is_debug
&& !is_break
&& break_should_continue(&addr
, rec
->ExceptionCode
))
168 if (addr
.Mode
!= dbg_curr_thread
->addr_mode
)
170 const char* name
= NULL
;
174 case AddrMode1616
: name
= "16 bit"; break;
175 case AddrMode1632
: name
= "segmented 32 bit"; break;
176 case AddrModeReal
: name
= "vm86"; break;
177 case AddrModeFlat
: name
= be_cpu
->pointer_size
== 4 ? "32 bit" : "64 bit"; break;
179 dbg_printf("In %s mode.\n", name
);
180 dbg_curr_thread
->addr_mode
= addr
.Mode
;
186 /* This is a real crash, dump some info */
187 be_cpu
->print_context(dbg_curr_thread
->handle
, &dbg_context
, 0);
189 be_cpu
->print_segment_info(dbg_curr_thread
->handle
, &dbg_context
);
190 stack_backtrace(dbg_curr_tid
);
194 static char* last_name
;
195 static char* last_file
;
197 char buffer
[sizeof(SYMBOL_INFO
) + 256];
198 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
199 void* lin
= memory_to_linear_addr(&addr
);
204 si
->SizeOfStruct
= sizeof(*si
);
205 si
->MaxNameLen
= 256;
206 il
.SizeOfStruct
= sizeof(il
);
207 if (SymFromAddr(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp64
, si
) &&
208 SymGetLineFromAddr64(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp
, &il
))
210 if ((!last_name
|| strcmp(last_name
, si
->Name
)) ||
211 (!last_file
|| strcmp(last_file
, il
.FileName
)))
213 HeapFree(GetProcessHeap(), 0, last_name
);
214 HeapFree(GetProcessHeap(), 0, last_file
);
215 last_name
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si
->Name
) + 1), si
->Name
);
216 last_file
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il
.FileName
) + 1), il
.FileName
);
217 dbg_printf("%s () at %s:%u\n", last_name
, last_file
, il
.LineNumber
);
221 if (!is_debug
|| is_break
||
222 dbg_curr_thread
->exec_mode
== dbg_exec_step_over_insn
||
223 dbg_curr_thread
->exec_mode
== dbg_exec_step_into_insn
)
225 ADDRESS64 tmp
= addr
;
226 /* Show where we crashed */
227 memory_disasm_one_insn(&tmp
);
229 source_list_from_addr(&addr
, 0);
234 static void dbg_exception_epilog(void)
236 break_restart_execution(dbg_curr_thread
->exec_count
);
238 * This will have gotten absorbed into the breakpoint info
239 * if it was used. Otherwise it would have been ignored.
240 * In any case, we don't mess with it any more.
242 if (dbg_curr_thread
->exec_mode
== dbg_exec_cont
)
243 dbg_curr_thread
->exec_count
= 0;
244 dbg_curr_thread
->in_exception
= FALSE
;
247 static DWORD
dbg_handle_exception(const EXCEPTION_RECORD
* rec
, BOOL first_chance
)
249 BOOL is_debug
= FALSE
;
250 const THREADNAME_INFO
* pThreadName
;
251 struct dbg_thread
* pThread
;
253 assert(dbg_curr_thread
);
255 WINE_TRACE("exception=%x first_chance=%c\n",
256 rec
->ExceptionCode
, first_chance
? 'Y' : 'N');
258 switch (rec
->ExceptionCode
)
260 case EXCEPTION_BREAKPOINT
:
261 case EXCEPTION_SINGLE_STEP
:
264 case EXCEPTION_NAME_THREAD
:
265 pThreadName
= (const THREADNAME_INFO
*)(rec
->ExceptionInformation
);
266 if (pThreadName
->dwThreadID
== -1)
267 pThread
= dbg_curr_thread
;
269 pThread
= dbg_get_thread(dbg_curr_process
, pThreadName
->dwThreadID
);
272 dbg_printf("Thread ID=%04x not in our list of threads -> can't rename\n", pThreadName
->dwThreadID
);
275 if (dbg_read_memory(pThreadName
->szName
, pThread
->name
, 9))
276 dbg_printf("Thread ID=%04x renamed using MS VC6 extension (name==\"%.9s\")\n",
277 pThread
->tid
, pThread
->name
);
281 if (first_chance
&& !is_debug
&& !DBG_IVAR(BreakOnFirstChance
) &&
282 !(rec
->ExceptionFlags
& EH_STACK_INVALID
))
284 /* pass exception to program except for debug exceptions */
285 return DBG_EXCEPTION_NOT_HANDLED
;
290 /* print some infos */
292 first_chance
? "First chance exception" : "Unhandled exception");
293 switch (rec
->ExceptionCode
)
295 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
296 dbg_printf("divide by zero");
298 case EXCEPTION_INT_OVERFLOW
:
299 dbg_printf("overflow");
301 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
302 dbg_printf("array bounds");
304 case EXCEPTION_ILLEGAL_INSTRUCTION
:
305 dbg_printf("illegal instruction");
307 case EXCEPTION_STACK_OVERFLOW
:
308 dbg_printf("stack overflow");
310 case EXCEPTION_PRIV_INSTRUCTION
:
311 dbg_printf("privileged instruction");
313 case EXCEPTION_ACCESS_VIOLATION
:
314 if (rec
->NumberParameters
== 2)
315 dbg_printf("page fault on %s access to 0x%08lx",
316 rec
->ExceptionInformation
[0] == EXCEPTION_WRITE_FAULT
? "write" :
317 rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
? "execute" : "read",
318 rec
->ExceptionInformation
[1]);
320 dbg_printf("page fault");
322 case EXCEPTION_DATATYPE_MISALIGNMENT
:
323 dbg_printf("Alignment");
331 case STATUS_POSSIBLE_DEADLOCK
:
335 addr
.Mode
= AddrModeFlat
;
336 addr
.Offset
= rec
->ExceptionInformation
[0];
338 dbg_printf("wait failed on critical section ");
339 print_address(&addr
, FALSE
);
341 if (!DBG_IVAR(BreakOnCritSectTimeOut
))
344 return DBG_EXCEPTION_NOT_HANDLED
;
347 case EXCEPTION_WINE_STUB
:
349 char dll
[32], name
[64];
350 memory_get_string(dbg_curr_process
,
351 (void*)rec
->ExceptionInformation
[0], TRUE
, FALSE
,
353 if (HIWORD(rec
->ExceptionInformation
[1]))
354 memory_get_string(dbg_curr_process
,
355 (void*)rec
->ExceptionInformation
[1], TRUE
, FALSE
,
358 sprintf( name
, "%ld", rec
->ExceptionInformation
[1] );
359 dbg_printf("unimplemented function %s.%s called", dll
, name
);
362 case EXCEPTION_WINE_ASSERTION
:
363 dbg_printf("assertion failed");
365 case EXCEPTION_VM86_INTx
:
366 dbg_printf("interrupt %02lx in vm86 mode", rec
->ExceptionInformation
[0]);
368 case EXCEPTION_VM86_STI
:
369 dbg_printf("sti in vm86 mode");
371 case EXCEPTION_VM86_PICRETURN
:
372 dbg_printf("PIC return in vm86 mode");
374 case EXCEPTION_FLT_DENORMAL_OPERAND
:
375 dbg_printf("denormal float operand");
377 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
378 dbg_printf("divide by zero");
380 case EXCEPTION_FLT_INEXACT_RESULT
:
381 dbg_printf("inexact float result");
383 case EXCEPTION_FLT_INVALID_OPERATION
:
384 dbg_printf("invalid float operation");
386 case EXCEPTION_FLT_OVERFLOW
:
387 dbg_printf("floating point overflow");
389 case EXCEPTION_FLT_UNDERFLOW
:
390 dbg_printf("floating point underflow");
392 case EXCEPTION_FLT_STACK_CHECK
:
393 dbg_printf("floating point stack check");
396 if(rec
->NumberParameters
== 3 && rec
->ExceptionInformation
[0] == CXX_FRAME_MAGIC
)
397 dbg_printf("C++ exception(object = 0x%08lx, type = 0x%08lx)",
398 rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[2]);
400 dbg_printf("C++ exception with strange parameter count %d or magic 0x%08lx",
401 rec
->NumberParameters
, rec
->ExceptionInformation
[0]);
404 dbg_printf("0x%08x", rec
->ExceptionCode
);
408 if( (rec
->ExceptionFlags
& EH_STACK_INVALID
) ) {
409 dbg_printf( ", invalid program stack" );
412 if (dbg_exception_prolog(is_debug
, first_chance
, rec
))
414 dbg_interactiveP
= TRUE
;
417 dbg_exception_epilog();
422 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
);
424 static void fetch_module_name(void* name_addr
, BOOL unicode
, void* mod_addr
,
425 WCHAR
* buffer
, size_t bufsz
, BOOL is_pcs
)
427 static WCHAR pcspid
[] = {'P','r','o','c','e','s','s','_','%','0','8','x',0};
428 static WCHAR dlladdr
[] = {'D','L','L','_','%','0','8','l','x',0};
430 memory_get_string_indirect(dbg_curr_process
, name_addr
, unicode
, buffer
, bufsz
);
432 !GetModuleFileNameExW(dbg_curr_process
->handle
, mod_addr
, buffer
, bufsz
))
437 WORD (WINAPI
*gpif
)(HANDLE
, LPWSTR
, DWORD
);
439 /* On Windows, when we get the process creation debug event for a process
440 * created by winedbg, the modules' list is not initialized yet. Hence,
441 * GetModuleFileNameExA (on the main module) will generate an error.
442 * Psapi (starting on XP) provides GetProcessImageFileName() which should
443 * give us the expected result
445 if (!(h
= GetModuleHandleA("psapi")) ||
446 !(gpif
= (void*)GetProcAddress(h
, "GetProcessImageFileNameW")) ||
447 !(gpif
)(dbg_curr_process
->handle
, buffer
, bufsz
))
448 snprintfW(buffer
, bufsz
, pcspid
, dbg_curr_pid
);
451 snprintfW(buffer
, bufsz
, dlladdr
, (unsigned long)mod_addr
);
455 static unsigned dbg_handle_debug_event(DEBUG_EVENT
* de
)
461 DWORD cont
= DBG_CONTINUE
;
463 dbg_curr_pid
= de
->dwProcessId
;
464 dbg_curr_tid
= de
->dwThreadId
;
466 if ((dbg_curr_process
= dbg_get_process(de
->dwProcessId
)) != NULL
)
467 dbg_curr_thread
= dbg_get_thread(dbg_curr_process
, de
->dwThreadId
);
469 dbg_curr_thread
= NULL
;
471 switch (de
->dwDebugEventCode
)
473 case EXCEPTION_DEBUG_EVENT
:
474 if (!dbg_curr_thread
)
476 WINE_ERR("%04x:%04x: not a registered process or thread (perhaps a 16 bit one ?)\n",
477 de
->dwProcessId
, de
->dwThreadId
);
481 WINE_TRACE("%04x:%04x: exception code=%08x\n",
482 de
->dwProcessId
, de
->dwThreadId
,
483 de
->u
.Exception
.ExceptionRecord
.ExceptionCode
);
485 if (dbg_curr_process
->continue_on_first_exception
)
487 dbg_curr_process
->continue_on_first_exception
= FALSE
;
488 if (!DBG_IVAR(BreakOnAttach
)) break;
490 if (dbg_fetch_context())
492 cont
= dbg_handle_exception(&de
->u
.Exception
.ExceptionRecord
,
493 de
->u
.Exception
.dwFirstChance
);
494 if (cont
&& dbg_curr_thread
)
496 SetThreadContext(dbg_curr_thread
->handle
, &dbg_context
);
501 case CREATE_PROCESS_DEBUG_EVENT
:
502 dbg_curr_process
= dbg_add_process(&be_process_active_io
, de
->dwProcessId
,
503 de
->u
.CreateProcessInfo
.hProcess
);
504 if (dbg_curr_process
== NULL
)
506 WINE_ERR("Couldn't create process\n");
509 fetch_module_name(de
->u
.CreateProcessInfo
.lpImageName
,
510 de
->u
.CreateProcessInfo
.fUnicode
,
511 de
->u
.CreateProcessInfo
.lpBaseOfImage
,
512 u
.buffer
, sizeof(u
.buffer
) / sizeof(WCHAR
), TRUE
);
514 WINE_TRACE("%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
515 de
->dwProcessId
, de
->dwThreadId
,
516 wine_dbgstr_w(u
.buffer
),
517 de
->u
.CreateProcessInfo
.lpImageName
,
518 de
->u
.CreateProcessInfo
.lpStartAddress
,
519 de
->u
.CreateProcessInfo
.dwDebugInfoFileOffset
,
520 de
->u
.CreateProcessInfo
.nDebugInfoSize
);
521 dbg_set_process_name(dbg_curr_process
, u
.buffer
);
523 if (!dbg_init(dbg_curr_process
->handle
, u
.buffer
, FALSE
))
524 dbg_printf("Couldn't initiate DbgHelp\n");
525 if (!dbg_load_module(dbg_curr_process
->handle
, de
->u
.CreateProcessInfo
.hFile
, u
.buffer
,
526 (DWORD_PTR
)de
->u
.CreateProcessInfo
.lpBaseOfImage
, 0))
527 dbg_printf("couldn't load main module (%u)\n", GetLastError());
529 WINE_TRACE("%04x:%04x: create thread I @%p\n",
530 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateProcessInfo
.lpStartAddress
);
532 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
534 de
->u
.CreateProcessInfo
.hThread
,
535 de
->u
.CreateProcessInfo
.lpThreadLocalBase
);
536 if (!dbg_curr_thread
)
538 WINE_ERR("Couldn't create thread\n");
541 dbg_init_current_process();
542 dbg_init_current_thread(de
->u
.CreateProcessInfo
.lpStartAddress
);
545 case EXIT_PROCESS_DEBUG_EVENT
:
546 WINE_TRACE("%04x:%04x: exit process (%d)\n",
547 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitProcess
.dwExitCode
);
549 if (dbg_curr_process
== NULL
)
551 WINE_ERR("Unknown process\n");
554 tgt_process_active_close_process(dbg_curr_process
, FALSE
);
555 dbg_printf("Process of pid=%04x has terminated\n", de
->dwProcessId
);
558 case CREATE_THREAD_DEBUG_EVENT
:
559 WINE_TRACE("%04x:%04x: create thread D @%p\n",
560 de
->dwProcessId
, de
->dwThreadId
, de
->u
.CreateThread
.lpStartAddress
);
562 if (dbg_curr_process
== NULL
)
564 WINE_ERR("Unknown process\n");
567 if (dbg_get_thread(dbg_curr_process
, de
->dwThreadId
) != NULL
)
569 WINE_TRACE("Thread already listed, skipping\n");
573 dbg_curr_thread
= dbg_add_thread(dbg_curr_process
,
575 de
->u
.CreateThread
.hThread
,
576 de
->u
.CreateThread
.lpThreadLocalBase
);
577 if (!dbg_curr_thread
)
579 WINE_ERR("Couldn't create thread\n");
582 dbg_init_current_thread(de
->u
.CreateThread
.lpStartAddress
);
585 case EXIT_THREAD_DEBUG_EVENT
:
586 WINE_TRACE("%04x:%04x: exit thread (%d)\n",
587 de
->dwProcessId
, de
->dwThreadId
, de
->u
.ExitThread
.dwExitCode
);
589 if (dbg_curr_thread
== NULL
)
591 WINE_ERR("Unknown thread\n");
594 /* FIXME: remove break point set on thread startup */
595 dbg_del_thread(dbg_curr_thread
);
598 case LOAD_DLL_DEBUG_EVENT
:
599 if (dbg_curr_thread
== NULL
)
601 WINE_ERR("Unknown thread\n");
604 fetch_module_name(de
->u
.LoadDll
.lpImageName
,
605 de
->u
.LoadDll
.fUnicode
,
606 de
->u
.LoadDll
.lpBaseOfDll
,
607 u
.buffer
, sizeof(u
.buffer
) / sizeof(WCHAR
), FALSE
);
609 WINE_TRACE("%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
610 de
->dwProcessId
, de
->dwThreadId
,
611 wine_dbgstr_w(u
.buffer
), de
->u
.LoadDll
.lpBaseOfDll
,
612 de
->u
.LoadDll
.dwDebugInfoFileOffset
,
613 de
->u
.LoadDll
.nDebugInfoSize
);
614 dbg_load_module(dbg_curr_process
->handle
, de
->u
.LoadDll
.hFile
, u
.buffer
,
615 (DWORD_PTR
)de
->u
.LoadDll
.lpBaseOfDll
, 0);
616 break_set_xpoints(FALSE
);
617 break_check_delayed_bp();
618 break_set_xpoints(TRUE
);
619 if (DBG_IVAR(BreakOnDllLoad
))
621 dbg_printf("Stopping on DLL %s loading at %p\n",
622 dbg_W2A(u
.buffer
, -1), de
->u
.LoadDll
.lpBaseOfDll
);
623 if (dbg_fetch_context()) cont
= 0;
627 case UNLOAD_DLL_DEBUG_EVENT
:
628 WINE_TRACE("%04x:%04x: unload DLL @%p\n",
629 de
->dwProcessId
, de
->dwThreadId
,
630 de
->u
.UnloadDll
.lpBaseOfDll
);
631 break_delete_xpoints_from_module((DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
632 SymUnloadModule64(dbg_curr_process
->handle
, (DWORD_PTR
)de
->u
.UnloadDll
.lpBaseOfDll
);
635 case OUTPUT_DEBUG_STRING_EVENT
:
636 if (dbg_curr_thread
== NULL
)
638 WINE_ERR("Unknown thread\n");
642 memory_get_string(dbg_curr_process
,
643 de
->u
.DebugString
.lpDebugStringData
, TRUE
,
644 de
->u
.DebugString
.fUnicode
, u
.bufferA
, sizeof(u
.bufferA
));
645 WINE_TRACE("%04x:%04x: output debug string (%s)\n",
646 de
->dwProcessId
, de
->dwThreadId
, u
.bufferA
);
650 WINE_TRACE("%04x:%04x: rip error=%u type=%u\n",
651 de
->dwProcessId
, de
->dwThreadId
, de
->u
.RipInfo
.dwError
,
652 de
->u
.RipInfo
.dwType
);
656 WINE_TRACE("%04x:%04x: unknown event (%x)\n",
657 de
->dwProcessId
, de
->dwThreadId
, de
->dwDebugEventCode
);
659 if (!cont
) return TRUE
; /* stop execution */
660 ContinueDebugEvent(de
->dwProcessId
, de
->dwThreadId
, cont
);
661 return FALSE
; /* continue execution */
664 static void dbg_resume_debuggee(DWORD cont
)
666 if (dbg_curr_thread
->in_exception
)
669 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
671 dbg_exception_epilog();
672 memory_get_current_pc(&addr
);
673 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
674 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
675 dbg_curr_thread
->exec_mode
,
676 dbg_curr_thread
->exec_count
);
679 if (!SetThreadContext(dbg_curr_thread
->handle
, &dbg_context
))
680 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid
);
683 dbg_interactiveP
= FALSE
;
684 if (!ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, cont
))
685 dbg_printf("Cannot continue on %04lx (%08x)\n", dbg_curr_tid
, cont
);
688 static void wait_exception(void)
692 while (dbg_num_processes() && WaitForDebugEvent(&de
, INFINITE
))
694 if (dbg_handle_debug_event(&de
)) break;
696 dbg_interactiveP
= TRUE
;
699 void dbg_wait_next_exception(DWORD cont
, int count
, int mode
)
702 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
704 if (cont
== DBG_CONTINUE
)
706 dbg_curr_thread
->exec_count
= count
;
707 dbg_curr_thread
->exec_mode
= mode
;
709 dbg_resume_debuggee(cont
);
712 if (!dbg_curr_process
) return;
714 memory_get_current_pc(&addr
);
715 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
716 memory_offset_to_string(hexbuf
, addr
.Offset
, 0),
717 dbg_curr_thread
->exec_mode
,
718 dbg_curr_thread
->exec_count
);
721 void dbg_active_wait_for_first_exception(void)
723 dbg_interactiveP
= FALSE
;
724 /* wait for first exception */
728 static unsigned dbg_start_debuggee(LPSTR cmdLine
)
730 PROCESS_INFORMATION info
;
731 STARTUPINFOA startup
, current
;
734 GetStartupInfoA(¤t
);
736 memset(&startup
, 0, sizeof(startup
));
737 startup
.cb
= sizeof(startup
);
738 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
740 startup
.wShowWindow
= (current
.dwFlags
& STARTF_USESHOWWINDOW
) ?
741 current
.wShowWindow
: SW_SHOWNORMAL
;
743 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
746 flags
= DEBUG_PROCESS
| CREATE_NEW_CONSOLE
;
747 if (!DBG_IVAR(AlsoDebugProcChild
)) flags
|= DEBUG_ONLY_THIS_PROCESS
;
749 if (!CreateProcessA(NULL
, cmdLine
, NULL
, NULL
, FALSE
, flags
,
750 NULL
, NULL
, &startup
, &info
))
752 dbg_printf("Couldn't start process '%s'\n", cmdLine
);
755 if (!info
.dwProcessId
)
757 /* this happens when the program being run is not a Wine binary
758 * (for example, a shell wrapper around a WineLib app)
760 /* Current fix: list running processes and let the user attach
761 * to one of them (sic)
762 * FIXME: implement a real fix => grab the process (from the
763 * running processes) from its name
765 dbg_printf("Debuggee has been started (%s)\n"
766 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
767 "Try to attach to one of those processes:\n", cmdLine
);
768 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
770 info_win32_processes();
773 dbg_curr_pid
= info
.dwProcessId
;
774 if (!(dbg_curr_process
= dbg_add_process(&be_process_active_io
, dbg_curr_pid
, 0))) return FALSE
;
775 dbg_curr_process
->active_debuggee
= TRUE
;
780 void dbg_run_debuggee(const char* args
)
784 WINE_FIXME("Re-running current program with %s as args is broken\n", wine_dbgstr_a(args
));
789 if (!dbg_last_cmd_line
)
791 dbg_printf("Cannot find previously used command line.\n");
794 dbg_start_debuggee(dbg_last_cmd_line
);
795 dbg_active_wait_for_first_exception();
796 source_list_from_addr(NULL
, 0);
800 static BOOL
str2int(const char* str
, DWORD_PTR
* val
)
804 *val
= strtol(str
, &ptr
, 10);
805 return str
< ptr
&& !*ptr
;
809 /******************************************************************
812 * Tries to attach to a running process
813 * Handles the <pid> or <pid> <evt> forms
815 enum dbg_start
dbg_active_attach(int argc
, char* argv
[])
819 /* try the form <myself> pid */
820 if (argc
== 1 && str2int(argv
[0], &pid
) && pid
!= 0)
822 if (!dbg_attach_debuggee(pid
, FALSE
))
823 return start_error_init
;
825 /* try the form <myself> pid evt (Win32 JIT debugger) */
826 else if (argc
== 2 && str2int(argv
[0], &pid
) && pid
!= 0 &&
827 str2int(argv
[1], &evt
) && evt
!= 0)
829 if (!dbg_attach_debuggee(pid
, TRUE
))
831 /* don't care about result */
832 SetEvent((HANDLE
)evt
);
833 return start_error_init
;
835 if (!SetEvent((HANDLE
)evt
))
837 WINE_ERR("Invalid event handle: %lx\n", evt
);
838 return start_error_init
;
840 CloseHandle((HANDLE
)evt
);
842 else return start_error_parse
;
848 /******************************************************************
851 * Launches a debuggee (with its arguments) from argc/argv
853 enum dbg_start
dbg_active_launch(int argc
, char* argv
[])
858 if (argc
== 0) return start_error_parse
;
860 if (!(cmd_line
= HeapAlloc(GetProcessHeap(), 0, len
= 1)))
863 dbg_printf("Out of memory\n");
864 return start_error_init
;
868 for (i
= 0; i
< argc
; i
++)
870 len
+= strlen(argv
[i
]) + 1;
871 if (!(cmd_line
= HeapReAlloc(GetProcessHeap(), 0, cmd_line
, len
)))
873 strcat(cmd_line
, argv
[i
]);
874 cmd_line
[len
- 2] = ' ';
875 cmd_line
[len
- 1] = '\0';
878 if (!dbg_start_debuggee(cmd_line
))
880 HeapFree(GetProcessHeap(), 0, cmd_line
);
881 return start_error_init
;
883 HeapFree(GetProcessHeap(), 0, dbg_last_cmd_line
);
884 dbg_last_cmd_line
= cmd_line
;
888 /******************************************************************
891 * Starts (<pid> or <pid> <evt>) in automatic mode
893 enum dbg_start
dbg_active_auto(int argc
, char* argv
[])
896 enum dbg_start ds
= start_error_parse
;
898 if (!strcmp(argv
[0], "--auto"))
902 ds
= dbg_active_attach(argc
, argv
);
903 if (ds
!= start_ok
) {
904 msgbox_res_id(NULL
, IDS_INVALID_PARAMS
, IDS_AUTO_CAPTION
, MB_OK
);
907 if (!display_crash_dialog()) {
909 dbg_start_interactive(INVALID_HANDLE_VALUE
);
913 hFile
= parser_generate_command_file("echo Modules:", "info share",
914 "echo Threads:", "info threads",
915 "backtrace", "detach", NULL
);
917 else if (!strcmp(argv
[0], "--minidump"))
919 const char* file
= NULL
;
920 char tmp
[8 + 1 + MAX_PATH
]; /* minidump <file> */
923 /* hard stuff now ; we can get things like:
924 * --minidump <pid> 1 arg
925 * --minidump <pid> <evt> 2 args
926 * --minidump <file> <pid> 2 args
927 * --minidump <file> <pid> <evt> 3 args
932 ds
= dbg_active_attach(argc
, argv
);
935 if ((ds
= dbg_active_attach(argc
, argv
)) != start_ok
)
938 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
943 ds
= dbg_active_attach(argc
- 1, argv
+ 1);
946 return start_error_parse
;
948 if (ds
!= start_ok
) return ds
;
949 memcpy(tmp
, "minidump \"", 10);
954 GetTempPathA(sizeof(path
), path
);
955 GetTempFileNameA(path
, "WD", 0, tmp
+ 10);
957 else strcpy(tmp
+ 10, file
);
961 /* FIXME: should generate unix name as well */
962 dbg_printf("Capturing program state in %s\n", tmp
+ 9);
964 hFile
= parser_generate_command_file(tmp
, "detach", NULL
);
966 else return start_error_parse
;
967 if (hFile
== INVALID_HANDLE_VALUE
) return start_error_parse
;
969 if (dbg_curr_process
->active_debuggee
)
970 dbg_active_wait_for_first_exception();
972 dbg_interactiveP
= TRUE
;
973 parser_handle(hFile
);
978 static BOOL
tgt_process_active_close_process(struct dbg_process
* pcs
, BOOL kill
)
980 if (pcs
== dbg_curr_process
)
982 /* remove all set breakpoints in debuggee code */
983 break_set_xpoints(FALSE
);
984 /* needed for single stepping (ugly).
985 * should this be handled inside the server ???
987 be_cpu
->single_step(&dbg_context
, FALSE
);
988 if (dbg_curr_thread
->in_exception
)
990 SetThreadContext(dbg_curr_thread
->handle
, &dbg_context
);
991 ContinueDebugEvent(dbg_curr_pid
, dbg_curr_tid
, DBG_CONTINUE
);
996 TerminateProcess(pcs
->handle
, 0);
1000 if (!DebugActiveProcessStop(pcs
->pid
)) return FALSE
;
1002 SymCleanup(pcs
->handle
);
1003 dbg_del_process(pcs
);
1008 static BOOL
tgt_process_active_read(HANDLE hProcess
, const void* addr
,
1009 void* buffer
, SIZE_T len
, SIZE_T
* rlen
)
1011 return ReadProcessMemory( hProcess
, addr
, buffer
, len
, rlen
);
1014 static BOOL
tgt_process_active_write(HANDLE hProcess
, void* addr
,
1015 const void* buffer
, SIZE_T len
, SIZE_T
* wlen
)
1017 return WriteProcessMemory( hProcess
, addr
, buffer
, len
, wlen
);
1020 static BOOL
tgt_process_active_get_selector(HANDLE hThread
, DWORD sel
, LDT_ENTRY
* le
)
1022 return GetThreadSelectorEntry( hThread
, sel
, le
);
1025 static struct be_process_io be_process_active_io
=
1027 tgt_process_active_close_process
,
1028 tgt_process_active_read
,
1029 tgt_process_active_write
,
1030 tgt_process_active_get_selector