Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Bratislav Filipovic <bfilipov@amd.com>
To: <lancelot.six@amd.com>
Cc: <bfilipov@amd.com>, <gdb-patches@sourceware.org>
Subject: [PATCH v5] gdb: Implement stop-on-solib-events for GPU code objects
Date: Thu, 19 Nov 2026 15:09:57 +0100	[thread overview]
Message-ID: <20261119142057.58123-1-bfilipov@amd.com> (raw)
In-Reply-To: <av3pJqTKuJFJ9SqP@khazad-dum>

GDB's "set stop-on-solib-events" setting allows users to stop execution
when shared libraries are loaded or unloaded, enabling inspection and
breakpoint placement before library code executes.  This works for CPU
shared libraries, but has no effect on GPU code objects loaded by the
AMD ROCm runtime, even though those are reported as shared libraries
(e.g. in "info sharedlibrary").

The reason is that amd_dbgapi_target_breakpoint::check_status
unconditionally sets bs->stop to 0.  This is in contrast to
internal_breakpoint::check_status for CPU shared libraries, which
honors stop_on_solib_events.

Fix this by having process_event_queue report whether a
CODE_OBJECT_LIST_UPDATED event was processed while draining the event
queue.  check_status then sets bs->stop and bs->print the same way
internal_breakpoint::check_status does for bp_shlib_event breakpoints.
It no longer sets bs->print_it to print_it_noop: bpstat_stop_status
already does that when the breakpoint does not stop.  A print_it
override reports the stop through print_solib_event, so the stop is
presented like a CPU shared library event, both in the CLI ("Stopped
due to shared library event") and in MI (*stopped,reason="solib-event").

"catch load" and "catch unload" catchpoints are not affected by this
change and still do not trigger for GPU code objects.

Add gdb.rocm/solib-event.exp, which tests both file:// code objects
(hipModuleLoad) and memory:// code objects (hipModuleLoadData), their
unload events, that nothing stops when the setting is off, and the MI
stop record.
---

Changes in v5 (addressing Lancelot's review of v4):
- Drop the CPU/GPU-specific messages: breakpoint.{c,h} and
  break-catch-load.c are no longer touched.  GPU code object events
  now print "Stopped due to shared library event", like CPU ones.
- Remove the leftover object-kind paragraph from the MI documentation.
- Fix the print_it comment; process_one_event is unchanged and
  process_event_queue sets code_object_list_updated itself.
- check_status now mirrors internal_breakpoint::check_status.
- Test: empty kernel body (no inline asm), unambiguous end-of-test
  marker, remove the test_file_load/test_memory_load breakpoints,
  use the default MI version, simplify the MI solib-event check.
- Rebased on current master; the int -> bool change for
  stop_on_solib_events is now upstream and is no longer part of this
  patch.

Tested on gfx90a: gdb.rocm/solib-event.exp, gdb.base/catch-load.exp,
gdb.mi/mi-catch-load.exp.

 gdb/NEWS                                      |   5 +
 gdb/amd-dbgapi-target.c                       |  47 +++++-
 gdb/doc/gdb.texinfo                           |   9 +-
 gdb/infrun.c                                  |   5 +-
 gdb/testsuite/gdb.rocm/solib-event-kernel.cpp |  26 +++
 gdb/testsuite/gdb.rocm/solib-event.cpp        | 105 ++++++++++++
 gdb/testsuite/gdb.rocm/solib-event.exp        | 155 ++++++++++++++++++
 7 files changed, 340 insertions(+), 12 deletions(-)
 create mode 100644 gdb/testsuite/gdb.rocm/solib-event-kernel.cpp
 create mode 100644 gdb/testsuite/gdb.rocm/solib-event.cpp
 create mode 100644 gdb/testsuite/gdb.rocm/solib-event.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index db3fa820df1..e9335955b1a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -7,6 +7,11 @@
   Support displaying and modifying Intel APX registers (Extended GPRs
   $r16 - $r31) including byte, word and dword pseudo registers.
 
+* The 'set stop-on-solib-events' setting now applies to GPU code objects
+  loaded by the AMD ROCm runtime, in addition to CPU shared libraries.
+  When enabled, GDB will stop execution whenever GPU code objects are
+  loaded or unloaded, allowing inspection and breakpoint placement.
+
 * GDB now distinguishes between the GNU (MinGW) and MSVC Windows ABIs.
 
   The "set osabi" command accepts two new values, "Windows-GNU" and
diff --git a/gdb/amd-dbgapi-target.c b/gdb/amd-dbgapi-target.c
index a38a8bafb90..bbb6b633a44 100644
--- a/gdb/amd-dbgapi-target.c
+++ b/gdb/amd-dbgapi-target.c
@@ -30,6 +30,7 @@
 #include "gdbsupport/unordered_map.h"
 #include "inf-loop.h"
 #include "inferior.h"
+#include "infrun.h"
 #include "objfiles.h"
 #include "observable.h"
 #include "registry.h"
@@ -275,7 +276,17 @@ struct amd_dbgapi_inferior_info
     wave_info_map;
 };
 
-static amd_dbgapi_event_id_t process_event_queue
+/* Return value for process_event_queue.  */
+struct process_event_queue_result
+{
+  /* The event ID, or AMD_DBGAPI_EVENT_NONE if no event was found.  */
+  amd_dbgapi_event_id_t event_id;
+
+  /* True if a CODE_OBJECT_LIST_UPDATED event was seen during processing.  */
+  bool code_object_list_updated;
+};
+
+static process_event_queue_result process_event_queue
   (amd_dbgapi_inferior_info &info,
    amd_dbgapi_event_kind_t until_event_kind = AMD_DBGAPI_EVENT_KIND_NONE);
 
@@ -587,6 +598,7 @@ struct amd_dbgapi_target_breakpoint : public code_breakpoint
 
   void re_set (program_space *) override;
   void check_status (struct bpstat *bs) override;
+  enum print_stop_action print_it (const bpstat *bs) const override;
 };
 
 void
@@ -603,7 +615,6 @@ amd_dbgapi_target_breakpoint::check_status (struct bpstat *bs)
   amd_dbgapi_status_t status;
 
   bs->stop = 0;
-  bs->print_it = print_it_noop;
 
   /* Find the address the breakpoint is set at.  */
   auto match_breakpoint
@@ -639,7 +650,7 @@ amd_dbgapi_target_breakpoint::check_status (struct bpstat *bs)
 
   /* If the action is AMD_DBGAPI_BREAKPOINT_ACTION_HALT, we need to wait until
      a breakpoint resume event for this breakpoint_id is seen.  */
-  amd_dbgapi_event_id_t resume_event_id
+  auto [resume_event_id, code_object_list_updated]
     = process_event_queue (info, AMD_DBGAPI_EVENT_KIND_BREAKPOINT_RESUME);
 
   /* We should always get a breakpoint_resume event after processing all
@@ -665,6 +676,23 @@ amd_dbgapi_target_breakpoint::check_status (struct bpstat *bs)
 	   pulongest (resume_breakpoint_id.handle));
 
   amd_dbgapi_event_processed (resume_event_id);
+
+  /* If the code object list was updated, stop if the user requested to
+     stop on shared library events, as internal_breakpoint::check_status
+     does for bp_shlib_event breakpoints.  */
+  bs->stop = code_object_list_updated && stop_on_solib_events;
+  bs->print = bs->stop;
+}
+
+enum print_stop_action
+amd_dbgapi_target_breakpoint::print_it (const bpstat *bs) const
+{
+  /* This is only called if check_status decided to stop, which happens
+     when the code object list was updated while stop_on_solib_events is
+     enabled.  Report it the same way as a shared library event.  */
+  print_solib_event (false);
+
+  return PRINT_NOTHING;
 }
 
 bool
@@ -1645,9 +1673,11 @@ event_kind_str (amd_dbgapi_event_kind_t kind)
 /* Drain the dbgapi event queue of a given inferior.  Stop processing the
    events if an event of a given kind is requested (not AMD_DBGAPI_EVENT_NONE).
    Wave stop events that are not returned are queued into their inferior's
-   amd_dbgapi_inferior_info pending wave events. */
+   amd_dbgapi_inferior_info pending wave events.  The code_object_list_updated
+   field of the result is true if a CODE_OBJECT_LIST_UPDATED event was
+   processed.  */
 
-static amd_dbgapi_event_id_t
+static process_event_queue_result
 process_event_queue (amd_dbgapi_inferior_info &info,
 		     amd_dbgapi_event_kind_t until_event_kind)
 {
@@ -1655,6 +1685,8 @@ process_event_queue (amd_dbgapi_inferior_info &info,
      performance, make sure it is not required.  */
   gdb_assert (!info.forward_progress_required);
 
+  bool code_object_list_updated = false;
+
   while (true)
     {
       amd_dbgapi_event_id_t event_id;
@@ -1675,7 +1707,10 @@ process_event_queue (amd_dbgapi_inferior_info &info,
 				 event_kind_str (event_kind));
 
       if (event_id == AMD_DBGAPI_EVENT_NONE || event_kind == until_event_kind)
-	return event_id;
+	return {event_id, code_object_list_updated};
+
+      if (event_kind == AMD_DBGAPI_EVENT_KIND_CODE_OBJECT_LIST_UPDATED)
+	code_object_list_updated = true;
 
       process_one_event (info, event_id, event_kind);
     }
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 7998dfb58c8..64a91e6a5c1 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22536,14 +22536,15 @@ The surrounding square brackets are optional.
 @item set stop-on-solib-events @r{[}on@r{|}off@r{]}
 @kindex set stop-on-solib-events
 This command controls whether @value{GDBN} should give you control
-when the dynamic linker notifies it about some shared library event.
-The most common event of interest is loading or unloading of a new
-shared library.
+when the dynamic linker notifies it about some shared library event,
+or when GPU code objects are loaded or unloaded.
+The most common events of interest are loading or unloading of a new
+shared library or GPU code object.
 
 @item show stop-on-solib-events
 @kindex show stop-on-solib-events
 Show whether @value{GDBN} stops and gives you control when shared
-library events happen.
+library events or GPU code object events happen.
 @end table
 
 Shared libraries are also supported in many cross or remote debugging
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 08fac30e9e8..432c2eb27ae 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -10851,8 +10851,9 @@ leave it stopped or free to run as needed."),
 Set stopping for shared library events."), _("\
 Show stopping for shared library events."), _("\
 If on, gdb will give control to the user when the dynamic linker\n\
-notifies gdb of shared library events.  The most common event of interest\n\
-to the user would be loading/unloading of a new library."),
+notifies gdb of shared library events, or when GPU code objects are loaded\n\
+or unloaded.  The most common events of interest to the user would be\n\
+loading/unloading of a new library or GPU code object."),
 			   set_stop_on_solib_events,
 			   show_stop_on_solib_events,
 			   &setlist, &showlist);
diff --git a/gdb/testsuite/gdb.rocm/solib-event-kernel.cpp b/gdb/testsuite/gdb.rocm/solib-event-kernel.cpp
new file mode 100644
index 00000000000..3e1c895f95e
--- /dev/null
+++ b/gdb/testsuite/gdb.rocm/solib-event-kernel.cpp
@@ -0,0 +1,26 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   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/>.  */
+
+/* Device kernel for solib-event test.
+   Compiled with --cuda-device-only to produce .co file.  */
+
+#include <hip/hip_runtime.h>
+
+extern "C" __global__ void
+test_kernel ()
+{
+}
diff --git a/gdb/testsuite/gdb.rocm/solib-event.cpp b/gdb/testsuite/gdb.rocm/solib-event.cpp
new file mode 100644
index 00000000000..161c7c15b17
--- /dev/null
+++ b/gdb/testsuite/gdb.rocm/solib-event.cpp
@@ -0,0 +1,105 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   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 "rocm-test-utils.h"
+#include <hip/hip_runtime.h>
+#include <fstream>
+#include <vector>
+
+/* Test file:// events (hipModuleLoad).  */
+static void
+test_file_load (const char *module_path)
+{
+  hipModule_t module;
+  CHECK (hipModuleLoad (&module, module_path));
+
+  hipFunction_t function;
+  CHECK (hipModuleGetFunction (&function, module, "test_kernel"));
+
+  CHECK (hipModuleLaunchKernel (function, 1, 1, 1, 1, 1, 1,
+				0, nullptr, nullptr, nullptr));
+
+  CHECK (hipDeviceSynchronize ());
+  CHECK (hipModuleUnload (module));
+}
+
+/* Test memory:// events (hipModuleLoadData).  */
+static void
+test_memory_load (const char *module_path)
+{
+  /* Read module file into memory buffer.  */
+  std::ifstream mod (module_path, std::ios::binary | std::ios::ate);
+  if (!mod.is_open ())
+    {
+      fprintf (stderr, "Failed to open module file\n");
+      exit (EXIT_FAILURE);
+    }
+
+  size_t module_size = mod.tellg ();
+  mod.seekg (0, std::ios::beg);
+  std::vector<unsigned char> module_buffer (module_size);
+
+  if (!mod.read (reinterpret_cast<char *> (module_buffer.data ()),
+		 module_size))
+    {
+      fprintf (stderr, "Failed to read module into memory\n");
+      exit (EXIT_FAILURE);
+    }
+  mod.close ();
+
+  /* Load from memory buffer.  */
+  hipModule_t module;
+  CHECK (hipModuleLoadData (&module, module_buffer.data ()));
+
+  hipFunction_t function;
+  CHECK (hipModuleGetFunction (&function, module, "test_kernel"));
+
+  CHECK (hipModuleLaunchKernel (function, 1, 1, 1, 1, 1, 1,
+				0, nullptr, nullptr, nullptr));
+
+  CHECK (hipDeviceSynchronize ());
+  CHECK (hipModuleUnload (module));
+}
+
+__global__ void
+warm_up ()
+{
+}
+
+int
+main (int argc, char **argv)
+{
+  if (argc != 2)
+    {
+      fprintf (stderr, "Usage: %s <module_path>\n", argv[0]);
+      return EXIT_FAILURE;
+    }
+
+  const char *module_path = argv[1];
+
+  /* Submit an empty kernel to force the runtime to do any necessary
+     setup which might include loading internal code objects.  */
+  warm_up<<<1, 1>>>();
+  CHECK (hipDeviceSynchronize ());
+
+  /* Enable SOLIB events here.  */
+
+  test_file_load (module_path);
+  test_memory_load (module_path);
+
+  return 0; /* Break here at end of test.  */
+}
diff --git a/gdb/testsuite/gdb.rocm/solib-event.exp b/gdb/testsuite/gdb.rocm/solib-event.exp
new file mode 100644
index 00000000000..0fde181c5cb
--- /dev/null
+++ b/gdb/testsuite/gdb.rocm/solib-event.exp
@@ -0,0 +1,155 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# 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/>.
+
+# Test that "set stop-on-solib-events on" works for GPU code objects.
+# Tests both file:// events (hipModuleLoad) and memory:// events
+# (hipModuleLoadData), as well as unload events (hipModuleUnload).
+# Also tests that the default setting (off) does not stop execution,
+# and that the MI stop record reports a solib-event.
+
+load_lib rocm.exp
+load_lib mi-support.exp
+
+require allow_hip_tests
+
+standard_testfile .cpp
+
+# Build GPU code object.
+set kernel_srcfile ${testfile}-kernel.cpp
+set hipmodule_path [standard_output_file ${testfile}.co]
+
+if {[gdb_compile $srcdir/$subdir/$kernel_srcfile \
+     $hipmodule_path object \
+     {debug hip additional_flags=--cuda-device-only}] != ""} {
+    return
+}
+
+# Build host executable.
+if {[build_executable "failed to prepare" $testfile $srcfile \
+	 {debug hip}] == -1} {
+    return
+}
+
+proc test_disabled {} {
+    with_rocm_gpu_lock {
+	clean_restart $::testfile
+
+	if {![runto_main -inferior-args $::hipmodule_path]} {
+	    return
+	}
+
+	# Set breakpoint at program exit and verify execution continues
+	# through all code object load/unload events without stopping.
+	gdb_breakpoint [gdb_get_line_number "Break here at end of test"]
+	gdb_test "continue" \
+	    "Breakpoint .* main .*Break here at end of test.*" \
+	    "no stops for code object events when disabled"
+    }
+}
+
+proc test_enabled {} {
+    with_rocm_gpu_lock {
+	clean_restart $::testfile
+
+	if {![runto_main -inferior-args $::hipmodule_path]} {
+	    return
+	}
+
+	gdb_breakpoint [gdb_get_line_number "Enable SOLIB events here"] \
+	    -temporary
+	gdb_continue_to_breakpoint "at enable solib-event"
+
+	gdb_test_no_output "set stop-on-solib-events on"
+
+	with_test_prefix "file://" {
+	    # Test 1: file:// load event (hipModuleLoad).
+	    gdb_test "continue" \
+		"Stopped due to shared library event.*Inferior loaded file://\[^\r\n\]+" \
+		"load"
+
+	    # Test 2: file:// unload event (hipModuleUnload).
+	    gdb_test "continue" \
+		"Stopped due to shared library event.*Inferior unloaded file://\[^\r\n\]+" \
+		"unload"
+	}
+
+	with_test_prefix "memory://" {
+	    # Test 3: memory:// load event (hipModuleLoadData).
+	    gdb_test "continue" \
+		"Stopped due to shared library event.*Inferior loaded memory://\[^\r\n\]+" \
+		"load"
+
+	    # Test 4: memory:// unload event (hipModuleUnload).
+	    gdb_test "continue" \
+		"Stopped due to shared library event.*Inferior unloaded memory://\[^\r\n\]+" \
+		"unload"
+	}
+    }
+}
+
+proc test_mi_output {} {
+    with_rocm_gpu_lock {
+	if {[mi_clean_restart $::testfile]} {
+	    return
+	}
+
+	# Set inferior arguments before running to main.
+	mi_gdb_test "-gdb-set args $::hipmodule_path" "\\^done" \
+	    "set inferior arguments"
+
+	if {![mi_runto_main]} {
+	    return
+	}
+
+	mi_create_breakpoint [gdb_get_line_number "Enable SOLIB events here"] \
+	    "insert breakpoint at enable marker"
+
+	mi_execute_to "exec-continue" "breakpoint-hit" "main" ".*" \
+	    ".*$::srcfile" [gdb_get_line_number "test_file_load (module_path);"] \
+	    {"" "disp=\"keep\""} "stop at enable marker"
+
+	mi_gdb_test "111-gdb-set stop-on-solib-events on" "111\\^done" \
+	    "set stop-on-solib-events via MI"
+
+	# The file:// code object load must be reported as a solib-event
+	# stop.  The stop record is preceded by =library-loaded and console
+	# output, which the unanchored pattern skips.
+	mi_send_resuming_command "exec-continue" "continue to GPU load event"
+
+	gdb_expect {
+	    -re "\\*stopped,reason=\"solib-event\",added=\\\[library=\"file://\[^\r\n\]*\r\n$::mi_gdb_prompt$" {
+		pass "solib-event stop"
+	    }
+	    -re "$::mi_gdb_prompt$" {
+		fail "solib-event stop"
+	    }
+	    timeout {
+		fail "solib-event stop (timeout)"
+	    }
+	}
+    }
+}
+
+with_test_prefix "stop-on-solib-events=off" {
+    test_disabled
+}
+
+with_test_prefix "stop-on-solib-events=on" {
+    test_enabled
+}
+
+with_test_prefix "MI output" {
+    test_mi_output
+}

base-commit: 20a3bfdbee7e4615e96d779744e25e272beb1f4c
-- 
2.43.0


  reply	other threads:[~2026-11-19 14:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 13:04 [PATCH] " Bratislav Filipovic
2026-09-28 20:03 ` Simon Marchi
2026-09-28 20:33 ` Simon Marchi
2026-10-01 10:35   ` Bratislav Filipovic
2026-10-01 10:54     ` Eli Zaretskii
2026-10-01 19:26     ` Simon Marchi
2026-10-08 13:09       ` [PATCH v3] " Bratislav Filipovic
2026-10-12 14:32         ` Lancelot SIX
2026-10-28 17:17           ` [PATCH v4] " Bratislav Filipovic
2026-11-18 18:31             ` Lancelot SIX
2026-11-19 14:09               ` Bratislav Filipovic [this message]
2026-11-19 15:29                 ` [PATCH v5] " Lancelot SIX

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=20261119142057.58123-1-bfilipov@amd.com \
    --to=bfilipov@amd.com \
    --cc=gdb-patches@sourceware.org \
    --cc=lancelot.six@amd.com \
    /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