diff --git a/pkg/proc/native/proc.go b/pkg/proc/native/proc.go index 7630f0b0..8d6901f8 100644 --- a/pkg/proc/native/proc.go +++ b/pkg/proc/native/proc.go @@ -370,19 +370,7 @@ func (dbp *Process) FindBreakpoint(pc uint64) (*proc.Breakpoint, bool) { } // Returns a new Process struct. -func initializeDebugProcess(dbp *Process, path string, attach bool) (*Process, error) { - if attach { - var err error - dbp.execPtraceFunc(func() { err = PtraceAttach(dbp.pid) }) - if err != nil { - return nil, err - } - _, _, err = dbp.wait(dbp.pid, 0) - if err != nil { - return nil, err - } - } - +func initializeDebugProcess(dbp *Process, path string) (*Process, error) { process, err := os.FindProcess(dbp.pid) if err != nil { return nil, err diff --git a/pkg/proc/native/proc_darwin.go b/pkg/proc/native/proc_darwin.go index 838e9eaa..38e40051 100644 --- a/pkg/proc/native/proc_darwin.go +++ b/pkg/proc/native/proc_darwin.go @@ -116,7 +116,7 @@ func Launch(cmd []string, wd string) (*Process, error) { } dbp.os.initialized = true - dbp, err = initializeDebugProcess(dbp, argv0Go, false) + dbp, err = initializeDebugProcess(dbp, argv0Go) if err != nil { return nil, err } @@ -142,7 +142,17 @@ func Attach(pid int) (*Process, error) { dbp.os.initialized = true - return initializeDebugProcess(dbp, "", true) + var err error + dbp.execPtraceFunc(func() { err = PtraceAttach(dbp.pid) }) + if err != nil { + return nil, err + } + _, _, err = dbp.wait(dbp.pid, 0) + if err != nil { + return nil, err + } + + return initializeDebugProcess(dbp, "") } // Kill kills the process. diff --git a/pkg/proc/native/proc_linux.go b/pkg/proc/native/proc_linux.go index ec1875cd..a6257959 100644 --- a/pkg/proc/native/proc_linux.go +++ b/pkg/proc/native/proc_linux.go @@ -73,12 +73,24 @@ func Launch(cmd []string, wd string) (*Process, error) { if err != nil { return nil, fmt.Errorf("waiting for target execve failed: %s", err) } - return initializeDebugProcess(dbp, process.Path, false) + return initializeDebugProcess(dbp, process.Path) } // Attach to an existing process with the given PID. func Attach(pid int) (*Process, error) { - return initializeDebugProcess(New(pid), "", true) + dbp := New(pid) + + var err error + dbp.execPtraceFunc(func() { err = PtraceAttach(dbp.pid) }) + if err != nil { + return nil, err + } + _, _, err = dbp.wait(dbp.pid, 0) + if err != nil { + return nil, err + } + + return initializeDebugProcess(dbp, "") } // Kill kills the target process. diff --git a/pkg/proc/native/proc_windows.go b/pkg/proc/native/proc_windows.go index a237232e..b98bac2e 100644 --- a/pkg/proc/native/proc_windows.go +++ b/pkg/proc/native/proc_windows.go @@ -114,7 +114,7 @@ func newDebugProcess(dbp *Process, exepath string) (*Process, error) { return nil, err } - return initializeDebugProcess(dbp, exepath, false) + return initializeDebugProcess(dbp, exepath) } // findExePath searches for process pid, and returns its executable path. @@ -451,7 +451,7 @@ func (dbp *Process) detach(kill bool) error { } } } - return PtraceDetach(dbp.pid, 0) + return _DebugActiveProcessStop(uint32(dbp.pid)) } func killProcess(pid int) error { diff --git a/pkg/proc/native/ptrace_windows.go b/pkg/proc/native/ptrace_windows.go deleted file mode 100644 index 4d1a7439..00000000 --- a/pkg/proc/native/ptrace_windows.go +++ /dev/null @@ -1,13 +0,0 @@ -package native - -import ( - "fmt" -) - -func PtraceAttach(pid int) error { - return fmt.Errorf("not implemented: PtraceAttach") -} - -func PtraceDetach(tid, sig int) error { - return _DebugActiveProcessStop(uint32(tid)) -}