GDB简单讲解

GDB的作用

gdb是一款调试工具

  • 在 Windows 操作系统中,使用的大多是集成的开发环境(IDE),也就是说软件开发工具中已经包含了调试工具,例如 VC、BCB、Dev-C++ 等,这些软件中都集成了调试器。

  • 在 Linux 操作系统中,编译程序使用GCC(GCC 编译器的功能非常的强大,几乎可以编译所有的编程语言),集成的开发环境相对较少,但可以使用的调试工具有很多,例如 GDB、binutil、strace 等,最常用的调试工具是 GDB。

GDB的安装(linux 终端下)

(1) 首先检查GDB是否安装

gdb -v

如果出现下面内容就是已安装,否则就没有

GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 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".

(2) 使用命令安装,例如,Ubuntu系统安装GDB,命令行输入:sudo apt-get install gdb。

(3)再次检查是否安装 gdb -v

GDB的使用

test.c的代码如下:

#include<stdio.h>
#include <stdlib.h>

int get_sum(int n)
{
    int sum = 0;
    for(int i = 0;i<n;i++)
    {
        sum+=i;
    }
    return sum;
}

int main()
{
    int i = 100,result;
    result = get_sum(i);
    printf("%d",result);

    return 0;
}

开始

首先使用gcc -g .c文件 -o 可执行文件名 进行编译,再使用gdb + 可执行文件名进入gdb环境,进行调试。
命令如下如:
(1) gcc -g test.c -o test
(2) gdb test
(3) list等gdb命令;
2.出现问题的可能性:
(1)当编译时,未加 - g 选项,则进入gdb环境中执行命令会出现No symbol table is loaded. Use the “file” command.提示;
(2)当进入gdb环境时,未加可执行文件名,也会出现No symbol table is loaded. Use the “file” command.提示;
3.补充说明一下-g选项的作用:
在linux C中gcc编译器一章有说,-g选项的意义是“生成调试信息,该程序可以被调试器调试”

llist

列出代码
list 列出10行代码,再次运行列出接下来10行代码

(gdb) list
2       #include <stdlib.h>
3
4       int get_sum(int n)
5       {
6           int sum = 0;
7           for(int i = 0;i<n;i++)
8           {
9               sum+=i;
10          }
11          return sum;

list 5,10 列出第5行到地10行代码

(gdb) list 5,10
5       {
6           int sum = 0;
7           for(int i = 0;i<n;i++)
8           {
9               sum+=i;
10          }

list get_sum 列出get_sum周围的代码

(gdb) list get_sum
1       #include<stdio.h>
2       #include <stdlib.h>
3
4       int get_sum(int n)
5       {
6           int sum = 0;
7           for(int i = 0;i<n;i++)
8           {
9               sum+=i;
10          }

你可以自己试试下面两个命令
list test.c:5,10
list test.c:get_sum

print

  • print 变量或表达式:打印变量或表达式当前的值
  • print 变量=值: 对变量进行赋值
  • print &变量 : 打印地址

quit

推出gdb

break

设置断点

  • break i:在第i行设置断点
  • break 行号或函数名 if 条件:当条件满足时,程序在这里中断

next和step

next执行下一步,next会把函数当一步进行操作,但step会追踪进入函数,一条一条的执行函数里的语句

clear

清除断点

  • clear:清除所有断点
  • clear 行号:清除该行断点
  • clear 函数名:清除该函数断点
  • clear 断点标号:删除指定断点,如果要删除多个断点,标号可用空格隔开

help

  • 如果你不知道一个命令的作用是什么就可以
(gdb) help list
  • 如果想了解更多命令
(gdb) help all

run

使用gcc -q test.c -o test和gdb test只是装入程序,并没有运行,如果要使程序运行,在gdb提示符下输入run

(gdb) run
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值