常用GDB调试流程

1、常用调试命令详解:

gdb ./文件名  ---file 文件名
以下为调试状态下的可以用到的命令(可以仅输入单词的输入,如break可简为b),尖括号中为说明  
list <显示源代码>  ----l
break 行号 <设置断点>  ---b
run <运行程序>  ---r
continue <继续从断点处执行>  ---c
print 变量 <调试时查看变量的值>  ---p
del 行号 <删除断点>  ---d
step <单步执行,可跟踪到函数内部>  ---s
next <单步执行,不可跟踪到函数内部>  ---n
quit <退出> ---q

 

 命令 解释 示例
file <文件名>加载被调试的可执行程序文件。
因为一般都在被调试程序所在目录下执行GDB,因而文本名不需要带路径。
(gdb) file gdb-sample
rRun的简写,运行被调试的程序。
如果此前没有下过断点,则执行完整个程序;如果有断点,则程序暂停在第一个可用断点处。
(gdb) r
cContinue的简写,继续执行被调试程序,直至下一个断点或程序结束。(gdb) c
b <行号>
b <函数名称>
b *<函数名称>
b *<代码地址>

d [编号]

b: Breakpoint的简写,设置断点。两可以使用“行号”“函数名称”“执行地址”等方式指定断点位置。
其中在函数名称前面加“*”符号表示将断点设置在“由编译器生成的prolog代码处”。如果不了解汇编,可以不予理会此用法。

d: Delete breakpoint的简写,删除指定编号的某个断点,或删除所有断点。断点编号从1开始递增。

(gdb) b 8
(gdb) b main
(gdb) b *main
(gdb) b *0x804835c

(gdb) d

s, ns: 执行一行源程序代码,如果此行代码中有函数调用,则进入该函数;
n: 执行一行源程序代码,此行代码中的函数调用也一并执行。

s 相当于其它调试器中的“Step Into (单步跟踪进入)”;
n 相当于其它调试器中的“Step Over (单步跟踪)”。

这两个命令必须在有源代码调试信息的情况下才可以使用(GCC编译时使用“-g”参数)。

(gdb) s
(gdb) n
si, nisi命令类似于s命令,ni命令类似于n命令。所不同的是,这两个命令(si/ni)所针对的是汇编指令,而s/n针对的是源代码。(gdb) si
(gdb) ni
p <变量名称>Print的简写,显示指定变量(临时变量或全局变量)的值。(gdb) p i
(gdb) p nGlobalVar
display ...

undisplay <编号>

display,设置程序中断后欲显示的数据及其格式。
例如,如果希望每次程序中断后可以看到即将被执行的下一条汇编指令,可以使用命令
“display /i $pc”
其中 $pc 代表当前汇编指令,/i 表示以十六进行显示。当需要关心汇编代码时,此命令相当有用。

undispaly,取消先前的display设置,编号从1开始递增。

(gdb) display /i $pc

(gdb) undisplay 1

iInfo的简写,用于显示各类信息,详情请查阅“help i”。(gdb) i r
qQuit的简写,退出GDB调试环境。(gdb) q
help [命令名称]GDB帮助命令,提供对GDB名种命令的解释说明。
如果指定了“命令名称”参数,则显示该命令的详细说明;如果没有指定参数,则分类显示所有GDB命令,供用户进一步浏览和查询。
(gdb) help display

2、调试gdbtest.c代码片段:

#include <stdio.h>
int GlobalTestCount = 0;//全局变量初始化
int TestFunction(int a, int b)
{
    printf("TestFunction is called, a = %d, b = %d /n", a, b);
    return (a + b);
}
int main()
{
    int n =1;
    n++;
    GlobalTestCount += 520;
    printf("n = %d, GlobalTestCount = %d /n", n, GlobalTestCount );
    int sum = TestFunction(1, 2);
    printf("sum = %d", sum );
    return 0;
}

3、代码编译命令

3.1 、常用编译参数

  -c                       编译、汇编到目标代码,不进行链接。---处理多个文件时一次性将所有中间文件链接到可执行文件,比如写makefile 
  -g   在编译的时候,产生调试信息。 ---一般gdb调试时使用

  -o <文件>                输出到 <文件>

3.2、 生成带调试信息的可执行文件

   gcc -g gdbtest.c -o gdbtest

4、 调试过程

[root@localhost Code]# ls
gdbtest.c
[root@localhost Code]# gcc -g gdbtest.c  -o gdbtest
[root@localhost Code]# ls
gdbtest  gdbtest.c
[root@localhost Code]# gdb ./gdbtest
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/Code/gdbtest...done.
(gdb) l
1       #include <stdio.h>
2       int GlobalTestCount = 0;//全局变量初始化
3       int TestFunction(int a, int b)
4       {
5           printf("TestFunction is called, a = %d, b = %d /n", a, b);
6           return (a + b);
7       }
8       int main()
9       {
10          int n =1;
(gdb) b main
Breakpoint 1 at 0x80483f9: file gdbtest.c, line 10.
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x080483f9 in main at gdbtest.c:10
(gdb) r
Starting program: /home/Code/gdbtest 


Breakpoint 1, main () at gdbtest.c:10
10          int n =1;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686
(gdb) n
11          n++;
(gdb) s
12          GlobalTestCount += 520;
(gdb) p n
$1 = 2
(gdb) n
13          printf("n = %d, GlobalTestCount = %d /n", n, GlobalTestCount );
(gdb) p GlobalTestCount
$2 = 520
(gdb) b  15
Breakpoint 2 at 0x804844c: file gdbtest.c, line 15.
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x080483f9 in main at gdbtest.c:10
        breakpoint already hit 1 time
2       breakpoint     keep y   0x0804844c in main at gdbtest.c:15
(gdb) d 1
(gdb) i b
Num     Type           Disp Enb Address    What
2       breakpoint     keep y   0x0804844c in main at gdbtest.c:15
(gdb) c
Continuing.
Breakpoint 2, main () at gdbtest.c:15
15          printf("sum = %d", sum );
(gdb) n
16          return 0;
(gdb) p sum
$3 = 3
(gdb) c
Continuing.
n = 2, GlobalTestCount = 520 /nTestFunction is called, a = 1, b = 2 /nsum = 3
Program exited normally.
(gdb) 

5、进程死锁调试

5.1 ps -aux|grep 进程名    查看当前进程详细信息

 USER :进程的所属用户,
PID :进程的进程ID号, 
%CPU :进程占用的 CPU资源 百分比,
%MEM :进程占用的 物理内存 百分比, 
VSZ :进程使用掉的虚拟内存量 (Kbytes) ,
RSS :进程占用的固定的内存量 (Kbytes) ,
TTY :与进程相关联的终端(tty),?代表无关,tty1-tty6是本机上面的登入者程序,pts/0表示为由网络连接进主机的程序。
STAT :进程的状态,具体见2.1列出来的部分 ,
START :进程开始创建的时间 ,
TIME :进程使用的总cpu时间,
COMMAND : 进程对应的实际程序。

5.2 gdb attach 进程号   使用gdb挂载该进程

5.3 i thread  看当前所有的线程情况

5.4 t 线程号  进入指定线程

5.5 bt 查看当前线程堆栈

5.6 f  n查看当前堆栈第n个函数

5.7 bt 查看当前函数堆栈

5.8 p mutex_ 查看该mutex owner 拥有者线程号

5.9 再结合上述前面4综合进行调试

6、查看core-dump 信息

6.1 gdb 进程名 -c coredump文件

6.2 bt 查看崩溃信息

7、杀进程

killall -9 进程名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值