[20220304]使用gdb完成各种进制转换.txt

编程

[20220304]使用gdb完成各种进制转换.txt

--//一般使用gdb调试跟踪程序,centos 7以上版本gdb支持管道,可以使用gdb p命令实现10,16进制转换,通过一些例子说明:

# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

# gdb -v
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.

# echo p/x 250 | gdb -q
(gdb) $1 = 0xfa
(gdb) quit

# echo p/d 0xfa | gdb -q
(gdb) $1 = 250
(gdb) quit

--//缺点就是输出一些不必要的信息。也可以使用实现一些位移位或者位与操作。

--//比如bbed下:
BBED> set dba 4,9567
        DBA             0x0100255f (16786783 4,9567)

--//如果知道0x0100255f或者16786783 如何转换成4,9567呢 ,oracle将32位的后22为作为块号,前面10位作为文件号。

# echo "p/d 0x0100255f >> 22" | gdb -q
(gdb) $1 = 4
(gdb) quit

# echo "p/d 0x0100255f & 0x3ffff " | gdb -q
(gdb) $1 = 9567
(gdb) quit

--//注意字串里面出现>>,& , 必须使用引号引起来。
--//当然如果在命令行里面使用基本没有问题,另外计算也不成问题,例子:
(gdb) p /x 5+0xA
$12 = 0xf
(gdb) p /d 5+0xA
$13 = 15
(gdb) p /o 5+0xA
$14 = 017

(gdb) p /d 5 * 0xa
$16 = 50
(gdb) p /d 5 * 10
$17 = 50

(gdb) p /d 100 /3
$30 = 33

(gdb) p /x 100 /3
$32 = 0x21

(gdb) p /d 2 << 6
$2 = 128
--//注意这个相当于2^7.

(gdb) p /d 1 << 6
$9 = 64

--//重要的是它可以混合各种进制在一起计算。

--//新的版本还支持使用2,8进制。
(gdb) p /d 0b111
$1 = 7

(gdb) p /d 077
$2 = 63
--//注小心前面的0表示8进制。

(gdb) p /d 0xf0  ^ 0x0f
$12 = 255
--// ^ 表示位异或
(gdb) p /d 0xf0  & 0x0f
$13 = 0
--// & 表示位与

(gdb) p /x 0xf0 | 0x0f
$9 = 0xff
--// | 表示位或

(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char) and s(string).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.

Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".

--//可以执行以上各种格式。

(gdb) p /t 7
$6 = 111
--//t 表示2进制。

(gdb) p /c 0x41
$7 = 65 "A"

(gdb) p /o 63
$10 = 077

--//顺便在oracle 5.9 版本上测试看看。
$ cat /etc/issue.net
Oracle Linux Server release 5.9

$ gdb -v
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-45.el5)
Copyright (C) 2009 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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.

$ echo p/d 0x3820  | gdb -q
(gdb) Hangup detected on fd 0
error detected on stdin

--//可以发现这个版本不支持管道。

$ gdb -q
(gdb) p/x 250
$1 = 0xfa

(gdb) p/d 0xfa
$2 = 250

--//DBA             0x0100255f (16786783 4,9567)
(gdb) p/d 0x0100255f >> 22
$3 = 4

(gdb) p/d 0x0100255f & 0x3ffff
$4 = 9567

(gdb) p /t 0xf
$6 = 1111

(gdb) p /d 12 + 0xa
$7 = 22

(gdb) p /d 011
$9 = 9

(gdb) p /d 0b111
Invalid number "0b111".
--//不支持2进制转换。
--//可以通过以上测试发现比bc命令计算器更加灵活。

以上是 [20220304]使用gdb完成各种进制转换.txt 的全部内容, 来源链接: utcz.com/z/520256.html

回到顶部