stack.c 15 KB

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