Avoid SIGSTOP/SIGCONT race when ptrace is disabled.
[wine/wine-kai.git] / debugger / stack.c
blob46a2d1d1000af742f20f35269d3efd8eecf3751e
1 /*
2 * Debugger stack handling
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Eric Youngdale
6 * Copyright 1999 Ove Kåven
7 */
9 #include "config.h"
11 #include <stdio.h>
12 #include <stdlib.h>
14 #include "debugger.h"
15 #include "stackframe.h"
16 #include "winbase.h"
18 #ifdef __i386__
20 * We keep this info for each frame, so that we can
21 * find local variable information correctly.
23 struct bt_info
25 unsigned int cs;
26 unsigned int eip;
27 unsigned int ss;
28 unsigned int ebp;
29 struct symbol_info frame;
32 static int nframe;
33 static struct bt_info * frames = NULL;
34 int curr_frame;
36 typedef struct
38 WORD bp;
39 WORD ip;
40 WORD cs;
41 } FRAME16;
43 typedef struct
45 DWORD bp;
46 DWORD ip;
47 WORD cs;
48 } FRAME32;
49 #endif
52 /***********************************************************************
53 * DEBUG_InfoStack
55 * Dump the top of the stack
57 void DEBUG_InfoStack(void)
59 #ifdef __i386__
60 DBG_ADDR addr;
62 addr.type = NULL;
63 addr.seg = DEBUG_context.SegSs;
64 addr.off = DEBUG_context.Esp;
66 fprintf(stderr,"Stack dump:\n");
67 switch (DEBUG_GetSelectorType(addr.seg)) {
68 case 32: /* 32-bit mode */
69 DEBUG_ExamineMemory( &addr, 24, 'x' );
70 break;
71 case 16: /* 16-bit mode */
72 addr.off &= 0xffff;
73 DEBUG_ExamineMemory( &addr, 24, 'w' );
74 break;
75 default:
76 fprintf(stderr, "Bad segment (%ld)\n", addr.seg);
78 fprintf(stderr,"\n");
79 #endif
82 #ifdef __i386__
83 static void DEBUG_ForceFrame(DBG_ADDR *stack, DBG_ADDR *code, int frameno, int bits, int noisy)
85 int theframe = nframe++;
86 frames = (struct bt_info *)DBG_realloc(frames,
87 nframe*sizeof(struct bt_info));
88 if (noisy)
89 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
90 frameno);
91 frames[theframe].cs = code->seg;
92 frames[theframe].eip = code->off;
93 if (noisy)
94 frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits,
95 stack->off, TRUE );
96 else
97 DEBUG_FindNearestSymbol( code, TRUE,
98 &frames[theframe].frame.sym, stack->off,
99 &frames[theframe].frame.list);
100 frames[theframe].ss = stack->seg;
101 frames[theframe].ebp = stack->off;
102 if (noisy) {
103 fprintf( stderr, (bits == 16) ? " (bp=%04lx)\n" : " (ebp=%08lx)\n", stack->off );
107 static BOOL DEBUG_Frame16(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
109 unsigned int ss = addr->seg, possible_cs = 0;
110 FRAME16 frame;
111 int theframe = nframe;
112 void* p = (void*)DEBUG_ToLinear(addr);
114 if (!p) return FALSE;
116 if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
117 if (noisy) {
118 fprintf(stderr,"*** Invalid address ");
119 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
120 fprintf(stderr,"\n");
122 return FALSE;
124 if (!frame.bp) return FALSE;
125 nframe++;
126 frames = (struct bt_info *)DBG_realloc(frames,
127 nframe*sizeof(struct bt_info));
128 if (noisy)
129 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
130 frameno);
131 if (frame.bp & 1) *cs = frame.cs;
132 else {
133 /* not explicitly marked as far call,
134 * but check whether it could be anyway */
135 if (((frame.cs&7)==7) && (frame.cs != *cs)) {
136 LDT_ENTRY le;
138 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, frame.cs, &le) &&
139 (le.HighWord.Bits.Type & 0x08)) { /* code segment */
140 /* it is very uncommon to push a code segment cs as
141 * a parameter, so this should work in most cases */
142 *cs = possible_cs = frame.cs;
146 frames[theframe].cs = addr->seg = *cs;
147 frames[theframe].eip = addr->off = frame.ip;
148 if (noisy)
149 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 16,
150 frame.bp, TRUE );
151 else
152 DEBUG_FindNearestSymbol( addr, TRUE,
153 &frames[theframe].frame.sym, frame.bp,
154 &frames[theframe].frame.list);
155 frames[theframe].ss = addr->seg = ss;
156 frames[theframe].ebp = addr->off = frame.bp & ~1;
157 if (noisy) {
158 fprintf( stderr, " (bp=%04lx", addr->off );
159 if (possible_cs) {
160 fprintf( stderr, ", far call assumed" );
162 fprintf( stderr, ")\n" );
164 return TRUE;
167 static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy)
169 unsigned int ss = addr->seg;
170 FRAME32 frame;
171 int theframe = nframe;
172 void* p = (void*)DEBUG_ToLinear(addr);
174 if (!p) return FALSE;
176 if (!DEBUG_READ_MEM(p, &frame, sizeof(frame))) {
177 if (noisy) {
178 fprintf(stderr,"*** Invalid address ");
179 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
180 fprintf(stderr,"\n");
182 return FALSE;
184 if (!frame.ip) return FALSE;
186 nframe++;
187 frames = (struct bt_info *)DBG_realloc(frames,
188 nframe*sizeof(struct bt_info));
189 if (noisy)
190 fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "),
191 frameno);
192 frames[theframe].cs = addr->seg = *cs;
193 frames[theframe].eip = addr->off = frame.ip;
194 if (noisy)
195 frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 32,
196 frame.bp, TRUE );
197 else
198 DEBUG_FindNearestSymbol( addr, TRUE,
199 &frames[theframe].frame.sym, frame.bp,
200 &frames[theframe].frame.list);
201 if (noisy) fprintf( stderr, " (ebp=%08lx)\n", frame.bp );
202 frames[theframe].ss = addr->seg = ss;
203 frames[theframe].ebp = frame.bp;
204 if (addr->off == frame.bp) return FALSE;
205 addr->off = frame.bp;
206 return TRUE;
208 #endif
211 /***********************************************************************
212 * DEBUG_BackTrace
214 * Display a stack back-trace.
216 void DEBUG_BackTrace(BOOL noisy)
218 #ifdef __i386
219 DBG_ADDR addr, sw_addr, code, tmp;
220 unsigned int ss = DEBUG_context.SegSs;
221 unsigned int cs = DEBUG_context.SegCs;
222 int frameno = 0, is16, ok;
223 DWORD next_switch, cur_switch, p;
224 STACK16FRAME frame16;
225 STACK32FRAME frame32;
226 char ch;
228 if (noisy) fprintf( stderr, "Backtrace:\n" );
230 nframe = 1;
231 if (frames) DBG_free( frames );
232 frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) );
233 if (noisy)
234 fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno);
236 if (DEBUG_IsSelectorSystem(ss)) ss = 0;
237 if (DEBUG_IsSelectorSystem(cs)) cs = 0;
239 /* first stack frame from registers */
240 switch (DEBUG_GetSelectorType(ss))
242 case 32:
243 frames[0].cs = addr.seg = cs;
244 frames[0].eip = addr.off = DEBUG_context.Eip;
245 if (noisy)
246 frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE );
247 else
248 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
249 &frames[0].frame.list);
250 frames[0].ss = addr.seg = ss;
251 frames[0].ebp = addr.off = DEBUG_context.Ebp;
252 if (noisy) fprintf( stderr, " (ebp=%08x)\n", frames[0].ebp );
253 is16 = FALSE;
254 break;
255 case 16:
256 frames[0].cs = addr.seg = cs;
257 frames[0].eip = addr.off = LOWORD(DEBUG_context.Eip);
258 if (noisy)
259 frames[0].frame = DEBUG_PrintAddress( &addr, 16, TRUE );
260 else
261 DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0,
262 &frames[0].frame.list);
263 frames[0].ss = addr.seg = ss;
264 frames[0].ebp = addr.off = LOWORD(DEBUG_context.Ebp);
265 if (noisy) fprintf( stderr, " (bp=%04x)\n", frames[0].ebp );
266 is16 = TRUE;
267 break;
268 default:
269 if (noisy) fprintf( stderr, "Bad segment '%u'\n", ss);
270 return;
273 /* cur_switch holds address of curr_stack's field in TEB in debuggee
274 * address space
276 cur_switch = (DWORD)DEBUG_CurrThread->teb + OFFSET_OF(TEB, cur_stack);
277 if (!DEBUG_READ_MEM((void*)cur_switch, &next_switch, sizeof(next_switch))) {
278 if (noisy) fprintf( stderr, "Can't read TEB:cur_stack\n");
279 return;
282 if (is16) {
283 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
284 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
285 (STACK32FRAME*)next_switch );
286 return;
288 cur_switch = (DWORD)frame32.frame16;
289 sw_addr.seg = SELECTOROF(cur_switch);
290 sw_addr.off = OFFSETOF(cur_switch);
291 } else {
292 tmp.seg = SELECTOROF(next_switch);
293 tmp.off = OFFSETOF(next_switch);
294 p = DEBUG_ToLinear(&tmp);
296 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
297 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK16FRAME*)p );
298 return;
300 cur_switch = (DWORD)frame16.frame32;
301 sw_addr.seg = ss;
302 sw_addr.off = cur_switch;
304 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
305 sw_addr.seg = (DWORD)-1;
306 sw_addr.off = (DWORD)-1;
309 for (ok = TRUE; ok;) {
310 if ((frames[frameno].ss == sw_addr.seg) &&
311 (frames[frameno].ebp >= sw_addr.off)) {
312 /* 16<->32 switch...
313 * yes, I know this is confusing, it gave me a headache too */
314 if (is16) {
316 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
317 if (noisy) fprintf( stderr, "Bad stack frame %p\n", (STACK32FRAME*)next_switch );
318 return;
320 code.type = NULL;
321 code.seg = 0;
322 code.off = frame32.retaddr;
324 cs = 0;
325 addr.seg = 0;
326 addr.off = frame32.ebp;
327 DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy );
329 next_switch = cur_switch;
330 tmp.seg = SELECTOROF(next_switch);
331 tmp.off = OFFSETOF(next_switch);
332 p = DEBUG_ToLinear(&tmp);
334 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
335 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
336 (STACK16FRAME*)p );
337 return;
339 cur_switch = (DWORD)frame16.frame32;
340 sw_addr.seg = 0;
341 sw_addr.off = cur_switch;
343 is16 = FALSE;
344 } else {
345 tmp.seg = SELECTOROF(next_switch);
346 tmp.off = OFFSETOF(next_switch);
347 p = DEBUG_ToLinear(&tmp);
349 if (!DEBUG_READ_MEM((void*)p, &frame16, sizeof(STACK16FRAME))) {
350 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
351 (STACK16FRAME*)p );
352 return;
355 code.type = NULL;
356 code.seg = frame16.cs;
357 code.off = frame16.ip;
359 cs = frame16.cs;
360 addr.seg = SELECTOROF(next_switch);
361 addr.off = frame16.bp;
362 DEBUG_ForceFrame( &addr, &code, ++frameno, 16, noisy );
364 next_switch = cur_switch;
365 if (!DEBUG_READ_MEM((void*)next_switch, &frame32, sizeof(STACK32FRAME))) {
366 if (noisy) fprintf( stderr, "Bad stack frame %p\n",
367 (STACK32FRAME*)next_switch );
368 return;
370 cur_switch = (DWORD)frame32.frame16;
371 sw_addr.seg = SELECTOROF(cur_switch);
372 sw_addr.off = OFFSETOF(cur_switch);
374 is16 = TRUE;
376 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&sw_addr), &ch, sizeof(ch))) {
377 sw_addr.seg = (DWORD)-1;
378 sw_addr.off = (DWORD)-1;
380 } else {
381 /* ordinary stack frame */
382 ok = is16 ? DEBUG_Frame16( &addr, &cs, ++frameno, noisy)
383 : DEBUG_Frame32( &addr, &cs, ++frameno, noisy);
386 if (noisy) fprintf( stderr, "\n" );
387 #endif
391 DEBUG_SetFrame(int newframe)
393 int rtn = FALSE;
395 curr_frame = newframe;
397 if( curr_frame >= nframe )
399 curr_frame = nframe - 1;
402 if( curr_frame < 0 )
404 curr_frame = 0;
407 if( frames && frames[curr_frame].frame.list.sourcefile != NULL )
409 DEBUG_List(&frames[curr_frame].frame.list, NULL, 0);
412 rtn = TRUE;
413 return (rtn);
417 DEBUG_GetCurrentFrame(struct name_hash ** name, unsigned int * eip,
418 unsigned int * ebp)
421 * If we don't have a valid backtrace, then just return.
423 if( frames == NULL )
425 return FALSE;
429 * If we don't know what the current function is, then we also have
430 * nothing to report here.
432 if( frames[curr_frame].frame.sym == NULL )
434 return FALSE;
437 *name = frames[curr_frame].frame.sym;
438 *eip = frames[curr_frame].eip;
439 *ebp = frames[curr_frame].ebp;
441 return TRUE;