From patchwork Fri Apr 11 17:50:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guinevere Larsen X-Patchwork-Id: 110270 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 14635382FADB for ; Fri, 11 Apr 2025 17:54:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14635382FADB Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JJj+IiEv X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 1F54E382FADB for ; Fri, 11 Apr 2025 17:51:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1F54E382FADB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1F54E382FADB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1744393876; cv=none; b=MRAzWIgQ3jAGy+0Zgrt8a2RM7pq4PQXDE6JqT/kbGcYq7Cb2+tHOuSnwc3EnxPbJd1VmuxukLEq2D1Qp/6oAKkYRL4mCC/1CyQvVt2ArCV/+JqW7gSldxOuHRneplKtClDAzpLivwx4CYCDRmpo/QyTPFFLgKYSiyj8YCanVyJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1744393876; c=relaxed/simple; bh=9sy5DIcUor5xg4dUx37ZsRPfkQFsUH4z3d97DQpMEg8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cGc8FAmGSVmblkIFmKhBC0FeBXfYVjMl1r0CpEpdQHhwjrv4hzFfhq8E0wASao5GsMnX4TReXO+fgW7jPF2xsl+I4E7X8+m9SFcMDtKhlDbHGdMPZNaZ4rqVpgXslSsk4LgumtJEdYbjoEbiVKXlhyYo9fHEbMdsyTrubO0ro2c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F54E382FADB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744393875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Il2idcG3WAVERDaVePefQAi5MQgJ9Yr2n75S8rL14qw=; b=JJj+IiEvo6Fwxf05GhF2tBWIs+y+Otb8hXXbeWY9d5vGIQKXudzpasOhQtADfDYS3zvRLZ GAGM5pTg70QZDMjy+Uov3VZLUD9fD84y318wGNDloxqitmQ5KQkeGKiLgN8/Zp+56G8K4q LKX3x6b9tZtT5mWYgNBKXkTym/ElHFY= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-682-tlZKNo1DN3CIJ1zQmwQU4A-1; Fri, 11 Apr 2025 13:51:14 -0400 X-MC-Unique: tlZKNo1DN3CIJ1zQmwQU4A-1 X-Mimecast-MFC-AGG-ID: tlZKNo1DN3CIJ1zQmwQU4A_1744393873 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6F41B180899B; Fri, 11 Apr 2025 17:51:13 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.96.134.43]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 81A0C1955DCE; Fri, 11 Apr 2025 17:51:11 +0000 (UTC) From: Guinevere Larsen To: gdb-patches@sourceware.org Cc: Guinevere Larsen , Eli Zaretskii Subject: [PATCH v3 1/3] gdb: add convenience variables around linker namespace debugging Date: Fri, 11 Apr 2025 14:50:20 -0300 Message-ID: <20250411175022.2379494-2-guinevere@redhat.com> In-Reply-To: <20250411175022.2379494-1-guinevere@redhat.com> References: <20250411175022.2379494-1-guinevere@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 4amATQAK476sSk9iNoopIavfjdxdrXKDZQisL60tQyc_1744393873 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org 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 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_linker_namespace variable to set a conditional breakpoint, while linespec changes aren't finalized to make it more convenient. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 6 +++ gdb/doc/gdb.texinfo | 12 +++++ gdb/solib-svr4.c | 6 +++ gdb/solib.c | 43 +++++++++++++++ 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, 130 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index 99ec392d4c4..053541683f1 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -40,6 +40,12 @@ 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. + * New commands maintenance check psymtabs diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index b251c8e1228..2dda8abd407 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. +@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 diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 83cb389dad5..148359a0227 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_linker_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 4876f1a92ea..1ba98137f5e 100644 --- a/gdb/solib.c +++ b/gdb/solib.c @@ -1715,6 +1715,42 @@ 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 '[[]]'. 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 ()); +} + +static const struct internalvar_funcs current_linker_namespace_funcs = +{ + current_linker_namespace_make_value, + nullptr, +}; + void _initialize_solib (); void @@ -1727,6 +1763,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", + ¤t_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.")); diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp index d4d6b208057..3abd0495387 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_linker_namespaces = 1} \ + {$_current_linker_namespace = }\ } 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 3ddc07e7773..1af57d13eb0 100644 --- a/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp +++ b/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp @@ -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