技術(shù)交流QQ群:1027579432他嫡,歡迎你的加入番官!
歡迎關(guān)注我的微信公眾號(hào):CurryCoder的程序人生
1.gdb調(diào)試
- gcc a.c b.c c.c -o app:無(wú)法進(jìn)行g(shù)bd調(diào)試
- gcc a.c b.c c.c -o app -g:可以進(jìn)行g(shù)db調(diào)試
2.啟動(dòng)gdb
- 啟動(dòng)方法:gdb 可執(zhí)行程序的名字,例如gbd app
- 給程序傳參:set args xxx xxx钢属,如下例所示:
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ cat test.c
/************************************************************************
> File Name: test.c
> Author: CurryCoder
> Mail: 1217096231@qq.com
> Created Time: 2020年05月23日 星期六 10時(shí)55分00秒
************************************************************************/
#include<stdio.h>
int main(int argc, const char* argv[]){
printf("args num = %d\n", argc);
for(int i = 0; i < argc; i++){
printf("arg%d: %s\n", i, argv[i]);
}
return 0;
}
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ gcc test.c
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ ls
a.out test.c
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ ./a.out
args num = 1
arg0: ./a.out
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ ./a.out abc def gh 000 666
args num = 6
arg0: ./a.out
arg1: abc
arg2: def
arg3: gh
arg4: 000
arg5: 666
/********************************************使用gbd開(kāi)始進(jìn)行調(diào)試*********************************/
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ gcc test.c -g -o app
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB/args$ gdb app
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from app...done.
(gdb) set args hello world 666
(gdb) r
Starting program: /home/cdl/Cpp_Tutorials/GDB/args/app hello world 666
args num = 4
arg0: /home/cdl/Cpp_Tutorials/GDB/args/app
arg1: hello
arg2: world
arg3: 666
[Inferior 1 (process 30054) exited normally]
(gdb) Quit
3.查看代碼:list
- 當(dāng)前文件:
- l
- l 行號(hào)
- l 函數(shù)名
- 非當(dāng)前文件:
- l 文件名:行號(hào)
- l 文件名:函數(shù)名
- 設(shè)置顯示的行數(shù):
- set listsize n
- show listsize
```
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$ ls
app args insert_sort.c main.c makefile mysort select_sort.c sort sort.h
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$ gdb app
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from app...done.
(gdb) l
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) show listsize 5
Number of source lines gdb will list by default is 10.
(gdb) l
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
(gdb) set listsize 5
(gdb) l 5
3
4 void main()
5 {
6 int i;
7 //????????????
(gdb) Quit
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$ gdb app
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from app...done.
(gdb) l
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) show listsize
Number of source lines gdb will list by default is 10.
(gdb) set listsize 20
(gdb) l
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
21 // selectionSort
22 selectionSort(array, len);
23 // printf
24 printf("Selection Sort:\n");
25 for (i = 0; i < len; ++i)
26 {
27 printf("%d ", array[i]);
28 }
29
30 // insertionSort
(gdb) l 5
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
(gdb) set listsize 10
(gdb)
(gdb)
(gdb) l 5
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) l main
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) l insert_sort.c:15
10 ?????:???????????????
11
12 ***********************************************/
13
14 //??????????(????????)
15 void insertionSort(int *array, int len)
16 {
17 int tmp = 0; // ?洢?????
18 int index = 0; // ???λ??
19 // ????????????
(gdb) l insert_sort.c:insertionSort
11
12 ***********************************************/
13
14 //??????????(????????)
15 void insertionSort(int *array, int len)
16 {
17 int tmp = 0; // ?洢?????
18 int index = 0; // ???λ??
19 // ????????????
20 for (int i = 1; i < len; ++i)
(gdb) l main.c:main
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb)
```
4.斷點(diǎn)相關(guān)操作:break/b
- 給當(dāng)前文件設(shè)置斷點(diǎn):
- 給非當(dāng)前文件設(shè)置斷點(diǎn):
- b 文件名:行號(hào)
- b 文件名:函數(shù)名
- 查看斷點(diǎn):info(i) b
- 刪除斷點(diǎn):d num(斷點(diǎn)具體的編號(hào),根據(jù)i b得到)
- 刪除多個(gè):d num1 num2 或者 d num1-num6
- 設(shè)置斷點(diǎn)無(wú)效:dis num(斷點(diǎn)具體的編號(hào),根據(jù)i b得到)
- 多個(gè)斷點(diǎn)無(wú)效:dis num1 num2 或者 dis num1-num6
- 設(shè)置斷點(diǎn)生效:ena num(斷點(diǎn)具體的編號(hào),根據(jù)i b得到)
- 多個(gè)斷點(diǎn)生效:ena num1 num2 或者 ena num1-num6
- 設(shè)置條件斷點(diǎn):b 行號(hào) if 變量名 == 變量的值
- 重新從程序開(kāi)頭連續(xù)執(zhí)行:r
- 執(zhí)行到下一個(gè)斷點(diǎn)停住徘熔,如果后面沒(méi)有斷點(diǎn),程序執(zhí)行結(jié)束:c
- 查看任何東西(變量/函數(shù)等)的值:p 變量名/函數(shù)名
- 退出gdb:q
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$ gdb app
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from app...done.
(gdb) l
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb)
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
(gdb)
21 // selectionSort
22 selectionSort(array, len);
23 // printf
24 printf("Selection Sort:\n");
25 for (i = 0; i < len; ++i)
26 {
27 printf("%d ", array[i]);
28 }
29
30 // insertionSort
(gdb) b 12
Breakpoint 1 at 0x400934: file main.c, line 12.
(gdb) b 14
Breakpoint 2 at 0x40093e: file main.c, line 14.
(gdb) b 15
Breakpoint 3 at 0x400948: file main.c, line 15.
(gdb) b 17
Breakpoint 4 at 0x400954: file main.c, line 17.
(gdb) b 19
Breakpoint 5 at 0x400989: file main.c, line 19.
(gdb) b 24
Breakpoint 6 at 0x4009aa: file main.c, line 24.
(gdb) b 27
Breakpoint 7 at 0x4009c0: file main.c, line 27.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400934 in main at main.c:12
2 breakpoint keep y 0x000000000040093e in main at main.c:14
3 breakpoint keep y 0x0000000000400948 in main at main.c:15
4 breakpoint keep y 0x0000000000400954 in main at main.c:17
5 breakpoint keep y 0x0000000000400989 in main at main.c:19
6 breakpoint keep y 0x00000000004009aa in main at main.c:24
7 breakpoint keep y 0x00000000004009c0 in main at main.c:27
(gdb) d 1
(gdb) i b
Num Type Disp Enb Address What
2 breakpoint keep y 0x000000000040093e in main at main.c:14
3 breakpoint keep y 0x0000000000400948 in main at main.c:15
4 breakpoint keep y 0x0000000000400954 in main at main.c:17
5 breakpoint keep y 0x0000000000400989 in main at main.c:19
6 breakpoint keep y 0x00000000004009aa in main at main.c:24
7 breakpoint keep y 0x00000000004009c0 in main at main.c:27
(gdb) d 2 3
(gdb) i b
Num Type Disp Enb Address What
4 breakpoint keep y 0x0000000000400954 in main at main.c:17
5 breakpoint keep y 0x0000000000400989 in main at main.c:19
6 breakpoint keep y 0x00000000004009aa in main at main.c:24
7 breakpoint keep y 0x00000000004009c0 in main at main.c:27
(gdb) d 4-7
(gdb) i b
No breakpoints or watchpoints.
(gdb) l main
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) l
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
(gdb) l
21 // selectionSort
22 selectionSort(array, len);
23 // printf
24 printf("Selection Sort:\n");
25 for (i = 0; i < len; ++i)
26 {
27 printf("%d ", array[i]);
28 }
29
30 // insertionSort
(gdb) b 17
Breakpoint 8 at 0x400954: file main.c, line 17.
(gdb) i b
Num Type Disp Enb Address What
8 breakpoint keep y 0x0000000000400954 in main at main.c:17
(gdb) dis 8
(gdb) i b
Num Type Disp Enb Address What
8 breakpoint keep n 0x0000000000400954 in main at main.c:17
(gdb) ena 8
(gdb) i b
Num Type Disp Enb Address What
8 breakpoint keep y 0x0000000000400954 in main at main.c:17
(gdb) r
Starting program: /home/cdl/Cpp_Tutorials/GDB/app
Sort Array:
Breakpoint 8, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb) i b
Num Type Disp Enb Address What
8 breakpoint keep y 0x0000000000400954 in main at main.c:17
breakpoint already hit 1 time
(gdb) d 8
(gdb) b 17 if i==10
Breakpoint 9 at 0x400954: file main.c, line 17.
(gdb) c
Continuing.
Breakpoint 9, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb) p i
$1 = 10
(gdb) p main
$2 = {void ()} 0x4006ff <main>
(gdb) q
5.gdb代碼調(diào)試相關(guān)命令
- 讓gdb跑起來(lái):start(運(yùn)行一行就停止)或r(停在第一個(gè)斷點(diǎn)的位置)
- 打印變量的值: p 變量名
- 打印變量的類(lèi)型:ptype 變量名
- 向下單步調(diào)試:n(遇到函數(shù)不會(huì)進(jìn)入函數(shù)體內(nèi)部)或s(遇到函數(shù)會(huì)進(jìn)入函數(shù)體內(nèi)部)
- 繼續(xù)運(yùn)行g(shù)db淆党,停在下一個(gè)斷點(diǎn)的位置:c
- 跳出函數(shù)體:finish,如果跳不出去酷师,看一下函數(shù)體內(nèi)部的循環(huán)中是否有斷點(diǎn),如果有則刪掉或設(shè)置無(wú)效宁否。
- 退出gdb: q
- 變量的自動(dòng)顯示:display 變量名
- 取消變量的自動(dòng)顯示:undisplay 編號(hào)
- 編號(hào)的查詢(xún):i display
- 從循環(huán)體中直接跳出:until(循環(huán)體中不能有斷點(diǎn))
- 直接設(shè)置變量等于某一個(gè)值:set var 變量名=變量的值
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$ gdb app
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from app...done.
(gdb) list
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb) l
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
(gdb)
21 // selectionSort
22 selectionSort(array, len);
23 // printf
24 printf("Selection Sort:\n");
25 for (i = 0; i < len; ++i)
26 {
27 printf("%d ", array[i]);
28 }
29
30 // insertionSort
(gdb) b 17
Breakpoint 1 at 0x400954: file main.c, line 17.
(gdb) b 22
Breakpoint 2 at 0x400993: file main.c, line 22.
(gdb) b 27
Breakpoint 3 at 0x4009c0: file main.c, line 27.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400954 in main at main.c:17
2 breakpoint keep y 0x0000000000400993 in main at main.c:22
3 breakpoint keep y 0x00000000004009c0 in main at main.c:27
(gdb) r
Starting program: /home/cdl/Cpp_Tutorials/GDB/app
Sort Array:
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb) p i
$1 = 0
(gdb) p array[i]
$2 = 12
(gdb) p len
$3 = 31
(gdb) ptype i
type = int
(gdb) ptype array
type = int [31]
(gdb) n
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb) p i
$4 = 4
(gdb) n
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb) p i
$5 = 5
(gdb) display i
1: i = 5
(gdb) display array[i]
2: array[i] = 35
(gdb) n
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 6
2: array[i] = 67
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 6
2: array[i] = 67
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 7
2: array[i] = 89
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 7
2: array[i] = 89
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 8
2: array[i] = 87
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 8
2: array[i] = 87
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 9
2: array[i] = 65
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 9
2: array[i] = 65
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 10
2: array[i] = 54
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 10
2: array[i] = 54
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 11
2: array[i] = 24
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 11
2: array[i] = 24
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
1: i = 12
2: array[i] = 58
(gdb)
15 for (i = 0; i < len; ++i)
1: i = 12
2: array[i] = 58
(gdb) i display
Auto-display expressions now in effect:
Num Enb Expression
1: y i
2: y array[i]
(gdb) undisplay 1
(gdb) undisplay 2
(gdb) n
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb) p i
$6 = 14
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400954 in main at main.c:17
breakpoint already hit 15 times
2 breakpoint keep y 0x0000000000400993 in main at main.c:22
3 breakpoint keep y 0x00000000004009c0 in main at main.c:27
(gdb) dis 1
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep n 0x0000000000400954 in main at main.c:17
breakpoint already hit 15 times
2 breakpoint keep y 0x0000000000400993 in main at main.c:22
3 breakpoint keep y 0x00000000004009c0 in main at main.c:27
(gdb) n
15 for (i = 0; i < len; ++i)
(gdb)
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb) c
Continuing.
12 5 33 6 10 35 67 89 87 65 54 24 58 92 100 24 46 78 99 200 55 44 33 22 11 71 2 4 86 8 9
Breakpoint 2, main () at main.c:22
22 selectionSort(array, len);
(gdb) p i
$7 = 31
(gdb) step
selectionSort (array=0x7fffffffdbd0, len=31) at select_sort.c:17
17 int min = 0; // ?????С??????λ??
(gdb) l
12 -------------------------------------------------*/
13
14 //???????(????????)
15 void selectionSort(int *array, int len)
16 {
17 int min = 0; // ?????С??????λ??
18 // ???????
19 for (int i = 0; i < len - 1; ++i)
20 {
21 min = i;
(gdb)
22 // ??????
23 for (int j = i + 1; j < len; ++j)
24 {
25 // ?ж?
26 if (array[min] > array[j])
27 {
28 // ??????С??????λ??
29 min = j;
30 }
31 }
(gdb)
32 // ?ж???????????
33 if (min != i)
34 {
35 // ??????μ???С?
36 // ????
37 int tmp = array[min];
38 array[min] = array[i];
39 array[i] = tmp;
40 }
41 }
(gdb) finish
Run till exit from #0 selectionSort (array=0x7fffffffdbd0, len=31)
at select_sort.c:17
main () at main.c:24
24 printf("Selection Sort:\n");
(gdb) q
A debugging session is active.
Inferior 1 [process 5911] will be killed.
Quit anyway? (y or n) y
cdl@cdl-Inspiron-5421:~/Cpp_Tutorials/GDB$ gdb app
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from app...done.
(gdb) l
1 #include <stdio.h>
2 #include "sort.h"
3
4 void main()
5 {
6 int i;
7 //????????????
8 int array[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
9 int array2[] = { 12, 5, 33, 6, 10, 35, 67, 89, 87, 65, 54, 24, 58, 92, 100, 24, 46, 78, 99, 200, 55, 44, 33, 22, 11, 71, 2, 4, 86, 8, 9 };
10
(gdb)
11 //???????鳤??
12 int len = sizeof(array) / sizeof(int);
13 //????????
14 printf("Sort Array:\n");
15 for (i = 0; i < len; ++i)
16 {
17 printf("%d\t", array[i]);
18 }
19 printf("\n");
20
(gdb)
21 // selectionSort
22 selectionSort(array, len);
23 // printf
24 printf("Selection Sort:\n");
25 for (i = 0; i < len; ++i)
26 {
27 printf("%d ", array[i]);
28 }
29
30 // insertionSort
(gdb) b 17
Breakpoint 1 at 0x400954: file main.c, line 17.
(gdb) r
Starting program: /home/cdl/Cpp_Tutorials/GDB/app
Sort Array:
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb) start
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Temporary breakpoint 2 at 0x40070a: file main.c, line 5.
Starting program: /home/cdl/Cpp_Tutorials/GDB/app
Temporary breakpoint 2, main () at main.c:5
5 {
(gdb) c
Continuing.
Sort Array:
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb) p i
$1 = 0
(gdb) p array[i]
$2 = 12
(gdb) set var i=5
(gdb) p i
$3 = 5
(gdb) p array[i]
$4 = 35
(gdb) until
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb)
Breakpoint 1, main () at main.c:17
17 printf("%d\t", array[i]);
(gdb)
15 for (i = 0; i < len; ++i)
(gdb) p i
$5 = 7
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400954 in main at main.c:17
breakpoint already hit 3 times
(gdb) d 1
(gdb) display i
1: i = 7
(gdb) until
19 printf("\n");
1: i = 31
(gdb)
35 67 89 87 65 54 24 58 92 100 24 46 78 99 200 55 44 33 22 11 71 2 4 86 8 9
22 selectionSort(array, len);
1: i = 31
(gdb) q
A debugging session is active.
Inferior 1 [process 6938] will be killed.
Quit anyway? (y or n) y
6.更多gdb命令