GDB用法(二)

预备

测试代码
请添加图片描述

main.cpp
#include <iostream>
#include <vector>
#include <unordered_map>
#include <memory>
#include <time.h>
#include <stdlib.h>
#include "student.h"

using namespace std;

std::string gen_name()
{
    std::vector<std::string> n1 = {"赵", "钱", "孙", "李", "周", "吴", "郑" ,"王"};
    std::vector<std::string> n2 = {"二狗", "三炮", "三", "世明", "土包"};
    int n1Index = rand() % n1.size();
    int n2Index = rand() % n2.size();
    return n1[n1Index] + n2[n2Index];
}

int gen_age()
{
    return rand() % (99) + 1;
}

int main(int argc,char *argv[])
{
    // 根据输入的id, 获得学生的信息
    srand((int)time(NULL));
    std::unordered_map<int, std::shared_ptr<Student>> m; m.clear();
    for(int i = 0; i < 7; i++)
    {
        std::string name = gen_name();
        int age = gen_age();
        std::shared_ptr<Student> s = std::make_shared<Student>(i, name, age);
        m.insert(std::make_pair(i, s));
    }
    
    std::cout << "查询学生的信息" << std::endl;
    std::cout << "输入学生的id=";
    int id;

    while(cin >> id)
    {
        if(m.count(id))
        {
            std::cout << "学生" << id << "的信息是= " << m[id]->ToString() << std::endl;
        } else 
        {
            std::cout << "不存在此学生" << std::endl;
        }
        std::cout << "输入学生的id=";
    }
  

    return 0;
}
Functions/student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_

#include <vector>
#include <string>

class Student
{
private:
    std::string m_name;
    int m_age;
    int m_id;
    
public:
    Student(int id, std::string name, int age);
    
    int GetAge() const;
    std::string GetName() const;
    std::string ToString() const;
};

#endif
Functions/student.cpp
#include "student.h"

Student::Student(int id, std::string name, int age): m_id(id), m_name(name), m_age(age)
{

}

int Student::GetAge() const
{
    return m_age;
}

std::string Student::GetName() const
{
    return m_name;
}

std::string Student::ToString() const
{
    return "学生姓名: " + m_name + " 学生年纪: " + std::to_string(m_age);
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

project(main)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall")

INCLUDE_DIRECTORIES(./)
INCLUDE_DIRECTORIES(./Functions)

AUX_SOURCE_DIRECTORY(./ MAIN)
AUX_SOURCE_DIRECTORY(./Functions FUNCTION)

add_executable(main ${MAIN} ${FUNCTION})

(attach) 调试已经启动的进程

attach pid(进程id)

请添加图片描述

输入学生id, 就会进入断点

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

(info proc) 显示进程信息

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

条件断点

(if) 特定条件下中断

b 断点 if 条件

给定条件为真则暂停运行, 只能进行简单的判断

修改main.cpp
// 37行下增加
   m.insert(std::make_pair(8, std::make_shared<Student>(8, "萧炎", 10)));
   m.insert(std::make_pair(9, std::make_shared<Student>(9, "萧炎", 11)));
   m.insert(std::make_pair(10, std::make_shared<Student>(10, "Tom", 12)));
     

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

(condition/cond) 为已设断点添加条件

condition 断点编号 条件

字符串比较需要用内置的函数

在这里插入图片描述

反复执行

(ignore) 忽略指定次数

ignore 断点编号 次数

修改main.cpp
#include <iostream>
#include <vector>
#include <unordered_map>
#include <memory>
#include <time.h>
#include <stdlib.h>
#include "student.h"

using namespace std;

std::string gen_name()
{
    std::vector<std::string> n1 = {"赵", "钱", "孙", "李", "周", "吴", "郑" ,"王"};
    std::vector<std::string> n2 = {"二狗", "三炮", "三", "世明", "土包"};
    int n1Index = rand() % n1.size();
    int n2Index = rand() % n2.size();
    return n1[n1Index] + n2[n2Index];
}

int gen_age()
{
    return rand() % (99) + 1;
}

int main(int argc,char *argv[])
{

    srand((int)time(NULL));
    std::unordered_map<int, std::shared_ptr<Student>> m; m.clear();
    for(int i = 0; i < 7; i++)
    {
        std::string name = gen_name();
        int age = gen_age();
        std::shared_ptr<Student> s = std::make_shared<Student>(i, name, age);
        m.insert(std::make_pair(i, s));
    }

    m.insert(std::make_pair(8, std::make_shared<Student>(8, "萧炎", 10)));
    m.insert(std::make_pair(9, std::make_shared<Student>(9, "萧炎", 11)));
    m.insert(std::make_pair(10, std::make_shared<Student>(10, "Tom", 12)));

    // 输出所有学生的信息
    int count = 0;
    for(auto iter = m.begin(); iter != m.end(); iter++)
    {
        std::cout << count << " 学生id=" << iter->first << " " << iter->second->ToString() << std::endl;
        count ++;
    }
  

    return 0;
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

忽略了前3次断点, 第4次就能进断点

(finish/fin) 执行完当前函数后暂停

修改main.cpp
#include <iostream>
#include <vector>
#include <unordered_map>
#include <memory>
#include <time.h>
#include <stdlib.h>
#include "student.h"

using namespace std;

std::string gen_name()
{
    std::vector<std::string> n1 = {"赵", "钱", "孙", "李", "周", "吴", "郑" ,"王"};
    std::vector<std::string> n2 = {"二狗", "三炮", "三", "世明", "土包"};
    int n1Index = rand() % n1.size();
    int n2Index = rand() % n2.size();
    return n1[n1Index] + n2[n2Index];
}

int gen_age()
{
    return rand() % (99) + 1;
}

void print(const std::unordered_map<int, std::shared_ptr<Student>>& m)
{
    int count = 0;
    for(auto iter = m.begin(); iter != m.end(); iter++)
    {
        std::cout << count << " 学生id=" << iter->first << " " << iter->second->ToString() << std::endl;
        count ++;
    }
}

int main(int argc,char *argv[])
{

    srand((int)time(NULL));
    std::unordered_map<int, std::shared_ptr<Student>> m; m.clear();
    for(int i = 0; i < 7; i++)
    {
        std::string name = gen_name();
        int age = gen_age();
        std::shared_ptr<Student> s = std::make_shared<Student>(i, name, age);
        m.insert(std::make_pair(i, s));
    }

    m.insert(std::make_pair(8, std::make_shared<Student>(8, "萧炎", 10)));
    m.insert(std::make_pair(9, std::make_shared<Student>(9, "萧炎", 11)));
    m.insert(std::make_pair(10, std::make_shared<Student>(10, "Tom", 12)));

    // 输出所有学生的信息
    print(m);
    std::cout << "全部输出完成" << std::endl;
  

    return 0;
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

只打了一个断点, finsh后, 停在函数的下一句

(until/u) 循环体运行, 直到第几行停止, 或者循环体结束

不在循环体中, util功能和next命令一样, 只是单步执行

until

不带参数的 until命令,可以使 gdb 调试器快速运行完当前的循环体,并运行至循环体外停止,且只能执行好循环体尾部(最后一行), 才会发挥最后, 比如for循环, 第一遍until类似于next, 执行完第一遍, 要执行第二遍的时候, 再执行until, 当前循环体直接运行到完

在这里插入图片描述

until 代码行号

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

(clear) 删除断点

clear和delet区别在于clear不根据断点编号删除, 根据位置删除, detete只能根据断点编号删除

clear 函数名

clear 行号

clear 文件名:行号

clear 文件名:函数名

在这里插入图片描述

(disable/dis) 禁用断点

dis // 禁用所有断点

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

dis 断点编号 // 禁用编号断点, 断点编号可以是多个

在这里插入图片描述

(enable/en) 启用断点

en // 启用所有断点

在这里插入图片描述

en 断点编号 // 断点编号可以是多个

在这里插入图片描述

en once 断点编号

指定的断点只能启用一次, 进入后, 就禁用这个断点

在这里插入图片描述

en delete 断点编号

指定的断点只能启用一次, 进入后, 就删除这个断点

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

(commands) 断点增加额外命令

commands 断点编号

​ 命令

end

给断点增加额外的命令

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

进行组合, 添加条件判断

在这里插入图片描述

(directory/dir) 插入源码目录

directory 目录名

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值