Improved Wine debugger backtrace. New features: - Displays ebp, in case you want to check stackframes manually - Detects far calls even when windows apps don't mark them - Tracks CallFrom16/CallTo16 relaying (thunking)
diff --git a/debugger/stack.c b/debugger/stack.c index 22577f1..6888f30 100644 --- a/debugger/stack.c +++ b/debugger/stack.c
@@ -3,12 +3,14 @@ * * Copyright 1995 Alexandre Julliard * Copyright 1996 Eric Youngdale + * Copyright 1999 Ove Kåven */ #include "config.h" #include <stdio.h> #include <stdlib.h> #include "debugger.h" +#include "stackframe.h" /* @@ -17,8 +19,9 @@ */ struct bt_info { + unsigned int cs; unsigned int eip; - unsigned int ess; + unsigned int ss; unsigned int ebp; struct symbol_info frame; }; @@ -70,6 +73,211 @@ } +static void DEBUG_ForceFrame(DBG_ADDR *stack, DBG_ADDR *code, int frameno, int bits, int noisy) +{ + int theframe = nframe++; + frames = (struct bt_info *)DBG_realloc(frames, + nframe*sizeof(struct bt_info)); + if (noisy) + fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "), + frameno); + frames[theframe].cs = code->seg; + frames[theframe].eip = code->off; + if (noisy) + frames[theframe].frame = DEBUG_PrintAddressAndArgs( code, bits, + stack->off, TRUE ); + else + DEBUG_FindNearestSymbol( code, TRUE, + &frames[theframe].frame.sym, stack->off, + &frames[theframe].frame.list); + frames[theframe].ss = stack->seg; + frames[theframe].ebp = stack->off; + if (noisy) { + fprintf( stderr, (bits == 16) ? " (bp=%04lx)\n" : " (ebp=%08lx)\n", stack->off ); + } +} + +static BOOL DEBUG_Frame16(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy) +{ + unsigned int ss = addr->seg, possible_cs = 0; + FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(addr); + int theframe = nframe; + + if (!DBG_CHECK_READ_PTR( addr, sizeof(FRAME16) )) return FALSE; + if (!frame->bp) return FALSE; + nframe++; + frames = (struct bt_info *)DBG_realloc(frames, + nframe*sizeof(struct bt_info)); + if (noisy) + fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "), + frameno); + if (frame->bp & 1) *cs = frame->cs; + else { + /* not explicitly marked as far call, + * but check whether it could be anyway */ + if (((frame->cs&7)==7) && (frame->cs != *cs) && !IS_SELECTOR_FREE(frame->cs)) { + ldt_entry tcs; + LDT_GetEntry( SELECTOR_TO_ENTRY(frame->cs), &tcs ); + if ( tcs.type == SEGMENT_CODE ) { + /* it is very uncommon to push a code segment cs as + * a parameter, so this should work in most cases */ + *cs = possible_cs = frame->cs; + } + } + } + frames[theframe].cs = addr->seg = *cs; + frames[theframe].eip = addr->off = frame->ip; + if (noisy) + frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 16, + frame->bp, TRUE ); + else + DEBUG_FindNearestSymbol( addr, TRUE, + &frames[theframe].frame.sym, frame->bp, + &frames[theframe].frame.list); + frames[theframe].ss = addr->seg = ss; + frames[theframe].ebp = addr->off = frame->bp & ~1; + if (noisy) { + fprintf( stderr, " (bp=%04lx", addr->off ); + if (possible_cs) { + fprintf( stderr, ", far call assumed" ); + } + fprintf( stderr, ")\n" ); + } + return TRUE; +} + +static BOOL DEBUG_Frame32(DBG_ADDR *addr, unsigned int *cs, int frameno, int noisy) +{ + unsigned int ss = addr->seg; + FRAME32 *frame = (FRAME32 *)DBG_ADDR_TO_LIN(addr); + int theframe = nframe; + + if (!DBG_CHECK_READ_PTR( addr, sizeof(FRAME32) )) return FALSE; + if (!frame->ip) return FALSE; + nframe++; + frames = (struct bt_info *)DBG_realloc(frames, + nframe*sizeof(struct bt_info)); + if (noisy) + fprintf(stderr,"%s%d ", (theframe == curr_frame ? "=>" : " "), + frameno); + frames[theframe].cs = addr->seg = *cs; + frames[theframe].eip = addr->off = frame->ip; + if (noisy) + frames[theframe].frame = DEBUG_PrintAddressAndArgs( addr, 32, + frame->bp, TRUE ); + else + DEBUG_FindNearestSymbol( addr, TRUE, + &frames[theframe].frame.sym, frame->bp, + &frames[theframe].frame.list); + if (noisy) fprintf( stderr, " (ebp=%08lx)\n", frame->bp ); + frames[theframe].ss = addr->seg = ss; + frames[theframe].ebp = frame->bp; + if (addr->off == frame->bp) return FALSE; + addr->off = frame->bp; + return TRUE; +} + +static void DEBUG_DoBackTrace(int noisy) +{ + DBG_ADDR addr, sw_addr; + unsigned int ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context); + int frameno = 0, is16, ok; + DWORD next_switch, cur_switch; + + if (noisy) fprintf( stderr, "Backtrace:\n" ); + + nframe = 1; + if (frames) DBG_free( frames ); + frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) ); + fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno); + + if (IS_SELECTOR_SYSTEM(ss)) ss = 0; + if (IS_SELECTOR_SYSTEM(cs)) cs = 0; + + /* first stack frame from registers */ + if (IS_SELECTOR_32BIT(ss)) + { + frames[0].cs = addr.seg = cs; + frames[0].eip = addr.off = EIP_reg(&DEBUG_context); + if (noisy) + frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE ); + else + DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0, + &frames[0].frame.list); + frames[0].ss = addr.seg = ss; + frames[0].ebp = addr.off = EBP_reg(&DEBUG_context); + if (noisy) fprintf( stderr, " (ebp=%08x)\n", frames[0].ebp ); + is16 = FALSE; + } else { + frames[0].cs = addr.seg = cs; + frames[0].eip = addr.off = IP_reg(&DEBUG_context); + if (noisy) + frames[0].frame = DEBUG_PrintAddress( &addr, 16, TRUE ); + else + DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0, + &frames[0].frame.list); + frames[0].ss = addr.seg = ss; + frames[0].ebp = addr.off = BP_reg(&DEBUG_context); + if (noisy) fprintf( stderr, " (bp=%04x)\n", frames[0].ebp ); + is16 = TRUE; + } + + next_switch = THREAD_Current()->cur_stack; + if (is16) { + cur_switch = (DWORD)((STACK32FRAME*)next_switch)->frame16; + sw_addr.seg = SELECTOROF(cur_switch); + sw_addr.off = OFFSETOF(cur_switch); + } else { + cur_switch = (DWORD)((STACK16FRAME*)PTR_SEG_TO_LIN(next_switch))->frame32; + sw_addr.seg = ss; + sw_addr.off = cur_switch; + } + + for (ok = TRUE; ok;) { + if ((frames[frameno].ss == sw_addr.seg) && + (frames[frameno].ebp >= sw_addr.off)) { + /* 16<->32 switch... + * yes, I know this is confusing, it gave me a headache too */ + if (is16) { + STACK32FRAME *frame = (STACK32FRAME*)next_switch; + DBG_ADDR code = { NULL, 0, frame->retaddr }; + + cs = 0; + addr.seg = 0; + addr.off = frame->ebp; + DEBUG_ForceFrame( &addr, &code, ++frameno, 32, noisy ); + + next_switch = cur_switch; + cur_switch = (DWORD)((STACK16FRAME*)PTR_SEG_TO_LIN(next_switch))->frame32; + sw_addr.seg = 0; + sw_addr.off = cur_switch; + + is16 = FALSE; + } else { + STACK16FRAME *frame = (STACK16FRAME*)PTR_SEG_TO_LIN(next_switch); + DBG_ADDR code = { NULL, frame->cs, frame->ip }; + + cs = frame->cs; + addr.seg = SELECTOROF(next_switch); + addr.off = frame->bp; + DEBUG_ForceFrame( &addr, &code, ++frameno, 16, noisy ); + + next_switch = cur_switch; + cur_switch = (DWORD)((STACK32FRAME*)next_switch)->frame16; + sw_addr.seg = SELECTOROF(cur_switch); + sw_addr.off = OFFSETOF(cur_switch); + + is16 = TRUE; + } + } else { + /* ordinary stack frame */ + ok = is16 ? DEBUG_Frame16( &addr, &cs, ++frameno, noisy) + : DEBUG_Frame32( &addr, &cs, ++frameno, noisy); + } + } + if (noisy) fprintf( stderr, "\n" ); +} + /*********************************************************************** * DEBUG_BackTrace * @@ -77,76 +285,7 @@ */ void DEBUG_BackTrace(void) { - DBG_ADDR addr; - int frameno = 0; - - fprintf(stderr,"Backtrace:\n"); - if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */ - { - nframe = 1; - if (frames) DBG_free( frames ); - frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) ); - fprintf(stderr,"%s%d ",(curr_frame == 0 ? "=>" : " "), frameno++); - - addr.seg = 0; - addr.off = EIP_reg(&DEBUG_context); - frames[0].eip = addr.off; - frames[0].frame = DEBUG_PrintAddress( &addr, 32, TRUE ); - fprintf( stderr, "\n" ); - frames[0].ebp = addr.off = EBP_reg(&DEBUG_context); - - while (addr.off) - { - FRAME32 *frame = (FRAME32 *)addr.off; - if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return; - if (!frame->ip) break; - nframe++; - frames = (struct bt_info *)DBG_realloc(frames, - nframe*sizeof(struct bt_info)); - fprintf(stderr,"%s%d ", (frameno == curr_frame ? "=>" : " "), - frameno); - addr.off = frame->ip; - frames[frameno].eip = addr.off; - frames[frameno].ebp = frame->bp; - frames[frameno].frame = DEBUG_PrintAddressAndArgs( &addr, 32, - frame->bp, TRUE ); - frameno++; - fprintf( stderr, "\n" ); - if (addr.off == frame->bp) break; - addr.off = frame->bp; - } - } - else /* 16-bit mode */ - { - WORD ss = SS_reg(&DEBUG_context), cs = CS_reg(&DEBUG_context); - if (GET_SEL_FLAGS(ss) & LDT_FLAGS_32BIT) - { - fprintf( stderr, "Not implemented: 32-bit backtrace on a different stack segment.\n" ); - return; - } - fprintf( stderr,"%d ", frameno++ ); - addr.seg = cs; - addr.off = IP_reg(&DEBUG_context); - DEBUG_PrintAddress( &addr, 16, TRUE ); - fprintf( stderr, "\n" ); - addr.seg = ss; - addr.off = BP_reg(&DEBUG_context) & ~1; - for (;;) - { - FRAME16 *frame = (FRAME16 *)DBG_ADDR_TO_LIN(&addr); - if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME16) )) return; - if (!frame->bp) break; - if (frame->bp & 1) cs = frame->cs; - fprintf( stderr,"%d ", frameno++ ); - addr.seg = cs; - addr.off = frame->ip; - DEBUG_PrintAddress( &addr, 16, TRUE ); - fprintf( stderr, "\n" ); - addr.seg = ss; - addr.off = frame->bp & ~1; - } - } - fprintf( stderr, "\n" ); + DEBUG_DoBackTrace( TRUE ); } /*********************************************************************** @@ -156,47 +295,7 @@ */ void DEBUG_SilentBackTrace(void) { - DBG_ADDR addr; - int frameno = 0; - - nframe = 1; - if (frames) DBG_free( frames ); - frames = (struct bt_info *) DBG_alloc( sizeof(struct bt_info) ); - if (IS_SELECTOR_SYSTEM(SS_reg(&DEBUG_context))) /* system stack */ - { - addr.seg = 0; - addr.off = EIP_reg(&DEBUG_context); - frames[0].eip = addr.off; - DEBUG_FindNearestSymbol( &addr, TRUE, &frames[0].frame.sym, 0, - &frames[0].frame.list); - frames[0].ebp = addr.off = EBP_reg(&DEBUG_context); - frameno++; - - while (addr.off) - { - FRAME32 *frame = (FRAME32 *)addr.off; - if (!DBG_CHECK_READ_PTR( &addr, sizeof(FRAME32) )) return; - if (!frame->ip) break; - nframe++; - frames = (struct bt_info *)DBG_realloc(frames, - nframe*sizeof(struct bt_info)); - addr.off = frame->ip; - frames[frameno].eip = addr.off; - frames[frameno].ebp = frame->bp; - DEBUG_FindNearestSymbol( &addr, TRUE, - &frames[frameno].frame.sym, frame->bp, - &frames[frameno].frame.list); - frameno++; - addr.off = frame->bp; - } - } - else /* 16-bit mode */ - { - /* - * Not implemented here. I am not entirely sure how best to handle - * this stuff. - */ - } + DEBUG_DoBackTrace( FALSE ); } int