MinGW(Minimalist GNU for Windows)提供了一套简单方便的Windows下的基于GCC程序开发环境。MinGW收集了一系列免费的Windows是用的头文件和库文件;同时整合了GNU的工具集,特别是GNU程序开发工具,如经典的gcc,g++,make等。MinGW是完全免费的自由软件,它在Windows平台下模拟了Linux下GCC的开发环境,为C++的跨平台开发提供了良好的基础支持,为了在Windows下工作的程序员熟悉Linux下的C++工程组织提供了条件。
int main(void) { int a=1,b=2; swap(&a,&b); printf("a = %d ,b = %d\n",a,b); return 0; }
要支持调试,在编译时要加入-g选项,编译命令:
1
gcc -g test.c -o test.exe
gdb调试加载文件:
1
gdb test.exe
出现gdb命令行:
1 2 3 4 5 6 7 8 9 10 11
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
print
输出制定的变量的值,变量要在程序运行处可见
p
break
设置断点
b
info break
查看断点的信息
i b
delete
删除断点
d
watch
监视一个变量的值,一旦值发生变化,程序将会被暂停执行
wa
help
查看gdb的帮助信息
h
l命令,列出部分代码:
1 2 3 4 5 6 7 8 9 10 11 12
(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命令可以继续列出后面的代码,直到文件里代码列完
1 2 3 4 5 6
(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.
start命令,开始运行,会停到main入口处:
1 2 3 4 5 6 7 8
(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;
n命令:单步调试,不进入函数,跳到第12行
1 2
(gdb) n 12 swap(&a,&b);
s命令:单步调试,进入函数,跳到第4行
1 2 3
(gdb) s swap (a=0x61ff2c, b=0x61ff28) at test.c:4 4 int temp = *a;
b命令设置断点(b + 第n行代码的行数):
1 2
(gdb) b 6 Breakpoint 2 at 0x401478: file test.c, line 6.
r命令,运行程序,直到下一个断点就停:
1 2 3 4 5
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;
p命令,输出制定的变量的值,变量要在程序运行处可见:
1 2 3 4 5 6 7 8
(gdb) p *a $1 = 2 (gdb) p *b $2 = 2 (gdb) p a $3 = (int *) 0x61ff2c (gdb) p b $4 = (int *) 0x61ff28
next一下,再看b的值:
1 2 3 4
(gdb) n 7 } (gdb) p *b $5 = 1
i b命令,查看断点信息:
1 2 3 4
(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
d命令,删除断点,不加断点位置即删除所有断点:
1 2 3 4
(gdb) d Delete all breakpoints? (y or n) [answered Y; input not from terminal] (gdb) i b No breakpoints or watchpoints.
没有断点后,再试一下r命令,可以看到,执行完了程序:
1 2 3 4 5 6 7 8 9
(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]