【C语言】GDB使用

gdb简介

GDB(GNU Debugger)是GCC的调试工具。

要使用GDB,需要在源代码编译的时候添加 -g 参数。

用到四个基础文件 file1.c、file2.c 、head.h、hello.c,一个 makefile 文件

其中文件file1.c 如下:

#include"head.h"

int sum(int a, int b)
{
		printf("here is file1\n");
        return a+b;
}

其中文件file2.c 如下:

#include"head.h"

int mul(int a,int b)
{
    	printf("here is file2\n");
        return a*b;
}

其中文件head.h如下:

#include<stdio.h>
int sum(int a,int b);
int mul(int a,int b);

其中调用文件 hello.c 如下:

#include"head.h"

int main()
{
    	printf("here is hello.c\n");
    	int a=sum(1,2);
    	int b=mul(2,2);
        printf("1+2=%d\n",a);
        printf("2*2=%d\n",b);
        return 0;
}

makefile文件:

src=$(wildcard ./*.c)
obj=$(patsubst %.c,%.o,$(src))
target=hello
CC=gcc
CPPFLAGS=-I ./
# 最终目标
$(target):$(obj)
        $(CC) -o $@ $^
#注意这里加了参数 -g
%.o:%.c
        $(CC) -o $@ -g -c $< $(CPPFLAGS)

# 伪目标
.PHONY:clean
clean:
        rm -rf $(target) $(obj)

使用 make 命令生成 hello 可执行文件,注意在编译阶段一定要添加 -g 参数

make
#输出,并生成 hello 可执行文件
gcc -o file2.o -g -c file2.c -I./
gcc -o hello.o -g -c hello.c -I./
gcc -o file1.o -g -c file1.c -I./
gcc -o hello file2.o hello.o file1.o

gdb启动

gdb启动命令

#gdb 可执行文件
gdb hello
#输出
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
...
...
Reading symbols from hello...done.
(gdb) 

gdb 就启动了,可以在gdb里面输入以下相关调试命令进行调试

1. 显示源代码

命令作用
show listsize查看当前 list 命令显示的行数
set listsize count设置 list 命令显示的行数为count
list显示当前源程序文件,显示下方默认行数的内容
list -这里可以理解成减号,显示上方默认行数的内容
list linenum显示 linenum 行处的上下文内容
list function显示函数名为function的函数的源程序
list file:linenum显示file文件下第n行
list file:function显示file文件的函数名为function的函数的源程序

show listsize

查看当前 list命令 显示行数的大小

(gdb) show listsize
#输出,默认显示 10行
Number of source lines gdb will list by default is 10.

set listsize count

设置一次显示源代码的行数

#设置默认显示 5行
(gdb) set listsize 5
#输出默认显示行数
(gdb) show listsize
#输出,默认显示5行
Number of source lines gdb will list by default is 5.

2. 设置断点

设置断点用 break 命令, 简写为 b

2.1 添加断点

命令作用
b linenum在第 linenum 行设置断点
b func在函数 func 入口处设置断点
b filename:linenum在源文件filename的linenum行处设置断点
b filename:function在源文件filename的function函数的入口处设置断点
b test.c:8 if value== 5条件断点,当变量value的值为5时断电生效

2.2 查看所有断点

命令作用
info break简写 i b ,查询所有断点

2.3 断点属性修改

删除断点

命令作用
delete num简写 d num ,删除某个断点
delete num1 num2简写 d num1 num2,删除多个断点
delete n1-n2简写 d n1-n2 ,删除多个断点
delete删除所有断点

禁用断点

命令作用
disable num简写 dis num ,禁用某个断点
disable num1 num2简写 dis num1 num2,禁用多个断点
disable n1-n2简写 dis n1-n2 ,禁用多个断点
disable禁用所有断点

启用断点

命令作用
enable num简写 ena num ,启用某个断点
enable num1 num2简写 ena num1 num2,启用多个断点
enable n1-n2简写 ena n1-n2 ,启用多个断点
enable启用所有断点

3. 调试代码

命令作用
run简写 r ,运行程序,有断点也不停
start运行程序,有断点停止运行
next简写 n,单步执行
step简写 s,单步执行,可以进入函数体内
finish退出函数体
until简写为u,在循环体内单步执行, 直到退出循环体
continue简写为c,继续运行程序, 若有断点则跳到下一个断点处

4. 查看变量的值

命令作用
print count简写p count,打印变量count
display 变量名添加一个变量名展示
info display查看添加了哪些变量名展示
undisplay num(info display时显示的编号)删除某个变量展示
delete display num删除某个变量展示
disable display num禁用某个变量的展示
enable display num启用某个变量的展示

5. 其他语法

查看变量类型: ptype 变量名

临时设置变量的值:set var 变量名=值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值