gdb attach fails with ptrace: Operation not permitted
Today I ran into a weird problem. I could not attach to my own process with gdb. The process ran under my UID, but gdb refused to attach. This is a problem of wrong permissions, although /proc/[pid]/status looked ok:
... Uid: 1000 1000 1000 1000 Gid: 1000 1000 1000 1000 ...
I am the owner but cannot attach? Well, I launched gdb as root and could attach. Strange. Without digging deeper into this, my dirty workaround was this:
sudo chmod +s /usr/bin/gdb
Update: Thanks to Mario, who pointed out, that the reason is the Kernel hardening stuff build into the Ubuntu kernel. See his comment how to fix the problem permanently.
KategorienLinux, Nerd attach, c, debugging, development, gdb, Linux, operation not permitted, ptrace

I like your style
. Nice hack!
I found the issue:
https://wiki.ubuntu.com/SecurityTeam/Roadmap/KernelHardening#ptrace%20Protection
And the solution is just either do this (as root):
$ echo 0 > /proc/sys/kernel/yama/ptrace_scope
…or make it permanent by setting ptrace_scope to 0 in /etc/sysctl.d/10-ptrace.conf
Hope you find it useful!
The solution above only works for Ubuntu. I am experiencing the same issue but in Debian. Holler if you have a solution!
Thanks!!!