0
点赞
收藏
分享

微信扫一扫

GDB常用命令


安装

// 基于 centos
yum -y install gdb
yum -y install cgdb //这个更好用

开启 core 日志, 采集程序崩溃状态, 并设置core日志文件格式

echo 'ulimit -S -c unlimited > /dev/null 2>&1' >> /etc/profile
source /etc/profile
echo 'kernel.core_pattern = ./core_%t_%p_%e' >> /etc/sysctl.conf
echo 'kernel.core_uses_pid = 1' >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf

测试实例

#include<assert.h>
#include <stdio.h>
void show(int data)
{
printf("----begining of function show-------\n");
printf("data=%d\n",data);
printf("----end of function show-------\n");
}
int main(int argc, char* argv[])
{
int data=1;
int i=0;
assert(argc>1);
printf("argc=%d\n", argc);
if(argc ==1 )
{
printf("You should give at least one param to this process!\n");
}
printf("data=%d\n", data);

while(i<=argc)
{
printf("argv[%d]=%s\n",i,argv[i]);
i++;
}
show(data);
return 0;
}

操作过程


[root@node_205 test]# gdb test
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 <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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/test/test...done.
(gdb) show args
Argument list to give program being debugged when it is started is "".
(gdb) set args 1 2 3
(gdb) show args
Argument list to give program being debugged when it is started is "1 2 3".
(gdb) l
2 #include <stdio.h>
3 void show(int data)
4 {
5 printf("----begining of function show-------\n");
6 printf("data=%d\n",data);
7 printf("----end of function show-------\n");
8 }
9 int main(int argc, char* argv[])
10 {
11 int data=1;
(gdb) i 11
Undefined info command: "11". Try "help info".
(gdb) b 11
Breakpoint 1 at 0x400611: file test.c, line 11.
(gdb) r
Starting program: /root/test/test 1 2 3

Breakpoint 1, main (argc=4, argv=0x7fffffffe5b8) at test.c:11
11 int data=1;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-317.el7.x86_64
(gdb) l
6 printf("data=%d\n",data);
7 printf("----end of function show-------\n");
8 }
9 int main(int argc, char* argv[])
10 {
11 int data=1;
12 int i=0;
13 assert(argc>1);
14 printf("argc=%d\n", argc);
15 if(argc ==1 )
(gdb) n
12 int i=0;
(gdb) n
13 assert(argc>1);
(gdb) p argc
$1 = 4
(gdb) l
8 }
9 int main(int argc, char* argv[])
10 {
11 int data=1;
12 int i=0;
13 assert(argc>1);
14 printf("argc=%d\n", argc);
15 if(argc ==1 )
16 {
17 printf("You should give at least one param to this process!\n");
(gdb) l
18 }
19 printf("data=%d\n", data);
20
21 while(i<=argc)
22 {
23 printf("argv[%d]=%s\n",i,argv[i]);
24 i++;
25 }
26 show(data);
27 return 0;
(gdb)
28 }
29
(gdb)
Line number 30 out of range; test.c has 29 lines.
(gdb) b 26
Breakpoint 2 at 0x4006af: file test.c, line 26.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) c
Continuing.
argc=4
data=1
argv[0]=/root/test/test
argv[1]=1
argv[2]=2
argv[3]=3
argv[4]=(null)

Breakpoint 2, main (argc=4, argv=0x7fffffffe5b8) at test.c:26
26 show(data);
(gdb) s
show (data=1) at test.c:5
5 printf("----begining of function show-------\n");
(gdb) l
1 #include<assert.h>
2 #include <stdio.h>
3 void show(int data)
4 {
5 printf("----begining of function show-------\n");
6 printf("data=%d\n",data);
7 printf("----end of function show-------\n");
8 }
9 int main(int argc, char* argv[])
10 {
(gdb) n
----begining of function show-------
6 printf("data=%d\n",data);
(gdb) n
data=1
7 printf("----end of function show-------\n");
(gdb) n
----end of function show-------
8 }
(gdb) n
main (argc=4, argv=0x7fffffffe5b8) at test.c:27
27 return 0;
(gdb) l
22 {
23 printf("argv[%d]=%s\n",i,argv[i]);
24 i++;
25 }
26 show(data);
27 return 0;
28 }
29
(gdb) c

  • 测试 display

[root@node_205 test]# gdb test -q
Reading symbols from /root/test/test...done.
(gdb) set args 1 2 3
(gdb) l
2 #include <stdio.h>
3 void show(int data)
4 {
5 printf("----begining of function show-------\n");
6 printf("data=%d\n",data);
7 printf("----end of function show-------\n");
8 }
9 int main(int argc, char* argv[])
10 {
11 int data=1;
(gdb) l
12 int i=0;
13 assert(argc>1);
14 printf("argc=%d\n", argc);
15 if(argc ==1 )
16 {
17 printf("You should give at least one param to this process!\n");
18 }
19 printf("data=%d\n", data);
20
21 while(i<=argc)
(gdb) l
22 {
23 printf("argv[%d]=%s\n",i,argv[i]);
24 i++;
25 }
26 show(data);
27 return 0;
28 }
29
(gdb) b 21
Breakpoint 1 at 0x400676: file test.c, line 21.
(gdb) r
Starting program: /root/test/test 1 2 3
argc=4
data=1

Breakpoint 1, main (argc=4, argv=0x7fffffffe5b8) at test.c:21
21 while(i<=argc)
Missing separate debuginfos, use: debuginfo-install glibc-2.17-317.el7.x86_64
(gdb) p i
$1 = 0
(gdb) p argc
$2 = 4
(gdb) display i
1: i = 0
(gdb) display argc
2: argc = 4
(gdb) n
23 printf("argv[%d]=%s\n",i,argv[i]);
2: argc = 4
1: i = 0
(gdb) l
18 }
19 printf("data=%d\n", data);
20
21 while(i<=argc)
22 {
23 printf("argv[%d]=%s\n",i,argv[i]);
24 i++;
25 }
26 show(data);
27 return 0;
(gdb) n
argv[0]=/root/test/test
24 i++;
2: argc = 4
1: i = 0
(gdb) n
21 while(i<=argc)
2: argc = 4
1: i = 1
(gdb) n
23 printf("argv[%d]=%s\n",i,argv[i]);
2: argc = 4
1: i = 1
(gdb) n
argv[1]=1
24 i++;
2: argc = 4
1: i = 1

常用命令实例

  • gcc test.c -o test -g
  • gdb test
  • gdb test -q: 不打印提示信息
  • b 11: 给第11行设置断点
  • b main: 给main函数设置断点
  • i b: 显示所有断点
  • d 1: 删除第一个断点
  • d: 删除所有断点
  • l:列出下面10行代码
  • set args 1 2 3 //设置参数
  • show args //查看参数值
  • n:执行下一行代码
  • s : 进入自定义函数内部
  • c: 继续运行程序
  • p data
  • display data: 监控data的值
  • q: 退出

调试 core 文件

实例如下

root@node_205 test_core]# cat  test.c
#include<stdio.h>

int main(void)
{
int a=1;
int b=0;
printf("1/0=%d\n",a/b);
return 0;
}
[root@node_205 test_core]#
[root@node_205 test_core]# gcc -g test.c -o test
[root@node_205 test_core]# ./test
Floating point exception (core dumped)
[root@node_205 test_core]#
[root@node_205 test_core]# ls
core.79215 test test.c
[root@node_205 test_core]# gdb test core.79215
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 <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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/test/test_core/test...done.
[New LWP 79215]
Core was generated by `./test'.
Program terminated with signal 8, Arithmetic exception.
#0 0x0000000000400547 in main () at test.c:7
7 printf("1/0=%d\n",a/b);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
(gdb) p a
$1 = 1
(gdb) p b
$2 = 0

调试正在运行的进程

  • 根据进程号调试
    说明:
    argv[argc] 为NULL.

补充一个 linux C 代码缩进工具 indent

indent -i2 -bli 0 -sob -npsl  input.c -o output.c

其中:
-i n:设置缩排的格数
-bli n:设置{ }缩排的格数
-sob:删除多余的空白行
-npsl:程序类型与程序名称放在同一行
参考 ​​​博文​​​ 参考2:​​博文​​

参考

​​https://wizardforcel.gitbooks.io/100-gdb-tips/content/set-pagination-off.html​​​
https://zhuanlan.zhihu.com/p/74897601



举报

相关推荐

0 条评论