入门JDB一些简单的调试指令 运行 gdb 退出gdb 等

1.编写一个c语言程序

[root@localhost cyuyan]# cat file3
cat: file3: 没有那个文件或目录
[root@localhost cyuyan]# cat file3.c
#include<stdio.h>
int main(){
printf("我喜欢使用gdb进行调试系统");
return  0;
}

编译一下这个程序

[root@localhost cyuyan]# gcc file3.c

默认生成调试程序 默认生成a.out

[root@localhost cyuyan]# gcc file3.c -g
[root@localhost cyuyan]# ls a.out
a.out

启动jdb调试

方法一 gdb 文件名

[root@localhost cyuyan]# gdb a.out
GNU gdb (GDB) 9.2
Copyright (C) 2020 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-pc-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"...
Reading symbols from a.out...
(gdb) 

在这里面与shell一样也是可以输入命令的环境
这里这是完成了jdb的启动 程序并没有开始运行

输入run指令启动程序

(gdb) run
Starting program: /home/cyuyan/a.out 
我喜欢使用gdb进行调试系统[Inferior 1 (process 38822) exited normally]
(gdb) `在这里插入代码片`

可以看到程序顺利 启动 执行 退出
gdb指令支持简写 run 可以写为 r

(gdb) r
Starting program: /home/cyuyan/a.out 
我喜欢使用gdb进行调试系统[Inferior 1 (process 38909) exited normally]
(gdb) 

退出jdb指令 quit/q

(gdb) quit
[root@localhost cyuyan]# 

方法二 直接输入gdb 启动gdb

[root@localhost cyuyan]# gdb
GNU gdb (GDB) 9.2
Copyright (C) 2020 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-pc-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".
(gdb) 

但是我们没有告诉gdb我们要调试那个程序 所以进入gdb中要输入自己要调试的程序

file指令 可执行程序的名字

(gdb) file a.out
Reading symbols from a.out...

这样就完成了调试程序的加载

设置断点

方法一:break/b 后加函数名 在main函数后面设置断点 info break查询断点
run 发现 停到了第九行的位置 如果需要继续执行 输入continue/c

(gdb) b main
Breakpoint 1 at 0x400531: file file3.c, line 3.
//提示我们在第三行设置了一个断点
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400531 in main at file3.c:3
(gdb) r
Starting program: /home/cyuyan/a.out 

Breakpoint 1, main () at file3.c:3
3	printf("我喜欢使用gdb进行调试系统");
(gdb) c
Continuing.
我喜欢使用gdb进行调试系统[Inferior 1 (process 39952) exited normally]

方法二:b/break 文件名:行号
在第三行设置断点 注意是文件名我设置的文件名为file3.c 程序名是file3

(gdb) b file3.c:3
Note: breakpoint 1 also set at pc 0x400531.
Breakpoint 2 at 0x400531: file file3.c, line 3.
//因为上面程序已经执行退出了 在调试时需要重新启动程序
(gdb) r
Starting program: /home/cyuyan/a.out 

Breakpoint 1, main () at file3.c:3
3	printf("我喜欢使用gdb进行调试系统");
(gdb) continue
Continuing.
我喜欢使用gdb进行调试系统[Inferior 1 (process 40304) exited normally]

附加知识点:

设置断点:
(gdb) break 断点
程序运行后,到达断点就会自动暂停运行。此时就可以查看该时刻的变量值,显示栈针,重新设置断点或者重新运行等。
断点可以通过函数名,文件内的行号来设置,也可以先指定文件名再指定行号,还可以指定与暂停位置的偏移量,或者用地址来设置。
(gdb) break 函数名
(gdb) break 行号
(gdb) break 文件名:行号
(gdb) break 文件名:函数名
(gdb) break +偏移量
(gdb) break -偏移量
(gdb) break *地址
在设置断点的时候,如果不指定断点位置,就在下一行代码上设置断点。
条件断点
(gdb) break 断点 if 条件
仅在特定条件下中断。对于已存在的断点,可使用condition为其添加条件。
(gdb) break 断点编号 条件
而删除指定编号断点的触发条件同样使用condition。
(gdb) condition 断点编号
查询断点
(gdb) info break
监视点
要想找到变量在何处被改变,可以使用 watch 命令(监视点, watchpoint)。
(gdb) watch <表达式>
<表达式>发生变化时暂停运行。<表达式>的意思是常量或变量等。
(gdb) awatch <表达式>
<表达式>被访问、改变时暂停运行。
(gdb) rwatch <表达式>
<表达式>被访问时暂停运行。
删除断点和监视点
用 delete 命令删除断点和监视点。
(gdb) delete <编号>

n 单步执行语句 输出的是下一步要执行的语句

Breakpoint 1, fun () at file5.c:4
4	int b=11;
(gdb) p $a
$1 = void
(gdb) 
$2 = void
(gdb) n
5	printf("b=%d\n",b);
(gdb) c
Continuing.
b=11
w xiaih1o[Inferior 1 (process 6098) exited normally]

//输入tab键 提示还有那些指令

list/l查看程序代码

在指令上面按方向箭头可以 回到上一次执行的指令

之前的c程序太简单了 用不上下面指令所以重新添加一个从程序

[root@localhost cyuyan]# vi file4.c
[root@localhost cyuyan]# vi file4.c
[root@localhost cyuyan]# cat file4
cat: file4: 没有那个文件或目录
[root@localhost cyuyan]# cat file4.c
#include<stdio.h>
int fun()
{
int b=11;
printf("b=%d\n",b);
return 0;
}
int main()
{
int a=10;
int array[5]={1,2,3,4,5};
printf("a=%d\n",a);
fun();
return 0;

}

list显示当行之后或周围的10多行

(gdb) list
1	
2	#include<stdio.h>
3	int fun()
4	{
5	int b=11;
6	printf("b=%d\n",b);
7	return 0;
8	}
9	int main()
10	{
(gdb) 

list start,end

list 1,3 显示1-3行

(gdb) list 1,3
1	
2	#include<stdio.h>
3	int fun()

显示当前变量的值 printf/p指令

//如果a是一个数组,10个元素,如果要显示则: 
(gdb) print *a@10 
//这样,会显示10个元素,无论a是double或者是int的都会正确地显示10个元素。 

//*修改运行时候的变量值: 
(gdb) print x=4 
//这里,x=4是C/C++的语法,意为把变量x值改为4,如果你当前调试的语言是Pascal,那么你可以使用Pascal的语法:x:=4。  

总结

进入gdb (两种方法 1.gdb 2.gdb 程序名) r-运行指令 q-退出指令 n-单目运行 c-程序继续执行 b-设置断点(两种方法 1.b 函数 2.b 文件名:行号)p输出当前变量值

一些更加详细的指令
链接: https://blog.csdn.net/yinjiabin/article/details/7732931.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临夏_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值