Remove additional current process tracking in backtrace_all as it can
[wine/wine64.git] / programs / winedbg / stack.c
blobb080f0ba0d3e2f664a1a2882fdece039a33354c6
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 static int nframe;
37 static IMAGEHLP_STACK_FRAME* frames = NULL;
39 /***********************************************************************
40 * stack_info
42 * Dump the top of the stack
44 void stack_info(void)
46 struct dbg_lvalue lvalue;
48 lvalue.cookie = 0;
49 lvalue.type.id = dbg_itype_none;
50 lvalue.type.module = 0;
52 /* FIXME: we assume stack grows the same way as on i386 */
53 if (!memory_get_current_stack(&lvalue.addr))
54 dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
56 dbg_printf("Stack dump:\n");
57 switch (lvalue.addr.Mode)
59 case AddrModeFlat: /* 32-bit mode */
60 case AddrMode1632: /* 32-bit mode */
61 memory_examine(&lvalue, 24, 'x');
62 break;
63 case AddrModeReal: /* 16-bit mode */
64 case AddrMode1616:
65 memory_examine(&lvalue, 24, 'w');
66 break;
70 int stack_set_frame(int newframe)
72 ADDRESS addr;
74 dbg_curr_frame = newframe;
75 if (dbg_curr_frame >= nframe) dbg_curr_frame = nframe - 1;
76 if (dbg_curr_frame < 0) dbg_curr_frame = 0;
78 addr.Mode = AddrModeFlat;
79 addr.Offset = frames[dbg_curr_frame].InstructionOffset;
80 source_list_from_addr(&addr, 0);
81 return TRUE;
84 BOOL stack_get_frame(SYMBOL_INFO* symbol, IMAGEHLP_STACK_FRAME* ihsf)
86 DWORD64 disp;
88 * If we don't have a valid backtrace, then just return.
90 if (frames == NULL) return FALSE;
93 * If we don't know what the current function is, then we also have
94 * nothing to report here.
96 if (!SymFromAddr(dbg_curr_process->handle, frames[dbg_curr_frame].InstructionOffset,
97 &disp, symbol))
98 return FALSE;
99 if (ihsf) *ihsf = frames[dbg_curr_frame];
101 return TRUE;
104 /******************************************************************
105 * backtrace
107 * Do a backtrace on the the current thread
109 static unsigned backtrace(BOOL with_frames, BOOL noisy)
111 STACKFRAME sf;
112 unsigned nf = 0;
114 memset(&sf, 0, sizeof(sf));
115 memory_get_current_frame(&sf.AddrFrame);
116 memory_get_current_pc(&sf.AddrPC);
118 /* don't confuse StackWalk by passing in inconsistent addresses */
119 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
121 sf.AddrFrame.Offset = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
122 sf.AddrFrame.Mode = AddrModeFlat;
125 if (noisy) dbg_printf("Backtrace:\n");
126 while (StackWalk(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle,
127 dbg_curr_thread->handle, &sf, &dbg_context, NULL,
128 SymFunctionTableAccess, SymGetModuleBase, NULL))
130 if (with_frames)
132 frames = dbg_heap_realloc(frames,
133 (nf + 1) * sizeof(IMAGEHLP_STACK_FRAME));
135 frames[nf].InstructionOffset = (unsigned long)memory_to_linear_addr(&sf.AddrPC);
136 frames[nf].FrameOffset = (unsigned long)memory_to_linear_addr(&sf.AddrFrame);
138 if (noisy)
140 dbg_printf("%s%d ",
141 (with_frames && nf == dbg_curr_frame ? "=>" : " "),
142 nf + 1);
143 print_addr_and_args(&sf.AddrPC, &sf.AddrFrame);
144 dbg_printf(" (");
145 print_bare_address(&sf.AddrFrame);
146 dbg_printf(")\n");
148 nf++;
149 /* we've probably gotten ourselves into an infinite loop so bail */
150 if (nf > 200)
151 break;
153 return nf;
156 /******************************************************************
157 * backtrace_tid
159 * Do a backtrace on a thread from its process and its identifier
160 * (preserves current thread and context information)
162 static void backtrace_tid(struct dbg_process* pcs, DWORD tid, BOOL noisy)
164 struct dbg_thread* thread = dbg_curr_thread;
166 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
167 dbg_printf("Unknown thread id (0x%lx) in process (0x%lx)\n", tid, pcs->pid);
168 else
170 CONTEXT saved_ctx = dbg_context;
172 dbg_curr_tid = dbg_curr_thread->tid;
173 memset(&dbg_context, 0, sizeof(dbg_context));
174 dbg_context.ContextFlags = CONTEXT_FULL;
175 if (SuspendThread(dbg_curr_thread->handle) != -1)
177 if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
179 dbg_printf("Can't get context for thread 0x%lx in current process\n",
180 tid);
182 else backtrace(FALSE, noisy);
183 ResumeThread(dbg_curr_thread->handle);
185 else dbg_printf("Can't suspend thread 0x%lx in current process\n", tid);
186 dbg_context = saved_ctx;
188 dbg_curr_thread = thread;
189 dbg_curr_tid = thread ? thread->tid : 0;
192 /******************************************************************
193 * backtrace_all
195 * Do a backtrace on every running thread in the system (except the debugger)
196 * (preserves current process information)
198 static void backtrace_all(void)
200 THREADENTRY32 entry;
201 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
203 if (snapshot == INVALID_HANDLE_VALUE)
205 dbg_printf("Unable to create toolhelp snapshot\n");
206 return;
209 entry.dwSize = sizeof(entry);
210 if (Thread32First(snapshot, &entry))
214 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
215 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID)
216 dbg_detach_debuggee();
218 if (entry.th32OwnerProcessID != dbg_curr_pid)
220 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE, TRUE))
222 dbg_printf("\nwarning: could not attach to 0x%lx\n",
223 entry.th32OwnerProcessID);
224 continue;
226 dbg_curr_pid = dbg_curr_process->pid;
229 dbg_printf("\nBacktracing for thread 0x%lx in process 0x%lx (%s):\n",
230 entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
231 backtrace_tid(dbg_curr_process, entry.th32ThreadID, TRUE);
233 while (Thread32Next(snapshot, &entry));
235 if (dbg_curr_process)
236 dbg_detach_debuggee();
238 CloseHandle(snapshot);
241 void stack_backtrace(DWORD tid, BOOL noisy)
243 /* backtrace every thread in every process except the debugger itself,
244 * invoking via "bt all"
246 if (tid == -1) return backtrace_all();
248 if (!dbg_curr_process)
250 dbg_printf("You must be attached to a process to run this command.\n");
251 return;
254 if (tid == dbg_curr_tid)
256 HeapFree(GetProcessHeap(), 0, frames);
257 frames = NULL;
258 nframe = backtrace(TRUE, noisy);
260 else
262 backtrace_tid(dbg_curr_process, tid, noisy);