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
= SS_reg(&DEBUG_context
);
61 addr
.off
= ESP_reg(&DEBUG_context
);
63 fprintf(stderr
,"Stack dump:\n");
64 if (IS_SELECTOR_32BIT(addr
.seg
))
66 DEBUG_ExamineMemory( &addr
, 24, 'x' );
68 else /* 16-bit mode */
71 DEBUG_ExamineMemory( &addr
, 24, 'w' );
78 static void DEBUG_ForceFrame(DBG_ADDR
*stack
, DBG_ADDR
*code
, int frameno
, int bits
, int noisy
)
80 int theframe
= nframe
++;
81 frames
= (struct bt_info
*)DBG_realloc(frames
,
82 nframe
*sizeof(struct bt_info
));
84 fprintf(stderr
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
86 frames
[theframe
].cs
= code
->seg
;
87 frames
[theframe
].eip
= code
->off
;
89 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( code
, bits
,
92 DEBUG_FindNearestSymbol( code
, TRUE
,
93 &frames
[theframe
].frame
.sym
, stack
->off
,
94 &frames
[theframe
].frame
.list
);
95 frames
[theframe
].ss
= stack
->seg
;
96 frames
[theframe
].ebp
= stack
->off
;
98 fprintf( stderr
, (bits
== 16) ? " (bp=%04lx)\n" : " (ebp=%08lx)\n", stack
->off
);
102 static BOOL
DEBUG_Frame16(DBG_ADDR
*addr
, unsigned int *cs
, int frameno
, int noisy
)
104 unsigned int ss
= addr
->seg
, possible_cs
= 0;
105 FRAME16
*frame
= (FRAME16
*)DBG_ADDR_TO_LIN(addr
);
106 int theframe
= nframe
;
108 if (DEBUG_IsBadReadPtr( addr
, sizeof(FRAME16
) )) {
110 fprintf(stderr
,"*** Invalid address ");
111 DEBUG_PrintAddress(addr
, dbg_mode
, FALSE
);
112 fprintf(stderr
,"\n");
116 if (!frame
->bp
) return FALSE
;
118 frames
= (struct bt_info
*)DBG_realloc(frames
,
119 nframe
*sizeof(struct bt_info
));
121 fprintf(stderr
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
123 if (frame
->bp
& 1) *cs
= frame
->cs
;
125 /* not explicitly marked as far call,
126 * but check whether it could be anyway */
127 if (((frame
->cs
&7)==7) && (frame
->cs
!= *cs
) && !IS_SELECTOR_FREE(frame
->cs
)) {
129 LDT_GetEntry( SELECTOR_TO_ENTRY(frame
->cs
), &tcs
);
130 if ( tcs
.type
== SEGMENT_CODE
) {
131 /* it is very uncommon to push a code segment cs as
132 * a parameter, so this should work in most cases */
133 *cs
= possible_cs
= frame
->cs
;
137 frames
[theframe
].cs
= addr
->seg
= *cs
;
138 frames
[theframe
].eip
= addr
->off
= frame
->ip
;
140 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( addr
, 16,
143 DEBUG_FindNearestSymbol( addr
, TRUE
,
144 &frames
[theframe
].frame
.sym
, frame
->bp
,
145 &frames
[theframe
].frame
.list
);
146 frames
[theframe
].ss
= addr
->seg
= ss
;
147 frames
[theframe
].ebp
= addr
->off
= frame
->bp
& ~1;
149 fprintf( stderr
, " (bp=%04lx", addr
->off
);
151 fprintf( stderr
, ", far call assumed" );
153 fprintf( stderr
, ")\n" );
158 static BOOL
DEBUG_Frame32(DBG_ADDR
*addr
, unsigned int *cs
, int frameno
, int noisy
)
160 unsigned int ss
= addr
->seg
;
161 FRAME32
*frame
= (FRAME32
*)DBG_ADDR_TO_LIN(addr
);
162 int theframe
= nframe
;
164 if (DEBUG_IsBadReadPtr( addr
, sizeof(FRAME32
) )) {
166 fprintf(stderr
,"*** Invalid address ");
167 DEBUG_PrintAddress(addr
, dbg_mode
, FALSE
);
168 fprintf(stderr
,"\n");
172 if (!frame
->ip
) return FALSE
;
174 frames
= (struct bt_info
*)DBG_realloc(frames
,
175 nframe
*sizeof(struct bt_info
));
177 fprintf(stderr
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
179 frames
[theframe
].cs
= addr
->seg
= *cs
;
180 frames
[theframe
].eip
= addr
->off
= frame
->ip
;
182 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( addr
, 32,
185 DEBUG_FindNearestSymbol( addr
, TRUE
,
186 &frames
[theframe
].frame
.sym
, frame
->bp
,
187 &frames
[theframe
].frame
.list
);
188 if (noisy
) fprintf( stderr
, " (ebp=%08lx)\n", frame
->bp
);
189 frames
[theframe
].ss
= addr
->seg
= ss
;
190 frames
[theframe
].ebp
= frame
->bp
;
191 if (addr
->off
== frame
->bp
) return FALSE
;
192 addr
->off
= frame
->bp
;
196 static void DEBUG_DoBackTrace(int noisy
)
198 DBG_ADDR addr
, sw_addr
;
199 unsigned int ss
= SS_reg(&DEBUG_context
), cs
= CS_reg(&DEBUG_context
);
200 int frameno
= 0, is16
, ok
;
201 DWORD next_switch
, cur_switch
;
203 if (noisy
) fprintf( stderr
, "Backtrace:\n" );
206 if (frames
) DBG_free( frames
);
207 frames
= (struct bt_info
*) DBG_alloc( sizeof(struct bt_info
) );
209 fprintf(stderr
,"%s%d ",(curr_frame
== 0 ? "=>" : " "), frameno
);
211 if (IS_SELECTOR_SYSTEM(ss
)) ss
= 0;
212 if (IS_SELECTOR_SYSTEM(cs
)) cs
= 0;
214 /* first stack frame from registers */
215 if (IS_SELECTOR_32BIT(ss
))
217 frames
[0].cs
= addr
.seg
= cs
;
218 frames
[0].eip
= addr
.off
= EIP_reg(&DEBUG_context
);
220 frames
[0].frame
= DEBUG_PrintAddress( &addr
, 32, TRUE
);
222 DEBUG_FindNearestSymbol( &addr
, TRUE
, &frames
[0].frame
.sym
, 0,
223 &frames
[0].frame
.list
);
224 frames
[0].ss
= addr
.seg
= ss
;
225 frames
[0].ebp
= addr
.off
= EBP_reg(&DEBUG_context
);
226 if (noisy
) fprintf( stderr
, " (ebp=%08x)\n", frames
[0].ebp
);
229 frames
[0].cs
= addr
.seg
= cs
;
230 frames
[0].eip
= addr
.off
= LOWORD(EIP_reg(&DEBUG_context
));
232 frames
[0].frame
= DEBUG_PrintAddress( &addr
, 16, TRUE
);
234 DEBUG_FindNearestSymbol( &addr
, TRUE
, &frames
[0].frame
.sym
, 0,
235 &frames
[0].frame
.list
);
236 frames
[0].ss
= addr
.seg
= ss
;
237 frames
[0].ebp
= addr
.off
= BP_reg(&DEBUG_context
);
238 if (noisy
) fprintf( stderr
, " (bp=%04x)\n", frames
[0].ebp
);
242 next_switch
= NtCurrentTeb()->cur_stack
;
244 if (IsBadReadPtr((STACK32FRAME
*)next_switch
, sizeof(STACK32FRAME
))) {
245 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK32FRAME
*)next_switch
);
248 cur_switch
= (DWORD
)((STACK32FRAME
*)next_switch
)->frame16
;
249 sw_addr
.seg
= SELECTOROF(cur_switch
);
250 sw_addr
.off
= OFFSETOF(cur_switch
);
252 if (IsBadReadPtr((STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
), sizeof(STACK16FRAME
))) {
253 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
) );
256 cur_switch
= (DWORD
)((STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
))->frame32
;
258 sw_addr
.off
= cur_switch
;
260 if (DEBUG_IsBadReadPtr(&sw_addr
,1)) {
261 sw_addr
.seg
= (DWORD
)-1;
262 sw_addr
.off
= (DWORD
)-1;
265 for (ok
= TRUE
; ok
;) {
266 if ((frames
[frameno
].ss
== sw_addr
.seg
) &&
267 (frames
[frameno
].ebp
>= sw_addr
.off
)) {
269 * yes, I know this is confusing, it gave me a headache too */
271 STACK32FRAME
*frame
= (STACK32FRAME
*)next_switch
;
274 if (IsBadReadPtr((STACK32FRAME
*)next_switch
, sizeof(STACK32FRAME
))) {
275 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK32FRAME
*)next_switch
);
280 code
.off
= frame
->retaddr
;
284 addr
.off
= frame
->ebp
;
285 DEBUG_ForceFrame( &addr
, &code
, ++frameno
, 32, noisy
);
287 next_switch
= cur_switch
;
288 if (IsBadReadPtr((STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
), sizeof(STACK16FRAME
))) {
289 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
) );
292 cur_switch
= (DWORD
)((STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
))->frame32
;
294 sw_addr
.off
= cur_switch
;
298 STACK16FRAME
*frame
= (STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
);
301 if (IsBadReadPtr((STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
), sizeof(STACK16FRAME
))) {
302 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK16FRAME
*)PTR_SEG_TO_LIN(next_switch
) );
307 code
.seg
= frame
->cs
;
308 code
.off
= frame
->ip
;
311 addr
.seg
= SELECTOROF(next_switch
);
312 addr
.off
= frame
->bp
;
313 DEBUG_ForceFrame( &addr
, &code
, ++frameno
, 16, noisy
);
315 next_switch
= cur_switch
;
316 if (IsBadReadPtr((STACK32FRAME
*)next_switch
, sizeof(STACK32FRAME
))) {
317 if (noisy
) fprintf( stderr
, "Bad stack frame %p\n", (STACK32FRAME
*)next_switch
);
320 cur_switch
= (DWORD
)((STACK32FRAME
*)next_switch
)->frame16
;
321 sw_addr
.seg
= SELECTOROF(cur_switch
);
322 sw_addr
.off
= OFFSETOF(cur_switch
);
326 if (DEBUG_IsBadReadPtr(&sw_addr
,1)) {
327 sw_addr
.seg
= (DWORD
)-1;
328 sw_addr
.off
= (DWORD
)-1;
331 /* ordinary stack frame */
332 ok
= is16
? DEBUG_Frame16( &addr
, &cs
, ++frameno
, noisy
)
333 : DEBUG_Frame32( &addr
, &cs
, ++frameno
, noisy
);
336 if (noisy
) fprintf( stderr
, "\n" );
342 /***********************************************************************
345 * Display a stack back-trace.
347 void DEBUG_BackTrace(void)
350 DEBUG_DoBackTrace( TRUE
);
354 /***********************************************************************
355 * DEBUG_SilentBackTrace
357 * Display a stack back-trace.
359 void DEBUG_SilentBackTrace(void)
362 DEBUG_DoBackTrace( FALSE
);
367 DEBUG_SetFrame(int newframe
)
371 curr_frame
= newframe
;
373 if( curr_frame
>= nframe
)
375 curr_frame
= nframe
- 1;
383 if( frames
&& frames
[curr_frame
].frame
.list
.sourcefile
!= NULL
)
385 DEBUG_List(&frames
[curr_frame
].frame
.list
, NULL
, 0);
393 DEBUG_GetCurrentFrame(struct name_hash
** name
, unsigned int * eip
,
397 * If we don't have a valid backtrace, then just return.
405 * If we don't know what the current function is, then we also have
406 * nothing to report here.
408 if( frames
[curr_frame
].frame
.sym
== NULL
)
413 *name
= frames
[curr_frame
].frame
.sym
;
414 *eip
= frames
[curr_frame
].eip
;
415 *ebp
= frames
[curr_frame
].ebp
;