Fixed Filesystem documentation.
[wine/wine64.git] / debugger / stack.c
blobdee3aacf89c2ad44b54d73481c253b2ed78e280e
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 "debugger.h"
15 * We keep this info for each frame, so that we can
16 * find local variable information correctly.
18 struct bt_info
20 unsigned int eip;
21 unsigned int ess;
22 unsigned int ebp;
23 struct symbol_info frame;
26 static int nframe;
27 static struct bt_info * frames = NULL;
28 int curr_frame;
30 typedef struct
32 WORD bp;
33 WORD ip;
34 WORD cs;
35 } FRAME16;
37 typedef struct
39 DWORD bp;
40 DWORD ip;
41 WORD cs;
42 } FRAME32;
46 /***********************************************************************
47 * DEBUG_InfoStack
49 * Dump the top of the stack
51 void DEBUG_InfoStack(void)
53 DBG_ADDR addr = { NULL, SS_reg(&DEBUG_context), ESP_reg(&DEBUG_context) };
55 fprintf(stderr,"Stack dump:\n");
56 if (IS_SELECTOR_32BIT(addr.seg))
57 { /* 32-bit mode */
58 DEBUG_ExamineMemory( &addr, 24, 'x' );
60 else /* 16-bit mode */
62 addr.off &= 0xffff;
63 DEBUG_ExamineMemory( &addr, 24, 'w' );
65 fprintf(stderr,"\n");
69 /***********************************************************************
70 * DEBUG_BackTrace
72 * Display a stack back-trace.
74 void DEBUG_BackTrace(void)
76 DBG_ADDR addr;
77 int frameno = 0;
79 fprintf(stderr,"Backtrace:\n");
80 if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */
82 nframe = 1;
83 if (frames) free( frames );
84 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
85 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++);
87 addr.seg = 0;
88 addr.off = EIP_reg(&DEBUG_context);
89 frames[0].eip = addr.off;
90 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
91 fprintf( stderr, "\n" );
92 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
94 while (addr.off)
96 FRAME32 *frame = (FRAME32 *)addr.off;
97 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
98 if (!frame->ip) break;
99 nframe++;
100 frames = (struct bt_info *)xrealloc(frames,
101 nframe*sizeof(struct bt_info));
102 fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "),
103 frameno);
104 addr.off = frame->ip;
105 frames[frameno].eip = addr.off;
106 frames[frameno].ebp = frame->bp;
107 frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32,
108 frame->bp, TRUE );
109 frameno++;
110 fprintf( stderr, "\n" );
111 if (addr.off == frame->bp) break;
112 addr.off = frame->bp;
115 else /* 16-bit mode */
117 WORD ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context);
118 if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT)
120 fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" );
121 return;
123 fprintf( stderr,"%d ", frameno++ );
124 addr.seg = cs;
125 addr.off = IP_reg(&DEBUG_context);
126 DEBUG_PrintAddress( &addr, 16, TRUE );
127 fprintf( stderr, "\n" );
128 addr.seg = ss;
129 addr.off = BP_reg(&DEBUG_context) & ~1;
130 for (;;)
132 FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr);
133 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return;
134 if (!frame->bp) break;
135 if (frame->bp & 1) cs = frame->cs;
136 fprintf( stderr,"%d ", frameno++ );
137 addr.seg = cs;
138 addr.off = frame->ip;
139 DEBUG_PrintAddress( &addr, 16, TRUE );
140 fprintf( stderr, "\n" );
141 addr.seg = ss;
142 addr.off = frame->bp & ~1;
145 fprintf( stderr, "\n" );
148 /***********************************************************************
149 * DEBUG_SilentBackTrace
151 * Display a stack back-trace.
153 void DEBUG_SilentBackTrace(void)
155 DBG_ADDR addr;
156 int frameno = 0;
158 nframe = 1;
159 if (frames) free( frames );
160 frames = (struct bt_info *) xmalloc( sizeof(struct bt_info) );
161 if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */
163 addr.seg = 0;
164 addr.off = EIP_reg(&DEBUG_context);
165 frames[0].eip = addr.off;
166 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
167 &frames[0].frame.list);
168 frames[0].ebp = addr.off = EBP_reg(&DEBUG_context);
169 frameno++;
171 while (addr.off)
173 FRAME32 *frame = (FRAME32 *)addr.off;
174 if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return;
175 if (!frame->ip) break;
176 nframe++;
177 frames = (struct bt_info *)xrealloc(frames,
178 nframe*sizeof(struct bt_info));
179 addr.off = frame->ip;
180 frames[frameno].eip = addr.off;
181 frames[frameno].ebp = frame->bp;
182 DEBUG_FindNearestSymbol( &addr, TRUE,
183 &frames[frameno].frame.sym, frame->bp,
184 &frames[frameno].frame.list);
185 frameno++;
186 addr.off = frame->bp;
189 else /* 16-bit mode */
192 * Not implemented here. I am not entirely sure how best to handle
193 * this stuff.
199 DEBUG_SetFrame(int newframe)
201 int rtn = FALSE;
203 curr_frame = newframe;
205 if( curr_frame >= nframe )
207 curr_frame = nframe - 1;
210 if( curr_frame < 0 )
212 curr_frame = 0;
215 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
217 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
220 rtn = TRUE;
221 return (rtn);
225 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
226 unsigned int * ebp)
229 * If we don't have a valid backtrace, then just return.
231 if( frames == NULL )
233 return FALSE;
237 * If we don't know what the current function is, then we also have
238 * nothing to report here.
240 if( frames[curr_frame].frame.sym == NULL )
242 return FALSE;
245 *name = frames[curr_frame].frame.sym;
246 *eip = frames[curr_frame].eip;
247 *ebp = frames[curr_frame].ebp;
249 return TRUE;