#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选项,编译命令:html
gcc -g test.c -o test.exe
gdb调试加载文件:程序员
gdb test.exe
出现gdb命令行:redis
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调试命令表:windows
命令 | 解释 | 简写 |
file | 加载一个可执行文件,也能够在运行gdb的时候加载,两种方法都不会运行程序。 | 无 |
list | 列出可执行源码的一部分,一般在程序开始运行前执行,用来设置断点。 | l |
next | 单步调试,不进入函数。 | n |
step | 单步执行,进入函数。 | s |
run | 运行加载了的程序。 | r |
continue | 继续执行程序。 | c |
quit | 退出调试。 | q |
printf | 输出指定的变量的值,变量要在程序运行处可见。 | p |
break | 设置断点。 | b |
info break | 查看断点的信息。 | i b |
delete | 删除断点。 | d |
watch | 监视一个变量的值,一旦发生变化,程序将会被暂停执行。 | wa |
help | 查看gdb的帮助信息。 | h |
(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.
(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;
(gdb) n 12 swap(&a,&b);
(gdb) s swap (a=0x61ff2c, b=0x61ff28) at test.c:4 4 int temp = *a;
(gdb) b 6 Breakpoint 2 at 0x401478: file test.c, line 6.
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;
(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
(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
(gdb) d Delete all breakpoints? (y or n) [answered Y; input not from terminal] (gdb) i b No breakpoints or watchpoints.
(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]
(gdb) q