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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/debug.h"
31 #include "wine/exception.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static char* dbg_last_cmd_line;
37 static void dbg_init_current_process(void)
41 static void dbg_init_current_thread(void* start)
45 if (dbg_curr_process->threads &&
46 !dbg_curr_process->threads->next && /* first thread ? */
47 DBG_IVAR(BreakAllThreadsStartup))
51 break_set_xpoints(FALSE);
52 addr.Mode = AddrModeFlat;
53 addr.Offset = (DWORD)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, BOOL cofe, BOOL wfe)
75 if (!(dbg_curr_process = dbg_add_process(pid, 0))) return FALSE;
77 if (!DebugActiveProcess(pid))
79 dbg_printf("Can't attach process %lx: error %ld\n", pid, GetLastError());
80 dbg_del_process(dbg_curr_process);
83 dbg_curr_process->continue_on_first_exception = cofe;
85 if (wfe) /* shall we proceed all debug events until we get an exception ? */
87 dbg_interactiveP = FALSE;
88 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
90 if (dbg_handle_debug_event(&de)) break;
92 if (dbg_curr_process) dbg_interactiveP = TRUE;
97 static unsigned dbg_fetch_context(void)
99 dbg_context.ContextFlags = CONTEXT_CONTROL
101 #ifdef CONTEXT_SEGMENTS
104 #ifdef CONTEXT_DEBUG_REGISTERS
105 | CONTEXT_DEBUG_REGISTERS
108 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
110 WINE_WARN("Can't get thread's context\n");
116 static unsigned dbg_exception_prolog(BOOL is_debug, const EXCEPTION_RECORD* rec)
121 memory_get_current_pc(&addr);
122 break_suspend_execution();
123 dbg_curr_thread->excpt_record = *rec;
124 dbg_curr_thread->in_exception = TRUE;
130 case AddrModeFlat: dbg_printf(" in 32-bit code (0x%08lx)", addr.Offset); break;
131 case AddrModeReal: dbg_printf(" in vm86 code (%04x:%04lx)", addr.Segment, addr.Offset); break;
132 case AddrMode1616: dbg_printf(" in 16-bit code (%04x:%04lx)", addr.Segment, addr.Offset); break;
133 case AddrMode1632: dbg_printf(" in 32-bit code (%04x:%08lx)", addr.Segment, addr.Offset); break;
134 default: dbg_printf(" bad address");
139 /* this will resynchronize builtin dbghelp's internal ELF module list */
140 SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0);
143 * Do a quiet backtrace so that we have an idea of what the situation
144 * is WRT the source files.
146 stack_fetch_frames();
148 break_should_continue(&addr, rec->ExceptionCode, &dbg_curr_thread->exec_count, &is_break))
151 if (addr.Mode != dbg_curr_thread->addr_mode)
153 const char* name = NULL;
157 case AddrMode1616: name = "16 bit"; break;
158 case AddrMode1632: name = "32 bit"; break;
159 case AddrModeReal: name = "vm86"; break;
160 case AddrModeFlat: name = "32 bit"; break;
163 dbg_printf("In %s mode.\n", name);
164 dbg_curr_thread->addr_mode = addr.Mode;
170 /* This is a real crash, dump some info */
171 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context);
173 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
174 stack_backtrace(dbg_curr_tid);
178 static char* last_name;
179 static char* last_file;
181 char buffer[sizeof(SYMBOL_INFO) + 256];
182 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
183 void* lin = memory_to_linear_addr(&addr);
188 si->SizeOfStruct = sizeof(*si);
189 si->MaxNameLen = 256;
190 il.SizeOfStruct = sizeof(il);
191 if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) &&
192 SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
194 if ((!last_name || strcmp(last_name, si->Name)) ||
195 (!last_file || strcmp(last_file, il.FileName)))
197 HeapFree(GetProcessHeap(), 0, last_name);
198 HeapFree(GetProcessHeap(), 0, last_file);
199 last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name);
200 last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName);
201 dbg_printf("%s () at %s:%ld\n", last_name, last_file, il.LineNumber);
205 if (!is_debug || is_break ||
206 dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
207 dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
210 /* Show where we crashed */
211 memory_disasm_one_insn(&tmp);
213 source_list_from_addr(&addr, 0);
218 static void dbg_exception_epilog(void)
220 break_restart_execution(dbg_curr_thread->exec_count);
222 * This will have gotten absorbed into the breakpoint info
223 * if it was used. Otherwise it would have been ignored.
224 * In any case, we don't mess with it any more.
226 if (dbg_curr_thread->exec_mode == dbg_exec_cont)
227 dbg_curr_thread->exec_count = 0;
228 dbg_curr_thread->in_exception = FALSE;
231 static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
233 BOOL is_debug = FALSE;
234 THREADNAME_INFO* pThreadName;
235 struct dbg_thread* pThread;
237 assert(dbg_curr_thread);
239 WINE_TRACE("exception=%lx first_chance=%c\n",
240 rec->ExceptionCode, first_chance ? 'Y' : 'N');
242 switch (rec->ExceptionCode)
244 case EXCEPTION_BREAKPOINT:
245 case EXCEPTION_SINGLE_STEP:
248 case EXCEPTION_NAME_THREAD:
249 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
250 if (pThreadName->dwThreadID == -1)
251 pThread = dbg_curr_thread;
253 pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
255 if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
256 dbg_printf("Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
257 pThread->tid, pThread->name);
261 if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance) &&
262 !(rec->ExceptionFlags & EH_STACK_INVALID))
264 /* pass exception to program except for debug exceptions */
265 return DBG_EXCEPTION_NOT_HANDLED;
270 /* print some infos */
272 first_chance ? "First chance exception" : "Unhandled exception");
273 switch (rec->ExceptionCode)
275 case EXCEPTION_INT_DIVIDE_BY_ZERO:
276 dbg_printf("divide by zero");
278 case EXCEPTION_INT_OVERFLOW:
279 dbg_printf("overflow");
281 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
282 dbg_printf("array bounds");
284 case EXCEPTION_ILLEGAL_INSTRUCTION:
285 dbg_printf("illegal instruction");
287 case EXCEPTION_STACK_OVERFLOW:
288 dbg_printf("stack overflow");
290 case EXCEPTION_PRIV_INSTRUCTION:
291 dbg_printf("privileged instruction");
293 case EXCEPTION_ACCESS_VIOLATION:
294 if (rec->NumberParameters == 2)
295 dbg_printf("page fault on %s access to 0x%08lx",
296 rec->ExceptionInformation[0] ? "write" : "read",
297 rec->ExceptionInformation[1]);
299 dbg_printf("page fault");
301 case EXCEPTION_DATATYPE_MISALIGNMENT:
302 dbg_printf("Alignment");
310 case STATUS_POSSIBLE_DEADLOCK:
314 addr.Mode = AddrModeFlat;
315 addr.Offset = rec->ExceptionInformation[0];
317 dbg_printf("wait failed on critical section ");
318 print_address(&addr, FALSE);
320 if (!DBG_IVAR(BreakOnCritSectTimeOut))
323 return DBG_EXCEPTION_NOT_HANDLED;
326 case EXCEPTION_WINE_STUB:
328 char dll[32], name[64];
329 memory_get_string(dbg_curr_process,
330 (void*)rec->ExceptionInformation[0], TRUE, FALSE,
332 if (HIWORD(rec->ExceptionInformation[1]))
333 memory_get_string(dbg_curr_process,
334 (void*)rec->ExceptionInformation[1], TRUE, FALSE,
337 sprintf( name, "%ld", rec->ExceptionInformation[1] );
338 dbg_printf("unimplemented function %s.%s called", dll, name);
341 case EXCEPTION_WINE_ASSERTION:
342 dbg_printf("assertion failed");
344 case EXCEPTION_VM86_INTx:
345 dbg_printf("interrupt %02lx in vm86 mode", rec->ExceptionInformation[0]);
347 case EXCEPTION_VM86_STI:
348 dbg_printf("sti in vm86 mode");
350 case EXCEPTION_VM86_PICRETURN:
351 dbg_printf("PIC return in vm86 mode");
353 case EXCEPTION_FLT_DENORMAL_OPERAND:
354 dbg_printf("denormal float operand");
356 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
357 dbg_printf("divide by zero");
359 case EXCEPTION_FLT_INEXACT_RESULT:
360 dbg_printf("inexact float result");
362 case EXCEPTION_FLT_INVALID_OPERATION:
363 dbg_printf("invalid float operation");
365 case EXCEPTION_FLT_OVERFLOW:
366 dbg_printf("floating pointer overflow");
368 case EXCEPTION_FLT_UNDERFLOW:
369 dbg_printf("floating pointer underflow");
371 case EXCEPTION_FLT_STACK_CHECK:
372 dbg_printf("floating point stack check");
375 dbg_printf("0x%08lx", rec->ExceptionCode);
379 if( (rec->ExceptionFlags & EH_STACK_INVALID) ) {
380 dbg_printf( ", invalid program stack" );
383 if (dbg_exception_prolog(is_debug, rec))
385 dbg_interactiveP = TRUE;
388 dbg_exception_epilog();
393 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill);
395 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
398 DWORD cont = DBG_CONTINUE;
400 dbg_curr_pid = de->dwProcessId;
401 dbg_curr_tid = de->dwThreadId;
403 if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
404 dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
406 dbg_curr_thread = NULL;
408 switch (de->dwDebugEventCode)
410 case EXCEPTION_DEBUG_EVENT:
411 if (!dbg_curr_thread)
413 WINE_ERR("%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
414 de->dwProcessId, de->dwThreadId);
418 WINE_TRACE("%08lx:%08lx: exception code=%08lx\n",
419 de->dwProcessId, de->dwThreadId,
420 de->u.Exception.ExceptionRecord.ExceptionCode);
422 if (dbg_curr_process->continue_on_first_exception)
424 dbg_curr_process->continue_on_first_exception = FALSE;
425 if (!DBG_IVAR(BreakOnAttach)) break;
427 if (dbg_fetch_context())
429 cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
430 de->u.Exception.dwFirstChance);
431 if (cont && dbg_curr_thread)
433 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
438 case CREATE_PROCESS_DEBUG_EVENT:
439 dbg_curr_process = dbg_add_process(de->dwProcessId,
440 de->u.CreateProcessInfo.hProcess);
441 if (dbg_curr_process == NULL)
443 WINE_ERR("Couldn't create process\n");
446 memory_get_string_indirect(dbg_curr_process,
447 de->u.CreateProcessInfo.lpImageName,
448 de->u.CreateProcessInfo.fUnicode,
449 buffer, sizeof(buffer));
450 if (!buffer[0]) strcpy(buffer, "<Debugged Process>");
452 WINE_TRACE("%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
453 de->dwProcessId, de->dwThreadId,
454 buffer, de->u.CreateProcessInfo.lpImageName,
455 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress,
456 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
457 de->u.CreateProcessInfo.nDebugInfoSize);
458 dbg_set_process_name(dbg_curr_process, buffer);
460 if (!SymInitialize(dbg_curr_process->handle, NULL, TRUE))
461 dbg_printf("Couldn't initiate DbgHelp\n");
463 WINE_TRACE("%08lx:%08lx: create thread I @%08lx\n",
464 de->dwProcessId, de->dwThreadId,
465 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress);
467 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
469 de->u.CreateProcessInfo.hThread,
470 de->u.CreateProcessInfo.lpThreadLocalBase);
471 if (!dbg_curr_thread)
473 WINE_ERR("Couldn't create thread\n");
476 dbg_init_current_process();
477 dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
480 case EXIT_PROCESS_DEBUG_EVENT:
481 WINE_TRACE("%08lx:%08lx: exit process (%ld)\n",
482 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
484 if (dbg_curr_process == NULL)
486 WINE_ERR("Unknown process\n");
489 tgt_process_active_close_process(dbg_curr_process, FALSE);
492 case CREATE_THREAD_DEBUG_EVENT:
493 WINE_TRACE("%08lx:%08lx: create thread D @%08lx\n",
494 de->dwProcessId, de->dwThreadId,
495 (unsigned long)(void*)de->u.CreateThread.lpStartAddress);
497 if (dbg_curr_process == NULL)
499 WINE_ERR("Unknown process\n");
502 if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
504 WINE_TRACE("Thread already listed, skipping\n");
508 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
510 de->u.CreateThread.hThread,
511 de->u.CreateThread.lpThreadLocalBase);
512 if (!dbg_curr_thread)
514 WINE_ERR("Couldn't create thread\n");
517 dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
520 case EXIT_THREAD_DEBUG_EVENT:
521 WINE_TRACE("%08lx:%08lx: exit thread (%ld)\n",
522 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
524 if (dbg_curr_thread == NULL)
526 WINE_ERR("Unknown thread\n");
529 /* FIXME: remove break point set on thread startup */
530 dbg_del_thread(dbg_curr_thread);
533 case LOAD_DLL_DEBUG_EVENT:
534 if (dbg_curr_thread == NULL)
536 WINE_ERR("Unknown thread\n");
539 memory_get_string_indirect(dbg_curr_process,
540 de->u.LoadDll.lpImageName,
541 de->u.LoadDll.fUnicode,
542 buffer, sizeof(buffer));
544 WINE_TRACE("%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
545 de->dwProcessId, de->dwThreadId,
546 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
547 de->u.LoadDll.dwDebugInfoFileOffset,
548 de->u.LoadDll.nDebugInfoSize);
549 SymLoadModule(dbg_curr_process->handle, de->u.LoadDll.hFile, buffer, NULL,
550 (unsigned long)de->u.LoadDll.lpBaseOfDll, 0);
551 break_set_xpoints(FALSE);
552 break_check_delayed_bp();
553 break_set_xpoints(TRUE);
554 if (DBG_IVAR(BreakOnDllLoad))
556 dbg_printf("Stopping on DLL %s loading at 0x%08lx\n",
557 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
558 if (dbg_fetch_context()) cont = 0;
562 case UNLOAD_DLL_DEBUG_EVENT:
563 WINE_TRACE("%08lx:%08lx: unload DLL @%08lx\n",
564 de->dwProcessId, de->dwThreadId,
565 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
566 break_delete_xpoints_from_module((unsigned long)de->u.UnloadDll.lpBaseOfDll);
567 SymUnloadModule(dbg_curr_process->handle,
568 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
571 case OUTPUT_DEBUG_STRING_EVENT:
572 if (dbg_curr_thread == NULL)
574 WINE_ERR("Unknown thread\n");
578 memory_get_string(dbg_curr_process,
579 de->u.DebugString.lpDebugStringData, TRUE,
580 de->u.DebugString.fUnicode, buffer, sizeof(buffer));
581 WINE_TRACE("%08lx:%08lx: output debug string (%s)\n",
582 de->dwProcessId, de->dwThreadId, buffer);
586 WINE_TRACE("%08lx:%08lx: rip error=%ld type=%ld\n",
587 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
588 de->u.RipInfo.dwType);
592 WINE_TRACE("%08lx:%08lx: unknown event (%ld)\n",
593 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
595 if (!cont) return TRUE; /* stop execution */
596 ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
597 return FALSE; /* continue execution */
600 static void dbg_resume_debuggee(DWORD cont)
602 if (dbg_curr_thread->in_exception)
606 dbg_exception_epilog();
607 memory_get_current_pc(&addr);
608 WINE_TRACE("Exiting debugger PC=0x%lx mode=%d count=%d\n",
609 addr.Offset, dbg_curr_thread->exec_mode,
610 dbg_curr_thread->exec_count);
613 if (!SetThreadContext(dbg_curr_thread->handle, &dbg_context))
614 dbg_printf("Cannot set ctx on %lu\n", dbg_curr_tid);
617 dbg_interactiveP = FALSE;
618 if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
619 dbg_printf("Cannot continue on %lu (%lu)\n", dbg_curr_tid, cont);
622 void dbg_wait_next_exception(DWORD cont, int count, int mode)
627 if (cont == DBG_CONTINUE)
629 dbg_curr_thread->exec_count = count;
630 dbg_curr_thread->exec_mode = mode;
632 dbg_resume_debuggee(cont);
634 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
636 if (dbg_handle_debug_event(&de)) break;
638 if (!dbg_curr_process) return;
639 dbg_interactiveP = TRUE;
641 memory_get_current_pc(&addr);
642 WINE_TRACE("Entering debugger PC=0x%lx mode=%d count=%d\n",
643 addr.Offset, dbg_curr_thread->exec_mode,
644 dbg_curr_thread->exec_count);
647 /*static*/ unsigned dbg_main_loop(HANDLE hFile)
651 if (dbg_curr_process)
652 dbg_printf("WineDbg starting on pid 0x%lx\n", dbg_curr_pid);
654 /* wait for first exception */
655 while (WaitForDebugEvent(&de, INFINITE))
657 if (dbg_handle_debug_event(&de)) break;
659 dbg_interactiveP = TRUE;
660 parser_handle(hFile);
665 static unsigned dbg_start_debuggee(LPSTR cmdLine)
667 PROCESS_INFORMATION info;
668 STARTUPINFOA startup;
670 memset(&startup, 0, sizeof(startup));
671 startup.cb = sizeof(startup);
672 startup.dwFlags = STARTF_USESHOWWINDOW;
673 startup.wShowWindow = SW_SHOWNORMAL;
675 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
678 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
680 DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,
681 NULL, NULL, &startup, &info))
683 dbg_printf("Couldn't start process '%s'\n", cmdLine);
686 if (!info.dwProcessId)
688 /* this happens when the program being run is not a Wine binary
689 * (for example, a shell wrapper around a WineLib app)
691 /* Current fix: list running processes and let the user attach
692 * to one of them (sic)
693 * FIXME: implement a real fix => grab the process (from the
694 * running processes) from its name
696 dbg_printf("Debuggee has been started (%s)\n"
697 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
698 "Try to attach to one of those processes:\n", cmdLine);
699 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
701 info_win32_processes();
704 dbg_curr_pid = info.dwProcessId;
705 if (!(dbg_curr_process = dbg_add_process(dbg_curr_pid, 0))) return FALSE;
710 void dbg_run_debuggee(const char* args)
714 WINE_FIXME("Re-running current program with %s as args is broken\n", args);
721 if (!dbg_last_cmd_line)
723 dbg_printf("Cannot find previously used command line.\n");
726 dbg_start_debuggee(dbg_last_cmd_line);
727 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
729 if (dbg_handle_debug_event(&de)) break;
731 source_list_from_addr(NULL, 0);
735 static BOOL str2int(const char* str, DWORD* val)
739 *val = strtol(str, &ptr, 10);
740 return str < ptr && !*ptr;
744 /******************************************************************
747 * Tries to attach to a running process
748 * Handles the <pid> or <pid> <evt> forms
750 enum dbg_start dbg_active_attach(int argc, char* argv[])
754 /* try the form <myself> pid */
755 if (argc == 1 && str2int(argv[0], &pid) && pid != 0)
757 if (dbg_attach_debuggee(pid, FALSE, FALSE))
762 return start_error_init;
765 /* try the form <myself> pid evt (Win32 JIT debugger) */
766 if (argc == 2 && str2int(argv[0], &pid) && pid != 0 &&
767 str2int(argv[1], &evt) && evt != 0)
769 if (!dbg_attach_debuggee(pid, TRUE, FALSE))
771 /* don't care about result */
772 SetEvent((HANDLE)evt);
773 return start_error_init;
775 if (!SetEvent((HANDLE)evt))
777 WINE_ERR("Invalid event handle: %lx\n", evt);
778 return start_error_init;
780 CloseHandle((HANDLE)evt);
784 return start_error_parse;
787 /******************************************************************
790 * Launches a debuggee (with its arguments) from argc/argv
792 enum dbg_start dbg_active_launch(int argc, char* argv[])
797 if (argc == 0) return start_error_parse;
799 if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, len = 1)))
802 dbg_printf("Out of memory\n");
803 return start_error_init;
807 for (i = 0; i < argc; i++)
809 len += strlen(argv[i]) + 1;
810 if (!(cmd_line = HeapReAlloc(GetProcessHeap(), 0, cmd_line, len)))
812 strcat(cmd_line, argv[i]);
813 cmd_line[len - 2] = ' ';
814 cmd_line[len - 1] = '\0';
817 if (!dbg_start_debuggee(cmd_line))
819 HeapFree(GetProcessHeap(), 0, cmd_line);
820 return start_error_init;
822 HeapFree(GetProcessHeap(), 0, dbg_last_cmd_line);
823 dbg_last_cmd_line = cmd_line;
827 /******************************************************************
830 * Starts (<pid> or <pid> <evt>) in automatic mode
832 enum dbg_start dbg_active_auto(int argc, char* argv[])
835 enum dbg_start ds = start_error_parse;
837 if (!strcmp(argv[0], "--auto"))
841 ds = dbg_active_attach(argc, argv);
842 if (ds != start_ok) return ds;
843 hFile = parser_generate_command_file("echo Modules:", "info share",
844 "echo Threads:", "info threads",
847 else if (!strcmp(argv[0], "--minidump"))
849 const char* file = NULL;
850 char tmp[8 + 1 + MAX_PATH]; /* minidump <file> */
853 /* hard stuff now ; we can get things like:
854 * --minidump <pid> 1 arg
855 * --minidump <pid> <evt> 2 args
856 * --minidump <file> <pid> 2 args
857 * --minidump <file> <pid> <evt> 3 args
862 ds = dbg_active_attach(argc, argv);
865 if ((ds = dbg_active_attach(argc, argv)) != start_ok)
868 ds = dbg_active_attach(argc - 1, argv + 1);
873 ds = dbg_active_attach(argc - 1, argv + 1);
876 return start_error_parse;
878 if (ds != start_ok) return ds;
879 memcpy(tmp, "minidump \"", 10);
884 GetTempPath(sizeof(path), path);
885 GetTempFileName(path, "WD", 0, tmp + 10);
887 else strcpy(tmp + 10, file);
891 /* FIXME: should generate unix name as well */
892 dbg_printf("Capturing program state in %s\n", tmp + 9);
894 hFile = parser_generate_command_file(tmp, NULL);
896 else return start_error_parse;
897 if (hFile == INVALID_HANDLE_VALUE) return start_error_parse;
898 dbg_main_loop(hFile);
902 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill)
904 if (pcs == dbg_curr_process)
906 /* remove all set breakpoints in debuggee code */
907 break_set_xpoints(FALSE);
908 /* needed for single stepping (ugly).
909 * should this be handled inside the server ???
911 be_cpu->single_step(&dbg_context, FALSE);
912 if (dbg_curr_thread->in_exception)
914 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
915 ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, DBG_CONTINUE);
917 if (!kill && !DebugActiveProcessStop(dbg_curr_pid)) return FALSE;
919 SymCleanup(pcs->handle);
920 dbg_printf("Process of pid=0x%08lx has terminated\n", pcs->pid);
921 dbg_del_process(pcs);
926 struct be_process_io be_process_active_io =
928 tgt_process_active_close_process,