Page 1 of 1

Debugging exceptions without custom gdb stub

Posted: Wed May 07, 2014 1:30 pm
by Peterbjornx
Here's a hack to trick the debugger into thinking its in the faulting thread instead of the exception handler:

Code: Select all

void debug_postmortem_hook(void *state, size_t state_size, void *instr_addr)
{
	uint32_t eip = (uint32_t) instr_addr;
	i386_pusha_registers_t *regs = (i386_pusha_registers_t *) state;
	if (state_size != sizeof(i386_pusha_registers_t)) {
		earlycon_puts("CORRUPT STATE STRUCT\n");
		return;
	}
	asm("movl %0, %%esp;push %1;push %2;mov %%esp, %%ebp;cli;hlt"::"r"(regs->esp), "r"(eip), "r"(regs->ebp));
}
Explained (in pseudo-NASM syntax):

Code: Select all

; Switch to faulting thread's stack
mov  esp, <esp-from-pusha>
; Create fake call frame from exception instr to here
push <eip-from-exception>
push <ebp-from-pusha>
mov ebp, esp
; Halt processor so debugger can attach at any time from now on
cli
hlt