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
30 #include "wine/winbase16.h"
31 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
37 static IMAGEHLP_STACK_FRAME
* frames
= NULL
;
39 /***********************************************************************
42 * Dump the top of the stack
46 struct dbg_lvalue lvalue
;
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');
63 case AddrModeReal
: /* 16-bit mode */
65 memory_examine(&lvalue
, 24, 'w');
70 int stack_set_frame(int newframe
)
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);
84 BOOL
stack_get_frame(SYMBOL_INFO
* symbol
, IMAGEHLP_STACK_FRAME
* ihsf
)
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
,
99 if (ihsf
) *ihsf
= frames
[dbg_curr_frame
];
104 /******************************************************************
107 * Do a backtrace on the the current thread
109 static unsigned backtrace(BOOL with_frames
, BOOL noisy
)
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
))
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
);
141 (with_frames
&& nf
== dbg_curr_frame
? "=>" : " "),
143 print_addr_and_args(&sf
.AddrPC
, &sf
.AddrFrame
);
145 print_bare_address(&sf
.AddrFrame
);
149 /* we've probably gotten ourselves into an infinite loop so bail */
156 /******************************************************************
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
);
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",
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 /******************************************************************
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)
201 HANDLE snapshot
= CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD
, 0);
203 if (snapshot
== INVALID_HANDLE_VALUE
)
205 dbg_printf("Unable to create toolhelp snapshot\n");
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
);
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");
254 if (tid
== dbg_curr_tid
)
256 HeapFree(GetProcessHeap(), 0, frames
);
258 nframe
= backtrace(TRUE
, noisy
);
262 backtrace_tid(dbg_curr_process
, tid
, noisy
);