• Linux GDB调试


    1、什么是GDB

    在这里插入图片描述

    2、准备工作

    在这里插入图片描述

    3、GDB启动、退出、查看代码

    在这里插入图片描述
    输入:

    gcc test.c -o test -g
    gcc test.c -o test1
    ll -h test test1
    
    • 1
    • 2
    • 3

    输出:

    -rwxr-xr-x 1 petri petri 20K Apr 21 18:50 test*
    -rwxr-xr-x 1 petri petri 17K Apr 21 18:50 test1*
    
    • 1
    • 2

    可以看出加入-g文件变大了。

    进入gdb调试:

    gdb test
    
    • 1
    (gdb) set args 10 20
    (gdb) show args
    Argument list to give program being debugged when it is started is "10 20".
    (gdb) help (查看帮助文档)
    (gdb) q (退出)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    关于list:

    -g 选项的作用是在可执行文件中加入源代码信息,比如可执行文件中第几条机器指令对应源代码的第几行,并没有把源代码嵌入可执行文件中。所以源代码文件不可以没有。

    1、显示
    list 一次显示10行:

    (gdb) list
    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
    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) {
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    (gdb) list 10 (第十行开始显示)
    
    (gdb) list main (main函数开始显示)
    
    (gdb) list bubble.cpp:10  (通过文件名:行号)
    
    (gdb) list select.cpp:selectSort (通过文件名:函数) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、设置显示行数

    (gdb) show list
    Number of source lines gdb will list by default is 10.
    (gdb) set list 20
    (gdb) show list
    Number of source lines gdb will list by default is 20.
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、GDB打断点

    在这里插入图片描述

    (gdb) break 10
    Breakpoint 1 at 0x11b2: file test.c, line 10
    
    (gdb) d 1
    
    (gdb) i b
    No breakpoints or watchpoints.
    
    (gdb) break 11
    Breakpoint 2 at 0x11b2: file test.c, line 11.
    
    (gdb) disable 2
    
    (gdb) enable 2
    
    (gdb) info break
    Num     Type           Disp Enb Address            What
    2       breakpoint     keep y   0x00000000000011b2 in main at test.c:11
    
    (gdb) break 21 if i=3
    Breakpoint 3 at 0x1231: file test.c, line 21.
    
    (gdb) info break
    Num     Type           Disp Enb Address            What
    2       breakpoint     keep y   0x00000000000011b2 in main at test.c:11
    3       breakpoint     keep y   0x0000000000001231 in main at test.c:21
            stop only if i=3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    5、GDB调试命令

    在这里插入图片描述

    (gdb) start
    Temporary breakpoint 4 at 0x1189: file test.c, line 6.
    Starting program: /home/petri/lesson01/03_gdb/test 
    
    Temporary breakpoint 4, main (argc=0, argv=0x0) at test.c:6
    6       int main(int argc, char* argv[]) {
    
    (gdb) run
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    Starting program: /home/petri/lesson01/03_gdb/test 
    argc = 1
    
    Breakpoint 2, main (argc=1, argv=0x7ffffffedc78) at test.c:10
    10          if(argc < 3) {
    
    (gdb) continue
    Continuing.
    a = 10, b = 30
    a + b = 40
    
    Breakpoint 3, main (argc=1, argv=0x7ffffffedc78) at test.c:21
    21              printf("i = %d\n", i);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    test.c的代码:

    #include 
    #include 
    
    int test(int a);
    
    int main(int argc, char* argv[]) {
        int a, b;
        printf("argc = %d\n", argc);
    
        if(argc < 3) {
            a = 10;
            b = 30;
        } else {
            a = atoi(argv[1]);
            b = atoi(argv[2]);
        }
        printf("a = %d, b = %d\n", a, b);
        printf("a + b = %d\n", a + b);
    
        for(int i = 0; i < a; ++i) {
            printf("i = %d\n", i);
            // 函数调用
            int res = test(i);
            printf("res value: %d\n", res);
        }
    
        printf("THE END !!!\n");
        return 0;
    }
    
    int test(int a) {
        int num = 0;
        for(int i = 0; i < a; ++i) {
            num += i;
        }
        return num;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    记录Kettle连不上mysql8
    合作式智能运输系统通信架构
    多目标应用:多目标蜣螂优化算法求解多旅行商问题(Multiple Traveling Salesman Problem, MTSP)
    温馨提示:不注意这几点,PDT(产品开发团队)就得散!
    HCNA VRP基础
    LeetCode算法二叉树—116. 填充每个节点的下一个右侧节点指针
    火车头采集器文章组合聚合
    系统与进程
    一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
    HTTP的理解和各个版本的介绍优化
  • 原文地址:https://blog.csdn.net/weixin_43349440/article/details/138041524