日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

gdb基础知识

發(fā)布時間:2024/7/23 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gdb基础知识 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文檔

一.gdb打印demo.cpp運(yùn)行結(jié)果

在CMakeLists.txt中添加?

set(CMAKE_BUILD_TYPE Debug)

然后make以后通過gdb filename進(jìn)入該文件的gdb調(diào)試模式,同時使用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的一些基礎(chǔ)命令

1.man gdb

在終端下執(zhí)行 man gdb查看幫助文檔

2.help b

在gdb下執(zhí)行 help b,,就可以得到break的說明使用

3.list查看代碼

show listsize查看展示的行數(shù)

set listsize 20

設(shè)置展示20行

list 7就是表示從第7行開始展示。

4.set args

對函數(shù)傳參

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函數(shù)傳參?

5.continue

可以簡寫為c,表示繼續(xù)執(zhí)行,可以使用在添加斷點(diǎn)后繼續(xù)執(zhí)行。

例如在41行設(shè)定斷點(diǎn)后,run執(zhí)行,在30行執(zhí)行斷點(diǎn),c繼續(xù)執(zhí)行。

6. delete

簡寫為d.

d取消所有的斷點(diǎn)設(shè)置

7. b main

通過函數(shù)名字方式加斷點(diǎn),b main就是在main函數(shù)加斷點(diǎn), run以后就可以 c繼續(xù)執(zhí)行。

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繼續(xù)執(zhí)行完后面。

9. info breakpoints

查看所有的斷點(diǎn)情況。

10.next

next簡寫為n,可以單步運(yùn)行

11.disable linenumber

取消掉第幾行斷點(diǎn)

可看出 disable 1以后,就把該斷點(diǎn)取消掉了(keep后面是n),跟delete還不一樣。

enable linenumber就可以恢復(fù)。

12.print

print可以打印當(dāng)前的值(或者打印函數(shù)),可看出執(zhí)行到當(dāng)前斷點(diǎn)位置后,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 監(jiān)測某個變量的值,要注意的是在變量作用域內(nèi)才能監(jiān)測

通過b 26設(shè)置斷點(diǎn),run以后,在display k,一步一步執(zhí)行n就可以發(fā)現(xiàn)k的值變化了

print k = 10

通過print改變k的值

而delete display或者disable display就是取消監(jiān)測。

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這個指針的問題。

總結(jié)

以上是生活随笔為你收集整理的gdb基础知识的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。