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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "stackframe.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static IMAGEHLP_STACK_FRAME* frames = NULL;
37 /***********************************************************************
40 * Dump the top of the stack
44 struct dbg_lvalue lvalue;
46 lvalue.typeid = dbg_itype_none;
47 lvalue.cookie = DLV_TARGET;
48 /* FIXME: we assume stack grows the same way as on i386 */
49 if (!memory_get_current_stack(&lvalue.addr))
50 dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
52 dbg_printf("Stack dump:\n");
53 switch (lvalue.addr.Mode)
55 case AddrModeFlat: /* 32-bit mode */
56 case AddrMode1632: /* 32-bit mode */
57 memory_examine(&lvalue, 24, 'x');
59 case AddrModeReal: /* 16-bit mode */
61 memory_examine(&lvalue, 24, 'w');
67 int stack_set_frame(int newframe)
71 dbg_curr_frame = newframe;
72 if (dbg_curr_frame >= nframe) dbg_curr_frame = nframe - 1;
73 if (dbg_curr_frame < 0) dbg_curr_frame = 0;
75 addr.Mode = AddrModeFlat;
76 addr.Offset = frames[dbg_curr_frame].InstructionOffset;
77 source_list_from_addr(&addr, 0);
81 int stack_get_frame(SYMBOL_INFO* symbol, IMAGEHLP_STACK_FRAME* ihsf)
84 * If we don't have a valid backtrace, then just return.
86 if (frames == NULL) return FALSE;
89 * If we don't know what the current function is, then we also have
90 * nothing to report here.
92 SymFromAddr(dbg_curr_process->handle, frames[dbg_curr_frame].InstructionOffset,
94 if (ihsf) *ihsf = frames[dbg_curr_frame];
99 void stack_backtrace(DWORD tid, BOOL noisy)
103 struct dbg_thread* thread;
106 if (tid == dbg_curr_tid)
108 ctx = dbg_context; /* as StackWalk may modify it... */
109 thread = dbg_curr_thread;
110 if (frames) HeapFree(GetProcessHeap(), 0, frames);
115 thread = dbg_get_thread(dbg_curr_process, tid);
118 dbg_printf("Unknown thread id (0x%08lx) in current process\n", tid);
121 memset(&ctx, 0, sizeof(ctx));
122 ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_SEGMENTS;
124 if (SuspendThread(thread->handle) == -1 ||
125 !GetThreadContext(thread->handle, &ctx))
127 dbg_printf("Can't get context for thread 0x%lx in current process\n",
134 memset(&sf, 0, sizeof(sf));
135 memory_get_current_frame(&sf.AddrFrame);
136 memory_get_current_pc(&sf.AddrPC);
138 if (noisy) dbg_printf("Backtrace:\n");
139 while (StackWalk(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle,
140 thread->handle, &sf, &ctx, NULL, SymFunctionTableAccess,
141 SymGetModuleBase, NULL))
143 if (tid == dbg_curr_tid)
145 frames = dbg_heap_realloc(frames,
146 (nf + 1) * sizeof(IMAGEHLP_STACK_FRAME));
148 frames[nf].InstructionOffset = (unsigned long)memory_to_linear_addr(&sf.AddrPC);
149 frames[nf].FrameOffset = (unsigned long)memory_to_linear_addr(&sf.AddrFrame);
154 (tid == dbg_curr_tid && nf == dbg_curr_frame ? "=>" : " "),
156 print_addr_and_args(&sf.AddrPC, &sf.AddrFrame);
158 print_bare_address(&sf.AddrFrame);
164 if (tid == dbg_curr_tid)
170 ResumeThread(thread->handle);