From 2ce7352aa46ac7d5fcbf039171f89bb3fc7f2c2e Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Sat, 4 Oct 2014 18:13:20 -0500 Subject: [PATCH] Simplify temp breakpoint cleanup --- proctl/proctl_linux_amd64.go | 49 ++++++++++++++++-------------------- proctl/proctl_test.go | 2 +- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/proctl/proctl_linux_amd64.go b/proctl/proctl_linux_amd64.go index 9d1a5418..dc05c109 100644 --- a/proctl/proctl_linux_amd64.go +++ b/proctl/proctl_linux_amd64.go @@ -23,17 +23,16 @@ import ( // Struct representing a debugged process. Holds onto pid, register values, // process struct and process state. type DebuggedProcess struct { - Pid int - Regs *syscall.PtraceRegs - Process *os.Process - ProcessState *syscall.WaitStatus - Executable *elf.File - Symbols []elf.Symbol - GoSymTable *gosym.Table - FrameEntries *frame.FrameDescriptionEntries - DebugLine *line.DebugLineInfo - BreakPoints map[uint64]*BreakPoint - TempBreakPoints map[uint64]*BreakPoint + Pid int + Regs *syscall.PtraceRegs + Process *os.Process + ProcessState *syscall.WaitStatus + Executable *elf.File + Symbols []elf.Symbol + GoSymTable *gosym.Table + FrameEntries *frame.FrameDescriptionEntries + DebugLine *line.DebugLineInfo + BreakPoints map[uint64]*BreakPoint } // Represents a single breakpoint. Stores information on the break @@ -81,12 +80,11 @@ func NewDebugProcess(pid int) (*DebuggedProcess, error) { } debuggedProc := DebuggedProcess{ - Pid: pid, - Regs: new(syscall.PtraceRegs), - Process: proc, - ProcessState: ps, - BreakPoints: make(map[uint64]*BreakPoint), - TempBreakPoints: make(map[uint64]*BreakPoint), + Pid: pid, + Regs: new(syscall.PtraceRegs), + Process: proc, + ProcessState: ps, + BreakPoints: make(map[uint64]*BreakPoint), } err = debuggedProc.LoadInformation() @@ -296,14 +294,13 @@ func (dbp *DebuggedProcess) Next() error { return err } } - dbp.TempBreakPoints[addr] = bp err = dbp.Continue() if err != nil { return err } - return dbp.clearTempBreakpoints() + return dbp.clearTempBreakpoint(bp.Addr) } for { @@ -349,22 +346,18 @@ func (dbp *DebuggedProcess) CurrentPC() (uint64, error) { return regs.Rip, nil } -func (dbp *DebuggedProcess) clearTempBreakpoints() error { +func (dbp *DebuggedProcess) clearTempBreakpoint(pc uint64) error { regs, err := dbp.Registers() if err != nil { return err } - bp, ok := dbp.PCtoBP(regs.PC() - 1) - for pc, _ := range dbp.TempBreakPoints { - _, err := dbp.Clear(pc) - if err != nil { - return err - } - delete(dbp.TempBreakPoints, pc) + _, err = dbp.Clear(pc) + if err != nil { + return err } - if ok { + if bp, ok := dbp.PCtoBP(regs.PC() - 1); ok { // Reset program counter to our restored instruction. regs.SetPC(bp.Addr) return syscall.PtraceSetRegs(dbp.Pid, regs) diff --git a/proctl/proctl_test.go b/proctl/proctl_test.go index 64450829..12181b71 100644 --- a/proctl/proctl_test.go +++ b/proctl/proctl_test.go @@ -205,7 +205,7 @@ func TestNext(t *testing.T) { } } - if len(p.TempBreakPoints) != 0 { + if len(p.BreakPoints) != 1 { t.Fatal("Not all breakpoints were cleaned up") }