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
))
68 case MODE_32
: /* 32-bit mode */
69 DEBUG_ExamineMemory( &value
, 24, 'x' );
71 case MODE_16
: /* 16-bit mode */
73 value
.addr
.off
&= 0xffff;
74 DEBUG_ExamineMemory( &value
, 24, 'w' );
77 DEBUG_Printf(DBG_CHN_MESG
, "Bad segment (%ld)\n", value
.addr
.seg
);
79 DEBUG_Printf(DBG_CHN_MESG
,"\n");
84 static void DEBUG_ForceFrame(DBG_ADDR
*stack
, DBG_ADDR
*code
, int frameno
, enum dbg_mode mode
,
85 int noisy
, const char *caveat
)
87 int theframe
= nframe
++;
88 frames
= (struct bt_info
*)DBG_realloc(frames
,
89 nframe
*sizeof(struct bt_info
));
91 DEBUG_Printf(DBG_CHN_MESG
,"%s%d ", (theframe
== curr_frame
? "=>" : " "),
93 frames
[theframe
].cs
= code
->seg
;
94 frames
[theframe
].eip
= code
->off
;
96 frames
[theframe
].frame
= DEBUG_PrintAddressAndArgs( code
, mode
, stack
->off
, TRUE
);
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 DEBUG_Printf( DBG_CHN_MESG
, (mode
!= MODE_32
) ? " (bp=%04lx%s)\n" : " (ebp=%08lx%s)\n",
105 stack
->off
, caveat
?caveat
:"" );
109 static BOOL
DEBUG_Frame16(DBG_THREAD
* thread
, DBG_ADDR
*addr
, unsigned int *cs
, int frameno
, int noisy
)
111 unsigned int possible_cs
= 0;
113 void* p
= (void*)DEBUG_ToLinear(addr
);
116 if (!p
) return FALSE
;
118 if (!DEBUG_READ_MEM(p
, &frame
, sizeof(frame
))) {
119 if (noisy
) DEBUG_InvalAddr(addr
);
122 if (!frame
.bp
) return FALSE
;
124 if (frame
.bp
& 1) *cs
= frame
.cs
;
126 /* not explicitly marked as far call,
127 * but check whether it could be anyway */
128 if (((frame
.cs
&7)==7) && (frame
.cs
!= *cs
)) {
131 if (GetThreadSelectorEntry( thread
->handle
, frame
.cs
, &le
) &&
132 (le
.HighWord
.Bits
.Type
& 0x08)) { /* code segment */
133 /* it is very uncommon to push a code segment cs as
134 * a parameter, so this should work in most cases */
135 *cs
= possible_cs
= frame
.cs
;
141 addr
->off
= frame
.bp
& ~1;
142 DEBUG_ForceFrame(addr
, &code
, frameno
, MODE_16
, noisy
,
143 possible_cs
? ", far call assumed" : NULL
);
147 static BOOL
DEBUG_Frame32(DBG_ADDR
*addr
, unsigned int *cs
, int frameno
, int noisy
)
150 void* p
= (void*)DEBUG_ToLinear(addr
);
152 DWORD old_bp
= addr
->off
;
154 if (!p
) return FALSE
;
156 if (!DEBUG_READ_MEM(p
, &frame
, sizeof(frame
))) {
157 if (noisy
) DEBUG_InvalAddr(addr
);
160 if (!frame
.ip
) return FALSE
;
164 addr
->off
= frame
.bp
;
165 DEBUG_ForceFrame(addr
, &code
, frameno
, MODE_32
, noisy
, NULL
);
166 if (addr
->off
== old_bp
) return FALSE
;
172 /***********************************************************************
175 * Display a stack back-trace.
177 void DEBUG_BackTrace(DWORD tid
, BOOL noisy
)
180 DBG_ADDR addr
, sw_addr
, code
, tmp
;
182 int frameno
= 0, is16
, ok
;
183 DWORD next_switch
, cur_switch
, p
;
184 STACK16FRAME frame16
;
185 STACK32FRAME frame32
;
191 int copy_curr_frame
= 0;
192 struct bt_info
* copy_frames
= NULL
;
194 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Backtrace:\n" );
196 if (tid
== DEBUG_CurrTid
)
199 thread
= DEBUG_CurrThread
;
201 if (frames
) DBG_free( frames
);
202 /* frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) ); */
206 thread
= DEBUG_GetThread(DEBUG_CurrProcess
, tid
);
210 DEBUG_Printf( DBG_CHN_MESG
, "Unknown thread id (0x%08lx) in current process\n", tid
);
213 memset(&ctx
, 0, sizeof(ctx
));
214 ctx
.ContextFlags
= CONTEXT_CONTROL
| CONTEXT_SEGMENTS
;
216 if ( SuspendThread( thread
->handle
) == -1 ||
217 !GetThreadContext( thread
->handle
, &ctx
))
219 DEBUG_Printf( DBG_CHN_MESG
, "Can't get context for thread id (0x%08lx) in current process\n", tid
);
222 /* need to avoid trashing stack frame for current thread */
223 copy_nframe
= nframe
;
224 copy_frames
= frames
;
225 copy_curr_frame
= curr_frame
;
235 if (DEBUG_IsSelectorSystem(ss
)) ss
= 0;
236 if (DEBUG_IsSelectorSystem(cs
)) cs
= 0;
238 /* first stack frame from registers */
239 switch (DEBUG_GetSelectorType(ss
))
246 DEBUG_ForceFrame( &addr
, &code
, frameno
, MODE_32
, noisy
, NULL
);
247 if (!(code
.seg
|| code
.off
)) {
248 /* trying to execute a null pointer... yuck...
249 * if it was a call to null, the return EIP should be
250 * available at SS:ESP, so let's try to retrieve it */
253 if (DEBUG_READ_MEM((void *)DEBUG_ToLinear(&tmp
), &code
.off
, sizeof(code
.off
))) {
254 DEBUG_ForceFrame( &addr
, &code
, ++frameno
, MODE_32
, noisy
, ", null call assumed" );
262 code
.off
= LOWORD(ctx
.Eip
);
264 addr
.off
= LOWORD(ctx
.Ebp
);
265 DEBUG_ForceFrame( &addr
, &code
, frameno
, MODE_16
, noisy
, NULL
);
269 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Bad segment '%x'\n", ss
);
273 /* cur_switch holds address of curr_stack's field in TEB in debuggee
276 cur_switch
= (DWORD
)thread
->teb
+ OFFSET_OF(TEB
, cur_stack
);
277 if (!DEBUG_READ_MEM((void*)cur_switch
, &next_switch
, sizeof(next_switch
))) {
278 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Can't read TEB:cur_stack\n");
283 if (!DEBUG_READ_MEM((void*)next_switch
, &frame32
, sizeof(STACK32FRAME
))) {
284 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Bad stack frame 0x%08lx\n",
285 (unsigned long)(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
) DEBUG_Printf( DBG_CHN_MESG
, "Bad stack frame 0x%08lx\n",
298 (unsigned long)(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 sw_addr
.off
&& (frames
[frameno
].ebp
>= sw_addr
.off
))
315 * yes, I know this is confusing, it gave me a headache too */
318 if (!DEBUG_READ_MEM((void*)next_switch
, &frame32
, sizeof(STACK32FRAME
))) {
319 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Bad stack frame 0x%08lx\n",
320 (unsigned long)(STACK32FRAME
*)next_switch
);
325 code
.off
= frame32
.retaddr
;
329 addr
.off
= frame32
.ebp
;
330 DEBUG_ForceFrame( &addr
, &code
, ++frameno
, MODE_32
, noisy
, NULL
);
332 next_switch
= cur_switch
;
333 tmp
.seg
= SELECTOROF(next_switch
);
334 tmp
.off
= OFFSETOF(next_switch
);
335 p
= DEBUG_ToLinear(&tmp
);
337 if (!DEBUG_READ_MEM((void*)p
, &frame16
, sizeof(STACK16FRAME
))) {
338 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Bad stack frame 0x%08lx\n",
339 (unsigned long)(STACK16FRAME
*)p
);
342 cur_switch
= (DWORD
)frame16
.frame32
;
344 sw_addr
.off
= cur_switch
;
348 tmp
.seg
= SELECTOROF(next_switch
);
349 tmp
.off
= OFFSETOF(next_switch
);
350 p
= DEBUG_ToLinear(&tmp
);
352 if (!DEBUG_READ_MEM((void*)p
, &frame16
, sizeof(STACK16FRAME
))) {
353 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Bad stack frame 0x%08lx\n",
354 (unsigned long)(STACK16FRAME
*)p
);
358 code
.seg
= frame16
.cs
;
359 code
.off
= frame16
.ip
;
362 addr
.seg
= SELECTOROF(next_switch
);
363 addr
.off
= frame16
.bp
;
364 DEBUG_ForceFrame( &addr
, &code
, ++frameno
, MODE_16
, noisy
, NULL
);
366 next_switch
= cur_switch
;
367 if (!DEBUG_READ_MEM((void*)next_switch
, &frame32
, sizeof(STACK32FRAME
))) {
368 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "Bad stack frame 0x%08lx\n",
369 (unsigned long)(STACK32FRAME
*)next_switch
);
372 cur_switch
= (DWORD
)frame32
.frame16
;
373 sw_addr
.seg
= SELECTOROF(cur_switch
);
374 sw_addr
.off
= OFFSETOF(cur_switch
);
378 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr
), &ch
, sizeof(ch
))) {
379 sw_addr
.seg
= (DWORD
)-1;
380 sw_addr
.off
= (DWORD
)-1;
383 /* ordinary stack frame */
384 ok
= is16
? DEBUG_Frame16( thread
, &addr
, &cs
, ++frameno
, noisy
)
385 : DEBUG_Frame32( &addr
, &cs
, ++frameno
, noisy
);
388 if (noisy
) DEBUG_Printf( DBG_CHN_MESG
, "\n" );
390 if (tid
!= DEBUG_CurrTid
)
392 ResumeThread( thread
->handle
);
393 /* restore stack frame for current thread */
394 if (frames
) DBG_free( frames
);
395 frames
= copy_frames
;
396 nframe
= copy_nframe
;
397 curr_frame
= copy_curr_frame
;
403 DEBUG_SetFrame(int newframe
)
408 curr_frame
= newframe
;
410 if( curr_frame
>= nframe
)
412 curr_frame
= nframe
- 1;
420 if( frames
&& frames
[curr_frame
].frame
.list
.sourcefile
!= NULL
)
422 DEBUG_List(&frames
[curr_frame
].frame
.list
, NULL
, 0);
429 #endif /* __i386__ */
433 DEBUG_GetCurrentFrame(struct name_hash
** name
, unsigned int * eip
,
438 * If we don't have a valid backtrace, then just return.
446 * If we don't know what the current function is, then we also have
447 * nothing to report here.
449 if( frames
[curr_frame
].frame
.sym
== NULL
)
454 *name
= frames
[curr_frame
].frame
.sym
;
455 *eip
= frames
[curr_frame
].eip
;
456 *ebp
= frames
[curr_frame
].ebp
;
461 #endif /* __i386__ */