gdb教程-实战演练

本文详细介绍了如何使用GDB进行源码级别的调试,包括设置断点、查看参数、执行代码、查看变量值、打印堆栈等操作,帮助开发者深入理解程序运行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        注意:本内容主要用于常见的gdb命令的熟悉,gdb不是在所有的情况下都能打印出对应的代码,如果调试的服务器上没有源码的情况下只能打印出代码的行号,这个时候需要根据行号自行去跟源码做对照。

源码流程调试:

1. 进入调试

[root@localhost testgdb]# gdb bin/basicFunction

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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 /opt/testgdb/bin/basicFunction...done.

    看到done,表示进入调试进程

 

2.调试命令 

(gdb) set args 10000 2 /opt/testgdb/1.txt         #设置进程启动入参

(gdb) show args                             #展示进程入参

Argument list to give program being debugged when it is started is "10000 2 /opt/testgdb/1.txt".

(gdb) b main                     #首先在main函数处设置断点

Breakpoint 1 at 0x4006cf: file /opt/testgdb/src/basicFunction.c, line 15.

(gdb) b parsePara

Breakpoint 2 at 0x4007ae: file /opt/testgdb/public/basicFuncHandler.c, line 5.

(gdb) r                          #启动后进程停在第一个断点处

Starting program: /opt/testgdb/bin/basicFunction

Breakpoint 1, main (argc=1, argv=0x7fffffffe4f8) at /opt/testgdb/src/basicFunction.c:15

15       long lRet = -1L;

Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64

(gdb) info break                      #展示断点信息

Num     Type           Disp Enb Address            What

1       breakpoint     keep y   0x00000000004007ae in parsePara at /opt/testgdb/public/basicFuncHandler.c:5

        breakpoint already hit 1 time

2       breakpoint     keep y   0x00000000004006cf in main at /opt/testgdb/src/basicFunction.c:15

        breakpoint already hit 1 time

 (gdb) list 10                                #展示代码

5          printf("---------------------------usage--------------\n");

6          printf("./basicFunction cycleCnt funcFlag fileName\n");

7          printf("cycleCnt:number of cycles\n");

8          printf("funcFlag:function number identification");

9          printf("fileName:the name of the file to detect if it exists");

10       return;

11   }

12  

13   int main(int argc,char **argv){

14      

(gdb) n                #执行下一行代码

17       if( argc != 4){

(gdb)                 #直接回车,重复上一条命令

22       struct ParaInfo * pParaInfo = (struct ParaInfo *)malloc(sizeof(struct ParaInfo));

(gdb) n

23       if(!pParaInfo){

 (gdb) ptype pParaInfo               #展示变量类型

type = struct ParaInfo {

    long lCycleCnt;

    long lFuncFlag;

    char sFilename[100];

} *

(gdb)c                            #直接跳到下一个断点

Continuing.



Breakpoint 2, parsePara (pParaInfo=0x602010, pParaList=0x7fffffffe4d8)

    at /opt/testgdb/public/basicFuncHandler.c:5

5          pParaInfo->lCycleCnt = atoi(pParaList[1]);

(gdb) b basicFuncHandler.c:51 if j==10

Breakpoint 3 at 0x4008cd: file /opt/testgdb/public/basicFuncHandler.c, line 51.

(gdb)c                          

Continuing.



Breakpoint 3, execLoop (lCycleCnt=10000) at /opt/testgdb/public/basicFuncHandler.c:51

51           long j = i+1;      

(gdb) p j      #打印变量的值

$1 = 10

(gdb) set j=100      #设置变量的值

(gdb) p j

$2 = 100

(gdb) b basicFuncHandler.c:63              #设置断点

Breakpoint 4 at 0x400921: file /opt/testgdb/public/basicFuncHandler.c, line 63.

(gdb) c

Continuing.



Breakpoint 4, checkFileExist (sFilename=0x602020 "/opt/testgdb/1.txt")

    at /opt/testgdb/public/basicFuncHandler.c:63

63           return -3;

(gdb) bt     #打印堆栈,很多时候进程的代码结构非常的复杂,正想一步步执行到指定位置难度很大,可以直接在指定位置设置断点,停在断点处后,打印堆栈就能清楚的知道代码执行路径

#0  checkFileExist (sFilename=0x602020 "/opt/testgdb/1.txt") at /opt/testgdb/public/basicFuncHandler.c:63

#1  0x0000000000400996 in handlerFunc (pParaInfo=0x602010) at /opt/testgdb/public/basicFuncHandler.c:82

#2  0x000000000040076d in main (argc=4, argv=0x7fffffffe4d8) at /opt/testgdb/src/basicFunction.c:34

(gdb) quit

A debugging session is active.



        Inferior 1 [process 14189] will be killed.



Quit anyway? (y or n) y

[root@localhost bin]#



将源码打包后在尝试一遍,结果如下:

[root@localhost testgdb]# gdb bin/basicFunction

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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 /opt/testgdb/bin/basicFunction...done.

(gdb) b main

Breakpoint 1 at 0x4006cf: file /opt/testgdb/src/basicFunction.c, line 15.

(gdb) r

Starting program: /opt/testgdb/bin/basicFunction



Breakpoint 1, main (argc=1, argv=0x7fffffffe508) at /opt/testgdb/src/basicFunction.c:15

15   /opt/testgdb/src/basicFunction.c: 没有那个文件或目录.

Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64

(gdb) n

17   in /opt/testgdb/src/basicFunction.c   #这时候只能看到行号需要自行跟源码对应

(gdb)

18   in /opt/testgdb/src/basicFunction.c

(gdb)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值