要調試生成的可執行程序,必須在生成的時候加入-g選項,生成能夠調試的代碼html
例如:gcc -o test a.c b.c -glinux
這樣gcc就會在連接的時候加入一些用於調試的符號redis
在生成能夠調試的可執行程序後,使用gdb命令進入調試模式ubuntu
1 root@ubuntu:/home/leo/test/project/classes# gdb test 2 GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10
3 Copyright (C) 2015 Free Software Foundation, Inc. 4 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
5 This is free software: you are free to change and redistribute it. 6 There is NO WARRANTY, to the extent permitted by law. Type "show copying"
7 and "show warranty" for details. 8 This GDB was configured as "x86_64-linux-gnu". 9 Type "show configuration" for configuration details. 10 For bug reporting instructions, please see: 11 <http://www.gnu.org/software/gdb/bugs/>.
12 Find the GDB manual and other documentation resources online at: 13 <http://www.gnu.org/software/gdb/documentation/>.
14 For help, type "help". 15 Type "apropos word" to search for commands related to "word"... 16 Reading symbols from test...done. 17 (gdb)
此時,程序並無開始運行,在運行程序以前,能夠作一些準備工做sass
1. 設置運行參數session
(gdb) set args 5 設置第一個參數爲5多線程
(gdb) show args
Argument list to give program being debugged when it is started is "5".函數
2. 設置斷點,break命令,簡寫bui
在指定行數設置斷點spa
(gdb) b 8 在第8行設置斷點 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb)
爲指定函數設置斷點
(gdb) b add_int Breakpoint 3 at 0x400660: file math/add_int.cpp, line 5.
在指定行數或函數設置條件變量
(gdb) b add_int if num1 == 1 Breakpoint 4 at 0x400660: file math/add_int.cpp, line 5.
查看斷點
(gdb) info breakpoints Num Type Disp Enb Address What 4 breakpoint keep y 0x0000000000400660 in add_int(int, int) at math/add_int.cpp:5 stop only if num1 == 1 breakpoint already hit 1 time
禁用斷點 disable b 4,禁用4號斷點
啓動斷點 enable b 4,啓用4號斷點
刪除斷點 clear 51 刪除51行的斷點
3. 查看源文件,list命令,簡寫l
(gdb) list 1 #include "stdlib.h"
2 #include "stdio.h"
3
4 int add_int(int, int); 5 float add_float(float, float); 6
7 int main(int argc, char *argv[]) 8 { 9 int value = (*argv[1]) - '0'; 10 printf("Leo is here %d . \n", value); (gdb) list 11
12 int num1 = add_int(1, 2); 13 float num2 = add_float(2.3, 4.5); 14 printf("result is %d %f.", num1, num2); 15
16 getchar(); 17
18 return 0; 19 } (gdb) list Line number 20 out of range; main.cpp has 19 lines. (gdb)
也能夠指定查看的行數list 5,,10,查看從5到10行的源代碼
作好準備工做以後,就能夠真正運行程序了
4. 運行程序,run命令,簡寫r
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 5 能夠直接在run後面能夠跟運行參數,也能夠用set設置運行參數 Starting program: /home/leo/test/project/classes/test 5 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8
8 { (gdb)
5. 查看變量,display命令。在使用斷點命令後,在運行到斷點處,就能夠使用此命令查看變量得值了
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 5 Starting program: /home/leo/test/project/classes/test 5 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8
8 { (gdb) display argc 查看運行參數個數 1: argc = 2 (gdb) display argv[1] 查看第一個運行參數的值 2: argv[1] = 0x7fffffffe38b "5" (gdb)
6. 繼續執行,continue命令,簡寫c
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 5 Starting program: /home/leo/test/project/classes/test 5 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8
8 { (gdb) c Continuing. Leo is here 5 . result is 3 6.800000.
[Inferior 1 (process 4009) exited normally]
7. 修改變量的值,set命令
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 7 Starting program: /home/leo/test/project/classes/test 7 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8
8 { (gdb) display argv[1] 1: argv[1] = 0x7fffffffe38b "7" (gdb) set argv[1] = "8" 將第一個運行參數的值從字符串「7」改成「8」 (gdb) c Continuing. Leo is here 8 . result is 3 6.800000.
[Inferior 1 (process 4009) exited normally]
8. 退出調試,q命令
(gdb) q A debugging session is active. Inferior 1 [process 3997] will be killed. Quit anyway? (y or n) y
9. 查看變量類型,whatis命令
(gdb) whatis num1 type = int (gdb)
10. 查看結構詳細定義,ptype命令
(gdb) ptype u type = struct User { int id char name[20] }
10. 進入函數體(step,簡寫s)和單步調試命令(next,簡寫n)
(gdb) s 6 }2: num2 = 2
1: num1 = 1 (gdb) n main (argc=<optimized out>, argv=<optimized out>) at main.cpp:13
13 float num2 = add_float(2.3, 4.5); (gdb)
11. 查看調用堆棧(backtrace,簡寫bt)
(gdb) bt #0 add_int (num1=num1@entry=1, num2=num2@entry=2) at math/add_int.cpp:6 #1 0x000000000040051b in main (argc=<optimized out>, argv=<optimized out>) at main.cpp:12 (gdb)
12. 獲取當前斷點下的運行狀況(info)
13. 多線程調試
info thread: 獲取當前進程中全部線程信息;
thread thread_id: 進入指定的線程進行調試;
14. 打印指定函數的彙編代碼
disassamble sum
15. 幫助信息
help