Fix: Properly attach to running process on OSX

This commit is contained in:
Derek Parker 2015-07-15 20:37:43 -05:00
parent b32afd92ad
commit 1727df4b1b
3 changed files with 20 additions and 9 deletions

@ -79,15 +79,6 @@ func (pe ProcessExitedError) Error() string {
return fmt.Sprintf("Process %d has exited with status %d", pe.Pid, pe.Status) 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. // Detach from the process being debugged, optionally killing it.
func (dbp *Process) Detach(kill bool) (err error) { func (dbp *Process) Detach(kill bool) (err error) {
if dbp.Running() { if dbp.Running() {

@ -82,6 +82,21 @@ func Launch(cmd []string) (*Process, error) {
return dbp, err 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) { func (dbp *Process) Kill() (err error) {
err = sys.Kill(dbp.Pid, sys.SIGKILL) err = sys.Kill(dbp.Pid, sys.SIGKILL)
if err != nil { if err != nil {

@ -57,6 +57,11 @@ func Launch(cmd []string) (*Process, error) {
return initializeDebugProcess(dbp, proc.Path, false) 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) { func (dbp *Process) Kill() (err error) {
if !stopped(dbp.Pid) { if !stopped(dbp.Pid) {
return errors.New("process must be stopped in order to kill it") return errors.New("process must be stopped in order to kill it")