configure: Changes from running autconf after previous patch.
[wine/hacks.git] / programs / winedbg / tgt_active.c
blobc0c12dd261a0fb3432c5cb76848f1098379ce398
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "psapi.h"
30 #include "resource.h"
31 #include "winternl.h"
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)
47 if (start)
49 if (list_count(&dbg_curr_process->threads) == 1 /* first thread ? */ &&
50 DBG_IVAR(BreakAllThreadsStartup))
52 ADDRESS64 addr;
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 /******************************************************************
66 * dbg_attach_debuggee
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);
82 return FALSE;
84 dbg_curr_process->continue_on_first_exception = cofe;
86 SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL);
88 dbg_curr_process->active_debuggee = TRUE;
89 return TRUE;
92 static unsigned dbg_fetch_context(void)
94 dbg_context.ContextFlags = CONTEXT_CONTROL
95 | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT
96 #ifdef CONTEXT_SEGMENTS
97 | CONTEXT_SEGMENTS
98 #endif
99 #ifdef CONTEXT_DEBUG_REGISTERS
100 | CONTEXT_DEBUG_REGISTERS
101 #endif
103 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
105 WINE_WARN("Can't get thread's context\n");
106 return FALSE;
108 return TRUE;
111 /***********************************************************************
112 * dbg_exception_prolog
114 * Examine exception and decide if interactive mode is entered(return TRUE)
115 * or exception is silently continued(return FALSE)
116 * is_debug means the exception is a breakpoint or single step exception
118 static unsigned dbg_exception_prolog(BOOL is_debug, BOOL first_chance, const EXCEPTION_RECORD* rec)
120 ADDRESS64 addr;
121 BOOL is_break;
122 char hexbuf[MAX_OFFSET_TO_STR_LEN];
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:
134 dbg_printf(" in %d-bit code (%s)",
135 be_cpu->pointer_size * 8,
136 memory_offset_to_string(hexbuf, addr.Offset, 0));
137 break;
138 case AddrModeReal:
139 dbg_printf(" in vm86 code (%04x:%04x)", addr.Segment, (unsigned) addr.Offset);
140 break;
141 case AddrMode1616:
142 dbg_printf(" in 16-bit code (%04x:%04x)", addr.Segment, (unsigned) addr.Offset);
143 break;
144 case AddrMode1632:
145 dbg_printf(" in segmented 32-bit code (%04x:%08lx)", addr.Segment, (unsigned long) addr.Offset);
146 break;
147 default: dbg_printf(" bad address");
149 dbg_printf(".\n");
152 /* this will resynchronize builtin dbghelp's internal ELF module list */
153 SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0);
155 if (is_debug) break_adjust_pc(&addr, rec->ExceptionCode, first_chance, &is_break);
157 * Do a quiet backtrace so that we have an idea of what the situation
158 * is WRT the source files.
160 stack_fetch_frames(&dbg_context);
162 if (is_debug && !is_break && break_should_continue(&addr, rec->ExceptionCode))
163 return FALSE;
165 if (addr.Mode != dbg_curr_thread->addr_mode)
167 const char* name = NULL;
169 switch (addr.Mode)
171 case AddrMode1616: name = "16 bit"; break;
172 case AddrMode1632: name = "segmented 32 bit"; break;
173 case AddrModeReal: name = "vm86"; break;
174 case AddrModeFlat: name = be_cpu->pointer_size == 4 ? "32 bit" : "64 bit"; break;
176 dbg_printf("In %s mode.\n", name);
177 dbg_curr_thread->addr_mode = addr.Mode;
179 display_print();
181 if (!is_debug)
183 /* This is a real crash, dump some info */
184 be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
185 stack_info(-1);
186 be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
187 stack_backtrace(dbg_curr_tid);
189 else
191 static char* last_name;
192 static char* last_file;
194 char buffer[sizeof(SYMBOL_INFO) + 256];
195 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
196 void* lin = memory_to_linear_addr(&addr);
197 DWORD64 disp64;
198 IMAGEHLP_LINE64 il;
199 DWORD disp;
201 si->SizeOfStruct = sizeof(*si);
202 si->MaxNameLen = 256;
203 il.SizeOfStruct = sizeof(il);
204 if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) &&
205 SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
207 if ((!last_name || strcmp(last_name, si->Name)) ||
208 (!last_file || strcmp(last_file, il.FileName)))
210 HeapFree(GetProcessHeap(), 0, last_name);
211 HeapFree(GetProcessHeap(), 0, last_file);
212 last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name);
213 last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName);
214 dbg_printf("%s () at %s:%u\n", last_name, last_file, il.LineNumber);
218 if (!is_debug || is_break ||
219 dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
220 dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
222 ADDRESS64 tmp = addr;
223 /* Show where we crashed */
224 memory_disasm_one_insn(&tmp);
226 source_list_from_addr(&addr, 0);
228 return TRUE;
231 static void dbg_exception_epilog(void)
233 break_restart_execution(dbg_curr_thread->exec_count);
235 * This will have gotten absorbed into the breakpoint info
236 * if it was used. Otherwise it would have been ignored.
237 * In any case, we don't mess with it any more.
239 if (dbg_curr_thread->exec_mode == dbg_exec_cont)
240 dbg_curr_thread->exec_count = 0;
241 dbg_curr_thread->in_exception = FALSE;
244 static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
246 BOOL is_debug = FALSE;
247 const THREADNAME_INFO* pThreadName;
248 struct dbg_thread* pThread;
250 assert(dbg_curr_thread);
252 WINE_TRACE("exception=%x first_chance=%c\n",
253 rec->ExceptionCode, first_chance ? 'Y' : 'N');
255 switch (rec->ExceptionCode)
257 case EXCEPTION_BREAKPOINT:
258 case EXCEPTION_SINGLE_STEP:
259 is_debug = TRUE;
260 break;
261 case EXCEPTION_NAME_THREAD:
262 pThreadName = (const THREADNAME_INFO*)(rec->ExceptionInformation);
263 if (pThreadName->dwThreadID == -1)
264 pThread = dbg_curr_thread;
265 else
266 pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
267 if(!pThread)
269 dbg_printf("Thread ID=%04x not in our list of threads -> can't rename\n", pThreadName->dwThreadID);
270 return DBG_CONTINUE;
272 if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
273 dbg_printf("Thread ID=%04x renamed using MS VC6 extension (name==\"%.9s\")\n",
274 pThread->tid, pThread->name);
275 return DBG_CONTINUE;
278 if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance) &&
279 !(rec->ExceptionFlags & EH_STACK_INVALID))
281 /* pass exception to program except for debug exceptions */
282 return DBG_EXCEPTION_NOT_HANDLED;
285 if (!is_debug)
287 /* print some infos */
288 dbg_printf("%s: ",
289 first_chance ? "First chance exception" : "Unhandled exception");
290 switch (rec->ExceptionCode)
292 case EXCEPTION_INT_DIVIDE_BY_ZERO:
293 dbg_printf("divide by zero");
294 break;
295 case EXCEPTION_INT_OVERFLOW:
296 dbg_printf("overflow");
297 break;
298 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
299 dbg_printf("array bounds");
300 break;
301 case EXCEPTION_ILLEGAL_INSTRUCTION:
302 dbg_printf("illegal instruction");
303 break;
304 case EXCEPTION_STACK_OVERFLOW:
305 dbg_printf("stack overflow");
306 break;
307 case EXCEPTION_PRIV_INSTRUCTION:
308 dbg_printf("privileged instruction");
309 break;
310 case EXCEPTION_ACCESS_VIOLATION:
311 if (rec->NumberParameters == 2)
312 dbg_printf("page fault on %s access to 0x%08lx",
313 rec->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT ? "write" :
314 rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT ? "execute" : "read",
315 rec->ExceptionInformation[1]);
316 else
317 dbg_printf("page fault");
318 break;
319 case EXCEPTION_DATATYPE_MISALIGNMENT:
320 dbg_printf("Alignment");
321 break;
322 case DBG_CONTROL_C:
323 dbg_printf("^C");
324 break;
325 case CONTROL_C_EXIT:
326 dbg_printf("^C");
327 break;
328 case STATUS_POSSIBLE_DEADLOCK:
330 ADDRESS64 addr;
332 addr.Mode = AddrModeFlat;
333 addr.Offset = rec->ExceptionInformation[0];
335 dbg_printf("wait failed on critical section ");
336 print_address(&addr, FALSE);
338 if (!DBG_IVAR(BreakOnCritSectTimeOut))
340 dbg_printf("\n");
341 return DBG_EXCEPTION_NOT_HANDLED;
343 break;
344 case EXCEPTION_WINE_STUB:
346 char dll[32], name[64];
347 memory_get_string(dbg_curr_process,
348 (void*)rec->ExceptionInformation[0], TRUE, FALSE,
349 dll, sizeof(dll));
350 if (HIWORD(rec->ExceptionInformation[1]))
351 memory_get_string(dbg_curr_process,
352 (void*)rec->ExceptionInformation[1], TRUE, FALSE,
353 name, sizeof(name));
354 else
355 sprintf( name, "%ld", rec->ExceptionInformation[1] );
356 dbg_printf("unimplemented function %s.%s called", dll, name);
358 break;
359 case EXCEPTION_WINE_ASSERTION:
360 dbg_printf("assertion failed");
361 break;
362 case EXCEPTION_VM86_INTx:
363 dbg_printf("interrupt %02lx in vm86 mode", rec->ExceptionInformation[0]);
364 break;
365 case EXCEPTION_VM86_STI:
366 dbg_printf("sti in vm86 mode");
367 break;
368 case EXCEPTION_VM86_PICRETURN:
369 dbg_printf("PIC return in vm86 mode");
370 break;
371 case EXCEPTION_FLT_DENORMAL_OPERAND:
372 dbg_printf("denormal float operand");
373 break;
374 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
375 dbg_printf("divide by zero");
376 break;
377 case EXCEPTION_FLT_INEXACT_RESULT:
378 dbg_printf("inexact float result");
379 break;
380 case EXCEPTION_FLT_INVALID_OPERATION:
381 dbg_printf("invalid float operation");
382 break;
383 case EXCEPTION_FLT_OVERFLOW:
384 dbg_printf("floating point overflow");
385 break;
386 case EXCEPTION_FLT_UNDERFLOW:
387 dbg_printf("floating point underflow");
388 break;
389 case EXCEPTION_FLT_STACK_CHECK:
390 dbg_printf("floating point stack check");
391 break;
392 case CXX_EXCEPTION:
393 if(rec->NumberParameters == 3 && rec->ExceptionInformation[0] == CXX_FRAME_MAGIC)
394 dbg_printf("C++ exception(object = 0x%08lx, type = 0x%08lx)",
395 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
396 else
397 dbg_printf("C++ exception with strange parameter count %d or magic 0x%08lx",
398 rec->NumberParameters, rec->ExceptionInformation[0]);
399 break;
400 default:
401 dbg_printf("0x%08x", rec->ExceptionCode);
402 break;
405 if( (rec->ExceptionFlags & EH_STACK_INVALID) ) {
406 dbg_printf( ", invalid program stack" );
409 if (dbg_exception_prolog(is_debug, first_chance, rec))
411 dbg_interactiveP = TRUE;
412 return 0;
414 dbg_exception_epilog();
416 return DBG_CONTINUE;
419 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill);
421 static void fetch_module_name(void* name_addr, BOOL unicode, void* mod_addr,
422 WCHAR* buffer, size_t bufsz, BOOL is_pcs)
424 static WCHAR pcspid[] = {'P','r','o','c','e','s','s','_','%','0','8','x',0};
425 static WCHAR dlladdr[] = {'D','L','L','_','%','0','8','l','x',0};
427 memory_get_string_indirect(dbg_curr_process, name_addr, unicode, buffer, bufsz);
428 if (!buffer[0] &&
429 !GetModuleFileNameExW(dbg_curr_process->handle, mod_addr, buffer, bufsz))
431 if (is_pcs)
433 HMODULE h;
434 WORD (WINAPI *gpif)(HANDLE, LPWSTR, DWORD);
436 /* On Windows, when we get the process creation debug event for a process
437 * created by winedbg, the modules' list is not initialized yet. Hence,
438 * GetModuleFileNameExA (on the main module) will generate an error.
439 * Psapi (starting on XP) provides GetProcessImageFileName() which should
440 * give us the expected result
442 if (!(h = GetModuleHandleA("psapi")) ||
443 !(gpif = (void*)GetProcAddress(h, "GetProcessImageFileNameW")) ||
444 !(gpif)(dbg_curr_process->handle, buffer, bufsz))
445 snprintfW(buffer, bufsz, pcspid, dbg_curr_pid);
447 else
448 snprintfW(buffer, bufsz, dlladdr, (unsigned long)mod_addr);
452 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
454 union {
455 char bufferA[256];
456 WCHAR buffer[256];
457 } u;
458 DWORD cont = DBG_CONTINUE;
460 dbg_curr_pid = de->dwProcessId;
461 dbg_curr_tid = de->dwThreadId;
463 if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
464 dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
465 else
466 dbg_curr_thread = NULL;
468 switch (de->dwDebugEventCode)
470 case EXCEPTION_DEBUG_EVENT:
471 if (!dbg_curr_thread)
473 WINE_ERR("%04x:%04x: not a registered process or thread (perhaps a 16 bit one ?)\n",
474 de->dwProcessId, de->dwThreadId);
475 break;
478 WINE_TRACE("%04x:%04x: exception code=%08x\n",
479 de->dwProcessId, de->dwThreadId,
480 de->u.Exception.ExceptionRecord.ExceptionCode);
482 if (dbg_curr_process->continue_on_first_exception)
484 dbg_curr_process->continue_on_first_exception = FALSE;
485 if (!DBG_IVAR(BreakOnAttach)) break;
487 if (dbg_fetch_context())
489 cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
490 de->u.Exception.dwFirstChance);
491 if (cont && dbg_curr_thread)
493 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
496 break;
498 case CREATE_PROCESS_DEBUG_EVENT:
499 dbg_curr_process = dbg_add_process(&be_process_active_io, de->dwProcessId,
500 de->u.CreateProcessInfo.hProcess);
501 if (dbg_curr_process == NULL)
503 WINE_ERR("Couldn't create process\n");
504 break;
506 fetch_module_name(de->u.CreateProcessInfo.lpImageName,
507 de->u.CreateProcessInfo.fUnicode,
508 de->u.CreateProcessInfo.lpBaseOfImage,
509 u.buffer, sizeof(u.buffer) / sizeof(WCHAR), TRUE);
511 WINE_TRACE("%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
512 de->dwProcessId, de->dwThreadId,
513 wine_dbgstr_w(u.buffer),
514 de->u.CreateProcessInfo.lpImageName,
515 de->u.CreateProcessInfo.lpStartAddress,
516 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
517 de->u.CreateProcessInfo.nDebugInfoSize);
518 dbg_set_process_name(dbg_curr_process, u.buffer);
520 if (!dbg_init(dbg_curr_process->handle, u.buffer, FALSE))
521 dbg_printf("Couldn't initiate DbgHelp\n");
522 if (!dbg_load_module(dbg_curr_process->handle, de->u.CreateProcessInfo.hFile, u.buffer,
523 (DWORD_PTR)de->u.CreateProcessInfo.lpBaseOfImage, 0))
524 dbg_printf("couldn't load main module (%u)\n", GetLastError());
526 WINE_TRACE("%04x:%04x: create thread I @%p\n",
527 de->dwProcessId, de->dwThreadId, de->u.CreateProcessInfo.lpStartAddress);
529 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
530 de->dwThreadId,
531 de->u.CreateProcessInfo.hThread,
532 de->u.CreateProcessInfo.lpThreadLocalBase);
533 if (!dbg_curr_thread)
535 WINE_ERR("Couldn't create thread\n");
536 break;
538 dbg_init_current_process();
539 dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
540 break;
542 case EXIT_PROCESS_DEBUG_EVENT:
543 WINE_TRACE("%04x:%04x: exit process (%d)\n",
544 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
546 if (dbg_curr_process == NULL)
548 WINE_ERR("Unknown process\n");
549 break;
551 tgt_process_active_close_process(dbg_curr_process, FALSE);
552 dbg_printf("Process of pid=%04x has terminated\n", de->dwProcessId);
553 break;
555 case CREATE_THREAD_DEBUG_EVENT:
556 WINE_TRACE("%04x:%04x: create thread D @%p\n",
557 de->dwProcessId, de->dwThreadId, de->u.CreateThread.lpStartAddress);
559 if (dbg_curr_process == NULL)
561 WINE_ERR("Unknown process\n");
562 break;
564 if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
566 WINE_TRACE("Thread already listed, skipping\n");
567 break;
570 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
571 de->dwThreadId,
572 de->u.CreateThread.hThread,
573 de->u.CreateThread.lpThreadLocalBase);
574 if (!dbg_curr_thread)
576 WINE_ERR("Couldn't create thread\n");
577 break;
579 dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
580 break;
582 case EXIT_THREAD_DEBUG_EVENT:
583 WINE_TRACE("%04x:%04x: exit thread (%d)\n",
584 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
586 if (dbg_curr_thread == NULL)
588 WINE_ERR("Unknown thread\n");
589 break;
591 /* FIXME: remove break point set on thread startup */
592 dbg_del_thread(dbg_curr_thread);
593 break;
595 case LOAD_DLL_DEBUG_EVENT:
596 if (dbg_curr_thread == NULL)
598 WINE_ERR("Unknown thread\n");
599 break;
601 fetch_module_name(de->u.LoadDll.lpImageName,
602 de->u.LoadDll.fUnicode,
603 de->u.LoadDll.lpBaseOfDll,
604 u.buffer, sizeof(u.buffer) / sizeof(WCHAR), FALSE);
606 WINE_TRACE("%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
607 de->dwProcessId, de->dwThreadId,
608 wine_dbgstr_w(u.buffer), de->u.LoadDll.lpBaseOfDll,
609 de->u.LoadDll.dwDebugInfoFileOffset,
610 de->u.LoadDll.nDebugInfoSize);
611 dbg_load_module(dbg_curr_process->handle, de->u.LoadDll.hFile, u.buffer,
612 (DWORD_PTR)de->u.LoadDll.lpBaseOfDll, 0);
613 break_set_xpoints(FALSE);
614 break_check_delayed_bp();
615 break_set_xpoints(TRUE);
616 if (DBG_IVAR(BreakOnDllLoad))
618 dbg_printf("Stopping on DLL %s loading at %p\n",
619 dbg_W2A(u.buffer, -1), de->u.LoadDll.lpBaseOfDll);
620 if (dbg_fetch_context()) cont = 0;
622 break;
624 case UNLOAD_DLL_DEBUG_EVENT:
625 WINE_TRACE("%04x:%04x: unload DLL @%p\n",
626 de->dwProcessId, de->dwThreadId,
627 de->u.UnloadDll.lpBaseOfDll);
628 break_delete_xpoints_from_module((DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
629 SymUnloadModule64(dbg_curr_process->handle, (DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
630 break;
632 case OUTPUT_DEBUG_STRING_EVENT:
633 if (dbg_curr_thread == NULL)
635 WINE_ERR("Unknown thread\n");
636 break;
639 memory_get_string(dbg_curr_process,
640 de->u.DebugString.lpDebugStringData, TRUE,
641 de->u.DebugString.fUnicode, u.bufferA, sizeof(u.bufferA));
642 WINE_TRACE("%04x:%04x: output debug string (%s)\n",
643 de->dwProcessId, de->dwThreadId, u.bufferA);
644 break;
646 case RIP_EVENT:
647 WINE_TRACE("%04x:%04x: rip error=%u type=%u\n",
648 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
649 de->u.RipInfo.dwType);
650 break;
652 default:
653 WINE_TRACE("%04x:%04x: unknown event (%x)\n",
654 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
656 if (!cont) return TRUE; /* stop execution */
657 ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
658 return FALSE; /* continue execution */
661 static void dbg_resume_debuggee(DWORD cont)
663 if (dbg_curr_thread->in_exception)
665 ADDRESS64 addr;
666 char hexbuf[MAX_OFFSET_TO_STR_LEN];
668 dbg_exception_epilog();
669 memory_get_current_pc(&addr);
670 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
671 memory_offset_to_string(hexbuf, addr.Offset, 0),
672 dbg_curr_thread->exec_mode,
673 dbg_curr_thread->exec_count);
674 if (dbg_curr_thread)
676 if (!SetThreadContext(dbg_curr_thread->handle, &dbg_context))
677 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid);
680 dbg_interactiveP = FALSE;
681 if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
682 dbg_printf("Cannot continue on %04lx (%08x)\n", dbg_curr_tid, cont);
685 static void wait_exception(void)
687 DEBUG_EVENT de;
689 while (dbg_num_processes() && WaitForDebugEvent(&de, INFINITE))
691 if (dbg_handle_debug_event(&de)) break;
693 dbg_interactiveP = TRUE;
696 void dbg_wait_next_exception(DWORD cont, int count, int mode)
698 ADDRESS64 addr;
699 char hexbuf[MAX_OFFSET_TO_STR_LEN];
701 if (cont == DBG_CONTINUE)
703 dbg_curr_thread->exec_count = count;
704 dbg_curr_thread->exec_mode = mode;
706 dbg_resume_debuggee(cont);
708 wait_exception();
709 if (!dbg_curr_process) return;
711 memory_get_current_pc(&addr);
712 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
713 memory_offset_to_string(hexbuf, addr.Offset, 0),
714 dbg_curr_thread->exec_mode,
715 dbg_curr_thread->exec_count);
718 void dbg_active_wait_for_first_exception(void)
720 dbg_interactiveP = FALSE;
721 /* wait for first exception */
722 wait_exception();
725 static unsigned dbg_start_debuggee(LPSTR cmdLine)
727 PROCESS_INFORMATION info;
728 STARTUPINFOA startup, current;
729 DWORD flags;
731 GetStartupInfoA(&current);
733 memset(&startup, 0, sizeof(startup));
734 startup.cb = sizeof(startup);
735 startup.dwFlags = STARTF_USESHOWWINDOW;
737 startup.wShowWindow = (current.dwFlags & STARTF_USESHOWWINDOW) ?
738 current.wShowWindow : SW_SHOWNORMAL;
740 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUI:s need it
741 * while GUI:s don't
743 flags = DEBUG_PROCESS | CREATE_NEW_CONSOLE;
744 if (!DBG_IVAR(AlsoDebugProcChild)) flags |= DEBUG_ONLY_THIS_PROCESS;
746 if (!CreateProcessA(NULL, cmdLine, NULL, NULL, FALSE, flags,
747 NULL, NULL, &startup, &info))
749 dbg_printf("Couldn't start process '%s'\n", cmdLine);
750 return FALSE;
752 if (!info.dwProcessId)
754 /* this happens when the program being run is not a Wine binary
755 * (for example, a shell wrapper around a WineLib app)
757 /* Current fix: list running processes and let the user attach
758 * to one of them (sic)
759 * FIXME: implement a real fix => grab the process (from the
760 * running processes) from its name
762 dbg_printf("Debuggee has been started (%s)\n"
763 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
764 "Try to attach to one of those processes:\n", cmdLine);
765 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
766 Sleep(100);
767 info_win32_processes();
768 return TRUE;
770 dbg_curr_pid = info.dwProcessId;
771 if (!(dbg_curr_process = dbg_add_process(&be_process_active_io, dbg_curr_pid, 0))) return FALSE;
772 dbg_curr_process->active_debuggee = TRUE;
774 return TRUE;
777 void dbg_run_debuggee(const char* args)
779 if (args)
781 WINE_FIXME("Re-running current program with %s as args is broken\n", args);
782 return;
784 else
786 if (!dbg_last_cmd_line)
788 dbg_printf("Cannot find previously used command line.\n");
789 return;
791 dbg_start_debuggee(dbg_last_cmd_line);
792 dbg_active_wait_for_first_exception();
793 source_list_from_addr(NULL, 0);
797 static BOOL str2int(const char* str, DWORD_PTR* val)
799 char* ptr;
801 *val = strtol(str, &ptr, 10);
802 return str < ptr && !*ptr;
806 /******************************************************************
807 * dbg_active_attach
809 * Tries to attach to a running process
810 * Handles the <pid> or <pid> <evt> forms
812 enum dbg_start dbg_active_attach(int argc, char* argv[])
814 DWORD_PTR pid, evt;
816 /* try the form <myself> pid */
817 if (argc == 1 && str2int(argv[0], &pid) && pid != 0)
819 if (!dbg_attach_debuggee(pid, FALSE))
820 return start_error_init;
822 /* try the form <myself> pid evt (Win32 JIT debugger) */
823 else if (argc == 2 && str2int(argv[0], &pid) && pid != 0 &&
824 str2int(argv[1], &evt) && evt != 0)
826 if (!dbg_attach_debuggee(pid, TRUE))
828 /* don't care about result */
829 SetEvent((HANDLE)evt);
830 return start_error_init;
832 if (!SetEvent((HANDLE)evt))
834 WINE_ERR("Invalid event handle: %lx\n", evt);
835 return start_error_init;
837 CloseHandle((HANDLE)evt);
839 else return start_error_parse;
841 dbg_curr_pid = pid;
842 return start_ok;
845 /******************************************************************
846 * dbg_active_launch
848 * Launches a debuggee (with its arguments) from argc/argv
850 enum dbg_start dbg_active_launch(int argc, char* argv[])
852 int i, len;
853 LPSTR cmd_line;
855 if (argc == 0) return start_error_parse;
857 if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, len = 1)))
859 oom_leave:
860 dbg_printf("Out of memory\n");
861 return start_error_init;
863 cmd_line[0] = '\0';
865 for (i = 0; i < argc; i++)
867 len += strlen(argv[i]) + 1;
868 if (!(cmd_line = HeapReAlloc(GetProcessHeap(), 0, cmd_line, len)))
869 goto oom_leave;
870 strcat(cmd_line, argv[i]);
871 cmd_line[len - 2] = ' ';
872 cmd_line[len - 1] = '\0';
875 if (!dbg_start_debuggee(cmd_line))
877 HeapFree(GetProcessHeap(), 0, cmd_line);
878 return start_error_init;
880 HeapFree(GetProcessHeap(), 0, dbg_last_cmd_line);
881 dbg_last_cmd_line = cmd_line;
882 return start_ok;
885 /******************************************************************
886 * dbg_active_auto
888 * Starts (<pid> or <pid> <evt>) in automatic mode
890 enum dbg_start dbg_active_auto(int argc, char* argv[])
892 HANDLE hFile;
893 enum dbg_start ds = start_error_parse;
895 if (!strcmp(argv[0], "--auto"))
897 /* auto mode */
898 argc--; argv++;
899 ds = dbg_active_attach(argc, argv);
900 if (ds != start_ok) {
901 msgbox_res_id(NULL, IDS_INVALID_PARAMS, IDS_AUTO_CAPTION, MB_OK);
902 return ds;
904 if (!display_crash_dialog()) {
905 dbg_init_console();
906 dbg_start_interactive(INVALID_HANDLE_VALUE);
907 return start_ok;
910 hFile = parser_generate_command_file("echo Modules:", "info share",
911 "echo Threads:", "info threads",
912 "backtrace", "detach", NULL);
914 else if (!strcmp(argv[0], "--minidump"))
916 const char* file = NULL;
917 char tmp[8 + 1 + MAX_PATH]; /* minidump <file> */
919 argc--; argv++;
920 /* hard stuff now ; we can get things like:
921 * --minidump <pid> 1 arg
922 * --minidump <pid> <evt> 2 args
923 * --minidump <file> <pid> 2 args
924 * --minidump <file> <pid> <evt> 3 args
926 switch (argc)
928 case 1:
929 ds = dbg_active_attach(argc, argv);
930 break;
931 case 2:
932 if ((ds = dbg_active_attach(argc, argv)) != start_ok)
934 file = argv[0];
935 ds = dbg_active_attach(argc - 1, argv + 1);
937 break;
938 case 3:
939 file = argv[0];
940 ds = dbg_active_attach(argc - 1, argv + 1);
941 break;
942 default:
943 return start_error_parse;
945 if (ds != start_ok) return ds;
946 memcpy(tmp, "minidump \"", 10);
947 if (!file)
949 char path[MAX_PATH];
951 GetTempPathA(sizeof(path), path);
952 GetTempFileNameA(path, "WD", 0, tmp + 10);
954 else strcpy(tmp + 10, file);
955 strcat(tmp, "\"");
956 if (!file)
958 /* FIXME: should generate unix name as well */
959 dbg_printf("Capturing program state in %s\n", tmp + 9);
961 hFile = parser_generate_command_file(tmp, "detach", NULL);
963 else return start_error_parse;
964 if (hFile == INVALID_HANDLE_VALUE) return start_error_parse;
966 if (dbg_curr_process->active_debuggee)
967 dbg_active_wait_for_first_exception();
969 dbg_interactiveP = TRUE;
970 parser_handle(hFile);
972 return start_ok;
975 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill)
977 if (pcs == dbg_curr_process)
979 /* remove all set breakpoints in debuggee code */
980 break_set_xpoints(FALSE);
981 /* needed for single stepping (ugly).
982 * should this be handled inside the server ???
984 be_cpu->single_step(&dbg_context, FALSE);
985 if (dbg_curr_thread->in_exception)
987 SetThreadContext(dbg_curr_thread->handle, &dbg_context);
988 ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, DBG_CONTINUE);
991 if (kill)
993 TerminateProcess(pcs->handle, 0);
995 else
997 if (!DebugActiveProcessStop(pcs->pid)) return FALSE;
999 SymCleanup(pcs->handle);
1000 dbg_del_process(pcs);
1002 return TRUE;
1005 static BOOL tgt_process_active_read(HANDLE hProcess, const void* addr,
1006 void* buffer, SIZE_T len, SIZE_T* rlen)
1008 return ReadProcessMemory( hProcess, addr, buffer, len, rlen );
1011 static BOOL tgt_process_active_write(HANDLE hProcess, void* addr,
1012 const void* buffer, SIZE_T len, SIZE_T* wlen)
1014 return WriteProcessMemory( hProcess, addr, buffer, len, wlen );
1017 static BOOL tgt_process_active_get_selector(HANDLE hThread, DWORD sel, LDT_ENTRY* le)
1019 return GetThreadSelectorEntry( hThread, sel, le );
1022 static struct be_process_io be_process_active_io =
1024 tgt_process_active_close_process,
1025 tgt_process_active_read,
1026 tgt_process_active_write,
1027 tgt_process_active_get_selector