- implemented serial numbers for audio CDs and data CDs
[wine.git] / debugger / stack.c
blob4965e1bd073a35dea1a11cd439973857ead9897d
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
7 */
9 #include "config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "debugger.h"
13 #include "stackframe.h"
17 * We keep this info for each frame, so that we can
18 * find local variable information correctly.
20 struct bt_info
22 unsigned int cs;
23 unsigned int eip;
24 unsigned int ss;
25 unsigned int ebp;
26 struct symbol_info frame;
29 static int nframe;
30 static struct bt_info * frames = NULL;
31 int curr_frame;
33 typedef struct
35 WORD bp;
36 WORD ip;
37 WORD cs;
38 } FRAME16;
40 typedef struct
42 DWORD bp;
43 DWORD ip;
44 WORD cs;
45 } FRAME32;
49 /***********************************************************************
50 * DEBUG_InfoStack
52 * Dump the top of the stack
54 void DEBUG_InfoStack(void)
56 #ifdef __i386__
57 DBG_ADDR addr;
59 addr.type = NULL;
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))
65 { /* 32-bit mode */
66 DEBUG_ExamineMemory( &addr, 24, 'x' );
68 else /* 16-bit mode */
70 addr.off &= 0xffff;
71 DEBUG_ExamineMemory( &addr, 24, 'w' );
73 fprintf(stderr,"\n");
74 #endif
77 #ifdef __i386__
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));
83 if (noisy)
84 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
85 frameno);
86 frames[theframe].cs = code->seg;
87 frames[theframe].eip = code->off;
88 if (noisy)
89 frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits,
90 stack->off, TRUE );
91 else
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;
97 if (noisy) {
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) )) {
109 if (noisy) {
110 fprintf(stderr,"*** Invalid address ");
111 DEBUG_PrintAddress(addr, dbg_mode, FALSE);
112 fprintf(stderr,"\n");
114 return FALSE;
116 if (!frame->bp) return FALSE;
117 nframe++;
118 frames = (struct bt_info *)DBG_realloc(frames,
119 nframe*sizeof(struct bt_info));
120 if (noisy)
121 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
122 frameno);
123 if (frame->bp & 1) *cs = frame->cs;
124 else {
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)) {
128 ldt_entry tcs;
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;
139 if (noisy)
140 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 16,
141 frame->bp, TRUE );
142 else
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;
148 if (noisy) {
149 fprintf( stderr, " (bp=%04lx", addr->off );
150 if (possible_cs) {
151 fprintf( stderr, ", far call assumed" );
153 fprintf( stderr, ")\n" );
155 return TRUE;
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) )) {
165 if (noisy) {
166 fprintf(stderr,"*** Invalid address ");
167 DEBUG_PrintAddress(addr, dbg_mode, FALSE);
168 fprintf(stderr,"\n");
170 return FALSE;
172 if (!frame->ip) return FALSE;
173 nframe++;
174 frames = (struct bt_info *)DBG_realloc(frames,
175 nframe*sizeof(struct bt_info));
176 if (noisy)
177 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
178 frameno);
179 frames[theframe].cs = addr->seg = *cs;
180 frames[theframe].eip = addr->off = frame->ip;
181 if (noisy)
182 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 32,
183 frame->bp, TRUE );
184 else
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;
193 return TRUE;
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" );
205 nframe = 1;
206 if (frames) DBG_free( frames );
207 frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
208 if (noisy)
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);
219 if (noisy)
220 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
221 else
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 );
227 is16 = FALSE;
228 } else {
229 frames[0].cs = addr.seg = cs;
230 frames[0].eip = addr.off = LOWORD(EIP_reg(&DEBUG_context));
231 if (noisy)
232 frames[0].frame = DEBUG_PrintAddress( &addr, 16, TRUE );
233 else
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 );
239 is16 = TRUE;
242 next_switch = NtCurrentTeb()->cur_stack;
243 if (is16) {
244 if (IsBadReadPtr((STACK32FRAME*)next_switch, sizeof(STACK32FRAME))) {
245 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK32FRAME*)next_switch );
246 return;
248 cur_switch = (DWORD)((STACK32FRAME*)next_switch)->frame16;
249 sw_addr.seg = SELECTOROF(cur_switch);
250 sw_addr.off = OFFSETOF(cur_switch);
251 } else {
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) );
254 return;
256 cur_switch = (DWORD)((STACK16FRAME*)PTR_SEG_TO_LIN(next_switch))->frame32;
257 sw_addr.seg = ss;
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)) {
268 /* 16<->32 switch...
269 * yes, I know this is confusing, it gave me a headache too */
270 if (is16) {
271 STACK32FRAME *frame = (STACK32FRAME*)next_switch;
272 DBG_ADDR code;
274 if (IsBadReadPtr((STACK32FRAME*)next_switch, sizeof(STACK32FRAME))) {
275 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK32FRAME*)next_switch );
276 return;
278 code.type = NULL;
279 code.seg = 0;
280 code.off = frame->retaddr;
282 cs = 0;
283 addr.seg = 0;
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) );
290 return;
292 cur_switch = (DWORD)((STACK16FRAME*)PTR_SEG_TO_LIN(next_switch))->frame32;
293 sw_addr.seg = 0;
294 sw_addr.off = cur_switch;
296 is16 = FALSE;
297 } else {
298 STACK16FRAME *frame = (STACK16FRAME*)PTR_SEG_TO_LIN(next_switch);
299 DBG_ADDR code;
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) );
303 return;
306 code.type = NULL;
307 code.seg = frame->cs;
308 code.off = frame->ip;
310 cs = frame->cs;
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 );
318 return;
320 cur_switch = (DWORD)((STACK32FRAME*)next_switch)->frame16;
321 sw_addr.seg = SELECTOROF(cur_switch);
322 sw_addr.off = OFFSETOF(cur_switch);
324 is16 = TRUE;
326 if (DEBUG_IsBadReadPtr(&sw_addr,1)) {
327 sw_addr.seg = (DWORD)-1;
328 sw_addr.off = (DWORD)-1;
330 } else {
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" );
338 #endif
342 /***********************************************************************
343 * DEBUG_BackTrace
345 * Display a stack back-trace.
347 void DEBUG_BackTrace(void)
349 #ifdef __i386__
350 DEBUG_DoBackTrace( TRUE );
351 #endif
354 /***********************************************************************
355 * DEBUG_SilentBackTrace
357 * Display a stack back-trace.
359 void DEBUG_SilentBackTrace(void)
361 #ifdef __i386__
362 DEBUG_DoBackTrace( FALSE );
363 #endif
367 DEBUG_SetFrame(int newframe)
369 int rtn = FALSE;
371 curr_frame = newframe;
373 if( curr_frame >= nframe )
375 curr_frame = nframe - 1;
378 if( curr_frame < 0 )
380 curr_frame = 0;
383 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
385 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
388 rtn = TRUE;
389 return (rtn);
393 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
394 unsigned int * ebp)
397 * If we don't have a valid backtrace, then just return.
399 if( frames == NULL )
401 return FALSE;
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 )
410 return FALSE;
413 *name = frames[curr_frame].frame.sym;
414 *eip = frames[curr_frame].eip;
415 *ebp = frames[curr_frame].ebp;
417 return TRUE;