网站建设 技术方案,网站排名有什么用,用新华做网站名是否侵权,陕西省建设工程质量安全监督总站网站1.GDB 调试程序
GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。在UNIX平台下做软件#xff0c;GDB这个调试工具有比VC的图形化调试器更强大的功能。所谓“寸有所长#xff0c;尺有所短”就是这个道理。
一般来说#xff0c;GDB主要帮忙你完成下面四个方面的功能…1.GDB 调试程序
GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。在UNIX平台下做软件GDB这个调试工具有比VC的图形化调试器更强大的功能。所谓“寸有所长尺有所短”就是这个道理。
一般来说GDB主要帮忙你完成下面四个方面的功能
1、启动你的程序可以按照你的自定义的要求随心所欲的运行程序。 2、可让被调试的程序在你所指定的调置的断点处停住。断点可以是条件表达式 3、当程序被停住时可以检查此时你的程序中所发生的事。 4、动态的改变你程序的执行环境。
从上面看来GDB和一般的调试工具没有什么两样基本上也是完成这些功能不过在细节上你会发现GDB这个调试工具的强大大家可能比较习惯了图形化的调试工具但有时候命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。
2.调试示例
2.1 示例代码 test.c
#includestdio.h
int func (int n) {int sum 0;int i;for (i 0; i n; i) {sum i;}return sum;
}
int main() {int i;long result;for (i 1; i 100; i) {result i;}printf(result[1-100] %d /n, result);printf(result[1-200] %d /n,func(200));return 0;
}
2.2 编译生成执行文件
qzubuntu:~/network/gdb$ gcc -g test.c -o test2.3 使用GDB进行调试
qzubuntu:~/network/gdb$ gdb test ---------- 启动GDB
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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-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/.
---Type return to continue, or q return to quit---
For help, type help.
Type apropos word to search for commands related to word...
Reading symbols from test...done.
(gdb)
l命令
(gdb) l -------------------- l命令相当于list从第一行开始例出原码。
1 #includestdio.h
2 int func (int n) {
3 int sum 0;
4 int i;
5 for (i 0; i n; i) {
6 sum i;
7 }
8 return sum;
9 }
10 int main() {
(gdb)
11 int i;
12 long result;
13 for (i 1; i 100; i) {
14 result i;
15 }
16 printf(result[1-100] %d /n, result);
17 printf(result[1-200] %d /n,func(200));
18 return 0;
19 }(gdb)
Line number 20 out of range; test.c has 19 lines.
(gdb)
break 设置断点
(gdb) break 11 -------------------- 设置断点在源程序第16行处。
Breakpoint 1 at 0x680: file test.c, line 11.(gdb) break func -------------------- 设置断点在函数func()入口处。
Breakpoint 2 at 0x651: file test.c, line 3.(gdb) info break -------------------- 查看断点信息。
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000000680 in main at test.c:11
2 breakpoint keep y 0x0000000000000651 in func at test.c:3
运行程序r命令
(gdb) r
Starting program: /home/qz/network/gdb/test Breakpoint 1, main () at test.c:13
13 for (i 1; i 100; i) {
(gdb) n --------------------- 单条语句执行next命令简写。
14 result i;
(gdb) n
13 for (i 1; i 100; i) {
(gdb) n
14 result i;
(gdb) n
13 for (i 1; i 100; i) {
(gdb) n
14 result i;
(gdb) n
13 for (i 1; i 100; i) {
(gdb) n
14 result i;
(gdb) n
13 for (i 1; i 100; i) {
(gdb) c
Continuing. --------------------- 继续运行程序continue命令简写。Breakpoint 2, func (n200) at test.c:3
3 int sum 0;
(gdb) n
5 for (i 0; i n; i) {
(gdb) p i
$1 0
(gdb) n
6 sum i;
(gdb) p i
$2 0
(gdb) n
5 for (i 0; i n; i) {
(gdb) p i --------------------- 打印变量i的值print命令简写。
$3 0
(gdb) n
6 sum i;
(gdb) p i
$4 1
(gdb) n
5 for (i 0; i n; i) {
(gdb) n
6 sum i;
(gdb) p i
$5 2
(gdb) p sum
$6 1
(gdb)
bt finish
(gdb) bt --------------------- 查看函数堆栈。
#0 func (n200) at test.c:6
#1 0x00005555555546be in main () at test.c:17
(gdb) finish --------------------- 退出函数。
Run till exit from #0 func (n200) at test.c:6
0x00005555555546be in main () at test.c:17
17 printf(result[1-200] %d /n,func(200));
Value returned is $7 19900
(gdb) c --------------------- 继续运行。
Continuing.
result[1-100] 5050 /n
result[1-200] 19900 /n
[Inferior 1 (process 58205) exited normally] ----------程序输出。
(gdb) q --------------------- 退出gdb。
3 使用GDB
一般来说GDB主要调试的是C/C的程序。要调试C/C的程序首先在编译时我们必须要把调试信息加到可执行文件中。
使用编译器cc/gcc/g的 -g 参数可以做到这一点。如
gcc -g hello.c -o hello
g -g hello.cpp -o hello如果没有-g你将看不见程序的函数名、变量名所代替的全是运行时的内存地址。
当你用-g把调试信息加入之后并成功编译目标代码以后让我们来看看如何用gdb来调试他。
启动GDB的方法有以下几种
1、gdb program program也就是你的执行文件一般在当然目录下。2、gdb program core 用gdb同时调试一个运行程序和core文件core是程序非法执行后core dump后产生的文件。3、db program PID 如果你的程序是一个服务程序那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去并调试他。program应该在PATH环境变量中搜索得到。
4 GDB启动开关
GDB启动时可以加上一些GDB的启动开关详细的开关可以用gdb -help查看。我在下面只例举一些比较常用的参数
-symbols file
-s file
从指定文件中读取符号表。-se file
从指定文件中读取符号表信息并把他用在可执行文件中。-core file
-c file
调试时core dump的core文件。-directory directory
-d directory
加入一个源文件的搜索路径。默认搜索路径是环境变量中PATH所定义的路径。