Release 970509
[wine/multimedia.git] / debugger / stack.c
blobca2bcc788411d79382f3307a8771933e8ffcdf7b
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;
56 fprintf(stderr,"Stack dump:\n");
57 if ((SS_reg(&DEBUG_context) == WINE_DATA_SELECTOR) ||
58 (GET_SEL_FLAGS(SS_reg(&DEBUG_context)) & LDT_FLAGS_32BIT))
59 { /* 32-bit mode */
60 addr.seg = 0;
61 addr.off = ESP_reg(&DEBUG_context);
62 addr.type = NULL;
63 DEBUG_ExamineMemory( &addr, 24, 'x' );
65 else /* 16-bit mode */
67 addr.seg = SS_reg(&DEBUG_context);
68 addr.off = SP_reg(&DEBUG_context);
69 addr.type = NULL;
70 DEBUG_ExamineMemory( &addr, 24, 'w' );
72 fprintf(stderr,"\n");
76 /***********************************************************************
77 * DEBUG_BackTrace
79 * Display a stack back-trace.
81 void DEBUG_BackTrace(void)
83 DBG_ADDR addr;
84 int frameno = 0;
86 fprintf(stderr,"Backtrace:\n");
87 if (SS_reg(&DEBUG_context) == WINE_DATA_SELECTOR) /* 32-bit mode */
89 nframe = 1;
90 if (frames) free( frames );
91 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
92 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++);
94 addr.seg = 0;
95 addr.off = EIP_reg(&DEBUG_context);
96 frames[0].eip = addr.off;
97 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
98 fprintf( stderr, "\n" );
99 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
101 while (addr.off)
103 FRAME32 *frame = (FRAME32 *)addr.off;
104 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
105 if (!frame->ip) break;
106 nframe++;
107 frames = (struct bt_info *)xrealloc(frames,
108 nframe*sizeof(struct bt_info));
109 fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "),
110 frameno);
111 addr.off = frame->ip;
112 frames[frameno].eip = addr.off;
113 frames[frameno].ebp = frame->bp;
114 frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32,
115 frame->bp, TRUE );
116 frameno++;
117 fprintf( stderr, "\n" );
118 addr.off = frame->bp;
121 else /* 16-bit mode */
123 WORD ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context);
124 if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT)
126 fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" );
127 return;
129 fprintf( stderr,"%d ", frameno++ );
130 addr.seg = cs;
131 addr.off = IP_reg(&DEBUG_context);
132 DEBUG_PrintAddress( &addr, 16, TRUE );
133 fprintf( stderr, "\n" );
134 addr.seg = ss;
135 addr.off = BP_reg(&DEBUG_context) & ~1;
136 for (;;)
138 FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr);
139 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return;
140 if (!frame->bp) break;
141 if (frame->bp & 1) cs = frame->cs;
142 fprintf( stderr,"%d ", frameno++ );
143 addr.seg = cs;
144 addr.off = frame->ip;
145 DEBUG_PrintAddress( &addr, 16, TRUE );
146 fprintf( stderr, "\n" );
147 addr.seg = ss;
148 addr.off = frame->bp & ~1;
151 fprintf( stderr, "\n" );
154 /***********************************************************************
155 * DEBUG_SilentBackTrace
157 * Display a stack back-trace.
159 void DEBUG_SilentBackTrace(void)
161 DBG_ADDR addr;
162 int frameno = 0;
164 nframe = 1;
165 if (frames) free( frames );
166 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
168 if (SS_reg(&DEBUG_context) == WINE_DATA_SELECTOR) /* 32-bit mode */
170 addr.seg = 0;
171 addr.off = EIP_reg(&DEBUG_context);
172 frames[0].eip = addr.off;
173 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
174 &frames[0].frame.list);
175 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
176 frameno++;
178 while (addr.off)
180 FRAME32 *frame = (FRAME32 *)addr.off;
181 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
182 if (!frame->ip) break;
183 nframe++;
184 frames = (struct bt_info *)xrealloc(frames,
185 nframe*sizeof(struct bt_info));
186 addr.off = frame->ip;
187 frames[frameno].eip = addr.off;
188 frames[frameno].ebp = frame->bp;
189 DEBUG_FindNearestSymbol( &addr, TRUE,
190 &frames[frameno].frame.sym, frame->bp,
191 &frames[frameno].frame.list);
192 frameno++;
193 addr.off = frame->bp;
196 else /* 16-bit mode */
199 * Not implemented here. I am not entirely sure how best to handle
200 * this stuff.
206 DEBUG_SetFrame(int newframe)
208 int rtn = FALSE;
210 curr_frame = newframe;
212 if( curr_frame >= nframe )
214 curr_frame = nframe - 1;
217 if( curr_frame < 0 )
219 curr_frame = 0;
222 if( frames[curr_frame].frame.list.sourcefile != NULL )
224 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
227 rtn = TRUE;
228 return (rtn);
232 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
233 unsigned int * ebp)
236 * If we don't have a valid backtrace, then just return.
238 if( frames == NULL )
240 return FALSE;
244 * If we don't know what the current function is, then we also have
245 * nothing to report here.
247 if( frames[curr_frame].frame.sym == NULL )
249 return FALSE;
252 *name = frames[curr_frame].frame.sym;
253 *eip = frames[curr_frame].eip;
254 *ebp = frames[curr_frame].ebp;
256 return TRUE;