oledb32/tests: Remove test_odbc_provider().
[wine.git] / programs / winedbg / stack.c
blob4acb5666079d1fe86ad54f440f61afd910f3fe09
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdlib.h>
24 #include <stdio.h>
26 #include "debugger.h"
27 #include "winbase.h"
28 #include "wine/winbase16.h"
29 #include "tlhelp32.h"
31 /***********************************************************************
32 * stack_info
34 * Dump the top of the stack. If len <= 0, a default length is used.
36 void stack_info(int len)
38 struct dbg_lvalue lvalue;
40 if(len <= 0)
41 len = 24;
42 init_lvalue(&lvalue, TRUE, 0);
43 lvalue.type.id = dbg_itype_segptr;
45 /* FIXME: we assume stack grows the same way as on i386 */
46 if (!memory_get_current_stack(&lvalue.addr))
47 dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
49 dbg_printf("Stack dump:\n");
50 switch (lvalue.addr.Mode)
52 case AddrModeFlat: /* 32-bit or 64-bit mode */
53 memory_examine(&lvalue, len, 'a');
54 break;
55 case AddrMode1632: /* 32-bit mode */
56 memory_examine(&lvalue, len, 'x');
57 break;
58 case AddrModeReal: /* 16-bit mode */
59 case AddrMode1616:
60 memory_examine(&lvalue, len, 'w');
61 break;
65 static BOOL stack_set_local_scope(void)
67 struct dbg_frame* frm = stack_get_thread_frame(dbg_curr_thread, dbg_curr_thread->curr_frame);
69 if (!frm) return FALSE;
70 /* if we're not the first frame, linear_pc is the return address
71 * after the call instruction (at least on most processors I know of).
72 * However, there are cases where this address is outside of the
73 * current function or inline site.
74 * This happens when the called function is marked <NO RETURN>, in which
75 * case the compiler can omit the epilog (gcc 4 does it).
76 * This happens also for inline sites, where the epilog (of the inline
77 * site) isn't present.
78 * Therefore, we decrement linear_pc in order to ensure that
79 * the considered address is really inside the current function or inline site.
81 return SymSetScopeFromInlineContext(dbg_curr_process->handle,
82 (dbg_curr_thread->curr_frame) ? frm->linear_pc - 1 : frm->linear_pc,
83 frm->inline_ctx);
86 static BOOL stack_set_frame_internal(int newframe)
88 if (newframe >= dbg_curr_thread->num_frames)
89 newframe = dbg_curr_thread->num_frames - 1;
90 if (newframe < 0)
91 newframe = 0;
93 if (dbg_curr_thread->curr_frame != newframe)
95 dbg_curr_thread->curr_frame = newframe;
96 stack_set_local_scope();
98 return TRUE;
101 BOOL stack_get_register_frame(const struct dbg_internal_var* div, struct dbg_lvalue* lvalue)
103 struct dbg_frame* currfrm = stack_get_curr_frame();
104 if (currfrm == NULL) return FALSE;
105 if (currfrm->is_ctx_valid)
106 init_lvalue_in_debugger(lvalue, 0, div->typeid,
107 (char*)&currfrm->context + (DWORD_PTR)div->pval);
108 else
110 enum be_cpu_addr kind;
111 DWORD itype = ADDRSIZE == 4 ? dbg_itype_unsigned_long32 : dbg_itype_unsigned_long64;
113 if (!dbg_curr_process->be_cpu->get_register_info(div->val, &kind)) return FALSE;
115 /* reuse some known registers directly out of stackwalk details */
116 switch (kind)
118 case be_cpu_addr_pc:
119 init_lvalue_in_debugger(lvalue, 0, itype, &currfrm->linear_pc);
120 break;
121 case be_cpu_addr_stack:
122 init_lvalue_in_debugger(lvalue, 0, itype, &currfrm->linear_stack);
123 break;
124 case be_cpu_addr_frame:
125 init_lvalue_in_debugger(lvalue, 0, itype, &currfrm->linear_frame);
126 break;
129 return TRUE;
132 BOOL stack_set_frame(int newframe)
134 ADDRESS64 addr;
135 if (!stack_set_frame_internal(newframe)) return FALSE;
136 addr.Mode = AddrModeFlat;
137 addr.Offset = (DWORD_PTR)memory_to_linear_addr(&stack_get_curr_frame()->addr_pc);
138 source_list_from_addr(&addr, 0);
139 return TRUE;
142 /******************************************************************
143 * stack_get_current_symbol
145 * Retrieves the symbol information for the current frame element
147 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
149 DWORD64 disp;
150 struct dbg_frame* frm = stack_get_curr_frame();
152 if (frm == NULL) return FALSE;
153 return SymFromInlineContext(dbg_curr_process->handle, frm->linear_pc, frm->inline_ctx, &disp, symbol);
156 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr,
157 PVOID buffer, DWORD size, PDWORD written)
159 SIZE_T sz;
160 BOOL ret;
162 struct dbg_process* pcs = dbg_get_process_h(hProc);
163 if (!pcs) return FALSE;
164 ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
165 size, &sz);
166 if (written != NULL) *written = sz;
167 return ret;
170 /******************************************************************
171 * stack_fetch_frames
173 * Do a backtrace on the current thread
175 unsigned stack_fetch_frames(const dbg_ctx_t* _ctx)
177 STACKFRAME_EX sf;
178 unsigned nf = 0;
179 /* as native stackwalk can modify the context passed to it, simply copy
180 * it to avoid any damage
182 dbg_ctx_t ctx = *_ctx;
183 BOOL ret;
185 free(dbg_curr_thread->frames);
186 dbg_curr_thread->frames = NULL;
188 memset(&sf, 0, sizeof(sf));
189 sf.StackFrameSize = sizeof(sf);
190 dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_frame, &sf.AddrFrame);
191 dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_pc, &sf.AddrPC);
192 dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_stack, &sf.AddrStack);
193 sf.InlineFrameContext = INLINE_FRAME_CONTEXT_INIT;
195 /* don't confuse StackWalk by passing in inconsistent addresses */
196 if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
198 sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame);
199 sf.AddrFrame.Mode = AddrModeFlat;
202 while ((ret = StackWalkEx(dbg_curr_process->be_cpu->machine, dbg_curr_process->handle,
203 dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
204 SymFunctionTableAccess64, SymGetModuleBase64, NULL, SYM_STKWALK_DEFAULT)) ||
205 nf == 0) /* we always register first frame information */
207 struct dbg_frame* new = realloc(dbg_curr_thread->frames,
208 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
209 if (!new) break;
210 dbg_curr_thread->frames = new;
212 dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
213 dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);
214 dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
215 dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame);
216 dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack;
217 dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack);
218 dbg_curr_thread->frames[nf].context = ctx;
219 dbg_curr_thread->frames[nf].inline_ctx = sf.InlineFrameContext;
220 /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones
221 * if it has been modified by the call to StackWalk...
223 dbg_curr_thread->frames[nf].is_ctx_valid =
224 (nf == 0 ||
225 (dbg_curr_thread->frames[nf - 1].is_ctx_valid &&
226 memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx))));
227 nf++;
228 /* bail if:
229 * - we've (probably) gotten ourselves into an infinite loop,
230 * - or StackWalk failed on first frame
232 if (nf > 200 || !ret) break;
234 dbg_curr_thread->curr_frame = -1;
235 dbg_curr_thread->num_frames = nf;
236 stack_set_frame_internal(0);
237 return nf;
240 struct sym_enum
242 DWORD_PTR frame;
243 BOOL first;
246 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
248 struct sym_enum* se = user;
250 if (sym_info->Flags & SYMFLAG_PARAMETER)
252 if (!se->first) dbg_printf(", "); else se->first = FALSE;
253 dbg_printf("%s=", sym_info->Name);
254 symbol_print_localvalue(sym_info, se->frame, FALSE);
256 return TRUE;
259 static void stack_print_addr_and_args(void)
261 char buffer[sizeof(SYMBOL_INFO) + 256];
262 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
263 IMAGEHLP_LINE64 il;
264 IMAGEHLP_MODULE im;
265 DWORD64 disp64;
266 struct dbg_frame* frm = stack_get_curr_frame();
268 if (!frm) return;
269 print_bare_address(&frm->addr_pc);
271 /* grab module where symbol is. If we don't have a module, we cannot print more */
272 im.SizeOfStruct = sizeof(im);
273 if (!SymGetModuleInfo(dbg_curr_process->handle, frm->linear_pc, &im))
274 return;
276 si->SizeOfStruct = sizeof(*si);
277 si->MaxNameLen = 256;
278 if (SymFromInlineContext(dbg_curr_process->handle, frm->linear_pc, frm->inline_ctx, &disp64, si))
280 struct sym_enum se;
281 DWORD disp;
283 dbg_printf(" %s", si->Name);
284 if (disp64) dbg_printf("+0x%I64x", disp64);
286 stack_set_local_scope();
287 se.first = TRUE;
288 se.frame = frm->linear_frame;
289 dbg_printf("(");
290 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
291 dbg_printf(")");
293 il.SizeOfStruct = sizeof(il);
294 if (SymGetLineFromInlineContext(dbg_curr_process->handle, frm->linear_pc, frm->inline_ctx,
295 0, &disp, &il))
296 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
297 dbg_printf(" in %s", im.ModuleName);
299 else dbg_printf(" in %s (+0x%Ix)", im.ModuleName, frm->linear_pc - (DWORD_PTR)im.BaseOfImage);
302 /******************************************************************
303 * backtrace
305 * Do a backtrace on the current thread
307 static void backtrace(void)
309 unsigned cf = dbg_curr_thread->curr_frame;
311 dbg_printf("Backtrace:\n");
312 for (dbg_curr_thread->curr_frame = 0;
313 dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
314 dbg_curr_thread->curr_frame++)
316 dbg_printf("%s%d ",
317 (cf == dbg_curr_thread->curr_frame ? "=>" : " "),
318 dbg_curr_thread->curr_frame);
319 stack_print_addr_and_args();
320 dbg_printf(" (");
321 print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
322 dbg_printf(")\n");
324 /* reset context to current stack frame */
325 dbg_curr_thread->curr_frame = cf;
326 if (!dbg_curr_thread->frames) return;
327 stack_set_local_scope();
330 /******************************************************************
331 * backtrace_tid
333 * Do a backtrace on a thread from its process and its identifier
334 * (preserves current thread and context information)
336 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
338 struct dbg_thread* thread = dbg_curr_thread;
340 if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
341 dbg_printf("Unknown thread id (%04lx) in process (%04lx)\n", tid, pcs->pid);
342 else
344 dbg_ctx_t ctx = {{0}};
346 dbg_curr_tid = dbg_curr_thread->tid;
347 if (SuspendThread(dbg_curr_thread->handle) != -1)
349 if (!pcs->be_cpu->get_context(dbg_curr_thread->handle, &ctx))
351 dbg_printf("Can't get context for thread %04lx in current process\n",
352 tid);
354 else
356 stack_fetch_frames(&ctx);
357 backtrace();
359 ResumeThread(dbg_curr_thread->handle);
361 else dbg_printf("Can't suspend thread %04lx in current process\n", tid);
363 dbg_curr_thread = thread;
364 dbg_curr_tid = thread ? thread->tid : 0;
367 /******************************************************************
368 * backtrace_all
370 * Do a backtrace on every running thread in the system (except the debugger)
371 * (preserves current process information)
373 static void backtrace_all(void)
375 struct dbg_process* process = dbg_curr_process;
376 struct dbg_thread* thread = dbg_curr_thread;
377 dbg_ctx_t ctx = dbg_context;
378 DWORD cpid = dbg_curr_pid;
379 THREADENTRY32 entry;
380 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
382 if (snapshot == INVALID_HANDLE_VALUE)
384 dbg_printf("Unable to create toolhelp snapshot\n");
385 return;
388 entry.dwSize = sizeof(entry);
389 if (Thread32First(snapshot, &entry))
393 if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
394 if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
395 cpid != dbg_curr_pid)
396 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
398 if (entry.th32OwnerProcessID == cpid)
400 dbg_curr_process = process;
401 dbg_curr_pid = cpid;
403 else if (entry.th32OwnerProcessID != dbg_curr_pid)
405 if (!dbg_attach_debuggee(entry.th32OwnerProcessID))
407 dbg_printf("\nwarning: could not attach to %04lx\n",
408 entry.th32OwnerProcessID);
409 continue;
411 dbg_curr_pid = dbg_curr_process->pid;
412 dbg_active_wait_for_first_exception();
415 dbg_printf("\nBacktracing for thread %04lx in process %04lx (%ls):\n",
416 entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
417 backtrace_tid(dbg_curr_process, entry.th32ThreadID);
419 while (Thread32Next(snapshot, &entry));
421 if (dbg_curr_process && cpid != dbg_curr_pid)
422 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
424 CloseHandle(snapshot);
425 dbg_curr_process = process;
426 dbg_curr_pid = cpid;
427 dbg_curr_thread = thread;
428 dbg_curr_tid = thread ? thread->tid : 0;
429 dbg_context = ctx;
432 void stack_backtrace(DWORD tid)
434 /* backtrace every thread in every process except the debugger itself,
435 * invoking via "bt all"
437 if (tid == -1)
439 backtrace_all();
440 return;
443 if (!dbg_curr_process)
445 dbg_printf("You must be attached to a process to run this command.\n");
446 return;
449 if (tid == dbg_curr_tid)
451 backtrace();
453 else
455 backtrace_tid(dbg_curr_process, tid);