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

时间:2015-05-02 22:35:09

标签: floating-point gdb

当我要求GDB打印真实的二进制文件时,我得到了这个:

(gdb) p/t 5210887.5
$1 = 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
$1 = 5210887.5
(gdb) p/t f
$2 = 10011111000001100000111

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

1 个答案:

答案 0 :(得分:3)

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

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

这由gdb&#39; print_scalar_formatted中的代码支持:

if (options->format != 'f')
    val_long = unpack_long (type, valaddr);

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

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

(gdb) p f
$2 = 5210887.5
(gdb) p/t *(int32_t *)&f
$3 = 1001010100111110000011000001111
(gdb) p/t {int32_t}&f
$4 = 1001010100111110000011000001111

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

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

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