From ccad114ed12f52b710aa287644c66653a045955a Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Fri, 3 Apr 2015 10:52:31 -0500 Subject: [PATCH] DRY code duplicated across OSes --- proctl/proctl.go | 21 +++++++++++++++++++++ proctl/proctl_darwin.go | 21 +-------------------- proctl/proctl_linux.go | 20 +------------------- 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/proctl/proctl.go b/proctl/proctl.go index bc1bb2ed..62d5d9e7 100644 --- a/proctl/proctl.go +++ b/proctl/proctl.go @@ -471,6 +471,27 @@ func (dbp *DebuggedProcess) clearTempBreakpoints() error { } return nil } +func (dbp *DebuggedProcess) handleBreakpointOnThread(id int) (*ThreadContext, *BreakPoint, error) { + thread, ok := dbp.Threads[id] + if !ok { + return nil, nil, fmt.Errorf("could not find thread for %d", id) + } + pc, err := thread.CurrentPC() + if err != nil { + return nil, nil, err + } + // Check for hardware breakpoint + for _, bp := range dbp.HWBreakPoints { + if bp != nil && bp.Addr == pc { + return thread, bp, nil + } + } + // Check to see if we have hit a software breakpoint. + if bp, ok := dbp.BreakPoints[pc-1]; ok { + return thread, bp, nil + } + return thread, nil, nil +} func (dbp *DebuggedProcess) run(fn func() error) error { if dbp.exited { diff --git a/proctl/proctl_darwin.go b/proctl/proctl_darwin.go index 47e0325d..c1bb7fd8 100644 --- a/proctl/proctl_darwin.go +++ b/proctl/proctl_darwin.go @@ -197,26 +197,7 @@ func trapWait(dbp *DebuggedProcess, pid int) (*ThreadContext, *BreakPoint, error // Since we cannot be notified of new threads on OS X // this is as good a time as any to check for them. dbp.updateThreadList() - thread, ok := dbp.Threads[int(port)] - if !ok { - return nil, nil, fmt.Errorf("could not find thread for %d", port) - } - pc, err := thread.CurrentPC() - if err != nil { - return nil, nil, err - } - // Check for hardware breakpoint - for _, bp := range dbp.HWBreakPoints { - if bp != nil && bp.Addr == pc { - return thread, bp, nil - } - } - // Check to see if we have hit a software breakpoint. - if bp, ok := dbp.BreakPoints[pc-1]; ok { - return thread, bp, nil - } - - return thread, nil, nil + return dbp.handleBreakpointOnThread(int(port)) } func wait(pid, options int) (int, *sys.WaitStatus, error) { diff --git a/proctl/proctl_linux.go b/proctl/proctl_linux.go index 0d72bccf..a7a4cfaf 100644 --- a/proctl/proctl_linux.go +++ b/proctl/proctl_linux.go @@ -258,25 +258,7 @@ func trapWait(dbp *DebuggedProcess, pid int) (*ThreadContext, *BreakPoint, error continue } if status.StopSignal() == sys.SIGTRAP { - thread, ok := dbp.Threads[wpid] - if !ok { - return nil, nil, fmt.Errorf("could not find thread for %d", wpid) - } - pc, err := thread.CurrentPC() - if err != nil { - return nil, nil, err - } - // Check for hardware breakpoint - for _, bp := range dbp.HWBreakPoints { - if bp != nil && bp.Addr == pc { - return thread, bp, nil - } - } - // Check to see if we have hit a software breakpoint. - if bp, ok := dbp.BreakPoints[pc-1]; ok { - return thread, bp, nil - } - return thread, nil, nil + return dbp.handleBreakpointOnThread(wpid) } if status.StopSignal() == sys.SIGSTOP && dbp.halt { return nil, nil, ManualStopError{}