gdb基础知识
文檔
一.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é)
- 上一篇: Python学习笔记(数据类型)
- 下一篇: 数字图像处理总结(冈萨雷斯版)