gdb基础知识

文档

一.gdb打印demo.cpp运行结果

在CMakeLists.txt中添加 

set(CMAKE_BUILD_TYPE Debug)

然后make以后通过gdb filename进入该文件的gdb调试模式,同时使用shell 就可以像终端一样使用shell命令。

例子:

demo.cpp

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class MyPrint{
public:
    void operator()(string test){
        cout<<test<<endl;
    }
};
class Myadd{
public:
    int operator()(int  num1, int num2){
        return num1+num2;
    }
};

void test06(){
    MyPrint m;
    m("hello world");
    Myadd a;
    int res = a(10,100);
    cout<<"==res:"<<res<<endl;

    cout<<"==Myadd()(10,100):"<<Myadd()(10,100)<<endl;
}
int main()
{
    test06();
}

CMakeLists.txt 

cmake_minimum_required(VERSION 3.4.1)
project(Infantry)

set(CMAKE_BUILD_TYPE Debug)
set(SRC_LIST demo.cpp)
add_executable(demo ${SRC_LIST})

mkdir build

cd build

cmake ..

make

gdb demo

run

shell ls

二.gdb的一些基础命令

1.man gdb

在终端下执行 man gdb查看帮助文档

2.help b

在gdb下执行 help b,,就可以得到break的说明使用

3.list查看代码

show listsize查看展示的行数

set listsize 20

设置展示20行

list 7就是表示从第7行开始展示。

4.set args

对函数传参

demo.cpp

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class MyPrint{
public:
    void operator()(string test){
        cout<<test<<endl;
    }
};
class Myadd{
public:
    int operator()(int  num1, int num2){
        return num1+num2;
    }
};
//
//ostream& operator<<(ostream& cout, Person& p){//benzhi  operator<<(cout, p) jianhua  cout<<p;
//    cout<<"==p.m_A:"<<p.m_A<<" ==p.m_B"<<p.m_B;
//    return cout;
//}

void test06(){
    MyPrint m;
    m("hello world");
    Myadd a;
    int res = a(10,100);
    cout<<"==res:"<<res<<endl;

    cout<<"==Myadd()(10,100):"<<Myadd()(10,100)<<endl;
}
int main(int argc, char** argv)
{
    if(argc != 2){
        cout<<"need argv"<<endl;
    }
    cout<<"==argv[0]:"<<argv[0]<<endl;
    cout<<"==argv[1]:"<<argv[1]<<endl;
    cout<<"==argv[1]:"<<argv[2]<<endl;
    test06();
}

set args 对main函数传参 

5.continue

可以简写为c,表示继续执行,可以使用在添加断点后继续执行。

例如在41行设定断点后,run执行,在30行执行断点,c继续执行。

6. delete

简写为d.

d取消所有的断点设置

7. b main

通过函数名字方式加断点,b main就是在main函数加断点, run以后就可以 c继续执行。

8.b linenumber if 语句

满足if 条件就断在此处

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

void test06(){
    int k = 0;
    for(int i = 0 ;i < 10; i++){
        cout<<"==i:"<<i<<endl;
        if(i == 5){
            k++;
        }
    }
}
int main(int argc, char** argv)
{
    if(argc != 2){
        cout<<"need argv"<<endl;
    }
    cout<<"==argv[0]:"<<argv[0]<<endl;
    cout<<"==argv[1]:"<<argv[1]<<endl;
    cout<<"==argv[2]:"<<argv[2]<<endl;
    test06();
}

可看出添加b 29 if i==5时, 在i==5时就停了下来,按c继续执行完后面。

9. info breakpoints

查看所有的断点情况。

10.next

next简写为n,可以单步运行

11.disable linenumber

取消掉第几行断点

可看出 disable 1以后,就把该断点取消掉了(keep后面是n),跟delete还不一样。

enable linenumber就可以恢复。

12.print

print可以打印当前的值(或者打印函数),可看出执行到当前断点位置后,print变量就可以打印出变量的值。

print也可以用来改变变量的值,见13.display有示例。

13.display 

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

void test06(){
    int k = 0;
    for(int i = 0 ;i < 10; i++){
        cout<<"==i:"<<i<<endl;
        if(i == 5){
            k++;
        }
    }
}
int main(int argc, char** argv)
{
    if(argc != 2){
        cout<<"need argv"<<endl;
    }
    cout<<"==argv[0]:"<<argv[0]<<endl;
    cout<<"==argv[1]:"<<argv[1]<<endl;
    cout<<"==argv[2]:"<<argv[2]<<endl;
    test06();
}

display 监测某个变量的值,要注意的是在变量作用域内才能监测

通过b 26设置断点,run以后,在display k,一步一步执行n就可以发现k的值变化了

print k = 10

通过print改变k的值

而delete display或者disable display就是取消监测。

14.jump

jump linenubmer 直接跳到某行

15.gdb查找segmentation fault

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class Student{
public:
    Student(int _age, string _name):age_(_age), name_(_name){}
    int getAge(){return age_;}
    string getName(){return name_;}

private:
    int age_;
    string name_;
};

void test06(){
    int k = 0;
    for(int i = 0 ;i < 10; i++){
        cout<<"==i:"<<i<<endl;
        if(i == 5){
            k++;
        }
    }
}
int main()
{
    cout<<"===gdb error test==="<<endl;
    Student *s1 = new Student(10, "Tom");
    Student *s2 = new Student(15, "Jack");
    s1 = NULL;
    int age1 = s1->getAge();
    string name1 = s1->getName();

    int age2 = s2->getAge();
    string name2 = s2->getName();

    cout<<"name1 is: "<<name1<<endl;
    cout<<"age1 is: "<<age1<<endl;
    cout<<"name2 is: "<<name2<<endl;
    cout<<"age2 is: "<<age2<<endl;
}

backtrace 简写bt,查询到错误代码编号

在frame 0

frame 1

然后就找到是不是s1这个指针的问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值