]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add convenience variables around linker namespace debugging
authorGuinevere Larsen <guinevere@redhat.com>
Fri, 21 Mar 2025 19:35:07 +0000 (16:35 -0300)
committerGuinevere Larsen <guinevere@redhat.com>
Wed, 23 Apr 2025 12:44:52 +0000 (09:44 -0300)
This commit adds 2 simple built-in convenience variables to help users
debug an inferior with multiple linker namespaces. The first is
$_active_linker_namespaces, which just counts how many namespaces have SOs
loaded onto them. The second is $_current_linker_namespace, and it tracks
which namespace 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_linker_namespace
variable to set a conditional breakpoint, while linespec changes aren't
finalized to make it more convenient.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-by: Kevin Buettner <kevinb@redhat.com>
gdb/NEWS
gdb/doc/gdb.texinfo
gdb/solib-svr4.c
gdb/solib.c
gdb/testsuite/gdb.base/default.exp
gdb/testsuite/gdb.base/dlmopen-ns-ids-main.c
gdb/testsuite/gdb.base/dlmopen-ns-ids.exp

index 4baed29d4caffa4b47a2efb9fb8321ed7c045a93..a47a1b85d21986e12d0ed9116527612ee4e4b00f 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
   namespace into which the library was loaded, if more than one namespace
   is active.
 
+* New built-in convenience variables $_active_linker_namespaces and
+  $_current_linker_namespace.  These show the number of active linkage
+  namespaces, and the namespace to which the current location belongs to.
+  In systems that don't support linkage namespaces, these always return 1
+  and [[0]] respectively.
+
  * Add record full support for rv64gc architectures
 
 * New commands
index d54ad651403e0fe0ed5993ecc137eda62f3d06ed..b423bb3692255e525fd93cc0565c28f7f3ee14d1 100644 (file)
@@ -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.
 
+@vindex $_active_linker_namespaces@r{, convenience variable}
+@item $_active_linker_namespaces
+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}.
+
+@vindex $_current_linker_namespace@r{, convenience variable}
+@item $_current_linker_namespace
+The namespace which contains the current location in the inferior.  This
+returns GDB's internal identifier for namespaces, which is @samp{[[@var{n}]]}
+where @var{n} is a zero-based namespace number.  In systems with no support
+for linkage namespaces, this variable will always be set to @samp{[[0]]}.
+
 @end table
 
 @node Convenience Funs
index 83cb389dad56d29314c0f6ce0748de6a318d5567..148359a0227ee2f074369f019357e37ec2cdfe8c 100644 (file)
@@ -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_linker_namespaces"),
+                          info->active_namespaces.size ());
 }
 
 /* Return whether DEBUG_BASE is the default namespace of INFO.  */
index 4876f1a92ea87ad6f16a3fb2daef935644888597..11c4d88caf2bf0e0ab0a110aa87fbacfe734ba98 100644 (file)
@@ -1715,6 +1715,44 @@ default_find_solib_addr (solib &so)
   return {};
 }
 
+/* Implementation of the current_linker_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_linker_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 ());
+}
+
+/* Implementation of `$_current_linker_namespace' variable.  */
+
+static const struct internalvar_funcs current_linker_namespace_funcs =
+{
+  current_linker_namespace_make_value,
+  nullptr,
+};
+
 void _initialize_solib ();
 
 void
@@ -1727,6 +1765,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_linker_namespace",
+                               &current_linker_namespace_funcs, nullptr);
+  set_internalvar_integer (lookup_internalvar ("_active_linker_namespaces"), 1);
+
   add_com (
     "sharedlibrary", class_files, sharedlibrary_command,
     _ ("Load shared object library symbols for files matching REGEXP."));
index d4d6b208057362fcabc2578a6951c5d30c3d6114..3abd04953872f9622b39b2e481eed3bf10c98030 100644 (file)
@@ -699,6 +699,8 @@ set show_conv_list \
        {$_gdb_minor = 1} \
        {$_shell_exitsignal = void} \
        {$_shell_exitcode = 0} \
+       {$_active_linker_namespaces = 1} \
+       {$_current_linker_namespace = <error: No registers.>}\
     }
 if [allow_python_tests] {
     append show_conv_list \
index 3bcd8196483e1f39a8d43d3c59a5034ecd7b843f..c7c038a08d1d377ca45dd25bd83332853b3f3d43 100644 (file)
@@ -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 */
index 3ddc07e7773851c0fefeb1da8abc0a1327c1e37a..1af57d13eb0a224cd00f5288861a63a677bbbd77 100644 (file)
@@ -105,4 +105,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_linker_namespaces" "1" \
+       "1 namespace before starting inferior"
+    gdb_test "print \$_current_linker_namespace" "No registers." \
+       "No current namespace before starting inferior"
+
+    if { ![runto_main] } {
+       return
+    }
+
+    gdb_test "print \$_active_linker_namespaces" "1" \
+       "Before activating namespaces"
+    gdb_test "print \$_current_linker_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_linker_namespace" ".*\"\\\[\\\[$dl\\\]\\\]\"" \
+           "Verify we're in namespace $dl"
+    }
+
+    gdb_continue_to_breakpoint "first dlclose"
+    gdb_test "print \$_active_linker_namespaces" "4" "all SOs loaded"
+
+    gdb_test "next" ".*second dlclose.*" "close one SO"
+    gdb_test "print \$_active_linker_namespaces" "3" "one SOs unloaded"
+    gdb_test "next" ".*third dlclose.*" "close another SO"
+    gdb_test "print \$_active_linker_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_linker_namespace, \"\[\[2\]\]\")"
+    gdb_continue_to_breakpoint "inc"
+    gdb_continue_to_end "" continue 1
+}
+
 test_info_shared
+test_conv_vars