minGW的gdb调试
程序员文章站
2022-07-14 12:40:39
...
MinGW(Minimalist GNU for Windows)提供了一套简单方便的Windows下的基于GCC程序开发环境。MinGW收集了一系列免费的Windows是用的头文件和库文件;同时整合了GNU的工具集,特别是GNU程序开发工具,如经典的gcc,g++,make等。MinGW是完全免费的*软件,它在Windows平台下模拟了Linux下GCC的开发环境,为C++的跨平台开发提供了良好的基础支持,为了在Windows下工作的程序员熟悉Linux下的C++工程组织提供了条件。简单的说,它是个精简的C/C++编译器,它实际上是将经典的开源C语言/C++编译器GCC/G++移植到了Windows下,并且包含了Win32API,因此可以将源代码编译生成Windows下的可执行程序。虽然VC6等编译器,只要点击鼠标就可以完成编译,但它会自动生成一大堆工程文件,让初学者摸不着头脑,而MinGW则只会生成一个可执行文件。
gdb调试测试代码(test.c):
#include <stdio.h>
void swap(int *a,int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int a=1,b=2;
swap(&a,&b);
printf("a = %d ,b = %d\n",a,b);
return 0;
}
要支持调试,在编译时要加入-g选项,编译命令:
gcc -g test.c -o test.exe
gdb调试加载文件:
gdb test.exe
出现gdb命令行:
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 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 "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from D:\mypro\C\test.exe...done.
(gdb)
gdb调试命令表:
命令 | 解释 | 简写 |
---|---|---|
file | 加载一个可执行文件,也可以在运行gdb的时候加载,两种方法都不会运行程序 | 无 |
list | 列出可执行源码的一部分,通常在程序开始运行前执行,用来设置断点 | l |
next | 单步调试,不进入函数 | n |
step | 单步调试,进入函数 | s |
run | 运行加载了的程序 | r |
continue | 继续执行程序 | c |
quit | 退出调试 | q |
输出制定的变量的值,变量要在程序运行处可见 | p | |
break | 设置断点 | b |
info break | 查看断点的信息 | i b |
delete | 删除断点 | d |
watch | 监视一个变量的值,一旦值发生变化,程序将会被暂停执行 | wa |
help | 查看gdb的帮助信息 | h |
1.l命令,列出部分代码:
(gdb) l
2
3 void swap(int *a,int *b){
4 int temp = *a;
5 *a = *b;
6 *b = temp;
7 }
8
9 int main(void)
10 {
11 int a=1,b=2;
(gdb)
继续输入l命令可以继续列出后面的代码,直到文件里代码列完
(gdb) l
12 swap(&a,&b);
13 printf("a = %d ,b = %d\n",a,b);
14 return 0;
15 }(gdb) l
(gdb) Line number 16 out of range; test.c has 15 lines.
2.start命令,开始运行,会停到main入口处:
(gdb) start
Temporary breakpoint 1 at 0x401491: file test.c, line 11.
Starting program: D:\mypro\C/test.exe
[New Thread 8000.0x18c4]
[New Thread 8000.0x2418]
Temporary breakpoint 1, main () at test.c:11
11 int a=1,b=2;
4.n命令:单步调试,不进入函数,跳到第12行
(gdb) n
12 swap(&a,&b);
5.s命令:单步调试,进入函数,跳到第4行
(gdb) s
swap (a=0x61ff2c, b=0x61ff28) at test.c:4
4 int temp = *a;
6.b命令设置断点(b + 第n行代码的行数):
(gdb) b 6
Breakpoint 2 at 0x401478: file test.c, line 6.
7.r命令,运行程序,直到下一个断点就停:
The program being debugged has been started already.
Start it from the beginning? (y or n)
...
Breakpoint 2, swap (a=0x61ff2c, b=0x61ff28) at test.c:6
6 *b = temp;
7.p命令,输出制定的变量的值,变量要在程序运行处可见:
(gdb) p *a
$1 = 2
(gdb) p *b
$2 = 2
(gdb) p a
$3 = (int *) 0x61ff2c
(gdb) p b
$4 = (int *) 0x61ff28
next一下,再看b的值:
(gdb) n
7 }
(gdb) p *b
$5 = 1
8.i b命令,查看断点信息:
(gdb) i b
Num Type Disp Enb Address What
2 breakpoint keep y 0x00401478 in swap at test.c:6
breakpoint already hit 1 time
9.d命令,删除断点,不加断点位置即删除所有断点:
(gdb) d
Delete all breakpoints? (y or n) [answered Y; input not from terminal]
(gdb) i b
No breakpoints or watchpoints.
10.没有断点后,再试一下r命令,可以看到,执行完了程序:
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) [answered Y; input not from terminal]
error return ../../gdb-7.6.1/gdb/windows-nat.c:1275 was 5
Starting program: D:\mypro\C/test.exe
[New Thread 1976.0x1460]
[New Thread 1976.0x5e0]
a = 2 ,b = 1
[Inferior 1 (process 1976) exited normally]
11.q命令,退出gdb:
(gdb) q