读书人

施用GDB 调试ATamp;T 汇编

发布时间: 2012-12-27 10:17:10 作者: rapoo

使用GDB 调试AT&T 汇编

注:以下内容为学习笔记,多数是从书本、资料中得来,只为加深印象,及日后参考。然而本人表达能力较差,写的不好。因非翻译、非转载,只好选原创,但多数乃摘抄,实为惭愧。但若能帮助一二访客,幸甚!


调试在编程中是不可缺少的,尝试使用GDB 调试一下昨晚的AT&T汇编代码:

liury@liury-laptop:~/program/asm/cpuid$ gdb cpuidGNU gdb (GDB) 7.1-ubuntuCopyright (C) 2010 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>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 "i486-linux-gnu".For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>...Reading symbols from /home/liury/program/asm/cpuid/cpuid...done.(gdb) l1# cpuid.s Sample program to extract the processor Vendor ID2.section .data3output:4.ascii "The processor Vendor ID is 'XXXXXXXXXXXX'\n"56.section .text7.global _start89_start:10nop(gdb) break *_start+1Breakpoint 1 at 0x8048075: file cpuid.s, line 11.(gdb) rStarting program: /home/liury/program/asm/cpuid/cpuid Breakpoint 1, _start () at cpuid.s:1111movl$0,%eax# The CPUID output option(the Vendor ID string)(gdb) next12cpuid(gdb) info registerseax            0x00ecx            0x00edx            0x00ebx            0x00esp            0xbffff0a00xbffff0a0ebp            0x00x0esi            0x00edi            0x00eip            0x804807a0x804807a <_start+6>eflags         0x212[ AF IF ]cs             0x73115ss             0x7b123ds             0x7b123es             0x7b123fs             0x00gs             0x00(gdb) n13movl$output,%edi(gdb) info registerseax            0xa10ecx            0x6c65746e1818588270edx            0x49656e691231384169ebx            0x756e65471970169159esp            0xbffff0a00xbffff0a0ebp            0x00x0esi            0x00edi            0x00eip            0x804807c0x804807c <_start+8>eflags         0x212[ AF IF ]cs             0x73115ss             0x7b123ds             0x7b123es             0x7b123fs             0x00gs             0x00(gdb) print /x $ecx$1 = 0x6c65746e(gdb) x /42cb &output0x80490ac <output>:84 'T'104 'h'101 'e'32 ' '112 'p'114 'r'111 'o'99 'c'0x80490b4 <output+8>:101 'e'115 's'115 's'111 'o'114 'r'32 ' '86 'V'101 'e'0x80490bc <output+16>:110 'n'100 'd'111 'o'114 'r'32 ' '73 'I'68 'D'32 ' '0x80490c4 <output+24>:105 'i'115 's'32 ' '39 '\''88 'X'88 'X'88 'X'88 'X'0x80490cc <output+32>:88 'X'88 'X'88 'X'88 'X'88 'X'88 'X'88 'X'88 'X'0x80490d4 <output+40>:39 '\''10 '\n'(gdb) cContinuing.The processor Vendor ID is 'GenuineIntel'Program exited normally.(gdb) q

注释:

break 加断点

run 运行

next 单步运行

info registers 显示所有寄存器的值

print 显示特定寄存器的值

print /d 显示十进制的值

print /t 显示二进制的值

print /x 显示十六进制的值


x 显示特定内存地址的内容

x /nyz

n是要显示的字段数,y是输出格式:

c 用于字符

d 十进制

x 十六进制
z是要显示的字段长度

b 字节8位

h 16位

w 32位字


q 退出gdb

2楼wangeen昨天 14:23
我觉得用GDB在调式C的时候有个直接打印某断地址的汇编代码的命令很有用ndisas 0x8048397 //Disassemble function around address 0x8048397
Re: guzhou_diaoke昨天 14:23
回复wangeenn我一般在对一些像func(i++, ++i) 之类的变态语句搞不懂他怎么弄才反汇编看一下。。。其他情况下调试C,一般用不到反汇编。。。当然主要原因是我汇编没学好,所以想好好学一下~
1楼wangeen前天 10:19
GDB在调试多线程程序的时候不太灵,总是莫名的freeze住了,不知道博主有没有感觉?
Re: guzhou_diaoke前天 10:21
回复wangeenn多线程是下一步的学习目标,目前只是了解

读书人网 >其他数据库

热点推荐