~laumann/hadb

32472ba4fa1b20611a2bbd1ec42d3462f82d0a9a — Thomas Bracht Laumann Jespersen 1 year, 3 months ago 99ebf7e
cmd/hadb: add breakpoint definition, and x86_64 specific things

The idea here is that for each target architecture, we keep a definition
of its breakpoint instruction (bpinst) and a bitmask we can use to
inject bpinst.
3 files changed, 36 insertions(+), 0 deletions(-)

A cmd/hadb/+x86_64/bp.ha
A cmd/hadb/bp.ha
M cmd/hadb/main.ha
A cmd/hadb/+x86_64/bp.ha => cmd/hadb/+x86_64/bp.ha +2 -0
@@ 0,0 1,2 @@
def bpinst: u64 = 0xcc;
def bpmask: u64 = ~0xff;

A cmd/hadb/bp.ha => cmd/hadb/bp.ha +25 -0
@@ 0,0 1,25 @@
use trace;

type breakpoint = struct {
	addr: uintptr,
	data: u64,
	enabled: bool,
};

fn bp_enable(pid: trace::pid_t, bp: *breakpoint) void = {
	if (bp.enabled)
		return;
	
	bp.data = trace::readmem(pid, bp.addr);
	let data = (bp.data & bpmask) | bpinst;
	trace::writemem(pid, bp.addr, data);
	bp.enabled = true;
};

fn bp_disable(pid: trace::pid_t, bp: *breakpoint) void = {
	if (!bp.enabled)
		return;
	
	trace::writemem(pid, bp.addr, bp.data);
	bp.enabled = false;
};

M cmd/hadb/main.ha => cmd/hadb/main.ha +9 -0
@@ 7,10 7,19 @@ use format::dwarf;
use trace;

export fn main() void = {
	fmt::printfln("bpinst = 0x{:02x}", bpinst)!;
	match (trace::fork()) {
	case let pid : trace::pid_t =>
		trace::waitpid(pid);
		time::sleep(2 * time::SECOND);

		//
		let bp = breakpoint {
			addr = 0x401126,
			...
		};
		//bp_enable(pid, &bp);

		fmt::printfln("continuing...")!;
		trace::cont(pid);
	case void =>