How to solve "ptrace operation not permitted" when trying to attach GDB to a process?

I'm trying to attach to a program with GDB but it returns: Attaching to process 29139 Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/