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