From 1727df4b1bbb3744ba6b61447144c10c687a5b32 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Wed, 15 Jul 2015 20:37:43 -0500 Subject: [PATCH] Fix: Properly attach to running process on OSX --- proc/proc.go | 9 --------- proc/proc_darwin.go | 15 +++++++++++++++ proc/proc_linux.go | 5 +++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/proc/proc.go b/proc/proc.go index 6da4c2db..5df25438 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -79,15 +79,6 @@ func (pe ProcessExitedError) Error() string { return fmt.Sprintf("Process %d has exited with status %d", pe.Pid, pe.Status) } -// Attach to an existing process with the given PID. -func Attach(pid int) (*Process, error) { - dbp, err := initializeDebugProcess(New(pid), "", true) - if err != nil { - return nil, err - } - return dbp, nil -} - // Detach from the process being debugged, optionally killing it. func (dbp *Process) Detach(kill bool) (err error) { if dbp.Running() { diff --git a/proc/proc_darwin.go b/proc/proc_darwin.go index ea30cae2..b41c8e79 100644 --- a/proc/proc_darwin.go +++ b/proc/proc_darwin.go @@ -82,6 +82,21 @@ func Launch(cmd []string) (*Process, error) { return dbp, err } +// Attach to an existing process with the given PID. +func Attach(pid int) (*Process, error) { + dbp := New(pid) + + kret := C.acquire_mach_task(C.int(pid), + &dbp.os.task, &dbp.os.portSet, &dbp.os.exceptionPort, + &dbp.os.notificationPort) + + if kret != C.KERN_SUCCESS { + return nil, fmt.Errorf("could not attach to %d", pid) + } + + return initializeDebugProcess(dbp, "", true) +} + func (dbp *Process) Kill() (err error) { err = sys.Kill(dbp.Pid, sys.SIGKILL) if err != nil { diff --git a/proc/proc_linux.go b/proc/proc_linux.go index fbb47916..268598b7 100644 --- a/proc/proc_linux.go +++ b/proc/proc_linux.go @@ -57,6 +57,11 @@ func Launch(cmd []string) (*Process, error) { return initializeDebugProcess(dbp, proc.Path, false) } +// Attach to an existing process with the given PID. +func Attach(pid int) (*Process, error) { + return initializeDebugProcess(New(pid), "", true) +} + func (dbp *Process) Kill() (err error) { if !stopped(dbp.Pid) { return errors.New("process must be stopped in order to kill it")