GDB是GNU Debuger的簡稱,是GNU發布的一款unix下應用程序調試工具。它被廣泛使用在各個產家各種應用中。GDB和所有常用的調試工具一樣,它的主要功能有:監視變量的值、設置斷點及單步執行。
注意,在源程序編譯時,要使用gcc -g 或 cc -g 或 g++ -g將源程序編譯成可執行文件,然後才能使用gdb進行調試。只有這樣,生成的可執行文件才包含調試信息。
參考別人的寫一個簡單的c程序,在linux下使用gcc編譯成可執行文件,然後使用gdb進行調試。
GDB調試程序用法 http://www.linuxidc.com/Linux/2013-06/86044.htm
GDB+GDBserver無源碼調試Android 動態鏈接庫的技巧 http://www.linuxidc.com/Linux/2013-06/85936.htm
使用hello-gl2建立ndk-GDB環境(有源碼和無源碼調試環境) http://www.linuxidc.com/Linux/2013-06/85935.htm
在Ubuntu上用GDB調試printf源碼 http://www.linuxidc.com/Linux/2013-03/80346.htm
Linux下用GDB調試可加載模塊 http://www.linuxidc.com/Linux/2013-01/77969.htm
Ubuntu下使用GDB斷點Go程序 http://www.linuxidc.com/Linux/2012-06/62941.htm
程序源代碼如下
使用gcc -g變成生成可執行文件miki,方法為 gcc -g miki.c -o miki
使用gdb開始調試miki應用,如下:
01.$gdb miki
02.GNU gdb (GDB) CentOS (7.0.1-42.el5.centos)
03.Copyright (C) 2009 Free Software Foundation, Inc.
04.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
05.This is free software: you are free to change and redistribute it.
06.There is NO WARRANTY, to the extent permitted by law. Type "show copying"
07.and "show warranty" for details.
08.This GDB was configured as "x86_64-RedHat-linux-gnu".
09.For bug reporting instructions, please see:
10.<http://www.gnu.org/software/gdb/bugs/>…
11.Reading symbols from /u01/home/Oracle/miki…done.
12.(gdb) l 1
13.1 #include <stdio.h>
14.2
15.3 int func(int n)
16.4 {
17.5 int sum=0,i;
18.6 for(i=0; i<n; i++)
19.7 {
20.8 sum+=i;
21.9 }
22.10 return sum;
23.(gdb)
24.11 }
25.12
26.13
27.14 main()
28.15 {
29.16 int i;
30.17 long result = 0;
31.18 for(i=1; i<=10; i++)
32.19 {
33.20 result += i;
34.(gdb)
35.21 }
36.22
37.23 printf("result[1-10] = %d \n\r", result );
38.24 printf("result[1-5] = %d \n\r", func(5) );
39.25 }
40.(gdb)
41.Line number 26 out of range; miki.c has 25 lines.
42.(gdb) r
43.Starting program: /u01/home/oracle/miki
44.warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
45.result[1-10] = 55
46.result[1-5] = 10
47.Program exited with code 023.
l 1 表示顯示源碼第一行開始的10行記錄,可以寫成list 1
l 表示顯示源碼中間的前後10行記錄,可以寫成list
空白回車 表示重復上一次命令操作
r 表示程序開始運行
01.(gdb) b 23
02.Breakpoint 1 at 0x4004f2: file miki.c, line 23.
03.(gdb) info b
04.Num Type Disp Enb Address What
05.1 breakpoint keep y 0x00000000004004f2 in main at miki.c:23
06.(gdb) i b
07.Num Type Disp Enb Address What
08.1 breakpoint keep y 0x00000000004004f2 in main at miki.c:23
09.(gdb) break func
10.Breakpoint 2 at 0x40049f: file miki.c, line 5.
11.(gdb) info break
12.Num Type Disp Enb Address What
13.1 breakpoint keep y 0x00000000004004f2 in main at miki.c:23
14.2 breakpoint keep y 0x000000000040049f in func at miki.c:5
15.
16.
17.(gdb) i b
18.Num Type Disp Enb Address What
19.1 breakpoint keep y 0x00000000004004f2 in main at miki.c:23
20.2 breakpoint keep y 0x000000000040049f in func at miki.c:5
21.3 breakpoint keep y 0x000000000040049f in func at miki.c:2
22.(gdb) d 2
23.(gdb) i b
24.Num Type Disp Enb Address What
25.1 breakpoint keep y 0x00000000004004f2 in main at miki.c:23
26.3 breakpoint keep y 0x000000000040049f in func at miki.c:2
27.(gdb) delete 3
28.(gdb) i b
29.Num Type Disp Enb Address What
30.1 breakpoint keep y 0x00000000004004f2 in main at miki.c:2
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-07/104296p2.htm