]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: rework _active_linker_namespaces variable
authorGuinevere Larsen <guinevere@redhat.com>
Thu, 14 Aug 2025 14:25:38 +0000 (11:25 -0300)
committerGuinevere Larsen <guinevere@redhat.com>
Wed, 20 Aug 2025 20:21:39 +0000 (17:21 -0300)
This commit reworks the _active_linker_namespaces convenience variable
following Simon's feedback here:
https://sourceware.org/pipermail/gdb-patches/2025-August/219938.html

This patch implements the renaming to _linker_namespace_count (following
the standard set by _inferior_thread_count) and makes the convenience
variable more resilient in the multi-inferior case by providing a new
function, solib_linker_namespace_count, which counts gets the count of
namespaces using the solib_ops of the provided program_space

Approved-By: Simon Marchi <simon.marchi@efficios.com>
gdb/NEWS
gdb/doc/gdb.texinfo
gdb/solib-svr4.c
gdb/solib.c
gdb/solib.h
gdb/testsuite/gdb.base/default.exp
gdb/testsuite/gdb.base/dlmopen-ns-ids.exp

index a8c17318d7eab0350ed42e5b3eafa56e58ffe3ce..db0e74a2623f931cfbf9911ad272a42ff45ff451 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
+* New built-in convenience variables $linker_namespace_count and
   $_linker_namespace.  These show the number of active linker
   namespaces, and the namespace to which the current location belongs to.
-  In systems that don't support linker namespaces, these always return
-  the integers 1 and 0 respectively.
+  In systems that don't support linker namespaces, or if the inferior hasn't
+  started yet, these always return the integer 0.
 
 * Add record full support for rv64gc architectures
 
index d4a5a632c24306f6c0ff6bd6d14e01a847e9a494..7718a9c4d838956a2b32e29e9a692cbe101d4fd0 100644 (file)
@@ -13104,11 +13104,11 @@ 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
+@vindex $_linker_namespace_count@r{, convenience variable}
+@item $_linker_namespace_count
 Number of active linker namespaces in the inferior (@pxref{Files}).  In systems
-with no support for linker namespaces, this variable will always be set to
-@samp{1}.
+with no support for linker namespaces or if the inferior hasn't started, this
+variable will always be set to @samp{0}.
 
 @vindex $_linker_namespace@r{, convenience variable}
 @item $_linker_namespace
index af08b754c14e593b88f680acbe11b41f986fe02f..f90757125df754304abc21a9bc675534718dd634 100644 (file)
@@ -435,12 +435,6 @@ 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 3ec2032f01289011ba1bc3a651b076364f9438f1..316ffd64a273945ea87b16bca98dd7dfb53c5733 100644 (file)
@@ -1805,6 +1805,18 @@ remove_user_added_objfile (struct objfile *objfile)
     }
 }
 
+/* See solib.h.  */
+
+int
+solib_linker_namespace_count (program_space *pspace)
+{
+  if (const auto ops = pspace->solib_ops (); ops != nullptr
+      && ops->supports_namespaces ())
+    return ops->num_active_namespaces ();
+
+  return 0;
+}
+
 /* Implementation of the linker_namespace convenience variable.
 
    This returns the GDB internal identifier of the linker namespace,
@@ -1840,6 +1852,23 @@ static const struct internalvar_funcs linker_namespace_funcs =
   nullptr,
 };
 
+static value *
+linker_namespace_count_make_value (gdbarch *gdbarch, internalvar *var,
+                                  void *ignore)
+{
+  return value_from_longest
+    (builtin_type (gdbarch)->builtin_int,
+     solib_linker_namespace_count (current_program_space));
+}
+
+/* Implementation of `$_linker_namespace_count' variable.  */
+
+static const struct internalvar_funcs linker_namespace_count_funcs =
+{
+  linker_namespace_count_make_value,
+  nullptr,
+};
+
 INIT_GDB_FILE (solib)
 {
   gdb::observers::free_objfile.attach (remove_user_added_objfile, "solib");
@@ -1854,7 +1883,8 @@ INIT_GDB_FILE (solib)
      for consistency.  */
   create_internalvar_type_lazy ("_linker_namespace",
                                &linker_namespace_funcs, nullptr);
-  set_internalvar_integer (lookup_internalvar ("_active_linker_namespaces"), 1);
+  create_internalvar_type_lazy ("_linker_namespace_count",
+                               &linker_namespace_count_funcs, nullptr);
 
   add_com (
     "sharedlibrary", class_files, sharedlibrary_command,
index b9465e103bdd89c83ba575d7551ce84e5e1c7a36..f6d6de92216908baa1bbcc2a77139355cb2f3998 100644 (file)
@@ -373,4 +373,8 @@ extern void update_solib_breakpoints (void);
 
 extern void handle_solib_event (void);
 
+/* Calculate the number of linker namespaces active in PSPACE.  */
+
+extern int solib_linker_namespace_count (program_space *pspace);
+
 #endif /* GDB_SOLIB_H */
index d8571654ad9e106572d3c57a860d3cd952779a8c..b5e64c200bd47296161628992fc546af890bd7e1 100644 (file)
@@ -768,7 +768,7 @@ set show_conv_list \
        {$_gdb_setting = <internal function _gdb_setting>} \
        {$_shell_exitsignal = void} \
        {$_shell_exitcode = 0} \
-       {$_active_linker_namespaces = 1} \
+       {$_linker_namespace_count = 0} \
        {$_linker_namespace = <error: No registers.>}\
     }
 if [allow_python_tests] {
index 4d3e8eba2ab13bdd8fe74f5c1cdcff3ce72b191d..e23edbfa2405e88b1af0f6af606942b9945c6e11 100644 (file)
@@ -107,11 +107,13 @@ proc test_info_shared {} {
 
 # Run all tests related to the linkage namespaces convenience
 # variables, _active_namespaces and _current_namespaces.
+# Also tests that the namespace ID is only printed at the correct
+# times.
 proc_with_prefix test_conv_vars {} {
     clean_restart $::binfile
 
-    gdb_test "print \$_active_linker_namespaces" "1" \
-       "1 namespace before starting inferior"
+    gdb_test "print \$_linker_namespace_count" "0" \
+       "0 namespace before starting inferior"
     gdb_test "print \$_linker_namespace" "No registers." \
        "No current namespace before starting inferior"
 
@@ -119,7 +121,7 @@ proc_with_prefix test_conv_vars {} {
        return
     }
 
-    gdb_test "print \$_active_linker_namespaces" "1" \
+    gdb_test "print \$_linker_namespace_count" "1" \
        "Before activating namespaces"
     gdb_test "print \$_linker_namespace" ".* = 0" \
        "Still in the default namespace"
@@ -141,12 +143,12 @@ proc_with_prefix test_conv_vars {} {
        "print namespace of selected frame"
 
     gdb_continue_to_breakpoint "first dlclose"
-    gdb_test "print \$_active_linker_namespaces" "4" "all SOs loaded"
+    gdb_test "print \$_linker_namespace_count" "4" "all SOs loaded"
 
     gdb_test "next" ".*second dlclose.*" "close one SO"
-    gdb_test "print \$_active_linker_namespaces" "3" "one SOs unloaded"
+    gdb_test "print \$_linker_namespace_count" "3" "one SOs unloaded"
     gdb_test "next" ".*third dlclose.*" "close another SO"
-    gdb_test "print \$_active_linker_namespaces" "2" "two SOs unloaded"
+    gdb_test "print \$_linker_namespace_count" "2" "two SOs unloaded"
 
     # Restarting GDB so that we can test setting a breakpoint
     # using the convenience variable, while a proper bp syntax