• linux gcc专题(三) gdb调试


    gdb是gcc编译器的调试桥,类似于adb,可以起到断点调试的作用。

    一、gdb安装

    查看版本号

    gdb -v
    
    • 1

    如果没有,安装命令:

    # centOS安装 gdb
    yum -y install gdb
    # Ubuntu安装 gdb
    apt install gdb
    
    • 1
    • 2
    • 3
    • 4

    二、gdb使用

    2.1、-g生成.out执行文件

    使用gdb之前,要求对文件进行编译时增加-g参数,加了这个参数过后生成的编译文件会大一些,这是因为增加了gdb调试内容。相关源代码参考[四、文档附录](# 四、文档附录)

    gcc test.c -o a.out -g
    
    • 1
    image-20220127202810611

    2.2、开启调试

    gdb a.out
    
    • 1

    然后输入 start,启动程序

    image-20220127202810611

    2.3、list (查看前后10行代码)

    image-20220127202810611

    2.4、n/next (执行下一步)

    image-20220127202810611

    2.5、ptype (查看当前变量类型)

    输入ptype q查看当前变量q的类型

    image-20220127202810611

    2.6、p/print (打印变量的值)

    使用p打印QueueLength(q)长度

    p QueueLength(q)
    
    • 1
    image-20220127202810611

    2.7、s/step (进入到函数内部)

    如果是系统函数,进去之后出不来了,这时用until+行号直接执行到行号处

    image-20220127202810611

    2.8、finish (退出当前函数调用)

    image-20220127202810611

    2.9、b num (打断点)

    b 180
    
    • 1
    image-20220726100804694

    此外,打断点还可以写判断语句,例如循环到i=5打断点

    b 20 if i = 5
    
    • 1

    2.10、info b(查看打了哪些断点)

    info b
    
    • 1
    image-20220127202810611

    2.11、continue (进入下一个断点)

    image-20220127202810611

    2.12、delete num (删除断点)

    image-20220127202810611

    2.13、bt (查看栈帧)

    从main函数进入到子函数,会堆栈

    bt
    
    • 1
    image-20220127202810611

    2.14、frame num (切换栈帧)

    当前在第0个栈帧,切换到main函数所在的栈帧

    frame 1
    
    • 1
    image-20220127202810611

    2.15、display跟踪栈帧 (每执行一步,会打印一次)

    display QueueLength(q)
    
    • 1
    image-20220127202810611

    2.16、undisplay num (取消跟踪栈帧)

    undisplay 1
    
    • 1

    2.17、quit (退出调试)

    image-20220127202810611

    三、其他指令

    • run/r: 运行程序

    • run:使用run查找段错误出现位置。

    • set args: 设置main函数命令行参数 (在 start、run 之前)

    • run 字串1 字串2 …: 设置main函数命令行参数

    四、文档附录

    本文调试用到的test.c源代码,你也可以使用其他的。

    #include 
    #include 
    
    #define MAX_QUEUE_SIZE 100
    
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -1
    #define INFEASIBLE 0
    
    typedef int Status;
    typedef int QElemType;
    
    typedef struct {
        QElemType *base;  //存储空间基地址
        int front;  //头指针
        int rear;  //尾指针
        int QueueSize;  //队列容量
    }SqQueue;
    
    Status InitQueue(SqQueue *);
    Status DestroyQueue(SqQueue *);
    Status ClearQueue(SqQueue *);
    Status QueueEmpty(SqQueue );
    int QueueLength(SqQueue);
    Status GetHead(SqQueue, QElemType *);
    Status EnQueue(SqQueue *, QElemType);
    Status DeQueue(SqQueue *, QElemType *);
    void QueueTraverse(SqQueue , void(*vi)(QElemType));
    
    /**
     *  * 操作结果:构造一个空队列 Q
     *   * @param e
     *    * @return
     *     */
    Status InitQueue(SqQueue *Q) {
        Q->base = (QElemType *)malloc(MAX_QUEUE_SIZE * sizeof(SqQueue));
        if (!Q->base) {
            exit(OVERFLOW);
        }
        Q->front = Q->rear = 0;  //头指针尾指针置为零,队列为空
        return OK;
    }
    
    /**
     *  * 初始条件:队列 Q 存在
     *   * 操作结果:队列 Q 被销毁
     *    * @param Q
     *     * @return
     *      */
    Status DestroyQueue(SqQueue *Q) {
        if (Q->base) {
            free(Q->base);
        }
        Q->base = NULL;
        Q->front = Q->rear = 0;
    }
    
    /**
     *  * 初始条件:队列 Q 存在
     *   * 操作结果:清空队列 Q
     *    * @param Q
     *     * @return
     *      */
    Status ClearQueue(SqQueue *Q) {
        Q->front = Q->rear = 0;
    }
    
    /**
     *  * 初始条件:队列 Q 存在
     *   * 操作结果:若 Q 为空队列,则返回 true,否则返回 false
     *    * @param Q
     *     * @return
     *      */
    Status QueueEmpty(SqQueue Q) {
        if (Q.front == Q.rear) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    /**
     *  * 初始条件:队列 Q 存在
     *   * 操作结果:返回 Q 中元素个数,即队列长度
     *    * @param Q
     *     * @return
     *      */
    int QueueLength(SqQueue Q) {
        return (Q.rear - Q.front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
    }
    
    /**
     *  * 初始条件:队列 Q 存在且非空
     *   * 操作结果:返回 Q 中队头元素
     *    * @param Q
     *     * @param e
     *      * @return
     *       */
    Status GetHead(SqQueue Q, QElemType *e) {
        if (Q.front == Q.rear) {
            return ERROR;
        }
        *e = Q.base[Q.front];
        return OK;
    }
    
    /**
     *  * 初始条件:队列 Q 存在
     *   * 操作结果:插入入元素 e 为 Q 的新队尾元素
     *    * @param Q
     *     * @param e
     *      * @return
     *       */
    Status EnQueue(SqQueue *Q, QElemType e) {
        if ((Q->rear + 1) % MAX_QUEUE_SIZE == Q->front) {
            return ERROR;  //队尾指针在循环意义上加 1 后等于头指针,表示队满
        }
        Q->base[Q->rear] = e;
        Q->rear = (Q->rear + 1) % MAX_QUEUE_SIZE;  //队尾指针加 1
        return OK;
    }
    
    /**
     *  * 初始条件:队列 Q 存在且非空
     *   * 操作结果:删除 Q 的队头元素,且用 e 返回
     *    * @param Q
     *     * @param e
     *      * @return
     *       */
    Status DeQueue(SqQueue *Q, QElemType *e) {
        if (Q->front == Q->rear) {
            return ERROR;
        }
        *e = Q->base[Q->front];
        Q->front = (Q->front + 1) % MAX_QUEUE_SIZE;  //队头指针加 1
        return OK;
    }
    
    /**
     *  * 初始条件:队列 Q 存在且非空
     *   * 操作结果:从队头到队尾,依次对遍历队列中每个元素
     *    * @param Q
     *     * @param vi
     *      */
    void QueueTraverse(SqQueue Q, void(*vi)(QElemType)) {
        int i = Q.front;
        while (i != Q.rear) {
            vi(Q.base[i]);  //遍历
            i = (i + 1) % MAX_QUEUE_SIZE;
        }
        printf("\n");
    }
    
    /**
     *  * 遍历函数
     *   * @param e
     *    */
    void vi(QElemType e) {
        printf("%d ",e);
    }
    
    /**
     *  * 主函数,测试程序
     *   * @return
     *    */
    int main() {
        SqQueue q;
        QElemType e;
    
        InitQueue(&q);
        printf("队列的长度:%d\n", QueueLength(q));
        printf("队列是否为空:%d\n", QueueEmpty(q));
        EnQueue(&q, 3);
        EnQueue(&q, 4);
        EnQueue(&q, 5);
        EnQueue(&q, 6);
        QueueTraverse(q, vi);
        printf("队列的长度:%d\n", QueueLength(q));
        printf("队列是否为空:%d\n", QueueEmpty(q));
        GetHead(q, &e);
        printf("队列的头元素:%d\n", e);
        DeQueue(&q, &e);
        QueueTraverse(q, vi);
        ClearQueue(&q);
        printf("队列的长度:%d\n", QueueLength(q));
        printf("队列是否为空:%d\n", QueueEmpty(q));
    
        DestroyQueue(&q);
    }
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
  • 相关阅读:
    AndroidSDK开发6我用kotlin写了一个简单sdk
    基于51单片机的简易电梯系统的设计
    java毕业设计师生健康信息管理系统mybatis+源码+调试部署+系统+数据库+lw
    【深度学习】 Python 和 NumPy 系列教程(三):Python容器:1、列表List详解(初始化、索引、切片、更新、删除、常用函数、拆包、遍历)
    VUE笔记
    Matlab顶级期刊配色工具Rggsci
    Electron + vue搭建项目
    【Android笔记41】使用Android实现一个简易版本的购物车小案例
    QXlsx 使用
    B/S端界面控件DevExtreme v22.1新版亮点 - HTML/Markdown编辑器升级
  • 原文地址:https://blog.csdn.net/liuxingyuzaixian/article/details/125998172