From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id yqXgOKn6vGqvKx4AWB0awg (envelope-from ) for ; Wed, 30 Sep 2026 08:03:53 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=Eamt0Bde; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id D296D1E051; Wed, 30 Sep 2026 08:03:53 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 82F6D1E033 for ; Wed, 30 Sep 2026 08:03:51 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 536734BB24CF for ; Wed, 30 Sep 2026 12:03:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 536734BB24CF Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=Eamt0Bde Received: from mail-oo2-x2b.google.com (mail-oo2-x2b.google.com [IPv6:2607:f8b0:4864:31::2b]) by sourceware.org (Postfix) with ESMTPS id 5222B4BAE7F0 for ; Wed, 30 Sep 2026 12:03:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5222B4BAE7F0 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5222B4BAE7F0 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:31::2b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790769797; cv=none; b=ZZOev6JoweHE4ivokDm36+D27FgywsOtBAG3GBCn3PiculzdsQWar/n11cU/VvEtYf8vXZThSDklMpCN3jVWzHVca2xzgvz0iqwof/T9XZX2ctedA2vqn/2OOQTlbLJbugFtvGQxUrqFRxm0ALiyB+S71KrRlZefn07zyaXFkFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790769797; c=relaxed/simple; bh=WMm71xFNfN7zBWi7WeBuLnBAYlHYJs3AGoCMvkqkV6g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=w+tHqzsG7QoRvHygQxo/UvbWQKDlNP2Vt2VFehdYOLhUDH7h5QvE7mRzwZbEIn5+EtXNs2AhcpCESScDdFB3w/bM5uU1Eewcz/UQGrjtLRCRsniYaGvp0D+YwofImHpcgF7padH6ScWqSTvzqcnaVw3cqL+2xvSmfnWLe7SwrO4= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=Eamt0Bde DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5222B4BAE7F0 Received: by mail-oo2-x2b.google.com with SMTP id 46e09a7af769-8144632e066so2872160a34.0 for ; Wed, 30 Sep 2026 05:03:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1790769796; x=1791374596; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=MfXAZbi+xx7EnjzxFmaGc3aKDNxuBlq3eSpLUlmqf4Q=; b=Eamt0Bde4zJWnwx2boXQydUZ3iT7j3yrEruRz1KW/hnNJf1/VXOfCeu1WkIDFWpAPD yytjW0xlkAXw//uXxSpSVgF7pZAtw0h9yeJVWquJe3aj1ZfOREN+8UaaJ6sB5tG4DgGT 6vY2sm0f8OmUP7zKjkk2+eGTrFNeokCLMrlhKPU7XgcJfmb9xnkhMPm11Hoiu60IS6n1 AEaUN8sb6bVk62dPbOYc6OSSpDaBTtSj7JxslfIpL5sfqJsxmiCJlQ25SRhn+HtXn4lF 9AaGjh2Tj4vKiNKSdFSxHN4k9oR1hAsyQuwsYxE+UjI65T9k4q6fZhEj04k3b82LPZ6n cV1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790769796; x=1791374596; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MfXAZbi+xx7EnjzxFmaGc3aKDNxuBlq3eSpLUlmqf4Q=; b=rHxQ1GetbRFyc3u5yAgaQMJ7rkhxV/EbnTNqLTv+JvzyqXXhzAsfKX7MidKvJ4WKkT luquBj/z6c3SF7iQ01fGR+YSn7pVfs4QMSNoMm6p892yKmL2GyJ3zuBZMoQH3WCzjtYB cRkef6kR10/t1/nW965VVYCAWLOhRnP1smKXTgyIYfQG6mDbHuNKY7lRnX/8+DK7w0yl m3mmdAYmK9kkn1rjKUKsvz0Lf/dulfG28fQa9wvGU+L+Yyp+nxhpyGkpIL420OFZlInm FsumTuYlax09jflkgDHtOWqdxAWABDYL95ysIl3lGiwu7H36pJFCN5nWca3b0b2N820Z h41A== X-Gm-Message-State: AFuF++msgxFi/g6nrPqoHUM6hnNhYQZe/gNKPuQ1X5oeKE0dMfM0xp87 D4z5HtSY80aiOhpMm5qUfIZzKtxfjTDrmRZf3HbDVYWahiT7jZvqggPtc5LxNXYcDSPggPeoKMX o3YtTJA== X-Gm-Gg: AYBFou3xMB+OMDkbA/59l7RW8cPO2YoFOSTLb7QGfNgcoQuqKJEEgzDD7Xy2zRzp+Yg C2WahxSA98qqutSSKY6D8hYIXGJfXL1wydrhmvTm05Xabnl11U+mcIgLBkJKMaVKqOfUyeMhhLx ywPsfNRkedcCahECGFdNslGewuThACULRicRdlbGyrEtXGaF0wfhcztJ8QovldwSYLqkbWB4IgW Z5dve2ckydCJEKH5egkH+jM4JwN1AU8X7sXuAzFFgXLd1nB6CJfc4poSEM805Tim2OVhEZyPxKI zKxEeVXFh0szUHviXZhM2e8EYqiux5kyz6Nvs0cxsQywT2OIsZxu61PevGTSmecOYd7+P67eoML lCodA9xlyqnKzFZYvML+ZAExQ1WtCX4/F+fYEOYUOAdMaTjInMDBZH5rc8+IK+giyEfFc9SKkf2 dH09ML02yqYdI5IjWP04ffOrboWEq95uejf0izVeNu2bw1Pq01nicG80ej94tILrXoMNz9Dbfb9 c263DEmK806gwJZBg== X-Received: by 2002:a05:6830:2b0f:b0:81d:7923:b61f with SMTP id 46e09a7af769-8204d239544mr1337218a34.5.1790769796554; Wed, 30 Sep 2026 05:03:16 -0700 (PDT) Received: from bapiya (75-166-229-74.hlrn.qwest.net. [75.166.229.74]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-820631d8136sm777284a34.15.2026.09.30.05.03.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2026 05:03:15 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Fix formatted print of functions Date: Wed, 30 Sep 2026 06:03:13 -0600 Message-ID: <20260930120313.3096634-1-tromey@adacore.com> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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~public-inbox=simark.ca@sourceware.org A user noticed that formatted printing of functions is strange. That is, printing a function normally does what you'd expect, but "print/d" or "print/x" will, bizarrely, instead print the value of the first byte of the function's body: (gdb) print main $1 = {} 0x393c20
(gdb) print/x main $2 = 0x41 This doesn't seem useful or intuitive to me. This patch changes gdb to instead simply print the function pointer using the specified format. Regression tested on x86-64 Fedora 43. v2: Fixed all the mistakes in the tests. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34685 --- gdb/testsuite/gdb.base/printcmds.exp | 13 +++++++++++++ gdb/testsuite/gdb.cp/printmethod.exp | 11 ++++++++++- gdb/valprint.c | 8 ++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp index 9de1bcdd1a3..2ae19e9e440 100644 --- a/gdb/testsuite/gdb.base/printcmds.exp +++ b/gdb/testsuite/gdb.base/printcmds.exp @@ -1176,6 +1176,18 @@ proc test_printf_convenience_var {prefix} { } } +# Test formatted print with a function. See PR gdb/34685. +proc test_function_print {} { + with_test_prefix function { + gdb_test_no_output "set print address on" + set out [get_valueof "" main ""] + gdb_test_no_output "set print address off" + } + regexp -- [subst_vars {^.* ($::hex) .*$}] $out ignore value + gdb_test "print/x main" " = $value" + gdb_test "print/d main" " = [expr {$value}]" +} + clean_restart gdb_test "print \$pc" "No registers\\." @@ -1267,3 +1279,4 @@ test_print_symbol test_repeat_bytes test_radices test_repeat_bytes_limit +test_function_print diff --git a/gdb/testsuite/gdb.cp/printmethod.exp b/gdb/testsuite/gdb.cp/printmethod.exp index fdc1c91bf23..0da08b1f7ee 100644 --- a/gdb/testsuite/gdb.cp/printmethod.exp +++ b/gdb/testsuite/gdb.cp/printmethod.exp @@ -49,4 +49,13 @@ gdb_test "print theA->nonvirt" \ "${::valnum_re} = {void \\(A \\* const\\)} $hex " \ "print nonvirtual method." -gdb_exit +proc test_formatted {exp} { + set out [get_valueof "" $exp ""] + regexp -- [subst_vars {^.* ($::hex) .*$}] $out ignore value + gdb_test "print/x $exp" " = $value" + gdb_test "print/d $exp" " = [expr {$value}]" +} + +# Test for PR gdb/34685. +test_formatted A::nonvirt +test_formatted theA->virt diff --git a/gdb/valprint.c b/gdb/valprint.c index 83a442dcef5..211bdca04f1 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -1300,6 +1300,14 @@ value_print_scalar_formatted (struct value *val, gdb_assert (val != NULL); + /* Treat a function or a method as if it were a pointer. */ + if (type->code () == TYPE_CODE_FUNC + || type->code () == TYPE_CODE_METHOD) + { + val = value_addr (val); + type = check_typedef (val->type ()); + } + /* If we get here with a string format, try again without it. Go all the way back to the language printers, which may call us again. */ base-commit: 9decc44bc67d9fb4248c2afe4fde77174e8abf80 -- 2.55.0