首先看一個最基本的nasm語法匯編程序hello.asm: section .text global main main: mov eax,4 ;4號調(diào)用 mov ebx,1 ;ebx送1表示stdout mov ecx,msg ;字符串的首地址送入ecx mov edx,14 ;字符串的長度送入edx int 80h ;輸出字串 mov eax,1 ;1號調(diào)用 int 80h ;結(jié)束 msg: db "Hello World!",0ah,0dh 我 們想調(diào)用gdb進(jìn)行調(diào)試,該怎么辦呢? 請看下面的示例: [root@localhost asm]# nasm -f elf hello.asm -g -F stabs [root@localhost asm]# gcc -o hello hello.o -g [root@localhost asm]# gdb hello <========= 啟動GDB GNU gdb Red Hat Linux (6.6-8.fc7rh) Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"... Using host libthread_db library "/lib/i686/nosegneg/libthread_db.so.1". (gdb) 說 明: 最關(guān)鍵的就是這一行nasm的編譯命令: [root@localhost asm]# nasm -f elf hello.asm -g -F stabs -f elf 用來指定輸出文件的格式 -F stabs 用來說明生成調(diào)試信息的格式,對于gcc來說都是用stabs, -g 主要是激活調(diào)試信息 好了,看看如何調(diào)試: (gdb) list 1 <================== l命令相當(dāng)于list,從第1行開始列出程序源代碼。 1 section .text 2 global main 3 4 main: 5 mov eax,4 ;4號調(diào)用 6 mov ebx,1 ;ebx送1表示stdout 7 mov ecx,msg ;字符串的首地址送入ecx 8 mov edx,14 ;字符串的長度送入edx 9 int 80h ;輸出字串 10 mov eax,1 ;1號調(diào)用 (gdb) break 5 <==================-- b 為 break 命令簡寫, 設(shè)置斷點,在源程序第5行處。 Breakpoint 1 at 0x8048380: file hello.asm, line 5. (gdb) break 6 Breakpoint 2 at 0x8048385: file hello.asm, line 6. (gdb) break *main <==================-- 設(shè)置斷點,在源程序標(biāo)號為“main”的地方設(shè)置斷點 Note: breakpoint 1 also set at pc 0x8048380. Breakpoint 3 at 0x8048380: file hello.asm, line 5. (gdb) info break <==================-- 查看斷點信息。 Num Type Disp Enb Address What 1 breakpoint keep y 0x08048380 hello.asm:5 2 breakpoint keep y 0x08048385 hello.asm:6 3 breakpoint keep y 0x08048380 hello.asm:5 (gdb) delete 1 <================== 刪除1號斷點 (gdb) info break Num Type Disp Enb Address What 2 breakpoint keep y 0x08048385 hello.asm:6 3 breakpoint keep y 0x08048380 hello.asm:5 (gdb) run <===================== 運行程序,run命令簡寫為r Starting program: /data/workspace/asm/hello Breakpoint 1, main () at hello.asm:5 5 mov eax,4 ;4號調(diào)用 (gdb) next <===================== 單步運行程序 6 mov ebx,1 ;ebx送1表示stdout (gdb) print $eax <================== 查看寄存器eax的值(十進(jìn)制) $1 = 4 (gdb) info registers <================== 查看所有寄存器的值 eax 0x4 4 ecx 0x29d1c08a 701612170 edx 0x1 1 ebx 0xd94ff4 14241780 esp 0xbfc0ab0c 0xbfc0ab0c ebp 0xbfc0ab68 0xbfc0ab68 esi 0x28bca0 2669728 edi 0x0 0 eip 0x8048385 0x8048385 <main+5> eflags 0x246 [ PF ZF IF ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb) n <===================== 單步運行程序 7 mov ecx,msg ;字符串的首地址送入ecx (gdb) n <===================== 單步運行程序 8 mov edx,14 ;字符串的長度送入edx (gdb) p/x $ecx <================== 查看所有寄存器的值(x表示結(jié)果以十六進(jìn)制顯示) $6 = 0x804839d (gdb) x/5cb 0x804839d <========= 查看內(nèi)存的值,5表示5個單元,c表示以字符形式顯示,b表示安裝字節(jié)讀取,那么就是顯示 0x804839d開始處的5個字節(jié)的內(nèi)容 0x804839d <msg>: 72 'H' 101 'e' 108 'l' 108 'l' 111 'o' (gdb) n 9 int 80h ;輸出字串 (gdb) n Hello 10 mov eax,1 ;1號調(diào)用 (gdb) n 11 int 80h ;結(jié)束 (gdb) n Program exited with code 01. (gdb) gdb 基本命令 gdb 支持很多的命令使你能實現(xiàn)不同的功能. 這些命令從簡單的文件裝入到允許你檢查所調(diào)用的堆棧內(nèi)容的復(fù)雜命令, 表27.1列出了你在用 gdb 調(diào)試時會用到的一些命令. 想了解 gdb 的詳細(xì)使用請參考 gdb 的指南頁. 命 令 描 述 file 裝入想要調(diào)試的可執(zhí)行文件. kill 終止正在調(diào)試的程序. list 列出產(chǎn)生執(zhí)行文件的源代碼的一部分. next 執(zhí)行一行源代碼但不進(jìn)入函數(shù)內(nèi)部. step 執(zhí)行一行源代碼而且進(jìn)入函數(shù)內(nèi)部. run 執(zhí)行當(dāng)前被調(diào)試的程序 quit 終止 gdb watch 使你能監(jiān)視一個變量的值而不管它何時被改變. break 在代碼里設(shè)置斷點, 這將使程序執(zhí)行到這里時被掛起. make 使你能不退出 gdb 就可以重新產(chǎn)生可執(zhí)行文件. shell 使你能不離開 gdb 就執(zhí)行 UNIX shell 命令. 還 可以用print命令顯示某個寄存器的值,例如:print $eax print/d $reg 以十進(jìn)制顯示reg的值 print/t $reg 二進(jìn)制 print/x $reg 十六進(jìn)制 x命令用來顯示特定內(nèi)存位置的值,格式如下: x/nyz n是要顯示的字段數(shù),y是輸出格式(c字符,x是16進(jìn)制,d是十進(jìn)制),z是字段長度(b字節(jié),h半字,w32位字) 例 如,x/42cb &output就是以字符模式顯示output變量前42字節(jié)的值。 |
原文地址 http://www.blogjava.net/jiajw0426/articles/298388.html