stack.c 15 KB

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