官链:http://www.sourceware.org/gdb/
GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
Those programs might be executing on the same machine as GDB (native), on another machine (remote), or on a simulator. GDB can run on most popular UNIX and Microsoft Windows variants, as well as on Mac OS X.
简单点说:GDB是一个跨平台的调试器工具,可以看到你程序运行时的过程,主要对于unix/Linux环境开发程序员调试工具,如c/c++等
开始测试环境——安装GDB
[root@spark03 demo]# yum install -y gdb
[root@spark03 demo]# gdb --version
1、编写一个c程序进行调试
[root@spark03 demo]# cat test.c
#include
int main(){
int arr[4] = {1,2,3,4};
int i = 0;
for(i=0; i<4; i++){
printf("%d\n",arr[i]);
}
return 0;
}
[root@spark03 demo]# gcc -g test.c
[root@spark03 demo]# ls
a.out test.c
[root@spark03 demo]# gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /demo/a.out...(no debugging symbols found)...done.
(gdb) run
Starting program: /demo/a.out
1
2
3
4
[Inferior 1 (process 2242) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64
(gdb) r
Starting program: /demo/a.out
2、gdb基础命令(括号为简写):
run (r) ——程序运行
quit (q) ——退出gdb调试
list —— 查看源代码
break (b) —— 打断点
b main — 函数地方,函数名字
b 6 — 在第六行打断点
info b —— 查看断点记录
next(n) —— 继续调试下一行
print(p) —— 打印变量
step(s) —— 进去某个具体的函数调试(函数调用的时候)
a. shell 调终端命令
(gdb) shell date
2023年 04月 07日 星期五 02:58:21 CST
b. 调试的日志功能——set logging on
由于实际环境中代码过多,调试步骤复杂,所以可以开启作为调试记录的日志,默认记录到gdb.txt文件
Breakpoint 1 at 0x400535: file test.c, line 3.
(gdb) b 7
Breakpoint 2 at 0x400561: file test.c, line 7.
(gdb) set logging on
Copying output to gdb.txt.
(gdb) r
Starting program: /demo/a.out
(gdb) shell ls
a.out gdb.txt test.c
(gdb)
c. watchpoint
对于变量,可以观察变量变化情况,新旧值对比
info 可以查看watchpoint
1、2开启coredump之前,需要先开启资源限制,core为0则不开启,需要设置为 unlimited
[root@spark03 demo]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 63358
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 63358
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@spark03 demo]# ulimit -c unlimited
[root@spark03 demo]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 63358
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 63358
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@spark03 demo]#
2、测试代码,空指针赋值
[root@spark03 demo]# cat err.c
#include
int main(){
int* tmp = NULL;
*tmp = 10;
return 0;
}
[root@spark03 demo]# gcc -g err.c
[root@spark03 demo]# ./a.out
段错误(吐核)
[root@spark03 demo]# ll
总用量 168
-rwxr-xr-x. 1 root root 9504 4月 7 03:16 a.out
-rw-------. 1 root root 245760 4月 7 03:17 core.3218
-rw-r--r--. 1 root root 76 4月 7 03:13 err.c
-rw-r--r--. 1 root root 651 4月 7 03:00 gdb.txt
-rw-r--r--. 1 root root 130 4月 7 02:00 test.c
此时产生了core文件:core.3218,调试方式:gdb+二进制文件+coredump
[root@spark03 demo]# gdb a.out core.3218
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /demo/a.out...done.
[New LWP 3218]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0 0x00000000004004fd in main () at err.c:6
6 *tmp = 10;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64
1、搭建运行环境
[root@spark03 demo]# cat run.c
#include
// 循环调用
void for1(){
}
void for2(){
int i = 0;
i++;
}
int main(){
for(;;){
for1();
for2();
}
return 0;
}
[root@spark03 demo]# gcc -g run.c
[root@spark03 demo]# ./a.out &
[1] 3712
[root@spark03 demo]# ps -ef |grep 3712
root 3712 1906 95 03:30 pts/0 00:00:07 ./a.out
root 3722 1906 0 03:30 pts/0 00:00:00 grep --color=auto 3712
2、调试运行程序 gdb -p $PID
[root@spark03 demo]# ps -ef |grep a.out
root 3787 1 65 03:35 pts/0 00:00:11 /demo/a.out
root 3799 1906 0 03:35 pts/0 00:00:00 grep --color=auto a.out
[root@spark03 demo]# gdb -p 3787
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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-redhat-linux-gnu".
For bug reporting instructions, please see:
.
Attaching to process 3787
Reading symbols from /demo/a.out...done.
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00000000004004ee in for1 () at run.c:3
3 void for1(){
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64
(gdb) n
5 }
(gdb) n
main () at run.c:17
17 for2();
(gdb) s
for2 () at run.c:8
8 int i = 0;
(gdb) n
9 i++;
(gdb) n
11 }
(gdb) n
main () at run.c:18
18 }
(gdb) n
16 for1();
(gdb) s
for1 () at run.c:5
5 }
(gdb) n
main () at run.c:17
17 for2();
(gdb) s
for2 () at run.c:8
8 int i = 0;
(gdb) n
9 i++;
(gdb) n
11 }
(gdb)
当然,这里只是浅学一下,更多更细节的东西还是需要阅读官方文档~