gdb调试工具的使用

技术交流QQ群:1027579432,欢迎你的加入!

欢迎关注我的微信公众号:CurryCoder的程序人生

1.gdb调试

  • gcc a.c b.c c.c -o app:无法进行gbd调试
  • gcc a.c b.c c.c -o app -g:可以进行gdb调试
    • -g:会保留函数名和变量名

2.启动gdb

  • 启动方法:gdb 可执行程序的名字,例如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时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开始进行调试*********************************/
    
    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

- 当前文件:
    - l
    - l 行号
    - l 函数名
- 非当前文件:
    - l 文件名:行号
    - l 文件名:函数名
- 设置显示的行数:
    - 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.断点相关操作:break/b

  • 给当前文件设置断点:
    • b 行号
    • b 函数名
  • 给非当前文件设置断点:
    • b 文件名:行号
    • b 文件名:函数名
  • 查看断点:info(i) b
  • 删除断点:d num(断点具体的编号,根据i b得到)
    • 删除多个:d num1 num2 或者 d num1-num6
  • 设置断点无效:dis num(断点具体的编号,根据i b得到)
    • 多个断点无效:dis num1 num2 或者 dis num1-num6
  • 设置断点生效:ena num(断点具体的编号,根据i b得到)
    • 多个断点生效:ena num1 num2 或者 ena num1-num6
  • 设置条件断点:b 行号 if 变量名 == 变量的值
  • 重新从程序开头连续执行:r
  • 执行到下一个断点停住,如果后面没有断点,程序执行结束:c
  • 查看任何东西(变量/函数等)的值:p 变量名/函数名
  • 退出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代码调试相关命令

  • 让gdb跑起来:start(运行一行就停止)或r(停在第一个断点的位置)
  • 打印变量的值: p 变量名
  • 打印变量的类型:ptype 变量名
  • 向下单步调试:n(遇到函数不会进入函数体内部)或s(遇到函数会进入函数体内部)
  • 继续运行gdb,停在下一个断点的位置:c
  • 跳出函数体:finish,如果跳不出去,看一下函数体内部的循环中是否有断点,如果有则删掉或设置无效
  • 退出gdb: q
  • 变量的自动显示:display 变量名
  • 取消变量的自动显示:undisplay 编号
    • 编号的查询:i display
  • 从循环体中直接跳出:until(循环体中不能有断点)
  • 直接设置变量等于某一个值: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命令

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,186评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,858评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,620评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,888评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,009评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,149评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,204评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,956评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,385评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,698评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,863评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,544评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,185评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,899评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,141评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,684评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,750评论 2 351