Linux 下調試彙編代碼

來源:互聯網
上載者:User

Linux 下調試彙編代碼既可以用 GDB、DDD 這類通用的調試器,也可以使用專門用來調試彙編代碼的 ALD(Assembly Language Debugger)。
從調試的角度來看,使用 GAS 的好處是可以在產生的目標代碼中包含符號表(symbol table),這樣就可以使用 GDB 和 DDD 來進行源碼級的調試了。要在產生的可執行程式中包含符號表,可以採用下面的方式進行編譯和連結:

[xiaowp@gary code]$ as --gstabs -o hello.o hello.s
[xiaowp@gary code]$ ld -o hello hello.o
 

執行 as 命令時帶上參數 --gstabs 可以告訴彙編器在產生的目標代碼中加上符號表,同時需要注意的是,在用 ld 命令進行連結時不要加上 -s 參數,否則目標代碼中的符號表在連結時將被刪去。
組譯工具員通常面對的都是一些比較苛刻的軟硬體環境,短小精悍的ALD可能更能符合實際的需要,因此下面主要介紹一下如何用ALD來調試組譯工具。首先在命令列方式下執行ald命令來啟動調試器,該命令的參數是將要被調試的可執行程式:

[xiaowp@gary doc]$ ald hello
Assembly Language Debugger 0.1.3Copyright (C) 2000-2002 Patrick Alken
hell ELF Intel 80386 (32 bit), LSB, Executable, Version 1 (current)
Loading debugging symbols...(15 symbols loaded)
ald>
 

當 ALD 的提示符出現之後,用 disassemble 命令對程式碼片段進行反組譯碼:

ald> disassemble -s .text
Disassembling section .text (0x08048074 - 0x08048096)
08048074 BA0F000000 mov edx, 0xf
08048079 B998900408 mov ecx, 0x8049098
0804807E BB01000000 mov ebx, 0x1
08048083 B804000000 mov eax, 0x4
08048088 CD80 int 0x80
0804808A BB00000000 mov ebx, 0x0
0804808F B801000000 mov eax, 0x1
08048094 CD80 int 0x80
 

上述輸出資訊的第一列是指令對應的地址碼,利用它可以設定在程式執行時的斷點:

ald> break 0x08048088
Breakpoint 1 set for 0x08048088
 

斷點設定好後,使用 run 命令開始執行程式。ALD 在遇到斷點時將自動暫停程式的運行,同時會顯示所有寄存器的當前值:

ald> run
Starting program: hello
Breakpoint 1 encountered at 0x08048088
eax = 0x00000004 ebx = 0x00000001 ecx = 0x08049098 edx = 0x0000000Fesp = 0xBFFFF6C0 ebp = 0x00000000 esi = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x08048088 eflags = 0x00000246
Flags: PF ZF IF
08048088 CD80 int 0x80
 

如果需要對彙編代碼進行單步調試,可以使用 next 命令:

ald> next
Hello, world!
eax = 0x0000000F ebx = 0x00000000 ecx = 0x08049098 edx = 0x0000000Fesp = 0xBFFFF6C0 ebp = 0x00000000 esi = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x0804808F eflags = 0x00000346
Flags: PF ZF TF IF
0804808F B801000000 mov eax, 0x1
 

若想獲得 ALD 支援的所有調試命令的詳細列表,可以使用 help 命令:

ald> help
Commands may be abbreviated.
If a blank command is entered, the last command is repeated.
Type `help <command>'' for more specific information on <command>.
General commands
attach clear continue detach disassemble
enter examine file help load
next quit register run set
step unload window write
Breakpoint related commands
break delete disable enable ignore
lbreak tbreak

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.