Release 961222
[wine/multimedia.git] / debugger / stack.c
blob9ac2ea28825a7d3080fb39ba8b8291e47d805bfd
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 */
8 #include <stdio.h>
9 #include <malloc.h>
10 #include "xmalloc.h"
11 #include "windows.h"
12 #include "debugger.h"
16 * We keep this info for each frame, so that we can
17 * find local variable information correctly.
19 struct bt_info
21 unsigned int eip;
22 unsigned int ebp;
23 struct name_hash * frame;
26 static int nframe;
27 static struct bt_info * frames = NULL;
28 int curr_frame;
29 static char * reg_name[] =
31 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
34 typedef struct
36 WORD bp;
37 WORD ip;
38 WORD cs;
39 } FRAME16;
41 typedef struct
43 DWORD bp;
44 DWORD ip;
45 WORD cs;
46 } FRAME32;
50 /***********************************************************************
51 * DEBUG_InfoStack
53 * Dump the top of the stack
55 void DEBUG_InfoStack(void)
57 DBG_ADDR addr;
59 fprintf(stderr,"Stack dump:\n");
60 if ((SS_reg(DEBUG_context) == WINE_DATA_SELECTOR) ||
61 (GET_SEL_FLAGS(SS_reg(DEBUG_context)) & LDT_FLAGS_32BIT))
62 { /* 32-bit mode */
63 addr.seg = 0;
64 addr.off = ESP_reg(DEBUG_context);
65 DEBUG_ExamineMemory( &addr, 24, 'x' );
67 else /* 16-bit mode */
69 addr.seg = SS_reg(DEBUG_context);
70 addr.off = SP_reg(DEBUG_context);
71 DEBUG_ExamineMemory( &addr, 24, 'w' );
73 fprintf(stderr,"\n");
77 /***********************************************************************
78 * DEBUG_BackTrace
80 * Display a stack back-trace.
82 void DEBUG_BackTrace(void)
84 DBG_ADDR addr;
85 int frameno = 0;
87 fprintf(stderr,"Backtrace:\n");
88 if (SS_reg(DEBUG_context) == WINE_DATA_SELECTOR) /* 32-bit mode */
90 addr.seg = 0;
91 addr.off = EBP_reg(DEBUG_context);
92 nframe = 1;
93 while (addr.off)
95 FRAME32 *frame = (FRAME32 *)addr.off;
96 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
97 if (!frame->ip) break;
98 addr.off = frame->bp;
99 nframe++;
101 if( frames != NULL )
103 free(frames);
106 frames = (struct bt_info *) xmalloc(nframe
107 * sizeof(struct bt_info) );
108 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++);
109 addr.off = EIP_reg(DEBUG_context);
110 frames[0].eip = addr.off;
111 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
112 fprintf( stderr, "\n" );
113 addr.off = EBP_reg(DEBUG_context);
115 frames[0].ebp = addr.off;
117 while (addr.off)
119 FRAME32 *frame = (FRAME32 *)addr.off;
120 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
121 if (!frame->ip) break;
122 fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "),
123 frameno);
124 addr.off = frame->ip;
125 frames[frameno].eip = addr.off;
126 frames[frameno].ebp = frame->bp;
127 frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32,
128 frame->bp, TRUE );
129 frameno++;
130 fprintf( stderr, "\n" );
131 addr.off = frame->bp;
134 else /* 16-bit mode */
136 WORD ss = SS_reg(DEBUG_context), cs = CS_reg(DEBUG_context);
137 if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT)
139 fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" );
140 return;
142 fprintf( stderr,"%d ", frameno++ );
143 addr.seg = cs;
144 addr.off = IP_reg(DEBUG_context);
145 DEBUG_PrintAddress( &addr, 16, TRUE );
146 fprintf( stderr, "\n" );
147 addr.seg = ss;
148 addr.off = BP_reg(DEBUG_context) & ~1;
149 for (;;)
151 FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr);
152 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return;
153 if (!frame->bp) break;
154 if (frame->bp & 1) cs = frame->cs;
155 fprintf( stderr,"%d ", frameno++ );
156 addr.seg = cs;
157 addr.off = frame->ip;
158 DEBUG_PrintAddress( &addr, 16, TRUE );
159 fprintf( stderr, "\n" );
160 addr.seg = ss;
161 addr.off = frame->bp & ~1;
164 fprintf( stderr, "\n" );
167 /***********************************************************************
168 * DEBUG_GetSymbolValue
170 * Get the address of a named symbol from the current stack frame.
172 BOOL32 DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr )
174 struct name_hash * curr_func;
175 int i;
178 * If we don't have a valid backtrace, then just return.
180 if( frames == NULL )
182 return FALSE;
185 curr_func = frames[curr_frame].frame;
188 * If we don't know what the current function is, then we also have
189 * nothing to report here.
191 if( curr_func == NULL )
193 return FALSE;
196 for(i=0; i < curr_func->n_locals; i++ )
199 * Test the range of validity of the local variable. This
200 * comes up with RBRAC/LBRAC stabs in particular.
202 if( (curr_func->local_vars[i].pc_start != 0)
203 && ((frames[curr_frame].eip - curr_func->addr.off)
204 < curr_func->local_vars[i].pc_start) )
206 continue;
209 if( (curr_func->local_vars[i].pc_end != 0)
210 && ((frames[curr_frame].eip - curr_func->addr.off)
211 > curr_func->local_vars[i].pc_end) )
213 continue;
216 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
219 * OK, we found it. Now figure out what to do with this.
221 if( curr_func->local_vars[i].regno != 0 )
224 * Register variable. We don't know how to treat
225 * this yet.
227 return FALSE;
230 addr->seg = 0;
231 addr->off = frames[curr_frame].ebp + curr_func->local_vars[i].offset;
233 return TRUE;
236 return FALSE;
240 DEBUG_SetFrame(int newframe)
242 int rtn = FALSE;
244 * Nothing for now. Add support later.
247 curr_frame = newframe;
248 if( curr_frame < 0 )
250 curr_frame = 0;
253 if( curr_frame >= nframe )
255 curr_frame = nframe - 1;
258 rtn = TRUE;
259 return (rtn);
263 DEBUG_InfoLocals()
265 struct name_hash * curr_func;
266 int i;
267 int rtn = FALSE;
268 unsigned int * ptr;
271 * If we don't have a valid backtrace, then just return.
273 if( frames == NULL )
275 return FALSE;
278 curr_func = frames[curr_frame].frame;
281 * If we don't know what the current function is, then we also have
282 * nothing to report here.
284 if( curr_func == NULL )
286 return FALSE;
289 for(i=0; i < curr_func->n_locals; i++ )
292 * Test the range of validity of the local variable. This
293 * comes up with RBRAC/LBRAC stabs in particular.
295 if( (curr_func->local_vars[i].pc_start != 0)
296 && ((frames[curr_frame].eip - curr_func->addr.off)
297 < curr_func->local_vars[i].pc_start) )
299 continue;
302 if( (curr_func->local_vars[i].pc_end != 0)
303 && ((frames[curr_frame].eip - curr_func->addr.off)
304 > curr_func->local_vars[i].pc_end) )
306 continue;
309 if( curr_func->local_vars[i].offset == 0 )
311 fprintf(stderr, "%s:%s optimized into register $%s \n",
312 curr_func->name, curr_func->local_vars[i].name,
313 reg_name[curr_func->local_vars[i].regno]);
315 else
317 ptr = (unsigned int *) (frames[curr_frame].ebp
318 + curr_func->local_vars[i].offset);
319 fprintf(stderr, "%s:%s == 0x%8.8x\n",
320 curr_func->name, curr_func->local_vars[i].name,
321 *ptr);
325 rtn = TRUE;
327 return (rtn);