Bug 16242 - print format for hex float
Summary: print format for hex float
Status: REOPENED
Alias: None
Product: gdb
Classification: Unclassified
Component: cli (show other bugs)
Version: HEAD
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 15318 16687 20506 25983 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-11-25 18:49 UTC by Andreas Schwab
Modified: 2023-08-31 16:47 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Schwab 2013-11-25 18:49:45 UTC
There should be a print format for displaying float values in hex.  /x doesn't work, it converts the value to integer first.
Comment 1 Tom Tromey 2017-06-09 12:29:17 UTC
I think it would make sense to repurpose /x, because I don't see much
value in how that currently handling floating point.
Comment 2 Ruslan 2017-07-29 11:24:41 UTC
There is /z format, which for floating-point values appears to print raw data. The only problem is that this isn't documented. Instead, `help x` says "x(hex)" and "z(hex, zero padded on the left)", without mentioning that the former converts to integer while the latter prints true raw value.

I guess the fix for this would be to make the documentation correct and to add some tests checking that the documentation matches actual behavior.
Comment 3 Tom Tromey 2017-07-29 17:54:58 UTC
(In reply to Ruslan from comment #2)
> There is /z format, which for floating-point values appears to print raw
> data. The only problem is that this isn't documented. Instead, `help x` says
> "x(hex)" and "z(hex, zero padded on the left)", without mentioning that the
> former converts to integer while the latter prints true raw value.
> 
> I guess the fix for this would be to make the documentation correct and to
> add some tests checking that the documentation matches actual behavior.

I think that would be good to do, but hex float is not the same as
printing the raw bits in hex.
Comment 4 Ruslan 2017-07-29 18:50:11 UTC
Ah, I misunderstood this bug report.
Comment 5 Tom Tromey 2020-06-06 14:58:27 UTC
This has been filed a few times; I'm picking this one as the canonical
bug since it mentions the /z thing.
Comment 6 Tom Tromey 2020-06-06 14:58:52 UTC
*** Bug 25983 has been marked as a duplicate of this bug. ***
Comment 7 Tom Tromey 2020-06-06 14:59:04 UTC
*** Bug 15318 has been marked as a duplicate of this bug. ***
Comment 8 Tom Tromey 2020-06-06 14:59:49 UTC
*** Bug 16687 has been marked as a duplicate of this bug. ***
Comment 9 Andreas Schwab 2020-06-06 16:02:51 UTC
/z is really the same as /x, just with padding.
Comment 10 Tom Tromey 2020-06-07 16:35:47 UTC
This is a bit of a pain to implement, since according to gnulib
some host printf implementations don't support %a.
Comment 11 Ruslan 2020-06-07 17:32:36 UTC
Doesn't gnulib already provide an implementation of sprintf function family? Or do you mean gnulib's sprintf doesn't implement %a on some architectures?
Comment 12 Tom Tromey 2020-06-07 17:49:12 UTC
Actually I misread the gnulib docs -- it does fix this.
I didn't check to see if gdb already uses this module.
Comment 13 Tom Tromey 2022-02-17 18:49:12 UTC
I added sprintf-posix to gdb's gnulib list and re-imported,
and there weren't any changes.  So I suppose it was already
pulled in.

Docs are here btw:
https://www.gnu.org/software/gnulib/manual/html_node/sprintf.html

So I think we're ok using %a for ordinary things.
However there's also the question of target-float.c.
Comment 14 Tom Tromey 2022-02-17 18:57:42 UTC
(In reply to Tom Tromey from comment #13)
> I added sprintf-posix to gdb's gnulib list and re-imported,
> and there weren't any changes.  So I suppose it was already
> pulled in.

LOL I used the wrong tree.
This pulls in a bunch more stuff, so I wonder whether it's
appropriate at this stage.
Comment 15 Tom Tromey 2022-02-17 20:15:03 UTC
Actually I've come around to the idea that p/x should just
reinterpret the bits and print those exactly.
It's what the manual implies, for one thing.
Comment 17 Sourceware Commits 2022-03-10 20:37:05 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=56262a931b7ca8ee3ec9104bc7e9e0b40cf3d64e

commit 56262a931b7ca8ee3ec9104bc7e9e0b40cf3d64e
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
Comment 18 Tom Tromey 2022-03-10 20:38:49 UTC
Fixed.
Comment 19 Andreas Schwab 2022-03-10 20:49:52 UTC
That isn't hex float, though.
Comment 20 Joel Brobecker 2022-03-13 04:37:37 UTC
Since this ticket has been re-opened, I've removed the target-milestone, as this isn't a blocking change for GDB 12 release.
Comment 21 Nikolaos Chatzikonstantinou 2022-06-02 11:25:23 UTC
If this is of any help, if you add the following python script and source it,

    import gdb
    
    class Hexfloat (gdb.Function):
      """Converts a float to a hex representation."""
    
      def __init__ (self):
        super (Hexfloat, self).__init__ ("hexfloat")
    
      def invoke (self, x):
        return float.hex(float(x))
    
    Hexfloat()

you can get what you want (I think) with

    (gdb) p $hexfloat(123.4567)
    $1 = "0x1.edd3a92a30553p+6"
Comment 22 Tom Tromey 2023-08-31 16:47:59 UTC
*** Bug 20506 has been marked as a duplicate of this bug. ***