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
30 #include "wine/winbase16.h"
33 /***********************************************************************
36 * Dump the top of the stack
40 struct dbg_lvalue lvalue;
43 lvalue.type.id = dbg_itype_segptr;
44 lvalue.type.module = 0;
46 /* FIXME: we assume stack grows the same way as on i386 */
47 if (!memory_get_current_stack(&lvalue.addr))
48 dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
50 dbg_printf("Stack dump:\n");
51 switch (lvalue.addr.Mode)
53 case AddrModeFlat: /* 32-bit mode */
54 case AddrMode1632: /* 32-bit mode */
55 memory_examine(&lvalue, 24, 'x');
57 case AddrModeReal: /* 16-bit mode */
59 memory_examine(&lvalue, 24, 'w');
64 static BOOL stack_set_frame_internal(int newframe)
66 if (newframe >= dbg_curr_thread->num_frames)
67 newframe = dbg_curr_thread->num_frames - 1;
71 if (dbg_curr_thread->curr_frame != newframe)
73 IMAGEHLP_STACK_FRAME ihsf;
75 dbg_curr_thread->curr_frame = newframe;
76 stack_get_current_frame(&ihsf);
77 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
82 static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
84 memset(ihsf, 0, sizeof(*ihsf));
85 ihsf->InstructionOffset = dbg_curr_thread->frames[nf].linear_pc;
86 /* if we're not the first frame, InstructionOffset is the return address
87 * after the call instruction (at least on most processors I know of).
88 * However, there are cases where this address is outside of the current function.
89 * This happens when the called function is marked <NO RETURN>, in which
90 * case the compiler can omit the epilog (gcc 4 does it)
91 * Therefore, we decrement InstructionOffset in order to ensure that
92 * the considered address is really inside the current function.
94 if (nf) ihsf->InstructionOffset--;
95 ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
96 ihsf->StackOffset = dbg_curr_thread->frames[nf].linear_stack;
100 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
103 * If we don't have a valid backtrace, then just return.
105 if (dbg_curr_thread->frames == NULL) return FALSE;
106 return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
109 BOOL stack_get_register_current_frame(unsigned regno, DWORD_PTR** pval)
111 enum be_cpu_addr kind;
113 if (dbg_curr_thread->frames == NULL) return FALSE;
115 if (!be_cpu->get_register_info(regno, &kind)) return FALSE;
120 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
122 case be_cpu_addr_stack:
123 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
125 case be_cpu_addr_frame:
126 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
132 BOOL stack_get_register_frame(const struct dbg_internal_var* div, DWORD_PTR** pval)
134 if (dbg_curr_thread->frames == NULL) return FALSE;
135 if (dbg_curr_thread->frames[dbg_curr_thread->curr_frame].is_ctx_valid)
136 *pval = (DWORD_PTR*)((char*)&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].context +
137 (DWORD_PTR)div->pval);
140 enum be_cpu_addr kind;
142 if (!be_cpu->get_register_info(div->val, &kind)) return FALSE;
144 /* reuse some known registers directly out of stackwalk details */
148 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
150 case be_cpu_addr_stack:
151 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
153 case be_cpu_addr_frame:
154 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
161 BOOL stack_set_frame(int newframe)
164 if (!stack_set_frame_internal(newframe)) return FALSE;
165 addr.Mode = AddrModeFlat;
166 addr.Offset = (DWORD_PTR)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
167 source_list_from_addr(&addr, 0);
171 /******************************************************************
172 * stack_get_current_symbol
174 * Retrieves the symbol information for the current frame element
176 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
178 IMAGEHLP_STACK_FRAME ihsf;
181 if (!stack_get_current_frame(&ihsf)) return FALSE;
182 return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
186 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
187 PVOID buffer, DWORD size, PDWORD written)
192 struct dbg_process* pcs = dbg_get_process_h(hProc);
193 if (!pcs) return FALSE;
194 ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
196 if (written != NULL) *written = sz;
200 /******************************************************************
203 * Do a backtrace on the current thread
205 unsigned stack_fetch_frames(const CONTEXT* _ctx)
209 /* as native stackwalk can modify the context passed to it, simply copy
210 * it to avoid any damage
212 CONTEXT ctx = *_ctx, prevctx = ctx;
214 HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
215 dbg_curr_thread->frames = NULL;
217 memset(&sf, 0, sizeof(sf));
218 memory_get_current_frame(&sf.AddrFrame);
219 memory_get_current_pc(&sf.AddrPC);
220 memory_get_current_stack(&sf.AddrStack);
222 /* don't confuse StackWalk by passing in inconsistent addresses */
223 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
225 sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
226 sf.AddrFrame.Mode = AddrModeFlat;
229 while (StackWalk64(be_cpu->machine, dbg_curr_process->handle,
230 dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
231 SymFunctionTableAccess64, SymGetModuleBase64, NULL))
233 dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
234 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
236 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
237 dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);
238 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
239 dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame);
240 dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
241 dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack);
242 dbg_curr_thread->frames[nf].context = prevctx;
243 /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones
244 * if it has been modified by the call to StackWalk...
246 dbg_curr_thread->frames[nf].is_ctx_valid =
248 (dbg_curr_thread->frames[nf - 1].is_ctx_valid &&
249 memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx))));
252 /* we've probably gotten ourselves into an infinite loop so bail */
255 dbg_curr_thread->curr_frame = -1;
256 dbg_curr_thread->num_frames = nf;
257 stack_set_frame_internal(0);
267 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
269 struct sym_enum* se = user;
271 if (sym_info->Flags & SYMFLAG_PARAMETER)
273 if (!se->first) dbg_printf(", "); else se->first = FALSE;
274 symbol_print_local(sym_info, se->frame, FALSE);
279 static void stack_print_addr_and_args(int nf)
281 char buffer[sizeof(SYMBOL_INFO) + 256];
282 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
283 IMAGEHLP_STACK_FRAME ihsf;
288 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
290 stack_get_frame(nf, &ihsf);
292 /* grab module where symbol is. If we don't have a module, we cannot print more */
293 im.SizeOfStruct = sizeof(im);
294 if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
297 si->SizeOfStruct = sizeof(*si);
298 si->MaxNameLen = 256;
299 if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
304 dbg_printf(" %s", si->Name);
305 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
307 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
309 se.frame = ihsf.FrameOffset;
311 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
314 il.SizeOfStruct = sizeof(il);
315 if (SymGetLineFromAddr64(dbg_curr_process->handle,
316 ihsf.InstructionOffset, &disp, &il))
317 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
318 dbg_printf(" in %s", im.ModuleName);
320 else dbg_printf(" in %s (+0x%lx)",
321 im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
324 /******************************************************************
327 * Do a backtrace on the current thread
329 static void backtrace(void)
331 unsigned cf = dbg_curr_thread->curr_frame;
332 IMAGEHLP_STACK_FRAME ihsf;
334 dbg_printf("Backtrace:\n");
335 for (dbg_curr_thread->curr_frame = 0;
336 dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
337 dbg_curr_thread->curr_frame++)
340 (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
341 dbg_curr_thread->curr_frame);
342 stack_print_addr_and_args(dbg_curr_thread->curr_frame);
344 print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
347 /* reset context to current stack frame */
348 dbg_curr_thread->curr_frame = cf;
349 if (!dbg_curr_thread->frames) return;
350 stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
351 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
354 /******************************************************************
357 * Do a backtrace on a thread from its process and its identifier
358 * (preserves current thread and context information)
360 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
362 struct dbg_thread* thread = dbg_curr_thread;
364 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
365 dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
370 dbg_curr_tid = dbg_curr_thread->tid;
371 memset(&context, 0, sizeof(context));
372 context.ContextFlags = CONTEXT_FULL;
373 if (SuspendThread(dbg_curr_thread->handle) != -1)
375 if (!GetThreadContext(dbg_curr_thread->handle, &context))
377 dbg_printf("Can't get context for thread %04x in current process\n",
382 stack_fetch_frames(&context);
385 ResumeThread(dbg_curr_thread->handle);
387 else dbg_printf("Can't suspend thread %04x in current process\n", tid);
389 dbg_curr_thread = thread;
390 dbg_curr_tid = thread ? thread->tid : 0;
393 /******************************************************************
396 * Do a backtrace on every running thread in the system (except the debugger)
397 * (preserves current process information)
399 static void backtrace_all(void)
401 struct dbg_process* process = dbg_curr_process;
402 struct dbg_thread* thread = dbg_curr_thread;
403 CONTEXT ctx = dbg_context;
404 DWORD cpid = dbg_curr_pid;
406 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
408 if (snapshot == INVALID_HANDLE_VALUE)
410 dbg_printf("Unable to create toolhelp snapshot\n");
414 entry.dwSize = sizeof(entry);
415 if (Thread32First(snapshot, &entry))
419 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
420 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
421 cpid != dbg_curr_pid)
422 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
424 if (entry.th32OwnerProcessID == cpid)
426 dbg_curr_process = process;
429 else if (entry.th32OwnerProcessID != dbg_curr_pid)
431 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
433 dbg_printf("\nwarning: could not attach to %04x\n",
434 entry.th32OwnerProcessID);
437 dbg_curr_pid = dbg_curr_process->pid;
438 dbg_active_wait_for_first_exception();
441 dbg_printf("\nBacktracing for thread %04x in process %04lx (%s):\n",
442 entry.th32ThreadID, dbg_curr_pid,
443 dbg_W2A(dbg_curr_process->imageName, -1));
444 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
446 while (Thread32Next(snapshot, &entry));
448 if (dbg_curr_process && cpid != dbg_curr_pid)
449 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
451 CloseHandle(snapshot);
452 dbg_curr_process = process;
454 dbg_curr_thread = thread;
455 dbg_curr_tid = thread ? thread->tid : 0;
459 void stack_backtrace(DWORD tid)
461 /* backtrace every thread in every process except the debugger itself,
462 * invoking via "bt all"
464 if (tid == -1) return backtrace_all();
466 if (!dbg_curr_process)
468 dbg_printf("You must be attached to a process to run this command.\n");
472 if (tid == dbg_curr_tid)
478 backtrace_tid(dbg_curr_process, tid);