Debugging exceptions without custom gdb stub

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Peterbjornx
Member
Member
Posts: 116
Joined: Thu May 06, 2010 4:34 am
Libera.chat IRC: peterbjornx
Location: Leiden, The Netherlands
Contact:

Debugging exceptions without custom gdb stub

Post 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
Post Reply