comctl32: A couple fixes for tab icon offsets.
[wine] / programs / winedbg / stack.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "wine/debug.h"
32 #include "tlhelp32.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35
36 /***********************************************************************
37  *           stack_info
38  *
39  * Dump the top of the stack
40  */
41 void stack_info(void)
42 {
43     struct dbg_lvalue lvalue;
44
45     lvalue.cookie = 0;
46     lvalue.type.id = dbg_itype_segptr;
47     lvalue.type.module = 0;
48
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);
52
53     dbg_printf("Stack dump:\n");
54     switch (lvalue.addr.Mode)
55     {
56     case AddrModeFlat: /* 32-bit mode */
57     case AddrMode1632: /* 32-bit mode */
58         memory_examine(&lvalue, 24, 'x');
59         break;
60     case AddrModeReal:  /* 16-bit mode */
61     case AddrMode1616:
62         memory_examine(&lvalue, 24, 'w');
63         break;
64     }
65 }
66
67 static BOOL stack_set_frame_internal(int newframe)
68 {
69     if (newframe >= dbg_curr_thread->num_frames)
70         newframe = dbg_curr_thread->num_frames - 1;
71     if (newframe < 0)
72         newframe = 0;
73
74     if (dbg_curr_thread->curr_frame != newframe)
75     {
76         IMAGEHLP_STACK_FRAME    ihsf;
77
78         dbg_curr_thread->curr_frame = newframe;
79         stack_get_current_frame(&ihsf);
80         SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
81     }
82     return TRUE;
83 }
84
85 static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
86 {
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);
90     return TRUE;
91 }
92
93 BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
94 {
95     /*
96      * If we don't have a valid backtrace, then just return.
97      */
98     if (dbg_curr_thread->frames == NULL) return FALSE;
99     return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
100 }
101
102 BOOL stack_set_frame(int newframe)
103 {
104     ADDRESS     addr;
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);
109     return TRUE;
110 }
111
112 /******************************************************************
113  *              stack_get_current_symbol
114  *
115  * Retrieves the symbol information for the current frame element
116  */
117 BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
118 {
119     IMAGEHLP_STACK_FRAME        ihsf;
120     DWORD64                     disp;
121
122     if (!stack_get_current_frame(&ihsf)) return FALSE;
123     return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
124                        &disp, symbol);
125 }
126
127 static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD addr, 
128                                     PVOID buffer, DWORD size, PDWORD written)
129 {
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);
133 }
134
135 /******************************************************************
136  *              stack_fetch_frames
137  *
138  * Do a backtrace on the the current thread
139  */
140 unsigned stack_fetch_frames(void)
141 {
142     STACKFRAME  sf;
143     unsigned    nf = 0;
144
145     HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
146     dbg_curr_thread->frames = NULL;
147
148     memset(&sf, 0, sizeof(sf));
149     memory_get_current_frame(&sf.AddrFrame);
150     memory_get_current_pc(&sf.AddrPC);
151
152     /* don't confuse StackWalk by passing in inconsistent addresses */
153     if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
154     {
155         sf.AddrFrame.Offset = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
156         sf.AddrFrame.Mode = AddrModeFlat;
157     }
158
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))
162     {
163         dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames, 
164                                                    (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
165
166         dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC;
167         dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame;
168         nf++;
169         /* we've probably gotten ourselves into an infinite loop so bail */
170         if (nf > 200) break;
171     }
172     dbg_curr_thread->curr_frame = -1;
173     dbg_curr_thread->num_frames = nf;
174     stack_set_frame_internal(0);
175     return nf;
176 }
177
178 struct sym_enum
179 {
180     char*       tmp;
181     DWORD       frame;
182 };
183
184 static BOOL WINAPI sym_enum_cb(SYMBOL_INFO* sym_info, ULONG size, void* user)
185 {
186     struct sym_enum*    se = (struct sym_enum*)user;
187     char                tmp[32];
188
189     if (sym_info->Flags & SYMFLAG_PARAMETER)
190     {
191         if (se->tmp[0]) strcat(se->tmp, ", ");
192     
193         if (sym_info->Flags & SYMFLAG_REGREL)
194         {
195             unsigned    val;
196             DWORD       addr = se->frame + sym_info->Address;
197
198             if (!dbg_read_memory((char*)addr, &val, sizeof(val)))
199                 snprintf(tmp, sizeof(tmp), "<*** cannot read at 0x%lx ***>", addr);
200             else
201                 snprintf(tmp, sizeof(tmp), "0x%x", val);
202         }
203         else if (sym_info->Flags & SYMFLAG_REGISTER)
204         {
205             DWORD* pval;
206
207             if (memory_get_register(sym_info->Register, &pval, tmp, sizeof(tmp)))
208                 snprintf(tmp, sizeof(tmp), "0x%lx", *pval);
209         }
210         sprintf(se->tmp + strlen(se->tmp), "%s=%s", sym_info->Name, tmp);
211     }
212     return TRUE;
213 }
214
215 static void stack_print_addr_and_args(int nf)
216 {
217     char                        buffer[sizeof(SYMBOL_INFO) + 256];
218     SYMBOL_INFO*                si = (SYMBOL_INFO*)buffer;
219     IMAGEHLP_STACK_FRAME        ihsf;
220     IMAGEHLP_LINE               il;
221     IMAGEHLP_MODULE             im;
222     DWORD64                     disp64;
223
224     print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);
225
226     stack_get_frame(nf, &ihsf);
227
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))
231         return;
232
233     si->SizeOfStruct = sizeof(*si);
234     si->MaxNameLen   = 256;
235     if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
236     {
237         struct sym_enum se;
238         char            tmp[1024];
239         DWORD           disp;
240
241         dbg_printf(" %s", si->Name);
242         if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
243
244         SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
245         se.tmp = tmp;
246         se.frame = ihsf.FrameOffset;
247         tmp[0] = '\0';
248         SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
249         if (tmp[0]) dbg_printf("(%s)", tmp);
250
251         il.SizeOfStruct = sizeof(il);
252         if (SymGetLineFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
253                                &disp, &il))
254             dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
255         dbg_printf(" in %s", im.ModuleName);
256     }
257     else dbg_printf(" in %s (+0x%lx)", 
258                     im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
259 }
260
261 /******************************************************************
262  *              backtrace
263  *
264  * Do a backtrace on the the current thread
265  */
266 static void backtrace(void)
267 {
268     unsigned                    cf = dbg_curr_thread->curr_frame;
269     IMAGEHLP_STACK_FRAME        ihsf;
270
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++)
275     {
276         dbg_printf("%s%d ", 
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);
280         dbg_printf(" (");
281         print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
282         dbg_printf(")\n");
283     }
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);
288 }
289
290 /******************************************************************
291  *              backtrace_tid
292  *
293  * Do a backtrace on a thread from its process and its identifier
294  * (preserves current thread and context information)
295  */
296 static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
297 {
298     struct dbg_thread*  thread = dbg_curr_thread;
299
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);
302     else
303     {
304         CONTEXT saved_ctx = dbg_context;
305
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)
310         {
311             if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
312             {
313                 dbg_printf("Can't get context for thread 0x%lx in current process\n",
314                            tid);
315             }
316             else backtrace();
317             ResumeThread(dbg_curr_thread->handle);
318         }
319         else dbg_printf("Can't suspend thread 0x%lx in current process\n", tid);
320         dbg_context = saved_ctx;
321     }
322     dbg_curr_thread = thread;
323     dbg_curr_tid = thread ? thread->tid : 0;
324 }
325
326 /******************************************************************
327  *              backtrace_all
328  *
329  * Do a backtrace on every running thread in the system (except the debugger)
330  * (preserves current process information)
331  */
332 static void backtrace_all(void)
333 {
334     THREADENTRY32       entry;
335     HANDLE              snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
336
337     if (snapshot == INVALID_HANDLE_VALUE)
338     {
339         dbg_printf("Unable to create toolhelp snapshot\n");
340         return;
341     }
342
343     entry.dwSize = sizeof(entry);
344     if (Thread32First(snapshot, &entry))
345     {
346         do
347         {
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);
351
352             if (entry.th32OwnerProcessID != dbg_curr_pid)
353             {
354                 if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE, TRUE))
355                 {
356                     dbg_printf("\nwarning: could not attach to 0x%lx\n",
357                                entry.th32OwnerProcessID);
358                     continue;
359                 }
360                 dbg_curr_pid = dbg_curr_process->pid;
361             }
362
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);
366         }
367         while (Thread32Next(snapshot, &entry));
368
369         if (dbg_curr_process)
370             dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
371     }
372     CloseHandle(snapshot);
373 }
374
375 void stack_backtrace(DWORD tid)
376 {
377     /* backtrace every thread in every process except the debugger itself,
378      * invoking via "bt all"
379      */
380     if (tid == -1) return backtrace_all();
381
382     if (!dbg_curr_process) 
383     {
384         dbg_printf("You must be attached to a process to run this command.\n");
385         return;
386     }
387     
388     if (tid == dbg_curr_tid)
389     {
390         backtrace();
391     }
392     else
393     {
394         backtrace_tid(dbg_curr_process, tid);
395     }
396 }