2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
13 #include "stackframe.h"
17 * We keep this info for each frame, so that we can
18 * find local variable information correctly.
26 struct symbol_info frame;
30 static struct bt_info * frames = NULL;
49 /***********************************************************************
52 * Dump the top of the stack
54 void DEBUG_InfoStack(void)
60 addr.seg = DEBUG_context.SegSs;
61 addr.off = DEBUG_context.Esp;
63 fprintf(stderr,"Stack dump:\n");
64 switch (DEBUG_GetSelectorType(addr.seg)) {
65 case 32: /* 32-bit mode */
66 DEBUG_ExamineMemory( &addr, 24, 'x' );
68 case 16: /* 16-bit mode */
70 DEBUG_ExamineMemory( &addr, 24, 'w' );
73 fprintf(stderr, "Bad segment (%ld)\n", addr.seg);
80 static void DEBUG_ForceFrame(DBG_ADDR *stack, DBG_ADDR *code, int frameno, int bits, int noisy)
82 int theframe = nframe++;
83 frames = (struct bt_info *)DBG_realloc(frames,
84 nframe*sizeof(struct bt_info));
86 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
88 frames[theframe].cs = code->seg;
89 frames[theframe].eip = code->off;
91 frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits,
94 DEBUG_FindNearestSymbol( code, TRUE,
95 &frames[theframe].frame.sym, stack->off,
96 &frames[theframe].frame.list);
97 frames[theframe].ss = stack->seg;
98 frames[theframe].ebp = stack->off;
100 fprintf( stderr, (bits == 16) ? " (bp=%04lx)\n" : " (ebp=%08lx)\n", stack->off );
104 static BOOL DEBUG_Frame16(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
106 unsigned int ss = addr->seg, possible_cs = 0;
108 int theframe = nframe;
109 void* p = (void*)DEBUG_ToLinear(addr);
111 if (!p) return FALSE;
113 if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
115 fprintf(stderr,"*** Invalid address ");
116 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
117 fprintf(stderr,"\n");
121 if (!frame.bp) return FALSE;
123 frames = (struct bt_info *)DBG_realloc(frames,
124 nframe*sizeof(struct bt_info));
126 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
128 if (frame.bp & 1) *cs = frame.cs;
130 /* not explicitly marked as far call,
131 * but check whether it could be anyway */
132 if (((frame.cs&7)==7) && (frame.cs != *cs)) {
135 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, frame.cs, &le) &&
136 (le.HighWord.Bits.Type & 0x08)) { /* code segment */
137 /* it is very uncommon to push a code segment cs as
138 * a parameter, so this should work in most cases */
139 *cs = possible_cs = frame.cs;
143 frames[theframe].cs = addr->seg = *cs;
144 frames[theframe].eip = addr->off = frame.ip;
146 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 16,
149 DEBUG_FindNearestSymbol( addr, TRUE,
150 &frames[theframe].frame.sym, frame.bp,
151 &frames[theframe].frame.list);
152 frames[theframe].ss = addr->seg = ss;
153 frames[theframe].ebp = addr->off = frame.bp & ~1;
155 fprintf( stderr, " (bp=%04lx", addr->off );
157 fprintf( stderr, ", far call assumed" );
159 fprintf( stderr, ")\n" );
164 static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
166 unsigned int ss = addr->seg;
168 int theframe = nframe;
169 void* p = (void*)DEBUG_ToLinear(addr);
171 if (!p) return FALSE;
173 if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
175 fprintf(stderr,"*** Invalid address ");
176 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
177 fprintf(stderr,"\n");
181 if (!frame.ip) return FALSE;
184 frames = (struct bt_info *)DBG_realloc(frames,
185 nframe*sizeof(struct bt_info));
187 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
189 frames[theframe].cs = addr->seg = *cs;
190 frames[theframe].eip = addr->off = frame.ip;
192 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 32,
195 DEBUG_FindNearestSymbol( addr, TRUE,
196 &frames[theframe].frame.sym, frame.bp,
197 &frames[theframe].frame.list);
198 if (noisy) fprintf( stderr, " (ebp=%08lx)\n", frame.bp );
199 frames[theframe].ss = addr->seg = ss;
200 frames[theframe].ebp = frame.bp;
201 if (addr->off == frame.bp) return FALSE;
202 addr->off = frame.bp;
208 /***********************************************************************
211 * Display a stack back-trace.
213 void DEBUG_BackTrace(BOOL noisy)
216 DBG_ADDR addr, sw_addr, code, tmp;
217 unsigned int ss = DEBUG_context.SegSs;
218 unsigned int cs = DEBUG_context.SegCs;
219 int frameno = 0, is16, ok;
220 DWORD next_switch, cur_switch, p;
221 STACK16FRAME frame16;
222 STACK32FRAME frame32;
225 if (noisy) fprintf( stderr, "Backtrace:\n" );
228 if (frames) DBG_free( frames );
229 frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
231 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno);
233 if (DEBUG_IsSelectorSystem(ss)) ss = 0;
234 if (DEBUG_IsSelectorSystem(cs)) cs = 0;
236 /* first stack frame from registers */
237 switch (DEBUG_GetSelectorType(ss))
240 frames[0].cs = addr.seg = cs;
241 frames[0].eip = addr.off = DEBUG_context.Eip;
243 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
245 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
246 &frames[0].frame.list);
247 frames[0].ss = addr.seg = ss;
248 frames[0].ebp = addr.off = DEBUG_context.Ebp;
249 if (noisy) fprintf( stderr, " (ebp=%08x)\n", frames[0].ebp );
253 frames[0].cs = addr.seg = cs;
254 frames[0].eip = addr.off = LOWORD(DEBUG_context.Eip);
256 frames[0].frame = DEBUG_PrintAddress( &addr, 16, TRUE );
258 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
259 &frames[0].frame.list);
260 frames[0].ss = addr.seg = ss;
261 frames[0].ebp = addr.off = LOWORD(DEBUG_context.Ebp);
262 if (noisy) fprintf( stderr, " (bp=%04x)\n", frames[0].ebp );
266 if (noisy) fprintf( stderr, "Bad segment '%u'\n", ss);
270 /* cur_switch holds address of curr_stack's field in TEB in debuggee
273 cur_switch = (DWORD)DEBUG_CurrThread->teb + OFFSET_OF(TEB, cur_stack);
274 if (!DEBUG_READ_MEM((void*)cur_switch, &next_switch, sizeof(next_switch))) {
275 if (noisy) fprintf( stderr, "Can't read TEB:cur_stack\n");
280 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
281 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
282 (STACK32FRAME*)next_switch );
285 cur_switch = (DWORD)frame32.frame16;
286 sw_addr.seg = SELECTOROF(cur_switch);
287 sw_addr.off = OFFSETOF(cur_switch);
289 tmp.seg = SELECTOROF(next_switch);
290 tmp.off = OFFSETOF(next_switch);
291 p = DEBUG_ToLinear(&tmp);
293 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
294 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK16FRAME*)p );
297 cur_switch = (DWORD)frame16.frame32;
299 sw_addr.off = cur_switch;
301 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
302 sw_addr.seg = (DWORD)-1;
303 sw_addr.off = (DWORD)-1;
306 for (ok = TRUE; ok;) {
307 if ((frames[frameno].ss == sw_addr.seg) &&
308 (frames[frameno].ebp >= sw_addr.off)) {
310 * yes, I know this is confusing, it gave me a headache too */
313 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
314 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK32FRAME*)next_switch );
319 code.off = frame32.retaddr;
323 addr.off = frame32.ebp;
324 DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy );
326 next_switch = cur_switch;
327 tmp.seg = SELECTOROF(next_switch);
328 tmp.off = OFFSETOF(next_switch);
329 p = DEBUG_ToLinear(&tmp);
331 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
332 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
336 cur_switch = (DWORD)frame16.frame32;
338 sw_addr.off = cur_switch;
342 tmp.seg = SELECTOROF(next_switch);
343 tmp.off = OFFSETOF(next_switch);
344 p = DEBUG_ToLinear(&tmp);
346 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
347 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
353 code.seg = frame16.cs;
354 code.off = frame16.ip;
357 addr.seg = SELECTOROF(next_switch);
358 addr.off = frame16.bp;
359 DEBUG_ForceFrame( &addr, &code, ++frameno, 16, noisy );
361 next_switch = cur_switch;
362 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
363 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
364 (STACK32FRAME*)next_switch );
367 cur_switch = (DWORD)frame32.frame16;
368 sw_addr.seg = SELECTOROF(cur_switch);
369 sw_addr.off = OFFSETOF(cur_switch);
373 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
374 sw_addr.seg = (DWORD)-1;
375 sw_addr.off = (DWORD)-1;
378 /* ordinary stack frame */
379 ok = is16 ? DEBUG_Frame16( &addr, &cs, ++frameno, noisy)
380 : DEBUG_Frame32( &addr, &cs, ++frameno, noisy);
383 if (noisy) fprintf( stderr, "\n" );
388 DEBUG_SetFrame(int newframe)
392 curr_frame = newframe;
394 if( curr_frame >= nframe )
396 curr_frame = nframe - 1;
404 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
406 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
414 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
418 * If we don't have a valid backtrace, then just return.
426 * If we don't know what the current function is, then we also have
427 * nothing to report here.
429 if( frames[curr_frame].frame.sym == NULL )
434 *name = frames[curr_frame].frame.sym;
435 *eip = frames[curr_frame].eip;
436 *ebp = frames[curr_frame].ebp;