kernel: Documentation cleanups.
[wine/multimedia.git] / programs / winedbg / stack.c
blobb33cf603330bac82cd6ba72e0fc8e0a51e09b0f8
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>
26 #include <stdio.h>
28 #include "debugger.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "wine/debug.h"
32 #include "tlhelp32.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
36 /***********************************************************************
37 * stack_info
39 * Dump the top of the stack
41 void stack_info(void)
43 struct dbg_lvalue lvalue;
45 lvalue.cookie = 0;
46 lvalue.type.id = dbg_itype_none;
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 mode */
57 case AddrMode1632: /* 32-bit mode */
58 memory_examine(&lvalue, 24, 'x');
59 break;
60 case AddrModeReal: /* 16-bit mode */
61 case AddrMode1616:
62 memory_examine(&lvalue, 24, 'w');
63 break;
67 static BOOL stack_set_frame_internal(int newframe)
69 if (newframe >= dbg_curr_thread->num_frames)
70 newframe = dbg_curr_thread->num_frames - 1;
71 if (newframe < 0)
72 newframe = 0;
74 if (dbg_curr_thread->curr_frame != newframe)
76 IMAGEHLP_STACK_FRAME ihsf;
78 dbg_curr_thread->curr_frame = newframe;
79 stack_get_current_frame(&ihsf);
80 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
82 return TRUE;
85 static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
87 ihsf->InstructionOffset = (unsigned long)memory_to_linear_addr(&dbg_curr_thread->frames[nf].addr_pc);
88 ihsf->FrameOffset = (unsigned long)memory_to_linear_addr(&dbg_curr_thread->frames[nf].addr_frame);
89 return TRUE;
92 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
95 * If we don't have a valid backtrace, then just return.
97 if (dbg_curr_thread->frames == NULL) return FALSE;
98 return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
101 BOOL stack_set_frame(int newframe)
103 ADDRESS addr;
104 if (!stack_set_frame_internal(newframe)) return FALSE;
105 addr.Mode = AddrModeFlat;
106 addr.Offset = (unsigned long)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
107 source_list_from_addr(&addr, 0);
108 return TRUE;
111 /******************************************************************
112 * stack_get_current_symbol
114 * Retrieves the symbol information for the current frame element
116 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
118 IMAGEHLP_STACK_FRAME ihsf;
119 DWORD64 disp;
121 if (!stack_get_current_frame(&ihsf)) return FALSE;
122 return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
123 &disp, symbol);
125 /******************************************************************
126 * stack_fetch_frames
128 * Do a backtrace on the the current thread
130 unsigned stack_fetch_frames(void)
132 STACKFRAME sf;
133 unsigned nf = 0;
135 HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
136 dbg_curr_thread->frames = NULL;
138 memset(&sf, 0, sizeof(sf));
139 memory_get_current_frame(&sf.AddrFrame);
140 memory_get_current_pc(&sf.AddrPC);
142 /* don't confuse StackWalk by passing in inconsistent addresses */
143 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
145 sf.AddrFrame.Offset = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
146 sf.AddrFrame.Mode = AddrModeFlat;
149 while (StackWalk(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle,
150 dbg_curr_thread->handle, &sf, &dbg_context, NULL,
151 SymFunctionTableAccess, SymGetModuleBase, NULL))
153 dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
154 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
156 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
157 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
158 nf++;
159 /* we've probably gotten ourselves into an infinite loop so bail */
160 if (nf > 200) break;
162 dbg_curr_thread->curr_frame = -1;
163 dbg_curr_thread->num_frames = nf;
164 stack_set_frame_internal(0);
165 return nf;
168 struct sym_enum
170 char* tmp;
171 DWORD frame;
174 static BOOL WINAPI sym_enum_cb(SYMBOL_INFO* sym_info, ULONG size, void* user)
176 struct sym_enum* se = (struct sym_enum*)user;
177 DWORD addr;
178 unsigned val;
180 if ((sym_info->Flags & (SYMFLAG_PARAMETER|SYMFLAG_FRAMEREL)) == (SYMFLAG_PARAMETER|SYMFLAG_FRAMEREL))
182 if (se->tmp[0]) strcat(se->tmp, ", ");
183 addr = se->frame + sym_info->Address;
184 if (dbg_read_memory((char*)addr, &val, sizeof(val)))
185 sprintf(se->tmp + strlen(se->tmp), "%s=0x%x", sym_info->Name, val);
186 else
187 sprintf(se->tmp + strlen(se->tmp), "%s=<\?\?\?>", sym_info->Name);
189 return TRUE;
192 static void stack_print_addr_and_args(int nf)
194 char buffer[sizeof(SYMBOL_INFO) + 256];
195 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
196 IMAGEHLP_STACK_FRAME ihsf;
197 IMAGEHLP_LINE il;
198 IMAGEHLP_MODULE im;
199 DWORD64 disp64;
201 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
203 stack_get_frame(nf, &ihsf);
205 /* grab module where symbol is. If we don't have a module, we cannot print more */
206 im.SizeOfStruct = sizeof(im);
207 if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
208 return;
210 si->SizeOfStruct = sizeof(*si);
211 si->MaxNameLen = 256;
212 if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
214 struct sym_enum se;
215 char tmp[1024];
216 DWORD disp;
218 dbg_printf(" %s", si->Name);
219 if (disp) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
221 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
222 se.tmp = tmp;
223 se.frame = ihsf.FrameOffset;
224 tmp[0] = '\0';
225 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
226 if (tmp[0]) dbg_printf("(%s)", tmp);
228 il.SizeOfStruct = sizeof(il);
229 if (SymGetLineFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
230 &disp, &il))
231 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
232 dbg_printf(" in %s", im.ModuleName);
234 else dbg_printf(" in %s (+0x%lx)",
235 im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
238 /******************************************************************
239 * backtrace
241 * Do a backtrace on the the current thread
243 static unsigned backtrace(void)
245 unsigned nf = 0;
246 IMAGEHLP_STACK_FRAME ihsf;
248 dbg_printf("Backtrace:\n");
249 for (nf = 0; nf < dbg_curr_thread->num_frames; nf++)
251 dbg_printf("%s%d ",
252 (nf == dbg_curr_thread->curr_frame ? "=>" : " "), nf + 1);
253 stack_print_addr_and_args(nf);
254 dbg_printf(" (");
255 print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
256 dbg_printf(")\n");
258 /* reset context to current stack frame */
259 stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
260 SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
261 return nf;
264 /******************************************************************
265 * backtrace_tid
267 * Do a backtrace on a thread from its process and its identifier
268 * (preserves current thread and context information)
270 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
272 struct dbg_thread* thread = dbg_curr_thread;
274 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
275 dbg_printf("Unknown thread id (0x%lx) in process (0x%lx)\n", tid, pcs->pid);
276 else
278 CONTEXT saved_ctx = dbg_context;
280 dbg_curr_tid = dbg_curr_thread->tid;
281 memset(&dbg_context, 0, sizeof(dbg_context));
282 dbg_context.ContextFlags = CONTEXT_FULL;
283 if (SuspendThread(dbg_curr_thread->handle) != -1)
285 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
287 dbg_printf("Can't get context for thread 0x%lx in current process\n",
288 tid);
290 else backtrace();
291 ResumeThread(dbg_curr_thread->handle);
293 else dbg_printf("Can't suspend thread 0x%lx in current process\n", tid);
294 dbg_context = saved_ctx;
296 dbg_curr_thread = thread;
297 dbg_curr_tid = thread ? thread->tid : 0;
300 /******************************************************************
301 * backtrace_all
303 * Do a backtrace on every running thread in the system (except the debugger)
304 * (preserves current process information)
306 static void backtrace_all(void)
308 THREADENTRY32 entry;
309 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
311 if (snapshot == INVALID_HANDLE_VALUE)
313 dbg_printf("Unable to create toolhelp snapshot\n");
314 return;
317 entry.dwSize = sizeof(entry);
318 if (Thread32First(snapshot, &entry))
322 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
323 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID)
324 dbg_detach_debuggee();
326 if (entry.th32OwnerProcessID != dbg_curr_pid)
328 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE, TRUE))
330 dbg_printf("\nwarning: could not attach to 0x%lx\n",
331 entry.th32OwnerProcessID);
332 continue;
334 dbg_curr_pid = dbg_curr_process->pid;
337 dbg_printf("\nBacktracing for thread 0x%lx in process 0x%lx (%s):\n",
338 entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
339 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
341 while (Thread32Next(snapshot, &entry));
343 if (dbg_curr_process)
344 dbg_detach_debuggee();
346 CloseHandle(snapshot);
349 void stack_backtrace(DWORD tid)
351 /* backtrace every thread in every process except the debugger itself,
352 * invoking via "bt all"
354 if (tid == -1) return backtrace_all();
356 if (!dbg_curr_process)
358 dbg_printf("You must be attached to a process to run this command.\n");
359 return;
362 if (tid == dbg_curr_tid)
364 backtrace();
366 else
368 backtrace_tid(dbg_curr_process, tid);