gdb調(diào)試的簡(jiǎn)單操作總結(jié)
gdb是一個(gè)命令行下的、功能強(qiáng)大的調(diào)試器。
在學(xué)習(xí) gdb 前,我們要知道幾個(gè)最基本的 cmd 命令。
cmd
首先,對(duì)于 win10 系統(tǒng),我們按 Windows + R 鍵,打開運(yùn)行窗口,在里面輸入 cmd,這樣就可以打開 cmd 命令窗口了,是一個(gè)黑框。

接下來是一些最基本的命令。
F: 打開 F 盤;E: 打開 E 盤,等等

dir 查看文件夾中的文件

cd XXX.XXX 打開 XXX.XXX 文件

cd.. 返回上一級(jí)(文件夾)

cls 清屏(如果你覺得屏幕中的信息太多的話可以用這個(gè)命令)
ping [網(wǎng)址] 查看一個(gè)網(wǎng)站的信息(中括號(hào)不用打)

gdb 調(diào)試前的操作
在進(jìn)行 gdb 調(diào)試之前,請(qǐng)先確保你安裝了 gdb、g++、gcc 等。
首先,在 cmd 中進(jìn)入到你的代碼的文件夾頁(yè)面,我的代碼在 F 盤,如下圖。

現(xiàn)在,我只要進(jìn)入這個(gè)地址就行了。

要進(jìn)行調(diào)試的文件是 ceshi.cpp,我們先在 cmd 中輸入 g++ -g ceshi.cpp -o ceshi.exe 命令,然后等待一會(huì),文件夾中就會(huì)出現(xiàn)一個(gè)名為 ceshi.exe 的文件。
之后輸入 gdb 命令,會(huì)變成這樣。

現(xiàn)在,我們用 file 命令來將剛才生成的 .exe 文件載入 gdb 調(diào)試器中,輸入 file ceshi.exe(這里的 ceshi.exe 是剛才生成的 .exe 文件,你也可以起別的名字),如果出現(xiàn)下面的情況,就說明你成功了。

現(xiàn)在,我們就可以對(duì)這份代碼進(jì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é)會(huì)一些基礎(chǔ)的命令。
r 運(yùn)行代碼,也就是我們平時(shí)的運(yùn)行,你輸入數(shù)據(jù),它進(jìn)行操作,最后輸出。

l 查看代碼,l [數(shù)字] 查看第幾行附近的代碼(取決于你輸入的數(shù)字)

b [數(shù)字或函數(shù)名] 在第幾行設(shè)置斷點(diǎn)或者在函數(shù)處設(shè)置斷點(diǎn),代碼運(yùn)行到這一行時(shí)就會(huì)停下,函數(shù)名要帶小括號(hào),例如 main 函數(shù)就是 main()

c 從斷點(diǎn)處繼續(xù)運(yùn)行至下一斷點(diǎn)處或者程序結(jié)束處。

p [變量、數(shù)組名或者 STL 容器名] 查看變量、數(shù)組或者 STL 容器中的元素,如果數(shù)組或者 STL 中的元素過多,則只會(huì)顯示大小。

d 斷點(diǎn)編號(hào) 刪除斷點(diǎn),i b 查看所有斷點(diǎn)。

watch [變量或數(shù)組] 觀測(cè)某個(gè)變量或數(shù)組,當(dāng)這個(gè)變量或者數(shù)組中的元素出現(xiàn)變化時(shí),程序就暫停,并給你顯示變化,在程序運(yùn)行后才可以使用。
(gdb) r // 運(yùn)行
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>(); // 碰到斷點(diǎn)
(gdb) watch n // 添加監(jiān)測(cè)變量 n
Hardware watchpoint 2: n
(gdb) c // 繼續(xù)運(yùn)行
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)測(cè)數(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)測(cè)。info locals顯示局部變量。info f顯示當(dāng)前棧的情況。finish完成函數(shù)。call [函數(shù)名]調(diào)用子函數(shù)。n從斷點(diǎn)處開始,下一行,不進(jìn)入子函數(shù)。s從斷點(diǎn)處開始,下一行,進(jìn)入子函數(shù)(包括快讀、STL 的輸入函數(shù)等等)。return忽略程序未完成的語(yǔ)句,強(qiáng)行返回。whatis [變量名]返回變量的數(shù)據(jù)類型。set var [變量 = 數(shù)值]強(qiáng)行給變量賦值。q退出。
到此這篇關(guān)于gdb調(diào)試的簡(jiǎn)單操作總結(jié)的文章就介紹到這了,更多相關(guān)gdb調(diào)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)火車訂票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)火車訂票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C語(yǔ)言跳轉(zhuǎn)瀏覽器打開指定URL的操作代碼
這篇文章主要介紹了C語(yǔ)言跳轉(zhuǎn)瀏覽器打開指定URL,該代碼使用sprintf()函數(shù)將要打開的URL添加到一個(gè)系統(tǒng)命令中,然后使用system()函數(shù)調(diào)用該命令以默認(rèn)瀏覽器打開URL,需要的朋友可以參考下2023-04-04
快來領(lǐng)取!你想要的C++/C語(yǔ)言優(yōu)秀書籍
如何選擇合適的C++/C語(yǔ)言書籍,是不是已經(jīng)眼花繚亂,不知道該選擇哪本好了呢?今天我來為大家分享兩本不可錯(cuò)過的優(yōu)秀書籍2017-09-09
char str[] 與 char *str的區(qū)別詳細(xì)解析
以下是對(duì)char str[]與char *str的區(qū)別進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下2013-09-09
VS2019創(chuàng)建C++工程的的實(shí)現(xiàn)步驟
本文主要介紹了VS2019創(chuàng)建C++工程步驟,包含新建項(xiàng)目、編輯文件、配置源文件目錄、編譯鏈接、輸出文件、設(shè)置斷點(diǎn)調(diào)試,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12

