~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/programs/winedbg/stack.c

Version: ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  * Debugger stack handling
  3  *
  4  * Copyright 1995 Alexandre Julliard
  5  * Copyright 1996 Eric Youngdale
  6  * Copyright 1999 Ove Kåven
  7  *
  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.
 12  *
 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.
 17  *
 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
 21  */
 22 
 23 #include "config.h"
 24 
 25 #include <stdlib.h>
 26 #include <stdio.h>
 27 
 28 #include "debugger.h"
 29 #include "winbase.h"
 30 #include "wine/winbase16.h"
 31 #include "tlhelp32.h"
 32 
 33 /***********************************************************************
 34  *           stack_info
 35  *
 36  * Dump the top of the stack
 37  */
 38 void stack_info(void)
 39 {
 40     struct dbg_lvalue lvalue;
 41 
 42     lvalue.cookie = 0;
 43     lvalue.type.id = dbg_itype_segptr;
 44     lvalue.type.module = 0;
 45 
 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);
 49 
 50     dbg_printf("Stack dump:\n");
 51     switch (lvalue.addr.Mode)
 52     {
 53     case AddrModeFlat: /* 32-bit mode */
 54     case AddrMode1632: /* 32-bit mode */
 55         memory_examine(&lvalue, 24, 'x');
 56         break;
 57     case AddrModeReal:  /* 16-bit mode */
 58     case AddrMode1616:
 59         memory_examine(&lvalue, 24, 'w');
 60         break;
 61     }
 62 }
 63 
 64 static BOOL stack_set_frame_internal(int newframe)
 65 {
 66     if (newframe >= dbg_curr_thread->num_frames)
 67         newframe = dbg_curr_thread->num_frames - 1;
 68     if (newframe < 0)
 69         newframe = 0;
 70 
 71     if (dbg_curr_thread->curr_frame != newframe)
 72     {
 73         IMAGEHLP_STACK_FRAME    ihsf;
 74 
 75         dbg_curr_thread->curr_frame = newframe;
 76         stack_get_current_frame(&ihsf);
 77         SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
 78     }
 79     return TRUE;
 80 }
 81 
 82 static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
 83 {
 84     memset(ihsf, 0, sizeof(*ihsf));
 85     ihsf->InstructionOffset = dbg_curr_thread->frames[nf].linear_pc;
 86     ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
 87     return TRUE;
 88 }
 89 
 90 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
 91 {
 92     /*
 93      * If we don't have a valid backtrace, then just return.
 94      */
 95     if (dbg_curr_thread->frames == NULL) return FALSE;
 96     return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
 97 }
 98 
 99 BOOL stack_get_register_current_frame(unsigned regno, DWORD** pval)
100 {
101     enum be_cpu_addr            kind;
102 
103     if (dbg_curr_thread->frames == NULL) return FALSE;
104 
105     if (!be_cpu->get_register_info(regno, &kind)) return FALSE;
106 
107     switch (kind)
108     {
109     case be_cpu_addr_pc:
110         *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
111         break;
112     case be_cpu_addr_stack:
113         *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
114         break;
115     case be_cpu_addr_frame:
116         *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
117         break;
118     }
119     return TRUE;
120 }
121 
122 BOOL stack_set_frame(int newframe)
123 {
124     ADDRESS64   addr;
125     if (!stack_set_frame_internal(newframe)) return FALSE;
126     addr.Mode = AddrModeFlat;
127     addr.Offset = (unsigned long)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
128     source_list_from_addr(&addr, 0);
129     return TRUE;
130 }
131 
132 /******************************************************************
133  *              stack_get_current_symbol
134  *
135  * Retrieves the symbol information for the current frame element
136  */
137 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
138 {
139     IMAGEHLP_STACK_FRAME        ihsf;
140     DWORD64                     disp;
141 
142     if (!stack_get_current_frame(&ihsf)) return FALSE;
143     return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
144                        &disp, symbol);
145 }
146 
147 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr, 
148                                     PVOID buffer, DWORD size, PDWORD written)
149 {
150     SIZE_T sz;
151     BOOL ret;
152 
153     struct dbg_process* pcs = dbg_get_process_h(hProc);
154     if (!pcs) return FALSE;
155     ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
156                                 size, &sz);
157     if (written != NULL) *written = sz;
158     return ret;
159 }
160 
161 /******************************************************************
162  *              stack_fetch_frames
163  *
164  * Do a backtrace on the current thread
165  */
166 unsigned stack_fetch_frames(void)
167 {
168     STACKFRAME64 sf;
169     unsigned     nf = 0;
170     /* as native stackwalk can modify the context passed to it, simply copy
171      * it to avoid any damage
172      */
173     CONTEXT      ctx = dbg_context;
174 
175     HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
176     dbg_curr_thread->frames = NULL;
177 
178     memset(&sf, 0, sizeof(sf));
179     memory_get_current_frame(&sf.AddrFrame);
180     memory_get_current_pc(&sf.AddrPC);
181 
182     /* don't confuse StackWalk by passing in inconsistent addresses */
183     if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
184     {
185         sf.AddrFrame.Offset = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
186         sf.AddrFrame.Mode = AddrModeFlat;
187     }
188 
189     while (StackWalk64(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle, 
190                        dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
191                        SymFunctionTableAccess64, SymGetModuleBase64, NULL))
192     {
193         dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames, 
194                                                    (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
195 
196         dbg_curr_thread->frames[nf].addr_pc      = sf.AddrPC;
197         dbg_curr_thread->frames[nf].linear_pc    = (DWORD)memory_to_linear_addr(&sf.AddrPC);
198         dbg_curr_thread->frames[nf].addr_frame   = sf.AddrFrame;
199         dbg_curr_thread->frames[nf].linear_frame = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
200         dbg_curr_thread->frames[nf].addr_stack   = sf.AddrStack;
201         dbg_curr_thread->frames[nf].linear_stack = (DWORD)memory_to_linear_addr(&sf.AddrStack);
202         nf++;
203         /* we've probably gotten ourselves into an infinite loop so bail */
204         if (nf > 200) break;
205     }
206     dbg_curr_thread->curr_frame = -1;
207     dbg_curr_thread->num_frames = nf;
208     stack_set_frame_internal(0);
209     return nf;
210 }
211 
212 struct sym_enum
213 {
214     DWORD       frame;
215     BOOL        first;
216 };
217 
218 static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
219 {
220     struct sym_enum*    se = (struct sym_enum*)user;
221 
222     if (sym_info->Flags & SYMFLAG_PARAMETER)
223     {
224         if (!se->first) dbg_printf(", "); else se->first = FALSE;
225         symbol_print_local(sym_info, se->frame, FALSE);
226     }
227     return TRUE;
228 }
229 
230 static void stack_print_addr_and_args(int nf)
231 {
232     char                        buffer[sizeof(SYMBOL_INFO) + 256];
233     SYMBOL_INFO*                si = (SYMBOL_INFO*)buffer;
234     IMAGEHLP_STACK_FRAME        ihsf;
235     IMAGEHLP_LINE               il;
236     IMAGEHLP_MODULE             im;
237     DWORD64                     disp64;
238 
239     print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
240 
241     stack_get_frame(nf, &ihsf);
242 
243     /* grab module where symbol is. If we don't have a module, we cannot print more */
244     im.SizeOfStruct = sizeof(im);
245     if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
246         return;
247 
248     si->SizeOfStruct = sizeof(*si);
249     si->MaxNameLen   = 256;
250     if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
251     {
252         struct sym_enum se;
253         DWORD           disp;
254 
255         dbg_printf(" %s", si->Name);
256         if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
257 
258         SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
259         se.first = TRUE;
260         se.frame = ihsf.FrameOffset;
261         dbg_printf("(");
262         SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
263         dbg_printf(")");
264 
265         il.SizeOfStruct = sizeof(il);
266         if (SymGetLineFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
267                                &disp, &il))
268             dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
269         dbg_printf(" in %s", im.ModuleName);
270     }
271     else dbg_printf(" in %s (+0x%lx)", 
272                     im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
273 }
274 
275 /******************************************************************
276  *              backtrace
277  *
278  * Do a backtrace on the current thread
279  */
280 static void backtrace(void)
281 {
282     unsigned                    cf = dbg_curr_thread->curr_frame;
283     IMAGEHLP_STACK_FRAME        ihsf;
284 
285     dbg_printf("Backtrace:\n");
286     for (dbg_curr_thread->curr_frame = 0;
287          dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
288          dbg_curr_thread->curr_frame++)
289     {
290         dbg_printf("%s%d ", 
291                    (cf == dbg_curr_thread->curr_frame ? "=>" : "  "),
292                    dbg_curr_thread->curr_frame + 1);
293         stack_print_addr_and_args(dbg_curr_thread->curr_frame);
294         dbg_printf(" (");
295         print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
296         dbg_printf(")\n");
297     }
298     /* reset context to current stack frame */
299     dbg_curr_thread->curr_frame = cf;
300     if (!dbg_curr_thread->frames) return;
301     stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
302     SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
303 }
304 
305 /******************************************************************
306  *              backtrace_tid
307  *
308  * Do a backtrace on a thread from its process and its identifier
309  * (preserves current thread and context information)
310  */
311 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
312 {
313     struct dbg_thread*  thread = dbg_curr_thread;
314 
315     if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
316         dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
317     else
318     {
319         CONTEXT saved_ctx = dbg_context;
320 
321         dbg_curr_tid = dbg_curr_thread->tid;
322         memset(&dbg_context, 0, sizeof(dbg_context));
323         dbg_context.ContextFlags = CONTEXT_FULL;
324         if (SuspendThread(dbg_curr_thread->handle) != -1)
325         {
326             if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
327             {
328                 dbg_printf("Can't get context for thread %04x in current process\n",
329                            tid);
330             }
331             else
332             {
333                 stack_fetch_frames();
334                 backtrace();
335             }
336             ResumeThread(dbg_curr_thread->handle);
337         }
338         else dbg_printf("Can't suspend thread %04x in current process\n", tid);
339         dbg_context = saved_ctx;
340     }
341     dbg_curr_thread = thread;
342     dbg_curr_tid = thread ? thread->tid : 0;
343 }
344 
345 /******************************************************************
346  *              backtrace_all
347  *
348  * Do a backtrace on every running thread in the system (except the debugger)
349  * (preserves current process information)
350  */
351 static void backtrace_all(void)
352 {
353     struct dbg_process* process = dbg_curr_process;
354     struct dbg_thread*  thread = dbg_curr_thread;
355     CONTEXT             ctx = dbg_context;
356     DWORD               cpid = dbg_curr_pid;
357     THREADENTRY32       entry;
358     HANDLE              snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
359 
360     if (snapshot == INVALID_HANDLE_VALUE)
361     {
362         dbg_printf("Unable to create toolhelp snapshot\n");
363         return;
364     }
365 
366     entry.dwSize = sizeof(entry);
367     if (Thread32First(snapshot, &entry))
368     {
369         do
370         {
371             if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
372             if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID &&
373                 cpid != dbg_curr_pid)
374                 dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
375 
376             if (entry.th32OwnerProcessID == cpid)
377             {
378                 dbg_curr_process = process;
379                 dbg_curr_pid = cpid;
380             }
381             else if (entry.th32OwnerProcessID != dbg_curr_pid)
382             {
383                 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
384                 {
385                     dbg_printf("\nwarning: could not attach to %04x\n",
386                                entry.th32OwnerProcessID);
387                     continue;
388                 }
389                 dbg_curr_pid = dbg_curr_process->pid;
390                 dbg_active_wait_for_first_exception();
391             }
392 
393             dbg_printf("\nBacktracing for thread %04x in process %04x (%s):\n",
394                        entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
395             backtrace_tid(dbg_curr_process, entry.th32ThreadID);
396         }
397         while (Thread32Next(snapshot, &entry));
398 
399         if (dbg_curr_process && cpid != dbg_curr_pid)
400             dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
401     }
402     CloseHandle(snapshot);
403     dbg_curr_process = process;
404     dbg_curr_pid = cpid;
405     dbg_curr_thread = thread;
406     dbg_curr_tid = thread ? thread->tid : 0;
407     dbg_context = ctx;
408 }
409 
410 void stack_backtrace(DWORD tid)
411 {
412     /* backtrace every thread in every process except the debugger itself,
413      * invoking via "bt all"
414      */
415     if (tid == -1) return backtrace_all();
416 
417     if (!dbg_curr_process) 
418     {
419         dbg_printf("You must be attached to a process to run this command.\n");
420         return;
421     }
422     
423     if (tid == dbg_curr_tid)
424     {
425         backtrace();
426     }
427     else
428     {
429         backtrace_tid(dbg_curr_process, tid);
430     }
431 }
432 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.