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
);
36 /***********************************************************************
39 * Dump the top of the stack
43 struct dbg_lvalue lvalue
;
46 lvalue
.type
.id
= dbg_itype_segptr
;
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');
60 case AddrModeReal
: /* 16-bit mode */
62 memory_examine(&lvalue
, 24, 'w');
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;
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
);
85 static BOOL
stack_get_frame(int nf
, IMAGEHLP_STACK_FRAME
* ihsf
)
87 memset(ihsf
, 0, sizeof(*ihsf
));
88 ihsf
->InstructionOffset
= (unsigned long)memory_to_linear_addr(&dbg_curr_thread
->frames
[nf
].addr_pc
);
89 ihsf
->FrameOffset
= (unsigned long)memory_to_linear_addr(&dbg_curr_thread
->frames
[nf
].addr_frame
);
93 BOOL
stack_get_current_frame(IMAGEHLP_STACK_FRAME
* ihsf
)
96 * If we don't have a valid backtrace, then just return.
98 if (dbg_curr_thread
->frames
== NULL
) return FALSE
;
99 return stack_get_frame(dbg_curr_thread
->curr_frame
, ihsf
);
102 BOOL
stack_set_frame(int newframe
)
105 if (!stack_set_frame_internal(newframe
)) return FALSE
;
106 addr
.Mode
= AddrModeFlat
;
107 addr
.Offset
= (unsigned long)memory_to_linear_addr(&dbg_curr_thread
->frames
[dbg_curr_thread
->curr_frame
].addr_pc
);
108 source_list_from_addr(&addr
, 0);
112 /******************************************************************
113 * stack_get_current_symbol
115 * Retrieves the symbol information for the current frame element
117 BOOL
stack_get_current_symbol(SYMBOL_INFO
* symbol
)
119 IMAGEHLP_STACK_FRAME ihsf
;
122 if (!stack_get_current_frame(&ihsf
)) return FALSE
;
123 return SymFromAddr(dbg_curr_process
->handle
, ihsf
.InstructionOffset
,
127 static BOOL CALLBACK
stack_read_mem(HANDLE hProc
, DWORD addr
,
128 PVOID buffer
, DWORD size
, PDWORD written
)
130 struct dbg_process
* pcs
= dbg_get_process_h(hProc
);
131 if (!pcs
) return FALSE
;
132 return pcs
->process_io
->read(hProc
, (const void*)addr
, buffer
, size
, written
);
135 /******************************************************************
138 * Do a backtrace on the the current thread
140 unsigned stack_fetch_frames(void)
145 HeapFree(GetProcessHeap(), 0, dbg_curr_thread
->frames
);
146 dbg_curr_thread
->frames
= NULL
;
148 memset(&sf
, 0, sizeof(sf
));
149 memory_get_current_frame(&sf
.AddrFrame
);
150 memory_get_current_pc(&sf
.AddrPC
);
152 /* don't confuse StackWalk by passing in inconsistent addresses */
153 if ((sf
.AddrPC
.Mode
== AddrModeFlat
) && (sf
.AddrFrame
.Mode
!= AddrModeFlat
))
155 sf
.AddrFrame
.Offset
= (DWORD
)memory_to_linear_addr(&sf
.AddrFrame
);
156 sf
.AddrFrame
.Mode
= AddrModeFlat
;
159 while (StackWalk(IMAGE_FILE_MACHINE_I386
, dbg_curr_process
->handle
,
160 dbg_curr_thread
->handle
, &sf
, &dbg_context
, stack_read_mem
,
161 SymFunctionTableAccess
, SymGetModuleBase
, NULL
))
163 dbg_curr_thread
->frames
= dbg_heap_realloc(dbg_curr_thread
->frames
,
164 (nf
+ 1) * sizeof(dbg_curr_thread
->frames
[0]));
166 dbg_curr_thread
->frames
[nf
].addr_pc
= sf
.AddrPC
;
167 dbg_curr_thread
->frames
[nf
].addr_frame
= sf
.AddrFrame
;
169 /* we've probably gotten ourselves into an infinite loop so bail */
172 dbg_curr_thread
->curr_frame
= -1;
173 dbg_curr_thread
->num_frames
= nf
;
174 stack_set_frame_internal(0);
184 static BOOL WINAPI
sym_enum_cb(SYMBOL_INFO
* sym_info
, ULONG size
, void* user
)
186 struct sym_enum
* se
= (struct sym_enum
*)user
;
189 if (sym_info
->Flags
& SYMFLAG_PARAMETER
)
191 if (se
->tmp
[0]) strcat(se
->tmp
, ", ");
193 if (sym_info
->Flags
& SYMFLAG_REGREL
)
196 DWORD addr
= se
->frame
+ sym_info
->Address
;
198 if (!dbg_read_memory((char*)addr
, &val
, sizeof(val
)))
199 snprintf(tmp
, sizeof(tmp
), "<*** cannot read at 0x%lx ***>", addr
);
201 snprintf(tmp
, sizeof(tmp
), "0x%x", val
);
203 else if (sym_info
->Flags
& SYMFLAG_REGISTER
)
207 if (memory_get_register(sym_info
->Register
, &pval
, tmp
, sizeof(tmp
)))
208 snprintf(tmp
, sizeof(tmp
), "0x%lx", *pval
);
210 sprintf(se
->tmp
+ strlen(se
->tmp
), "%s=%s", sym_info
->Name
, tmp
);
215 static void stack_print_addr_and_args(int nf
)
217 char buffer
[sizeof(SYMBOL_INFO
) + 256];
218 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
219 IMAGEHLP_STACK_FRAME ihsf
;
224 print_bare_address(&dbg_curr_thread
->frames
[nf
].addr_pc
);
226 stack_get_frame(nf
, &ihsf
);
228 /* grab module where symbol is. If we don't have a module, we cannot print more */
229 im
.SizeOfStruct
= sizeof(im
);
230 if (!SymGetModuleInfo(dbg_curr_process
->handle
, ihsf
.InstructionOffset
, &im
))
233 si
->SizeOfStruct
= sizeof(*si
);
234 si
->MaxNameLen
= 256;
235 if (SymFromAddr(dbg_curr_process
->handle
, ihsf
.InstructionOffset
, &disp64
, si
))
241 dbg_printf(" %s", si
->Name
);
242 if (disp64
) dbg_printf("+0x%lx", (DWORD_PTR
)disp64
);
244 SymSetContext(dbg_curr_process
->handle
, &ihsf
, NULL
);
246 se
.frame
= ihsf
.FrameOffset
;
248 SymEnumSymbols(dbg_curr_process
->handle
, 0, NULL
, sym_enum_cb
, &se
);
249 if (tmp
[0]) dbg_printf("(%s)", tmp
);
251 il
.SizeOfStruct
= sizeof(il
);
252 if (SymGetLineFromAddr(dbg_curr_process
->handle
, ihsf
.InstructionOffset
,
254 dbg_printf(" [%s:%lu]", il
.FileName
, il
.LineNumber
);
255 dbg_printf(" in %s", im
.ModuleName
);
257 else dbg_printf(" in %s (+0x%lx)",
258 im
.ModuleName
, (DWORD_PTR
)(ihsf
.InstructionOffset
- im
.BaseOfImage
));
261 /******************************************************************
264 * Do a backtrace on the the current thread
266 static void backtrace(void)
268 unsigned cf
= dbg_curr_thread
->curr_frame
;
269 IMAGEHLP_STACK_FRAME ihsf
;
271 dbg_printf("Backtrace:\n");
272 for (dbg_curr_thread
->curr_frame
= 0;
273 dbg_curr_thread
->curr_frame
< dbg_curr_thread
->num_frames
;
274 dbg_curr_thread
->curr_frame
++)
277 (cf
== dbg_curr_thread
->curr_frame
? "=>" : " "),
278 dbg_curr_thread
->curr_frame
+ 1);
279 stack_print_addr_and_args(dbg_curr_thread
->curr_frame
);
281 print_bare_address(&dbg_curr_thread
->frames
[dbg_curr_thread
->curr_frame
].addr_pc
);
284 /* reset context to current stack frame */
285 dbg_curr_thread
->curr_frame
= cf
;
286 stack_get_frame(dbg_curr_thread
->curr_frame
, &ihsf
);
287 SymSetContext(dbg_curr_process
->handle
, &ihsf
, NULL
);
290 /******************************************************************
293 * Do a backtrace on a thread from its process and its identifier
294 * (preserves current thread and context information)
296 static void backtrace_tid(struct dbg_process
* pcs
, DWORD tid
)
298 struct dbg_thread
* thread
= dbg_curr_thread
;
300 if (!(dbg_curr_thread
= dbg_get_thread(pcs
, tid
)))
301 dbg_printf("Unknown thread id (0x%lx) in process (0x%lx)\n", tid
, pcs
->pid
);
304 CONTEXT saved_ctx
= dbg_context
;
306 dbg_curr_tid
= dbg_curr_thread
->tid
;
307 memset(&dbg_context
, 0, sizeof(dbg_context
));
308 dbg_context
.ContextFlags
= CONTEXT_FULL
;
309 if (SuspendThread(dbg_curr_thread
->handle
) != -1)
311 if (!GetThreadContext(dbg_curr_thread
->handle
, &dbg_context
))
313 dbg_printf("Can't get context for thread 0x%lx in current process\n",
317 ResumeThread(dbg_curr_thread
->handle
);
319 else dbg_printf("Can't suspend thread 0x%lx in current process\n", tid
);
320 dbg_context
= saved_ctx
;
322 dbg_curr_thread
= thread
;
323 dbg_curr_tid
= thread
? thread
->tid
: 0;
326 /******************************************************************
329 * Do a backtrace on every running thread in the system (except the debugger)
330 * (preserves current process information)
332 static void backtrace_all(void)
335 HANDLE snapshot
= CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD
, 0);
337 if (snapshot
== INVALID_HANDLE_VALUE
)
339 dbg_printf("Unable to create toolhelp snapshot\n");
343 entry
.dwSize
= sizeof(entry
);
344 if (Thread32First(snapshot
, &entry
))
348 if (entry
.th32OwnerProcessID
== GetCurrentProcessId()) continue;
349 if (dbg_curr_process
&& dbg_curr_pid
!= entry
.th32OwnerProcessID
)
350 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, FALSE
);
352 if (entry
.th32OwnerProcessID
!= dbg_curr_pid
)
354 if (!dbg_attach_debuggee(entry
.th32OwnerProcessID
, FALSE
, TRUE
))
356 dbg_printf("\nwarning: could not attach to 0x%lx\n",
357 entry
.th32OwnerProcessID
);
360 dbg_curr_pid
= dbg_curr_process
->pid
;
363 dbg_printf("\nBacktracing for thread 0x%lx in process 0x%lx (%s):\n",
364 entry
.th32ThreadID
, dbg_curr_pid
, dbg_curr_process
->imageName
);
365 backtrace_tid(dbg_curr_process
, entry
.th32ThreadID
);
367 while (Thread32Next(snapshot
, &entry
));
369 if (dbg_curr_process
)
370 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, FALSE
);
372 CloseHandle(snapshot
);
375 void stack_backtrace(DWORD tid
)
377 /* backtrace every thread in every process except the debugger itself,
378 * invoking via "bt all"
380 if (tid
== -1) return backtrace_all();
382 if (!dbg_curr_process
)
384 dbg_printf("You must be attached to a process to run this command.\n");
388 if (tid
== dbg_curr_tid
)
394 backtrace_tid(dbg_curr_process
, tid
);