GDB调试实践
调试实例
huhan@HuHan ~/cpp/coredump ls
test.cpp
huhan@HuHan ~/cpp/coredump
huhan@HuHan ~/cpp/coredump g++ -o test test.cpp -g
huhan@HuHan ~/cpp/coredump ulimit
unlimited
huhan@HuHan ~/cpp/coredump ulimit- c unlimited
zsh: command not found: ulimit-
✘ huhan@HuHan ~/cpp/coredump ulimit -c unlimied
ulimit: invalid number: unlimied
✘ huhan@HuHan ~/cpp/coredump ulimit -c unlimited
huhan@HuHan ~/cpp/coredump
huhan@HuHan ~/cpp/coredump g++ -o test test.cpp -g
huhan@HuHan ~/cpp/coredump ulimit -c unlimited
huhan@HuHan ~/cpp/coredump ls
test test.cpp
huhan@HuHan ~/cpp/coredump ./test
10
[1] 290 segmentation fault (core dumped) ./test
✘ huhan@HuHan ~/cpp/coredump gdb test core
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
[New LWP 290]
Core was generated by `./test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00005586369c71f9 in func (p=0x0) at test.cpp:6
6 int y = *p;
(gdb) where
#0 0x00005586369c71f9 in func (p=0x0) at test.cpp:6
#1 0x00005586369c7296 in main () at test.cpp:19
(gdb) l
1 #include <iostream>
2 using namespace std;
3
4 int func(int *p)
5 {
6 int y = *p;
7 return y;
8 }
9
10 int add(int const& a, int const& b)
(gdb) l
11 {
12 return a + b;
13 }
14
15 int main()
16 {
17 cout<< add(4, 6) <<endl;
18 int *p = NULL;
19 return func(p);
20 }
(gdb) b 5
Breakpoint 1 at 0x5586369c71e9: file test.cpp, line 5.
(gdb) b 13
Breakpoint 2 at 0x5586369c7221: file test.cpp, line 13.
(gdb) b 17
Breakpoint 3 at 0x5586369c723e: file test.cpp, line 17.
(gdb) b main
Breakpoint 4 at 0x5586369c7223: file test.cpp, line 16.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00005586369c71e9 in func(int*) at test.cpp:5
2 breakpoint keep y 0x00005586369c7221 in add(int const&, int const&) at test.cpp:13
3 breakpoint keep y 0x00005586369c723e in main() at test.cpp:17
4 breakpoint keep y 0x00005586369c7223 in main() at test.cpp:16
(gdb) r
Starting program: /home/huhan/cpp/coredump/test
Breakpoint 4, main () at test.cpp:16
16 {
(gdb) s
Breakpoint 3, main () at test.cpp:17
17 cout<< add(4, 6) <<endl;
(gdb) s
add (a=@0x55555555537d: 29590344, b=<error reading variable>) at test.cpp:11
11 {
(gdb) s
12 return a + b;
(gdb) p a
$1 = (const int &) @0x7fffffffdae8: 4
(gdb) n
Breakpoint 2, add (a=@0x7fffffffdae8: 4, b=@0x7fffffffdaec: 6) at test.cpp:13
13 }
(gdb) n
10
main () at test.cpp:18
18 int *p = NULL;
(gdb) n
19 return func(p);
(gdb) disassemble
Dump of assembler code for function main():
0x0000555555555223 <+0>: endbr64
0x0000555555555227 <+4>: push %rbp
0x0000555555555228 <+5>: mov %rsp,%rbp
0x000055555555522b <+8>: sub $0x20,%rsp
0x000055555555522f <+12>: mov %fs:0x28,%rax
0x0000555555555238 <+21>: mov %rax,-0x8(%rbp)
0x000055555555523c <+25>: xor %eax,%eax
0x000055555555523e <+27>: movl $0x6,-0x14(%rbp)
0x0000555555555245 <+34>: movl $0x4,-0x18(%rbp)
0x000055555555524c <+41>: lea -0x14(%rbp),%rdx
0x0000555555555250 <+45>: lea -0x18(%rbp),%rax
0x0000555555555254 <+49>: mov %rdx,%rsi
0x0000555555555257 <+52>: mov %rax,%rdi
0x000055555555525a <+55>: callq 0x555555555203 <add(int const&, int const&)>
0x000055555555525f <+60>: mov %eax,%esi
0x0000555555555261 <+62>: lea 0x2dd8(%rip),%rdi # 0x555555558040 <_ZSt4cout@@GLIBCXX_3.4>
0x0000555555555268 <+69>: callq 0x5555555550e0 <_ZNSolsEi@plt>
0x000055555555526d <+74>: mov %rax,%rdx
0x0000555555555270 <+77>: mov 0x2d59(%rip),%rax # 0x555555557fd0
0x0000555555555277 <+84>: mov %rax,%rsi
0x000055555555527a <+87>: mov %rdx,%rdi
0x000055555555527d <+90>: callq 0x5555555550b0 <_ZNSolsEPFRSoS_E@plt>
0x0000555555555282 <+95>: movq $0x0,-0x10(%rbp)
=> 0x000055555555528a <+103>: mov -0x10(%rbp),%rax
0x000055555555528e <+107>: mov %rax,%rdi
0x0000555555555291 <+110>: callq 0x5555555551e9 <func(int*)>
0x0000555555555296 <+115>: nop
0x0000555555555297 <+116>: mov -0x8(%rbp),%rcx
0x000055555555529b <+120>: xor %fs:0x28,%rcx
0x00005555555552a4 <+129>: je 0x5555555552b9 <main()+150>
0x00005555555552a6 <+131>: jmp 0x5555555552b4 <main()+145>
0x00005555555552a8 <+133>: endbr64
0x00005555555552ac <+137>: mov %rax,%rdi
0x00005555555552af <+140>: callq 0x5555555550f0 <_Unwind_Resume@plt>
0x00005555555552b4 <+145>: callq 0x5555555550c0 <__stack_chk_fail@plt>
0x00005555555552b9 <+150>: leaveq
0x00005555555552ba <+151>: retq
End of assembler dump.
(gdb) s
Breakpoint 1, func (p=0x555555555330 <__libc_csu_init>) at test.cpp:5
5 {
(gdb) s
6 int y = *p;
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x00005555555551f9 in func (p=0x0) at test.cpp:6
6 int y = *p;
(gdb) q
A debugging session is active.
Inferior 1 [process 299] will be killed.
Quit anyway? (y or n) n
Not confirmed.
(gdb) info frame
Stack level 0, frame at 0x7fffffffdae0:
rip = 0x5555555551f9 in func (test.cpp:6); saved rip = 0x555555555296
called by frame at 0x7fffffffdb10
source language c++.
Arglist at 0x7fffffffdac8, args: p=0x0
Locals at 0x7fffffffdac8, Previous frame's sp is 0x7fffffffdae0
Saved registers:
rbp at 0x7fffffffdad0, rip at 0x7fffffffdad8
(gdb) n
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) q
huhan@HuHan ~/cpp/coredump
命令速查
启动 GDB
gdb object
gdb object core
gdb object pid
gdb
gdb -tui
断点
break main
break 101
break basic.c:101
break basic.c:foo
break *0x00400448
info breakpoints
delete 1
delete
clear
clear function
clear line
clear basic.c:101
clear basic.c:main
clear *0x00400448
disable 2
enable 2
rbreak {regexpr}
tbreak function|line
hbreak function|line
ignore {id} {count}
condition {id} {expr}
condition 2 i == 20
watch {expr}
info watchpoints
catch exec
运行程序
run
run {args}
run < file
run < <(cmd)
run <<< $(cmd)
set args {args} ...
show args
cont
step
step {count}
next
next {count}
CTRL+C
attach {process-id}
detach
finish
until
until {line}
kill
栈帧
bt
frame
up
down
info locals
info args
代码浏览
list 101
list 1,10
list main
list basic.c:main
list -
list *0x22e4
cd dir
pwd
search {regexpr}
reverse-search {regexp}
dir {dirname}
dir
show directories
浏览数据
print {expression}
print /x {expression}
print array[i]@count
print $
print *$->next
print $1
print ::gx
print 'basic.c'::gx
print /x &main
x *0x11223344
x /nfu {address}
x /10xb *0x11223344
x/x &gx
x/4wx &main
x/gf &gd1
help x
info locals
info functions {regexp}
info variables {regexp}
ptype name
whatis {expression}
set var = {expression}
display {expression}
undisplay
info display
show values
info history
目标文件操作
file {object}
file
symbol-file {object}
exec-file {object}
core-file {core}
信号控制
info signals
handle {signo} {actions}
handle INT print
handle INT noprint
handle INT stop
handle INT nostop
handle INT pass
handle INT nopass
signal signo
signal 0
线程调试
info threads
thread {id}
break {line} thread all
thread apply {id..} cmd
thread apply all cmd
set schedule-locking ?
set non-stop on/off
set pagination on/off
set target-async on/off
进程调试
info inferiors
inferior {id}
kill inferior {id...}
set detach-on-fork on/off
set follow-fork-mode parent/child
汇编调试
info registers
info all-registers
print/x $pc
stepi
nexti
display/i $pc
x/x &gx
info line 22
info line *0x2c4e
disassemble {addr}
历史信息
show commands
info editing
ESC-CTRL-J
set history expansion on
break class::member
list class:member
ptype class
print *this
其他命令
define command ... end
<return>
shell {command} [args]
source {file}
quit
参考资料
GDB命令 | ZDZN 文档教程
GDB 调试指南
如何通过gdb查看反汇编代码