[v2] Fix formatted print of functions

Message ID 20260930120313.3096634-1-tromey@adacore.com
State New
Headers
Series [v2] Fix formatted print of functions |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed

Commit Message

Tom Tromey Sept. 30, 2026, 12:03 p.m. UTC
  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 = {<text variable, no debug info>} 0x393c20 <main>
    (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(-)


base-commit: 9decc44bc67d9fb4248c2afe4fde77174e8abf80
  

Comments

Tom de Vries Oct. 1, 2026, 8:46 a.m. UTC | #1
On 9/30/26 2:03 PM, Tom Tromey wrote:
> 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 = {<text variable, no debug info>} 0x393c20 <main>
>      (gdb) print/x main
>      $2 = 0x41
> 
> This doesn't seem useful or intuitive to me.
> 

Agreed.

While reviewing this patch, I stumbled on some inconsistency on handling 
ptype *main vs p *main, which I filed here ( 
https://sourceware.org/bugzilla/show_bug.cgi?id=34700 ).

> This patch changes gdb to instead simply print the function pointer
> using the specified format.
> 

Reviewed-By: Tom de Vries <tdevries@suse.de>

Thanks,
- Tom

> 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 <A::nonvirt\\((void|)\\)>" \
>       "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
  

Patch

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 <A::nonvirt\\((void|)\\)>" \
     "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.  */