winex11: Make sure that all fontconfig support is properly #ifdef'ed.
[wine/multimedia.git] / programs / winedbg / stack.c
blobbccb56aeb917b11fd0befbed82c8ab25524f36c8
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
38 void stack_info(void)
40 struct dbg_lvalue lvalue;
42 lvalue.cookie = 0;
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 or 64-bit mode */
54 memory_examine(&lvalue, 24, 'a');
55 break;
56 case AddrMode1632: /* 32-bit mode */
57 memory_examine(&lvalue, 24, 'x');
58 break;
59 case AddrModeReal: /* 16-bit mode */
60 case AddrMode1616:
61 memory_examine(&lvalue, 24, 'w');
62 break;
66 static BOOL stack_set_frame_internal(int newframe)
68 if (newframe >= dbg_curr_thread->num_frames)
69 newframe = dbg_curr_thread->num_frames - 1;
70 if (newframe < 0)
71 newframe = 0;
73 if (dbg_curr_thread->curr_frame != newframe)
75 IMAGEHLP_STACK_FRAME ihsf;
77 dbg_curr_thread->curr_frame = newframe;
78 stack_get_current_frame(&ihsf);
79 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
81 return TRUE;
84 static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
86 memset(ihsf, 0, sizeof(*ihsf));
87 ihsf->InstructionOffset = dbg_curr_thread->frames[nf].linear_pc;
88 /* if we're not the first frame, InstructionOffset is the return address
89 * after the call instruction (at least on most processors I know of).
90 * However, there are cases where this address is outside of the current function.
91 * This happens when the called function is marked <NO RETURN>, in which
92 * case the compiler can omit the epilog (gcc 4 does it)
93 * Therefore, we decrement InstructionOffset in order to ensure that
94 * the considered address is really inside the current function.
96 if (nf) ihsf->InstructionOffset--;
97 ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
98 ihsf->StackOffset = dbg_curr_thread->frames[nf].linear_stack;
99 return TRUE;
102 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
105 * If we don't have a valid backtrace, then just return.
107 if (dbg_curr_thread->frames == NULL) return FALSE;
108 return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
111 BOOL stack_get_register_current_frame(unsigned regno, DWORD_PTR** pval)
113 enum be_cpu_addr kind;
115 if (dbg_curr_thread->frames == NULL) return FALSE;
117 if (!be_cpu->get_register_info(regno, &kind)) return FALSE;
119 switch (kind)
121 case be_cpu_addr_pc:
122 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
123 break;
124 case be_cpu_addr_stack:
125 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
126 break;
127 case be_cpu_addr_frame:
128 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
129 break;
131 return TRUE;
134 BOOL stack_get_register_frame(const struct dbg_internal_var* div, DWORD_PTR** pval)
136 if (dbg_curr_thread->frames == NULL) return FALSE;
137 if (dbg_curr_thread->frames[dbg_curr_thread->curr_frame].is_ctx_valid)
138 *pval = (DWORD_PTR*)((char*)&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].context +
139 (DWORD_PTR)div->pval);
140 else
142 enum be_cpu_addr kind;
144 if (!be_cpu->get_register_info(div->val, &kind)) return FALSE;
146 /* reuse some known registers directly out of stackwalk details */
147 switch (kind)
149 case be_cpu_addr_pc:
150 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
151 break;
152 case be_cpu_addr_stack:
153 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
154 break;
155 case be_cpu_addr_frame:
156 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
157 break;
160 return TRUE;
163 BOOL stack_set_frame(int newframe)
165 ADDRESS64 addr;
166 if (!stack_set_frame_internal(newframe)) return FALSE;
167 addr.Mode = AddrModeFlat;
168 addr.Offset = (DWORD_PTR)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
169 source_list_from_addr(&addr, 0);
170 return TRUE;
173 /******************************************************************
174 * stack_get_current_symbol
176 * Retrieves the symbol information for the current frame element
178 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
180 IMAGEHLP_STACK_FRAME ihsf;
181 DWORD64 disp;
183 if (!stack_get_current_frame(&ihsf)) return FALSE;
184 return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
185 &disp, symbol);
188 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
189 PVOID buffer, DWORD size, PDWORD written)
191 SIZE_T sz;
192 BOOL ret;
194 struct dbg_process* pcs = dbg_get_process_h(hProc);
195 if (!pcs) return FALSE;
196 ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
197 size, &sz);
198 if (written != NULL) *written = sz;
199 return ret;
202 /******************************************************************
203 * stack_fetch_frames
205 * Do a backtrace on the current thread
207 unsigned stack_fetch_frames(const CONTEXT* _ctx)
209 STACKFRAME64 sf;
210 unsigned nf = 0;
211 /* as native stackwalk can modify the context passed to it, simply copy
212 * it to avoid any damage
214 CONTEXT ctx = *_ctx, prevctx = ctx;
216 HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
217 dbg_curr_thread->frames = NULL;
219 memset(&sf, 0, sizeof(sf));
220 memory_get_current_frame(&sf.AddrFrame);
221 memory_get_current_pc(&sf.AddrPC);
222 memory_get_current_stack(&sf.AddrStack);
224 /* don't confuse StackWalk by passing in inconsistent addresses */
225 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
227 sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
228 sf.AddrFrame.Mode = AddrModeFlat;
231 while (StackWalk64(be_cpu->machine, dbg_curr_process->handle,
232 dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
233 SymFunctionTableAccess64, SymGetModuleBase64, NULL))
235 dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
236 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
238 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
239 dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);
240 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
241 dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame);
242 dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
243 dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack);
244 dbg_curr_thread->frames[nf].context = prevctx;
245 /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones
246 * if it has been modified by the call to StackWalk...
248 dbg_curr_thread->frames[nf].is_ctx_valid =
249 (nf == 0 ||
250 (dbg_curr_thread->frames[nf - 1].is_ctx_valid &&
251 memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx))));
252 prevctx = ctx;
253 nf++;
254 /* we've probably gotten ourselves into an infinite loop so bail */
255 if (nf > 200) break;
257 dbg_curr_thread->curr_frame = -1;
258 dbg_curr_thread->num_frames = nf;
259 stack_set_frame_internal(0);
260 return nf;
263 struct sym_enum
265 DWORD_PTR frame;
266 BOOL first;
269 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
271 struct sym_enum* se = user;
273 if (sym_info->Flags & SYMFLAG_PARAMETER)
275 if (!se->first) dbg_printf(", "); else se->first = FALSE;
276 symbol_print_local(sym_info, se->frame, FALSE);
278 return TRUE;
281 static void stack_print_addr_and_args(int nf)
283 char buffer[sizeof(SYMBOL_INFO) + 256];
284 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
285 IMAGEHLP_STACK_FRAME ihsf;
286 IMAGEHLP_LINE64 il;
287 IMAGEHLP_MODULE im;
288 DWORD64 disp64;
290 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
292 stack_get_frame(nf, &ihsf);
294 /* grab module where symbol is. If we don't have a module, we cannot print more */
295 im.SizeOfStruct = sizeof(im);
296 if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
297 return;
299 si->SizeOfStruct = sizeof(*si);
300 si->MaxNameLen = 256;
301 if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
303 struct sym_enum se;
304 DWORD disp;
306 dbg_printf(" %s", si->Name);
307 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
309 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
310 se.first = TRUE;
311 se.frame = ihsf.FrameOffset;
312 dbg_printf("(");
313 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
314 dbg_printf(")");
316 il.SizeOfStruct = sizeof(il);
317 if (SymGetLineFromAddr64(dbg_curr_process->handle,
318 ihsf.InstructionOffset, &disp, &il))
319 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
320 dbg_printf(" in %s", im.ModuleName);
322 else dbg_printf(" in %s (+0x%lx)",
323 im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
326 /******************************************************************
327 * backtrace
329 * Do a backtrace on the current thread
331 static void backtrace(void)
333 unsigned cf = dbg_curr_thread->curr_frame;
334 IMAGEHLP_STACK_FRAME ihsf;
336 dbg_printf("Backtrace:\n");
337 for (dbg_curr_thread->curr_frame = 0;
338 dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
339 dbg_curr_thread->curr_frame++)
341 dbg_printf("%s%d ",
342 (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
343 dbg_curr_thread->curr_frame);
344 stack_print_addr_and_args(dbg_curr_thread->curr_frame);
345 dbg_printf(" (");
346 print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
347 dbg_printf(")\n");
349 /* reset context to current stack frame */
350 dbg_curr_thread->curr_frame = cf;
351 if (!dbg_curr_thread->frames) return;
352 stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
353 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
356 /******************************************************************
357 * backtrace_tid
359 * Do a backtrace on a thread from its process and its identifier
360 * (preserves current thread and context information)
362 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
364 struct dbg_thread* thread = dbg_curr_thread;
366 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
367 dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
368 else
370 CONTEXT context;
372 dbg_curr_tid = dbg_curr_thread->tid;
373 memset(&context, 0, sizeof(context));
374 context.ContextFlags = CONTEXT_FULL;
375 if (SuspendThread(dbg_curr_thread->handle) != -1)
377 if (!GetThreadContext(dbg_curr_thread->handle, &context))
379 dbg_printf("Can't get context for thread %04x in current process\n",
380 tid);
382 else
384 stack_fetch_frames(&context);
385 backtrace();
387 ResumeThread(dbg_curr_thread->handle);
389 else dbg_printf("Can't suspend thread %04x in current process\n", tid);
391 dbg_curr_thread = thread;
392 dbg_curr_tid = thread ? thread->tid : 0;
395 /******************************************************************
396 * backtrace_all
398 * Do a backtrace on every running thread in the system (except the debugger)
399 * (preserves current process information)
401 static void backtrace_all(void)
403 struct dbg_process* process = dbg_curr_process;
404 struct dbg_thread* thread = dbg_curr_thread;
405 CONTEXT ctx = dbg_context;
406 DWORD cpid = dbg_curr_pid;
407 THREADENTRY32 entry;
408 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
410 if (snapshot == INVALID_HANDLE_VALUE)
412 dbg_printf("Unable to create toolhelp snapshot\n");
413 return;
416 entry.dwSize = sizeof(entry);
417 if (Thread32First(snapshot, &entry))
421 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
422 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
423 cpid != dbg_curr_pid)
424 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
426 if (entry.th32OwnerProcessID == cpid)
428 dbg_curr_process = process;
429 dbg_curr_pid = cpid;
431 else if (entry.th32OwnerProcessID != dbg_curr_pid)
433 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
435 dbg_printf("\nwarning: could not attach to %04x\n",
436 entry.th32OwnerProcessID);
437 continue;
439 dbg_curr_pid = dbg_curr_process->pid;
440 dbg_active_wait_for_first_exception();
443 dbg_printf("\nBacktracing for thread %04x in process %04lx (%s):\n",
444 entry.th32ThreadID, dbg_curr_pid,
445 dbg_W2A(dbg_curr_process->imageName, -1));
446 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
448 while (Thread32Next(snapshot, &entry));
450 if (dbg_curr_process && cpid != dbg_curr_pid)
451 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
453 CloseHandle(snapshot);
454 dbg_curr_process = process;
455 dbg_curr_pid = cpid;
456 dbg_curr_thread = thread;
457 dbg_curr_tid = thread ? thread->tid : 0;
458 dbg_context = ctx;
461 void stack_backtrace(DWORD tid)
463 /* backtrace every thread in every process except the debugger itself,
464 * invoking via "bt all"
466 if (tid == -1) return backtrace_all();
468 if (!dbg_curr_process)
470 dbg_printf("You must be attached to a process to run this command.\n");
471 return;
474 if (tid == dbg_curr_tid)
476 backtrace();
478 else
480 backtrace_tid(dbg_curr_process, tid);