Define new propsheet messages.
[wine/multimedia.git] / programs / winedbg / stack.c
blobd1181bbee9fb7126d20b16bf63cfe5a27c6df6aa
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"
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.typeid = dbg_itype_none;
48 lvalue.cookie = DLV_TARGET;
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;
65 dbg_printf("\n");
68 int stack_set_frame(int newframe)
70 ADDRESS addr;
72 dbg_curr_frame = newframe;
73 if (dbg_curr_frame >= nframe) dbg_curr_frame = nframe - 1;
74 if (dbg_curr_frame < 0) dbg_curr_frame = 0;
76 addr.Mode = AddrModeFlat;
77 addr.Offset = frames[dbg_curr_frame].InstructionOffset;
78 source_list_from_addr(&addr, 0);
79 return TRUE;
82 int stack_get_frame(SYMBOL_INFO* symbol, IMAGEHLP_STACK_FRAME* ihsf)
85 * If we don't have a valid backtrace, then just return.
87 if (frames == NULL) return FALSE;
90 * If we don't know what the current function is, then we also have
91 * nothing to report here.
93 SymFromAddr(dbg_curr_process->handle, frames[dbg_curr_frame].InstructionOffset,
94 NULL, symbol);
95 if (ihsf) *ihsf = frames[dbg_curr_frame];
97 return TRUE;
100 void stack_backtrace(DWORD tid, BOOL noisy)
102 STACKFRAME sf;
103 CONTEXT saved_dbg_context;
104 struct dbg_thread* thread;
105 unsigned nf;
107 if (tid == -1) /* backtrace every thread in every process except the debugger itself, invoking via "bt all" */
109 THREADENTRY32 entry;
110 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
112 if (snapshot == INVALID_HANDLE_VALUE)
114 dbg_printf("unable to create toolhelp snapshot\n");
115 return;
118 entry.dwSize = sizeof(entry);
120 if (!Thread32First(snapshot, &entry))
122 CloseHandle(snapshot);
123 return;
128 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
129 if (dbg_curr_process) dbg_detach_debuggee();
131 dbg_printf("\n");
132 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE, TRUE))
134 dbg_printf("\nwarning: could not attach to 0x%lx\n", entry.th32OwnerProcessID);
135 continue;
138 dbg_printf("Backtracing for thread 0x%lx in process 0x%lx (%s):\n", entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
140 stack_backtrace(entry.th32ThreadID, TRUE);
142 while (Thread32Next(snapshot, &entry));
144 if (dbg_curr_process) dbg_detach_debuggee();
145 CloseHandle(snapshot);
146 return;
149 if (!dbg_curr_process)
151 dbg_printf("You must be attached to a process to run this command.\n");
152 return;
155 saved_dbg_context = dbg_context; /* as we may modify dbg_context... */
156 if (tid == dbg_curr_tid)
158 thread = dbg_curr_thread;
159 if (frames) HeapFree(GetProcessHeap(), 0, frames);
160 frames = NULL;
162 else
164 thread = dbg_get_thread(dbg_curr_process, tid);
165 if (!thread)
167 dbg_printf("Unknown thread id (0x%08lx) in current process\n", tid);
168 return;
170 memset(&dbg_context, 0, sizeof(dbg_context));
171 dbg_context.ContextFlags = CONTEXT_FULL;
172 if (SuspendThread(thread->handle) != -1)
174 if (!GetThreadContext(thread->handle, &dbg_context))
176 dbg_printf("Can't get context for thread 0x%lx in current process\n",
177 tid);
178 ResumeThread(thread->handle);
179 return;
182 else
184 dbg_printf("Can't suspend thread 0x%lx in current process\n", tid);
185 return;
189 nf = 0;
190 memset(&sf, 0, sizeof(sf));
191 memory_get_current_frame(&sf.AddrFrame);
192 memory_get_current_pc(&sf.AddrPC);
194 if (noisy) dbg_printf("Backtrace:\n");
195 while (StackWalk(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle,
196 thread->handle, &sf, &dbg_context, NULL, SymFunctionTableAccess,
197 SymGetModuleBase, NULL))
199 if (tid == dbg_curr_tid)
201 frames = dbg_heap_realloc(frames,
202 (nf + 1) * sizeof(IMAGEHLP_STACK_FRAME));
204 frames[nf].InstructionOffset = (unsigned long)memory_to_linear_addr(&sf.AddrPC);
205 frames[nf].FrameOffset = (unsigned long)memory_to_linear_addr(&sf.AddrFrame);
207 if (noisy)
209 dbg_printf("%s%d ",
210 (tid == dbg_curr_tid && nf == dbg_curr_frame ? "=>" : " "),
211 nf + 1);
212 print_addr_and_args(&sf.AddrPC, &sf.AddrFrame);
213 dbg_printf(" (");
214 print_bare_address(&sf.AddrFrame);
215 dbg_printf(")\n");
217 nf++;
220 dbg_context = saved_dbg_context;
221 if (tid == dbg_curr_tid)
223 nframe = nf;
225 else
227 ResumeThread(thread->handle);