在 GDB 中显示 IEEE 754 浮点表示?

Displaying IEEE 754 floating point representation in GDB?

当我要求 GDB 打印二进制实数时,我得到了这个:

(gdb) p/t 5210887.5
 = 10011111000001100000111

根据this,

0 10010101 00111110000011000001111

是期望值。

对齐它们,

         1 0011111000001100000111
0 10010101 00111110000011000001111

看起来 GDB 在四舍五入后给我整数表示。事情是它也在用一个变量来做这件事。在 C 程序中使用声明 -

main()
{
  float f = 5210887.5;
}

并调试它 -

$ gcc -g -O0 floatdec.c -o floatdec
$ gdb floatdec
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ /floatdec...done.
(gdb) b main
Breakpoint 1 at 0x804839a: file floatdec.c, line 3.
(gdb) r
Starting program: /home/ /floatdec 

Breakpoint 1, main () at floatdec.c:3
3         float f = 5210887.5;
(gdb) s
4       }
(gdb) p f
 = 5210887.5
(gdb) p/t f
 = 10011111000001100000111

同样的事情,它向我展示了整数表示。有没有办法让GDB给我显示浮点格式?

t 格式确实将值转换为整数。来自 gdb: Output Formats:

t
   Print as integer in binary. The letter ‘t’ stands for “two”.

gdb print_scalar_formatted:

中的代码支持这一点
if (options->format != 'f')
    val_long = unpack_long (type, valaddr);

其中 unpack_long 将各种类型的值(包括 float)转换为 long。

解决方法是获取变量的地址,将其转换为 (int32 *),然后取消引用。

(gdb) p f
 = 5210887.5
(gdb) p/t *(int32_t *)&f
 = 1001010100111110000011000001111
(gdb) p/t {int32_t}&f
 = 1001010100111110000011000001111

或者,在与语言无关的级别,使用 x 命令:

(gdb) x/tw &f
0xbffff3f4: 01001010100111110000011000001111

这是在 x86 上完成的。其他字节序系统可能会产生不同的位模式。