fonts: Add an 11 ppem strike of 'Small Fonts'.
[wine/testsucceed.git] / programs / winedbg / tgt_active.c
blobcdd50ad36f56b42753072f4a6198e310261162c4
1 /*
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdarg.h>
28 #include "debugger.h"
29 #include "winternl.h"
30 #include "wine/debug.h"
31 #include "wine/exception.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
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)
44 if (start)
46 if (dbg_curr_process->threads &&
47 !dbg_curr_process->threads->next && /* first thread ? */
48 DBG_IVAR(BreakAllThreadsStartup))
50 ADDRESS addr;
52 break_set_xpoints(FALSE);
53 addr.Mode = AddrModeFlat;
54 addr.Offset = (DWORD)start;
55 break_add_break(&addr, TRUE, TRUE);
56 break_set_xpoints(TRUE);
61 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de);
63 /******************************************************************
64 * dbg_attach_debuggee
66 * Sets the debuggee to <pid>
67 * cofe instructs winedbg what to do when first exception is received
68 * (break=FALSE, continue=TRUE)
69 * wfe is set to TRUE if dbg_attach_debuggee should also proceed with all debug events
70 * until the first exception is received (aka: attach to an already running process)
72 BOOL dbg_attach_debuggee(DWORD pid, BOOL cofe, BOOL wfe)
74 DEBUG_EVENT de;
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 %lx: error %ld\n", pid, GetLastError());
81 dbg_del_process(dbg_curr_process);
82 return FALSE;
84 dbg_curr_process->continue_on_first_exception = cofe;
86 SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL);
88 if (wfe) /* shall we proceed all debug events until we get an exception ? */
90 dbg_interactiveP = FALSE;
91 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
93 if (dbg_handle_debug_event(&de)) break;
95 if (dbg_curr_process) dbg_interactiveP = TRUE;
97 return TRUE;
100 static unsigned dbg_fetch_context(void)
102 dbg_context.ContextFlags = CONTEXT_CONTROL
103 | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT
104 #ifdef CONTEXT_SEGMENTS
105 | CONTEXT_SEGMENTS
106 #endif
107 #ifdef CONTEXT_DEBUG_REGISTERS
108 | CONTEXT_DEBUG_REGISTERS
109 #endif
111 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
113 WINE_WARN("Can't get thread's context\n");
114 return FALSE;
116 return TRUE;
119 static unsigned dbg_exception_prolog(BOOL is_debug, const EXCEPTION_RECORD* rec)
121 ADDRESS addr;
122 BOOL is_break;
124 memory_get_current_pc(&addr);
125 break_suspend_execution();
126 dbg_curr_thread->excpt_record = *rec;
127 dbg_curr_thread->in_exception = TRUE;
129 if (!is_debug)
131 switch (addr.Mode)
133 case AddrModeFlat: dbg_printf(" in 32-bit code (0x%08lx)", addr.Offset); break;
134 case AddrModeReal: dbg_printf(" in vm86 code (%04x:%04lx)", addr.Segment, addr.Offset); break;
135 case AddrMode1616: dbg_printf(" in 16-bit code (%04x:%04lx)", addr.Segment, addr.Offset); break;
136 case AddrMode1632: dbg_printf(" in 32-bit code (%04x:%08lx)", addr.Segment, addr.Offset); break;
137 default: dbg_printf(" bad address");
139 dbg_printf(".\n");
142 /* this will resynchronize builtin dbghelp's internal ELF module list */
143 SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0);
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();
150 if (is_debug &&
151 break_should_continue(&addr, rec->ExceptionCode, &dbg_curr_thread->exec_count, &is_break))
152 return FALSE;
154 if (addr.Mode != dbg_curr_thread->addr_mode)
156 const char* name = NULL;
158 switch (addr.Mode)
160 case AddrMode1616: name = "16 bit"; break;
161 case AddrMode1632: name = "32 bit"; break;
162 case AddrModeReal: name = "vm86"; break;
163 case AddrModeFlat: name = "32 bit"; break;
166 dbg_printf("In %s mode.\n", name);
167 dbg_curr_thread->addr_mode = addr.Mode;
169 display_print();
171 if (!is_debug)
173 /* This is a real crash, dump some info */
174 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
175 stack_info();
176 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
177 stack_backtrace(dbg_curr_tid);
179 else
181 static char* last_name;
182 static char* last_file;
184 char buffer[sizeof(SYMBOL_INFO) + 256];
185 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
186 void* lin = memory_to_linear_addr(&addr);
187 DWORD64 disp64;
188 IMAGEHLP_LINE il;
189 DWORD disp;
191 si->SizeOfStruct = sizeof(*si);
192 si->MaxNameLen = 256;
193 il.SizeOfStruct = sizeof(il);
194 if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) &&
195 SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
197 if ((!last_name || strcmp(last_name, si->Name)) ||
198 (!last_file || strcmp(last_file, il.FileName)))
200 HeapFree(GetProcessHeap(), 0, last_name);
201 HeapFree(GetProcessHeap(), 0, last_file);
202 last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name);
203 last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName);
204 dbg_printf("%s () at %s:%ld\n", last_name, last_file, il.LineNumber);
208 if (!is_debug || is_break ||
209 dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
210 dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
212 ADDRESS tmp = addr;
213 /* Show where we crashed */
214 memory_disasm_one_insn(&tmp);
216 source_list_from_addr(&addr, 0);
218 return TRUE;
221 static void dbg_exception_epilog(void)
223 break_restart_execution(dbg_curr_thread->exec_count);
225 * This will have gotten absorbed into the breakpoint info
226 * if it was used. Otherwise it would have been ignored.
227 * In any case, we don't mess with it any more.
229 if (dbg_curr_thread->exec_mode == dbg_exec_cont)
230 dbg_curr_thread->exec_count = 0;
231 dbg_curr_thread->in_exception = FALSE;
234 static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
236 BOOL is_debug = FALSE;
237 THREADNAME_INFO* pThreadName;
238 struct dbg_thread* pThread;
240 assert(dbg_curr_thread);
242 WINE_TRACE("exception=%lx first_chance=%c\n",
243 rec->ExceptionCode, first_chance ? 'Y' : 'N');
245 switch (rec->ExceptionCode)
247 case EXCEPTION_BREAKPOINT:
248 case EXCEPTION_SINGLE_STEP:
249 is_debug = TRUE;
250 break;
251 case EXCEPTION_NAME_THREAD:
252 pThreadName = (THREADNAME_INFO*)(rec->ExceptionInformation);
253 if (pThreadName->dwThreadID == -1)
254 pThread = dbg_curr_thread;
255 else
256 pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
258 if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
259 dbg_printf("Thread ID=0x%lx renamed using MS VC6 extension (name==\"%s\")\n",
260 pThread->tid, pThread->name);
261 return DBG_CONTINUE;
264 if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance) &&
265 !(rec->ExceptionFlags & EH_STACK_INVALID))
267 /* pass exception to program except for debug exceptions */
268 return DBG_EXCEPTION_NOT_HANDLED;
271 if (!is_debug)
273 /* print some infos */
274 dbg_printf("%s: ",
275 first_chance ? "First chance exception" : "Unhandled exception");
276 switch (rec->ExceptionCode)
278 case EXCEPTION_INT_DIVIDE_BY_ZERO:
279 dbg_printf("divide by zero");
280 break;
281 case EXCEPTION_INT_OVERFLOW:
282 dbg_printf("overflow");
283 break;
284 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
285 dbg_printf("array bounds");
286 break;
287 case EXCEPTION_ILLEGAL_INSTRUCTION:
288 dbg_printf("illegal instruction");
289 break;
290 case EXCEPTION_STACK_OVERFLOW:
291 dbg_printf("stack overflow");
292 break;
293 case EXCEPTION_PRIV_INSTRUCTION:
294 dbg_printf("privileged instruction");
295 break;
296 case EXCEPTION_ACCESS_VIOLATION:
297 if (rec->NumberParameters == 2)
298 dbg_printf("page fault on %s access to 0x%08lx",
299 rec->ExceptionInformation[0] ? "write" : "read",
300 rec->ExceptionInformation[1]);
301 else
302 dbg_printf("page fault");
303 break;
304 case EXCEPTION_DATATYPE_MISALIGNMENT:
305 dbg_printf("Alignment");
306 break;
307 case DBG_CONTROL_C:
308 dbg_printf("^C");
309 break;
310 case CONTROL_C_EXIT:
311 dbg_printf("^C");
312 break;
313 case STATUS_POSSIBLE_DEADLOCK:
315 ADDRESS addr;
317 addr.Mode = AddrModeFlat;
318 addr.Offset = rec->ExceptionInformation[0];
320 dbg_printf("wait failed on critical section ");
321 print_address(&addr, FALSE);
323 if (!DBG_IVAR(BreakOnCritSectTimeOut))
325 dbg_printf("\n");
326 return DBG_EXCEPTION_NOT_HANDLED;
328 break;
329 case EXCEPTION_WINE_STUB:
331 char dll[32], name[64];
332 memory_get_string(dbg_curr_process,
333 (void*)rec->ExceptionInformation[0], TRUE, FALSE,
334 dll, sizeof(dll));
335 if (HIWORD(rec->ExceptionInformation[1]))
336 memory_get_string(dbg_curr_process,
337 (void*)rec->ExceptionInformation[1], TRUE, FALSE,
338 name, sizeof(name));
339 else
340 sprintf( name, "%ld", rec->ExceptionInformation[1] );
341 dbg_printf("unimplemented function %s.%s called", dll, name);
343 break;
344 case EXCEPTION_WINE_ASSERTION:
345 dbg_printf("assertion failed");
346 break;
347 case EXCEPTION_VM86_INTx:
348 dbg_printf("interrupt %02lx in vm86 mode", rec->ExceptionInformation[0]);
349 break;
350 case EXCEPTION_VM86_STI:
351 dbg_printf("sti in vm86 mode");
352 break;
353 case EXCEPTION_VM86_PICRETURN:
354 dbg_printf("PIC return in vm86 mode");
355 break;
356 case EXCEPTION_FLT_DENORMAL_OPERAND:
357 dbg_printf("denormal float operand");
358 break;
359 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
360 dbg_printf("divide by zero");
361 break;
362 case EXCEPTION_FLT_INEXACT_RESULT:
363 dbg_printf("inexact float result");
364 break;
365 case EXCEPTION_FLT_INVALID_OPERATION:
366 dbg_printf("invalid float operation");
367 break;
368 case EXCEPTION_FLT_OVERFLOW:
369 dbg_printf("floating pointer overflow");
370 break;
371 case EXCEPTION_FLT_UNDERFLOW:
372 dbg_printf("floating pointer underflow");
373 break;
374 case EXCEPTION_FLT_STACK_CHECK:
375 dbg_printf("floating point stack check");
376 break;
377 default:
378 dbg_printf("0x%08lx", rec->ExceptionCode);
379 break;
382 if( (rec->ExceptionFlags & EH_STACK_INVALID) ) {
383 dbg_printf( ", invalid program stack" );
386 if (dbg_exception_prolog(is_debug, rec))
388 dbg_interactiveP = TRUE;
389 return 0;
391 dbg_exception_epilog();
393 return DBG_CONTINUE;
396 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill);
398 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
400 char buffer[256];
401 DWORD cont = DBG_CONTINUE;
403 dbg_curr_pid = de->dwProcessId;
404 dbg_curr_tid = de->dwThreadId;
406 if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
407 dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
408 else
409 dbg_curr_thread = NULL;
411 switch (de->dwDebugEventCode)
413 case EXCEPTION_DEBUG_EVENT:
414 if (!dbg_curr_thread)
416 WINE_ERR("%08lx:%08lx: not a registered process or thread (perhaps a 16 bit one ?)\n",
417 de->dwProcessId, de->dwThreadId);
418 break;
421 WINE_TRACE("%08lx:%08lx: exception code=%08lx\n",
422 de->dwProcessId, de->dwThreadId,
423 de->u.Exception.ExceptionRecord.ExceptionCode);
425 if (dbg_curr_process->continue_on_first_exception)
427 dbg_curr_process->continue_on_first_exception = FALSE;
428 if (!DBG_IVAR(BreakOnAttach)) break;
430 if (dbg_fetch_context())
432 cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
433 de->u.Exception.dwFirstChance);
434 if (cont && dbg_curr_thread)
436 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
439 break;
441 case CREATE_PROCESS_DEBUG_EVENT:
442 dbg_curr_process = dbg_add_process(&be_process_active_io, de->dwProcessId,
443 de->u.CreateProcessInfo.hProcess);
444 if (dbg_curr_process == NULL)
446 WINE_ERR("Couldn't create process\n");
447 break;
449 memory_get_string_indirect(dbg_curr_process,
450 de->u.CreateProcessInfo.lpImageName,
451 de->u.CreateProcessInfo.fUnicode,
452 buffer, sizeof(buffer));
453 if (!buffer[0]) strcpy(buffer, "<Debugged Process>");
455 WINE_TRACE("%08lx:%08lx: create process '%s'/%p @%08lx (%ld<%ld>)\n",
456 de->dwProcessId, de->dwThreadId,
457 buffer, de->u.CreateProcessInfo.lpImageName,
458 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress,
459 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
460 de->u.CreateProcessInfo.nDebugInfoSize);
461 dbg_set_process_name(dbg_curr_process, buffer);
463 if (!SymInitialize(dbg_curr_process->handle, NULL, TRUE))
464 dbg_printf("Couldn't initiate DbgHelp\n");
466 WINE_TRACE("%08lx:%08lx: create thread I @%08lx\n",
467 de->dwProcessId, de->dwThreadId,
468 (unsigned long)(void*)de->u.CreateProcessInfo.lpStartAddress);
470 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
471 de->dwThreadId,
472 de->u.CreateProcessInfo.hThread,
473 de->u.CreateProcessInfo.lpThreadLocalBase);
474 if (!dbg_curr_thread)
476 WINE_ERR("Couldn't create thread\n");
477 break;
479 dbg_init_current_process();
480 dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
481 break;
483 case EXIT_PROCESS_DEBUG_EVENT:
484 WINE_TRACE("%08lx:%08lx: exit process (%ld)\n",
485 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
487 if (dbg_curr_process == NULL)
489 WINE_ERR("Unknown process\n");
490 break;
492 tgt_process_active_close_process(dbg_curr_process, FALSE);
493 break;
495 case CREATE_THREAD_DEBUG_EVENT:
496 WINE_TRACE("%08lx:%08lx: create thread D @%08lx\n",
497 de->dwProcessId, de->dwThreadId,
498 (unsigned long)(void*)de->u.CreateThread.lpStartAddress);
500 if (dbg_curr_process == NULL)
502 WINE_ERR("Unknown process\n");
503 break;
505 if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
507 WINE_TRACE("Thread already listed, skipping\n");
508 break;
511 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
512 de->dwThreadId,
513 de->u.CreateThread.hThread,
514 de->u.CreateThread.lpThreadLocalBase);
515 if (!dbg_curr_thread)
517 WINE_ERR("Couldn't create thread\n");
518 break;
520 dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
521 break;
523 case EXIT_THREAD_DEBUG_EVENT:
524 WINE_TRACE("%08lx:%08lx: exit thread (%ld)\n",
525 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
527 if (dbg_curr_thread == NULL)
529 WINE_ERR("Unknown thread\n");
530 break;
532 /* FIXME: remove break point set on thread startup */
533 dbg_del_thread(dbg_curr_thread);
534 break;
536 case LOAD_DLL_DEBUG_EVENT:
537 if (dbg_curr_thread == NULL)
539 WINE_ERR("Unknown thread\n");
540 break;
542 memory_get_string_indirect(dbg_curr_process,
543 de->u.LoadDll.lpImageName,
544 de->u.LoadDll.fUnicode,
545 buffer, sizeof(buffer));
547 WINE_TRACE("%08lx:%08lx: loads DLL %s @%08lx (%ld<%ld>)\n",
548 de->dwProcessId, de->dwThreadId,
549 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll,
550 de->u.LoadDll.dwDebugInfoFileOffset,
551 de->u.LoadDll.nDebugInfoSize);
552 SymLoadModule(dbg_curr_process->handle, de->u.LoadDll.hFile, buffer, NULL,
553 (unsigned long)de->u.LoadDll.lpBaseOfDll, 0);
554 break_set_xpoints(FALSE);
555 break_check_delayed_bp();
556 break_set_xpoints(TRUE);
557 if (DBG_IVAR(BreakOnDllLoad))
559 dbg_printf("Stopping on DLL %s loading at 0x%08lx\n",
560 buffer, (unsigned long)de->u.LoadDll.lpBaseOfDll);
561 if (dbg_fetch_context()) cont = 0;
563 break;
565 case UNLOAD_DLL_DEBUG_EVENT:
566 WINE_TRACE("%08lx:%08lx: unload DLL @%08lx\n",
567 de->dwProcessId, de->dwThreadId,
568 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
569 break_delete_xpoints_from_module((unsigned long)de->u.UnloadDll.lpBaseOfDll);
570 SymUnloadModule(dbg_curr_process->handle,
571 (unsigned long)de->u.UnloadDll.lpBaseOfDll);
572 break;
574 case OUTPUT_DEBUG_STRING_EVENT:
575 if (dbg_curr_thread == NULL)
577 WINE_ERR("Unknown thread\n");
578 break;
581 memory_get_string(dbg_curr_process,
582 de->u.DebugString.lpDebugStringData, TRUE,
583 de->u.DebugString.fUnicode, buffer, sizeof(buffer));
584 WINE_TRACE("%08lx:%08lx: output debug string (%s)\n",
585 de->dwProcessId, de->dwThreadId, buffer);
586 break;
588 case RIP_EVENT:
589 WINE_TRACE("%08lx:%08lx: rip error=%ld type=%ld\n",
590 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
591 de->u.RipInfo.dwType);
592 break;
594 default:
595 WINE_TRACE("%08lx:%08lx: unknown event (%ld)\n",
596 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
598 if (!cont) return TRUE; /* stop execution */
599 ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
600 return FALSE; /* continue execution */
603 static void dbg_resume_debuggee(DWORD cont)
605 if (dbg_curr_thread->in_exception)
607 ADDRESS addr;
609 dbg_exception_epilog();
610 memory_get_current_pc(&addr);
611 WINE_TRACE("Exiting debugger PC=0x%lx mode=%d count=%d\n",
612 addr.Offset, dbg_curr_thread->exec_mode,
613 dbg_curr_thread->exec_count);
614 if (dbg_curr_thread)
616 if (!SetThreadContext(dbg_curr_thread->handle, &dbg_context))
617 dbg_printf("Cannot set ctx on %lu\n", dbg_curr_tid);
620 dbg_interactiveP = FALSE;
621 if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
622 dbg_printf("Cannot continue on %lu (%lu)\n", dbg_curr_tid, cont);
625 void dbg_wait_next_exception(DWORD cont, int count, int mode)
627 DEBUG_EVENT de;
628 ADDRESS addr;
630 if (cont == DBG_CONTINUE)
632 dbg_curr_thread->exec_count = count;
633 dbg_curr_thread->exec_mode = mode;
635 dbg_resume_debuggee(cont);
637 while (dbg_curr_process && WaitForDebugEvent(&de, INFINITE))
639 if (dbg_handle_debug_event(&de)) break;
641 if (!dbg_curr_process) return;
642 dbg_interactiveP = TRUE;
644 memory_get_current_pc(&addr);
645 WINE_TRACE("Entering debugger PC=0x%lx mode=%d count=%d\n",
646 addr.Offset, dbg_curr_thread->exec_mode,
647 dbg_curr_thread->exec_count);
650 static void dbg_wait_for_first_exception(void)
652 DEBUG_EVENT de;
654 if (dbg_curr_process)
655 dbg_printf("WineDbg starting on pid 0x%lx\n", dbg_curr_pid);
657 /* wait for first exception */
658 while (WaitForDebugEvent(&de, INFINITE))
660 if (dbg_handle_debug_event(&de)) break;
664 static unsigned dbg_start_debuggee(LPSTR cmdLine)
666 PROCESS_INFORMATION info;
667 STARTUPINFOA startup;
669 memset(&startup, 0, sizeof(startup));
670 startup.cb = sizeof(startup);
671 startup.dwFlags = STARTF_USESHOWWINDOW;
672 startup.wShowWindow = SW_SHOWNORMAL;
674 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
675 * while GUI:s don't
677 if (!CreateProcess(NULL, cmdLine, NULL, NULL,
678 FALSE,
679 DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,
680 NULL, NULL, &startup, &info))
682 dbg_printf("Couldn't start process '%s'\n", cmdLine);
683 return FALSE;
685 if (!info.dwProcessId)
687 /* this happens when the program being run is not a Wine binary
688 * (for example, a shell wrapper around a WineLib app)
690 /* Current fix: list running processes and let the user attach
691 * to one of them (sic)
692 * FIXME: implement a real fix => grab the process (from the
693 * running processes) from its name
695 dbg_printf("Debuggee has been started (%s)\n"
696 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
697 "Try to attach to one of those processes:\n", cmdLine);
698 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
699 Sleep(100);
700 info_win32_processes();
701 return TRUE;
703 dbg_curr_pid = info.dwProcessId;
704 if (!(dbg_curr_process = dbg_add_process(&be_process_active_io, dbg_curr_pid, 0))) return FALSE;
705 dbg_wait_for_first_exception();
707 return TRUE;
710 void dbg_run_debuggee(const char* args)
712 if (args)
714 WINE_FIXME("Re-running current program with %s as args is broken\n", args);
715 return;
717 else
719 DEBUG_EVENT de;
721 if (!dbg_last_cmd_line)
723 dbg_printf("Cannot find previously used command line.\n");
724 return;
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)
737 char* ptr;
739 *val = strtol(str, &ptr, 10);
740 return str < ptr && !*ptr;
744 /******************************************************************
745 * dbg_active_attach
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[])
752 DWORD pid, evt;
754 /* try the form <myself> pid */
755 if (argc == 1 && str2int(argv[0], &pid) && pid != 0)
757 if (!dbg_attach_debuggee(pid, FALSE, FALSE))
758 return start_error_init;
760 /* try the form <myself> pid evt (Win32 JIT debugger) */
761 else if (argc == 2 && str2int(argv[0], &pid) && pid != 0 &&
762 str2int(argv[1], &evt) && evt != 0)
764 if (!dbg_attach_debuggee(pid, TRUE, FALSE))
766 /* don't care about result */
767 SetEvent((HANDLE)evt);
768 return start_error_init;
770 if (!SetEvent((HANDLE)evt))
772 WINE_ERR("Invalid event handle: %lx\n", evt);
773 return start_error_init;
775 CloseHandle((HANDLE)evt);
777 else return start_error_parse;
779 dbg_curr_pid = pid;
780 dbg_wait_for_first_exception();
781 return start_ok;
784 /******************************************************************
785 * dbg_active_launch
787 * Launches a debuggee (with its arguments) from argc/argv
789 enum dbg_start dbg_active_launch(int argc, char* argv[])
791 int i, len;
792 LPSTR cmd_line;
794 if (argc == 0) return start_error_parse;
796 if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, len = 1)))
798 oom_leave:
799 dbg_printf("Out of memory\n");
800 return start_error_init;
802 cmd_line[0] = '\0';
804 for (i = 0; i < argc; i++)
806 len += strlen(argv[i]) + 1;
807 if (!(cmd_line = HeapReAlloc(GetProcessHeap(), 0, cmd_line, len)))
808 goto oom_leave;
809 strcat(cmd_line, argv[i]);
810 cmd_line[len - 2] = ' ';
811 cmd_line[len - 1] = '\0';
814 if (!dbg_start_debuggee(cmd_line))
816 HeapFree(GetProcessHeap(), 0, cmd_line);
817 return start_error_init;
819 HeapFree(GetProcessHeap(), 0, dbg_last_cmd_line);
820 dbg_last_cmd_line = cmd_line;
821 return start_ok;
824 /******************************************************************
825 * dbg_active_auto
827 * Starts (<pid> or <pid> <evt>) in automatic mode
829 enum dbg_start dbg_active_auto(int argc, char* argv[])
831 HANDLE hFile;
832 enum dbg_start ds = start_error_parse;
834 if (!strcmp(argv[0], "--auto"))
836 /* auto mode */
837 argc--; argv++;
838 ds = dbg_active_attach(argc, argv);
839 if (ds != start_ok) return ds;
840 hFile = parser_generate_command_file("echo Modules:", "info share",
841 "echo Threads:", "info threads",
842 NULL);
844 else if (!strcmp(argv[0], "--minidump"))
846 const char* file = NULL;
847 char tmp[8 + 1 + MAX_PATH]; /* minidump <file> */
849 argc--; argv++;
850 /* hard stuff now ; we can get things like:
851 * --minidump <pid> 1 arg
852 * --minidump <pid> <evt> 2 args
853 * --minidump <file> <pid> 2 args
854 * --minidump <file> <pid> <evt> 3 args
856 switch (argc)
858 case 1:
859 ds = dbg_active_attach(argc, argv);
860 break;
861 case 2:
862 if ((ds = dbg_active_attach(argc, argv)) != start_ok)
864 file = argv[0];
865 ds = dbg_active_attach(argc - 1, argv + 1);
867 break;
868 case 3:
869 file = argv[0];
870 ds = dbg_active_attach(argc - 1, argv + 1);
871 break;
872 default:
873 return start_error_parse;
875 if (ds != start_ok) return ds;
876 memcpy(tmp, "minidump \"", 10);
877 if (!file)
879 char path[MAX_PATH];
881 GetTempPath(sizeof(path), path);
882 GetTempFileName(path, "WD", 0, tmp + 10);
884 else strcpy(tmp + 10, file);
885 strcat(tmp, "\"");
886 if (!file)
888 /* FIXME: should generate unix name as well */
889 dbg_printf("Capturing program state in %s\n", tmp + 9);
891 hFile = parser_generate_command_file(tmp, NULL);
893 else return start_error_parse;
894 if (hFile == INVALID_HANDLE_VALUE) return start_error_parse;
896 dbg_interactiveP = TRUE;
897 parser_handle(hFile);
899 return start_ok;
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);
923 return TRUE;
926 static struct be_process_io be_process_active_io =
928 tgt_process_active_close_process,
929 ReadProcessMemory,
930 WriteProcessMemory,