enhmetafile added
[wine/multimedia.git] / debugger / stack.c
blob22577f141b4c88d765348e0f3f714baec97938d1
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 */
8 #include "config.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "debugger.h"
15 * We keep this info for each frame, so that we can
16 * find local variable information correctly.
18 struct bt_info
20 unsigned int eip;
21 unsigned int ess;
22 unsigned int ebp;
23 struct symbol_info frame;
26 static int nframe;
27 static struct bt_info * frames = NULL;
28 int curr_frame;
30 typedef struct
32 WORD bp;
33 WORD ip;
34 WORD cs;
35 } FRAME16;
37 typedef struct
39 DWORD bp;
40 DWORD ip;
41 WORD cs;
42 } FRAME32;
46 /***********************************************************************
47 * DEBUG_InfoStack
49 * Dump the top of the stack
51 void DEBUG_InfoStack(void)
53 DBG_ADDR addr;
55 addr.type = NULL;
56 addr.seg = SS_reg(&DEBUG_context);
57 addr.off = ESP_reg(&DEBUG_context);
59 fprintf(stderr,"Stack dump:\n");
60 if (IS_SELECTOR_32BIT(addr.seg))
61 { /* 32-bit mode */
62 DEBUG_ExamineMemory( &addr, 24, 'x' );
64 else /* 16-bit mode */
66 addr.off &= 0xffff;
67 DEBUG_ExamineMemory( &addr, 24, 'w' );
69 fprintf(stderr,"\n");
73 /***********************************************************************
74 * DEBUG_BackTrace
76 * Display a stack back-trace.
78 void DEBUG_BackTrace(void)
80 DBG_ADDR addr;
81 int frameno = 0;
83 fprintf(stderr,"Backtrace:\n");
84 if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */
86 nframe = 1;
87 if (frames) DBG_free( frames );
88 frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
89 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++);
91 addr.seg = 0;
92 addr.off = EIP_reg(&DEBUG_context);
93 frames[0].eip = addr.off;
94 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
95 fprintf( stderr, "\n" );
96 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
98 while (addr.off)
100 FRAME32 *frame = (FRAME32 *)addr.off;
101 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
102 if (!frame->ip) break;
103 nframe++;
104 frames = (struct bt_info *)DBG_realloc(frames,
105 nframe*sizeof(struct bt_info));
106 fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "),
107 frameno);
108 addr.off = frame->ip;
109 frames[frameno].eip = addr.off;
110 frames[frameno].ebp = frame->bp;
111 frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32,
112 frame->bp, TRUE );
113 frameno++;
114 fprintf( stderr, "\n" );
115 if (addr.off == frame->bp) break;
116 addr.off = frame->bp;
119 else /* 16-bit mode */
121 WORD ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context);
122 if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT)
124 fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" );
125 return;
127 fprintf( stderr,"%d ", frameno++ );
128 addr.seg = cs;
129 addr.off = IP_reg(&DEBUG_context);
130 DEBUG_PrintAddress( &addr, 16, TRUE );
131 fprintf( stderr, "\n" );
132 addr.seg = ss;
133 addr.off = BP_reg(&DEBUG_context) & ~1;
134 for (;;)
136 FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr);
137 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return;
138 if (!frame->bp) break;
139 if (frame->bp & 1) cs = frame->cs;
140 fprintf( stderr,"%d ", frameno++ );
141 addr.seg = cs;
142 addr.off = frame->ip;
143 DEBUG_PrintAddress( &addr, 16, TRUE );
144 fprintf( stderr, "\n" );
145 addr.seg = ss;
146 addr.off = frame->bp & ~1;
149 fprintf( stderr, "\n" );
152 /***********************************************************************
153 * DEBUG_SilentBackTrace
155 * Display a stack back-trace.
157 void DEBUG_SilentBackTrace(void)
159 DBG_ADDR addr;
160 int frameno = 0;
162 nframe = 1;
163 if (frames) DBG_free( frames );
164 frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
165 if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */
167 addr.seg = 0;
168 addr.off = EIP_reg(&DEBUG_context);
169 frames[0].eip = addr.off;
170 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
171 &frames[0].frame.list);
172 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
173 frameno++;
175 while (addr.off)
177 FRAME32 *frame = (FRAME32 *)addr.off;
178 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
179 if (!frame->ip) break;
180 nframe++;
181 frames = (struct bt_info *)DBG_realloc(frames,
182 nframe*sizeof(struct bt_info));
183 addr.off = frame->ip;
184 frames[frameno].eip = addr.off;
185 frames[frameno].ebp = frame->bp;
186 DEBUG_FindNearestSymbol( &addr, TRUE,
187 &frames[frameno].frame.sym, frame->bp,
188 &frames[frameno].frame.list);
189 frameno++;
190 addr.off = frame->bp;
193 else /* 16-bit mode */
196 * Not implemented here. I am not entirely sure how best to handle
197 * this stuff.
203 DEBUG_SetFrame(int newframe)
205 int rtn = FALSE;
207 curr_frame = newframe;
209 if( curr_frame >= nframe )
211 curr_frame = nframe - 1;
214 if( curr_frame < 0 )
216 curr_frame = 0;
219 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
221 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
224 rtn = TRUE;
225 return (rtn);
229 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
230 unsigned int * ebp)
233 * If we don't have a valid backtrace, then just return.
235 if( frames == NULL )
237 return FALSE;
241 * If we don't know what the current function is, then we also have
242 * nothing to report here.
244 if( frames[curr_frame].frame.sym == NULL )
246 return FALSE;
249 *name = frames[curr_frame].frame.sym;
250 *eip = frames[curr_frame].eip;
251 *ebp = frames[curr_frame].ebp;
253 return TRUE;