Fix small typo.
[wine/multimedia.git] / programs / winedbg / stack.c
blob26caf2be35e1c9c74466d4e90495b12f13b45d86
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 "winbase.h"
29 #include "wine/winbase16.h"
30 #include "wine/debug.h"
31 #include "tlhelp32.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static int nframe;
36 static IMAGEHLP_STACK_FRAME* frames = NULL;
38 /***********************************************************************
39 * stack_info
41 * Dump the top of the stack
43 void stack_info(void)
45 struct dbg_lvalue lvalue;
47 lvalue.cookie = 0;
48 lvalue.type.id = dbg_itype_none;
49 lvalue.type.module = 0;
51 /* FIXME: we assume stack grows the same way as on i386 */
52 if (!memory_get_current_stack(&lvalue.addr))
53 dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
55 dbg_printf("Stack dump:\n");
56 switch (lvalue.addr.Mode)
58 case AddrModeFlat: /* 32-bit mode */
59 case AddrMode1632: /* 32-bit mode */
60 memory_examine(&lvalue, 24, 'x');
61 break;
62 case AddrModeReal: /* 16-bit mode */
63 case AddrMode1616:
64 memory_examine(&lvalue, 24, 'w');
65 break;
69 int stack_set_frame(int newframe)
71 ADDRESS addr;
73 dbg_curr_frame = newframe;
74 if (dbg_curr_frame >= nframe) dbg_curr_frame = nframe - 1;
75 if (dbg_curr_frame < 0) dbg_curr_frame = 0;
77 addr.Mode = AddrModeFlat;
78 addr.Offset = frames[dbg_curr_frame].InstructionOffset;
79 source_list_from_addr(&addr, 0);
80 return TRUE;
83 BOOL stack_get_frame(SYMBOL_INFO* symbol, IMAGEHLP_STACK_FRAME* ihsf)
85 DWORD64 disp;
87 * If we don't have a valid backtrace, then just return.
89 if (frames == NULL) return FALSE;
92 * If we don't know what the current function is, then we also have
93 * nothing to report here.
95 if (!SymFromAddr(dbg_curr_process->handle, frames[dbg_curr_frame].InstructionOffset,
96 &disp, symbol))
97 return FALSE;
98 if (ihsf) *ihsf = frames[dbg_curr_frame];
100 return TRUE;
103 void stack_backtrace(DWORD tid, BOOL noisy)
105 STACKFRAME sf;
106 CONTEXT saved_dbg_context;
107 struct dbg_thread* thread;
108 unsigned nf;
110 if (tid == -1) /* backtrace every thread in every process except the debugger itself, invoking via "bt all" */
112 THREADENTRY32 entry;
113 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
115 if (snapshot == INVALID_HANDLE_VALUE)
117 dbg_printf("unable to create toolhelp snapshot\n");
118 return;
121 entry.dwSize = sizeof(entry);
123 if (!Thread32First(snapshot, &entry))
125 CloseHandle(snapshot);
126 return;
131 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
132 if (dbg_curr_process) dbg_detach_debuggee();
134 dbg_printf("\n");
135 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE, TRUE))
137 dbg_printf("\nwarning: could not attach to 0x%lx\n", entry.th32OwnerProcessID);
138 continue;
141 dbg_printf("Backtracing for thread 0x%lx in process 0x%lx (%s):\n", entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
143 stack_backtrace(entry.th32ThreadID, TRUE);
145 while (Thread32Next(snapshot, &entry));
147 if (dbg_curr_process) dbg_detach_debuggee();
148 CloseHandle(snapshot);
149 return;
152 if (!dbg_curr_process)
154 dbg_printf("You must be attached to a process to run this command.\n");
155 return;
158 saved_dbg_context = dbg_context; /* as we may modify dbg_context... */
159 if (tid == dbg_curr_tid)
161 thread = dbg_curr_thread;
162 HeapFree(GetProcessHeap(), 0, frames);
163 frames = NULL;
165 else
167 thread = dbg_get_thread(dbg_curr_process, tid);
168 if (!thread)
170 dbg_printf("Unknown thread id (0x%08lx) in current process\n", tid);
171 return;
173 memset(&dbg_context, 0, sizeof(dbg_context));
174 dbg_context.ContextFlags = CONTEXT_FULL;
175 if (SuspendThread(thread->handle) != -1)
177 if (!GetThreadContext(thread->handle, &dbg_context))
179 dbg_printf("Can't get context for thread 0x%lx in current process\n",
180 tid);
181 ResumeThread(thread->handle);
182 return;
185 else
187 dbg_printf("Can't suspend thread 0x%lx in current process\n", tid);
188 return;
192 nf = 0;
193 memset(&sf, 0, sizeof(sf));
194 memory_get_current_frame(&sf.AddrFrame);
195 memory_get_current_pc(&sf.AddrPC);
197 /* don't confuse StackWalk by passing in inconsistent addresses */
198 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
200 sf.AddrFrame.Offset = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
201 sf.AddrFrame.Mode = AddrModeFlat;
204 if (noisy) dbg_printf("Backtrace:\n");
205 while (StackWalk(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle,
206 thread->handle, &sf, &dbg_context, NULL, SymFunctionTableAccess,
207 SymGetModuleBase, NULL))
209 if (tid == dbg_curr_tid)
211 frames = dbg_heap_realloc(frames,
212 (nf + 1) * sizeof(IMAGEHLP_STACK_FRAME));
214 frames[nf].InstructionOffset = (unsigned long)memory_to_linear_addr(&sf.AddrPC);
215 frames[nf].FrameOffset = (unsigned long)memory_to_linear_addr(&sf.AddrFrame);
217 if (noisy)
219 dbg_printf("%s%d ",
220 (tid == dbg_curr_tid && nf == dbg_curr_frame ? "=>" : " "),
221 nf + 1);
222 print_addr_and_args(&sf.AddrPC, &sf.AddrFrame);
223 dbg_printf(" (");
224 print_bare_address(&sf.AddrFrame);
225 dbg_printf(")\n");
227 nf++;
228 /* we've probably gotten ourselves into an infinite loop so bail */
229 if (nf > 200)
230 break;
233 dbg_context = saved_dbg_context;
234 if (tid == dbg_curr_tid)
236 nframe = nf;
238 else
240 ResumeThread(thread->handle);