gdb是什么?
gdb是一个强大的程序调试工具,可以本地调试,也可以跨平台跨机器远程调试。
gdb有啥用?
程序中的错误主要分为语法错误和逻辑错误,语法错误几乎都可以由编译器在编译阶段解决,
逻辑错误可以借助gdb调试工具对程序进行调试,跟踪程序的运行过程,可以在指定代码处暂停运行,
查看当前程序的运行状态,也可以改变某个变量的值或者改变代码的执行顺序,尝试修改逻辑错误。
gdb怎么用?
下面是一个简单的示例,涉及list、break、run、next、step、continue、print、set、bt、quit等常用命令。
loongson@linux:~$ cat hello.c
#include <stdio.h>
int main()
{
int i = 5;
printf("Hello world!\n");
return 0;
}
loongson@linux:~$ gcc hello.c -g -o hello
loongson@linux:~$ gdb hello
This GDB was configured as "loongarch64-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/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
(gdb) list
1 #include <stdio.h>
2
3 int main()
4 {
5 int i = 5;
6
7 printf("Hello world!\n");
8
9 return 0;
10 }
(gdb) break main
Breakpoint 1 at 0x120000710: file hello.c, line 5.
(gdb) run
Starting program: /home/loongson/hello
Breakpoint 1, main () at hello.c:5
5 int i = 5;
(gdb) next
7 printf("Hello world!\n");
(gdb) bt
#0 main () at hello.c:7
(gdb) print i
$1 = 5
(gdb) set var i = 10
(gdb) print i
$2 = 10
(gdb) next
Hello world!
9 return 0;
(gdb) step
10 }
(gdb) continue
Continuing.
[Inferior 1 (process 30585) exited normally]
(gdb) quit