如何打开C语言dbug中的命令窗口,debug —— C语言 编译时候进行debug的调试

gdb是the GNU Debugger的简称。它是一款UNIX平台的调试器(debugger),可用于为C, C++, Objective-C, Java, Fortran等程序debug。

在gdb中,你可以通过设置断点(break point)来控制程序运行的进度,并查看断点时的变量和函数调用状况,从而发现可能的问题。在许多IDE中,gdb拥有图形化界面。

一、初次使用gdb调试器,出现的No symbol table is loaded. Use the "file" command.问题:

b3aa8c73d8ce206b6a4916975ea0d3ff.png

首先使用gcc   -g   .c文件   -o  可执行文件名  进行编译;再使用gdb + 可执行文件名进入gdb环境,进行调试。

为了使用gdb对进行调试,必须使用-g选项(在编译时生成debug信息)

命令如下如:

#gcc -g test.c -o test

#gdb test

如果上一步gdb + 不是 -g编译后的 可执行文件,而是 gdb  ./a.out ,则会出现 Use the "file" command.问题

(gdb)list

list命令是用来列出源码的。

详细的list的使用查看文章《 debug —— list调试命令》

(2)   gdb  test

(3)  list等gdb命令;

(如有必要,使用:$chmod +x test来增加用户的执行权限。)

二、使用gdb调试:

1》启动gdb:

[root@node-2 jieer]# gcc -g struct.c -o struct

[root@node-2 jieer]# gdb struct

2》显示程序:详细的list的使用查看文章《 debug —— list调试命令》

<1> 将显示以第3行为中心,总共10行的程序。

如果要查看某个文件中的内容,需要说明文件名(例如:(gdb) list struct.c:12)。

(gdb) list 3

<2>可以具体说明所要列出的程序行的范围(即 显示5-15行的程序).

(gdb) list 5,15

<3>显示某个函数.

(gdb) list main

3》设置断点

<1>我们可以在程序的某一行设置断点,比如:

(gdb) break 16 (或者是简写 b 16)

将在test.c的第16行设置断点。

d06d2bd2b8566b986be94efd4cf6bb52.png

<2>你可以查看自己设置的断点:

(gdb) info break

1529ac79783a8896203d0549e6b4c709.png

<3>每个断点有一个识别序号。我们可以根据序号删除某个断点:

(gdb) delete 1

<4>也可以删除所有断点:

(gdb) delete breakpoints

到目前为止,程序内变量的赋值都是在程序内部完成的,如果程序内的一些变量需要执行文件的时候,用命令行传入呢?

例如:需要你打印出argv[0]、argv[1]、argv[2]的值得一个函数你该如何操作呢?

《可以查看文章《debug —— set args调试命令(作为程序运行时的参数)》

4》保存断点

<1>

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x0000000000400536 in main at struct.c:12

2 breakpoint keep y 0x0000000000400547 in main at struct.c:13

<2>

(gdb) save breakpoint fig.dp

Saved to file 'fig.dp'.

<3>

[root@node-2 jieer]# gdb struct -x fig.dp

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1

Copyright (C) 2013 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later

This is free software: you are free to change and redistribute it.

There is NO WA RRANTY, 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:

...

Reading symbols from /root/jieer/struct...done.

Breakpoint 1 at 0x400536: file struct.c, line 12.

Breakpoint 2 at 0x400547: file struct.c, line 13.

5》运行控制

<1>让程序从断点开始,再多运行一行(可以看函数内嵌套的另一个函数的内容):

(gdb) step (可以用简写 s)

源代码:

21 int ccc(){

22 int total = 0;

23 char other[512] = {'\0'};

24

25 bbb(&total);

26 printf("ccc:total=%d\n",total);

27 printf("ccc:total=%p\n",&total);

28 ddd(other);

29 }

30 int main(){

31 ccc();

32 return 0;

33 }

b4e5c8aae41db93efe651b9f4a2d4574.png

步骤1:在文件内第30行设置一个断点,即在test.c文件,mian函数中第30 行。

步骤2:运行程序:可以看到执行到ccc()函数哪一行了;

步骤3:执行s命令:从此处开始多运行一行,进入ccc()函数内部;

步骤4,5:继续调试函数:看到bbb()函数,这是很确定我们进入了ccc()函数中了。

那么不用s命令会有怎么的结果呢?我们来看一下

8eb0364f69c5df2876adb641619b2da5.png

<2>也可以使用下面命令,从断点恢复运行,直到下一个断点:

(gdb) continue

<3>使用run重新开始运行

(gdb) run

程序正常结束。

6》退出

使用下面命令退出gdb:

(gdb) quit (可以使用简写 q 或者 .qu)

三、举例分析:

1》struct.c文件的源码如下:

1 #include2 #include3 #include4 struct student

5 {

6 char *name;

7 int score;

8 }stu,*pstu;

9

10 int main()

11 {

12 stu.name = (char *)malloc(20*sizeof(char));

13 strcpy(stu.name,"jie");

14 stu.score = 90;

15

16 pstu = (struct student *)malloc(sizeof(struct student));

17 pstu->name = (char *)malloc(20*sizeof(char));

18 strcpy(pstu->name,"jieer");

19 pstu->score = 9;

20

21 return 0;

22 }

2》具体操作如下:

[root@node-2 jieer]# gcc -g struct.c -o struct

[root@node-2 jieer]# gdb struct

(gdb) break 12

Breakpoint 1 at 0x400536: file struct.c, line 12.

(gdb) break 13

Breakpoint 2 at 0x400547: file struct.c, line 13.

(gdb) r

Starting program: /root/jieer/struct

Breakpoint 1, main () at struct.c:12

12 stu.name = (char *)malloc(20*sizeof(char));

Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64

(gdb) p stu.name

$1 = 0x0

(gdb) c

Continuing.

Breakpoint 2, main () at struct.c:13

13 strcpy(stu.name,"jie");

(gdb) p stu.name

$2 = 0x602010 ""

(gdb) quit

A debugging session is active.

Inferior 1 [process 12909] will be killed.

Quit anyway? (y or n) y

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值