linux下 gdb安装与使用简介

linux下 gdb安装与使用简介

  • gdb简介
    GDB,又称GNU调试器,是用来帮助调试我们程序的工具。gdb可以设置断点、查看变量、堆栈空间的值、设置执行条件的值等。
  • red hat6.3下安装gdb调试工具
    在这里插入图片描述
    拷贝gdb软件包到用户目录下,切换到用户目录下,安装gdb工具
[xsw@xsw Packages]$ cp ./gdb-7.2-56.el6.i686.rpm  ~
[xsw@xsw Packages]$ cd ~
[xsw@xsw ~]$ sudo rpm -ivh gdb-7.2-56.el6.i686.rpm 

在这里插入图片描述

  • ubuntu下gdb安装
sudo apt-get install gdb
  • gdb工具常用命令
项目功能
rrun, 直接调到断点处,没有设置断点的话直接运行程序
b func设置一个断点breakpoint在函数”func”的最开始
b N在当前函数的第N行设置断点
b file.c:N在当前源文件file.c的第N行设置断点
d N删除第N行的断点
info b显示所有断点
c继续运行到下一个断点或程序结束
f运行到当前程序结束
s单步调试,进入函数
s N执行接下来的N行程序
n单步调试,不进入函数
p data输出变量data的值
bt查看调用的堆栈
l一次列出10行源码(从mian.c开始)
l func列出函数func的10行源码
condition设置条件断点,condition 1 i=5,设置断点1的条件断点为i=5
q退出gdb
finish跳出当前函数
set args当可执行文件后需要带参数时可通过set args添加
  • 可执行文件带参数调试示例
例如:gdb a.out  #启动调试
(gdb) set args 参数1 参数2 参数3 
(gdb) l
1	#include <stdio.h>
2	#include <stdlib.h>
3	#include "link_test.h"
4	int main(int argc,char **argv)
5	{
6	    if(argc!=2)
7	    {
8	        printf("格式:./app <文件名>\n");
9	        return 0;
10	    }
(gdb) b 6  #设置断点
(gdb) r  #开始运行
(gdb) p argv[0]  #打印形参信息
$1 = 0x7fffffffe2a0 "/mnt/hgfs/ubuntu/work/a.out"
(gdb) p argv[1]
$2 = 0x7fffffffe2bc "参数1"
(gdb) p argv[2]
$3 = 0x7fffffffe2c4 "参数2"
(gdb) p argv[3]
$4 = 0x7fffffffe2cc "参数3"
(gdb) p argv[4]
$5 = 0x0
  • 可执行文件不带参数调试示例
#include <stdio.h>
int main()
{
	int a;
	int *p=&a;
	printf("请输入val:\n");
	scanf("%d",&a);
	int i=0;
	for(i=0;i<5;i++)
	{
		*p+=i;	
	}
	printf("a=%d\n",*p);	
}
  • 调试步骤
    1.编译程序,使用gdb调试时需加上-g,进入调试模式
[xsw@xsw cc]$ gcc main.c -g
[xsw@xsw cc]$ gdb ./a.out
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-56.el6)
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/xsw/xsw_work/cc/a.out...done.
(gdb) 

2.列出源码

(gdb) l
1	#include <stdio.h>
2	int main()
3	{
4		int a;
5		int *p=&a;
6		printf("请输入val:\n");
7		scanf("%d",&a);
8		int i=0;
9		for(i=0;i<5;i++)
10		{
(gdb)

3.设置断点,开始运行

(gdb) b 4
Breakpoint 1 at 0x804844d: file main.c, line 4.
(gdb) r
Starting program: /home/xsw/xsw_work/cc/a.out 

Breakpoint 1, main () at main.c:5
5		int *p=&a;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.i686
(gdb) 

4.单步执行

(gdb) s
6		printf("请输入val:\n");
(gdb) s
请输入val:
7		scanf("%d",&a);
(gdb) s
5
8		int i=0;

5.查看变量值

(gdb) p a
$1 = 5
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT_阿水

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

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

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

打赏作者

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

抵扣说明:

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

余额充值