Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
To: Andrew Burgess <aburgess@redhat.com>
Cc: Tom Tromey <tromey@adacore.com>,
	Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH] Change how "print/x" displays floating-point value
Date: Fri, 18 Feb 2022 09:44:24 -0700	[thread overview]
Message-ID: <87k0dsqg47.fsf@tromey.com> (raw)
In-Reply-To: <874k4xdqwl.fsf@redhat.com> (Andrew Burgess's message of "Thu, 17 Feb 2022 23:17:30 +0000")

Andrew> Would it be possible to increase the testing to cover printing doubles
Andrew> as well as floats, and also to test /o and /t format?

Did it.

Andrew> FWIW, I think this change makes sense.  Though I wonder if it is worth
Andrew> having a NEWS entry as this might look like a bug to someone who is used
Andrew> to the old behaviour?

I had actually considered this, so I since you also asked, I think it's
a good idea.

Andrew> Also, maybe I'm just overly paranoid, but I wonder if it is worth really
Andrew> stressing the point in the manual (@node Output Format) that the value
Andrew> is reinterpreted, rather than cast.  Especially for /d /u /o, etc the
Andrew> manual is rather vague on how a float will be handled, e.g.:

I did this too.

Let me know what you think of this.

Tom

commit bb4117440a91bff97483eaab4d12543c9663de09
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Feb 17 13:43:59 2022 -0700

    Change how "print/x" displays floating-point value
    
    Currently, "print/x" will display a floating-point value by first
    casting it to an integer type.  This yields weird results like:
    
        (gdb) print/x 1.5
        $1 = 0x1
    
    This has confused users multiple times -- see PR gdb/16242, where
    there are several dups.  I've also seen some confusion from this
    internally at AdaCore.
    
    The manual says:
    
        'x'
             Regard the bits of the value as an integer, and print the integer
             in hexadecimal.
    
    ... which seems more useful.  So, perhaps what happened is that this
    was incorrectly implemented (or maybe correctly implemented and then
    regressed, as there don't seem to be any tests).
    
    This patch fixes the bug.
    
    There was a previous discussion where we agreed to preserve the old
    behavior:
    
        https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00314.html
    
    However, I think it makes more sense to follow the manual.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16242

diff --git a/gdb/NEWS b/gdb/NEWS
index 9da74e71796..544efa3fe74 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -97,6 +97,12 @@ show style disassembler enabled
 
 * Changed commands
 
+print
+  Printing of floating-point values with base-modifying formats like
+  /x has been changed to display the underlying bytes of the value in
+  the desired base.  This was GDB's documented behavior, but was never
+  implemented correctly.
+
 maint packet
   This command can now print a reply, if the reply includes
   non-printable characters.  Any non-printable characters are printed
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index a68cf31dcf3..88121c9d4c5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -10758,16 +10758,20 @@ Regard the bits of the value as an integer, and print the integer in
 hexadecimal.
 
 @item d
-Print as integer in signed decimal.
+Regard the bits of the value as an integer, and print the integer in
+decimal.
 
 @item u
-Print as integer in unsigned decimal.
+Regard the bits of the value as an integer, and print the integer in
+unsigned decimal.
 
 @item o
-Print as integer in octal.
+Regard the bits of the value as an integer, and print the integer in
+octal.
 
 @item t
-Print as integer in binary.  The letter @samp{t} stands for ``two''.
+Regard the bits of the value as an integer, and print the integer in
+binary.  The letter @samp{t} stands for ``two''.
 @footnote{@samp{b} cannot be used because these format letters are also
 used with the @code{x} command, where @samp{b} stands for ``byte'';
 see @ref{Memory,,Examining Memory}.}
@@ -10789,10 +10793,11 @@ The command @code{info symbol 0x54320} yields similar results.
 @xref{Symbols, info symbol}.
 
 @item c
-Regard as an integer and print it as a character constant.  This
-prints both the numerical value and its character representation.  The
-character representation is replaced with the octal escape @samp{\nnn}
-for characters outside the 7-bit @sc{ascii} range.
+Cast the value to an integer (unlike other formats, this does not just
+reinterpret the underlying bits) and print it as a character constant.
+This prints both the numerical value and its character representation.
+The character representation is replaced with the octal escape
+@samp{\nnn} for characters outside the 7-bit @sc{ascii} range.
 
 Without this format, @value{GDBN} displays @code{char},
 @w{@code{unsigned char}}, and @w{@code{signed char}} data as character
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 6f9be820b0c..30de1927d39 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -426,19 +426,14 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
       len = newlen;
     }
 
-  /* Historically gdb has printed floats by first casting them to a
-     long, and then printing the long.  PR cli/16242 suggests changing
-     this to using C-style hex float format.
-
-     Biased range types and sub-word scalar types must also be handled
+  /* Biased range types and sub-word scalar types must be handled
      here; the value is correctly computed by unpack_long.  */
   gdb::byte_vector converted_bytes;
   /* Some cases below will unpack the value again.  In the biased
      range case, we want to avoid this, so we store the unpacked value
      here for possible use later.  */
   gdb::optional<LONGEST> val_long;
-  if (((type->code () == TYPE_CODE_FLT
-	|| is_fixed_point_type (type))
+  if ((is_fixed_point_type (type)
        && (options->format == 'o'
 	   || options->format == 'x'
 	   || options->format == 't'
diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index 37632985a07..78a2147017d 100644
--- a/gdb/testsuite/gdb.base/printcmds.exp
+++ b/gdb/testsuite/gdb.base/printcmds.exp
@@ -158,8 +158,14 @@ proc test_float_rejected {} {
 # Regression test for PR gdb/21675
 proc test_radices {} {
     gdb_test "print/o 16777211" " = 077777773"
-    gdb_test "print/d 1.5" " = 1"
-    gdb_test "print/u 1.5" " = 1"
+
+    # See PR gdb/16242 for this.
+    gdb_test "print/d 1.5f" " = 1069547520"
+    gdb_test "print/u 1.5f" " = 1069547520"
+    gdb_test "print/x 1.5f" " = 0x3fc00000"
+    gdb_test "print/t 1.5f" " = 111111110000000000000000000000"
+    gdb_test "print/o 1.5f" " = 07760000000"
+    gdb_test "print/c 65.0f" " = 65 'A'"
 
     gdb_test "print/u (char) -1" " = 255"
     gdb_test "print/d (unsigned char) -1" " = -1"
diff --git a/gdb/testsuite/gdb.base/return-nodebug.exp b/gdb/testsuite/gdb.base/return-nodebug.exp
index 6fd41bee884..1cce09d2fc4 100644
--- a/gdb/testsuite/gdb.base/return-nodebug.exp
+++ b/gdb/testsuite/gdb.base/return-nodebug.exp
@@ -38,7 +38,12 @@ proc do_test {type} {
 		"advance to marker"
 
 	    # And if it returned the full width of the result.
-	    gdb_test "print /d t" " = -1" "full width of the returned result"
+	    if {$type == "float" || $type == "double"} {
+		set flag ""
+	    } else {
+		set flag "/d"
+	    }
+	    gdb_test "print $flag t" " = -1" "full width of the returned result"
 	}
     }
 }

  reply	other threads:[~2022-02-18 16:44 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-17 21:29 Tom Tromey via Gdb-patches
2022-02-17 23:17 ` Andrew Burgess via Gdb-patches
2022-02-18 16:44   ` Tom Tromey via Gdb-patches [this message]
2022-02-18 17:02     ` Eli Zaretskii via Gdb-patches
2022-03-10 17:27       ` Tom Tromey via Gdb-patches
2022-03-10 17:54         ` Eli Zaretskii via Gdb-patches
2022-03-10 19:19           ` Tom Tromey via Gdb-patches
2022-02-18 17:41     ` Andreas Schwab
2022-02-19 10:05       ` Andrew Burgess via Gdb-patches
2022-03-10 17:30         ` Tom Tromey via Gdb-patches
2022-03-10 17:30       ` Tom Tromey via Gdb-patches
2022-03-10 18:23         ` Andreas Schwab
2022-03-10 20:39           ` Tom Tromey via Gdb-patches
2022-03-10 20:53             ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k0dsqg47.fsf@tromey.com \
    --to=gdb-patches@sourceware.org \
    --cc=aburgess@redhat.com \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox