1. 程式人生 > >gdb調試小結

gdb調試小結

gdb http std 查看 文件 clas 一次 div include

一、啟動調試

測試程序

  1 #include<iostream>
  2 using namespace std;
  3 class AA
  4 {
  5     void f(){}
  6 };
  7 int main()
  8 {
  9     cout<<sizeof(AA)<<endl;
 10     AA a;
 11     cout<<sizeof(a);
 12     return 0;
 13 }

g++/gcc 命令加-g表示使用了gdb調試:

技術分享

file+生成的可執行文件(test):導入調試文件

技術分享

二、常用調試命令(斷點調試)

r 開始執行(run),如果有斷點則直至第一個斷點(break)

技術分享

b 設置斷點

b num(行號)

技術分享

b function(函數名)

技術分享

tb 斷點位置 設置臨時斷點(只生效一次)

技術分享

i b 檢查斷點信息

b 斷點位置 if 條件 設置條件斷點

ignore bnum count 表示接下來的count編號為bnum的斷點忽略。

d 斷點編號 刪除斷點(delete)

技術分享

c 斷點之後繼續執行到下一個斷點(continue)

n 單步運行(next)

s 進入函數(step)

finish 執行至退出函數

start 停留在main函數的第一條語句

p 參數 打印參數信息

info 文件/函數 文件/函數信息

技術分享

三 、設置觀察點

修改了一下測試文件

  1 #include<iostream>
  2 using namespace std;
  3 class AA
  4 {
  5     void f(){}
  6 };
  7 int main()
  8 {
  9     int b=1;
 10     for(int i=1;i<10;i++)
 11         b++;
 12     cout<<sizeof(AA)<<endl;
 13     AA a;
 14     cout<<sizeof
(a); 15 return 0; 16 }

watch 觀察點 (遇到變量值變化,程序就會停下來)

i watch 查看觀察點

d 序號 刪除觀察點

技術分享

四、退出

q

技術分享

gdb調試小結