From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 61r/J1/3u2qS9hgAWB0awg (envelope-from ) for ; Tue, 29 Sep 2026 13:37:35 -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=ei+FwSZJ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 7D1951E01F; Tue, 29 Sep 2026 13:37:35 -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 A86DF1E01F for ; Tue, 29 Sep 2026 13:37:34 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BF05D4B92086 for ; Tue, 29 Sep 2026 17:37:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF05D4B92086 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=ei+FwSZJ Received: from mail-oo2-x2a.google.com (mail-oo2-x2a.google.com [IPv6:2607:f8b0:4864:31::2a]) by sourceware.org (Postfix) with ESMTPS id F1C3F4BB3BA6 for ; Tue, 29 Sep 2026 17:37:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1C3F4BB3BA6 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 F1C3F4BB3BA6 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:31::2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790703429; cv=none; b=kjvznYMkvJgV2eh2pw/MMGENOSjyj3ynTiMNIWxaZ+ZkZvzcmViX2x9rvhX8IbVoS77LLyEBk2gDMPUhUCmINIze/TuXfh5QHbcgNeJYB9baFc+ZqLumaK2AyMUkEz0TDzYIyEo29LMwpNeG4Da0M/uFTgTrizYm9JtITa1LsdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790703429; c=relaxed/simple; bh=vMnxejpAPrFtusr+tyYn3FUUqkkNDN+1q5FkgJ7PWmk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B51rIXlUwdCKDUpy+ZophaAM2S8Gvv+NrHSJnj3fneypE7k0f9Nd0h+ygcp+Ba4o+fSqltuL0jdWQTCY+wktOpK+8DQ6IL2Cvb+8et/fTFNnzb1Kd0gc01fSrQRyH+yFZnj6GAMP7E2d1BFF0PJtglicc0nw8K+UpIuFzhFm3Gc= 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=ei+FwSZJ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F1C3F4BB3BA6 Received: by mail-oo2-x2a.google.com with SMTP id 006d021491bc7-6d8a300d018so1390840eaf.1 for ; Tue, 29 Sep 2026 10:37:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1790703427; x=1791308227; 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=1aI1AGF2vMNUhyVZxnVVvhF0LBD0iqbHyXhY+zp9SFY=; b=ei+FwSZJ3Ji/kBnPZ+jbtoAeg6u21WO3LHFj418RUvmnlIfnXRx/uELjYXJLhJWI1K v/CTTE4h++gpRT1S5jm0JpwTjTaTJqpk98u3va0dCFs7v34B+8/KbBkUVkgPy5PIKxrh swWdeJUS5vuo3sbSkoUEBA2yH1UAeIxiFscKULyocV1OCuPBWuprwE5rtFUx6SuU3icl 0mYF3i6b9KuHP7O/DRy0GhJ5TIZ5LgieH/f5tQaUVcp1kfXq+1AmsonNt67peVNh/tYt sUtk6beoo9Oy1bGTD4pBBqwMDRlJdThw4NtNpQepvYwM1hx8epx3TTW1kOLf8dxoFyAZ VDGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790703427; x=1791308227; 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=1aI1AGF2vMNUhyVZxnVVvhF0LBD0iqbHyXhY+zp9SFY=; b=YH22Ea6LWyfxofhHcup6nT1lI0EVOW1Ev8gymFnMiGAUQIDb+Oi3F66QTqF2FTMHCV /aTp93WPOSHaaLcLYWYTnnQdbbJYzg29CcFZN6zOZGI9Ks6AlgwtpSop4VWTaJwLg5L6 ltMZIHBbHiwz13AVV8Ze4ti319HKWIcJyiOqNmp1cH35hRAm9oIVXYU0QjL2gZFyJYmo 5yCFC11lP/U7xpakCdi1L88lYarA236E/HySn8uMcLbw8cYfDr79tDFEsDnoZNbfEXp2 3ZWnmyB4hHkzJK5MqKpcEzceoRQUXH8m0yXZbnT2pMmHohzYwV2Z4UKstxIgrTbaYNKn DKKQ== X-Gm-Message-State: AFuF++lmwhuRZ8+6x0sVGbT942YQ0QYoJmrh8guRokpEwSIVH/tpnU/e vUZfatKsannV1z6jCgfAwenCwNB7x5mNLwya9LjR6VBb4rfNNHyngudx5F9utvjl5y/ZGMS3CmO 3AR6RZg== X-Gm-Gg: AYBFou1cNSQKGfK9DMD9C0WqILX73Ch+UZexkn3ZKkvSO4oqrfUM+ZpQxENN9NNYucN /8SUlUJ6a3bvOkNJXNZBxeweZbFTUtHEca2Mkyv3cLxm54rqKTuGv+l5M6kyDRtkUV971f94knI vlkdwI5mutne/cPP49krVDqMAdY4gTo2nTq5QjnWHjeHZj4w9O0MwMPXoD/w46ka1qtWafbSDiU bDYVemTZWgjm78g3PIer+ApIBLYLAauftMywu06iylbB+krdMqFcq3Jxge50Ed6yUORdXPTQ4fQ Neoiesbmh12aK8nHaiKaBfu5869A5F0NysS4AIl6TBdkV3ojlg4jHQYUet8Acude7VWATBy2LbG OlnpBgG+x3igb5mRkSK3G3I2dvbb9qOlGhyiUB2Vax8gLxZQv4i7jNiYSNsIUsJ5lB26GszZ8pt ZuPMYsEpSC6wgXs4o28BjgOqGT1UmEWcLZ9Xcp7rjVSJEbiMFG25cK7RwbfMDqveD5kzkjOO4Xj YreB87pS197xq7TNJNz0yF+pK4rfw== X-Received: by 2002:a05:6820:810c:b0:6cd:3fdb:fa66 with SMTP id 006d021491bc7-6d441c7d504mr14224762eaf.81.1790703427167; Tue, 29 Sep 2026 10:37:07 -0700 (PDT) Received: from bapiya (75-166-229-74.hlrn.qwest.net. [75.166.229.74]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-6dc4639e706sm482624eaf.12.2026.09.29.10.37.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Sep 2026 10:37:06 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Fix formatted print of functions Date: Tue, 29 Sep 2026 11:37:04 -0600 Message-ID: <20260929173704.2866399-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. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34685 --- gdb/testsuite/gdb.base/printcmds.exp | 7 +++++++ gdb/testsuite/gdb.cp/printmethod.exp | 11 ++++++++++- gdb/valprint.c | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp index 9de1bcdd1a3..940622abb76 100644 --- a/gdb/testsuite/gdb.base/printcmds.exp +++ b/gdb/testsuite/gdb.base/printcmds.exp @@ -1176,6 +1176,13 @@ proc test_printf_convenience_var {prefix} { } } +# Test formatted print with a function. See PR gdb/34685. +proc test_function_print {} { + set value [get_var_address main] + gdb_test "print/x main" " = $value" + gdb_test "print/d main" " = [expr {$value}]" +} + clean_restart gdb_test "print \$pc" "No registers\\." diff --git a/gdb/testsuite/gdb.cp/printmethod.exp b/gdb/testsuite/gdb.cp/printmethod.exp index fdc1c91bf23..e19646b0366 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