Page 1 of 1

TrapFlag ignored

Posted: Wed Jan 11, 2017 1:27 pm
by robbiedobbie
I'm currently writing a gdb stub for 64bit to make debugging of my kernel easier on real hardware (Since some issues only occur on actual hardware, and not on a simulator).
The stub seems to work (I can read/write memory, registers and I can step through the code), except that stepping stops working at the moment I call a function in the code being debugged. I tried debugging my debugger through qemu, and it shows that indeed my interrupt handler is not being triggered anymore, while the TF flag is still set in the EFLAGS register.

My ISR assembly:

Code: Select all

%macro isr 2
align 8
global _isr_%1
_isr_%1:
        push 0
        pushaq
        mov rdi, %2
        mov rsi, rsp
        call fault_handler
        popaq
        add rsp,8
        iretq
%endmacro

section .text

extern fault_handler;

isr         divide_error, 0
isr         debug, 1
isr         nmi_interrupt, 2
isr         breakpoint, 3
My c fault handler:

Code: Select all

struct interrupt_state {
    uint64_t r15,r14,r13,r12,r11,r10;
    uint64_t r9,r8,rsi,rdi,rdx,rcx;
    uint64_t rbx,rax,rbp,errorCode;
    uint64_t rip, cs, rflags, rsp;
} __attribute__((packed));

void fault_handler(int num, struct interrupt_state* state) {
    if(gdbDebugInitialized) {
        gdbHandleException(num, state);
        return;
    }
//Left out some code here since it will never be called while the stub is active
}
My gdb stub stepping code:

Code: Select all

void gdbHandleException(int num, struct interrupt_state* state) {
    if(gdbInMemoryRoutine) {
        gdbErrorOccurred = true;
        return;
    }

    int sigval = computeSignal(num);
...
//Left out all code starting the loop and reading packages since the debug packages themselves are being read correctly
...

        case 's':
            stepping = true;
        case 'c':
            /* try to read optional parameter, pc unchanged if no parm */
            if (hexToInt (&ptr, &addr)) {
                state->rip = addr;
            }

            /* clear the trace bit */
            state->rflags &= 0xfffffeff;

            /* set the trace bit if we're stepping */
            //if (stepping) //Commented out so that we are always setting the flag after breakpoint (To see whether it still stops after each instruction)
                state->rflags |= 0x100;

            return;
Am I making some obvious mistake, or does someone know how this can happen?

Any help is appreciated!

Re: TrapFlag ignored

Posted: Mon Jan 16, 2017 6:15 am
by robbiedobbie
Anyone has a clue how this could happen?