gdb調(diào)試的簡單操作總結(jié)
gdb是一個命令行下的、功能強大的調(diào)試器。
在學(xué)習(xí) gdb 前,我們要知道幾個最基本的 cmd 命令。
cmd
首先,對于 win10 系統(tǒng),我們按 Windows + R 鍵,打開運行窗口,在里面輸入 cmd
,這樣就可以打開 cmd 命令窗口了,是一個黑框。
接下來是一些最基本的命令。
F:
打開 F 盤;E:
打開 E 盤,等等
dir
查看文件夾中的文件
cd XXX.XXX
打開 XXX.XXX
文件
cd..
返回上一級(文件夾)
cls
清屏(如果你覺得屏幕中的信息太多的話可以用這個命令)
ping [網(wǎng)址]
查看一個網(wǎng)站的信息(中括號不用打)
gdb 調(diào)試前的操作
在進行 gdb 調(diào)試之前,請先確保你安裝了 gdb、g++、gcc 等。
首先,在 cmd 中進入到你的代碼的文件夾頁面,我的代碼在 F 盤,如下圖。
現(xiàn)在,我只要進入這個地址就行了。
要進行調(diào)試的文件是 ceshi.cpp
,我們先在 cmd 中輸入 g++ -g ceshi.cpp -o ceshi.exe
命令,然后等待一會,文件夾中就會出現(xiàn)一個名為 ceshi.exe
的文件。
之后輸入 gdb
命令,會變成這樣。
現(xiàn)在,我們用 file
命令來將剛才生成的 .exe
文件載入 gdb 調(diào)試器中,輸入 file ceshi.exe
(這里的 ceshi.exe
是剛才生成的 .exe
文件,你也可以起別的名字),如果出現(xiàn)下面的情況,就說明你成功了。
現(xiàn)在,我們就可以對這份代碼進行調(diào)試了。
調(diào)試
這是 ceshi.cpp
里面的代碼。
//The code was written by yifan, and yifan is neutral!!! #include <bits/stdc++.h> using namespace std; typedef long long ll; #define bug puts("NOIP rp ++!"); template<typename T> inline T read() { T x = 0; bool fg = 0; char ch = getchar(); while (ch < '0' || ch > '9') { fg |= (ch == '-'); ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return fg ? ~x + 1 : x; } int a[10]; vector<int> s; void bre() { int n = 10; while (n --) { puts("qwq"); } } int main() { int n = read<int>(), m = read<int>(); for (int i = 1; i <= 8; ++ i) { a[i] = i; s.emplace_back(i); } puts("AK IOI!"); bre(); puts("end!"); return 0; }
我們要學(xué)會一些基礎(chǔ)的命令。
r
運行代碼,也就是我們平時的運行,你輸入數(shù)據(jù),它進行操作,最后輸出。
l
查看代碼,l [數(shù)字]
查看第幾行附近的代碼(取決于你輸入的數(shù)字)
b [數(shù)字或函數(shù)名]
在第幾行設(shè)置斷點或者在函數(shù)處設(shè)置斷點,代碼運行到這一行時就會停下,函數(shù)名要帶小括號,例如 main 函數(shù)就是 main()
c
從斷點處繼續(xù)運行至下一斷點處或者程序結(jié)束處。
p [變量、數(shù)組名或者 STL 容器名]
查看變量、數(shù)組或者 STL 容器中的元素,如果數(shù)組或者 STL 中的元素過多,則只會顯示大小。
d 斷點編號
刪除斷點,i b
查看所有斷點。
watch [變量或數(shù)組]
觀測某個變量或數(shù)組,當(dāng)這個變量或者數(shù)組中的元素出現(xiàn)變化時,程序就暫停,并給你顯示變化,在程序運行后才可以使用。
(gdb) r // 運行 Starting program: F:\\8.7\ceshi.exe [New Thread 10196.0x1b74] [New Thread 10196.0x173c] [New Thread 10196.0xe84] Thread 1 hit Breakpoint 1, main () at ceshi.cpp:36 36 n = read<int>(), m = read<int>(); // 碰到斷點 (gdb) watch n // 添加監(jiān)測變量 n Hardware watchpoint 2: n (gdb) c // 繼續(xù)運行 Continuing. 2 3 Thread 1 hit Hardware watchpoint 2: n // n 的變化 Old value = 0 New value = 2 main () at ceshi.cpp:36 36 n = read<int>(), m = read<int>(); (gdb) watch a // 添加監(jiān)測數(shù)組 a Watchpoint 3: a (gdb) c Continuing. Watchpoint 2 deleted because the program has left the block in which its expression is valid. Thread 1 hit Watchpoint 3: a // a 的變化 Old value = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} New value = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0} New value = {0, 1, 2, 0, 0, 0, 0, 0, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 2, 0, 0, 0, 0, 0, 0, 0} New value = {0, 1, 2, 3, 0, 0, 0, 0, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 2, 3, 0, 0, 0, 0, 0, 0} New value = {0, 1, 2, 3, 4, 0, 0, 0, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 2, 3, 4, 0, 0, 0, 0, 0} New value = {0, 1, 2, 3, 4, 5, 0, 0, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 2, 3, 4, 5, 0, 0, 0, 0} New value = {0, 1, 2, 3, 4, 5, 6, 0, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 2, 3, 4, 5, 6, 0, 0, 0} New value = {0, 1, 2, 3, 4, 5, 6, 7, 0, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. Thread 1 hit Watchpoint 3: a Old value = {0, 1, 2, 3, 4, 5, 6, 7, 0, 0} New value = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0} main () at ceshi.cpp:39 39 s.emplace_back(i); (gdb) c Continuing. AK IOI! qwq qwq qwq qwq qwq qwq qwq qwq qwq qwq end! [Thread 10196.0xe84 exited with code 0] [Thread 10196.0x173c exited with code 0] [Thread 10196.0x1b74 exited with code 0] [Inferior 1 (process 10196) exited normally] (gdb)
info watch
查看所有監(jiān)測。info locals
顯示局部變量。info f
顯示當(dāng)前棧的情況。finish
完成函數(shù)。call [函數(shù)名]
調(diào)用子函數(shù)。n
從斷點處開始,下一行,不進入子函數(shù)。s
從斷點處開始,下一行,進入子函數(shù)(包括快讀、STL 的輸入函數(shù)等等)。return
忽略程序未完成的語句,強行返回。whatis [變量名]
返回變量的數(shù)據(jù)類型。set var [變量 = 數(shù)值]
強行給變量賦值。q
退出。
到此這篇關(guān)于gdb調(diào)試的簡單操作總結(jié)的文章就介紹到這了,更多相關(guān)gdb調(diào)試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
快來領(lǐng)取!你想要的C++/C語言優(yōu)秀書籍
如何選擇合適的C++/C語言書籍,是不是已經(jīng)眼花繚亂,不知道該選擇哪本好了呢?今天我來為大家分享兩本不可錯過的優(yōu)秀書籍2017-09-09char str[] 與 char *str的區(qū)別詳細(xì)解析
以下是對char str[]與char *str的區(qū)別進行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-09-09VS2019創(chuàng)建C++工程的的實現(xiàn)步驟
本文主要介紹了VS2019創(chuàng)建C++工程步驟,包含新建項目、編輯文件、配置源文件目錄、編譯鏈接、輸出文件、設(shè)置斷點調(diào)試,具有一定的參考價值,感興趣的可以了解一下2024-12-12