2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
14 #include "stackframe.h"
19 * We keep this info for each frame, so that we can
20 * find local variable information correctly.
28 struct symbol_info frame;
32 static struct bt_info * frames = NULL;
50 /***********************************************************************
53 * Dump the top of the stack
55 void DEBUG_InfoStack(void)
61 value.cookie = DV_TARGET;
62 value.addr.seg = DEBUG_context.SegSs;
63 value.addr.off = DEBUG_context.Esp;
65 DEBUG_Printf(DBG_CHN_MESG,"Stack dump:\n");
66 switch (DEBUG_GetSelectorType(value.addr.seg)) {
67 case 32: /* 32-bit mode */
68 DEBUG_ExamineMemory( &value, 24, 'x' );
70 case 16: /* 16-bit mode */
71 value.addr.off &= 0xffff;
72 DEBUG_ExamineMemory( &value, 24, 'w' );
75 DEBUG_Printf(DBG_CHN_MESG, "Bad segment (%ld)\n", value.addr.seg);
77 DEBUG_Printf(DBG_CHN_MESG,"\n");
82 static void DEBUG_ForceFrame(DBG_ADDR *stack, DBG_ADDR *code, int frameno, int bits, int noisy, const char *caveat)
84 int theframe = nframe++;
85 frames = (struct bt_info *)DBG_realloc(frames,
86 nframe*sizeof(struct bt_info));
88 DEBUG_Printf(DBG_CHN_MESG,"%s%d ", (theframe == curr_frame ? "=>" : " "),
90 frames[theframe].cs = code->seg;
91 frames[theframe].eip = code->off;
93 frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits,
96 DEBUG_FindNearestSymbol( code, TRUE,
97 &frames[theframe].frame.sym, stack->off,
98 &frames[theframe].frame.list);
99 frames[theframe].ss = stack->seg;
100 frames[theframe].ebp = stack->off;
102 DEBUG_Printf( DBG_CHN_MESG, (bits == 16) ? " (bp=%04lx%s)\n" : " (ebp=%08lx%s)\n", stack->off, caveat?caveat:"" );
106 static BOOL DEBUG_Frame16(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
108 unsigned int possible_cs = 0;
110 void* p = (void*)DEBUG_ToLinear(addr);
113 if (!p) return FALSE;
115 if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
116 if (noisy) DEBUG_InvalAddr(addr);
119 if (!frame.bp) return FALSE;
121 if (frame.bp & 1) *cs = frame.cs;
123 /* not explicitly marked as far call,
124 * but check whether it could be anyway */
125 if (((frame.cs&7)==7) && (frame.cs != *cs)) {
128 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, frame.cs, &le) &&
129 (le.HighWord.Bits.Type & 0x08)) { /* code segment */
130 /* it is very uncommon to push a code segment cs as
131 * a parameter, so this should work in most cases */
132 *cs = possible_cs = frame.cs;
138 addr->off = frame.bp & ~1;
139 DEBUG_ForceFrame(addr, &code, frameno, 16, noisy, possible_cs ? ", far call assumed" : NULL );
143 static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
146 void* p = (void*)DEBUG_ToLinear(addr);
148 DWORD old_bp = addr->off;
150 if (!p) return FALSE;
152 if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
153 if (noisy) DEBUG_InvalAddr(addr);
156 if (!frame.ip) return FALSE;
160 addr->off = frame.bp;
161 DEBUG_ForceFrame(addr, &code, frameno, 32, noisy, NULL);
162 if (addr->off == old_bp) return FALSE;
168 /***********************************************************************
171 * Display a stack back-trace.
173 void DEBUG_BackTrace(BOOL noisy)
176 DBG_ADDR addr, sw_addr, code, tmp;
177 unsigned int ss = DEBUG_context.SegSs;
178 unsigned int cs = DEBUG_context.SegCs;
179 int frameno = 0, is16, ok;
180 DWORD next_switch, cur_switch, p;
181 STACK16FRAME frame16;
182 STACK32FRAME frame32;
185 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Backtrace:\n" );
188 if (frames) DBG_free( frames );
189 /* frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) ); */
192 if (DEBUG_IsSelectorSystem(ss)) ss = 0;
193 if (DEBUG_IsSelectorSystem(cs)) cs = 0;
195 /* first stack frame from registers */
196 switch (DEBUG_GetSelectorType(ss))
200 code.off = DEBUG_context.Eip;
202 addr.off = DEBUG_context.Ebp;
203 DEBUG_ForceFrame( &addr, &code, frameno, 32, noisy, NULL );
204 if (!(code.seg || code.off)) {
205 /* trying to execute a null pointer... yuck...
206 * if it was a call to null, the return EIP should be
207 * available at SS:ESP, so let's try to retrieve it */
209 tmp.off = DEBUG_context.Esp;
210 if (DEBUG_READ_MEM((void *)DEBUG_ToLinear(&tmp), &code.off, sizeof(code.off))) {
211 DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy, ", null call assumed" );
218 code.off = LOWORD(DEBUG_context.Eip);
220 addr.off = LOWORD(DEBUG_context.Ebp);
221 DEBUG_ForceFrame( &addr, &code, frameno, 16, noisy, NULL );
225 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad segment '%u'\n", ss);
229 /* cur_switch holds address of curr_stack's field in TEB in debuggee
232 cur_switch = (DWORD)DEBUG_CurrThread->teb + OFFSET_OF(TEB, cur_stack);
233 if (!DEBUG_READ_MEM((void*)cur_switch, &next_switch, sizeof(next_switch))) {
234 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Can't read TEB:cur_stack\n");
239 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
240 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad stack frame 0x%08lx\n",
241 (unsigned long)(STACK32FRAME*)next_switch );
244 cur_switch = (DWORD)frame32.frame16;
245 sw_addr.seg = SELECTOROF(cur_switch);
246 sw_addr.off = OFFSETOF(cur_switch);
248 tmp.seg = SELECTOROF(next_switch);
249 tmp.off = OFFSETOF(next_switch);
250 p = DEBUG_ToLinear(&tmp);
252 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
253 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad stack frame 0x%08lx\n",
254 (unsigned long)(STACK16FRAME*)p );
257 cur_switch = (DWORD)frame16.frame32;
259 sw_addr.off = cur_switch;
261 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
262 sw_addr.seg = (DWORD)-1;
263 sw_addr.off = (DWORD)-1;
266 for (ok = TRUE; ok;) {
267 if ((frames[frameno].ss == sw_addr.seg) &&
268 (frames[frameno].ebp >= sw_addr.off)) {
270 * yes, I know this is confusing, it gave me a headache too */
273 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
274 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad stack frame 0x%08lx\n",
275 (unsigned long)(STACK32FRAME*)next_switch );
280 code.off = frame32.retaddr;
284 addr.off = frame32.ebp;
285 DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy, NULL );
287 next_switch = cur_switch;
288 tmp.seg = SELECTOROF(next_switch);
289 tmp.off = OFFSETOF(next_switch);
290 p = DEBUG_ToLinear(&tmp);
292 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
293 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad stack frame 0x%08lx\n",
294 (unsigned long)(STACK16FRAME*)p );
297 cur_switch = (DWORD)frame16.frame32;
299 sw_addr.off = cur_switch;
303 tmp.seg = SELECTOROF(next_switch);
304 tmp.off = OFFSETOF(next_switch);
305 p = DEBUG_ToLinear(&tmp);
307 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
308 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad stack frame 0x%08lx\n",
309 (unsigned long)(STACK16FRAME*)p );
313 code.seg = frame16.cs;
314 code.off = frame16.ip;
317 addr.seg = SELECTOROF(next_switch);
318 addr.off = frame16.bp;
319 DEBUG_ForceFrame( &addr, &code, ++frameno, 16, noisy, NULL );
321 next_switch = cur_switch;
322 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
323 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "Bad stack frame 0x%08lx\n",
324 (unsigned long)(STACK32FRAME*)next_switch );
327 cur_switch = (DWORD)frame32.frame16;
328 sw_addr.seg = SELECTOROF(cur_switch);
329 sw_addr.off = OFFSETOF(cur_switch);
333 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
334 sw_addr.seg = (DWORD)-1;
335 sw_addr.off = (DWORD)-1;
338 /* ordinary stack frame */
339 ok = is16 ? DEBUG_Frame16( &addr, &cs, ++frameno, noisy)
340 : DEBUG_Frame32( &addr, &cs, ++frameno, noisy);
343 if (noisy) DEBUG_Printf( DBG_CHN_MESG, "\n" );
348 DEBUG_SetFrame(int newframe)
353 curr_frame = newframe;
355 if( curr_frame >= nframe )
357 curr_frame = nframe - 1;
365 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
367 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
374 #endif /* __i386__ */
378 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
383 * If we don't have a valid backtrace, then just return.
391 * If we don't know what the current function is, then we also have
392 * nothing to report here.
394 if( frames[curr_frame].frame.sym == NULL )
399 *name = frames[curr_frame].frame.sym;
400 *eip = frames[curr_frame].eip;
401 *ebp = frames[curr_frame].ebp;
406 #endif /* __i386__ */