Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Guinevere Larsen <guinevere@redhat.com>
To: gdb-patches@sourceware.org
Cc: Guinevere Larsen <guinevere@redhat.com>
Subject: [PATCH 1/3] gdb: add convenience variables around linker namespace debugging
Date: Mon,  7 Apr 2025 11:54:41 -0300	[thread overview]
Message-ID: <20250407145443.1895323-2-guinevere@redhat.com> (raw)
In-Reply-To: <20250407145443.1895323-1-guinevere@redhat.com>

This commit adds 2 simple built-in convenience variables to help users
debug an inferior with multiple linker namespaces. The first is
$_active_namespaces, which just counts how many namespaces have SOs
loaded onto them. The second is $_current_namespace, and it tracks which
namespace does the current location in the inferior belongs to.

This commit also introduces a test ensuring that we track namespaces
correctly, and that a user can use the $_current_namespace variable to
set a conditional breakpoint, while linespec changes aren't finalized to
make it more convenient.
---
 gdb/NEWS                                     |  5 ++
 gdb/doc/gdb.texinfo                          | 12 +++++
 gdb/solib-svr4.c                             |  6 +++
 gdb/solib.c                                  | 42 +++++++++++++++
 gdb/testsuite/gdb.base/default.exp           |  2 +
 gdb/testsuite/gdb.base/dlmopen-ns-ids-main.c |  6 +++
 gdb/testsuite/gdb.base/dlmopen-ns-ids.exp    | 55 ++++++++++++++++++++
 7 files changed, 128 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 99ec392d4c4..4bf380cbadd 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -40,6 +40,11 @@
   namespace into which the library was loaded, if more than one namespace
   is active.
 
+* New built-in convenience variables $_active_namespaces and
+  $_current_namespace.  These show the number of active linkage namespaces,
+  and which one the current location belongs to. In systems that don't
+  support linkage namespaces, these always return 1 and [[0]] respectively.
+
 * New commands
 
 maintenance check psymtabs
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4e4509a4649..f0ebf4b7dcf 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -13046,6 +13046,18 @@ variable which may be @samp{truecolor} or @samp{24bit}. Other color spaces are
 determined by the "Co" termcap which in turn depends on the @env{TERM}
 environment variable.
 
+@item $_active_namespaces
+@vindex $_active_namespaces@r{, convenience variable}
+Number of active linkage namespaces in the inferior.  In systems with no
+support for linkage namespaces, this variable will always be set to @samp{1}.
+
+@item $_current_namespace
+@vindex $_current_namespace@r{, convenience variable}
+Which namespace contains the current location in the inferior.  This returns
+GDB's internal identifier for namespaces, which is @samp{[[]]} with an
+integer in the middle.  In systems with no support for linkage namespaces,
+this variable will always be set to @samp{[[0]]}
+
 @end table
 
 @node Convenience Funs
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 2f839bd7b0b..60b61e7e07d 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -451,6 +451,12 @@ svr4_maybe_add_namespace (svr4_info *info, CORE_ADDR lmid)
     info->namespace_id.push_back (lmid);
 
   info->active_namespaces.insert (i);
+
+  /* Create or update the convenience variable "active_namespaces".
+     It only needs to be updated here, as this only changes when a
+     dlmopen or dlclose call happens.  */
+  set_internalvar_integer (lookup_internalvar ("_active_namespaces"),
+			   info->active_namespaces.size ());
 }
 
 /* Return whether DEBUG_BASE is the default namespace of INFO.  */
diff --git a/gdb/solib.c b/gdb/solib.c
index 3ddd4f919c6..52fd6410429 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1715,6 +1715,41 @@ default_find_solib_addr (solib &so)
   return {};
 }
 
+/* Implementation of the current_namespace convenience variable.
+   This returns the GDB internal identifier of the linker namespace,
+   for the current frame, in the form '[[<number>]]'.  If the inferior
+   doesn't support linker namespaces, this always returns [[0]].  */
+
+static value *
+current_namespace_make_value (gdbarch *gdbarch, internalvar *var, void *ignore)
+{
+  const solib_ops *ops = gdbarch_so_ops (gdbarch);
+  const language_defn *lang = language_def (get_frame_language
+					      (get_current_frame ()));
+  std::string nsid = "[[0]]";
+  if (ops->find_solib_ns != nullptr)
+    {
+      CORE_ADDR curr_pc = get_frame_pc (get_current_frame ());
+      for (const solib &so : current_program_space->solibs ())
+	if (solib_contains_address_p (so, curr_pc))
+	  {
+	    nsid = string_printf ("[[%d]]", ops->find_solib_ns (so));
+	    break;
+	  }
+    }
+
+
+  /* If the PC is not in an SO, or the solib_ops doesn't support
+     linker namespaces, the inferior is in the default namespace.  */
+  return lang->value_string (gdbarch, nsid.c_str (), nsid.length ());
+}
+
+static const struct internalvar_funcs current_namespace_funcs =
+{
+  current_namespace_make_value,
+  nullptr,
+};
+
 void _initialize_solib ();
 
 void
@@ -1727,6 +1762,13 @@ _initialize_solib ()
   },
     "solib");
 
+  /* Convenience variables for debugging linker namespaces.  These are
+     set here, even if the solib_ops doesn't support them,
+     for consistency.  */
+  create_internalvar_type_lazy ("_current_namespace",
+				&current_namespace_funcs, nullptr);
+  set_internalvar_integer (lookup_internalvar ("_active_namespaces"), 1);
+
   add_com (
     "sharedlibrary", class_files, sharedlibrary_command,
     _ ("Load shared object library symbols for files matching REGEXP."));
diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp
index 93a6733c9ad..8e42b23d038 100644
--- a/gdb/testsuite/gdb.base/default.exp
+++ b/gdb/testsuite/gdb.base/default.exp
@@ -699,6 +699,8 @@ set show_conv_list \
 	{$_gdb_minor = 1} \
 	{$_shell_exitsignal = void} \
 	{$_shell_exitcode = 0} \
+	{$_active_namespaces = 1} \
+	{$_current_namespace = <error: No registers.>}\
     }
 if [allow_python_tests] {
     append show_conv_list \
diff --git a/gdb/testsuite/gdb.base/dlmopen-ns-ids-main.c b/gdb/testsuite/gdb.base/dlmopen-ns-ids-main.c
index 3bcd8196483..c7c038a08d1 100644
--- a/gdb/testsuite/gdb.base/dlmopen-ns-ids-main.c
+++ b/gdb/testsuite/gdb.base/dlmopen-ns-ids-main.c
@@ -41,6 +41,12 @@ main (void)
   handle[2] = dlmopen (LM_ID_NEWLM, DSO_NAME, RTLD_LAZY | RTLD_LOCAL);
   assert (handle[2] != NULL);
 
+  for (dl = 2; dl >= 0; dl--)
+    {
+      fun = dlsym (handle[dl], "inc");
+      fun (dl);
+    }
+
   dlclose (handle[0]); /* TAG: first dlclose */
   dlclose (handle[1]); /* TAG: second dlclose */
   dlclose (handle[2]); /* TAG: third dlclose */
diff --git a/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp b/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp
index 03b7a527af5..51d461d05a3 100644
--- a/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp
+++ b/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp
@@ -103,4 +103,59 @@ proc test_info_shared {} {
 	"after unloading everything"
 }
 
+# Run all tests related to the linkage namespaces convenience
+# variables, _active_namespaces and _current_namespaces.
+proc_with_prefix test_conv_vars {} {
+    clean_restart $::binfile
+
+    gdb_test "print \$_active_namespaces" "1" \
+	"1 namespace before starting inferior"
+    gdb_test "print \$_current_namespace" "No registers." \
+	"No current namespace before starting inferior"
+
+    if { ![runto_main] } {
+	return
+    }
+
+    gdb_test "print \$_active_namespaces" "1" \
+	"Before activating namespaces"
+    gdb_test "print \$_current_namespace" ".*\"\\\[\\\[0\\\]\\\]\"" \
+	"Still in the default namespace"
+
+    gdb_breakpoint "inc" allow-pending
+    gdb_breakpoint [gdb_get_line_number "TAG: first dlclose"]
+
+    foreach_with_prefix dl {3 2 1} {
+	gdb_continue_to_breakpoint "inc"
+
+	gdb_test "print \$_current_namespace" ".*\"\\\[\\\[$dl\\\]\\\]\"" \
+	    "Verify we're in namespace $dl"
+    }
+
+    gdb_continue_to_breakpoint "first dlclose"
+    gdb_test "print \$_active_namespaces" "4" "all SOs loaded"
+
+    gdb_test "next" ".*second dlclose.*" "close one SO"
+    gdb_test "print \$_active_namespaces" "3" "one SOs unloaded"
+    gdb_test "next" ".*third dlclose.*" "close another SO"
+    gdb_test "print \$_active_namespaces" "2" "two SOs unloaded"
+
+    # Restarting GDB so that we can test setting a breakpoint
+    # using the convenience variable, while a proper bp syntax
+    # isn't implemented for namespaces
+    clean_restart $::binfile
+    if {![runto_main]} {
+	return
+    }
+
+    # We need to load one SO because you can't have confitional
+    # breakpoints and pending breakpoints at the same time with
+    # gdb_breakpoint.
+    gdb_test "next" ".*assert.*" "load the first SO"
+    gdb_breakpoint "inc if \$_streq(\$_current_namespace, \"\[\[2\]\]\")"
+    gdb_continue_to_breakpoint "inc"
+    gdb_continue_to_end "" continue 1
+}
+
 test_info_shared
+test_conv_vars
-- 
2.49.0


  reply	other threads:[~2025-04-07 14:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-07 14:54 [PATCH 0/3] Add some linker namespaces conveniences Guinevere Larsen
2025-04-07 14:54 ` Guinevere Larsen [this message]
2025-04-07 15:39   ` [PATCH 1/3] gdb: add convenience variables around linker namespace debugging Eli Zaretskii
2025-04-07 17:10     ` Guinevere Larsen
2025-04-07 14:54 ` [PATCH 2/3] gdb: factor out printing a table of solibs for info sharedlibrary Guinevere Larsen
2025-04-07 14:54 ` [PATCH 3/3] GDB: Introduce "info namespaces" command Guinevere Larsen
2025-04-07 15:45   ` Eli Zaretskii
2025-04-07 17:04     ` Guinevere Larsen
2025-04-07 18:08 ` [PATCH 0/3] Add some linker namespaces conveniences Kevin Buettner
2025-04-09 20:20   ` Guinevere Larsen

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=20250407145443.1895323-2-guinevere@redhat.com \
    --to=guinevere@redhat.com \
    --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