{"record":{"id":"95375c08e25923ce","repo":"go-delve/delve","slug":"could-not-attach-to-d","errorCode":null,"errorMessage":"could not attach to %d","messagePattern":"could not attach to (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/native/proc_darwin.go","lineNumber":161,"sourceCode":"\treturn 0, proc.ErrWaitForNotImplemented\n}\n\n// Attach to an existing process with the given PID.\nfunc Attach(pid int, waitFor *proc.WaitFor, _ []string) (*proc.TargetGroup, error) {\n\tif waitFor.Valid() {\n\t\treturn nil, proc.ErrWaitForNotImplemented\n\t}\n\tif err := macutil.CheckRosetta(); err != nil {\n\t\treturn nil, err\n\t}\n\tdbp := newProcess(pid)\n\n\tkret := C.acquire_mach_task(C.int(pid),\n\t\t&dbp.os.task, &dbp.os.portSet, &dbp.os.exceptionPort,\n\t\t&dbp.os.notificationPort)\n\n\tif kret != C.KERN_SUCCESS {\n\t\treturn nil, fmt.Errorf(\"could not attach to %d\", pid)\n\t}\n\n\tdbp.os.initialized = true\n\n\tvar err error\n\tdbp.execPtraceFunc(func() { err = ptraceAttach(dbp.pid) })\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\t_, _, err = dbp.wait(dbp.pid, 0)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\ttgt, err := dbp.initialize(\"\", []string{})\n\tif err != nil {\n\t\tdetachWithoutGroup(dbp, false)\n\t\treturn nil, err","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/native/proc_darwin.go#L143-L179","documentation":"On macOS, attaching requires acquiring the target's mach task port via acquire_mach_task. If the mach call does not return KERN_SUCCESS, Delve cannot control the process and Attach fails with \"could not attach to <pid>\". This is the mach-level equivalent of a ptrace attach denial.","triggerScenarios":"Calling `dlv attach <pid>` (proc.Attach) on macOS when acquire_mach_task returns non-KERN_SUCCESS: wrong pid, process owned by another user, target already being debugged, or SIP protection.","commonSituations":"Attaching to a process started by another user or by launchd; attaching to a pid that already exited (race between finding the pid and attaching); attaching to system binaries protected by SIP; missing developer mode / codesign entitlements on the delve binary.","solutions":["Confirm the pid is correct and the process is still alive (ps -p <pid>).","Ensure you own the target process; run dlv with sudo only if necessary.","Enable developer mode (`sudo DevToolsSecurity -enable`) and make sure dlv is properly signed.","Check the target isn't already debugged by another debugger (lldb, another dlv).","Avoid attaching to SIP-protected system processes; re-launch a non-protected copy instead."],"exampleFix":"// before\ndlv attach 9999   // pid guessed, already dead\n// after\nps aux | grep myapp\ndlv attach $(pgrep -u $USER myapp)","handlingStrategy":"validation","validationCode":"p, err := os.FindProcess(pid)\nif err != nil {\n    return err\n}\nif err := p.Signal(syscall.Signal(0)); err != nil {\n    return fmt.Errorf(\"pid %d not attachable/alive: %w\", pid, err)\n}\n// also ensure process owner matches current user (ps -o user -p pid)","typeGuard":null,"tryCatchPattern":"tgt, err := proc.Attach(pid, [], logger)\nif err != nil && strings.Contains(err.Error(), \"could not attach to\") {\n    // check pid, ownership, SIP, and existing debugger before retrying\n}","preventionTips":["Resolve the pid immediately before attaching to avoid exit races.","Only attach to processes you own; escalate deliberately if needed.","Enable developer mode and keep delve properly codesigned on macOS.","Never attach to SIP-protected system binaries."],"tags":["darwin","attach","mach","permissions"],"backgroundTag":"ptrace-attach-denied","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}