Release 980329
[wine/multimedia.git] / debugger / stack.c
blob37f6d5ad1ab475daf445b3d4fe5813b5ba4d0d31
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 */
8 #include <stdio.h>
9 #include <stdlib.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 ess;
23 unsigned int ebp;
24 struct symbol_info frame;
27 static int nframe;
28 static struct bt_info * frames = NULL;
29 int curr_frame;
31 typedef struct
33 WORD bp;
34 WORD ip;
35 WORD cs;
36 } FRAME16;
38 typedef struct
40 DWORD bp;
41 DWORD ip;
42 WORD cs;
43 } FRAME32;
47 /***********************************************************************
48 * DEBUG_InfoStack
50 * Dump the top of the stack
52 void DEBUG_InfoStack(void)
54 DBG_ADDR addr;
55 WORD ss;
57 fprintf(stderr,"Stack dump:\n");
58 GET_SS(ss);
59 if ((SS_reg(&DEBUG_context) == ss) ||
60 (GET_SEL_FLAGS(SS_reg(&DEBUG_context)) & LDT_FLAGS_32BIT))
61 { /* 32-bit mode */
62 addr.seg = 0;
63 addr.off = ESP_reg(&DEBUG_context);
64 addr.type = NULL;
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 addr.type = NULL;
72 DEBUG_ExamineMemory( &addr, 24, 'w' );
74 fprintf(stderr,"\n");
78 /***********************************************************************
79 * DEBUG_BackTrace
81 * Display a stack back-trace.
83 void DEBUG_BackTrace(void)
85 DBG_ADDR addr;
86 int frameno = 0;
87 WORD ss;
89 fprintf(stderr,"Backtrace:\n");
90 GET_SS(ss);
91 if (SS_reg(&DEBUG_context) == ss) /* 32-bit mode */
93 nframe = 1;
94 if (frames) free( frames );
95 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
96 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++);
98 addr.seg = 0;
99 addr.off = EIP_reg(&DEBUG_context);
100 frames[0].eip = addr.off;
101 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
102 fprintf( stderr, "\n" );
103 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
105 while (addr.off)
107 FRAME32 *frame = (FRAME32 *)addr.off;
108 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
109 if (!frame->ip) break;
110 nframe++;
111 frames = (struct bt_info *)xrealloc(frames,
112 nframe*sizeof(struct bt_info));
113 fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "),
114 frameno);
115 addr.off = frame->ip;
116 frames[frameno].eip = addr.off;
117 frames[frameno].ebp = frame->bp;
118 frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32,
119 frame->bp, TRUE );
120 frameno++;
121 fprintf( stderr, "\n" );
122 if (addr.off == frame->bp) break;
123 addr.off = frame->bp;
126 else /* 16-bit mode */
128 WORD ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context);
129 if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT)
131 fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" );
132 return;
134 fprintf( stderr,"%d ", frameno++ );
135 addr.seg = cs;
136 addr.off = IP_reg(&DEBUG_context);
137 DEBUG_PrintAddress( &addr, 16, TRUE );
138 fprintf( stderr, "\n" );
139 addr.seg = ss;
140 addr.off = BP_reg(&DEBUG_context) & ~1;
141 for (;;)
143 FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr);
144 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return;
145 if (!frame->bp) break;
146 if (frame->bp & 1) cs = frame->cs;
147 fprintf( stderr,"%d ", frameno++ );
148 addr.seg = cs;
149 addr.off = frame->ip;
150 DEBUG_PrintAddress( &addr, 16, TRUE );
151 fprintf( stderr, "\n" );
152 addr.seg = ss;
153 addr.off = frame->bp & ~1;
156 fprintf( stderr, "\n" );
159 /***********************************************************************
160 * DEBUG_SilentBackTrace
162 * Display a stack back-trace.
164 void DEBUG_SilentBackTrace(void)
166 WORD ss;
167 DBG_ADDR addr;
168 int frameno = 0;
170 nframe = 1;
171 if (frames) free( frames );
172 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
173 GET_SS(ss);
174 if (SS_reg(&DEBUG_context) == ss) /* 32-bit mode */
176 addr.seg = 0;
177 addr.off = EIP_reg(&DEBUG_context);
178 frames[0].eip = addr.off;
179 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
180 &frames[0].frame.list);
181 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
182 frameno++;
184 while (addr.off)
186 FRAME32 *frame = (FRAME32 *)addr.off;
187 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
188 if (!frame->ip) break;
189 nframe++;
190 frames = (struct bt_info *)xrealloc(frames,
191 nframe*sizeof(struct bt_info));
192 addr.off = frame->ip;
193 frames[frameno].eip = addr.off;
194 frames[frameno].ebp = frame->bp;
195 DEBUG_FindNearestSymbol( &addr, TRUE,
196 &frames[frameno].frame.sym, frame->bp,
197 &frames[frameno].frame.list);
198 frameno++;
199 addr.off = frame->bp;
202 else /* 16-bit mode */
205 * Not implemented here. I am not entirely sure how best to handle
206 * this stuff.
212 DEBUG_SetFrame(int newframe)
214 int rtn = FALSE;
216 curr_frame = newframe;
218 if( curr_frame >= nframe )
220 curr_frame = nframe - 1;
223 if( curr_frame < 0 )
225 curr_frame = 0;
228 if( frames[curr_frame].frame.list.sourcefile != NULL )
230 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
233 rtn = TRUE;
234 return (rtn);
238 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
239 unsigned int * ebp)
242 * If we don't have a valid backtrace, then just return.
244 if( frames == NULL )
246 return FALSE;
250 * If we don't know what the current function is, then we also have
251 * nothing to report here.
253 if( frames[curr_frame].frame.sym == NULL )
255 return FALSE;
258 *name = frames[curr_frame].frame.sym;
259 *eip = frames[curr_frame].eip;
260 *ebp = frames[curr_frame].ebp;
262 return TRUE;