push e7552b73e75828caa36d732caa646c0c70d8de9d
[wine/hacks.git] / programs / winedbg / stack.c
blobfbaf228c8eefb6eca81a21878b921b527ebe791b
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 mode */
54 case AddrMode1632: /* 32-bit mode */
55 memory_examine(&lvalue, 24, 'x');
56 break;
57 case AddrModeReal: /* 16-bit mode */
58 case AddrMode1616:
59 memory_examine(&lvalue, 24, 'w');
60 break;
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;
68 if (newframe < 0)
69 newframe = 0;
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);
79 return TRUE;
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 ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
87 return TRUE;
90 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
93 * If we don't have a valid backtrace, then just return.
95 if (dbg_curr_thread->frames == NULL) return FALSE;
96 return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
99 BOOL stack_get_register_current_frame(unsigned regno, DWORD** pval)
101 enum be_cpu_addr kind;
103 if (dbg_curr_thread->frames == NULL) return FALSE;
105 if (!be_cpu->get_register_info(regno, &kind)) return FALSE;
107 switch (kind)
109 case be_cpu_addr_pc:
110 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
111 break;
112 case be_cpu_addr_stack:
113 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
114 break;
115 case be_cpu_addr_frame:
116 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
117 break;
119 return TRUE;
122 BOOL stack_set_frame(int newframe)
124 ADDRESS64 addr;
125 if (!stack_set_frame_internal(newframe)) return FALSE;
126 addr.Mode = AddrModeFlat;
127 addr.Offset = (unsigned long)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
128 source_list_from_addr(&addr, 0);
129 return TRUE;
132 /******************************************************************
133 * stack_get_current_symbol
135 * Retrieves the symbol information for the current frame element
137 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
139 IMAGEHLP_STACK_FRAME ihsf;
140 DWORD64 disp;
142 if (!stack_get_current_frame(&ihsf)) return FALSE;
143 return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
144 &disp, symbol);
147 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
148 PVOID buffer, DWORD size, PDWORD written)
150 SIZE_T sz;
151 BOOL ret;
153 struct dbg_process* pcs = dbg_get_process_h(hProc);
154 if (!pcs) return FALSE;
155 ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
156 size, &sz);
157 if (written != NULL) *written = sz;
158 return ret;
161 /******************************************************************
162 * stack_fetch_frames
164 * Do a backtrace on the current thread
166 unsigned stack_fetch_frames(void)
168 STACKFRAME64 sf;
169 unsigned nf = 0;
170 /* as native stackwalk can modify the context passed to it, simply copy
171 * it to avoid any damage
173 CONTEXT ctx = dbg_context;
175 HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
176 dbg_curr_thread->frames = NULL;
178 memset(&sf, 0, sizeof(sf));
179 memory_get_current_frame(&sf.AddrFrame);
180 memory_get_current_pc(&sf.AddrPC);
182 /* don't confuse StackWalk by passing in inconsistent addresses */
183 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
185 sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
186 sf.AddrFrame.Mode = AddrModeFlat;
189 while (StackWalk64(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle,
190 dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
191 SymFunctionTableAccess64, SymGetModuleBase64, NULL))
193 dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
194 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
196 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
197 dbg_curr_thread->frames[nf].linear_pc = (DWORD)memory_to_linear_addr(&sf.AddrPC);
198 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
199 dbg_curr_thread->frames[nf].linear_frame = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
200 dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
201 dbg_curr_thread->frames[nf].linear_stack = (DWORD)memory_to_linear_addr(&sf.AddrStack);
202 nf++;
203 /* we've probably gotten ourselves into an infinite loop so bail */
204 if (nf > 200) break;
206 dbg_curr_thread->curr_frame = -1;
207 dbg_curr_thread->num_frames = nf;
208 stack_set_frame_internal(0);
209 return nf;
212 struct sym_enum
214 DWORD frame;
215 BOOL first;
218 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
220 struct sym_enum* se = user;
222 if (sym_info->Flags & SYMFLAG_PARAMETER)
224 if (!se->first) dbg_printf(", "); else se->first = FALSE;
225 symbol_print_local(sym_info, se->frame, FALSE);
227 return TRUE;
230 static void stack_print_addr_and_args(int nf)
232 char buffer[sizeof(SYMBOL_INFO) + 256];
233 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
234 IMAGEHLP_STACK_FRAME ihsf;
235 IMAGEHLP_LINE il;
236 IMAGEHLP_MODULE im;
237 DWORD64 disp64;
239 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
241 stack_get_frame(nf, &ihsf);
243 /* grab module where symbol is. If we don't have a module, we cannot print more */
244 im.SizeOfStruct = sizeof(im);
245 if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
246 return;
248 si->SizeOfStruct = sizeof(*si);
249 si->MaxNameLen = 256;
250 if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
252 struct sym_enum se;
253 DWORD disp;
255 dbg_printf(" %s", si->Name);
256 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
258 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
259 se.first = TRUE;
260 se.frame = ihsf.FrameOffset;
261 dbg_printf("(");
262 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
263 dbg_printf(")");
265 il.SizeOfStruct = sizeof(il);
266 if (SymGetLineFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
267 &disp, &il))
268 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
269 dbg_printf(" in %s", im.ModuleName);
271 else dbg_printf(" in %s (+0x%lx)",
272 im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
275 /******************************************************************
276 * backtrace
278 * Do a backtrace on the current thread
280 static void backtrace(void)
282 unsigned cf = dbg_curr_thread->curr_frame;
283 IMAGEHLP_STACK_FRAME ihsf;
285 dbg_printf("Backtrace:\n");
286 for (dbg_curr_thread->curr_frame = 0;
287 dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
288 dbg_curr_thread->curr_frame++)
290 dbg_printf("%s%d ",
291 (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
292 dbg_curr_thread->curr_frame);
293 stack_print_addr_and_args(dbg_curr_thread->curr_frame);
294 dbg_printf(" (");
295 print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
296 dbg_printf(")\n");
298 /* reset context to current stack frame */
299 dbg_curr_thread->curr_frame = cf;
300 if (!dbg_curr_thread->frames) return;
301 stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
302 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
305 /******************************************************************
306 * backtrace_tid
308 * Do a backtrace on a thread from its process and its identifier
309 * (preserves current thread and context information)
311 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
313 struct dbg_thread* thread = dbg_curr_thread;
315 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
316 dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
317 else
319 CONTEXT saved_ctx = dbg_context;
321 dbg_curr_tid = dbg_curr_thread->tid;
322 memset(&dbg_context, 0, sizeof(dbg_context));
323 dbg_context.ContextFlags = CONTEXT_FULL;
324 if (SuspendThread(dbg_curr_thread->handle) != -1)
326 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
328 dbg_printf("Can't get context for thread %04x in current process\n",
329 tid);
331 else
333 stack_fetch_frames();
334 backtrace();
336 ResumeThread(dbg_curr_thread->handle);
338 else dbg_printf("Can't suspend thread %04x in current process\n", tid);
339 dbg_context = saved_ctx;
341 dbg_curr_thread = thread;
342 dbg_curr_tid = thread ? thread->tid : 0;
345 /******************************************************************
346 * backtrace_all
348 * Do a backtrace on every running thread in the system (except the debugger)
349 * (preserves current process information)
351 static void backtrace_all(void)
353 struct dbg_process* process = dbg_curr_process;
354 struct dbg_thread* thread = dbg_curr_thread;
355 CONTEXT ctx = dbg_context;
356 DWORD cpid = dbg_curr_pid;
357 THREADENTRY32 entry;
358 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
360 if (snapshot == INVALID_HANDLE_VALUE)
362 dbg_printf("Unable to create toolhelp snapshot\n");
363 return;
366 entry.dwSize = sizeof(entry);
367 if (Thread32First(snapshot, &entry))
371 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
372 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
373 cpid != dbg_curr_pid)
374 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
376 if (entry.th32OwnerProcessID == cpid)
378 dbg_curr_process = process;
379 dbg_curr_pid = cpid;
381 else if (entry.th32OwnerProcessID != dbg_curr_pid)
383 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
385 dbg_printf("\nwarning: could not attach to %04x\n",
386 entry.th32OwnerProcessID);
387 continue;
389 dbg_curr_pid = dbg_curr_process->pid;
390 dbg_active_wait_for_first_exception();
393 dbg_printf("\nBacktracing for thread %04x in process %04x (%s):\n",
394 entry.th32ThreadID, dbg_curr_pid,
395 dbg_W2A(dbg_curr_process->imageName, -1));
396 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
398 while (Thread32Next(snapshot, &entry));
400 if (dbg_curr_process && cpid != dbg_curr_pid)
401 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
403 CloseHandle(snapshot);
404 dbg_curr_process = process;
405 dbg_curr_pid = cpid;
406 dbg_curr_thread = thread;
407 dbg_curr_tid = thread ? thread->tid : 0;
408 dbg_context = ctx;
411 void stack_backtrace(DWORD tid)
413 /* backtrace every thread in every process except the debugger itself,
414 * invoking via "bt all"
416 if (tid == -1) return backtrace_all();
418 if (!dbg_curr_process)
420 dbg_printf("You must be attached to a process to run this command.\n");
421 return;
424 if (tid == dbg_curr_tid)
426 backtrace();
428 else
430 backtrace_tid(dbg_curr_process, tid);