2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
15 #include "stackframe.h"
20 * We keep this info for each frame, so that we can
21 * find local variable information correctly.
29 struct symbol_info frame;
33 static struct bt_info * frames = NULL;
52 /***********************************************************************
55 * Dump the top of the stack
57 void DEBUG_InfoStack(void)
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' );
72 case 16: /* 16-bit mode */
73 value.addr.off &= 0xffff;
74 DEBUG_ExamineMemory( &value, 24, 'w' );
77 fprintf(stderr, "Bad segment (%ld)\n", value.addr.seg);
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));
90 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
92 frames[theframe].cs = code->seg;
93 frames[theframe].eip = code->off;
95 frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits,
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;
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;
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))) {
119 fprintf(stderr,"*** Invalid address ");
120 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
121 fprintf(stderr,"\n");
125 if (!frame.bp) return FALSE;
127 frames = (struct bt_info *)DBG_realloc(frames,
128 nframe*sizeof(struct bt_info));
130 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
132 if (frame.bp & 1) *cs = frame.cs;
134 /* not explicitly marked as far call,
135 * but check whether it could be anyway */
136 if (((frame.cs&7)==7) && (frame.cs != *cs)) {
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;
150 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 16,
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;
159 fprintf( stderr, " (bp=%04lx", addr->off );
161 fprintf( stderr, ", far call assumed" );
163 fprintf( stderr, ")\n" );
168 static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
170 unsigned int ss = addr->seg;
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))) {
179 fprintf(stderr,"*** Invalid address ");
180 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
181 fprintf(stderr,"\n");
185 if (!frame.ip) return FALSE;
188 frames = (struct bt_info *)DBG_realloc(frames,
189 nframe*sizeof(struct bt_info));
191 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
193 frames[theframe].cs = addr->seg = *cs;
194 frames[theframe].eip = addr->off = frame.ip;
196 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 32,
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;
212 /***********************************************************************
215 * Display a stack back-trace.
217 void DEBUG_BackTrace(BOOL noisy)
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;
229 if (noisy) fprintf( stderr, "Backtrace:\n" );
232 if (frames) DBG_free( frames );
233 frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
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))
244 frames[0].cs = addr.seg = cs;
245 frames[0].eip = addr.off = DEBUG_context.Eip;
247 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
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 );
257 frames[0].cs = addr.seg = cs;
258 frames[0].eip = addr.off = LOWORD(DEBUG_context.Eip);
260 frames[0].frame = DEBUG_PrintAddress( &addr, 16, TRUE );
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 );
270 if (noisy) fprintf( stderr, "Bad segment '%u'\n", ss);
274 /* cur_switch holds address of curr_stack's field in TEB in debuggee
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");
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 );
289 cur_switch = (DWORD)frame32.frame16;
290 sw_addr.seg = SELECTOROF(cur_switch);
291 sw_addr.off = OFFSETOF(cur_switch);
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 );
301 cur_switch = (DWORD)frame16.frame32;
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)) {
314 * yes, I know this is confusing, it gave me a headache too */
317 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
318 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK32FRAME*)next_switch );
323 code.off = frame32.retaddr;
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",
340 cur_switch = (DWORD)frame16.frame32;
342 sw_addr.off = cur_switch;
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",
356 code.seg = frame16.cs;
357 code.off = frame16.ip;
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 );
370 cur_switch = (DWORD)frame32.frame16;
371 sw_addr.seg = SELECTOROF(cur_switch);
372 sw_addr.off = OFFSETOF(cur_switch);
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;
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" );
391 DEBUG_SetFrame(int newframe)
395 curr_frame = newframe;
397 if( curr_frame >= nframe )
399 curr_frame = nframe - 1;
407 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
409 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
417 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
421 * If we don't have a valid backtrace, then just return.
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 )
437 *name = frames[curr_frame].frame.sym;
438 *eip = frames[curr_frame].eip;
439 *ebp = frames[curr_frame].ebp;