winemac: Reenable window autodisplay when the frame is set and redisplayed.
[wine.git] / programs / winedbg / stack.c
blobf49480afb5b5064410b183d704d0a953408c7461
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_frame(const struct dbg_internal_var* div, DWORD_PTR** pval)
116 if (dbg_curr_thread->frames == NULL) return FALSE;
117 if (dbg_curr_thread->frames[dbg_curr_thread->curr_frame].is_ctx_valid)
118 *pval = (DWORD_PTR*)((char*)&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].context +
119 (DWORD_PTR)div->pval);
120 else
122 enum be_cpu_addr kind;
124 if (!be_cpu->get_register_info(div->val, &kind)) return FALSE;
126 /* reuse some known registers directly out of stackwalk details */
127 switch (kind)
129 case be_cpu_addr_pc:
130 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
131 break;
132 case be_cpu_addr_stack:
133 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
134 break;
135 case be_cpu_addr_frame:
136 *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
137 break;
140 return TRUE;
143 BOOL stack_set_frame(int newframe)
145 ADDRESS64 addr;
146 if (!stack_set_frame_internal(newframe)) return FALSE;
147 addr.Mode = AddrModeFlat;
148 addr.Offset = (DWORD_PTR)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
149 source_list_from_addr(&addr, 0);
150 return TRUE;
153 /******************************************************************
154 * stack_get_current_symbol
156 * Retrieves the symbol information for the current frame element
158 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
160 IMAGEHLP_STACK_FRAME ihsf;
161 DWORD64 disp;
163 if (!stack_get_current_frame(&ihsf)) return FALSE;
164 return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
165 &disp, symbol);
168 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
169 PVOID buffer, DWORD size, PDWORD written)
171 SIZE_T sz;
172 BOOL ret;
174 struct dbg_process* pcs = dbg_get_process_h(hProc);
175 if (!pcs) return FALSE;
176 ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
177 size, &sz);
178 if (written != NULL) *written = sz;
179 return ret;
182 /******************************************************************
183 * stack_fetch_frames
185 * Do a backtrace on the current thread
187 unsigned stack_fetch_frames(const CONTEXT* _ctx)
189 STACKFRAME64 sf;
190 unsigned nf = 0;
191 /* as native stackwalk can modify the context passed to it, simply copy
192 * it to avoid any damage
194 CONTEXT ctx = *_ctx;
195 BOOL ret;
197 HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
198 dbg_curr_thread->frames = NULL;
200 memset(&sf, 0, sizeof(sf));
201 be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_frame, &sf.AddrFrame);
202 be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_pc, &sf.AddrPC);
203 be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_stack, &sf.AddrStack);
205 /* don't confuse StackWalk by passing in inconsistent addresses */
206 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
208 sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
209 sf.AddrFrame.Mode = AddrModeFlat;
212 while ((ret = StackWalk64(be_cpu->machine, dbg_curr_process->handle,
213 dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
214 SymFunctionTableAccess64, SymGetModuleBase64, NULL)) ||
215 nf == 0) /* we always register first frame information */
217 dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
218 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
220 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
221 dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);
222 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
223 dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame);
224 dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
225 dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack);
226 dbg_curr_thread->frames[nf].context = ctx;
227 /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones
228 * if it has been modified by the call to StackWalk...
230 dbg_curr_thread->frames[nf].is_ctx_valid =
231 (nf == 0 ||
232 (dbg_curr_thread->frames[nf - 1].is_ctx_valid &&
233 memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx))));
234 nf++;
235 /* bail if:
236 * - we've (probably) gotten ourselves into an infinite loop,
237 * - or StackWalk failed on first frame
239 if (nf > 200 || !ret) break;
241 dbg_curr_thread->curr_frame = -1;
242 dbg_curr_thread->num_frames = nf;
243 stack_set_frame_internal(0);
244 return nf;
247 struct sym_enum
249 DWORD_PTR frame;
250 BOOL first;
253 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
255 struct sym_enum* se = user;
257 if (sym_info->Flags & SYMFLAG_PARAMETER)
259 if (!se->first) dbg_printf(", "); else se->first = FALSE;
260 symbol_print_local(sym_info, se->frame, FALSE);
262 return TRUE;
265 static void stack_print_addr_and_args(int nf)
267 char buffer[sizeof(SYMBOL_INFO) + 256];
268 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
269 IMAGEHLP_STACK_FRAME ihsf;
270 IMAGEHLP_LINE64 il;
271 IMAGEHLP_MODULE im;
272 DWORD64 disp64;
274 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
276 stack_get_frame(nf, &ihsf);
278 /* grab module where symbol is. If we don't have a module, we cannot print more */
279 im.SizeOfStruct = sizeof(im);
280 if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
281 return;
283 si->SizeOfStruct = sizeof(*si);
284 si->MaxNameLen = 256;
285 if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
287 struct sym_enum se;
288 DWORD disp;
290 dbg_printf(" %s", si->Name);
291 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
293 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
294 se.first = TRUE;
295 se.frame = ihsf.FrameOffset;
296 dbg_printf("(");
297 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
298 dbg_printf(")");
300 il.SizeOfStruct = sizeof(il);
301 if (SymGetLineFromAddr64(dbg_curr_process->handle,
302 ihsf.InstructionOffset, &disp, &il))
303 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
304 dbg_printf(" in %s", im.ModuleName);
306 else dbg_printf(" in %s (+0x%lx)",
307 im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
310 /******************************************************************
311 * backtrace
313 * Do a backtrace on the current thread
315 static void backtrace(void)
317 unsigned cf = dbg_curr_thread->curr_frame;
318 IMAGEHLP_STACK_FRAME ihsf;
320 dbg_printf("Backtrace:\n");
321 for (dbg_curr_thread->curr_frame = 0;
322 dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
323 dbg_curr_thread->curr_frame++)
325 dbg_printf("%s%d ",
326 (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
327 dbg_curr_thread->curr_frame);
328 stack_print_addr_and_args(dbg_curr_thread->curr_frame);
329 dbg_printf(" (");
330 print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
331 dbg_printf(")\n");
333 /* reset context to current stack frame */
334 dbg_curr_thread->curr_frame = cf;
335 if (!dbg_curr_thread->frames) return;
336 stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
337 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
340 /******************************************************************
341 * backtrace_tid
343 * Do a backtrace on a thread from its process and its identifier
344 * (preserves current thread and context information)
346 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
348 struct dbg_thread* thread = dbg_curr_thread;
350 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
351 dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
352 else
354 CONTEXT context;
356 dbg_curr_tid = dbg_curr_thread->tid;
357 memset(&context, 0, sizeof(context));
358 context.ContextFlags = CONTEXT_FULL;
359 if (SuspendThread(dbg_curr_thread->handle) != -1)
361 if (!GetThreadContext(dbg_curr_thread->handle, &context))
363 dbg_printf("Can't get context for thread %04x in current process\n",
364 tid);
366 else
368 stack_fetch_frames(&context);
369 backtrace();
371 ResumeThread(dbg_curr_thread->handle);
373 else dbg_printf("Can't suspend thread %04x in current process\n", tid);
375 dbg_curr_thread = thread;
376 dbg_curr_tid = thread ? thread->tid : 0;
379 /******************************************************************
380 * backtrace_all
382 * Do a backtrace on every running thread in the system (except the debugger)
383 * (preserves current process information)
385 static void backtrace_all(void)
387 struct dbg_process* process = dbg_curr_process;
388 struct dbg_thread* thread = dbg_curr_thread;
389 CONTEXT ctx = dbg_context;
390 DWORD cpid = dbg_curr_pid;
391 THREADENTRY32 entry;
392 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
394 if (snapshot == INVALID_HANDLE_VALUE)
396 dbg_printf("Unable to create toolhelp snapshot\n");
397 return;
400 entry.dwSize = sizeof(entry);
401 if (Thread32First(snapshot, &entry))
405 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
406 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
407 cpid != dbg_curr_pid)
408 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
410 if (entry.th32OwnerProcessID == cpid)
412 dbg_curr_process = process;
413 dbg_curr_pid = cpid;
415 else if (entry.th32OwnerProcessID != dbg_curr_pid)
417 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
419 dbg_printf("\nwarning: could not attach to %04x\n",
420 entry.th32OwnerProcessID);
421 continue;
423 dbg_curr_pid = dbg_curr_process->pid;
424 dbg_active_wait_for_first_exception();
427 dbg_printf("\nBacktracing for thread %04x in process %04lx (%s):\n",
428 entry.th32ThreadID, dbg_curr_pid,
429 dbg_W2A(dbg_curr_process->imageName, -1));
430 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
432 while (Thread32Next(snapshot, &entry));
434 if (dbg_curr_process && cpid != dbg_curr_pid)
435 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
437 CloseHandle(snapshot);
438 dbg_curr_process = process;
439 dbg_curr_pid = cpid;
440 dbg_curr_thread = thread;
441 dbg_curr_tid = thread ? thread->tid : 0;
442 dbg_context = ctx;
445 void stack_backtrace(DWORD tid)
447 /* backtrace every thread in every process except the debugger itself,
448 * invoking via "bt all"
450 if (tid == -1)
452 backtrace_all();
453 return;
456 if (!dbg_curr_process)
458 dbg_printf("You must be attached to a process to run this command.\n");
459 return;
462 if (tid == dbg_curr_tid)
464 backtrace();
466 else
468 backtrace_tid(dbg_curr_process, tid);