stack.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Debugger stack handling
  3. *
  4. * Copyright 1995 Alexandre Julliard
  5. * Copyright 1996 Eric Youngdale
  6. * Copyright 1999 Ove Kåven
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include "config.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "debugger.h"
  26. #include "winbase.h"
  27. #include "wine/winbase16.h"
  28. #include "tlhelp32.h"
  29. /***********************************************************************
  30. * stack_info
  31. *
  32. * Dump the top of the stack. If len <= 0, a default length is used.
  33. */
  34. void stack_info(int len)
  35. {
  36. struct dbg_lvalue lvalue;
  37. if(len <= 0)
  38. len = 24;
  39. lvalue.cookie = 0;
  40. lvalue.type.id = dbg_itype_segptr;
  41. lvalue.type.module = 0;
  42. /* FIXME: we assume stack grows the same way as on i386 */
  43. if (!memory_get_current_stack(&lvalue.addr))
  44. dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
  45. dbg_printf("Stack dump:\n");
  46. switch (lvalue.addr.Mode)
  47. {
  48. case AddrModeFlat: /* 32-bit or 64-bit mode */
  49. memory_examine(&lvalue, len, 'a');
  50. break;
  51. case AddrMode1632: /* 32-bit mode */
  52. memory_examine(&lvalue, len, 'x');
  53. break;
  54. case AddrModeReal: /* 16-bit mode */
  55. case AddrMode1616:
  56. memory_examine(&lvalue, len, 'w');
  57. break;
  58. }
  59. }
  60. static BOOL stack_set_frame_internal(int newframe)
  61. {
  62. if (newframe >= dbg_curr_thread->num_frames)
  63. newframe = dbg_curr_thread->num_frames - 1;
  64. if (newframe < 0)
  65. newframe = 0;
  66. if (dbg_curr_thread->curr_frame != newframe)
  67. {
  68. IMAGEHLP_STACK_FRAME ihsf;
  69. dbg_curr_thread->curr_frame = newframe;
  70. stack_get_current_frame(&ihsf);
  71. SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
  72. }
  73. return TRUE;
  74. }
  75. static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
  76. {
  77. memset(ihsf, 0, sizeof(*ihsf));
  78. ihsf->InstructionOffset = dbg_curr_thread->frames[nf].linear_pc;
  79. /* if we're not the first frame, InstructionOffset is the return address
  80. * after the call instruction (at least on most processors I know of).
  81. * However, there are cases where this address is outside of the current function.
  82. * This happens when the called function is marked <NO RETURN>, in which
  83. * case the compiler can omit the epilog (gcc 4 does it)
  84. * Therefore, we decrement InstructionOffset in order to ensure that
  85. * the considered address is really inside the current function.
  86. */
  87. if (nf) ihsf->InstructionOffset--;
  88. ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
  89. ihsf->StackOffset = dbg_curr_thread->frames[nf].linear_stack;
  90. return TRUE;
  91. }
  92. BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
  93. {
  94. /*
  95. * If we don't have a valid backtrace, then just return.
  96. */
  97. if (dbg_curr_thread->frames == NULL) return FALSE;
  98. return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
  99. }
  100. BOOL stack_get_register_frame(const struct dbg_internal_var* div, DWORD_PTR** pval)
  101. {
  102. if (dbg_curr_thread->frames == NULL) return FALSE;
  103. if (dbg_curr_thread->frames[dbg_curr_thread->curr_frame].is_ctx_valid)
  104. *pval = (DWORD_PTR*)((char*)&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].context +
  105. (DWORD_PTR)div->pval);
  106. else
  107. {
  108. enum be_cpu_addr kind;
  109. if (!dbg_curr_process->be_cpu->get_register_info(div->val, &kind)) return FALSE;
  110. /* reuse some known registers directly out of stackwalk details */
  111. switch (kind)
  112. {
  113. case be_cpu_addr_pc:
  114. *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
  115. break;
  116. case be_cpu_addr_stack:
  117. *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
  118. break;
  119. case be_cpu_addr_frame:
  120. *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
  121. break;
  122. }
  123. }
  124. return TRUE;
  125. }
  126. BOOL stack_set_frame(int newframe)
  127. {
  128. ADDRESS64 addr;
  129. if (!stack_set_frame_internal(newframe)) return FALSE;
  130. addr.Mode = AddrModeFlat;
  131. addr.Offset = (DWORD_PTR)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
  132. source_list_from_addr(&addr, 0);
  133. return TRUE;
  134. }
  135. /******************************************************************
  136. * stack_get_current_symbol
  137. *
  138. * Retrieves the symbol information for the current frame element
  139. */
  140. BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
  141. {
  142. IMAGEHLP_STACK_FRAME ihsf;
  143. DWORD64 disp;
  144. if (!stack_get_current_frame(&ihsf)) return FALSE;
  145. return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
  146. &disp, symbol);
  147. }
  148. static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
  149. PVOID buffer, DWORD size, PDWORD written)
  150. {
  151. SIZE_T sz;
  152. BOOL ret;
  153. struct dbg_process* pcs = dbg_get_process_h(hProc);
  154. if (!pcs) return FALSE;
  155. ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
  156. size, &sz);
  157. if (written != NULL) *written = sz;
  158. return ret;
  159. }
  160. /******************************************************************
  161. * stack_fetch_frames
  162. *
  163. * Do a backtrace on the current thread
  164. */
  165. unsigned stack_fetch_frames(const dbg_ctx_t* _ctx)
  166. {
  167. STACKFRAME64 sf;
  168. unsigned nf = 0;
  169. /* as native stackwalk can modify the context passed to it, simply copy
  170. * it to avoid any damage
  171. */
  172. dbg_ctx_t ctx = *_ctx;
  173. BOOL ret;
  174. HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
  175. dbg_curr_thread->frames = NULL;
  176. memset(&sf, 0, sizeof(sf));
  177. dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_frame, &sf.AddrFrame);
  178. dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_pc, &sf.AddrPC);
  179. dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_stack, &sf.AddrStack);
  180. /* don't confuse StackWalk by passing in inconsistent addresses */
  181. if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
  182. {
  183. sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
  184. sf.AddrFrame.Mode = AddrModeFlat;
  185. }
  186. while ((ret = StackWalk64(dbg_curr_process->be_cpu->machine, dbg_curr_process->handle,
  187. dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
  188. SymFunctionTableAccess64, SymGetModuleBase64, NULL)) ||
  189. nf == 0) /* we always register first frame information */
  190. {
  191. dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
  192. (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
  193. dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
  194. dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);
  195. dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
  196. dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame);
  197. dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
  198. dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack);
  199. dbg_curr_thread->frames[nf].context = ctx;
  200. /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones
  201. * if it has been modified by the call to StackWalk...
  202. */
  203. dbg_curr_thread->frames[nf].is_ctx_valid =
  204. (nf == 0 ||
  205. (dbg_curr_thread->frames[nf - 1].is_ctx_valid &&
  206. memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx))));
  207. nf++;
  208. /* bail if:
  209. * - we've (probably) gotten ourselves into an infinite loop,
  210. * - or StackWalk failed on first frame
  211. */
  212. if (nf > 200 || !ret) break;
  213. }
  214. dbg_curr_thread->curr_frame = -1;
  215. dbg_curr_thread->num_frames = nf;
  216. stack_set_frame_internal(0);
  217. return nf;
  218. }
  219. struct sym_enum
  220. {
  221. DWORD_PTR frame;
  222. BOOL first;
  223. };
  224. static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
  225. {
  226. struct sym_enum* se = user;
  227. if (sym_info->Flags & SYMFLAG_PARAMETER)
  228. {
  229. if (!se->first) dbg_printf(", "); else se->first = FALSE;
  230. symbol_print_local(sym_info, se->frame, FALSE);
  231. }
  232. return TRUE;
  233. }
  234. static void stack_print_addr_and_args(int nf)
  235. {
  236. char buffer[sizeof(SYMBOL_INFO) + 256];
  237. SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
  238. IMAGEHLP_STACK_FRAME ihsf;
  239. IMAGEHLP_LINE64 il;
  240. IMAGEHLP_MODULE im;
  241. DWORD64 disp64;
  242. print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
  243. stack_get_frame(nf, &ihsf);
  244. /* grab module where symbol is. If we don't have a module, we cannot print more */
  245. im.SizeOfStruct = sizeof(im);
  246. if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
  247. return;
  248. si->SizeOfStruct = sizeof(*si);
  249. si->MaxNameLen = 256;
  250. if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
  251. {
  252. struct sym_enum se;
  253. DWORD disp;
  254. dbg_printf(" %s", si->Name);
  255. if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
  256. SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
  257. se.first = TRUE;
  258. se.frame = ihsf.FrameOffset;
  259. dbg_printf("(");
  260. SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
  261. dbg_printf(")");
  262. il.SizeOfStruct = sizeof(il);
  263. if (SymGetLineFromAddr64(dbg_curr_process->handle,
  264. ihsf.InstructionOffset, &disp, &il))
  265. dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
  266. dbg_printf(" in %s", im.ModuleName);
  267. }
  268. else dbg_printf(" in %s (+0x%lx)",
  269. im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
  270. }
  271. /******************************************************************
  272. * backtrace
  273. *
  274. * Do a backtrace on the current thread
  275. */
  276. static void backtrace(void)
  277. {
  278. unsigned cf = dbg_curr_thread->curr_frame;
  279. IMAGEHLP_STACK_FRAME ihsf;
  280. dbg_printf("Backtrace:\n");
  281. for (dbg_curr_thread->curr_frame = 0;
  282. dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
  283. dbg_curr_thread->curr_frame++)
  284. {
  285. dbg_printf("%s%d ",
  286. (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
  287. dbg_curr_thread->curr_frame);
  288. stack_print_addr_and_args(dbg_curr_thread->curr_frame);
  289. dbg_printf(" (");
  290. print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
  291. dbg_printf(")\n");
  292. }
  293. /* reset context to current stack frame */
  294. dbg_curr_thread->curr_frame = cf;
  295. if (!dbg_curr_thread->frames) return;
  296. stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
  297. SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
  298. }
  299. /******************************************************************
  300. * backtrace_tid
  301. *
  302. * Do a backtrace on a thread from its process and its identifier
  303. * (preserves current thread and context information)
  304. */
  305. static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
  306. {
  307. struct dbg_thread* thread = dbg_curr_thread;
  308. if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
  309. dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
  310. else
  311. {
  312. dbg_ctx_t ctx = {{0}};
  313. dbg_curr_tid = dbg_curr_thread->tid;
  314. if (SuspendThread(dbg_curr_thread->handle) != -1)
  315. {
  316. if (!pcs->be_cpu->get_context(dbg_curr_thread->handle, &ctx))
  317. {
  318. dbg_printf("Can't get context for thread %04x in current process\n",
  319. tid);
  320. }
  321. else
  322. {
  323. stack_fetch_frames(&ctx);
  324. backtrace();
  325. }
  326. ResumeThread(dbg_curr_thread->handle);
  327. }
  328. else dbg_printf("Can't suspend thread %04x in current process\n", tid);
  329. }
  330. dbg_curr_thread = thread;
  331. dbg_curr_tid = thread ? thread->tid : 0;
  332. }
  333. /******************************************************************
  334. * backtrace_all
  335. *
  336. * Do a backtrace on every running thread in the system (except the debugger)
  337. * (preserves current process information)
  338. */
  339. static void backtrace_all(void)
  340. {
  341. struct dbg_process* process = dbg_curr_process;
  342. struct dbg_thread* thread = dbg_curr_thread;
  343. dbg_ctx_t ctx = dbg_context;
  344. DWORD cpid = dbg_curr_pid;
  345. THREADENTRY32 entry;
  346. HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  347. if (snapshot == INVALID_HANDLE_VALUE)
  348. {
  349. dbg_printf("Unable to create toolhelp snapshot\n");
  350. return;
  351. }
  352. entry.dwSize = sizeof(entry);
  353. if (Thread32First(snapshot, &entry))
  354. {
  355. do
  356. {
  357. if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
  358. if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
  359. cpid != dbg_curr_pid)
  360. dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
  361. if (entry.th32OwnerProcessID == cpid)
  362. {
  363. dbg_curr_process = process;
  364. dbg_curr_pid = cpid;
  365. }
  366. else if (entry.th32OwnerProcessID != dbg_curr_pid)
  367. {
  368. if (!dbg_attach_debuggee(entry.th32OwnerProcessID))
  369. {
  370. dbg_printf("\nwarning: could not attach to %04x\n",
  371. entry.th32OwnerProcessID);
  372. continue;
  373. }
  374. dbg_curr_pid = dbg_curr_process->pid;
  375. dbg_active_wait_for_first_exception();
  376. }
  377. dbg_printf("\nBacktracing for thread %04x in process %04lx (%s):\n",
  378. entry.th32ThreadID, dbg_curr_pid,
  379. dbg_W2A(dbg_curr_process->imageName, -1));
  380. backtrace_tid(dbg_curr_process, entry.th32ThreadID);
  381. }
  382. while (Thread32Next(snapshot, &entry));
  383. if (dbg_curr_process && cpid != dbg_curr_pid)
  384. dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
  385. }
  386. CloseHandle(snapshot);
  387. dbg_curr_process = process;
  388. dbg_curr_pid = cpid;
  389. dbg_curr_thread = thread;
  390. dbg_curr_tid = thread ? thread->tid : 0;
  391. dbg_context = ctx;
  392. }
  393. void stack_backtrace(DWORD tid)
  394. {
  395. /* backtrace every thread in every process except the debugger itself,
  396. * invoking via "bt all"
  397. */
  398. if (tid == -1)
  399. {
  400. backtrace_all();
  401. return;
  402. }
  403. if (!dbg_curr_process)
  404. {
  405. dbg_printf("You must be attached to a process to run this command.\n");
  406. return;
  407. }
  408. if (tid == dbg_curr_tid)
  409. {
  410. backtrace();
  411. }
  412. else
  413. {
  414. backtrace_tid(dbg_curr_process, tid);
  415. }
  416. }