Made request tracing more robust against bogus lengths.
[wine/multimedia.git] / debugger / stack.c
blob07f2a958488a0daaa6de63cd92e3f71ed3a5f1a0
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
7 */
9 #include "config.h"
11 #include <stdio.h>
12 #include <stdlib.h>
14 #include "debugger.h"
15 #include "stackframe.h"
16 #include "winbase.h"
18 #ifdef __i386__
20 * We keep this info for each frame, so that we can
21 * find local variable information correctly.
23 struct bt_info
25 unsigned int cs;
26 unsigned int eip;
27 unsigned int ss;
28 unsigned int ebp;
29 struct symbol_info frame;
32 static int nframe;
33 static struct bt_info * frames = NULL;
34 int curr_frame;
36 typedef struct
38 WORD bp;
39 WORD ip;
40 WORD cs;
41 } FRAME16;
43 typedef struct
45 DWORD bp;
46 DWORD ip;
47 WORD cs;
48 } FRAME32;
49 #endif
52 /***********************************************************************
53 * DEBUG_InfoStack
55 * Dump the top of the stack
57 void DEBUG_InfoStack(void)
59 #ifdef __i386__
60 DBG_VALUE value;
62 value.type = NULL;
63 value.cookie = DV_TARGET;
64 value.addr.seg = DEBUG_context.SegSs;
65 value.addr.off = DEBUG_context.Esp;
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);
79 fprintf(stderr,"\n");
80 #endif
83 #ifdef __i386__
84 static void DEBUG_ForceFrame(DBG_ADDR *stack, DBG_ADDR *code, int frameno, int bits, int noisy)
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 );
108 static BOOL DEBUG_Frame16(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
110 unsigned int ss = addr->seg, possible_cs = 0;
111 FRAME16 frame;
112 int theframe = nframe;
113 void* p = (void*)DEBUG_ToLinear(addr);
115 if (!p) return FALSE;
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");
123 return FALSE;
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;
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;
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" );
163 fprintf( stderr, ")\n" );
165 return TRUE;
168 static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
170 unsigned int ss = addr->seg;
171 FRAME32 frame;
172 int theframe = nframe;
173 void* p = (void*)DEBUG_ToLinear(addr);
175 if (!p) return FALSE;
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");
183 return FALSE;
185 if (!frame.ip) return FALSE;
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;
209 #endif
212 /***********************************************************************
213 * DEBUG_BackTrace
215 * Display a stack back-trace.
217 void DEBUG_BackTrace(BOOL noisy)
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;
229 if (noisy) fprintf( stderr, "Backtrace:\n" );
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);
237 if (DEBUG_IsSelectorSystem(ss)) ss = 0;
238 if (DEBUG_IsSelectorSystem(cs)) cs = 0;
240 /* first stack frame from registers */
241 switch (DEBUG_GetSelectorType(ss))
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;
274 /* cur_switch holds address of curr_stack's field in TEB in debuggee
275 * address space
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;
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;
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);
297 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
298 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK16FRAME*)p );
299 return;
301 cur_switch = (DWORD)frame16.frame32;
302 sw_addr.seg = ss;
303 sw_addr.off = cur_switch;
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;
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) {
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;
322 code.seg = 0;
323 code.off = frame32.retaddr;
325 cs = 0;
326 addr.seg = 0;
327 addr.off = frame32.ebp;
328 DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy );
330 next_switch = cur_switch;
331 tmp.seg = SELECTOROF(next_switch);
332 tmp.off = OFFSETOF(next_switch);
333 p = DEBUG_ToLinear(&tmp);
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;
340 cur_switch = (DWORD)frame16.frame32;
341 sw_addr.seg = 0;
342 sw_addr.off = cur_switch;
344 is16 = FALSE;
345 } else {
346 tmp.seg = SELECTOROF(next_switch);
347 tmp.off = OFFSETOF(next_switch);
348 p = DEBUG_ToLinear(&tmp);
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;
356 code.seg = frame16.cs;
357 code.off = frame16.ip;
359 cs = frame16.cs;
360 addr.seg = SELECTOROF(next_switch);
361 addr.off = frame16.bp;
362 DEBUG_ForceFrame( &addr, &code, ++frameno, 16, noisy );
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;
370 cur_switch = (DWORD)frame32.frame16;
371 sw_addr.seg = SELECTOROF(cur_switch);
372 sw_addr.off = OFFSETOF(cur_switch);
374 is16 = TRUE;
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;
380 } else {
381 /* ordinary stack frame */
382 ok = is16 ? DEBUG_Frame16( &addr, &cs, ++frameno, noisy)
383 : DEBUG_Frame32( &addr, &cs, ++frameno, noisy);
386 if (noisy) fprintf( stderr, "\n" );
387 #endif
391 DEBUG_SetFrame(int newframe)
393 int rtn = FALSE;
395 curr_frame = newframe;
397 if( curr_frame >= nframe )
399 curr_frame = nframe - 1;
402 if( curr_frame < 0 )
404 curr_frame = 0;
407 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
409 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
412 rtn = TRUE;
413 return (rtn);
417 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
418 unsigned int * ebp)
421 * If we don't have a valid backtrace, then just return.
423 if( frames == NULL )
425 return FALSE;
429 * If we don't know what the current function is, then we also have
430 * nothing to report here.
432 if( frames[curr_frame].frame.sym == NULL )
434 return FALSE;
437 *name = frames[curr_frame].frame.sym;
438 *eip = frames[curr_frame].eip;
439 *ebp = frames[curr_frame].ebp;
441 return TRUE;