[binutils-gdb] gdb/amd-dbgapi-target: Move Linux code to amd-dbagapi-posix-hdep.c

Pedro Alves via Gdb-cvs <[email protected]> Fri, 12 Jun 2026 13:37:13 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c4aa08decda4eaa2e3fd9126c6e152131b203dc1

commit c4aa08decda4eaa2e3fd9126c6e152131b203dc1
Author: Lancelot SIX <[email protected]>
Date:   Wed May 8 11:15:22 2024 +0100

    gdb/amd-dbgapi-target: Move Linux code to amd-dbagapi-posix-hdep.c
    
    The current amd-dbgapi-target.c file contains some code that is Linux
    specific.  This patch extracts such code to host dependent files, so
    they can be replaced for other host platforms.
    
    The code in question is related to the event loop.  The way dbgapi
    notifies GDB that there is a pending event is platform specific.  For
    Linux (the only platform supported so far), this is done using a file
    descriptor GDB can use in its main event loop.
    
    This patch introduces an overall host-dependency infrastructure and
    moves the Linux-specific code to amd-dbgapi-posix-hdep.c.  The "posix"
    naming is because even though only Linux is supported, the code is
    really plain POSIX code that would work on other POSIX platforms.
    This also follows the existing posix-hdep.c nomenclature.
    
    Co-Authored-By: Pedro Alves <[email protected]>
    Change-Id: Id3d4b947176e5cfe91b0ab64376fca1a6a8c6fc9

Diff:
---
 gdb/Makefile.in             |  2 ++
 gdb/amd-dbgapi-hdep.h       | 34 ++++++++++++++++++++++++++++++++
 gdb/amd-dbgapi-posix-hdep.c | 48 +++++++++++++++++++++++++++++++++++++++++++++
 gdb/amd-dbgapi-target.c     | 32 +++++++++++++-----------------
 gdb/configure               | 10 ++++++++++
 gdb/configure.ac            | 10 ++++++++++
 6 files changed, 118 insertions(+), 18 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index b7e9acb6e70..f67305a00b0 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1285,6 +1285,7 @@ HFILES_NO_SRCDIR = \
 	amd64-nat.h \
 	amd64-ravenscar-thread.h \
 	amd64-tdep.h \
+	amd-dbgapi-hdep.h \
 	amd-dbgapi-target.h \
 	amdgpu-tdep.h \
 	annotate.h \
@@ -1792,6 +1793,7 @@ ALLDEPFILES = \
 	alpha-netbsd-tdep.c \
 	alpha-obsd-tdep.c \
 	alpha-tdep.c \
+	amd-dbgapi-posix-hdep.c \
 	amd-dbgapi-target.c \
 	amd64-bsd-nat.c \
 	amd64-darwin-tdep.c \
diff --git a/gdb/amd-dbgapi-hdep.h b/gdb/amd-dbgapi-hdep.h
new file mode 100644
index 00000000000..5d6fccb980c
--- /dev/null
+++ b/gdb/amd-dbgapi-hdep.h
@@ -0,0 +1,34 @@
+/* Host dependent utilities used by the amd-dbgapi target.
+
+   Copyright (C) 2024-2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_AMD_DBGAPI_HDEP_H
+#define GDB_AMD_DBGAPI_HDEP_H
+
+#include <amd-dbgapi/amd-dbgapi.h>
+
+/* Null amd_dbgapi_notifier_t.  */
+extern const amd_dbgapi_notifier_t null_amd_dbgapi_notifier;
+
+/* Clear the notifier.  */
+extern void amd_dbgapi_notifier_clear (amd_dbgapi_notifier_t notifier);
+
+/* Get the file descriptor associated with the notifier.  */
+extern int amd_dbgapi_notifier_get_fd (amd_dbgapi_notifier_t notifier);
+
+#endif /* GDB_AMD_DBGAPI_HDEP_H */
diff --git a/gdb/amd-dbgapi-posix-hdep.c b/gdb/amd-dbgapi-posix-hdep.c
new file mode 100644
index 00000000000..b8f6b44987f
--- /dev/null
+++ b/gdb/amd-dbgapi-posix-hdep.c
@@ -0,0 +1,48 @@
+/* Host dependent utilities for the amd-dbgapi target on POSIX.
+
+   Copyright (C) 2019-2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "amd-dbgapi-hdep.h"
+#include <amd-dbgapi/amd-dbgapi.h>
+
+/* See amd-dbgapi-hdep.h.  */
+const amd_dbgapi_notifier_t null_amd_dbgapi_notifier = -1;
+
+/* See amd-dbgapi-hdep.h.  */
+
+void
+amd_dbgapi_notifier_clear (amd_dbgapi_notifier_t notifier)
+{
+  int ret;
+
+  /* Drain the notifier pipe.  */
+  do
+    {
+      char buf;
+      ret = read (notifier, &buf, 1);
+    }
+  while (ret >= 0 || (ret == -1 && errno == EINTR));
+}
+
+/* See amd-dbgapi-hdep.h.  */
+
+int
+amd_dbgapi_notifier_get_fd (amd_dbgapi_notifier_t notifier)
+{
+  return notifier;
+}
diff --git a/gdb/amd-dbgapi-target.c b/gdb/amd-dbgapi-target.c
index d44f03d0b80..0d0ccdc082d 100644
--- a/gdb/amd-dbgapi-target.c
+++ b/gdb/amd-dbgapi-target.c
@@ -19,6 +19,7 @@
 
 
 #include "amd-dbgapi-target.h"
+#include "amd-dbgapi-hdep.h"
 #include "amdgpu-tdep.h"
 #include "async-event.h"
 #include "breakpoint.h"
@@ -203,7 +204,7 @@ struct amd_dbgapi_inferior_info
   amd_dbgapi_process_id_t process_id = AMD_DBGAPI_PROCESS_NONE;
 
   /* The amd_dbgapi_notifier_t for this inferior.  */
-  amd_dbgapi_notifier_t notifier = -1;
+  amd_dbgapi_notifier_t notifier = null_amd_dbgapi_notifier;
 
   /* The status of the inferior's runtime support.  */
   amd_dbgapi_runtime_state_t runtime_state = AMD_DBGAPI_RUNTIME_STATE_UNLOADED;
@@ -1205,15 +1206,8 @@ dbgapi_notifier_handler (int err, gdb_client_data client_data)
 {
   amd_dbgapi_inferior_info &info
     = *static_cast<amd_dbgapi_inferior_info *> (client_data);
-  int ret;
 
-  /* Drain the notifier pipe.  */
-  do
-    {
-      char buf;
-      ret = read (info.notifier, &buf, 1);
-    }
-  while (ret >= 0 || (ret == -1 && errno == EINTR));
+  amd_dbgapi_notifier_clear (info.notifier);
 
   if (info.inf->target_is_pushed (&the_amd_dbgapi_target))
     {
@@ -1315,8 +1309,9 @@ amd_dbgapi_target::async (bool enable)
 	{
 	  amd_dbgapi_inferior_info &info = get_amd_dbgapi_inferior_info (inf);
 
-	  if (info.notifier != -1)
-	    add_file_handler (info.notifier, dbgapi_notifier_handler, &info,
+	  if (info.notifier != null_amd_dbgapi_notifier)
+	    add_file_handler (amd_dbgapi_notifier_get_fd (info.notifier),
+			      dbgapi_notifier_handler, &info,
 			      string_printf ("amd-dbgapi notifier for pid %d",
 					     inf->pid));
 	}
@@ -1339,8 +1334,8 @@ amd_dbgapi_target::async (bool enable)
 	  const amd_dbgapi_inferior_info &info
 	    = get_amd_dbgapi_inferior_info (inf);
 
-	  if (info.notifier != -1)
-	    delete_file_handler (info.notifier);
+	  if (info.notifier != null_amd_dbgapi_notifier)
+	    delete_file_handler (amd_dbgapi_notifier_get_fd (info.notifier));
 	}
 
       delete_async_event_handler (&amd_dbgapi_async_event_handler);
@@ -1879,7 +1874,8 @@ attach_amd_dbgapi (inferior *inf)
     }
 
   amd_dbgapi_debug_printf ("process_id = %" PRIu64 ", notifier fd = %d",
-			   info.process_id.handle, info.notifier);
+			   info.process_id.handle,
+			   amd_dbgapi_notifier_get_fd (info.notifier));
 
   set_process_memory_precision (info);
 
@@ -1888,8 +1884,8 @@ attach_amd_dbgapi (inferior *inf)
      target.  */
   dbgapi_notifier_handler (0, &info);
 
-  add_file_handler (info.notifier, dbgapi_notifier_handler, &info,
-		    "amd-dbgapi notifier");
+  add_file_handler (amd_dbgapi_notifier_get_fd (info.notifier),
+		    dbgapi_notifier_handler, &info, "amd-dbgapi notifier");
 }
 
 static void maybe_reset_amd_dbgapi ();
@@ -1917,8 +1913,8 @@ detach_amd_dbgapi (inferior *inf)
     warning (_("amd-dbgapi: could not detach from process %d (%s)"),
 	     inf->pid, get_status_string (status));
 
-  gdb_assert (info.notifier != -1);
-  delete_file_handler (info.notifier);
+  gdb_assert (info.notifier != null_amd_dbgapi_notifier);
+  delete_file_handler (amd_dbgapi_notifier_get_fd (info.notifier));
 
   /* This is a noop if the target is not pushed.  */
   inf->unpush_target (&the_amd_dbgapi_target);
diff --git a/gdb/configure b/gdb/configure
index 80931d10bef..a03b7ad39ae 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -25444,6 +25444,16 @@ $as_echo "#define HAVE_AMD_DBGAPI 1" >>confdefs.h
     if test "$all_targets" = true; then
       TARGET_OBS="$TARGET_OBS \$(ALL_AMD_DBGAPI_TARGET_OBS)"
     fi
+
+    # Add the host-specific objects.
+    case ${gdb_host} in
+      *linux*)
+	gdb_host_obs="${gdb_host_obs} amd-dbgapi-posix-hdep.o"
+	;;
+      *)
+	as_fn_error $? "amd-dbgapi not supported for host ${gdb_host}" "$LINENO" 5
+	;;
+    esac
   elif test "$gdb_require_amd_dbgapi" = true -o "$with_amd_dbgapi" = yes; then
     # amd-dbgapi was not found and...
     #
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 2f05daf614f..03ed2b386bb 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -341,6 +341,16 @@ if test "$gdb_require_amd_dbgapi" = true \
     if test "$all_targets" = true; then
       TARGET_OBS="$TARGET_OBS \$(ALL_AMD_DBGAPI_TARGET_OBS)"
     fi
+
+    # Add the host-specific objects.
+    case ${gdb_host} in
+      *linux*)
+	gdb_host_obs="${gdb_host_obs} amd-dbgapi-posix-hdep.o"
+	;;
+      *)
+	AC_MSG_ERROR([amd-dbgapi not supported for host ${gdb_host}])
+	;;
+    esac
   elif test "$gdb_require_amd_dbgapi" = true -o "$with_amd_dbgapi" = yes; then
     # amd-dbgapi was not found and...
     #