[20220304]使用gdb完成各種進位制轉換.txt

lfree發表於2022-03-04

[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 <
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:
<

# 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 <
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:
<

$ 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命令計算器更加靈活。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2864628/,如需轉載,請註明出處,否則將追究法律責任。

相關文章