diff --git a/pkg/proc/native/proc.go b/pkg/proc/native/proc.go index 500072ec..336e760c 100644 --- a/pkg/proc/native/proc.go +++ b/pkg/proc/native/proc.go @@ -41,6 +41,7 @@ type Process struct { exited bool ptraceChan chan func() ptraceDoneChan chan interface{} + childProcess bool // this process was launched, not attached to } // New returns an initialized Process struct. Before returning, @@ -71,6 +72,14 @@ func (dbp *Process) Detach(kill bool) (err error) { if dbp.exited { return nil } + if kill && dbp.childProcess { + err := dbp.Kill() + if err != nil { + return err + } + dbp.bi.Close() + return nil + } if dbp.Running() { if err = dbp.Halt(); err != nil { return diff --git a/pkg/proc/native/proc_darwin.go b/pkg/proc/native/proc_darwin.go index ef870389..5333819e 100644 --- a/pkg/proc/native/proc_darwin.go +++ b/pkg/proc/native/proc_darwin.go @@ -75,6 +75,7 @@ func Launch(cmd []string, wd string) (*Process, error) { return nil, fmt.Errorf("could not fork/exec") } dbp.pid = pid + dbp.childProcess = true for i := range argvSlice { C.free(unsafe.Pointer(argvSlice[i])) } diff --git a/pkg/proc/native/proc_linux.go b/pkg/proc/native/proc_linux.go index eabc6b88..b04274ec 100644 --- a/pkg/proc/native/proc_linux.go +++ b/pkg/proc/native/proc_linux.go @@ -68,6 +68,7 @@ func Launch(cmd []string, wd string) (*Process, error) { return nil, err } dbp.pid = process.Process.Pid + dbp.childProcess = true _, _, err = dbp.wait(process.Process.Pid, 0) if err != nil { return nil, fmt.Errorf("waiting for target execve failed: %s", err) diff --git a/pkg/proc/native/proc_windows.go b/pkg/proc/native/proc_windows.go index 11461e96..5741479a 100644 --- a/pkg/proc/native/proc_windows.go +++ b/pkg/proc/native/proc_windows.go @@ -125,6 +125,7 @@ func Launch(cmd []string, wd string) (*Process, error) { sys.CloseHandle(sys.Handle(pi.Thread)) dbp.pid = int(pi.ProcessId) + dbp.childProcess = true return newDebugProcess(dbp, argv0Go) } diff --git a/pkg/proc/proc_test.go b/pkg/proc/proc_test.go index 0e10d779..f62e36c6 100644 --- a/pkg/proc/proc_test.go +++ b/pkg/proc/proc_test.go @@ -918,6 +918,20 @@ func TestKill(t *testing.T) { } } }) + withTestProcess("testprog", t, func(p proc.Process, fixture protest.Fixture) { + if err := p.Detach(true); err != nil { + t.Fatal(err) + } + if p.Exited() != true { + t.Fatal("expected process to have exited") + } + if runtime.GOOS == "linux" { + _, err := os.Open(fmt.Sprintf("/proc/%d/", p.Pid())) + if err == nil { + t.Fatal("process has not exited", p.Pid()) + } + } + }) } func testGSupportFunc(name string, t *testing.T, p proc.Process, fixture protest.Fixture) {