{"record":{"id":"6c9eb623e98e7567","repo":"go-delve/delve","slug":"could-not-attach-to-pid-d-this-could-be-caused-b","errorCode":null,"errorMessage":"Could not attach to pid %d: this could be caused by a kernel security setting, try writing \"0\" to /proc/sys/kernel/yama/ptrace_scope","messagePattern":"Could not attach to pid (.+?): this could be caused by a kernel security setting, try writing \"0\" to /proc/sys/kernel/yama/ptrace_scope","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"service/debugger/debugger_linux.go","lineNumber":27,"sourceCode":"\t\"syscall\"\n)\n\nfunc init() {\n\tattachErrorMessage = attachErrorMessageLinux\n}\n\n//lint:file-ignore ST1005 errors here can be capitalized\n\nfunc attachErrorMessageLinux(pid int, err error) error {\n\tfallbackerr := fmt.Errorf(\"could not attach to pid %d: %s\", pid, err)\n\tif serr, ok := err.(syscall.Errno); ok {\n\t\tswitch serr {\n\t\tcase syscall.EPERM:\n\t\t\t// check if ptrace of non-child processes is disabled\n\t\t\tbs, err := os.ReadFile(\"/proc/sys/kernel/yama/ptrace_scope\")\n\t\t\tif err == nil && len(bs) >= 1 && bs[0] != '0' {\n\t\t\t\t// Yama documentation: https://www.kernel.org/doc/Documentation/security/Yama.txt\n\t\t\t\treturn fmt.Errorf(\"Could not attach to pid %d: this could be caused by a kernel security setting, try writing \\\"0\\\" to /proc/sys/kernel/yama/ptrace_scope\", pid)\n\t\t\t}\n\n\t\t\t// check if the pid belongs to a different process\n\t\t\tfi, err := os.Stat(fmt.Sprintf(\"/proc/%d\", pid))\n\t\t\tif err != nil {\n\t\t\t\treturn fallbackerr\n\t\t\t}\n\t\t\tif fi.Sys().(*syscall.Stat_t).Uid != uint32(os.Getuid()) {\n\t\t\t\treturn fmt.Errorf(\"Could not attach to pid %d: current user does not own the process\", pid)\n\t\t\t}\n\n\t\t\t// check if the process is already being traced\n\t\t\tstatusfh, err := os.Open(fmt.Sprintf(\"/proc/%d/status\", pid))\n\t\t\tif err != nil {\n\t\t\t\treturn fallbackerr\n\t\t\t}\n\t\t\tdefer statusfh.Close()\n\t\t\tscan := bufio.NewScanner(statusfh)","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/service/debugger/debugger_linux.go#L9-L45","documentation":"Specific EPERM diagnostic: the kernel Yama security module restricts ptrace to descendants, so attaching to an unrelated process fails with EPERM. The debugger reads /proc/sys/kernel/yama/ptrace_scope and, if it is non-zero, reports this as the likely cause.","triggerScenarios":"'dlv attach <pid>' or debugger.Attach on a process that is not a child of the debugger while /proc/sys/kernel/yama/ptrace_scope contains a value != '0'.","commonSituations":"Default Ubuntu/Debian kernels (ptrace_scope=1); CI containers with hardened settings; attaching to an already-running service started by another session.","solutions":["Write 0 to /proc/sys/kernel/yama/ptrace_scope: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope","Launch the target as a child of delve (dlv debug / dlv exec) instead of attaching","Attach as root (sudo dlv attach) which bypasses Yama restrictions","Make the setting permanent via sysctl: kernel.yama.ptrace_scope=0"],"exampleFix":"// before\ndlv attach 12345 // fails with EPERM\n// after (shell)\nsudo sysctl -w kernel.yama.ptrace_scope=0\ndlv attach 12345","handlingStrategy":"validation","validationCode":"bs, _ := os.ReadFile(\"/proc/sys/kernel/yama/ptrace_scope\")\nyamaBlocked := len(bs) > 0 && bs[0] != '0'","typeGuard":null,"tryCatchPattern":"err := debugger.Attach(pid, []string{}, nil)\nif err != nil && strings.Contains(err.Error(), \"yama/ptrace_scope\") {\n    // surface instruction to run: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope\n}","preventionTips":["Set kernel.yama.ptrace_scope=0 in dev environments via sysctl","Prefer dlv exec/debug (child process) over attach when Yama is on","Document required privileges for attach workflows","Use sudo dlv attach for cross-process debugging"],"tags":["linux","yama","ptrace-scope","permissions"],"backgroundTag":"yama-ptrace-scope-blocked","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}