- 1.complile
GCC:
gcc -Wall -o2 -g file.cpp
MAKEFILE:
CFLAGS = -Wall -o2 -g
configure:
./configure CFLAGS="-Wall -o2 -g"
- 2.gdb filename [core file]
file core.29724
core.29724: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from './a.out'
- 3.command
set breakpoint : break -> b
b function-name
b line-number
b file-name:line-number
b file-name:function-name
b +/- offset
b *address
+/- offset :从当前位置的偏移行数
*address 代码段内的地址
example:
info break
Num Type Disp Enb Address What
2 breakpoint keep y 0x0000000000454112 in main(int, char**) at /home/lt/src/test/src/test.cpp:27
- 4.run -a
(gdb)run -a
- 5.backtrace -> bt
bt
bt N/-N
bt full
bt full N/-N
bt number
- 6.info register -> info reg
rax 0x7ffff102cf40 140737236881216
rbx 0x4940d0 4800720
rcx 0xb 11
rdx 0x7fffffffe248 140737488347720
rsi 0x1 1
rdi 0x7fffffffe10f 140737488347407
rbp 0x7fffffffe0c0 0x7fffffffe0c0
rsp 0x7fffffffd040 0x7fffffffd040
r8 0x0 0
r9 0x5241564e20524148 5927113478145982792
r10 0x4c42203252414843 5494989895845890115
r11 0x7ffff0d3e740 140737233807168
r12 0x40f840 4257856
r13 0x7fffffffe240 140737488347712
r14 0x0 0
r15 0x0 0
rip 0x453c98 0x453c98 <DALServiceMain::DALServiceMain(int, char**)+34>
eflags 0x202 [ IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
显示register内容,使用$号:
p $rax
p/格式
x 16进制
d 10
u 无符号十进制
o 8
t 2进制,two
a 地址
c ascii
f 浮点
s 字串
i 机器语言
x命令可显示内存的内容, x来源eXamining
程序 i=100
(gdb) x $pc
0x4009f6 <main()+8>: -57 '\307'
x/i 显示汇编指令
(gdb) x/i
0x4009fa <main()+12>: add %al,(%rax)
x/NFU ADDR ADDR希望显示的地址 N为次数 F为之前的格式,U表示单位:b 字节 h 半字(2字节) w字(4字节)g 双字(8字节)
从pc所指地址显示10条指令:
(gdb) x/10i $pc
=> 0x4009f6 <main()+8>: movl $0x64,-0x4(%rbp)
0x4009fd <main()+15>: mov $0x8,%esi
0x400a02 <main()+20>: mov $0x601080,%edi
0x400a07 <main()+25>: callq 0x400868 <_ZNSolsEm@plt>
0x400a0c <main()+30>: mov %rax,%rdi
0x400a0f <main()+33>: mov $0x400888,%esi
0x400a14 <main()+38>: callq 0x400878 <_ZNSolsEPFRSoS_E@plt>
0x400a19 <main()+43>: mov $0x4,%esi
0x400a1e <main()+48>: mov $0x400b64,%edi
0x400a23 <main()+53>: mov $0x0,%eax
6.反汇编命令
disassemble 简写 disas
disas
disas 程序计数器
disas 开始地址 结束地址
(gdb) disas
Dump of assembler code for function main():
0x00000000004009ee <+0>: push %rbp
0x00000000004009ef <+1>: mov %rsp,%rbp
0x00000000004009f2 <+4>: sub $0x10,%rsp
=> 0x00000000004009f6 <+8>: movl $0x64,-0x4(%rbp)
0x00000000004009fd <+15>: mov $0x8,%esi
0x0000000000400a02 <+20>: mov $0x601080,%edi
0x0000000000400a07 <+25>: callq 0x400868 <_ZNSolsEm@plt>
0x0000000000400a0c <+30>: mov %rax,%rdi
0x0000000000400a0f <+33>: mov $0x400888,%esi
0x0000000000400a14 <+38>: callq 0x400878 <_ZNSolsEPFRSoS_E@plt>
0x0000000000400a19 <+43>: mov $0x4,%esi
0x0000000000400a1e <+48>: mov $0x400b64,%edi
0x0000000000400a23 <+53>: mov $0x0,%eax
0x0000000000400a28 <+58>: callq 0x4007f8 <printf@plt>
0x0000000000400a2d <+63>: mov $0x0,%eax
0x0000000000400a32 <+68>: leaveq
0x0000000000400a33 <+69>: retq
End of assembler dump.