600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > linux gdb网络调试工具 Linux--gdb调试工具

linux gdb网络调试工具 Linux--gdb调试工具

时间:2021-10-18 05:31:48

相关推荐

linux gdb网络调试工具 Linux--gdb调试工具

gdb是强大的调试工具,下面通过一个交换函数来初步认识他

main.c

#include

#include

void swap(int a, int b){

int t;

t = a;

a = b;

b = t;

}

int main()

{

int a = 3;

int b = 4;

swap(a, b);

printf("a = %d\n b = %d \n", a, b);

}

第一步:编译命令

gcc -g -Wall main.c -o main

解释:

gcc 是编译命令

-g 在输出的目标文件中包含调试信息

-Wall 产生警告信息

-o 指定编译输出文件名默认为a.out(为什么输出文件没有文件扩展名呢?参考另一篇笔记/p/f40188931d8b)

第二步:使用gdb

命令:

gdb main

输出:

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

Copyright (C) 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...done.

(gdb)

这样我们就进入了gdb模式,可以使用gdb命令对程序进行调试

第三步:列出程序行

命令:

(gdb) l

输出:

1 #include

2 #include

3

4 void swap(int a, int b){

5 int t;

6 t = a;

7 a = b;

8 b = t;

9 }

10

11 int main()

12 {

13 int a = 3;

14 int b = 4;

15 swap(a, b);

16 printf("a = %d\n b = %d \n", a, b);

17 }

第四步:加断点 (b + 行号)

命令:

(gdb) b 9

输出:

Breakpoint 1 at 0x400542: file main.c, line 9.

表示在内存0x400542处添加了第一个断点

第五步:运行程序并在断点处停止

命令:

(gdb) r

输出:

Starting program: /home/yourname/WorkSpace/codeblocks/hellolinux/main

Breakpoint 1, swap (a=4, b=3) at main.c:9

9 }

第六步:查看调用栈

命令:

(gdb)bt

输出:

#0 swap (a=4, b=3) at main.c:9

#1 0x000000000040056a in main () at main.c:15

该调用栈中存在两个栈帧(后文解释)

第七步:打印栈帧信息

命令:

p a

输出:

$1 = 4

调用栈描述的是函数之间的调用关系,它由多个栈帧组成每个栈帧对应着没有运行完的函数,栈帧中保存着该函数的信息比如:返回地址,局部变量等。程序执行到断点处时swap函数还没有调用完毕,依然存在调用栈中我们可以使用p命令来查看当前栈帧的的信息。通过上面的例子可以发现通过三变量法swap函数的形参a , b的值发生了交换

第八步:更换当前栈帧

命令:

(gdb)up

输出:

#1 0x000000000040056a in main () at main.c:15

15 swap(a, b);

同上打印出main函数中的a , b

(gdb) p a

$3 = 3

(gdb) p b

$4 = 4

通过上面的的调试结果我们发现实际上a , b的值并没有发生交换

第九步:退出调试

命令:

(gdb) q

输出:

A debugging session is active.

Inferior 1 [process 11524] will be killed.

Quit anyway? (y or n) y

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。