wined3d: Move texture2d_load_drawable() to texture.c.
[wine.git] / programs / winedbg / tgt_active.c
blob5385d0effa002a4bab90ad1f456c183c5556b15c
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 if (!dbg_curr_process->be_cpu->get_context(dbg_curr_thread->handle, &dbg_context))
96 WINE_WARN("Can't get thread's context\n");
97 return FALSE;
99 return TRUE;
102 /***********************************************************************
103 * dbg_exception_prolog
105 * Examine exception and decide if interactive mode is entered(return TRUE)
106 * or exception is silently continued(return FALSE)
107 * is_debug means the exception is a breakpoint or single step exception
109 static BOOL dbg_exception_prolog(BOOL is_debug, const EXCEPTION_RECORD* rec)
111 ADDRESS64 addr;
112 BOOL is_break;
114 memory_get_current_pc(&addr);
115 break_suspend_execution();
117 /* this will resynchronize builtin dbghelp's internal ELF module list */
118 SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0);
120 if (is_debug) break_adjust_pc(&addr, rec->ExceptionCode, dbg_curr_thread->first_chance, &is_break);
122 * Do a quiet backtrace so that we have an idea of what the situation
123 * is WRT the source files.
125 stack_fetch_frames(&dbg_context);
127 if (is_debug && !is_break && break_should_continue(&addr, rec->ExceptionCode))
128 return FALSE;
130 if (addr.Mode != dbg_curr_thread->addr_mode)
132 const char* name = NULL;
134 switch (addr.Mode)
136 case AddrMode1616: name = "16 bit"; break;
137 case AddrMode1632: name = "segmented 32 bit"; break;
138 case AddrModeReal: name = "vm86"; break;
139 case AddrModeFlat: name = dbg_curr_process->be_cpu->pointer_size == 4
140 ? "32 bit" : "64 bit"; break;
142 dbg_printf("In %s mode.\n", name);
143 dbg_curr_thread->addr_mode = addr.Mode;
145 display_print();
147 if (!is_debug)
149 /* This is a real crash, dump some info */
150 dbg_curr_process->be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
151 stack_info(-1);
152 dbg_curr_process->be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
153 stack_backtrace(dbg_curr_tid);
155 else
157 static char* last_name;
158 static char* last_file;
160 char buffer[sizeof(SYMBOL_INFO) + 256];
161 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
162 void* lin = memory_to_linear_addr(&addr);
163 DWORD64 disp64;
164 IMAGEHLP_LINE64 il;
165 DWORD disp;
167 si->SizeOfStruct = sizeof(*si);
168 si->MaxNameLen = 256;
169 il.SizeOfStruct = sizeof(il);
170 if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) &&
171 SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
173 if ((!last_name || strcmp(last_name, si->Name)) ||
174 (!last_file || strcmp(last_file, il.FileName)))
176 HeapFree(GetProcessHeap(), 0, last_name);
177 HeapFree(GetProcessHeap(), 0, last_file);
178 last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name);
179 last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName);
180 dbg_printf("%s () at %s:%u\n", last_name, last_file, il.LineNumber);
184 if (!is_debug || is_break ||
185 dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
186 dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
188 ADDRESS64 tmp = addr;
189 /* Show where we crashed */
190 memory_disasm_one_insn(&tmp);
192 source_list_from_addr(&addr, 0);
194 return TRUE;
197 static void dbg_exception_epilog(void)
199 break_restart_execution(dbg_curr_thread->exec_count);
201 * This will have gotten absorbed into the breakpoint info
202 * if it was used. Otherwise it would have been ignored.
203 * In any case, we don't mess with it any more.
205 if (dbg_curr_thread->exec_mode == dbg_exec_cont)
206 dbg_curr_thread->exec_count = 0;
207 dbg_curr_thread->in_exception = FALSE;
210 static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
212 BOOL is_debug = FALSE;
213 const THREADNAME_INFO* pThreadName;
214 struct dbg_thread* pThread;
216 assert(dbg_curr_thread);
218 WINE_TRACE("exception=%x first_chance=%c\n",
219 rec->ExceptionCode, first_chance ? 'Y' : 'N');
221 switch (rec->ExceptionCode)
223 case EXCEPTION_BREAKPOINT:
224 case EXCEPTION_SINGLE_STEP:
225 is_debug = TRUE;
226 break;
227 case EXCEPTION_NAME_THREAD:
228 pThreadName = (const THREADNAME_INFO*)(rec->ExceptionInformation);
229 if (pThreadName->dwThreadID == -1)
230 pThread = dbg_curr_thread;
231 else
232 pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
233 if(!pThread)
235 dbg_printf("Thread ID=%04x not in our list of threads -> can't rename\n", pThreadName->dwThreadID);
236 return DBG_CONTINUE;
238 if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
239 dbg_printf("Thread ID=%04x renamed using MS VC6 extension (name==\"%.9s\")\n",
240 pThread->tid, pThread->name);
241 return DBG_CONTINUE;
242 case EXCEPTION_INVALID_HANDLE:
243 return DBG_CONTINUE;
246 if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance) &&
247 !(rec->ExceptionFlags & EH_STACK_INVALID))
249 /* pass exception to program except for debug exceptions */
250 return DBG_EXCEPTION_NOT_HANDLED;
253 dbg_curr_thread->excpt_record = *rec;
254 dbg_curr_thread->in_exception = TRUE;
255 dbg_curr_thread->first_chance = first_chance;
257 if (!is_debug) info_win32_exception();
259 if (rec->ExceptionCode == STATUS_POSSIBLE_DEADLOCK && !DBG_IVAR(BreakOnCritSectTimeOut))
261 dbg_curr_thread->in_exception = FALSE;
262 return DBG_EXCEPTION_NOT_HANDLED;
265 if (dbg_exception_prolog(is_debug, rec))
267 dbg_interactiveP = TRUE;
268 return 0;
270 dbg_exception_epilog();
272 return DBG_CONTINUE;
275 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill);
277 static void fetch_module_name(void* name_addr, BOOL unicode, void* mod_addr,
278 WCHAR* buffer, size_t bufsz, BOOL is_pcs)
280 static const WCHAR pcspid[] = {'P','r','o','c','e','s','s','_','%','0','8','x',0};
281 static const WCHAR dlladdr[] = {'D','L','L','_','%','0','8','l','x',0};
283 memory_get_string_indirect(dbg_curr_process, name_addr, unicode, buffer, bufsz);
284 if (!buffer[0] &&
285 !GetModuleFileNameExW(dbg_curr_process->handle, mod_addr, buffer, bufsz))
287 if (is_pcs)
289 HMODULE h;
290 WORD (WINAPI *gpif)(HANDLE, LPWSTR, DWORD);
292 /* On Windows, when we get the process creation debug event for a process
293 * created by winedbg, the modules' list is not initialized yet. Hence,
294 * GetModuleFileNameExA (on the main module) will generate an error.
295 * Psapi (starting on XP) provides GetProcessImageFileName() which should
296 * give us the expected result
298 if (!(h = GetModuleHandleA("psapi")) ||
299 !(gpif = (void*)GetProcAddress(h, "GetProcessImageFileNameW")) ||
300 !(gpif)(dbg_curr_process->handle, buffer, bufsz))
301 snprintfW(buffer, bufsz, pcspid, dbg_curr_pid);
303 else
304 snprintfW(buffer, bufsz, dlladdr, (ULONG_PTR)mod_addr);
308 static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
310 union {
311 char bufferA[256];
312 WCHAR buffer[256];
313 } u;
314 DWORD cont = DBG_CONTINUE;
316 dbg_curr_pid = de->dwProcessId;
317 dbg_curr_tid = de->dwThreadId;
319 if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
320 dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
321 else
322 dbg_curr_thread = NULL;
324 switch (de->dwDebugEventCode)
326 case EXCEPTION_DEBUG_EVENT:
327 if (!dbg_curr_thread)
329 WINE_ERR("%04x:%04x: not a registered process or thread (perhaps a 16 bit one ?)\n",
330 de->dwProcessId, de->dwThreadId);
331 break;
334 WINE_TRACE("%04x:%04x: exception code=%08x\n",
335 de->dwProcessId, de->dwThreadId,
336 de->u.Exception.ExceptionRecord.ExceptionCode);
338 if (dbg_curr_process->continue_on_first_exception)
340 dbg_curr_process->continue_on_first_exception = FALSE;
341 if (!DBG_IVAR(BreakOnAttach)) break;
343 if (dbg_fetch_context())
345 cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
346 de->u.Exception.dwFirstChance);
347 if (cont && dbg_curr_thread)
349 dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context);
352 break;
354 case CREATE_PROCESS_DEBUG_EVENT:
355 dbg_curr_process = dbg_add_process(&be_process_active_io, de->dwProcessId,
356 de->u.CreateProcessInfo.hProcess);
357 if (dbg_curr_process == NULL)
359 WINE_ERR("Couldn't create process\n");
360 break;
362 fetch_module_name(de->u.CreateProcessInfo.lpImageName,
363 de->u.CreateProcessInfo.fUnicode,
364 de->u.CreateProcessInfo.lpBaseOfImage,
365 u.buffer, ARRAY_SIZE(u.buffer), TRUE);
367 WINE_TRACE("%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
368 de->dwProcessId, de->dwThreadId,
369 wine_dbgstr_w(u.buffer),
370 de->u.CreateProcessInfo.lpImageName,
371 de->u.CreateProcessInfo.lpStartAddress,
372 de->u.CreateProcessInfo.dwDebugInfoFileOffset,
373 de->u.CreateProcessInfo.nDebugInfoSize);
374 dbg_set_process_name(dbg_curr_process, u.buffer);
376 if (!dbg_init(dbg_curr_process->handle, u.buffer, FALSE))
377 dbg_printf("Couldn't initiate DbgHelp\n");
378 if (!dbg_load_module(dbg_curr_process->handle, de->u.CreateProcessInfo.hFile, u.buffer,
379 (DWORD_PTR)de->u.CreateProcessInfo.lpBaseOfImage, 0))
380 dbg_printf("couldn't load main module (%u)\n", GetLastError());
382 WINE_TRACE("%04x:%04x: create thread I @%p\n",
383 de->dwProcessId, de->dwThreadId, de->u.CreateProcessInfo.lpStartAddress);
385 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
386 de->dwThreadId,
387 de->u.CreateProcessInfo.hThread,
388 de->u.CreateProcessInfo.lpThreadLocalBase);
389 if (!dbg_curr_thread)
391 WINE_ERR("Couldn't create thread\n");
392 break;
394 dbg_init_current_process();
395 dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
396 break;
398 case EXIT_PROCESS_DEBUG_EVENT:
399 WINE_TRACE("%04x:%04x: exit process (%d)\n",
400 de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
402 if (dbg_curr_process == NULL)
404 WINE_ERR("Unknown process\n");
405 break;
407 tgt_process_active_close_process(dbg_curr_process, FALSE);
408 dbg_printf("Process of pid=%04x has terminated\n", de->dwProcessId);
409 break;
411 case CREATE_THREAD_DEBUG_EVENT:
412 WINE_TRACE("%04x:%04x: create thread D @%p\n",
413 de->dwProcessId, de->dwThreadId, de->u.CreateThread.lpStartAddress);
415 if (dbg_curr_process == NULL)
417 WINE_ERR("Unknown process\n");
418 break;
420 if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
422 WINE_TRACE("Thread already listed, skipping\n");
423 break;
426 dbg_curr_thread = dbg_add_thread(dbg_curr_process,
427 de->dwThreadId,
428 de->u.CreateThread.hThread,
429 de->u.CreateThread.lpThreadLocalBase);
430 if (!dbg_curr_thread)
432 WINE_ERR("Couldn't create thread\n");
433 break;
435 dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
436 break;
438 case EXIT_THREAD_DEBUG_EVENT:
439 WINE_TRACE("%04x:%04x: exit thread (%d)\n",
440 de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
442 if (dbg_curr_thread == NULL)
444 WINE_ERR("Unknown thread\n");
445 break;
447 /* FIXME: remove break point set on thread startup */
448 dbg_del_thread(dbg_curr_thread);
449 break;
451 case LOAD_DLL_DEBUG_EVENT:
452 if (dbg_curr_thread == NULL)
454 WINE_ERR("Unknown thread\n");
455 break;
457 fetch_module_name(de->u.LoadDll.lpImageName,
458 de->u.LoadDll.fUnicode,
459 de->u.LoadDll.lpBaseOfDll,
460 u.buffer, ARRAY_SIZE(u.buffer), FALSE);
462 WINE_TRACE("%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
463 de->dwProcessId, de->dwThreadId,
464 wine_dbgstr_w(u.buffer), de->u.LoadDll.lpBaseOfDll,
465 de->u.LoadDll.dwDebugInfoFileOffset,
466 de->u.LoadDll.nDebugInfoSize);
467 dbg_load_module(dbg_curr_process->handle, de->u.LoadDll.hFile, u.buffer,
468 (DWORD_PTR)de->u.LoadDll.lpBaseOfDll, 0);
469 break_set_xpoints(FALSE);
470 break_check_delayed_bp();
471 break_set_xpoints(TRUE);
472 if (DBG_IVAR(BreakOnDllLoad))
474 dbg_printf("Stopping on DLL %s loading at %p\n",
475 dbg_W2A(u.buffer, -1), de->u.LoadDll.lpBaseOfDll);
476 if (dbg_fetch_context()) cont = 0;
478 break;
480 case UNLOAD_DLL_DEBUG_EVENT:
481 WINE_TRACE("%04x:%04x: unload DLL @%p\n",
482 de->dwProcessId, de->dwThreadId,
483 de->u.UnloadDll.lpBaseOfDll);
484 break_delete_xpoints_from_module((DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
485 SymUnloadModule64(dbg_curr_process->handle, (DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
486 break;
488 case OUTPUT_DEBUG_STRING_EVENT:
489 if (dbg_curr_thread == NULL)
491 WINE_ERR("Unknown thread\n");
492 break;
495 memory_get_string(dbg_curr_process,
496 de->u.DebugString.lpDebugStringData, TRUE,
497 de->u.DebugString.fUnicode, u.bufferA, sizeof(u.bufferA));
498 WINE_TRACE("%04x:%04x: output debug string (%s)\n",
499 de->dwProcessId, de->dwThreadId, u.bufferA);
500 break;
502 case RIP_EVENT:
503 WINE_TRACE("%04x:%04x: rip error=%u type=%u\n",
504 de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
505 de->u.RipInfo.dwType);
506 break;
508 default:
509 WINE_TRACE("%04x:%04x: unknown event (%x)\n",
510 de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
512 if (!cont) return TRUE; /* stop execution */
513 ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
514 return FALSE; /* continue execution */
517 static void dbg_resume_debuggee(DWORD cont)
519 if (dbg_curr_thread->in_exception)
521 ADDRESS64 addr;
522 char hexbuf[MAX_OFFSET_TO_STR_LEN];
524 dbg_exception_epilog();
525 memory_get_current_pc(&addr);
526 WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
527 memory_offset_to_string(hexbuf, addr.Offset, 0),
528 dbg_curr_thread->exec_mode,
529 dbg_curr_thread->exec_count);
530 if (dbg_curr_thread)
532 if (!dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context))
533 dbg_printf("Cannot set ctx on %04lx\n", dbg_curr_tid);
536 dbg_interactiveP = FALSE;
537 if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
538 dbg_printf("Cannot continue on %04lx (%08x)\n", dbg_curr_tid, cont);
541 static void wait_exception(void)
543 DEBUG_EVENT de;
545 while (dbg_num_processes() && WaitForDebugEvent(&de, INFINITE))
547 if (dbg_handle_debug_event(&de)) break;
549 dbg_interactiveP = TRUE;
552 void dbg_wait_next_exception(DWORD cont, int count, int mode)
554 ADDRESS64 addr;
555 char hexbuf[MAX_OFFSET_TO_STR_LEN];
557 if (cont == DBG_CONTINUE)
559 dbg_curr_thread->exec_count = count;
560 dbg_curr_thread->exec_mode = mode;
562 dbg_resume_debuggee(cont);
564 wait_exception();
565 if (!dbg_curr_process) return;
567 memory_get_current_pc(&addr);
568 WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
569 memory_offset_to_string(hexbuf, addr.Offset, 0),
570 dbg_curr_thread->exec_mode,
571 dbg_curr_thread->exec_count);
574 void dbg_active_wait_for_first_exception(void)
576 dbg_interactiveP = FALSE;
577 /* wait for first exception */
578 wait_exception();
581 static BOOL dbg_start_debuggee(LPSTR cmdLine)
583 PROCESS_INFORMATION info;
584 STARTUPINFOA startup, current;
585 DWORD flags;
587 GetStartupInfoA(&current);
589 memset(&startup, 0, sizeof(startup));
590 startup.cb = sizeof(startup);
591 startup.dwFlags = STARTF_USESHOWWINDOW;
593 startup.wShowWindow = (current.dwFlags & STARTF_USESHOWWINDOW) ?
594 current.wShowWindow : SW_SHOWNORMAL;
596 /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUIs need it
597 * while GUIs don't
599 flags = DEBUG_PROCESS | CREATE_NEW_CONSOLE;
600 if (!DBG_IVAR(AlsoDebugProcChild)) flags |= DEBUG_ONLY_THIS_PROCESS;
602 if (!CreateProcessA(NULL, cmdLine, NULL, NULL, FALSE, flags,
603 NULL, NULL, &startup, &info))
605 dbg_printf("Couldn't start process '%s'\n", cmdLine);
606 return FALSE;
608 if (!info.dwProcessId)
610 /* this happens when the program being run is not a Wine binary
611 * (for example, a shell wrapper around a WineLib app)
613 /* Current fix: list running processes and let the user attach
614 * to one of them (sic)
615 * FIXME: implement a real fix => grab the process (from the
616 * running processes) from its name
618 dbg_printf("Debuggee has been started (%s)\n"
619 "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
620 "Try to attach to one of those processes:\n", cmdLine);
621 /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
622 Sleep(100);
623 info_win32_processes();
624 return TRUE;
626 dbg_curr_pid = info.dwProcessId;
627 if (!(dbg_curr_process = dbg_add_process(&be_process_active_io, dbg_curr_pid, 0))) return FALSE;
628 dbg_curr_process->active_debuggee = TRUE;
630 return TRUE;
633 void dbg_run_debuggee(const char* args)
635 if (args)
637 WINE_FIXME("Re-running current program with %s as args is broken\n", wine_dbgstr_a(args));
638 return;
640 else
642 if (!dbg_last_cmd_line)
644 dbg_printf("Cannot find previously used command line.\n");
645 return;
647 dbg_start_debuggee(dbg_last_cmd_line);
648 dbg_active_wait_for_first_exception();
649 source_list_from_addr(NULL, 0);
653 static BOOL str2int(const char* str, DWORD_PTR* val)
655 char* ptr;
657 *val = strtol(str, &ptr, 10);
658 return str < ptr && !*ptr;
661 static HANDLE create_temp_file(void)
663 static const WCHAR prefixW[] = {'w','d','b',0};
664 WCHAR path[MAX_PATH], name[MAX_PATH];
666 if (!GetTempPathW( MAX_PATH, path ) || !GetTempFileNameW( path, prefixW, 0, name ))
667 return INVALID_HANDLE_VALUE;
668 return CreateFileW( name, GENERIC_READ|GENERIC_WRITE|DELETE, FILE_SHARE_DELETE,
669 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0 );
672 static const struct
674 int type;
675 int platform;
676 int major;
677 int minor;
678 const char *str;
680 version_table[] =
682 { 0, VER_PLATFORM_WIN32s, 2, 0, "2.0" },
683 { 0, VER_PLATFORM_WIN32s, 3, 0, "3.0" },
684 { 0, VER_PLATFORM_WIN32s, 3, 10, "3.1" },
685 { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 0, "95" },
686 { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 10, "98" },
687 { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 90, "ME" },
688 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 3, 51, "NT 3.51" },
689 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 4, 0, "NT 4.0" },
690 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 0, "2000" },
691 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 1, "XP" },
692 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 2, "XP" },
693 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 5, 2, "Server 2003" },
694 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 0, "Vista" },
695 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 0, "Server 2008" },
696 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 1, "7" },
697 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 1, "Server 2008 R2" },
698 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 2, "8" },
699 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 2, "Server 2012" },
700 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 3, "8.1" },
701 { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 3, "Server 2012 R2" },
702 { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 10, 0, "10" },
705 static const char *get_windows_version(void)
707 OSVERSIONINFOEXW info = { sizeof(OSVERSIONINFOEXW) };
708 static char str[64];
709 int i;
711 GetVersionExW( (OSVERSIONINFOW *)&info );
713 for (i = 0; i < ARRAY_SIZE(version_table); i++)
715 if (version_table[i].type == info.wProductType &&
716 version_table[i].platform == info.dwPlatformId &&
717 version_table[i].major == info.dwMajorVersion &&
718 version_table[i].minor == info.dwMinorVersion)
720 return version_table[i].str;
724 snprintf( str, sizeof(str), "%d.%d (%d)", info.dwMajorVersion,
725 info.dwMinorVersion, info.wProductType );
726 return str;
729 static void output_system_info(void)
731 #ifdef __i386__
732 static const char platform[] = "i386";
733 #elif defined(__x86_64__)
734 static const char platform[] = "x86_64";
735 #elif defined(__powerpc__)
736 static const char platform[] = "powerpc";
737 #elif defined(__arm__)
738 static const char platform[] = "arm";
739 #elif defined(__aarch64__)
740 static const char platform[] = "arm64";
741 #else
742 # error CPU unknown
743 #endif
745 const char *(CDECL *wine_get_build_id)(void);
746 void (CDECL *wine_get_host_version)( const char **sysname, const char **release );
747 BOOL is_wow64;
749 wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
750 wine_get_host_version = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
751 if (!IsWow64Process( dbg_curr_process->handle, &is_wow64 )) is_wow64 = FALSE;
753 dbg_printf( "System information:\n" );
754 if (wine_get_build_id) dbg_printf( " Wine build: %s\n", wine_get_build_id() );
755 dbg_printf( " Platform: %s%s\n", platform, is_wow64 ? " (WOW64)" : "" );
756 dbg_printf( " Version: Windows %s\n", get_windows_version() );
757 if (wine_get_host_version)
759 const char *sysname, *release;
760 wine_get_host_version( &sysname, &release );
761 dbg_printf( " Host system: %s\n", sysname );
762 dbg_printf( " Host version: %s\n", release );
766 /******************************************************************
767 * dbg_active_attach
769 * Tries to attach to a running process
770 * Handles the <pid> or <pid> <evt> forms
772 enum dbg_start dbg_active_attach(int argc, char* argv[])
774 DWORD_PTR pid, evt;
776 /* try the form <myself> pid */
777 if (argc == 1 && str2int(argv[0], &pid) && pid != 0)
779 if (!dbg_attach_debuggee(pid, FALSE))
780 return start_error_init;
782 /* try the form <myself> pid evt (Win32 JIT debugger) */
783 else if (argc == 2 && str2int(argv[0], &pid) && pid != 0 &&
784 str2int(argv[1], &evt) && evt != 0)
786 if (!dbg_attach_debuggee(pid, TRUE))
788 /* don't care about result */
789 SetEvent((HANDLE)evt);
790 return start_error_init;
792 if (!SetEvent((HANDLE)evt))
794 WINE_ERR("Invalid event handle: %lx\n", evt);
795 return start_error_init;
797 CloseHandle((HANDLE)evt);
799 else return start_error_parse;
801 dbg_curr_pid = pid;
802 return start_ok;
805 /******************************************************************
806 * dbg_active_launch
808 * Launches a debuggee (with its arguments) from argc/argv
810 enum dbg_start dbg_active_launch(int argc, char* argv[])
812 int i, len;
813 LPSTR cmd_line;
815 if (argc == 0) return start_error_parse;
817 if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, len = 1)))
819 oom_leave:
820 dbg_printf("Out of memory\n");
821 return start_error_init;
823 cmd_line[0] = '\0';
825 for (i = 0; i < argc; i++)
827 len += strlen(argv[i]) + 1;
828 if (!(cmd_line = HeapReAlloc(GetProcessHeap(), 0, cmd_line, len)))
829 goto oom_leave;
830 strcat(cmd_line, argv[i]);
831 cmd_line[len - 2] = ' ';
832 cmd_line[len - 1] = '\0';
835 if (!dbg_start_debuggee(cmd_line))
837 HeapFree(GetProcessHeap(), 0, cmd_line);
838 return start_error_init;
840 HeapFree(GetProcessHeap(), 0, dbg_last_cmd_line);
841 dbg_last_cmd_line = cmd_line;
842 return start_ok;
845 /******************************************************************
846 * dbg_active_auto
848 * Starts (<pid> or <pid> <evt>) in automatic mode
850 enum dbg_start dbg_active_auto(int argc, char* argv[])
852 HANDLE thread = 0, event = 0, input, output = INVALID_HANDLE_VALUE;
853 enum dbg_start ds = start_error_parse;
855 DBG_IVAR(BreakOnDllLoad) = 0;
857 /* auto mode */
858 argc--; argv++;
859 ds = dbg_active_attach(argc, argv);
860 if (ds != start_ok) {
861 msgbox_res_id(NULL, IDS_INVALID_PARAMS, IDS_AUTO_CAPTION, MB_OK);
862 return ds;
865 switch (display_crash_dialog())
867 case ID_DEBUG:
868 AllocConsole();
869 dbg_init_console();
870 dbg_start_interactive(INVALID_HANDLE_VALUE);
871 return start_ok;
872 case ID_DETAILS:
873 event = CreateEventW( NULL, TRUE, FALSE, NULL );
874 if (event) thread = display_crash_details( event );
875 if (thread) dbg_houtput = output = create_temp_file();
876 break;
879 input = parser_generate_command_file("echo Modules:", "info share",
880 "echo Threads:", "info threads", NULL);
881 if (input == INVALID_HANDLE_VALUE) return start_error_parse;
883 if (dbg_curr_process->active_debuggee)
884 dbg_active_wait_for_first_exception();
886 dbg_interactiveP = TRUE;
887 parser_handle(input);
888 output_system_info();
890 if (output != INVALID_HANDLE_VALUE)
892 SetEvent( event );
893 WaitForSingleObject( thread, INFINITE );
894 CloseHandle( output );
895 CloseHandle( thread );
896 CloseHandle( event );
899 CloseHandle( input );
900 dbg_curr_process->process_io->close_process(dbg_curr_process, TRUE);
901 return start_ok;
904 /******************************************************************
905 * dbg_active_minidump
907 * Starts (<pid> or <pid> <evt>) in minidump mode
909 enum dbg_start dbg_active_minidump(int argc, char* argv[])
911 HANDLE hFile;
912 enum dbg_start ds = start_error_parse;
913 const char* file = NULL;
914 char tmp[8 + 1 + 2 + MAX_PATH]; /* minidump "<file>" */
916 dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
917 DBG_IVAR(BreakOnDllLoad) = 0;
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);
962 if (hFile == INVALID_HANDLE_VALUE) return start_error_parse;
964 if (dbg_curr_process->active_debuggee)
965 dbg_active_wait_for_first_exception();
967 dbg_interactiveP = TRUE;
968 parser_handle(hFile);
970 return start_ok;
973 static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill)
975 if (kill)
977 DWORD exit_code = 0;
979 if (pcs == dbg_curr_process && dbg_curr_thread->in_exception)
980 exit_code = dbg_curr_thread->excpt_record.ExceptionCode;
982 TerminateProcess(pcs->handle, exit_code);
984 else if (pcs == dbg_curr_process)
986 /* remove all set breakpoints in debuggee code */
987 break_set_xpoints(FALSE);
988 /* needed for single stepping (ugly).
989 * should this be handled inside the server ???
991 dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
992 if (dbg_curr_thread->in_exception)
994 dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context);
995 ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, DBG_CONTINUE);
999 if (!kill)
1001 if (!DebugActiveProcessStop(pcs->pid)) return FALSE;
1003 SymCleanup(pcs->handle);
1004 dbg_del_process(pcs);
1006 return TRUE;
1009 static BOOL tgt_process_active_read(HANDLE hProcess, const void* addr,
1010 void* buffer, SIZE_T len, SIZE_T* rlen)
1012 return ReadProcessMemory( hProcess, addr, buffer, len, rlen );
1015 static BOOL tgt_process_active_write(HANDLE hProcess, void* addr,
1016 const void* buffer, SIZE_T len, SIZE_T* wlen)
1018 return WriteProcessMemory( hProcess, addr, buffer, len, wlen );
1021 static BOOL tgt_process_active_get_selector(HANDLE hThread, DWORD sel, LDT_ENTRY* le)
1023 return GetThreadSelectorEntry( hThread, sel, le );
1026 static struct be_process_io be_process_active_io =
1028 tgt_process_active_close_process,
1029 tgt_process_active_read,
1030 tgt_process_active_write,
1031 tgt_process_active_get_selector