[20220314]利用vim下使用gdb進行各種進位制轉換外掛.txt

lfree發表於2022-03-16

[20220314]利用vim下使用gdb進行各種進位制轉換外掛.txt

--//前一段時間學習使用gdb進行各種進位制轉換,當時遇到有時候16進位制與10進位制一起運算的問題,當時我使用tpt的calc指令碼來解決這個
--//問題來解決。當然bc以及bash也可以就是比較麻煩,比如。

$ echo $((0xa+10))
20

--//有時候我還給使用bccalc_linux外掛轉換為16進位制,我後面發現gdb比較簡單,不過我發現我的測試環境不支援gdb+管道,centos
--//7.8以上版本可以。嘗試自己寫一個外掛完成這類換行操作。

--//參考連結 http://blog.itpub.net/267265/viewspace-2864628/ =>[20220304]使用gdb完成各種進位制轉換.txt

(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".

--//在bash下執行,因為不支援管道,寫成如下:
$ echo -e 'p /x 1212\nquit\n' >| /tmp/gdb.txt ;  gdb -q  -x /tmp/gdb.txt| awk '{print $NF}'
0x4bc

$ echo p/x 1212 >| /tmp/gdb.txt ; echo quit >> /tmp/gdb.txt ;gdb -q  -x /tmp/gdb.txt| awk '{print $NF}'
0x4bc

$ cat gdb.vim
noremap <Leader>px Yp!!sed -e 's+^+p /x +' -e 's+$+\nquit\n+' >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk '{print $NF}'<CR>kA = <ESC>J
noremap <Leader>pd Yp!!sed -e 's+^+p /d +' -e 's+$+\nquit\n+' >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk '{print $NF}'<CR>kA = <ESC>J
noremap <Leader>po Yp!!sed -e 's+^+p /o +' -e 's+$+\nquit\n+' >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk '{print $NF}'<CR>kA = <ESC>J
noremap <Leader>pt Yp!!sed -e 's+^+p /t +' -e 's+$+\nquit\n+' >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk '{print $NF}'<CR>kA = <ESC>J

"" noremap <Leader>px Yp!!sed -e 's+^+p /x +' -e 's+$+\nquit\n+' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
"" noremap <Leader>pd Yp!!sed -e 's+^+p /d +' -e 's+$+\nquit\n+' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
"" noremap <Leader>po Yp!!sed -e 's+^+p /o +' -e 's+$+\nquit\n+' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
"" noremap <Leader>pt Yp!!sed -e 's+^+p /t +' -e 's+$+\nquit\n+' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J

--//如果你gdb支援管道,註解上面部分,取消下面的註解。
--//臨時呼叫可以執行:source ~/gdb.vim

--//還可以進行簡單混合進位制的計算例子:
12 * 0xa
--//打入\pd,顯示如下:
12 * 0xa  = 120

--//還不完善,有時間完善善看看。主要看中gdb能做一些特殊運算。

BBED> set dba 4,135
        DBA             0x01000087 (16777351 4,135)

16777351 & 0x3ffff = 135
16777351 >> 22 = 4

16777351 & 0x3ffff
16777351 & 0x3ffff

--//當然在windows下就不行了,因為沒有安裝gdb,我嘗試在Cygwin64 Terminal下安裝gdb。
"" windows
noremap <Leader>px Yp!!sed -e "s+^+p /x +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J
noremap <Leader>pd Yp!!sed -e "s+^+p /d +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J
noremap <Leader>po Yp!!sed -e "s+^+p /o +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J
noremap <Leader>pt Yp!!sed -e "s+^+p /t +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J

--//注意,環境變數Path加入cygwin的安裝路徑。D:\tools\cygwin64\bin;另外awk在cygwin是連結,它沒有字尾.exe.
--//我直接使用gawk。如果想使用awk修改如下:
$ ln gawk.exe awk.exe

$ ls -l awk*
lrwxrwxrwx 1 Administrator None      8 Mar 14 15:36 awk -> gawk.exe
-rwxr-xr-x 3 Administrator None 641024 Oct 30 04:46 awk.exe
--//注我的測試軟連線不行。

--//最終我修改整理如下:
$ cat gdb.vim
"" gdb Low version for linux
noremap <Leader>px Yp!!sed -e 's+^+p /x +'  >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
noremap <Leader>pd Yp!!sed -e 's+^+p /d +'  >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
noremap <Leader>po Yp!!sed -e 's+^+p /o +'  >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
noremap <Leader>pt Yp!!sed -e 's+^+p /t +'  >\| /tmp/gdb.txt && gdb -q  -x /tmp/gdb.txt \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J

"" gdb high version for linux
"" noremap <Leader>px Yp!!sed -e 's+^+p /x +' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
"" noremap <Leader>pd Yp!!sed -e 's+^+p /d +' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
"" noremap <Leader>po Yp!!sed -e 's+^+p /o +' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J
"" noremap <Leader>pt Yp!!sed -e 's+^+p /t +' \| gdb -q \| awk 'NR==1{print $NF}'<CR>kA = <ESC>J

"" gdb for windwows, you must be cygwin + gdb.
""noremap <Leader>px Yp!!sed -e "s+^+p /x +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J
""noremap <Leader>pd Yp!!sed -e "s+^+p /d +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J
""noremap <Leader>po Yp!!sed -e "s+^+p /o +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J
""noremap <Leader>pt Yp!!sed -e "s+^+p /t +" \| gdb -q \| gawk "NR==1{print $NF}"<CR>kA = <ESC>J

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

相關文章