public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] [gdb/tdep] Use PTRACE_GET_SYSCALL_INFO for syscall number
Date: Mon, 20 Nov 2023 11:34:09 +0100	[thread overview]
Message-ID: <20231120103409.27602-1-tdevries@suse.de> (raw)

When stopped at syscall-enter-stop or syscall-exit-stop, we currently use the
architecture-specific hook gdbarch_get_syscall_number to get the number of the
current syscall.

This approach requires supporting architectures to implement the hook, which
possibly relies on architecture specific co-operation from the kernel.

Starting linux kernel version 5.3, ptrace supports a PTRACE_GET_SYSCALL_INFO
request.

Use the ptrace request, if available, to get the syscall number.

It's unfortunate that the request only returns the syscall number for
PTRACE_SYSCALL_INFO_ENTRY, and not for PTRACE_SYSCALL_INFO_EXIT, so we still
need the hook for syscall-exit-stop.

The kernel commit 201766a20e30 ("ptrace: add PTRACE_GET_SYSCALL_INFO
request") names a few properties of the new request:
- syscall number identification is more reliable in some corner cases,
- tracers don't need to support any architecture specific code, and
- syscall-enter-stop / syscall-exit-stop detection is no longer necessary.

This first is a good reason for this patch.

The second less so, because as mentioned we still need to support the hook for
syscall-exit-stop.

The third could be used as well in gdb, but that's out-of-scope for this
patch, which focuses on getting the syscall number.

Tested on x86_64-linux, and verified with a simple test-case that I see the
ptrace request in the strace.
---
 gdb/linux-nat.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index d3e9560c2fc..ad36c964852 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -1750,6 +1750,30 @@ kill_lwp (int lwpid, int signo)
   return ret;
 }
 
+/* Return current syscall.  */
+
+static LONGEST
+linux_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
+{
+#ifdef PTRACE_GET_SYSCALL_INFO
+  {
+    __ptrace_syscall_info info;
+    pid_t pid = (thread->ptid.tid_p ()
+		 ? thread->ptid.tid ()
+		 : thread->ptid.pid ());
+
+    long res = ptrace (PTRACE_GET_SYSCALL_INFO, pid, sizeof (info), &info);
+
+    /* It's unfortunate that for PTRACE_SYSCALL_INFO_EXIT there's no syscall
+       number.  */
+    if (res != -1 && info.op == PTRACE_SYSCALL_INFO_ENTRY)
+      return info.entry.nr;
+  }
+#endif
+
+  return gdbarch_get_syscall_number (gdbarch, thread);
+}
+
 /* Handle a GNU/Linux syscall trap wait response.  If we see a syscall
    event, check if the core is interested in it: if not, ignore the
    event, and keep waiting; otherwise, we need to toggle the LWP's
@@ -1762,7 +1786,7 @@ linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
   struct target_waitstatus *ourstatus = &lp->waitstatus;
   struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
   thread_info *thread = linux_target->find_thread (lp->ptid);
-  int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, thread);
+  int syscall_number = (int) linux_get_syscall_number (gdbarch, thread);
 
   if (stopping)
     {

base-commit: 11788869e0a3713e847733be8712e4b3b5e4dfd9
-- 
2.35.3


             reply	other threads:[~2023-11-20 10:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 10:34 Tom de Vries [this message]
2023-11-21  0:15 ` John Baldwin
2023-11-21  9:03   ` Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231120103409.27602-1-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).