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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/winbase16.h"
33 /***********************************************************************
36 * Dump the top of the stack
40 struct dbg_lvalue lvalue
;
43 lvalue
.type
.id
= dbg_itype_segptr
;
44 lvalue
.type
.module
= 0;
46 /* FIXME: we assume stack grows the same way as on i386 */
47 if (!memory_get_current_stack(&lvalue
.addr
))
48 dbg_printf("Bad segment (%d)\n", lvalue
.addr
.Segment
);
50 dbg_printf("Stack dump:\n");
51 switch (lvalue
.addr
.Mode
)
53 case AddrModeFlat
: /* 32-bit mode */
54 case AddrMode1632
: /* 32-bit mode */
55 memory_examine(&lvalue
, 24, 'x');
57 case AddrModeReal
: /* 16-bit mode */
59 memory_examine(&lvalue
, 24, 'w');
64 static BOOL
stack_set_frame_internal(int newframe
)
66 if (newframe
>= dbg_curr_thread
->num_frames
)
67 newframe
= dbg_curr_thread
->num_frames
- 1;
71 if (dbg_curr_thread
->curr_frame
!= newframe
)
73 IMAGEHLP_STACK_FRAME ihsf
;
75 dbg_curr_thread
->curr_frame
= newframe
;
76 stack_get_current_frame(&ihsf
);
77 SymSetContext(dbg_curr_process
->handle
, &ihsf
, NULL
);
82 static BOOL
stack_get_frame(int nf
, IMAGEHLP_STACK_FRAME
* ihsf
)
84 memset(ihsf
, 0, sizeof(*ihsf
));
85 ihsf
->InstructionOffset
= (unsigned long)memory_to_linear_addr(&dbg_curr_thread
->frames
[nf
].addr_pc
);
86 ihsf
->FrameOffset
= (unsigned long)memory_to_linear_addr(&dbg_curr_thread
->frames
[nf
].addr_frame
);
90 BOOL
stack_get_current_frame(IMAGEHLP_STACK_FRAME
* ihsf
)
93 * If we don't have a valid backtrace, then just return.
95 if (dbg_curr_thread
->frames
== NULL
) return FALSE
;
96 return stack_get_frame(dbg_curr_thread
->curr_frame
, ihsf
);
99 BOOL
stack_set_frame(int newframe
)
102 if (!stack_set_frame_internal(newframe
)) return FALSE
;
103 addr
.Mode
= AddrModeFlat
;
104 addr
.Offset
= (unsigned long)memory_to_linear_addr(&dbg_curr_thread
->frames
[dbg_curr_thread
->curr_frame
].addr_pc
);
105 source_list_from_addr(&addr
, 0);
109 /******************************************************************
110 * stack_get_current_symbol
112 * Retrieves the symbol information for the current frame element
114 BOOL
stack_get_current_symbol(SYMBOL_INFO
* symbol
)
116 IMAGEHLP_STACK_FRAME ihsf
;
119 if (!stack_get_current_frame(&ihsf
)) return FALSE
;
120 return SymFromAddr(dbg_curr_process
->handle
, ihsf
.InstructionOffset
,
124 static BOOL CALLBACK
stack_read_mem(HANDLE hProc
, DWORD64 addr
,
125 PVOID buffer
, DWORD size
, PDWORD written
)
130 struct dbg_process
* pcs
= dbg_get_process_h(hProc
);
131 if (!pcs
) return FALSE
;
132 ret
= pcs
->process_io
->read(hProc
, (const void*)(DWORD_PTR
)addr
, buffer
,
134 if (written
!= NULL
) *written
= sz
;
138 /******************************************************************
141 * Do a backtrace on the the current thread
143 unsigned stack_fetch_frames(void)
147 /* as native stackwalk can modify the context passed to it, simply copy
148 * it to avoid any damage
150 CONTEXT ctx
= dbg_context
;
152 HeapFree(GetProcessHeap(), 0, dbg_curr_thread
->frames
);
153 dbg_curr_thread
->frames
= NULL
;
155 memset(&sf
, 0, sizeof(sf
));
156 memory_get_current_frame(&sf
.AddrFrame
);
157 memory_get_current_pc(&sf
.AddrPC
);
159 /* don't confuse StackWalk by passing in inconsistent addresses */
160 if ((sf
.AddrPC
.Mode
== AddrModeFlat
) && (sf
.AddrFrame
.Mode
!= AddrModeFlat
))
162 sf
.AddrFrame
.Offset
= (DWORD
)memory_to_linear_addr(&sf
.AddrFrame
);
163 sf
.AddrFrame
.Mode
= AddrModeFlat
;
166 while (StackWalk64(IMAGE_FILE_MACHINE_I386
, dbg_curr_process
->handle
,
167 dbg_curr_thread
->handle
, &sf
, &ctx
, stack_read_mem
,
168 SymFunctionTableAccess64
, SymGetModuleBase64
, NULL
))
170 dbg_curr_thread
->frames
= dbg_heap_realloc(dbg_curr_thread
->frames
,
171 (nf
+ 1) * sizeof(dbg_curr_thread
->frames
[0]));
173 dbg_curr_thread
->frames
[nf
].addr_pc
= sf
.AddrPC
;
174 dbg_curr_thread
->frames
[nf
].addr_frame
= sf
.AddrFrame
;
176 /* we've probably gotten ourselves into an infinite loop so bail */
179 dbg_curr_thread
->curr_frame
= -1;
180 dbg_curr_thread
->num_frames
= nf
;
181 stack_set_frame_internal(0);
191 static BOOL WINAPI
sym_enum_cb(SYMBOL_INFO
* sym_info
, ULONG size
, void* user
)
193 struct sym_enum
* se
= (struct sym_enum
*)user
;
196 if (sym_info
->Flags
& SYMFLAG_PARAMETER
)
198 if (se
->tmp
[0]) strcat(se
->tmp
, ", ");
200 if (sym_info
->Flags
& SYMFLAG_REGREL
)
203 DWORD addr
= se
->frame
+ sym_info
->Address
;
205 if (!dbg_read_memory((char*)addr
, &val
, sizeof(val
)))
206 snprintf(tmp
, sizeof(tmp
), "<*** cannot read at 0x%lx ***>", addr
);
208 snprintf(tmp
, sizeof(tmp
), "0x%x", val
);
210 else if (sym_info
->Flags
& SYMFLAG_REGISTER
)
214 if (memory_get_register(sym_info
->Register
, &pval
, tmp
, sizeof(tmp
)))
215 snprintf(tmp
, sizeof(tmp
), "0x%lx", *pval
);
217 sprintf(se
->tmp
+ strlen(se
->tmp
), "%s=%s", sym_info
->Name
, tmp
);
222 static void stack_print_addr_and_args(int nf
)
224 char buffer
[sizeof(SYMBOL_INFO
) + 256];
225 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
226 IMAGEHLP_STACK_FRAME ihsf
;
231 print_bare_address(&dbg_curr_thread
->frames
[nf
].addr_pc
);
233 stack_get_frame(nf
, &ihsf
);
235 /* grab module where symbol is. If we don't have a module, we cannot print more */
236 im
.SizeOfStruct
= sizeof(im
);
237 if (!SymGetModuleInfo(dbg_curr_process
->handle
, ihsf
.InstructionOffset
, &im
))
240 si
->SizeOfStruct
= sizeof(*si
);
241 si
->MaxNameLen
= 256;
242 if (SymFromAddr(dbg_curr_process
->handle
, ihsf
.InstructionOffset
, &disp64
, si
))
248 dbg_printf(" %s", si
->Name
);
249 if (disp64
) dbg_printf("+0x%lx", (DWORD_PTR
)disp64
);
251 SymSetContext(dbg_curr_process
->handle
, &ihsf
, NULL
);
253 se
.frame
= ihsf
.FrameOffset
;
255 SymEnumSymbols(dbg_curr_process
->handle
, 0, NULL
, sym_enum_cb
, &se
);
256 if (tmp
[0]) dbg_printf("(%s)", tmp
);
258 il
.SizeOfStruct
= sizeof(il
);
259 if (SymGetLineFromAddr(dbg_curr_process
->handle
, ihsf
.InstructionOffset
,
261 dbg_printf(" [%s:%lu]", il
.FileName
, il
.LineNumber
);
262 dbg_printf(" in %s", im
.ModuleName
);
264 else dbg_printf(" in %s (+0x%lx)",
265 im
.ModuleName
, (DWORD_PTR
)(ihsf
.InstructionOffset
- im
.BaseOfImage
));
268 /******************************************************************
271 * Do a backtrace on the the current thread
273 static void backtrace(void)
275 unsigned cf
= dbg_curr_thread
->curr_frame
;
276 IMAGEHLP_STACK_FRAME ihsf
;
278 dbg_printf("Backtrace:\n");
279 for (dbg_curr_thread
->curr_frame
= 0;
280 dbg_curr_thread
->curr_frame
< dbg_curr_thread
->num_frames
;
281 dbg_curr_thread
->curr_frame
++)
284 (cf
== dbg_curr_thread
->curr_frame
? "=>" : " "),
285 dbg_curr_thread
->curr_frame
+ 1);
286 stack_print_addr_and_args(dbg_curr_thread
->curr_frame
);
288 print_bare_address(&dbg_curr_thread
->frames
[dbg_curr_thread
->curr_frame
].addr_pc
);
291 /* reset context to current stack frame */
292 dbg_curr_thread
->curr_frame
= cf
;
293 if (!dbg_curr_thread
->frames
) return;
294 stack_get_frame(dbg_curr_thread
->curr_frame
, &ihsf
);
295 SymSetContext(dbg_curr_process
->handle
, &ihsf
, NULL
);
298 /******************************************************************
301 * Do a backtrace on a thread from its process and its identifier
302 * (preserves current thread and context information)
304 static void backtrace_tid(struct dbg_process
* pcs
, DWORD tid
)
306 struct dbg_thread
* thread
= dbg_curr_thread
;
308 if (!(dbg_curr_thread
= dbg_get_thread(pcs
, tid
)))
309 dbg_printf("Unknown thread id (0x%lx) in process (0x%lx)\n", tid
, pcs
->pid
);
312 CONTEXT saved_ctx
= dbg_context
;
314 dbg_curr_tid
= dbg_curr_thread
->tid
;
315 memset(&dbg_context
, 0, sizeof(dbg_context
));
316 dbg_context
.ContextFlags
= CONTEXT_FULL
;
317 if (SuspendThread(dbg_curr_thread
->handle
) != -1)
319 if (!GetThreadContext(dbg_curr_thread
->handle
, &dbg_context
))
321 dbg_printf("Can't get context for thread 0x%lx in current process\n",
326 stack_fetch_frames();
329 ResumeThread(dbg_curr_thread
->handle
);
331 else dbg_printf("Can't suspend thread 0x%lx in current process\n", tid
);
332 dbg_context
= saved_ctx
;
334 dbg_curr_thread
= thread
;
335 dbg_curr_tid
= thread
? thread
->tid
: 0;
338 /******************************************************************
341 * Do a backtrace on every running thread in the system (except the debugger)
342 * (preserves current process information)
344 static void backtrace_all(void)
346 struct dbg_process
* process
= dbg_curr_process
;
348 HANDLE snapshot
= CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD
, 0);
350 if (snapshot
== INVALID_HANDLE_VALUE
)
352 dbg_printf("Unable to create toolhelp snapshot\n");
356 entry
.dwSize
= sizeof(entry
);
357 if (Thread32First(snapshot
, &entry
))
361 if (entry
.th32OwnerProcessID
== GetCurrentProcessId()) continue;
362 if (dbg_curr_process
&& dbg_curr_pid
!= entry
.th32OwnerProcessID
)
363 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, FALSE
);
365 if (entry
.th32OwnerProcessID
!= dbg_curr_pid
)
367 if (!dbg_attach_debuggee(entry
.th32OwnerProcessID
, FALSE
))
369 dbg_printf("\nwarning: could not attach to 0x%lx\n",
370 entry
.th32OwnerProcessID
);
373 dbg_curr_pid
= dbg_curr_process
->pid
;
374 dbg_active_wait_for_first_exception();
377 dbg_printf("\nBacktracing for thread 0x%lx in process 0x%lx (%s):\n",
378 entry
.th32ThreadID
, dbg_curr_pid
, dbg_curr_process
->imageName
);
379 backtrace_tid(dbg_curr_process
, entry
.th32ThreadID
);
381 while (Thread32Next(snapshot
, &entry
));
383 if (dbg_curr_process
)
384 dbg_curr_process
->process_io
->close_process(dbg_curr_process
, FALSE
);
386 CloseHandle(snapshot
);
387 dbg_curr_process
= process
;
388 dbg_curr_pid
= process
? process
->pid
: 0;
391 void stack_backtrace(DWORD tid
)
393 /* backtrace every thread in every process except the debugger itself,
394 * invoking via "bt all"
396 if (tid
== -1) return backtrace_all();
398 if (!dbg_curr_process
)
400 dbg_printf("You must be attached to a process to run this command.\n");
404 if (tid
== dbg_curr_tid
)
410 backtrace_tid(dbg_curr_process
, tid
);