作者:良知猶存
轉(zhuǎn)載授權(quán)以及圍觀:歡迎添加微信號(hào):Conscience_Remains
總述
????在window下我們習(xí)慣了IDE的各種調(diào)試按鈕账阻,說實(shí)話確實(shí)挺方便的熔任,但到了Linux下,沒有那么多的IDE支持我們調(diào)試蠢琳,但是Linux有也有強(qiáng)大的命令行C/C++的調(diào)試工具——GDB啊终,GNU提供的開源調(diào)試工具。
????剛開始不習(xí)慣傲须,使用多了我們就會(huì)喜歡上他蓝牲,程序調(diào)試的單步執(zhí)行,跳入函數(shù)泰讽,跳出函數(shù)例衍,設(shè)置斷點(diǎn),設(shè)置觀察點(diǎn)已卸,查看變量佛玄。GDB都有,此外gdb還可以生成程序非法執(zhí)行后core dump文件咬最,這個(gè)文件有快照功能翎嫡,在程序崩潰的時(shí)候保存了程序的堆棧等信息,我們執(zhí)行core文件就可以方便的找程序崩潰的原因了永乌。
一惑申、編譯可以調(diào)試的代碼
? ??在linux下對于單個(gè)c/c++文件編譯通常就是加-o 進(jìn)行編譯成可執(zhí)行文件具伍,但是我們?nèi)绻枰{(diào)試,則需要加一個(gè) -g 用來向編譯器進(jìn)行表明該程序需要編譯成可以gdb調(diào)試的代碼圈驼,加上編譯信息人芽,生成的執(zhí)行文件就會(huì)變大,如圖所示绩脆。所以我們只在調(diào)試的時(shí)候進(jìn)行 -g 編譯萤厅。Java調(diào)試的時(shí)候也是類似后續(xù)也說一哈java的Linux。
Makefile的文件中我們也是如上靴迫,只不過是在Makefile文件中 -o 編譯的時(shí)候添加 -g
二惕味、調(diào)試過程
調(diào)用gdb調(diào)試,先查看電腦環(huán)境里面是否有g(shù)db調(diào)試器玉锌,一般我們安裝了gcc編譯器名挥,就默認(rèn)同時(shí)安裝了gdb調(diào)試器
沒有的話要安裝gdb調(diào)試器,使用apt-get 就可以快速安裝
apt-get?update
apt-get install gdb
gdb 調(diào)用執(zhí)行文件?
gdb?./big_endian /*執(zhí)行文件*/
最基本的GDB命令
示例執(zhí)行:
/*剛開始有很多打印的信息*/
book@lyn:~/Documents/linux/test/wds/wds_c++/c++_test1/c11th$ gdb person1
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.? Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from person1...done.
(gdb)?l?10??/*list?10??顯示10行代碼?方便下一步打斷點(diǎn)調(diào)試*/
5? ? ? using namespace std;
6
7? ? ? class Person {
8? ? ? private:
9? ? ? ? ? ? ? static int cnt;
10? ? ? ? ? ? ? char *name;
11? ? ? ? ? ? ? int age;
12
13? ? ? public:
14
(gdb)???/*不輸入?直接?Enter?鍵?重復(fù)上一步驟*/
15? ? ? ? ? ? ? static int getCount(void);
16
17? ? ? ? ? ? ? Person() {//cout <<"Pserson()"<<endl;
18? ? ? ? ? ? ? ? ? ? ? name = NULL;
19? ? ? ? ? ? ? ? ? ? ? cnt++;
20? ? ? ? ? ? ? }
21? ? ? ? ? ? ? Person(char *name)
22? ? ? ? ? ? ? {
23? ? ? ? ? ? ? ? ? ? ? //cout <<"Pserson(char *)"<<endl;
24? ? ? ? ? ? ? ? ? ? ? this->name = new char[strlen(name) + 1];
(gdb)
25? ? ? ? ? ? ? ? ? ? ? strcpy(this->name, name);
26? ? ? ? ? ? ? ? ? ? ? cnt++;
27? ? ? ? ? ? ? }
28
29? ? ? ? ? ? ? Person(char *name, int age)
30? ? ? ? ? ? ? {
31? ? ? ? ? ? ? ? ? ? ? cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
32? ? ? ? ? ? ? ? ? ? ? this->age = age;
33
34? ? ? ? ? ? ? ? ? ? ? this->name = new char[strlen(name) + 1];
(gdb)?/*......*/
......
95
96? ? ? int main(int argc, char **argv)
97? ? ? {
98? ? ? ? ? ? ? Student p;
99? ? ? ? ? ? ? p.setName("zhangsan");
100? ? ? ? ? ? p.setAge(16);
101? ? ? ? ? ? p.printInfo();
102
103? ? ? ? ? ? return 0;
104? ? }
(gdb)?b?99?/*在顯示的?第?99行代碼處?打斷點(diǎn)*/
Breakpoint 1 at 0x400b5d: file person.cpp, line 99.
(gdb)?i?b?/*顯示?設(shè)置的斷點(diǎn)*/
Num? ? Type? ? ? ? ? Disp Enb Address? ? ? ? ? ? What
1? ? ? breakpoint? ? keep y? 0x0000000000400b5d in main(int, char**) at person.cpp:99
(gdb)?r??/*開始全速執(zhí)行代碼?直到第一個(gè)斷點(diǎn)處*/
Starting program: /home/book/Documents/linux/test/wds/wds_c++/c++
Breakpoint 1, main (argc=1, argv=0x7fffffffe3d8) at person.cpp:99
99? ? ? ? ? ? ? p.setName("zhangsan");
(gdb)?p?p??/*打印?p?變量信息*/
$1 = {<Person> = {static cnt = 1, name = 0x0, age = 0}, <No data fields>}
(gdb)?n??/*執(zhí)行下一步*/
100?????????????p.setAge(16);
(gdb)?q??/*退出?gdb?調(diào)試*/
A debugging session is active.
?
? ? ? ? Inferior 1 [process 3410] will be killed.
?
Quit anyway? (y or n) y
(gdb) s
hanoi (n=3, x=120 'x', y=121 'y', z=122 'z') at Hanoi.c:21
21? ? ? ? ? ? ? if(n==1)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //欲移動(dòng)n個(gè)圓盤主守,需先移動(dòng)其上的n-1個(gè)圓盤
(gdb) n
25??????????????????????hanoi(n-1,?x,?z,?y);????????????????????//將x上編號(hào)為1至n-1的圓盤移到y(tǒng)禀倔,z作輔助塔?
?這就是我分享的gdb調(diào)試一些方法,里面代碼是實(shí)踐過的参淫,如果大家有什么更好的思路救湖,歡迎分享交流哈