- all symbol information storage is now module relative, so we can
[wine/multimedia.git] / programs / winedbg / stack.c
blobab59ef64947a98733547fc83677dc3a446923db7
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <stdlib.h>
27 #include "debugger.h"
28 #include "stackframe.h"
29 #include "winbase.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
34 static int nframe;
35 static IMAGEHLP_STACK_FRAME* frames = NULL;
37 /***********************************************************************
38 * stack_info
40 * Dump the top of the stack
42 void stack_info(void)
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');
58 break;
59 case AddrModeReal: /* 16-bit mode */
60 case AddrMode1616:
61 memory_examine(&lvalue, 24, 'w');
62 break;
64 dbg_printf("\n");
67 int stack_set_frame(int newframe)
69 ADDRESS addr;
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);
78 return TRUE;
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,
93 NULL, symbol);
94 if (ihsf) *ihsf = frames[dbg_curr_frame];
96 return TRUE;
99 void stack_backtrace(DWORD tid, BOOL noisy)
101 STACKFRAME sf;
102 CONTEXT ctx;
103 struct dbg_thread* thread;
104 unsigned nf;
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);
111 frames = NULL;
113 else
115 thread = dbg_get_thread(dbg_curr_process, tid);
116 if (!thread)
118 dbg_printf("Unknown thread id (0x%08lx) in current process\n", tid);
119 return;
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",
128 tid);
129 return;
133 nf = 0;
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);
151 if (noisy)
153 dbg_printf("%s%d ",
154 (tid == dbg_curr_tid && nf == dbg_curr_frame ? "=>" : " "),
155 nf + 1);
156 print_addr_and_args(&sf.AddrPC, &sf.AddrFrame);
157 dbg_printf(" (");
158 print_bare_address(&sf.AddrFrame);
159 dbg_printf(")\n");
161 nf++;
164 if (tid == dbg_curr_tid)
166 nframe = nf;
168 else
170 ResumeThread(thread->handle);