winedbg: Use the correct context when fetching the frame state for backtraces.
[wine/multimedia.git] / programs / winedbg / stack.c
blob57eca5ce79eb84a9188fd0e594bf2aa0a9b2c328
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
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.
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.
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
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
28 #include "debugger.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "tlhelp32.h"
33 /***********************************************************************
34 * stack_info
36 * Dump the top of the stack. If len <= 0, a default length is used.
38 void stack_info(int len)
40 struct dbg_lvalue lvalue;
42 if(len <= 0)
43 len = 24;
45 lvalue.cookie = 0;
46 lvalue.type.id = dbg_itype_segptr;
47 lvalue.type.module = 0;
49 /* FIXME: we assume stack grows the same way as on i386 */
50 if (!memory_get_current_stack(&lvalue.addr))
51 dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
53 dbg_printf("Stack dump:\n");
54 switch (lvalue.addr.Mode)
56 case AddrModeFlat: /* 32-bit or 64-bit mode */
57 memory_examine(&lvalue, len, 'a');
58 break;
59 case AddrMode1632: /* 32-bit mode */
60 memory_examine(&lvalue, len, 'x');
61 break;
62 case AddrModeReal: /* 16-bit mode */
63 case AddrMode1616:
64 memory_examine(&lvalue, len, 'w');
65 break;
69 static BOOL stack_set_frame_internal(int newframe)
71 if (newframe >= dbg_curr_thread->num_frames)
72 newframe = dbg_curr_thread->num_frames - 1;
73 if (newframe < 0)
74 newframe = 0;
76 if (dbg_curr_thread->curr_frame != newframe)
78 IMAGEHLP_STACK_FRAME ihsf;
80 dbg_curr_thread->curr_frame = newframe;
81 stack_get_current_frame(&ihsf);
82 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
84 return TRUE;
87 static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
89 memset(ihsf, 0, sizeof(*ihsf));
90 ihsf->InstructionOffset = dbg_curr_thread->frames[nf].linear_pc;
91 /* if we're not the first frame, InstructionOffset is the return address
92 * after the call instruction (at least on most processors I know of).
93 * However, there are cases where this address is outside of the current function.
94 * This happens when the called function is marked <NO RETURN>, in which
95 * case the compiler can omit the epilog (gcc 4 does it)
96 * Therefore, we decrement InstructionOffset in order to ensure that
97 * the considered address is really inside the current function.
99 if (nf) ihsf->InstructionOffset--;
100 ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
101 ihsf->StackOffset = dbg_curr_thread->frames[nf].linear_stack;
102 return TRUE;
105 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
108 * If we don't have a valid backtrace, then just return.
110 if (dbg_curr_thread->frames == NULL) return FALSE;
111 return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
114 BOOL stack_get_register_current_frame(unsigned regno, DWORD_PTR** pval)
116 enum be_cpu_addr kind;
118 if (dbg_curr_thread->frames == NULL) return FALSE;
120 if (!be_cpu->get_register_info(regno, &kind)) return FALSE;
122 switch (kind)
124 case be_cpu_addr_pc:
125 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
126 break;
127 case be_cpu_addr_stack:
128 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
129 break;
130 case be_cpu_addr_frame:
131 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
132 break;
134 return TRUE;
137 BOOL stack_get_register_frame(const struct dbg_internal_var* div, DWORD_PTR** pval)
139 if (dbg_curr_thread->frames == NULL) return FALSE;
140 if (dbg_curr_thread->frames[dbg_curr_thread->curr_frame].is_ctx_valid)
141 *pval = (DWORD_PTR*)((char*)&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].context +
142 (DWORD_PTR)div->pval);
143 else
145 enum be_cpu_addr kind;
147 if (!be_cpu->get_register_info(div->val, &kind)) return FALSE;
149 /* reuse some known registers directly out of stackwalk details */
150 switch (kind)
152 case be_cpu_addr_pc:
153 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
154 break;
155 case be_cpu_addr_stack:
156 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
157 break;
158 case be_cpu_addr_frame:
159 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
160 break;
163 return TRUE;
166 BOOL stack_set_frame(int newframe)
168 ADDRESS64 addr;
169 if (!stack_set_frame_internal(newframe)) return FALSE;
170 addr.Mode = AddrModeFlat;
171 addr.Offset = (DWORD_PTR)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
172 source_list_from_addr(&addr, 0);
173 return TRUE;
176 /******************************************************************
177 * stack_get_current_symbol
179 * Retrieves the symbol information for the current frame element
181 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
183 IMAGEHLP_STACK_FRAME ihsf;
184 DWORD64 disp;
186 if (!stack_get_current_frame(&ihsf)) return FALSE;
187 return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
188 &disp, symbol);
191 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
192 PVOID buffer, DWORD size, PDWORD written)
194 SIZE_T sz;
195 BOOL ret;
197 struct dbg_process* pcs = dbg_get_process_h(hProc);
198 if (!pcs) return FALSE;
199 ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
200 size, &sz);
201 if (written != NULL) *written = sz;
202 return ret;
205 /******************************************************************
206 * stack_fetch_frames
208 * Do a backtrace on the current thread
210 unsigned stack_fetch_frames(const CONTEXT* _ctx)
212 STACKFRAME64 sf;
213 unsigned nf = 0;
214 /* as native stackwalk can modify the context passed to it, simply copy
215 * it to avoid any damage
217 CONTEXT ctx = *_ctx, prevctx = ctx;
219 HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
220 dbg_curr_thread->frames = NULL;
222 memset(&sf, 0, sizeof(sf));
223 be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_frame, &sf.AddrFrame);
224 be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_pc, &sf.AddrPC);
225 be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_stack, &sf.AddrStack);
227 /* don't confuse StackWalk by passing in inconsistent addresses */
228 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
230 sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
231 sf.AddrFrame.Mode = AddrModeFlat;
234 while (StackWalk64(be_cpu->machine, dbg_curr_process->handle,
235 dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
236 SymFunctionTableAccess64, SymGetModuleBase64, NULL))
238 dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
239 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
241 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
242 dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);
243 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
244 dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame);
245 dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
246 dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack);
247 dbg_curr_thread->frames[nf].context = prevctx;
248 /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones
249 * if it has been modified by the call to StackWalk...
251 dbg_curr_thread->frames[nf].is_ctx_valid =
252 (nf == 0 ||
253 (dbg_curr_thread->frames[nf - 1].is_ctx_valid &&
254 memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx))));
255 prevctx = ctx;
256 nf++;
257 /* we've probably gotten ourselves into an infinite loop so bail */
258 if (nf > 200) break;
260 dbg_curr_thread->curr_frame = -1;
261 dbg_curr_thread->num_frames = nf;
262 stack_set_frame_internal(0);
263 return nf;
266 struct sym_enum
268 DWORD_PTR frame;
269 BOOL first;
272 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
274 struct sym_enum* se = user;
276 if (sym_info->Flags & SYMFLAG_PARAMETER)
278 if (!se->first) dbg_printf(", "); else se->first = FALSE;
279 symbol_print_local(sym_info, se->frame, FALSE);
281 return TRUE;
284 static void stack_print_addr_and_args(int nf)
286 char buffer[sizeof(SYMBOL_INFO) + 256];
287 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
288 IMAGEHLP_STACK_FRAME ihsf;
289 IMAGEHLP_LINE64 il;
290 IMAGEHLP_MODULE im;
291 DWORD64 disp64;
293 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
295 stack_get_frame(nf, &ihsf);
297 /* grab module where symbol is. If we don't have a module, we cannot print more */
298 im.SizeOfStruct = sizeof(im);
299 if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
300 return;
302 si->SizeOfStruct = sizeof(*si);
303 si->MaxNameLen = 256;
304 if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
306 struct sym_enum se;
307 DWORD disp;
309 dbg_printf(" %s", si->Name);
310 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
312 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
313 se.first = TRUE;
314 se.frame = ihsf.FrameOffset;
315 dbg_printf("(");
316 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
317 dbg_printf(")");
319 il.SizeOfStruct = sizeof(il);
320 if (SymGetLineFromAddr64(dbg_curr_process->handle,
321 ihsf.InstructionOffset, &disp, &il))
322 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
323 dbg_printf(" in %s", im.ModuleName);
325 else dbg_printf(" in %s (+0x%lx)",
326 im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
329 /******************************************************************
330 * backtrace
332 * Do a backtrace on the current thread
334 static void backtrace(void)
336 unsigned cf = dbg_curr_thread->curr_frame;
337 IMAGEHLP_STACK_FRAME ihsf;
339 dbg_printf("Backtrace:\n");
340 for (dbg_curr_thread->curr_frame = 0;
341 dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
342 dbg_curr_thread->curr_frame++)
344 dbg_printf("%s%d ",
345 (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
346 dbg_curr_thread->curr_frame);
347 stack_print_addr_and_args(dbg_curr_thread->curr_frame);
348 dbg_printf(" (");
349 print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
350 dbg_printf(")\n");
352 /* reset context to current stack frame */
353 dbg_curr_thread->curr_frame = cf;
354 if (!dbg_curr_thread->frames) return;
355 stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
356 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
359 /******************************************************************
360 * backtrace_tid
362 * Do a backtrace on a thread from its process and its identifier
363 * (preserves current thread and context information)
365 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
367 struct dbg_thread* thread = dbg_curr_thread;
369 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
370 dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
371 else
373 CONTEXT context;
375 dbg_curr_tid = dbg_curr_thread->tid;
376 memset(&context, 0, sizeof(context));
377 context.ContextFlags = CONTEXT_FULL;
378 if (SuspendThread(dbg_curr_thread->handle) != -1)
380 if (!GetThreadContext(dbg_curr_thread->handle, &context))
382 dbg_printf("Can't get context for thread %04x in current process\n",
383 tid);
385 else
387 stack_fetch_frames(&context);
388 backtrace();
390 ResumeThread(dbg_curr_thread->handle);
392 else dbg_printf("Can't suspend thread %04x in current process\n", tid);
394 dbg_curr_thread = thread;
395 dbg_curr_tid = thread ? thread->tid : 0;
398 /******************************************************************
399 * backtrace_all
401 * Do a backtrace on every running thread in the system (except the debugger)
402 * (preserves current process information)
404 static void backtrace_all(void)
406 struct dbg_process* process = dbg_curr_process;
407 struct dbg_thread* thread = dbg_curr_thread;
408 CONTEXT ctx = dbg_context;
409 DWORD cpid = dbg_curr_pid;
410 THREADENTRY32 entry;
411 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
413 if (snapshot == INVALID_HANDLE_VALUE)
415 dbg_printf("Unable to create toolhelp snapshot\n");
416 return;
419 entry.dwSize = sizeof(entry);
420 if (Thread32First(snapshot, &entry))
424 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
425 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
426 cpid != dbg_curr_pid)
427 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
429 if (entry.th32OwnerProcessID == cpid)
431 dbg_curr_process = process;
432 dbg_curr_pid = cpid;
434 else if (entry.th32OwnerProcessID != dbg_curr_pid)
436 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
438 dbg_printf("\nwarning: could not attach to %04x\n",
439 entry.th32OwnerProcessID);
440 continue;
442 dbg_curr_pid = dbg_curr_process->pid;
443 dbg_active_wait_for_first_exception();
446 dbg_printf("\nBacktracing for thread %04x in process %04lx (%s):\n",
447 entry.th32ThreadID, dbg_curr_pid,
448 dbg_W2A(dbg_curr_process->imageName, -1));
449 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
451 while (Thread32Next(snapshot, &entry));
453 if (dbg_curr_process && cpid != dbg_curr_pid)
454 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
456 CloseHandle(snapshot);
457 dbg_curr_process = process;
458 dbg_curr_pid = cpid;
459 dbg_curr_thread = thread;
460 dbg_curr_tid = thread ? thread->tid : 0;
461 dbg_context = ctx;
464 void stack_backtrace(DWORD tid)
466 /* backtrace every thread in every process except the debugger itself,
467 * invoking via "bt all"
469 if (tid == -1) return backtrace_all();
471 if (!dbg_curr_process)
473 dbg_printf("You must be attached to a process to run this command.\n");
474 return;
477 if (tid == dbg_curr_tid)
479 backtrace();
481 else
483 backtrace_tid(dbg_curr_process, tid);