tgt_active.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Wine debugger - back-end for an active target
  3. *
  4. * Copyright 2000-2006 Eric Pouech
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include "debugger.h"
  25. #include "psapi.h"
  26. #include "resource.h"
  27. #include "winternl.h"
  28. #include "wine/debug.h"
  29. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  30. static char* dbg_executable;
  31. static char* dbg_last_cmd_line;
  32. static struct be_process_io be_process_active_io;
  33. static void dbg_init_current_process(void)
  34. {
  35. }
  36. static void dbg_init_current_thread(void* start)
  37. {
  38. if (start)
  39. {
  40. if (list_count(&dbg_curr_process->threads) == 1 /* first thread ? */ &&
  41. DBG_IVAR(BreakAllThreadsStartup))
  42. {
  43. ADDRESS64 addr;
  44. break_set_xpoints(FALSE);
  45. addr.Mode = AddrModeFlat;
  46. addr.Offset = (DWORD_PTR)start;
  47. break_add_break(&addr, TRUE, TRUE);
  48. break_set_xpoints(TRUE);
  49. }
  50. }
  51. }
  52. static unsigned dbg_handle_debug_event(DEBUG_EVENT* de);
  53. /******************************************************************
  54. * dbg_attach_debuggee
  55. *
  56. * Sets the debuggee to <pid>
  57. * cofe instructs winedbg what to do when first exception is received
  58. * (break=FALSE, continue=TRUE)
  59. * wfe is set to TRUE if dbg_attach_debuggee should also proceed with all debug events
  60. * until the first exception is received (aka: attach to an already running process)
  61. */
  62. BOOL dbg_attach_debuggee(DWORD pid)
  63. {
  64. if (!(dbg_curr_process = dbg_add_process(&be_process_active_io, pid, 0))) return FALSE;
  65. if (!DebugActiveProcess(pid))
  66. {
  67. dbg_printf("Can't attach process %04x: error %u\n", pid, GetLastError());
  68. dbg_del_process(dbg_curr_process);
  69. return FALSE;
  70. }
  71. SetEnvironmentVariableA("DBGHELP_NOLIVE", NULL);
  72. dbg_curr_process->active_debuggee = TRUE;
  73. return TRUE;
  74. }
  75. static unsigned dbg_fetch_context(void)
  76. {
  77. if (!dbg_curr_process->be_cpu->get_context(dbg_curr_thread->handle, &dbg_context))
  78. {
  79. WINE_WARN("Can't get thread's context\n");
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. BOOL dbg_set_curr_thread(DWORD tid)
  85. {
  86. struct dbg_thread* thread;
  87. if (!dbg_curr_process)
  88. {
  89. dbg_printf("No process loaded\n");
  90. return FALSE;
  91. }
  92. thread = dbg_get_thread(dbg_curr_process, tid);
  93. if (thread)
  94. {
  95. dbg_curr_thread = thread;
  96. dbg_fetch_context();
  97. stack_fetch_frames(&dbg_context);
  98. dbg_curr_tid = tid;
  99. return TRUE;
  100. }
  101. dbg_printf("No such thread\n");
  102. return thread != NULL;
  103. }
  104. /***********************************************************************
  105. * dbg_exception_prolog
  106. *
  107. * Examine exception and decide if interactive mode is entered(return TRUE)
  108. * or exception is silently continued(return FALSE)
  109. * is_debug means the exception is a breakpoint or single step exception
  110. */
  111. static BOOL dbg_exception_prolog(BOOL is_debug, const EXCEPTION_RECORD* rec)
  112. {
  113. ADDRESS64 addr;
  114. BOOL is_break;
  115. memory_get_current_pc(&addr);
  116. 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);
  119. if (is_debug) break_adjust_pc(&addr, rec->ExceptionCode, dbg_curr_thread->first_chance, &is_break);
  120. /*
  121. * Do a quiet backtrace so that we have an idea of what the situation
  122. * is WRT the source files.
  123. */
  124. stack_fetch_frames(&dbg_context);
  125. if (is_debug && !is_break && break_should_continue(&addr, rec->ExceptionCode))
  126. return FALSE;
  127. if (addr.Mode != dbg_curr_thread->addr_mode)
  128. {
  129. const char* name = NULL;
  130. switch (addr.Mode)
  131. {
  132. case AddrMode1616: name = "16 bit"; break;
  133. case AddrMode1632: name = "segmented 32 bit"; break;
  134. case AddrModeReal: name = "vm86"; break;
  135. case AddrModeFlat: name = dbg_curr_process->be_cpu->pointer_size == 4
  136. ? "32 bit" : "64 bit"; break;
  137. }
  138. dbg_printf("In %s mode.\n", name);
  139. dbg_curr_thread->addr_mode = addr.Mode;
  140. }
  141. display_print();
  142. if (!is_debug)
  143. {
  144. /* This is a real crash, dump some info */
  145. dbg_curr_process->be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0);
  146. stack_info(-1);
  147. dbg_curr_process->be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context);
  148. stack_backtrace(dbg_curr_tid);
  149. }
  150. else
  151. {
  152. static char* last_name;
  153. static char* last_file;
  154. char buffer[sizeof(SYMBOL_INFO) + 256];
  155. SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
  156. void* lin = memory_to_linear_addr(&addr);
  157. DWORD64 disp64;
  158. IMAGEHLP_LINE64 il;
  159. DWORD disp;
  160. si->SizeOfStruct = sizeof(*si);
  161. si->MaxNameLen = 256;
  162. il.SizeOfStruct = sizeof(il);
  163. if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) &&
  164. SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
  165. {
  166. if ((!last_name || strcmp(last_name, si->Name)) ||
  167. (!last_file || strcmp(last_file, il.FileName)))
  168. {
  169. HeapFree(GetProcessHeap(), 0, last_name);
  170. HeapFree(GetProcessHeap(), 0, last_file);
  171. last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name);
  172. last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName);
  173. dbg_printf("%s () at %s:%u\n", last_name, last_file, il.LineNumber);
  174. }
  175. }
  176. }
  177. if (!is_debug || is_break ||
  178. dbg_curr_thread->exec_mode == dbg_exec_step_over_insn ||
  179. dbg_curr_thread->exec_mode == dbg_exec_step_into_insn)
  180. {
  181. ADDRESS64 tmp = addr;
  182. /* Show where we crashed */
  183. memory_disasm_one_insn(&tmp);
  184. }
  185. source_list_from_addr(&addr, 0);
  186. return TRUE;
  187. }
  188. static void dbg_exception_epilog(void)
  189. {
  190. break_restart_execution(dbg_curr_thread->exec_count);
  191. /*
  192. * This will have gotten absorbed into the breakpoint info
  193. * if it was used. Otherwise it would have been ignored.
  194. * In any case, we don't mess with it any more.
  195. */
  196. if (dbg_curr_thread->exec_mode == dbg_exec_cont)
  197. dbg_curr_thread->exec_count = 0;
  198. dbg_curr_thread->in_exception = FALSE;
  199. }
  200. static DWORD dbg_handle_exception(const EXCEPTION_RECORD* rec, BOOL first_chance)
  201. {
  202. BOOL is_debug = FALSE;
  203. const THREADNAME_INFO* pThreadName;
  204. struct dbg_thread* pThread;
  205. assert(dbg_curr_thread);
  206. WINE_TRACE("exception=%x first_chance=%c\n",
  207. rec->ExceptionCode, first_chance ? 'Y' : 'N');
  208. switch (rec->ExceptionCode)
  209. {
  210. case EXCEPTION_BREAKPOINT:
  211. case EXCEPTION_SINGLE_STEP:
  212. is_debug = TRUE;
  213. break;
  214. case EXCEPTION_WINE_NAME_THREAD:
  215. pThreadName = (const THREADNAME_INFO*)(rec->ExceptionInformation);
  216. if (pThreadName->dwThreadID == -1)
  217. pThread = dbg_curr_thread;
  218. else
  219. pThread = dbg_get_thread(dbg_curr_process, pThreadName->dwThreadID);
  220. if(!pThread)
  221. {
  222. dbg_printf("Thread ID=%04x not in our list of threads -> can't rename\n", pThreadName->dwThreadID);
  223. return DBG_CONTINUE;
  224. }
  225. if (dbg_read_memory(pThreadName->szName, pThread->name, 9))
  226. dbg_printf("Thread ID=%04x renamed using MS VC6 extension (name==\"%.9s\")\n",
  227. pThread->tid, pThread->name);
  228. return DBG_CONTINUE;
  229. case EXCEPTION_INVALID_HANDLE:
  230. return DBG_CONTINUE;
  231. }
  232. if (first_chance && !is_debug && !DBG_IVAR(BreakOnFirstChance) &&
  233. !(rec->ExceptionFlags & EH_STACK_INVALID))
  234. {
  235. /* pass exception to program except for debug exceptions */
  236. return DBG_EXCEPTION_NOT_HANDLED;
  237. }
  238. dbg_curr_thread->excpt_record = *rec;
  239. dbg_curr_thread->in_exception = TRUE;
  240. dbg_curr_thread->first_chance = first_chance;
  241. if (!is_debug) info_win32_exception();
  242. if (rec->ExceptionCode == STATUS_POSSIBLE_DEADLOCK && !DBG_IVAR(BreakOnCritSectTimeOut))
  243. {
  244. dbg_curr_thread->in_exception = FALSE;
  245. return DBG_EXCEPTION_NOT_HANDLED;
  246. }
  247. if (dbg_exception_prolog(is_debug, rec))
  248. {
  249. dbg_interactiveP = TRUE;
  250. return 0;
  251. }
  252. dbg_exception_epilog();
  253. return DBG_CONTINUE;
  254. }
  255. static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill);
  256. void fetch_module_name(void* name_addr, void* mod_addr, WCHAR* buffer, size_t bufsz)
  257. {
  258. memory_get_string_indirect(dbg_curr_process, name_addr, TRUE, buffer, bufsz);
  259. if (!buffer[0] && !GetModuleFileNameExW(dbg_curr_process->handle, mod_addr, buffer, bufsz))
  260. {
  261. if (GetMappedFileNameW( dbg_curr_process->handle, mod_addr, buffer, bufsz ))
  262. {
  263. /* FIXME: proper NT->Dos conversion */
  264. static const WCHAR nt_prefixW[] = {'\\','?','?','\\'};
  265. if (!wcsncmp( buffer, nt_prefixW, 4 ))
  266. memmove( buffer, buffer + 4, (lstrlenW(buffer + 4) + 1) * sizeof(WCHAR) );
  267. }
  268. else
  269. swprintf(buffer, bufsz, L"DLL_%08lx", (ULONG_PTR)mod_addr);
  270. }
  271. }
  272. static unsigned dbg_handle_debug_event(DEBUG_EVENT* de)
  273. {
  274. union {
  275. char bufferA[256];
  276. WCHAR buffer[256];
  277. } u;
  278. DWORD size, cont = DBG_CONTINUE;
  279. dbg_curr_pid = de->dwProcessId;
  280. dbg_curr_tid = de->dwThreadId;
  281. if ((dbg_curr_process = dbg_get_process(de->dwProcessId)) != NULL)
  282. dbg_curr_thread = dbg_get_thread(dbg_curr_process, de->dwThreadId);
  283. else
  284. dbg_curr_thread = NULL;
  285. switch (de->dwDebugEventCode)
  286. {
  287. case EXCEPTION_DEBUG_EVENT:
  288. if (!dbg_curr_thread)
  289. {
  290. WINE_ERR("%04x:%04x: not a registered process or thread (perhaps a 16 bit one ?)\n",
  291. de->dwProcessId, de->dwThreadId);
  292. break;
  293. }
  294. WINE_TRACE("%04x:%04x: exception code=%08x\n",
  295. de->dwProcessId, de->dwThreadId,
  296. de->u.Exception.ExceptionRecord.ExceptionCode);
  297. if (dbg_curr_process->event_on_first_exception)
  298. {
  299. SetEvent(dbg_curr_process->event_on_first_exception);
  300. CloseHandle(dbg_curr_process->event_on_first_exception);
  301. dbg_curr_process->event_on_first_exception = NULL;
  302. if (!DBG_IVAR(BreakOnAttach)) break;
  303. }
  304. if (dbg_fetch_context())
  305. {
  306. cont = dbg_handle_exception(&de->u.Exception.ExceptionRecord,
  307. de->u.Exception.dwFirstChance);
  308. if (cont && dbg_curr_thread)
  309. {
  310. dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context);
  311. }
  312. }
  313. break;
  314. case CREATE_PROCESS_DEBUG_EVENT:
  315. dbg_curr_process = dbg_add_process(&be_process_active_io, de->dwProcessId,
  316. de->u.CreateProcessInfo.hProcess);
  317. if (dbg_curr_process == NULL)
  318. {
  319. WINE_ERR("Couldn't create process\n");
  320. break;
  321. }
  322. size = ARRAY_SIZE(u.buffer);
  323. if (!QueryFullProcessImageNameW( dbg_curr_process->handle, 0, u.buffer, &size ))
  324. {
  325. swprintf(u.buffer, ARRAY_SIZE(u.buffer), L"Process_%08x", dbg_curr_pid);
  326. }
  327. WINE_TRACE("%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
  328. de->dwProcessId, de->dwThreadId,
  329. wine_dbgstr_w(u.buffer),
  330. de->u.CreateProcessInfo.lpImageName,
  331. de->u.CreateProcessInfo.lpStartAddress,
  332. de->u.CreateProcessInfo.dwDebugInfoFileOffset,
  333. de->u.CreateProcessInfo.nDebugInfoSize);
  334. dbg_set_process_name(dbg_curr_process, u.buffer);
  335. if (!dbg_init(dbg_curr_process->handle, u.buffer, FALSE))
  336. dbg_printf("Couldn't initiate DbgHelp\n");
  337. if (!dbg_load_module(dbg_curr_process->handle, de->u.CreateProcessInfo.hFile, u.buffer,
  338. (DWORD_PTR)de->u.CreateProcessInfo.lpBaseOfImage, 0))
  339. dbg_printf("couldn't load main module (%u)\n", GetLastError());
  340. WINE_TRACE("%04x:%04x: create thread I @%p\n",
  341. de->dwProcessId, de->dwThreadId, de->u.CreateProcessInfo.lpStartAddress);
  342. dbg_curr_thread = dbg_add_thread(dbg_curr_process,
  343. de->dwThreadId,
  344. de->u.CreateProcessInfo.hThread,
  345. de->u.CreateProcessInfo.lpThreadLocalBase);
  346. if (!dbg_curr_thread)
  347. {
  348. WINE_ERR("Couldn't create thread\n");
  349. break;
  350. }
  351. dbg_init_current_process();
  352. dbg_init_current_thread(de->u.CreateProcessInfo.lpStartAddress);
  353. break;
  354. case EXIT_PROCESS_DEBUG_EVENT:
  355. WINE_TRACE("%04x:%04x: exit process (%d)\n",
  356. de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
  357. if (dbg_curr_process == NULL)
  358. {
  359. WINE_ERR("Unknown process\n");
  360. break;
  361. }
  362. tgt_process_active_close_process(dbg_curr_process, FALSE);
  363. dbg_printf("Process of pid=%04x has terminated\n", de->dwProcessId);
  364. break;
  365. case CREATE_THREAD_DEBUG_EVENT:
  366. WINE_TRACE("%04x:%04x: create thread D @%p\n",
  367. de->dwProcessId, de->dwThreadId, de->u.CreateThread.lpStartAddress);
  368. if (dbg_curr_process == NULL)
  369. {
  370. WINE_ERR("Unknown process\n");
  371. break;
  372. }
  373. if (dbg_get_thread(dbg_curr_process, de->dwThreadId) != NULL)
  374. {
  375. WINE_TRACE("Thread already listed, skipping\n");
  376. break;
  377. }
  378. dbg_curr_thread = dbg_add_thread(dbg_curr_process,
  379. de->dwThreadId,
  380. de->u.CreateThread.hThread,
  381. de->u.CreateThread.lpThreadLocalBase);
  382. if (!dbg_curr_thread)
  383. {
  384. WINE_ERR("Couldn't create thread\n");
  385. break;
  386. }
  387. dbg_init_current_thread(de->u.CreateThread.lpStartAddress);
  388. break;
  389. case EXIT_THREAD_DEBUG_EVENT:
  390. WINE_TRACE("%04x:%04x: exit thread (%d)\n",
  391. de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
  392. if (dbg_curr_thread == NULL)
  393. {
  394. WINE_ERR("Unknown thread\n");
  395. break;
  396. }
  397. /* FIXME: remove break point set on thread startup */
  398. dbg_del_thread(dbg_curr_thread);
  399. break;
  400. case LOAD_DLL_DEBUG_EVENT:
  401. if (dbg_curr_thread == NULL)
  402. {
  403. WINE_ERR("Unknown thread\n");
  404. break;
  405. }
  406. fetch_module_name(de->u.LoadDll.lpImageName, de->u.LoadDll.lpBaseOfDll,
  407. u.buffer, ARRAY_SIZE(u.buffer));
  408. WINE_TRACE("%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
  409. de->dwProcessId, de->dwThreadId,
  410. wine_dbgstr_w(u.buffer), de->u.LoadDll.lpBaseOfDll,
  411. de->u.LoadDll.dwDebugInfoFileOffset,
  412. de->u.LoadDll.nDebugInfoSize);
  413. dbg_load_module(dbg_curr_process->handle, de->u.LoadDll.hFile, u.buffer,
  414. (DWORD_PTR)de->u.LoadDll.lpBaseOfDll, 0);
  415. break_set_xpoints(FALSE);
  416. break_check_delayed_bp();
  417. break_set_xpoints(TRUE);
  418. if (DBG_IVAR(BreakOnDllLoad))
  419. {
  420. dbg_printf("Stopping on DLL %s loading at %p\n",
  421. dbg_W2A(u.buffer, -1), de->u.LoadDll.lpBaseOfDll);
  422. if (dbg_fetch_context()) cont = 0;
  423. }
  424. break;
  425. case UNLOAD_DLL_DEBUG_EVENT:
  426. WINE_TRACE("%04x:%04x: unload DLL @%p\n",
  427. de->dwProcessId, de->dwThreadId,
  428. de->u.UnloadDll.lpBaseOfDll);
  429. break_delete_xpoints_from_module((DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
  430. SymUnloadModule64(dbg_curr_process->handle, (DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
  431. break;
  432. case OUTPUT_DEBUG_STRING_EVENT:
  433. if (dbg_curr_thread == NULL)
  434. {
  435. WINE_ERR("Unknown thread\n");
  436. break;
  437. }
  438. memory_get_string(dbg_curr_process,
  439. de->u.DebugString.lpDebugStringData, TRUE,
  440. de->u.DebugString.fUnicode, u.bufferA, sizeof(u.bufferA));
  441. WINE_TRACE("%04x:%04x: output debug string (%s)\n",
  442. de->dwProcessId, de->dwThreadId, u.bufferA);
  443. break;
  444. case RIP_EVENT:
  445. WINE_TRACE("%04x:%04x: rip error=%u type=%u\n",
  446. de->dwProcessId, de->dwThreadId, de->u.RipInfo.dwError,
  447. de->u.RipInfo.dwType);
  448. break;
  449. default:
  450. WINE_TRACE("%04x:%04x: unknown event (%x)\n",
  451. de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
  452. }
  453. if (!cont) return TRUE; /* stop execution */
  454. ContinueDebugEvent(de->dwProcessId, de->dwThreadId, cont);
  455. return FALSE; /* continue execution */
  456. }
  457. static void dbg_resume_debuggee(DWORD cont)
  458. {
  459. if (dbg_curr_thread->in_exception)
  460. {
  461. ADDRESS64 addr;
  462. char hexbuf[MAX_OFFSET_TO_STR_LEN];
  463. dbg_exception_epilog();
  464. memory_get_current_pc(&addr);
  465. WINE_TRACE("Exiting debugger PC=%s mode=%d count=%d\n",
  466. memory_offset_to_string(hexbuf, addr.Offset, 0),
  467. dbg_curr_thread->exec_mode,
  468. dbg_curr_thread->exec_count);
  469. if (dbg_curr_thread)
  470. {
  471. if (!dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context))
  472. dbg_printf("Cannot set ctx on %04x\n", dbg_curr_tid);
  473. }
  474. }
  475. dbg_interactiveP = FALSE;
  476. if (!ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, cont))
  477. dbg_printf("Cannot continue on %04x (%08x)\n", dbg_curr_tid, cont);
  478. }
  479. static void wait_exception(void)
  480. {
  481. DEBUG_EVENT de;
  482. while (dbg_num_processes() && WaitForDebugEvent(&de, INFINITE))
  483. {
  484. if (dbg_handle_debug_event(&de)) break;
  485. }
  486. dbg_interactiveP = TRUE;
  487. }
  488. void dbg_wait_next_exception(DWORD cont, int count, int mode)
  489. {
  490. ADDRESS64 addr;
  491. char hexbuf[MAX_OFFSET_TO_STR_LEN];
  492. if (cont == DBG_CONTINUE)
  493. {
  494. dbg_curr_thread->exec_count = count;
  495. dbg_curr_thread->exec_mode = mode;
  496. }
  497. dbg_resume_debuggee(cont);
  498. wait_exception();
  499. if (!dbg_curr_process) return;
  500. memory_get_current_pc(&addr);
  501. WINE_TRACE("Entering debugger PC=%s mode=%d count=%d\n",
  502. memory_offset_to_string(hexbuf, addr.Offset, 0),
  503. dbg_curr_thread->exec_mode,
  504. dbg_curr_thread->exec_count);
  505. }
  506. void dbg_active_wait_for_first_exception(void)
  507. {
  508. dbg_interactiveP = FALSE;
  509. /* wait for first exception */
  510. wait_exception();
  511. }
  512. static BOOL dbg_start_debuggee(LPSTR cmdLine)
  513. {
  514. PROCESS_INFORMATION info;
  515. STARTUPINFOA startup, current;
  516. DWORD flags;
  517. GetStartupInfoA(&current);
  518. memset(&startup, 0, sizeof(startup));
  519. startup.cb = sizeof(startup);
  520. startup.dwFlags = STARTF_USESHOWWINDOW;
  521. startup.wShowWindow = (current.dwFlags & STARTF_USESHOWWINDOW) ?
  522. current.wShowWindow : SW_SHOWNORMAL;
  523. /* FIXME: shouldn't need the CREATE_NEW_CONSOLE, but as usual CUIs need it
  524. * while GUIs don't
  525. */
  526. flags = DEBUG_PROCESS | CREATE_NEW_CONSOLE;
  527. if (!DBG_IVAR(AlsoDebugProcChild)) flags |= DEBUG_ONLY_THIS_PROCESS;
  528. if (!CreateProcessA(NULL, cmdLine, NULL, NULL, FALSE, flags,
  529. NULL, NULL, &startup, &info))
  530. {
  531. dbg_printf("Couldn't start process '%s'\n", cmdLine);
  532. return FALSE;
  533. }
  534. if (!info.dwProcessId)
  535. {
  536. /* this happens when the program being run is not a Wine binary
  537. * (for example, a shell wrapper around a WineLib app)
  538. */
  539. /* Current fix: list running processes and let the user attach
  540. * to one of them (sic)
  541. * FIXME: implement a real fix => grab the process (from the
  542. * running processes) from its name
  543. */
  544. dbg_printf("Debuggee has been started (%s)\n"
  545. "But WineDbg isn't attached to it. Maybe you're trying to debug a winelib wrapper ??\n"
  546. "Try to attach to one of those processes:\n", cmdLine);
  547. /* FIXME: (HACK) we need some time before the wrapper executes the winelib app */
  548. Sleep(100);
  549. info_win32_processes();
  550. return TRUE;
  551. }
  552. dbg_curr_pid = info.dwProcessId;
  553. if (!(dbg_curr_process = dbg_add_process(&be_process_active_io, dbg_curr_pid, 0))) return FALSE;
  554. dbg_curr_process->active_debuggee = TRUE;
  555. if (cmdLine != dbg_last_cmd_line)
  556. {
  557. free(dbg_last_cmd_line);
  558. dbg_last_cmd_line = cmdLine;
  559. }
  560. return TRUE;
  561. }
  562. /***********************************************************************
  563. * dbg_build_command_line
  564. *
  565. * (converted from dlls/ntdll/unix/env.c)
  566. *
  567. * Build the command line of a process from the argv array.
  568. *
  569. * We must quote and escape characters so that the argv array can be rebuilt
  570. * from the command line:
  571. * - spaces and tabs must be quoted
  572. * 'a b' -> '"a b"'
  573. * - quotes must be escaped
  574. * '"' -> '\"'
  575. * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
  576. * resulting in an odd number of '\' followed by a '"'
  577. * '\"' -> '\\\"'
  578. * '\\"' -> '\\\\\"'
  579. * - '\'s are followed by the closing '"' must be doubled,
  580. * resulting in an even number of '\' followed by a '"'
  581. * ' \' -> '" \\"'
  582. * ' \\' -> '" \\\\"'
  583. * - '\'s that are not followed by a '"' can be left as is
  584. * 'a\b' == 'a\b'
  585. * 'a\\b' == 'a\\b'
  586. */
  587. static char *dbg_build_command_line( char **argv )
  588. {
  589. int len;
  590. char **arg, *ret;
  591. LPSTR p;
  592. len = 1;
  593. for (arg = argv; *arg; arg++) len += 3 + 2 * strlen( *arg );
  594. if (!(ret = malloc( len ))) return NULL;
  595. p = ret;
  596. for (arg = argv; *arg; arg++)
  597. {
  598. BOOL has_space, has_quote;
  599. int i, bcount;
  600. char *a;
  601. /* check for quotes and spaces in this argument (first arg is always quoted) */
  602. has_space = (arg == argv) || !**arg || strchr( *arg, ' ' ) || strchr( *arg, '\t' );
  603. has_quote = strchr( *arg, '"' ) != NULL;
  604. /* now transfer it to the command line */
  605. if (has_space) *p++ = '"';
  606. if (has_quote || has_space)
  607. {
  608. bcount = 0;
  609. for (a = *arg; *a; a++)
  610. {
  611. if (*a == '\\') bcount++;
  612. else
  613. {
  614. if (*a == '"') /* double all the '\\' preceding this '"', plus one */
  615. for (i = 0; i <= bcount; i++) *p++ = '\\';
  616. bcount = 0;
  617. }
  618. *p++ = *a;
  619. }
  620. }
  621. else
  622. {
  623. strcpy( p, *arg );
  624. p += strlen( p );
  625. }
  626. if (has_space)
  627. {
  628. /* Double all the '\' preceding the closing quote */
  629. for (i = 0; i < bcount; i++) *p++ = '\\';
  630. *p++ = '"';
  631. }
  632. *p++ = ' ';
  633. }
  634. if (p > ret) p--; /* remove last space */
  635. *p = 0;
  636. return ret;
  637. }
  638. void dbg_run_debuggee(struct list_string* ls)
  639. {
  640. if (dbg_curr_process)
  641. {
  642. dbg_printf("Already attached to a process. Use 'detach' or 'kill' before using 'run'\n");
  643. return;
  644. }
  645. if (!dbg_executable)
  646. {
  647. dbg_printf("No active target to be restarted\n");
  648. return;
  649. }
  650. if (ls)
  651. {
  652. char* cl;
  653. char** argv;
  654. unsigned argc = 2, i;
  655. struct list_string* cls;
  656. for (cls = ls; cls; cls = cls->next) argc++;
  657. if (!(argv = malloc(argc * sizeof(argv[0])))) return;
  658. argv[0] = dbg_executable;
  659. for (i = 1, cls = ls; cls; cls = cls->next, i++) argv[i] = cls->string;
  660. argv[i] = NULL;
  661. cl = dbg_build_command_line(argv);
  662. free(argv);
  663. if (!cl || !dbg_start_debuggee(cl))
  664. {
  665. free(cl);
  666. return;
  667. }
  668. }
  669. else
  670. {
  671. if (!dbg_last_cmd_line) dbg_last_cmd_line = strdup(dbg_executable);
  672. dbg_start_debuggee(dbg_last_cmd_line);
  673. }
  674. dbg_active_wait_for_first_exception();
  675. source_list_from_addr(NULL, 0);
  676. }
  677. static BOOL str2int(const char* str, DWORD_PTR* val)
  678. {
  679. char* ptr;
  680. *val = strtol(str, &ptr, 0);
  681. return str < ptr && !*ptr;
  682. }
  683. static HANDLE create_temp_file(void)
  684. {
  685. WCHAR path[MAX_PATH], name[MAX_PATH];
  686. if (!GetTempPathW( MAX_PATH, path ) || !GetTempFileNameW( path, L"wdb", 0, name ))
  687. return INVALID_HANDLE_VALUE;
  688. return CreateFileW( name, GENERIC_READ|GENERIC_WRITE|DELETE, FILE_SHARE_DELETE,
  689. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0 );
  690. }
  691. static const struct
  692. {
  693. int type;
  694. int platform;
  695. int major;
  696. int minor;
  697. const char *str;
  698. }
  699. version_table[] =
  700. {
  701. { 0, VER_PLATFORM_WIN32s, 2, 0, "2.0" },
  702. { 0, VER_PLATFORM_WIN32s, 3, 0, "3.0" },
  703. { 0, VER_PLATFORM_WIN32s, 3, 10, "3.1" },
  704. { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 0, "95" },
  705. { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 10, "98" },
  706. { 0, VER_PLATFORM_WIN32_WINDOWS, 4, 90, "ME" },
  707. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 3, 51, "NT 3.51" },
  708. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 4, 0, "NT 4.0" },
  709. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 0, "2000" },
  710. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 1, "XP" },
  711. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 5, 2, "XP" },
  712. { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 5, 2, "Server 2003" },
  713. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 0, "Vista" },
  714. { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 0, "Server 2008" },
  715. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 1, "7" },
  716. { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 1, "Server 2008 R2" },
  717. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 2, "8" },
  718. { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 2, "Server 2012" },
  719. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 6, 3, "8.1" },
  720. { VER_NT_SERVER, VER_PLATFORM_WIN32_NT, 6, 3, "Server 2012 R2" },
  721. { VER_NT_WORKSTATION, VER_PLATFORM_WIN32_NT, 10, 0, "10" },
  722. };
  723. static const char *get_windows_version(void)
  724. {
  725. RTL_OSVERSIONINFOEXW info = { sizeof(RTL_OSVERSIONINFOEXW) };
  726. static char str[64];
  727. int i;
  728. RtlGetVersion( &info );
  729. for (i = 0; i < ARRAY_SIZE(version_table); i++)
  730. {
  731. if (version_table[i].type == info.wProductType &&
  732. version_table[i].platform == info.dwPlatformId &&
  733. version_table[i].major == info.dwMajorVersion &&
  734. version_table[i].minor == info.dwMinorVersion)
  735. {
  736. return version_table[i].str;
  737. }
  738. }
  739. snprintf( str, sizeof(str), "%d.%d (%d)", info.dwMajorVersion,
  740. info.dwMinorVersion, info.wProductType );
  741. return str;
  742. }
  743. static void output_system_info(void)
  744. {
  745. #ifdef __i386__
  746. static const char platform[] = "i386";
  747. #elif defined(__x86_64__)
  748. static const char platform[] = "x86_64";
  749. #elif defined(__arm__)
  750. static const char platform[] = "arm";
  751. #elif defined(__aarch64__)
  752. static const char platform[] = "arm64";
  753. #else
  754. # error CPU unknown
  755. #endif
  756. const char *(CDECL *wine_get_build_id)(void);
  757. void (CDECL *wine_get_host_version)( const char **sysname, const char **release );
  758. BOOL is_wow64;
  759. wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
  760. wine_get_host_version = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
  761. if (!IsWow64Process( dbg_curr_process->handle, &is_wow64 )) is_wow64 = FALSE;
  762. dbg_printf( "System information:\n" );
  763. if (wine_get_build_id) dbg_printf( " Wine build: %s\n", wine_get_build_id() );
  764. dbg_printf( " Platform: %s%s\n", platform, is_wow64 ? " (WOW64)" : "" );
  765. dbg_printf( " Version: Windows %s\n", get_windows_version() );
  766. if (wine_get_host_version)
  767. {
  768. const char *sysname, *release;
  769. wine_get_host_version( &sysname, &release );
  770. dbg_printf( " Host system: %s\n", sysname );
  771. dbg_printf( " Host version: %s\n", release );
  772. }
  773. }
  774. /******************************************************************
  775. * dbg_active_attach
  776. *
  777. * Tries to attach to a running process
  778. * Handles the <pid> or <pid> <evt> forms
  779. */
  780. enum dbg_start dbg_active_attach(int argc, char* argv[])
  781. {
  782. DWORD_PTR pid, evt;
  783. /* try the form <myself> pid */
  784. if (argc == 1 && str2int(argv[0], &pid) && pid != 0)
  785. {
  786. if (!dbg_attach_debuggee(pid))
  787. return start_error_init;
  788. }
  789. /* try the form <myself> pid evt (Win32 JIT debugger) */
  790. else if (argc == 2 && str2int(argv[0], &pid) && pid != 0 &&
  791. str2int(argv[1], &evt) && evt != 0)
  792. {
  793. if (!dbg_attach_debuggee(pid))
  794. {
  795. /* don't care about result */
  796. SetEvent((HANDLE)evt);
  797. return start_error_init;
  798. }
  799. dbg_curr_process->event_on_first_exception = (HANDLE)evt;
  800. }
  801. else return start_error_parse;
  802. dbg_curr_pid = pid;
  803. return start_ok;
  804. }
  805. /******************************************************************
  806. * dbg_active_launch
  807. *
  808. * Launches a debuggee (with its arguments) from argc/argv
  809. */
  810. enum dbg_start dbg_active_launch(int argc, char* argv[])
  811. {
  812. LPSTR cmd_line;
  813. if (argc == 0) return start_error_parse;
  814. dbg_executable = strdup(argv[0]);
  815. cmd_line = dbg_build_command_line(argv);
  816. if (!dbg_start_debuggee(cmd_line))
  817. {
  818. free(cmd_line);
  819. return start_error_init;
  820. }
  821. return start_ok;
  822. }
  823. /******************************************************************
  824. * dbg_active_auto
  825. *
  826. * Starts (<pid> or <pid> <evt>) in automatic mode
  827. */
  828. enum dbg_start dbg_active_auto(int argc, char* argv[])
  829. {
  830. HANDLE thread = 0, event = 0, input, output = INVALID_HANDLE_VALUE;
  831. enum dbg_start ds = start_error_parse;
  832. DBG_IVAR(BreakOnDllLoad) = 0;
  833. /* auto mode */
  834. argc--; argv++;
  835. ds = dbg_active_attach(argc, argv);
  836. if (ds != start_ok) {
  837. msgbox_res_id(NULL, IDS_INVALID_PARAMS, IDS_AUTO_CAPTION, MB_OK);
  838. return ds;
  839. }
  840. switch (display_crash_dialog())
  841. {
  842. case ID_DEBUG:
  843. AllocConsole();
  844. dbg_init_console();
  845. dbg_start_interactive(NULL, INVALID_HANDLE_VALUE);
  846. return start_ok;
  847. case ID_DETAILS:
  848. event = CreateEventW( NULL, TRUE, FALSE, NULL );
  849. if (event) thread = display_crash_details( event );
  850. if (thread) dbg_houtput = output = create_temp_file();
  851. break;
  852. }
  853. input = parser_generate_command_file("echo Modules:", "info share",
  854. "echo Threads:", "info threads", NULL);
  855. if (input == INVALID_HANDLE_VALUE) return start_error_parse;
  856. if (dbg_curr_process->active_debuggee)
  857. dbg_active_wait_for_first_exception();
  858. dbg_interactiveP = TRUE;
  859. parser_handle(NULL, input);
  860. output_system_info();
  861. if (output != INVALID_HANDLE_VALUE)
  862. {
  863. SetEvent( event );
  864. WaitForSingleObject( thread, INFINITE );
  865. CloseHandle( output );
  866. CloseHandle( thread );
  867. CloseHandle( event );
  868. }
  869. CloseHandle( input );
  870. dbg_curr_process->process_io->close_process(dbg_curr_process, TRUE);
  871. return start_ok;
  872. }
  873. /******************************************************************
  874. * dbg_active_minidump
  875. *
  876. * Starts (<pid> or <pid> <evt>) in minidump mode
  877. */
  878. enum dbg_start dbg_active_minidump(int argc, char* argv[])
  879. {
  880. HANDLE hFile;
  881. enum dbg_start ds = start_error_parse;
  882. const char* file = NULL;
  883. char tmp[8 + 1 + 2 + MAX_PATH]; /* minidump "<file>" */
  884. dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
  885. DBG_IVAR(BreakOnDllLoad) = 0;
  886. argc--; argv++;
  887. /* hard stuff now ; we can get things like:
  888. * --minidump <pid> 1 arg
  889. * --minidump <pid> <evt> 2 args
  890. * --minidump <file> <pid> 2 args
  891. * --minidump <file> <pid> <evt> 3 args
  892. */
  893. switch (argc)
  894. {
  895. case 1:
  896. ds = dbg_active_attach(argc, argv);
  897. break;
  898. case 2:
  899. if ((ds = dbg_active_attach(argc, argv)) != start_ok)
  900. {
  901. file = argv[0];
  902. ds = dbg_active_attach(argc - 1, argv + 1);
  903. }
  904. break;
  905. case 3:
  906. file = argv[0];
  907. ds = dbg_active_attach(argc - 1, argv + 1);
  908. break;
  909. default:
  910. return start_error_parse;
  911. }
  912. if (ds != start_ok) return ds;
  913. memcpy(tmp, "minidump \"", 10);
  914. if (!file)
  915. {
  916. char path[MAX_PATH];
  917. GetTempPathA(sizeof(path), path);
  918. GetTempFileNameA(path, "WD", 0, tmp + 10);
  919. }
  920. else strcpy(tmp + 10, file);
  921. strcat(tmp, "\"");
  922. if (!file)
  923. {
  924. /* FIXME: should generate unix name as well */
  925. dbg_printf("Capturing program state in %s\n", tmp + 9);
  926. }
  927. hFile = parser_generate_command_file(tmp, "detach", NULL);
  928. if (hFile == INVALID_HANDLE_VALUE) return start_error_parse;
  929. if (dbg_curr_process->active_debuggee)
  930. dbg_active_wait_for_first_exception();
  931. dbg_interactiveP = TRUE;
  932. parser_handle(NULL, hFile);
  933. return start_ok;
  934. }
  935. static BOOL tgt_process_active_close_process(struct dbg_process* pcs, BOOL kill)
  936. {
  937. if (kill)
  938. {
  939. DWORD exit_code = 0;
  940. if (pcs == dbg_curr_process && dbg_curr_thread->in_exception)
  941. exit_code = dbg_curr_thread->excpt_record.ExceptionCode;
  942. TerminateProcess(pcs->handle, exit_code);
  943. }
  944. else if (pcs == dbg_curr_process)
  945. {
  946. /* remove all set breakpoints in debuggee code */
  947. break_set_xpoints(FALSE);
  948. /* needed for single stepping (ugly).
  949. * should this be handled inside the server ???
  950. */
  951. dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
  952. if (dbg_curr_thread->in_exception)
  953. {
  954. dbg_curr_process->be_cpu->set_context(dbg_curr_thread->handle, &dbg_context);
  955. ContinueDebugEvent(dbg_curr_pid, dbg_curr_tid, DBG_CONTINUE);
  956. }
  957. }
  958. if (!kill)
  959. {
  960. if (!DebugActiveProcessStop(pcs->pid)) return FALSE;
  961. }
  962. SymCleanup(pcs->handle);
  963. dbg_del_process(pcs);
  964. return TRUE;
  965. }
  966. static BOOL tgt_process_active_read(HANDLE hProcess, const void* addr,
  967. void* buffer, SIZE_T len, SIZE_T* rlen)
  968. {
  969. return ReadProcessMemory( hProcess, addr, buffer, len, rlen );
  970. }
  971. static BOOL tgt_process_active_write(HANDLE hProcess, void* addr,
  972. const void* buffer, SIZE_T len, SIZE_T* wlen)
  973. {
  974. return WriteProcessMemory( hProcess, addr, buffer, len, wlen );
  975. }
  976. static BOOL tgt_process_active_get_selector(HANDLE hThread, DWORD sel, LDT_ENTRY* le)
  977. {
  978. return GetThreadSelectorEntry( hThread, sel, le );
  979. }
  980. static struct be_process_io be_process_active_io =
  981. {
  982. tgt_process_active_close_process,
  983. tgt_process_active_read,
  984. tgt_process_active_write,
  985. tgt_process_active_get_selector
  986. };