GDB調試簡明教程
目錄
創建GDB調試程序
一般關閉編譯優化 -o
顯示所有warnings -Wall
gcc -g -Wall program.c -o program
g++ -g -Wall program1.c program2.c -o program
可以看到加入了調試的test更大,但事實上只是加入行號等信息,沒有加入源代碼。因此源代碼要在同一目錄下
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# gcc test.c -o test -g
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# gcc test.c -o test1
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# ll
total 68
drwxr-xr-x 2 root root 4096 Dec 14 10:07 ./
drwxr-xr-x 9 root root 4096 Dec 14 09:17 ../
-rw-r--r-- 1 root root 310 Dec 14 09:17 bubble.cpp
-rw-r--r-- 1 root root 691 Dec 14 09:17 main.cpp
-rw-r--r-- 1 root root 294 Dec 14 09:17 select.cpp
-rw-r--r-- 1 root root 117 Dec 14 09:17 sort.h
-rwxr-xr-x 1 root root 19736 Dec 14 10:06 test*
-rwxr-xr-x 1 root root 16808 Dec 14 10:07 test1*
-rw-r--r-- 1 root root 657 Dec 14 09:17 test.c
啟動GDB調試
命令
root@iZwz953bcwdl9hfnde2odsZ:~/Linux/lession08# gdb test
運行結果
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
(gdb)
退出GDB調試
quit/q
設置獲取參數
(gdb) set args 10 20
(gdb) show args
結果
Argument list to give program being debugged when it is started is "10 20".
查看當前文件代碼
從當前位置往下顯示
(gdb) l
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
(gdb) l
11 a = 10;
12 b = 30;
13 } else {
14 a = atoi(argv[1]);
15 b = atoi(argv[2]);
16 }
17 printf("a = %d, b = %d\n", a, b);
18 printf("a + b = %d\n", a + b);
19
20 for(int i = 0; i < a; ++i) {
指定行
gdb) l 11
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
11 a = 10;
12 b = 30;
13 } else {
14 a = atoi(argv[1]);
15 b = atoi(argv[2]);
指定函數
(gdb) l main
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
查看其他文件代碼
(gdb) list test.c:10
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
11 a = 10;
12 b = 30;
13 } else {
14 a = atoi(argv[1]);
(gdb) list test.c:main
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
設置顯示行數
(gdb) show listsize
Number of source lines gdb will list by default is 10.
(gdb) set listsize 30
(gdb) show listsize
Number of source lines gdb will list by default is 30.
查看斷點 設置斷點
(gdb) break 10
Breakpoint 1 at 0x14c6: file main.cpp, line 11.
(gdb) break main
Breakpoint 2 at 0x1481: file main.cpp, line 6.
(gdb) break bubble.cpp:10
Breakpoint 3 at 0x1227: file bubble.cpp, line 10.
(gdb) break bubble.cpp:bubbleSort
Breakpoint 4 at 0x11e9: file bubble.cpp, line 6.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000000014c6 in main() at main.cpp:11
2 breakpoint keep y 0x0000000000001481 in main() at main.cpp:6
3 breakpoint keep y 0x0000000000001227 in bubbleSort(int*, int) at bubble.cpp:10
4 breakpoint keep y 0x00000000000011e9 in bubbleSort(int*, int) at bubble.cpp:6
斷點意味着 執行到該行之前
刪除斷點
info/i break/b
設置無效/有效斷點
dis/disable 斷點編號
ena/enable 斷點編號
條件斷點
b/break 10 if i==5
運行GDB調試的程序
start 停在第一行
(gdb) start
Temporary breakpoint 1 at 0x1481: file main.cpp, line 6.
Starting program: /root/Linux/lession08/main
Temporary breakpoint 1, main () at main.cpp:6
6 int main() {
run 遇到斷點為止
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/Linux/lession08/main
冒泡排序之后的數組: 12 22 27 55 67
===================================
Breakpoint 2, main () at main.cpp:21
21 int array1[] = {25, 47, 36, 80, 11};
向下執行
next/n 向下執行 不進入函數體
(gdb) n
22 len = sizeof(array1) / sizeof(int);
(gdb) n
24 selectSort(array1, len);
(gdb) n
27 cout << "選擇排序之后的數組: ";
(gdb) n
28 for(int i = 0; i < len; i++) {
(gdb) n
29 cout << array1[i] << " ";
step/s 進入函數體 運行結束跳出函數體 finish
Breakpoint 4, main () at main.cpp:24
24 selectSort(array1, len);
(gdb) s
selectSort (array=0x555555555690 <__libc_csu_init>, len=32767) at select.cpp:6
6 void selectSort(int *array, int len) {
(gdb) s
8 for (int j = 0; j < len - 1; j++) {
(gdb) s
9 for (int i = j + 1; i < len; i++) {
(gdb) s
10 if (array[j] > array[i]) {
(gdb) s
9 for (int i = j + 1; i < len; i++) {
繼續運行
遇到下一斷點 continue/c
變量操作和自動變量操作
print/p
(gdb) n
8 int array[] = {12, 27, 55, 22, 67};
(gdb) n
9 int len = sizeof(array) / sizeof(int);
(gdb) n
11 bubbleSort(array, len);
(gdb) print array[0]
$1 = 12
display undisplay info/i display
(gdb) display len
2: len = 5
(gdb) s
10 if (array[j] > array[j + 1]) {
1: a = {i = {0, 1045149306}, d = 1.2904777690891933e-08}
2: len = 5
(gdb)
其它操作
循環時 設置值
set var 變量名 = 變量值
until 跳出循環