1、调试hello,要求编译时指定了-gstabs选项
2、运行hello
deepfuture-lx@deepfuture-lx-desktop:~/private/mytest$ ./hello
hello,world
ABCD
3、用gdb打开hello
deepfuture-lx@deepfuture-lx-desktop:~/private/mytest$ gdb hello
GNU gdb (GDB) 7.1-ubuntu
Copyright (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 "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/deepfuture-lx/private/mytest/hello...done.
4、列出源代码
(gdb) list
warning: Source file is more recent than executable.
1.section .data#初始化的变量
2output:
3 .ascii "hello,world\n"
4 #要打印的字符串,.data为初始化值的变量。output是标签,指示字符串开始的位置,ascii为数据类型
5.section .bss#未初始化的变量,由0填充的缓冲区
6 .lcomm num,20
7 #lcomm为本地内存区域,即本地汇编外的不能进行访问。.comm是通用内存区域。
8.section .text#汇编语言指令码
9 .globl _start#启动入口
10 _start:
(gdb) list
11 movl $4,%eax#调用的系统功能,4为write
12 movl $output,%ecx#要打印的字符串
13 movl $1,%ebx#文件描述符,屏幕为1
14 movl $12,%edx#字符串长度
15 int $0x80#显示字符串hello,world
16
17 movl $0,%eax
18 movl $num,%edi
19 movl $65,1(%edi)#A 的ascii
20 movl $66,2(%edi)#B 的ascii
5、设置断点
(gdb) break 17
Breakpoint 1 at 0x4000c6: file hello.s, line 17.
6、运行至断点
(gdb) run
Starting program: /home/deepfuture-lx/private/mytest/hello
hello,world
Breakpoint 1, _start () at hello.s:17
7、运行下条语句
17 movl $0,%eax
(gdb) next
18 movl $num,%edi
8、显示所有寄存器的值
(gdb) info registers
rax 0x00
rbx 0x11
rcx 0x60011c6291740
rdx 0xc12
rsi 0x00
rdi 0x00
rbp 0x00x0
rsp 0x7fffffffe2d00x7fffffffe2d0
r8 0x00
r9 0x00
r10 0x00
r11 0x00
r12 0x00
r13 0x00
r14 0x00
r15 0x00
rip 0x4000cb0x4000cb <_start+27>
eflags 0x202[ IF ]
cs 0x3351
ss 0x2b43
ds 0x00
es 0x00
fs 0x00
---Type <return> to continue, or q <return> to quit---
gs 0x00
(gdb) next
19 movl $65,1(%edi)#A 的ascii
9、按十六进制格式输出edi寄存器的值。/x表示16进制,/d表示10进制,/t表示二进制
(gdb) print/x $rdi
$3 = 0x600128
10、显示所有寄存器值
(gdb) info registers
rax 0x00
rbx 0x11
rcx 0x60011c6291740
rdx 0xc12
rsi 0x00
rdi 0x6001286291752
rbp 0x00x0
rsp 0x7fffffffe2d00x7fffffffe2d0
r8 0x00
r9 0x00
r10 0x00
r11 0x00
r12 0x00
r13 0x00
r14 0x00
r15 0x00
rip 0x4000d00x4000d0 <_start+32>
eflags 0x202[ IF ]
cs 0x3351
ss 0x2b43
ds 0x00
es 0x00
fs 0x00
---Type <return> to continue, or q <return> to quit---
gs 0x00
(gdb) next
20 movl $66,2(%edi)#B 的ascii