From c731a8342476ccd3ac1b324cc34d69bfaf3114c5 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Tue, 6 Oct 2015 10:45:36 -0700 Subject: [PATCH] proc: Refactor ptrace calls on Darwin --- proc/proc.go | 2 +- proc/ptrace_darwin.go | 29 +++++++++-------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/proc/proc.go b/proc/proc.go index b32b4d3a..260898de 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -673,7 +673,7 @@ func (dbp *Process) run(fn func() error) error { func (dbp *Process) handlePtraceFuncs() { // We must ensure here that we are running on the same thread during - // the execution of dbg. This is due to the fact that ptrace(2) expects + // while invoking the ptrace(2) syscall. This is due to the fact that ptrace(2) expects // all commands after PTRACE_ATTACH to come from the same thread. runtime.LockOSThread() diff --git a/proc/ptrace_darwin.go b/proc/ptrace_darwin.go index 3c31451c..a6c3147d 100644 --- a/proc/ptrace_darwin.go +++ b/proc/ptrace_darwin.go @@ -1,31 +1,20 @@ package proc -import ( - "syscall" - - sys "golang.org/x/sys/unix" -) +import sys "golang.org/x/sys/unix" func PtraceDetach(tid, sig int) error { - _, _, err := sys.Syscall6(sys.SYS_PTRACE, sys.PT_DETACH, uintptr(tid), 1, uintptr(sig), 0, 0) - if err != syscall.Errno(0) { - return err - } - return nil + return ptrace(sys.PT_DETACH, tid, 1, uintptr(sig)) } func PtraceCont(tid, sig int) error { - _, _, err := sys.Syscall6(sys.SYS_PTRACE, sys.PTRACE_CONT, uintptr(tid), 1, uintptr(sig), 0, 0) - if err != syscall.Errno(0) { - return err - } - return nil + return ptrace(sys.PTRACE_CONT, tid, 1, 0) } func PtraceSingleStep(tid int) error { - _, _, err := sys.Syscall6(sys.SYS_PTRACE, sys.PT_STEP, uintptr(tid), 1, 0, 0, 0) - if err != syscall.Errno(0) { - return err - } - return nil + return ptrace(sys.PT_STEP, tid, 1, 0) +} + +func ptrace(request, pid int, addr uintptr, data uintptr) (err error) { + _, _, err = sys.Syscall6(sys.SYS_PTRACE, uintptr(request), uintptr(pid), uintptr(addr), uintptr(data), 0, 0) + return }