GDB – How to Fix GDB Not Attaching to Process After Upgrade

11.04debugdebuggingsudo

I just recently upgraded from 10.04 to 11.04 and gdb won't allow me to attach to processes anymore I get the error

Attaching to process 10144 Could not
attach to process. If your uid
matches the uid of the target process,
check the setting of
/proc/sys/kernel/yama/ptrace_scope, or
try again as the root user. For more
details, see
/etc/sysctl.d/10-ptrace.conf ptrace:
Operation not permitted.

How do I fix this so that I can debug again without sudo?

Best Answer

In Maverick Meerkat (10.10) Ubuntu introduced a patch to disallow ptracing of non-child processes by non-root users - ie. only a process which is a parent of another process can ptrace it for normal users - whilst root can still ptrace every process. Hence why you can use gdb to attach via sudo still.

You can temporarily disable this restriction (and revert to the old behaviour allowing your user to ptrace (gdb) any of their other processes) by doing:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

To permanently allow it edit /etc/sysctl.d/10-ptrace.conf and change the line:

kernel.yama.ptrace_scope = 1

To read:

kernel.yama.ptrace_scope = 0

For some background on why this change was made, see the Ubuntu wiki.