gdb調(diào)試工具的使用

技術(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)試
    • -g:會(huì)保留函數(shù)名和變量名

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):
    • b 行號(hào)
    • b 函數(shù)名
  • 給非當(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命令

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末窒升,一起剝皮案震驚了整個(gè)濱河市缀遍,隨后出現(xiàn)的幾起案子慕匠,更是在濱河造成了極大的恐慌,老刑警劉巖域醇,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件台谊,死亡現(xiàn)場(chǎng)離奇詭異蓉媳,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)锅铅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)酪呻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人盐须,你說(shuō)我怎么就攤上這事玩荠。” “怎么了贼邓?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵阶冈,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我塑径,道長(zhǎng)女坑,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任统舀,我火速辦了婚禮匆骗,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘誉简。我一直安慰自己碉就,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布闷串。 她就那樣靜靜地躺著铝噩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪窿克。 梳的紋絲不亂的頭發(fā)上骏庸,一...
    開(kāi)封第一講書(shū)人閱讀 51,292評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音年叮,去河邊找鬼具被。 笑死,一個(gè)胖子當(dāng)著我的面吹牛只损,可吹牛的內(nèi)容都是我干的一姿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼跃惫,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼叮叹!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起爆存,我...
    開(kāi)封第一講書(shū)人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蛉顽,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后先较,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體携冤,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡悼粮,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了曾棕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扣猫。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖翘地,靈堂內(nèi)的尸體忽然破棺而出申尤,到底是詐尸還是另有隱情,我是刑警寧澤衙耕,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布瀑凝,位于F島的核電站,受9級(jí)特大地震影響臭杰,放射性物質(zhì)發(fā)生泄漏粤咪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一渴杆、第九天 我趴在偏房一處隱蔽的房頂上張望寥枝。 院中可真熱鬧,春花似錦磁奖、人聲如沸囊拜。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)冠跷。三九已至,卻和暖如春身诺,著一層夾襖步出監(jiān)牢的瞬間蜜托,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工霉赡, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留橄务,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓穴亏,卻偏偏與公主長(zhǎng)得像蜂挪,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嗓化,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354