]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
gdb/linux: consider ptrace_scope when building attach error message
authorAndrew Burgess <aburgess@redhat.com>
Wed, 10 Sep 2025 13:54:21 +0000 (14:54 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Wed, 24 Sep 2025 12:17:03 +0000 (13:17 +0100)
commit5b582f86bd7a6ece68f4a979c40faeffde9eec5e
tree47f808bf3c89438398dba5cc8779c538e823868c
parent52bb1ca383ee2765332aee3023650ccba895a8ac
gdb/linux: consider ptrace_scope when building attach error message

On GNU/Linux, if an attach fails, then we end up calling
linux_ptrace_attach_fail_reason, which returns a string describing why
the attach might have failed.

This commit extends linux_ptrace_attach_fail_reason to check the
current /proc/sys/kernel/yama/ptrace_scope setting.  When this is set
to a value greater than 0 attaching can be restricted in some cases.

If I first set ptrace_scope to 1, like this:

  $ echo 1 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Then use current master GDB to attach to a process, I see this:

  (gdb) attach 2476518
  Attaching to process 2476518
  ptrace: Operation not permitted.
  (gdb)

With this patch applied the output becomes:

  (gdb) attach 2476518
  Attaching to process 2476518
  warning: the /proc/sys/kernel/yama/ptrace_scope setting of 1 might prevent attaching, see 'man 2 ptrace'
  ptrace: Operation not permitted.
  (gdb)

In my (limited) experience, not every user is familiar with the
ptrace_scope setting.  Finding that attach just doesn't work often
leads users to believe that the only way to have GDB attach to another
process is to run GDB as root.

Though that is a solution to the problem, my hope is that, by giving
the users more information, they can make a more informed choice about
which solution is right for them (e.g. run as root, change the
setting, start the inferior directly under GDB, etc).

This change also works for gdbserver.  For example:

  (gdb) target extended-remote :54321
  Remote debugging using :54321
  (gdb) attach 2475389
  Attaching to process 2475389
  Attaching to process 2475389 failed: Cannot attach to process 2475389: \
     Operation not permitted (1), the /proc/sys/kernel/yama/ptrace_scope \
     setting of 1 might prevent attaching, see 'man 2 ptrace'
  (gdb)

There's no test for this as testing would require adjusting a system
wide setting.

I've linked to a semi-related bug.  It is discussing the same issue at
least.  This patch doesn't try to "fix" anything that the bug is
talking about, it just makes it (I think) slightly clearer when the
issue does arise.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31520

Approved-By: Pedro Alves <pedro@palves.net>
Reviewed-By: Guinevere Larsen <guinevere@redhat.com>
gdb/nat/linux-ptrace.c