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 addr
.seg
= DEBUG_context
.SegSs
;
64 addr
.off
= DEBUG_context
.Esp
;
66 fprintf(stderr
,"Stack dump:\n");
67 switch (DEBUG_GetSelectorType(addr
.seg
)) {
68 case 32: /* 32-bit mode */
69 DEBUG_ExamineMemory( &addr
, 24, 'x' );
71 case 16: /* 16-bit mode */
73 DEBUG_ExamineMemory( &addr
, 24, 'w' );
76 fprintf(stderr
, "Bad segment (%ld)\n", addr
.seg
);
83 static void DEBUG_ForceFrame(DBG_ADDR
*stack
, DBG_ADDR
*code
, int frameno
, int bits
, int noisy
)
85 int theframe
= nframe
++;
86 frames
= (struct bt_info
*)DBG_realloc(frames
,
87 nframe
*sizeof(struct bt_info
));
89 fprintf(stderr
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
91 frames
[theframe
].cs
= code
->seg
;
92 frames
[theframe
].eip
= code
->off
;
94 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( code
, bits
,
97 DEBUG_FindNearestSymbol( code
, TRUE
,
98 &frames
[theframe
].frame
.sym
, stack
->off
,
99 &frames
[theframe
].frame
.list
);
100 frames
[theframe
].ss
= stack
->seg
;
101 frames
[theframe
].ebp
= stack
->off
;
103 fprintf( stderr
, (bits
== 16) ? " (bp=%04lx)\n" : " (ebp=%08lx)\n", stack
->off
);
107 static BOOL
DEBUG_Frame16(DBG_ADDR
*addr
, unsigned int *cs
, int frameno
, int noisy
)
109 unsigned int ss
= addr
->seg
, possible_cs
= 0;
111 int theframe
= nframe
;
112 void* p
= (void*)DEBUG_ToLinear(addr
);
114 if (!p
) return FALSE
;
116 if (!DEBUG_READ_MEM(p
, &frame
, sizeof(frame
))) {
118 fprintf(stderr
,"*** Invalid address ");
119 DEBUG_PrintAddress(addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
120 fprintf(stderr
,"\n");
124 if (!frame
.bp
) return FALSE
;
126 frames
= (struct bt_info
*)DBG_realloc(frames
,
127 nframe
*sizeof(struct bt_info
));
129 fprintf(stderr
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
131 if (frame
.bp
& 1) *cs
= frame
.cs
;
133 /* not explicitly marked as far call,
134 * but check whether it could be anyway */
135 if (((frame
.cs
&7)==7) && (frame
.cs
!= *cs
)) {
138 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, frame
.cs
, &le
) &&
139 (le
.HighWord
.Bits
.Type
& 0x08)) { /* code segment */
140 /* it is very uncommon to push a code segment cs as
141 * a parameter, so this should work in most cases */
142 *cs
= possible_cs
= frame
.cs
;
146 frames
[theframe
].cs
= addr
->seg
= *cs
;
147 frames
[theframe
].eip
= addr
->off
= frame
.ip
;
149 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( addr
, 16,
152 DEBUG_FindNearestSymbol( addr
, TRUE
,
153 &frames
[theframe
].frame
.sym
, frame
.bp
,
154 &frames
[theframe
].frame
.list
);
155 frames
[theframe
].ss
= addr
->seg
= ss
;
156 frames
[theframe
].ebp
= addr
->off
= frame
.bp
& ~1;
158 fprintf( stderr
, " (bp=%04lx", addr
->off
);
160 fprintf( stderr
, ", far call assumed" );
162 fprintf( stderr
, ")\n" );
167 static BOOL
DEBUG_Frame32(DBG_ADDR
*addr
, unsigned int *cs
, int frameno
, int noisy
)
169 unsigned int ss
= addr
->seg
;
171 int theframe
= nframe
;
172 void* p
= (void*)DEBUG_ToLinear(addr
);
174 if (!p
) return FALSE
;
176 if (!DEBUG_READ_MEM(p
, &frame
, sizeof(frame
))) {
178 fprintf(stderr
,"*** Invalid address ");
179 DEBUG_PrintAddress(addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
180 fprintf(stderr
,"\n");
184 if (!frame
.ip
) return FALSE
;
187 frames
= (struct bt_info
*)DBG_realloc(frames
,
188 nframe
*sizeof(struct bt_info
));
190 fprintf(stderr
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
192 frames
[theframe
].cs
= addr
->seg
= *cs
;
193 frames
[theframe
].eip
= addr
->off
= frame
.ip
;
195 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( addr
, 32,
198 DEBUG_FindNearestSymbol( addr
, TRUE
,
199 &frames
[theframe
].frame
.sym
, frame
.bp
,
200 &frames
[theframe
].frame
.list
);
201 if (noisy
) fprintf( stderr
, " (ebp=%08lx)\n", frame
.bp
);
202 frames
[theframe
].ss
= addr
->seg
= ss
;
203 frames
[theframe
].ebp
= frame
.bp
;
204 if (addr
->off
== frame
.bp
) return FALSE
;
205 addr
->off
= frame
.bp
;
211 /***********************************************************************
214 * Display a stack back-trace.
216 void DEBUG_BackTrace(BOOL noisy
)
219 DBG_ADDR addr
, sw_addr
, code
, tmp
;
220 unsigned int ss
= DEBUG_context
.SegSs
;
221 unsigned int cs
= DEBUG_context
.SegCs
;
222 int frameno
= 0, is16
, ok
;
223 DWORD next_switch
, cur_switch
, p
;
224 STACK16FRAME frame16
;
225 STACK32FRAME frame32
;
228 if (noisy
) fprintf( stderr
, "Backtrace:\n" );
231 if (frames
) DBG_free( frames
);
232 frames
= (struct bt_info
*) DBG_alloc( sizeof(struct bt_info
) );
234 fprintf(stderr
,"%s%d ",(curr_frame
== 0 ? "=>" : " "), frameno
);
236 if (DEBUG_IsSelectorSystem(ss
)) ss
= 0;
237 if (DEBUG_IsSelectorSystem(cs
)) cs
= 0;
239 /* first stack frame from registers */
240 switch (DEBUG_GetSelectorType(ss
))
243 frames
[0].cs
= addr
.seg
= cs
;
244 frames
[0].eip
= addr
.off
= DEBUG_context
.Eip
;
246 frames
[0].frame
= DEBUG_PrintAddress( &addr
, 32, TRUE
);
248 DEBUG_FindNearestSymbol( &addr
, TRUE
, &frames
[0].frame
.sym
, 0,
249 &frames
[0].frame
.list
);
250 frames
[0].ss
= addr
.seg
= ss
;
251 frames
[0].ebp
= addr
.off
= DEBUG_context
.Ebp
;
252 if (noisy
) fprintf( stderr
, " (ebp=%08x)\n", frames
[0].ebp
);
256 frames
[0].cs
= addr
.seg
= cs
;
257 frames
[0].eip
= addr
.off
= LOWORD(DEBUG_context
.Eip
);
259 frames
[0].frame
= DEBUG_PrintAddress( &addr
, 16, TRUE
);
261 DEBUG_FindNearestSymbol( &addr
, TRUE
, &frames
[0].frame
.sym
, 0,
262 &frames
[0].frame
.list
);
263 frames
[0].ss
= addr
.seg
= ss
;
264 frames
[0].ebp
= addr
.off
= LOWORD(DEBUG_context
.Ebp
);
265 if (noisy
) fprintf( stderr
, " (bp=%04x)\n", frames
[0].ebp
);
269 if (noisy
) fprintf( stderr
, "Bad segment '%u'\n", ss
);
273 /* cur_switch holds address of curr_stack's field in TEB in debuggee
276 cur_switch
= (DWORD
)DEBUG_CurrThread
->teb
+ OFFSET_OF(TEB
, cur_stack
);
277 if (!DEBUG_READ_MEM((void*)cur_switch
, &next_switch
, sizeof(next_switch
))) {
278 if (noisy
) fprintf( stderr
, "Can't read TEB:cur_stack\n");
283 if (!DEBUG_READ_MEM((void*)next_switch
, &frame32
, sizeof(STACK32FRAME
))) {
284 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n",
285 (STACK32FRAME
*)next_switch
);
288 cur_switch
= (DWORD
)frame32
.frame16
;
289 sw_addr
.seg
= SELECTOROF(cur_switch
);
290 sw_addr
.off
= OFFSETOF(cur_switch
);
292 tmp
.seg
= SELECTOROF(next_switch
);
293 tmp
.off
= OFFSETOF(next_switch
);
294 p
= DEBUG_ToLinear(&tmp
);
296 if (!DEBUG_READ_MEM((void*)p
, &frame16
, sizeof(STACK16FRAME
))) {
297 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK16FRAME
*)p
);
300 cur_switch
= (DWORD
)frame16
.frame32
;
302 sw_addr
.off
= cur_switch
;
304 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr
), &ch
, sizeof(ch
))) {
305 sw_addr
.seg
= (DWORD
)-1;
306 sw_addr
.off
= (DWORD
)-1;
309 for (ok
= TRUE
; ok
;) {
310 if ((frames
[frameno
].ss
== sw_addr
.seg
) &&
311 (frames
[frameno
].ebp
>= sw_addr
.off
)) {
313 * yes, I know this is confusing, it gave me a headache too */
316 if (!DEBUG_READ_MEM((void*)next_switch
, &frame32
, sizeof(STACK32FRAME
))) {
317 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK32FRAME
*)next_switch
);
322 code
.off
= frame32
.retaddr
;
326 addr
.off
= frame32
.ebp
;
327 DEBUG_ForceFrame( &addr
, &code
, ++frameno
, 32, noisy
);
329 next_switch
= cur_switch
;
330 tmp
.seg
= SELECTOROF(next_switch
);
331 tmp
.off
= OFFSETOF(next_switch
);
332 p
= DEBUG_ToLinear(&tmp
);
334 if (!DEBUG_READ_MEM((void*)p
, &frame16
, sizeof(STACK16FRAME
))) {
335 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n",
339 cur_switch
= (DWORD
)frame16
.frame32
;
341 sw_addr
.off
= cur_switch
;
345 tmp
.seg
= SELECTOROF(next_switch
);
346 tmp
.off
= OFFSETOF(next_switch
);
347 p
= DEBUG_ToLinear(&tmp
);
349 if (!DEBUG_READ_MEM((void*)p
, &frame16
, sizeof(STACK16FRAME
))) {
350 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
;