diff --git a/pkg/proc/native/proc.go b/pkg/proc/native/proc.go index 7630f0b06f8b422e18a683795b28da5d478eb663..8d6901f8f677d28d48f53e28dd9f8629e2a9acfd 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 838e9eaaad51bc2467ac095d641e10b22489476a..38e400511e4f0d29da94c7310fa50f9b9a659e76 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 ec1875cd17b94d86a2d39179a583e7072f85c982..a62579599536c1ff25c0f6c71dc22ecad1fc1a1b 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 a237232e066a499fc468eacd9c9a5199bb897030..b98bac2ee64dad50a1688ec1e3c1298b5c5c4752 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 4d1a7439a4b8a1d6e202be9ad62e1a183c4ea17f..0000000000000000000000000000000000000000 --- 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)) -}