Added the TEXT() and MAKELRESULT() definitions.
[wine/multimedia.git] / debugger / stack.c
blob2fe17c0328f40cd6b098f3f3e3a499b1a7ff345b
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 = { NULL, SS_reg(&DEBUG_context), ESP_reg(&DEBUG_context) };
56 fprintf(stderr,"Stack dump:\n");
57 if (IS_SELECTOR_32BIT(addr.seg))
58 { /* 32-bit mode */
59 DEBUG_ExamineMemory( &addr, 24, 'x' );
61 else /* 16-bit mode */
63 addr.off &= 0xffff;
64 DEBUG_ExamineMemory( &addr, 24, 'w' );
66 fprintf(stderr,"\n");
70 /***********************************************************************
71 * DEBUG_BackTrace
73 * Display a stack back-trace.
75 void DEBUG_BackTrace(void)
77 DBG_ADDR addr;
78 int frameno = 0;
80 fprintf(stderr,"Backtrace:\n");
81 if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */
83 nframe = 1;
84 if (frames) free( frames );
85 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
86 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++);
88 addr.seg = 0;
89 addr.off = EIP_reg(&DEBUG_context);
90 frames[0].eip = addr.off;
91 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
92 fprintf( stderr, "\n" );
93 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
95 while (addr.off)
97 FRAME32 *frame = (FRAME32 *)addr.off;
98 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
99 if (!frame->ip) break;
100 nframe++;
101 frames = (struct bt_info *)xrealloc(frames,
102 nframe*sizeof(struct bt_info));
103 fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "),
104 frameno);
105 addr.off = frame->ip;
106 frames[frameno].eip = addr.off;
107 frames[frameno].ebp = frame->bp;
108 frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32,
109 frame->bp, TRUE );
110 frameno++;
111 fprintf( stderr, "\n" );
112 if (addr.off == frame->bp) break;
113 addr.off = frame->bp;
116 else /* 16-bit mode */
118 WORD ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context);
119 if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT)
121 fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" );
122 return;
124 fprintf( stderr,"%d ", frameno++ );
125 addr.seg = cs;
126 addr.off = IP_reg(&DEBUG_context);
127 DEBUG_PrintAddress( &addr, 16, TRUE );
128 fprintf( stderr, "\n" );
129 addr.seg = ss;
130 addr.off = BP_reg(&DEBUG_context) & ~1;
131 for (;;)
133 FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr);
134 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return;
135 if (!frame->bp) break;
136 if (frame->bp & 1) cs = frame->cs;
137 fprintf( stderr,"%d ", frameno++ );
138 addr.seg = cs;
139 addr.off = frame->ip;
140 DEBUG_PrintAddress( &addr, 16, TRUE );
141 fprintf( stderr, "\n" );
142 addr.seg = ss;
143 addr.off = frame->bp & ~1;
146 fprintf( stderr, "\n" );
149 /***********************************************************************
150 * DEBUG_SilentBackTrace
152 * Display a stack back-trace.
154 void DEBUG_SilentBackTrace(void)
156 DBG_ADDR addr;
157 int frameno = 0;
159 nframe = 1;
160 if (frames) free( frames );
161 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
162 if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */
164 addr.seg = 0;
165 addr.off = EIP_reg(&DEBUG_context);
166 frames[0].eip = addr.off;
167 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
168 &frames[0].frame.list);
169 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
170 frameno++;
172 while (addr.off)
174 FRAME32 *frame = (FRAME32 *)addr.off;
175 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
176 if (!frame->ip) break;
177 nframe++;
178 frames = (struct bt_info *)xrealloc(frames,
179 nframe*sizeof(struct bt_info));
180 addr.off = frame->ip;
181 frames[frameno].eip = addr.off;
182 frames[frameno].ebp = frame->bp;
183 DEBUG_FindNearestSymbol( &addr, TRUE,
184 &frames[frameno].frame.sym, frame->bp,
185 &frames[frameno].frame.list);
186 frameno++;
187 addr.off = frame->bp;
190 else /* 16-bit mode */
193 * Not implemented here. I am not entirely sure how best to handle
194 * this stuff.
200 DEBUG_SetFrame(int newframe)
202 int rtn = FALSE;
204 curr_frame = newframe;
206 if( curr_frame >= nframe )
208 curr_frame = nframe - 1;
211 if( curr_frame < 0 )
213 curr_frame = 0;
216 if( frames[curr_frame].frame.list.sourcefile != NULL )
218 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
221 rtn = TRUE;
222 return (rtn);
226 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
227 unsigned int * ebp)
230 * If we don't have a valid backtrace, then just return.
232 if( frames == NULL )
234 return FALSE;
238 * If we don't know what the current function is, then we also have
239 * nothing to report here.
241 if( frames[curr_frame].frame.sym == NULL )
243 return FALSE;
246 *name = frames[curr_frame].frame.sym;
247 *eip = frames[curr_frame].eip;
248 *ebp = frames[curr_frame].ebp;
250 return TRUE;