#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