天天看點

使用 gdb 對程式進行彙編級調試

開始/重新開始

( gdb) r ( run )

下斷點

(gdb) b *0x0804ce2b

b 表示 break

單步步過

(gdb) ni (next instruction)

單步步入

(gdb) si ( step instruction )

繼續執行

( gdb )c

執行到傳回

(gdb) finish

disas

反彙編一段指令。可以帶零個、一個或兩個參數。第一個參數是反彙編開始位址,第二個參數是反彙編結束位址。

如果沒有參數,則反彙編目前的函數。

記憶體讀/寫斷點

watch (int )0x8049aa4

在 0x8049aa4 處下寫斷點,斷點的範圍為 4個位元組。

gdb支援更大範圍的記憶體寫斷點。但在彙編條件下,我還沒有找到設定更大記憶體寫斷點的方法。

rwatch ,awatch 用法同 watch ,分别表示讀斷點和 讀寫斷點。

讀寫斷點依賴于 gcc可用的硬體高度寄存器。

寄存器視窗

(gdb) display

設定要觀察的變量的内容。這些變量的值在程式每次被斷下來後顯示在螢幕上。

例如,我們經常要關注 eax, ebx, ecx,edx的内容,則用 display 設定他們為觀察變量。

(gdb) display /x $eax

(gdb) display /x $ebx

(gdb) display /x $ecx

(gdb) display /x $edx

(gdb) until

相當于 od 的 f4

特色功能:

1、設定反彙編代碼使用的指令集

(gdb) set disas intel

設定反彙編代碼使用的指令集,可選擇 intel 指令集或 AT&T指令集.

該指令隻能用于x86平台。

捕獲 “段錯誤”的信号

(gdb) handle SIGSEGV

抛出異常時捕獲

(gdb) catch throw

檢視棧幀。

(gdb) where

強制傳回

(gdb) return

程式直接從目前行跳轉到 return 處。如果函數有傳回值,則加在 return 指令之後。例如, return 1。跳轉中,棧平衡是自動維護的。修改程式代碼段:

By default, GDB opens the le containing your program’s executable code (or the core le) read-only. This prevents accidental alterations to machine code; but it also prevents you from intentionally patching your program’s binary. If you’d like to be able to patch the binary, you can specify that explicitly with the set write command. For example, you might want to turn on internal debugging ags, or even

to make emergency repairs.

set write on

exec-file

The dump and append commands write data to a file, and the restore command reads data from a file back into the inferior’s memory.

寫記憶體:

To store values into arbitrary places in memory, use the `{…}’ construct to generate a value of speci ed type at a speci ed address (see Section 8.1 [Expressions], page 63). For example, {int}0x83040 refers to memory location 0x83040 as an integer (which implies a certain size and representation in memory), and set {int}0x83040 = 4

stores the value 4 into that memory

參考資料:

by Richard Stallman, Roland Pesch etc, Published by the Free Software Foundation

繼續閱讀