MinGW的gdb調試

    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選項,編譯命令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
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;
3.n命令:單步調試,不進入函數,跳到第12行:
(gdb) n
12      swap(&a,&b);
4.s命令:單步調試,進入函數,跳到第4行:
(gdb) s
swap (a=0x61ff2c, b=0x61ff28) at test.c:4
4       int temp = *a;
5.b命令設置斷點(b + 第n行代碼的行數):
(gdb) b 6
Breakpoint 2 at 0x401478: file test.c, line 6.
6.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
相關文章
相關標籤/搜索