{"record":{"id":"7107e11aa5d674ca","repo":"go-delve/delve","slug":"could-not-read-proc-stat-v","errorCode":null,"errorMessage":"could not read proc stat: %v","messagePattern":"could not read proc stat: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/native/proc_linux.go","lineNumber":279,"sourceCode":"\t\tlog.Debugf(\"waitfor: new process %q\", string(buf))\n\t\tif strings.HasPrefix(string(buf), pfx) {\n\t\t\treturn pid, nil\n\t\t}\n\t}\n\treturn 0, nil\n}\n\nfunc initialize(dbp *nativeProcess) (string, error) {\n\tcomm, err := os.ReadFile(fmt.Sprintf(\"/proc/%d/comm\", dbp.pid))\n\tif err == nil {\n\t\t// removes newline character\n\t\tcomm = bytes.TrimSuffix(comm, []byte(\"\\n\"))\n\t}\n\n\tif len(comm) <= 0 {\n\t\tstat, err := os.ReadFile(fmt.Sprintf(\"/proc/%d/stat\", dbp.pid))\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"could not read proc stat: %v\", err)\n\t\t}\n\t\texpr := fmt.Sprintf(\"%d\\\\s*\\\\((.*)\\\\)\", dbp.pid)\n\t\trexp, err := regexp.Compile(expr)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"regexp compile error: %v\", err)\n\t\t}\n\t\tmatch := rexp.FindSubmatch(stat)\n\t\tif match == nil {\n\t\t\treturn \"\", fmt.Errorf(\"no match found using regexp '%s' in /proc/%d/stat\", expr, dbp.pid)\n\t\t}\n\t\tcomm = match[1]\n\t}\n\tdbp.os.comm = strings.ReplaceAll(string(comm), \"%\", \"%%\")\n\n\treturn getCmdLine(dbp.pid), nil\n}\n\nfunc (dbp *nativeProcess) GetBufferedTracepoints() []ebpf.RawUProbeParams {","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/native/proc_linux.go#L261-L297","documentation":"During initialize on Linux, delve reads the target's command name (comm) from /proc/<pid>/comm; when comm is empty it falls back to parsing /proc/<pid>/stat. If os.ReadFile of that stat file fails, delve returns 'could not read proc stat'. It needs the process name to populate thread/cmd metadata.","triggerScenarios":"initialize (called from Launch/Attach) reads /proc/<pid>/stat and gets an error — the pid no longer exists (process exited during setup), permission denied on /proc, or non-Linux kernel without procfs mounted.","commonSituations":"Attaching to a short-lived process that dies before initialization completes; running inside containers without /proc mounted or with hidepid=2 on /proc mounts; chroot environments lacking procfs.","solutions":["Re-check the target pid: if it exited, attach earlier or use dlv exec on the binary with a held process.","Ensure /proc is mounted and readable for the debugger user (check mount -t proc, hidepid mount options).","Run delve as root or the same user as the target so /proc/<pid>/stat is readable.","Retry attach; the failure is frequently a race with process death."],"exampleFix":"// before\n// attach fails: could not read proc stat: open /proc/1234/stat: no such file or directory\n\n// after\nif _, err := os.Stat(\"/proc/1234\"); err != nil {\n    return fmt.Errorf(\"target %d already exited before attach; start it under dlv or pause it first\", 1234)\n}\n_ = debugger.Attach(1234, nil)","handlingStrategy":"validation","validationCode":"// confirm procfs readable and pid alive before attach on Linux\nfunc pidAttachable(pid int) error {\n    if _, err := os.Stat(fmt.Sprintf(\"/proc/%d/stat\", pid)); err != nil {\n        return fmt.Errorf(\"pid %d gone or /proc unreadable: %v\", pid, err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"err := debugger.Attach(pid, nil)\nif err != nil && strings.Contains(err.Error(), \"could not read proc stat\") {\n    return fmt.Errorf(\"%w (target likely exited during attach; retry earlier in process lifetime)\", err)\n}","preventionTips":["Attach promptly or pause the target to avoid exit races.","Ensure /proc is mounted and not mounted hidepid for your user.","Run delve as root or the target's owner.","Verify the pid before attach (kill -0 <pid>)."],"tags":["linux","procfs","attach","process-lifetime"],"backgroundTag":"proc-stat-unreadable","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}