Introduced DBG_VALUE struct to manipulate debugger/debuggee address space.
[wine] / debugger / 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
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "debugger.h"
15 #include "stackframe.h"
16 #include "winbase.h"
17
18 #ifdef __i386__
19 /*
20  * We keep this info for each frame, so that we can
21  * find local variable information correctly.
22  */
23 struct bt_info
24 {
25   unsigned int       cs;
26   unsigned int       eip;
27   unsigned int       ss;
28   unsigned int       ebp;
29   struct symbol_info frame;
30 };
31
32 static int nframe;
33 static struct bt_info * frames = NULL;
34 int curr_frame;
35
36 typedef struct
37 {
38     WORD bp;
39     WORD ip;
40     WORD cs;
41 } FRAME16;
42
43 typedef struct
44 {
45     DWORD bp;
46     DWORD ip;
47     WORD cs;
48 } FRAME32;
49 #endif
50
51
52 /***********************************************************************
53  *           DEBUG_InfoStack
54  *
55  * Dump the top of the stack
56  */
57 void DEBUG_InfoStack(void)
58 {
59 #ifdef __i386__
60     DBG_VALUE   value;
61     
62     value.type = NULL;
63     value.cookie = DV_TARGET;
64     value.addr.seg = DEBUG_context.SegSs;
65     value.addr.off = DEBUG_context.Esp;
66
67     fprintf(stderr,"Stack dump:\n");
68     switch (DEBUG_GetSelectorType(value.addr.seg)) {
69     case 32: /* 32-bit mode */
70        DEBUG_ExamineMemory( &value, 24, 'x' );
71        break;
72     case 16:  /* 16-bit mode */
73         value.addr.off &= 0xffff;
74         DEBUG_ExamineMemory( &value, 24, 'w' );
75         break;
76     default:
77        fprintf(stderr, "Bad segment (%ld)\n", value.addr.seg);
78     }
79     fprintf(stderr,"\n");
80 #endif
81 }
82
83 #ifdef __i386__
84 static void DEBUG_ForceFrame(DBG_ADDR *stack, DBG_ADDR *code, int frameno, int bits, int noisy)
85 {
86     int theframe = nframe++;
87     frames = (struct bt_info *)DBG_realloc(frames,
88                                            nframe*sizeof(struct bt_info));
89     if (noisy)
90       fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : "  "),
91               frameno);
92     frames[theframe].cs = code->seg;
93     frames[theframe].eip = code->off;
94     if (noisy)
95       frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits, 
96                                                           stack->off, TRUE );
97     else
98       DEBUG_FindNearestSymbol( code, TRUE, 
99                                &frames[theframe].frame.sym, stack->off, 
100                                &frames[theframe].frame.list);
101     frames[theframe].ss = stack->seg;
102     frames[theframe].ebp = stack->off;
103     if (noisy) {
104       fprintf( stderr, (bits == 16) ? " (bp=%04lx)\n" : " (ebp=%08lx)\n", stack->off );
105     }
106 }
107
108 static BOOL DEBUG_Frame16(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
109 {
110     unsigned int        ss = addr->seg, possible_cs = 0;
111     FRAME16             frame;
112     int                 theframe = nframe;
113     void*               p = (void*)DEBUG_ToLinear(addr);
114     
115     if (!p) return FALSE;
116     
117     if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
118         if (noisy) {
119             fprintf(stderr,"*** Invalid address ");
120             DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
121             fprintf(stderr,"\n");
122         }
123         return FALSE;
124     }
125     if (!frame.bp) return FALSE;
126     nframe++;
127     frames = (struct bt_info *)DBG_realloc(frames,
128                                            nframe*sizeof(struct bt_info));
129     if (noisy)
130         fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : "  "),
131                 frameno);
132     if (frame.bp & 1) *cs = frame.cs;
133     else {
134         /* not explicitly marked as far call,
135          * but check whether it could be anyway */
136         if (((frame.cs&7)==7) && (frame.cs != *cs)) {
137             LDT_ENTRY   le;
138          
139             if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, frame.cs, &le) &&
140                 (le.HighWord.Bits.Type & 0x08)) { /* code segment */
141                 /* it is very uncommon to push a code segment cs as
142                  * a parameter, so this should work in most cases */
143                 *cs = possible_cs = frame.cs;
144             }
145         }
146     }
147     frames[theframe].cs = addr->seg = *cs;
148     frames[theframe].eip = addr->off = frame.ip;
149     if (noisy)
150         frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 16, 
151                                                             frame.bp, TRUE );
152     else
153         DEBUG_FindNearestSymbol( addr, TRUE, 
154                                  &frames[theframe].frame.sym, frame.bp, 
155                                  &frames[theframe].frame.list);
156     frames[theframe].ss = addr->seg = ss;
157     frames[theframe].ebp = addr->off = frame.bp & ~1;
158     if (noisy) {
159         fprintf( stderr, " (bp=%04lx", addr->off );
160         if (possible_cs) {
161             fprintf( stderr, ", far call assumed" );
162         }
163         fprintf( stderr, ")\n" );
164     }
165     return TRUE;
166 }
167
168 static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
169 {
170     unsigned int        ss = addr->seg;
171     FRAME32             frame;
172     int                 theframe = nframe;
173     void*               p = (void*)DEBUG_ToLinear(addr);
174     
175     if (!p) return FALSE;
176     
177     if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
178        if (noisy) {
179           fprintf(stderr,"*** Invalid address ");
180           DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
181           fprintf(stderr,"\n");
182        }
183        return FALSE;
184     }
185     if (!frame.ip) return FALSE;
186     
187     nframe++;
188     frames = (struct bt_info *)DBG_realloc(frames,
189                                            nframe*sizeof(struct bt_info));
190     if (noisy)
191        fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : "  "),
192                frameno);
193     frames[theframe].cs = addr->seg = *cs;
194     frames[theframe].eip = addr->off = frame.ip;
195     if (noisy)
196         frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 32, 
197                                                             frame.bp, TRUE );
198     else
199         DEBUG_FindNearestSymbol( addr, TRUE, 
200                                  &frames[theframe].frame.sym, frame.bp, 
201                                  &frames[theframe].frame.list);
202     if (noisy) fprintf( stderr, " (ebp=%08lx)\n", frame.bp );
203     frames[theframe].ss = addr->seg = ss;
204     frames[theframe].ebp = frame.bp;
205     if (addr->off == frame.bp) return FALSE;
206     addr->off = frame.bp;
207     return TRUE;
208 }
209 #endif
210
211
212 /***********************************************************************
213  *           DEBUG_BackTrace
214  *
215  * Display a stack back-trace.
216  */
217 void DEBUG_BackTrace(BOOL noisy)
218 {
219 #ifdef __i386
220     DBG_ADDR            addr, sw_addr, code, tmp;
221     unsigned int        ss = DEBUG_context.SegSs;
222     unsigned int        cs = DEBUG_context.SegCs;
223     int                 frameno = 0, is16, ok;
224     DWORD               next_switch, cur_switch, p;
225     STACK16FRAME        frame16;
226     STACK32FRAME        frame32;
227     char                ch;
228
229     if (noisy) fprintf( stderr, "Backtrace:\n" );
230
231     nframe = 1;
232     if (frames) DBG_free( frames );
233     frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
234     if (noisy)
235         fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : "  "), frameno);
236
237     if (DEBUG_IsSelectorSystem(ss)) ss = 0;
238     if (DEBUG_IsSelectorSystem(cs)) cs = 0;
239
240     /* first stack frame from registers */
241     switch (DEBUG_GetSelectorType(ss))
242     {
243     case 32:
244         frames[0].cs = addr.seg = cs;
245         frames[0].eip = addr.off = DEBUG_context.Eip;
246         if (noisy)
247             frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
248         else
249             DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0, 
250                                      &frames[0].frame.list);
251         frames[0].ss = addr.seg = ss;
252         frames[0].ebp = addr.off = DEBUG_context.Ebp;
253         if (noisy) fprintf( stderr, " (ebp=%08x)\n", frames[0].ebp );
254         is16 = FALSE;
255         break;
256     case 16:
257         frames[0].cs = addr.seg = cs;
258         frames[0].eip = addr.off = LOWORD(DEBUG_context.Eip);
259         if (noisy)
260             frames[0].frame = DEBUG_PrintAddress( &addr, 16, TRUE );
261         else
262             DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0, 
263                                      &frames[0].frame.list);
264         frames[0].ss = addr.seg = ss;
265         frames[0].ebp = addr.off = LOWORD(DEBUG_context.Ebp);
266         if (noisy) fprintf( stderr, " (bp=%04x)\n", frames[0].ebp );
267         is16 = TRUE;
268         break;
269     default:
270         if (noisy) fprintf( stderr, "Bad segment '%u'\n", ss);
271         return;
272     }
273
274     /* cur_switch holds address of curr_stack's field in TEB in debuggee 
275      * address space 
276      */
277     cur_switch = (DWORD)DEBUG_CurrThread->teb + OFFSET_OF(TEB, cur_stack);
278     if (!DEBUG_READ_MEM((void*)cur_switch, &next_switch, sizeof(next_switch))) {
279         if (noisy) fprintf( stderr, "Can't read TEB:cur_stack\n");
280         return;
281     }
282
283     if (is16) {
284         if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
285             if (noisy) fprintf( stderr, "Bad stack frame %p\n", 
286                                 (STACK32FRAME*)next_switch );
287             return;
288         }
289         cur_switch = (DWORD)frame32.frame16;
290         sw_addr.seg = SELECTOROF(cur_switch);
291         sw_addr.off = OFFSETOF(cur_switch);
292     } else {
293         tmp.seg = SELECTOROF(next_switch);
294         tmp.off = OFFSETOF(next_switch);
295         p = DEBUG_ToLinear(&tmp);
296         
297         if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
298             if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK16FRAME*)p );
299             return;
300         }
301         cur_switch = (DWORD)frame16.frame32;
302         sw_addr.seg = ss;
303         sw_addr.off = cur_switch;
304     }
305     if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
306         sw_addr.seg = (DWORD)-1;
307         sw_addr.off = (DWORD)-1;
308     }
309     
310     for (ok = TRUE; ok;) {
311         if ((frames[frameno].ss == sw_addr.seg) &&
312             (frames[frameno].ebp >= sw_addr.off)) {
313            /* 16<->32 switch...
314             * yes, I know this is confusing, it gave me a headache too */
315            if (is16) {
316               
317                if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
318                   if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK32FRAME*)next_switch );
319                   return;
320                }
321
322                code.seg  = 0;
323                code.off  = frame32.retaddr;
324                
325                cs = 0;
326                addr.seg = 0;
327                addr.off = frame32.ebp;
328                DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy );
329                
330                next_switch = cur_switch;
331                tmp.seg = SELECTOROF(next_switch);
332                tmp.off = OFFSETOF(next_switch);
333                p = DEBUG_ToLinear(&tmp);
334                
335                if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
336                    if (noisy) fprintf( stderr, "Bad stack frame %p\n", 
337                                        (STACK16FRAME*)p );
338                    return;
339                }
340                cur_switch = (DWORD)frame16.frame32;
341                sw_addr.seg = 0;
342                sw_addr.off = cur_switch;
343                
344                is16 = FALSE;
345            } else {
346               tmp.seg = SELECTOROF(next_switch);
347               tmp.off = OFFSETOF(next_switch);
348               p = DEBUG_ToLinear(&tmp);
349               
350               if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
351                   if (noisy) fprintf( stderr, "Bad stack frame %p\n",
352                                       (STACK16FRAME*)p );
353                   return;
354               }
355               
356               code.seg  = frame16.cs;
357               code.off  = frame16.ip;
358               
359               cs = frame16.cs;
360               addr.seg = SELECTOROF(next_switch);
361               addr.off = frame16.bp;
362               DEBUG_ForceFrame( &addr, &code, ++frameno, 16, noisy );
363               
364               next_switch = cur_switch;
365               if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
366                  if (noisy) fprintf( stderr, "Bad stack frame %p\n", 
367                                      (STACK32FRAME*)next_switch );
368                  return;
369               }
370               cur_switch = (DWORD)frame32.frame16;
371               sw_addr.seg = SELECTOROF(cur_switch);
372               sw_addr.off = OFFSETOF(cur_switch);
373               
374               is16 = TRUE;
375            }
376            if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
377               sw_addr.seg = (DWORD)-1;
378               sw_addr.off = (DWORD)-1;
379            }
380         } else {
381             /* ordinary stack frame */
382            ok = is16 ? DEBUG_Frame16( &addr, &cs, ++frameno, noisy)
383               : DEBUG_Frame32( &addr, &cs, ++frameno, noisy);
384         }
385     }
386     if (noisy) fprintf( stderr, "\n" );
387 #endif
388 }
389
390 int
391 DEBUG_SetFrame(int newframe)
392 {
393   int           rtn = FALSE;
394
395   curr_frame = newframe;
396
397   if( curr_frame >= nframe )
398     {
399       curr_frame = nframe - 1;
400     }
401
402   if( curr_frame < 0 )
403     {
404       curr_frame = 0;
405     }
406
407    if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
408     {
409       DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
410     }
411
412   rtn = TRUE;
413   return (rtn);
414 }
415
416 int
417 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
418                       unsigned int * ebp)
419 {
420   /*
421    * If we don't have a valid backtrace, then just return.
422    */
423   if( frames == NULL )
424     {
425       return FALSE;
426     }
427
428   /*
429    * If we don't know what the current function is, then we also have
430    * nothing to report here.
431    */
432   if( frames[curr_frame].frame.sym == NULL )
433     {
434       return FALSE;
435     }
436
437   *name = frames[curr_frame].frame.sym;
438   *eip = frames[curr_frame].eip;
439   *ebp = frames[curr_frame].ebp;
440
441   return TRUE;
442 }
443