server: Split the fallback to desktop async keystate.
[wine/zf.git] / programs / winedbg / tgt_active.c
blobe71f461573544111be1ecba59c7a9767e01eadf1
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)
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;
85 SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL);
87 dbg_curr_process->active_debuggee = TRUE;
88 return TRUE;
91 static unsigned dbg_fetch_context(void)
93 if (!dbg_curr_process->be_cpu->get_context(dbg_curr_thread->handle, &dbg_context))
95 WINE_WARN("Can't get thread's context\n");
96 return FALSE;
98 return TRUE;
101 BOOL dbg_set_curr_thread(DWORD tid)
103 struct dbg_thread* thread;
105 if (!dbg_curr_process)
107 dbg_printf("No process loaded\n");
108 return FALSE;
111 thread = dbg_get_thread(dbg_curr_process, tid);
112 if (thread)
114 dbg_curr_thread = thread;
115 dbg_fetch_context();
116 stack_fetch_frames(&dbg_context);
117 dbg_curr_tid = tid;
118 return TRUE;
120 dbg_printf("No such thread\n");
121 return thread != NULL;
124 /***********************************************************************
125 * dbg_exception_prolog
127 * Examine exception and decide if interactive mode is entered(return TRUE)
128 * or exception is silently continued(return FALSE)
129 * is_debug means the exception is a breakpoint or single step exception
131 static BOOL dbg_exception_prolog(BOOL is_debug, const EXCEPTION_RECORD* rec)
133 ADDRESS64 addr;
134 BOOL is_break;
136 memory_get_current_pc(&addr);
137 break_suspend_execution();
139 /* this will resynchronize builtin dbghelp's internal ELF module list */
140 SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0);
142 if (is_debug) break_adjust_pc(&addr, rec->ExceptionCode, dbg_curr_thread->first_chance, &is_break);
144 * Do a quiet backtrace so that we have an idea of what the situation
145 * is WRT the source files.
147 stack_fetch_frames(&dbg_context);
149 if (is_debug && !is_break && break_should_continue(&addr, rec->ExceptionCode))
150 return FALSE;
152 if (addr.Mode != dbg_curr_thread->addr_mode)
154 const char* name = NULL;
156 switch (addr.Mode)
158 case AddrMode1616: name = "16 bit"; break;
159 case AddrMode1632: name = "segmented 32 bit"; break;
160 case AddrModeReal: name = "vm86"; break;
161 case AddrModeFlat: name = dbg_curr_process->be_cpu->pointer_size == 4
162 ? "32 bit" : "64 bit"; break;
164 dbg_printf("In %s mode.\n", name);
165 dbg_curr_thread->addr_mode = addr.Mode;
167 display_print();
169 if (!is_debug)
171 /* This is a real crash, dump some info */
172 dbg_curr_process->be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
173 stack_info(-1);
174 dbg_curr_process->be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
175 stack_backtrace(dbg_curr_tid);
177 else
179 static char* last_name;
180 static char* last_file;
182 char buffer[sizeof(SYMBOL_INFO) + 256];
183 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
184 void* lin = memory_to_linear_addr(&addr);
185 DWORD64 disp64;
186 IMAGEHLP_LINE64 il;
187 DWORD disp;
189 si->SizeOfStruct = sizeof(*si);
190 si->MaxNameLen = 256;
191 il.SizeOfStruct = sizeof(il);
192 if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) &&
193 SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
195 if ((!last_name || strcmp(last_name, si->Name)) ||
196 (!last_file || strcmp(last_file, il.FileName)))
198 HeapFree(GetProcessHeap(), 0, last_name);
199 HeapFree(GetProcessHeap(), 0, last_file);
200 last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name);
201 last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName);
202 dbg_printf("%s () at %s:%u\n", last_name, last_file, il.LineNumber);
206 if (!is_debug || is_break ||
207 dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
208 dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
210 ADDRESS64 tmp = addr;
211 /* Show where we crashed */
212 memory_disasm_one_insn(&tmp);
214 source_list_from_addr(&addr, 0);
216 return TRUE;
219 static void dbg_exception_epilog(void)
221 break_restart_execution(dbg_curr_thread->exec_count);
223 * This will have gotten absorbed into the breakpoint info
224 * if it was used. Otherwise it would have been ignored.
225 * In any case, we don't mess with it any more.
227 if (dbg_curr_thread->exec_mode == dbg_exec_cont)
228 dbg_curr_thread->exec_count = 0;
229 dbg_curr_thread->in_exception = FALSE;
232 static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
234 BOOL is_debug = FALSE;
235 const THREADNAME_INFO* pThreadName;
236 struct dbg_thread* pThread;
238 assert(dbg_curr_thread);
240 WINE_TRACE("exception=%x first_chance=%c\n",
241 rec->ExceptionCode, first_chance ? 'Y' : 'N');
243 switch (rec->ExceptionCode)
245 case EXCEPTION_BREAKPOINT:
246 case EXCEPTION_SINGLE_STEP:
247 is_debug = TRUE;
248 break;
249 case EXCEPTION_WINE_NAME_THREAD:
250 pThreadName = (const THREADNAME_INFO*)(rec->ExceptionInformation);
251 if (pThreadName->dwThreadID == -1)
252 pThread = dbg_curr_thread;
253 else
254 pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
255 if(!pThread)
257 dbg_printf("Thread ID=%04x not in our list of threads -> can't rename\n", pThreadName->dwThreadID);
258 return DBG_CONTINUE;
260 if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
261 dbg_printf("Thread ID=%04x renamed using MS VC6 extension (name==\"%.9s\")\n",
262 pThread->tid, pThread->name);
263 return DBG_CONTINUE;
264 case EXCEPTION_INVALID_HANDLE:
265 return DBG_CONTINUE;
268 if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance) &&
269 !(rec->ExceptionFlags & EH_STACK_INVALID))
271 /* pass exception to program except for debug exceptions */
272 return DBG_EXCEPTION_NOT_HANDLED;
275 dbg_curr_thread->excpt_record = *rec;
276 dbg_curr_thread->in_exception = TRUE;
277 dbg_curr_thread->first_chance = first_chance;
279 if (!is_debug) info_win32_exception();
281 if (rec->ExceptionCode == STATUS_POSSIBLE_DEADLOCK && !DBG_IVAR(BreakOnCritSectTimeOut))
283 dbg_curr_thread->in_exception = FALSE;
284 return DBG_EXCEPTION_NOT_HANDLED;
287 if (dbg_exception_prolog(is_debug, rec))
289 dbg_interactiveP = TRUE;
290 return 0;
292 dbg_exception_epilog();
294 return DBG_CONTINUE;
297 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill);
299 static void fetch_module_name(void* name_addr, BOOL unicode, void* mod_addr,
300 WCHAR* buffer, size_t bufsz, BOOL is_pcs)
302 static const WCHAR pcspid[] = {'P','r','o','c','e','s','s','_','%','0','8','x',0};
303 static const WCHAR dlladdr[] = {'D','L','L','_','%','0','8','l','x',0};
305 memory_get_string_indirect(dbg_curr_process, name_addr, unicode, buffer, bufsz);
306 if (!buffer[0] &&
307 !GetModuleFileNameExW(dbg_curr_process->handle, mod_addr, buffer, bufsz))
309 if (is_pcs)
311 HMODULE h;
312 WORD (WINAPI *gpif)(HANDLE, LPWSTR, DWORD);
314 /* On Windows, when we get the process creation debug event for a process
315 * created by winedbg, the modules' list is not initialized yet. Hence,
316 * GetModuleFileNameExA (on the main module) will generate an error.
317 * Psapi (starting on XP) provides GetProcessImageFileName() which should
318 * give us the expected result
320 if (!(h = GetModuleHandleA("psapi")) ||
321 !(gpif = (void*)GetProcAddress(h, "GetProcessImageFileNameW")) ||
322 !(gpif)(dbg_curr_process->handle, buffer, bufsz))
323 snprintfW(buffer, bufsz, pcspid, dbg_curr_pid);
325 else
326 snprintfW(buffer, bufsz, dlladdr, (ULONG_PTR)mod_addr);
330 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
332 union {
333 char bufferA[256];
334 WCHAR buffer[256];
335 } u;
336 DWORD cont = DBG_CONTINUE;
338 dbg_curr_pid = de->dwProcessId;
339 dbg_curr_tid = de->dwThreadId;
341 if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
342 dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
343 else
344 dbg_curr_thread = NULL;
346 switch (de->dwDebugEventCode)
348 case EXCEPTION_DEBUG_EVENT:
349 if (!dbg_curr_thread)
351 WINE_ERR("%04x:%04x: not a registered process or thread (perhaps a 16 bit one ?)\n",
352 de->dwProcessId, de->dwThreadId);
353 break;
356 WINE_TRACE("%04x:%04x: exception code=%08x\n",
357 de->dwProcessId, de->dwThreadId,
358 de->u.Exception.ExceptionRecord.ExceptionCode);
360 if (dbg_curr_process->event_on_first_exception)
362 SetEvent(dbg_curr_process->event_on_first_exception);
363 CloseHandle(dbg_curr_process->event_on_first_exception);
364 dbg_curr_process->event_on_first_exception = NULL;
365 if (!DBG_IVAR(BreakOnAttach)) break;
367 if (dbg_fetch_context())
369 cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
370 de->u.Exception.dwFirstChance);
371 if (cont && dbg_curr_thread)
373 dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context);
376 break;
378 case CREATE_PROCESS_DEBUG_EVENT:
379 dbg_curr_process = dbg_add_process(&be_process_active_io, de->dwProcessId,
380 de->u.CreateProcessInfo.hProcess);
381 if (dbg_curr_process == NULL)
383 WINE_ERR("Couldn't create process\n");
384 break;
386 fetch_module_name(de->u.CreateProcessInfo.lpImageName,
387 de->u.CreateProcessInfo.fUnicode,
388 de->u.CreateProcessInfo.lpBaseOfImage,
389 u.buffer, ARRAY_SIZE(u.buffer), TRUE);
391 WINE_TRACE("%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
392 de->dwProcessId, de->dwThreadId,
393 wine_dbgstr_w(u.buffer),
394 de->u.CreateProcessInfo.lpImageName,
395 de->u.CreateProcessInfo.lpStartAddress,
396 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
397 de->u.CreateProcessInfo.nDebugInfoSize);
398 dbg_set_process_name(dbg_curr_process, u.buffer);
400 if (!dbg_init(dbg_curr_process->handle, u.buffer, FALSE))
401 dbg_printf("Couldn't initiate DbgHelp\n");
402 if (!dbg_load_module(dbg_curr_process->handle, de->u.CreateProcessInfo.hFile, u.buffer,
403 (DWORD_PTR)de->u.CreateProcessInfo.lpBaseOfImage, 0))
404 dbg_printf("couldn't load main module (%u)\n", GetLastError());
406 WINE_TRACE("%04x:%04x: create thread I @%p\n",
407 de->dwProcessId, de->dwThreadId, de->u.CreateProcessInfo.lpStartAddress);
409 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
410 de->dwThreadId,
411 de->u.CreateProcessInfo.hThread,
412 de->u.CreateProcessInfo.lpThreadLocalBase);
413 if (!dbg_curr_thread)
415 WINE_ERR("Couldn't create thread\n");
416 break;
418 dbg_init_current_process();
419 dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
420 break;
422 case EXIT_PROCESS_DEBUG_EVENT:
423 WINE_TRACE("%04x:%04x: exit process (%d)\n",
424 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
426 if (dbg_curr_process == NULL)
428 WINE_ERR("Unknown process\n");
429 break;
431 tgt_process_active_close_process(dbg_curr_process, FALSE);
432 dbg_printf("Process of pid=%04x has terminated\n", de->dwProcessId);
433 break;
435 case CREATE_THREAD_DEBUG_EVENT:
436 WINE_TRACE("%04x:%04x: create thread D @%p\n",
437 de->dwProcessId, de->dwThreadId, de->u.CreateThread.lpStartAddress);
439 if (dbg_curr_process == NULL)
441 WINE_ERR("Unknown process\n");
442 break;
444 if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
446 WINE_TRACE("Thread already listed, skipping\n");
447 break;
450 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
451 de->dwThreadId,
452 de->u.CreateThread.hThread,
453 de->u.CreateThread.lpThreadLocalBase);
454 if (!dbg_curr_thread)
456 WINE_ERR("Couldn't create thread\n");
457 break;
459 dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
460 break;
462 case EXIT_THREAD_DEBUG_EVENT:
463 WINE_TRACE("%04x:%04x: exit thread (%d)\n",
464 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
466 if (dbg_curr_thread == NULL)
468 WINE_ERR("Unknown thread\n");
469 break;
471 /* FIXME: remove break point set on thread startup */
472 dbg_del_thread(dbg_curr_thread);
473 break;
475 case LOAD_DLL_DEBUG_EVENT:
476 if (dbg_curr_thread == NULL)
478 WINE_ERR("Unknown thread\n");
479 break;
481 fetch_module_name(de->u.LoadDll.lpImageName,
482 de->u.LoadDll.fUnicode,
483 de->u.LoadDll.lpBaseOfDll,
484 u.buffer, ARRAY_SIZE(u.buffer), FALSE);
486 WINE_TRACE("%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
487 de->dwProcessId, de->dwThreadId,
488 wine_dbgstr_w(u.buffer), de->u.LoadDll.lpBaseOfDll,
489 de->u.LoadDll.dwDebugInfoFileOffset,
490 de->u.LoadDll.nDebugInfoSize);
491 dbg_load_module(dbg_curr_process->handle, de->u.LoadDll.hFile, u.buffer,
492 (DWORD_PTR)de->u.LoadDll.lpBaseOfDll, 0);
493 break_set_xpoints(FALSE);
494 break_check_delayed_bp();
495 break_set_xpoints(TRUE);
496 if (DBG_IVAR(BreakOnDllLoad))
498 dbg_printf("Stopping on DLL %s loading at %p\n",
499 dbg_W2A(u.buffer, -1), de->u.LoadDll.lpBaseOfDll);
500 if (dbg_fetch_context()) cont = 0;
502 break;
504 case UNLOAD_DLL_DEBUG_EVENT:
505 WINE_TRACE("%04x:%04x: unload DLL @%p\n",
506 de->dwProcessId, de->dwThreadId,
507 de->u.UnloadDll.lpBaseOfDll);
508 break_delete_xpoints_from_module((DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
509 SymUnloadModule64(dbg_curr_process->handle, (DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
510 break;
512 case OUTPUT_DEBUG_STRING_EVENT:
513 if (dbg_curr_thread == NULL)
515 WINE_ERR("Unknown thread\n");
516 break;
519 memory_get_string(dbg_curr_process,
520 de->u.DebugString.lpDebugStringData, TRUE,
521 de->u.DebugString.fUnicode, u.bufferA, sizeof(u.bufferA));
522 WINE_TRACE("%04x:%04x: output debug string (%s)\n",
523 de->dwProcessId, de->dwThreadId, u.bufferA);
524 break;
526 case RIP_EVENT:
527 WINE_TRACE("%04x:%04x: rip error=%u type=%u\n",
528 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
529 de->u.RipInfo.dwType);
530 break;
532 default:
533 WINE_TRACE("%04x:%04x: unknown event (%x)\n",
534 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
536 if (!cont) return TRUE; /* stop execution */
537 ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
538 return FALSE; /* continue execution */
541 static void dbg_resume_debuggee(DWORD cont)
543 if (dbg_curr_thread->in_exception)
545 ADDRESS64 addr;
546 char hexbuf[MAX_OFFSET_TO_STR_LEN];
548 dbg_exception_epilog();
549 memory_get_current_pc(&addr);
550 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
551 memory_offset_to_string(hexbuf, addr.Offset, 0),
552 dbg_curr_thread->exec_mode,
553 dbg_curr_thread->exec_count);
554 if (dbg_curr_thread)
556 if (!dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context))
557 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid);
560 dbg_interactiveP = FALSE;
561 if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
562 dbg_printf("Cannot continue on %04lx (%08x)\n", dbg_curr_tid, cont);
565 static void wait_exception(void)
567 DEBUG_EVENT de;
569 while (dbg_num_processes() && WaitForDebugEvent(&de, INFINITE))
571 if (dbg_handle_debug_event(&de)) break;
573 dbg_interactiveP = TRUE;
576 void dbg_wait_next_exception(DWORD cont, int count, int mode)
578 ADDRESS64 addr;
579 char hexbuf[MAX_OFFSET_TO_STR_LEN];
581 if (cont == DBG_CONTINUE)
583 dbg_curr_thread->exec_count = count;
584 dbg_curr_thread->exec_mode = mode;
586 dbg_resume_debuggee(cont);
588 wait_exception();
589 if (!dbg_curr_process) return;
591 memory_get_current_pc(&addr);
592 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
593 memory_offset_to_string(hexbuf, addr.Offset, 0),
594 dbg_curr_thread->exec_mode,
595 dbg_curr_thread->exec_count);
598 void dbg_active_wait_for_first_exception(void)
600 dbg_interactiveP = FALSE;
601 /* wait for first exception */
602 wait_exception();
605 static BOOL dbg_start_debuggee(LPSTR cmdLine)
607 PROCESS_INFORMATION info;
608 STARTUPINFOA startup, current;
609 DWORD flags;
611 GetStartupInfoA(&current);
613 memset(&startup, 0, sizeof(startup));
614 startup.cb = sizeof(startup);
615 startup.dwFlags = STARTF_USESHOWWINDOW;
617 startup.wShowWindow = (current.dwFlags & STARTF_USESHOWWINDOW) ?
618 current.wShowWindow : SW_SHOWNORMAL;
620 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUIs need it
621 * while GUIs don't
623 flags = DEBUG_PROCESS | CREATE_NEW_CONSOLE;
624 if (!DBG_IVAR(AlsoDebugProcChild)) flags |= DEBUG_ONLY_THIS_PROCESS;
626 if (!CreateProcessA(NULL, cmdLine, NULL, NULL, FALSE, flags,
627 NULL, NULL, &startup, &info))
629 dbg_printf("Couldn't start process '%s'\n", cmdLine);
630 return FALSE;
632 if (!info.dwProcessId)
634 /* this happens when the program being run is not a Wine binary
635 * (for example, a shell wrapper around a WineLib app)
637 /* Current fix: list running processes and let the user attach
638 * to one of them (sic)
639 * FIXME: implement a real fix => grab the process (from the
640 * running processes) from its name
642 dbg_printf("Debuggee has been started (%s)\n"
643 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
644 "Try to attach to one of those processes:\n", cmdLine);
645 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
646 Sleep(100);
647 info_win32_processes();
648 return TRUE;
650 dbg_curr_pid = info.dwProcessId;
651 if (!(dbg_curr_process = dbg_add_process(&be_process_active_io, dbg_curr_pid, 0))) return FALSE;
652 dbg_curr_process->active_debuggee = TRUE;
654 return TRUE;
657 void dbg_run_debuggee(const char* args)
659 if (args)
661 WINE_FIXME("Re-running current program with %s as args is broken\n", wine_dbgstr_a(args));
662 return;
664 else
666 if (!dbg_last_cmd_line)
668 dbg_printf("Cannot find previously used command line.\n");
669 return;
671 dbg_start_debuggee(dbg_last_cmd_line);
672 dbg_active_wait_for_first_exception();
673 source_list_from_addr(NULL, 0);
677 static BOOL str2int(const char* str, DWORD_PTR* val)
679 char* ptr;
681 *val = strtol(str, &ptr, 10);
682 return str < ptr && !*ptr;
685 static HANDLE create_temp_file(void)
687 static const WCHAR prefixW[] = {'w','d','b',0};
688 WCHAR path[MAX_PATH], name[MAX_PATH];
690 if (!GetTempPathW( MAX_PATH, path ) || !GetTempFileNameW( path, prefixW, 0, name ))
691 return INVALID_HANDLE_VALUE;
692 return CreateFileW( name, GENERIC_READ|GENERIC_WRITE|DELETE, FILE_SHARE_DELETE,
693 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0 );
696 static const struct
698 int type;
699 int platform;
700 int major;
701 int minor;
702 const char *str;
704 version_table[] =
706 { 0, VER_PLATFORM_WIN32s, 2, 0, "2.0" },
707 { 0, VER_PLATFORM_WIN32s, 3, 0, "3.0" },
708 { 0, VER_PLATFORM_WIN32s, 3, 10, "3.1" },
709 { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 0, "95" },
710 { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 10, "98" },
711 { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 90, "ME" },
712 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 3, 51, "NT 3.51" },
713 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 4, 0, "NT 4.0" },
714 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 0, "2000" },
715 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 1, "XP" },
716 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 2, "XP" },
717 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 5, 2, "Server 2003" },
718 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 0, "Vista" },
719 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 0, "Server 2008" },
720 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 1, "7" },
721 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 1, "Server 2008 R2" },
722 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 2, "8" },
723 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 2, "Server 2012" },
724 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 3, "8.1" },
725 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 3, "Server 2012 R2" },
726 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 10, 0, "10" },
729 static const char *get_windows_version(void)
731 RTL_OSVERSIONINFOEXW info = { sizeof(RTL_OSVERSIONINFOEXW) };
732 static char str[64];
733 int i;
735 RtlGetVersion( &info );
737 for (i = 0; i < ARRAY_SIZE(version_table); i++)
739 if (version_table[i].type == info.wProductType &&
740 version_table[i].platform == info.dwPlatformId &&
741 version_table[i].major == info.dwMajorVersion &&
742 version_table[i].minor == info.dwMinorVersion)
744 return version_table[i].str;
748 snprintf( str, sizeof(str), "%d.%d (%d)", info.dwMajorVersion,
749 info.dwMinorVersion, info.wProductType );
750 return str;
753 static void output_system_info(void)
755 #ifdef __i386__
756 static const char platform[] = "i386";
757 #elif defined(__x86_64__)
758 static const char platform[] = "x86_64";
759 #elif defined(__arm__)
760 static const char platform[] = "arm";
761 #elif defined(__aarch64__)
762 static const char platform[] = "arm64";
763 #else
764 # error CPU unknown
765 #endif
767 const char *(CDECL *wine_get_build_id)(void);
768 void (CDECL *wine_get_host_version)( const char **sysname, const char **release );
769 BOOL is_wow64;
771 wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
772 wine_get_host_version = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
773 if (!IsWow64Process( dbg_curr_process->handle, &is_wow64 )) is_wow64 = FALSE;
775 dbg_printf( "System information:\n" );
776 if (wine_get_build_id) dbg_printf( " Wine build: %s\n", wine_get_build_id() );
777 dbg_printf( " Platform: %s%s\n", platform, is_wow64 ? " (WOW64)" : "" );
778 dbg_printf( " Version: Windows %s\n", get_windows_version() );
779 if (wine_get_host_version)
781 const char *sysname, *release;
782 wine_get_host_version( &sysname, &release );
783 dbg_printf( " Host system: %s\n", sysname );
784 dbg_printf( " Host version: %s\n", release );
788 /******************************************************************
789 * dbg_active_attach
791 * Tries to attach to a running process
792 * Handles the <pid> or <pid> <evt> forms
794 enum dbg_start dbg_active_attach(int argc, char* argv[])
796 DWORD_PTR pid, evt;
798 /* try the form <myself> pid */
799 if (argc == 1 && str2int(argv[0], &pid) && pid != 0)
801 if (!dbg_attach_debuggee(pid))
802 return start_error_init;
804 /* try the form <myself> pid evt (Win32 JIT debugger) */
805 else if (argc == 2 && str2int(argv[0], &pid) && pid != 0 &&
806 str2int(argv[1], &evt) && evt != 0)
808 if (!dbg_attach_debuggee(pid))
810 /* don't care about result */
811 SetEvent((HANDLE)evt);
812 return start_error_init;
814 dbg_curr_process->event_on_first_exception = (HANDLE)evt;
816 else return start_error_parse;
818 dbg_curr_pid = pid;
819 return start_ok;
822 /******************************************************************
823 * dbg_active_launch
825 * Launches a debuggee (with its arguments) from argc/argv
827 enum dbg_start dbg_active_launch(int argc, char* argv[])
829 int i, len;
830 LPSTR cmd_line;
832 if (argc == 0) return start_error_parse;
834 if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, len = 1)))
836 oom_leave:
837 dbg_printf("Out of memory\n");
838 return start_error_init;
840 cmd_line[0] = '\0';
842 for (i = 0; i < argc; i++)
844 len += strlen(argv[i]) + 1;
845 if (!(cmd_line = HeapReAlloc(GetProcessHeap(), 0, cmd_line, len)))
846 goto oom_leave;
847 strcat(cmd_line, argv[i]);
848 cmd_line[len - 2] = ' ';
849 cmd_line[len - 1] = '\0';
852 if (!dbg_start_debuggee(cmd_line))
854 HeapFree(GetProcessHeap(), 0, cmd_line);
855 return start_error_init;
857 HeapFree(GetProcessHeap(), 0, dbg_last_cmd_line);
858 dbg_last_cmd_line = cmd_line;
859 return start_ok;
862 /******************************************************************
863 * dbg_active_auto
865 * Starts (<pid> or <pid> <evt>) in automatic mode
867 enum dbg_start dbg_active_auto(int argc, char* argv[])
869 HANDLE thread = 0, event = 0, input, output = INVALID_HANDLE_VALUE;
870 enum dbg_start ds = start_error_parse;
872 DBG_IVAR(BreakOnDllLoad) = 0;
874 /* auto mode */
875 argc--; argv++;
876 ds = dbg_active_attach(argc, argv);
877 if (ds != start_ok) {
878 msgbox_res_id(NULL, IDS_INVALID_PARAMS, IDS_AUTO_CAPTION, MB_OK);
879 return ds;
882 switch (display_crash_dialog())
884 case ID_DEBUG:
885 AllocConsole();
886 dbg_init_console();
887 dbg_start_interactive(INVALID_HANDLE_VALUE);
888 return start_ok;
889 case ID_DETAILS:
890 event = CreateEventW( NULL, TRUE, FALSE, NULL );
891 if (event) thread = display_crash_details( event );
892 if (thread) dbg_houtput = output = create_temp_file();
893 break;
896 input = parser_generate_command_file("echo Modules:", "info share",
897 "echo Threads:", "info threads", NULL);
898 if (input == INVALID_HANDLE_VALUE) return start_error_parse;
900 if (dbg_curr_process->active_debuggee)
901 dbg_active_wait_for_first_exception();
903 dbg_interactiveP = TRUE;
904 parser_handle(input);
905 output_system_info();
907 if (output != INVALID_HANDLE_VALUE)
909 SetEvent( event );
910 WaitForSingleObject( thread, INFINITE );
911 CloseHandle( output );
912 CloseHandle( thread );
913 CloseHandle( event );
916 CloseHandle( input );
917 dbg_curr_process->process_io->close_process(dbg_curr_process, TRUE);
918 return start_ok;
921 /******************************************************************
922 * dbg_active_minidump
924 * Starts (<pid> or <pid> <evt>) in minidump mode
926 enum dbg_start dbg_active_minidump(int argc, char* argv[])
928 HANDLE hFile;
929 enum dbg_start ds = start_error_parse;
930 const char* file = NULL;
931 char tmp[8 + 1 + 2 + MAX_PATH]; /* minidump "<file>" */
933 dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
934 DBG_IVAR(BreakOnDllLoad) = 0;
936 argc--; argv++;
937 /* hard stuff now ; we can get things like:
938 * --minidump <pid> 1 arg
939 * --minidump <pid> <evt> 2 args
940 * --minidump <file> <pid> 2 args
941 * --minidump <file> <pid> <evt> 3 args
943 switch (argc)
945 case 1:
946 ds = dbg_active_attach(argc, argv);
947 break;
948 case 2:
949 if ((ds = dbg_active_attach(argc, argv)) != start_ok)
951 file = argv[0];
952 ds = dbg_active_attach(argc - 1, argv + 1);
954 break;
955 case 3:
956 file = argv[0];
957 ds = dbg_active_attach(argc - 1, argv + 1);
958 break;
959 default:
960 return start_error_parse;
962 if (ds != start_ok) return ds;
963 memcpy(tmp, "minidump \"", 10);
964 if (!file)
966 char path[MAX_PATH];
968 GetTempPathA(sizeof(path), path);
969 GetTempFileNameA(path, "WD", 0, tmp + 10);
971 else strcpy(tmp + 10, file);
972 strcat(tmp, "\"");
973 if (!file)
975 /* FIXME: should generate unix name as well */
976 dbg_printf("Capturing program state in %s\n", tmp + 9);
978 hFile = parser_generate_command_file(tmp, "detach", NULL);
979 if (hFile == INVALID_HANDLE_VALUE) return start_error_parse;
981 if (dbg_curr_process->active_debuggee)
982 dbg_active_wait_for_first_exception();
984 dbg_interactiveP = TRUE;
985 parser_handle(hFile);
987 return start_ok;
990 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill)
992 if (kill)
994 DWORD exit_code = 0;
996 if (pcs == dbg_curr_process && dbg_curr_thread->in_exception)
997 exit_code = dbg_curr_thread->excpt_record.ExceptionCode;
999 TerminateProcess(pcs->handle, exit_code);
1001 else if (pcs == dbg_curr_process)
1003 /* remove all set breakpoints in debuggee code */
1004 break_set_xpoints(FALSE);
1005 /* needed for single stepping (ugly).
1006 * should this be handled inside the server ???
1008 dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
1009 if (dbg_curr_thread->in_exception)
1011 dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context);
1012 ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, DBG_CONTINUE);
1016 if (!kill)
1018 if (!DebugActiveProcessStop(pcs->pid)) return FALSE;
1020 SymCleanup(pcs->handle);
1021 dbg_del_process(pcs);
1023 return TRUE;
1026 static BOOL tgt_process_active_read(HANDLE hProcess, const void* addr,
1027 void* buffer, SIZE_T len, SIZE_T* rlen)
1029 return ReadProcessMemory( hProcess, addr, buffer, len, rlen );
1032 static BOOL tgt_process_active_write(HANDLE hProcess, void* addr,
1033 const void* buffer, SIZE_T len, SIZE_T* wlen)
1035 return WriteProcessMemory( hProcess, addr, buffer, len, wlen );
1038 static BOOL tgt_process_active_get_selector(HANDLE hThread, DWORD sel, LDT_ENTRY* le)
1040 return GetThreadSelectorEntry( hThread, sel, le );
1043 static struct be_process_io be_process_active_io =
1045 tgt_process_active_close_process,
1046 tgt_process_active_read,
1047 tgt_process_active_write,
1048 tgt_process_active_get_selector