栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

2、gdb调试

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2、gdb调试

再vs中有Debug和Release版本生成解决方案,Debug生成的exe文件比Release生成的exe文件大,原因是Debug版本的带有调试信息可以对程序进行调试,而Release原则上不能进行调试。

1、gdb中常用命令

调试对象:Windows:exe
Linux: main
调试程序是对可执行文件进行调试。
编译时需要增加调试信息 -g //ex gcc -o main Test.c -g
l 显示代码
b + 行号 设置断点
info break 查看断点信息
r 启动程序
n 单步执行
p 打印
q 退出

s进入函数

finish跳出函数

每个程序运行后都会打开三个文件:
stdin 键盘,标准输入文件
stdout 屏幕,标准输出文件
stderr 屏幕,标准错误输出
他们三个的类型都是File*

2、gdb调试
stu@stu-virtual-machine:~/Test/gdbex$ ls
main  makefile  Test.c  Test.o
stu@stu-virtual-machine:~/Test/gdbex$ gdb main
GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1
Copyright (C) 2020 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 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:
.
Find the GDB manual and other documentation resources online at:
    .

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...
(No debugging symbols found in main)
(gdb) Quit
(gdb) l
No symbol table is loaded.  Use the "file" command.
(gdb) 


会发现没有(No debugging symbols found in main),要调试的话要编译成Debug版本,默认条件下编译的不是Debug版本

3、将源码编译为Debug版本
stu@stu-virtual-machine:~/Test/gdbex$ ls -l
总用量 32
-rwxrwxr-x 1 stu stu 16920 10月 16 01:02 main
-rw-rw-r-- 1 stu stu    94 10月 16 01:02 makefile
-rw-rw-r-- 1 stu stu   351 10月 16 00:29 Test.c
-rw-rw-r-- 1 stu stu  2288 10月 16 01:02 Test.o
stu@stu-virtual-machine:~/Test/gdbex$ gcc -o main Test.c -g
stu@stu-virtual-machine:~/Test/gdbex$ ls -l
总用量 32
-rwxrwxr-x 1 stu stu 19640 10月 16 12:13 main
-rw-rw-r-- 1 stu stu    94 10月 16 01:02 makefile
-rw-rw-r-- 1 stu stu   351 10月 16 00:29 Test.c
-rw-rw-r-- 1 stu stu  2288 10月 16 01:02 Test.o

gcc -o main Test.c -g

编译成Debug版本之后会发现,main由原来的16920变为19640变大了,里面增加了调试信息。

stu@stu-virtual-machine:~/Test/gdbex$ gcc -o main Test.c -g
stu@stu-virtual-machine:~/Test/gdbex$ gdb main
GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1
Copyright (C) 2020 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 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:
.
Find the GDB manual and other documentation resources online at:
    .

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...
(gdb) 

如上显示才正常。

4、调试
(gdb) l
1	#include
2	#include
3	int main()
4	{
5	    while(1)
6	    {
7	        printf("please entern");
8	        char buffer[128]={};
9	        fgets(buffer,128,stdin);//从标准输入设备读取到buffer
10	        if(strcmp(buffer,"end")==0)//如果以"end"结束就退出
(gdb) l
11	        {
12	            break;
13	        }
14	        printf("read:%s",buffer);
15	    }
16	}
(gdb) b 9
Breakpoint 1 at 0x1279: file Test.c, line 9.
(gdb) b 14
Breakpoint 2 at 0x12c4: file Test.c, line 14.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001279 in main at Test.c:9
2       breakpoint     keep y   0x00000000000012c4 in main 
                                                   at Test.c:14
(gdb) r
Starting program: /home/stu/Test/gdbex/main 
please enter

Breakpoint 1, main () at Test.c:9
9	        fgets(buffer,128,stdin);//从标准输入设备读取到buffer
(gdb) n
hello
10	        if(strcmp(buffer,"end")==0)//如果以"end"结束就退出
(gdb) p
The history is empty.
(gdb) p buffer
$1 = "hellon", '00' 
(gdb) n

Breakpoint 2, main () at Test.c:14
14	        printf("read:%s",buffer);
(gdb) n
read:hello
6	    {
(gdb) n
7	        printf("please entern");
(gdb) n
please enter
8	        char buffer[128]={};
(gdb) n

Breakpoint 1, main () at Test.c:9
9	        fgets(buffer,128,stdin);//从标准输入设备读取到buffer
(gdb) n
end
10	        if(strcmp(buffer,"end")==0)//如果以"end"结束就退出
(gdb) p buffer
$2 = "endn", '00' 
(gdb) q
A debugging session is active.

	Inferior 1 [process 4232] will be killed.
Quit anyway? (y or n) y
5、进入函数

s进入函数

finish退出函数

(gdb) l
1	#include
2	#include
3	int add(int x,int y)
4	{
5	    return x+y;
6	}
7	int main()
8	{
9	    int a=2,b=6;
10	    int f=add(a,b);
(gdb) l
11	    while(1)
12	    {
13	        printf("please entern");
14	        char buffer[128]={};
15	        fgets(buffer,128,stdin);//从标准输入设备读取到buffer
16	        buffer[strlen(buffer)-1]='';//让buffer中的最后一个元素变为0或者0就行了,因为读入的时候会加入一个"n"在末尾
17	        //if(strcmp(buffer),"endn"==0)
18	        //if(strncmp(buffer),"end",3)==0)也可以,比较前三个字符
19	        if(strcmp(buffer,"end")==0)//如果以"end"结束就退出
20	        {
(gdb) b 10
Breakpoint 1 at 0x1233: file Test.c, line 10.
(gdb) r
Starting program: /home/stu/Test/gdbex/main 

Breakpoint 1, main () at Test.c:10
10	    int f=add(a,b);
(gdb) s
add (x=0, y=256) at Test.c:4
4	{
(gdb) n
5	    return x+y;
(gdb) p x
$1 = 2
(gdb) p y
$2 = 6
(gdb) p &x
$3 = (int *) 0x7fffffffdedc
(gdb) finsh
Undefined command: "finsh".  Try "help".
(gdb) finish
Run till exit from #0  add (x=2, y=6) at Test.c:5
0x0000555555555248 in main () at Test.c:10
10	    int f=add(a,b);
Value returned is $4 = 8
(gdb) 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/334348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号