debug —— C语言 编译时候进行debug的调试
gdb是the GNU Debugger的簡(jiǎn)稱。它是一款UNIX平臺(tái)的調(diào)試器(debugger),可用于為C, C++, Objective-C, Java, Fortran等程序debug。
在gdb中,你可以通過(guò)設(shè)置斷點(diǎn)(break point)來(lái)控制程序運(yùn)行的進(jìn)度,并查看斷點(diǎn)時(shí)的變量和函數(shù)調(diào)用狀況,從而發(fā)現(xiàn)可能的問(wèn)題。在許多IDE中,gdb擁有圖形化界面。
?
一、初次使用gdb調(diào)試器,出現(xiàn)的No symbol table is loaded. Use the "file" command.問(wèn)題:
首先使用gcc ? -g ? ?.c文件 ? -o ?可執(zhí)行文件名 ?進(jìn)行編譯;再使用gdb + 可執(zhí)行文件名進(jìn)入gdb環(huán)境,進(jìn)行調(diào)試。
為了使用gdb對(duì)進(jìn)行調(diào)試,必須使用-g選項(xiàng)(在編譯時(shí)生成debug信息)
命令如下如:
#gcc -g test.c -o test #gdb test? ?如果上一步gdb + 不是 -g編譯后的 可執(zhí)行文件,而是 gdb? ./a.out ,則會(huì)出現(xiàn)?Use the "file" command.問(wèn)題
(gdb)list? ?list命令是用來(lái)列出源碼的。
? ?詳細(xì)的list的使用查看文章《?debug —— list調(diào)試命令》
(2) ? gdb ?test
(3) ?list等gdb命令;
(如有必要,使用:$chmod +x test來(lái)增加用戶的執(zhí)行權(quán)限。)
?
二、使用gdb調(diào)試:
1》啟動(dòng)gdb:
[root@node-2 jieer]# gcc -g struct.c -o struct [root@node-2 jieer]# gdb struct2》顯示程序:詳細(xì)的list的使用查看文章《?debug —— list調(diào)試命令》
<1> 將顯示以第3行為中心,總共10行的程序。
如果要查看某個(gè)文件中的內(nèi)容,需要說(shuō)明文件名(例如:(gdb) list struct.c:12)。
(gdb) list 3<2>可以具體說(shuō)明所要列出的程序行的范圍(即 顯示5-15行的程序).
(gdb) list 5,15<3>顯示某個(gè)函數(shù).
(gdb) list main3》設(shè)置斷點(diǎn)
<1>我們可以在程序的某一行設(shè)置斷點(diǎn),比如:
(gdb) break 16 (或者是簡(jiǎn)寫 b 16)將在test.c的第16行設(shè)置斷點(diǎn)。
<2>你可以查看自己設(shè)置的斷點(diǎn):
(gdb) info break<3>每個(gè)斷點(diǎn)有一個(gè)識(shí)別序號(hào)。我們可以根據(jù)序號(hào)刪除某個(gè)斷點(diǎn):
(gdb) delete 1<4>也可以刪除所有斷點(diǎn):
(gdb) delete breakpoints到目前為止,程序內(nèi)變量的賦值都是在程序內(nèi)部完成的,如果程序內(nèi)的一些變量需要執(zhí)行文件的時(shí)候,用命令行傳入呢?
例如:需要你打印出argv[0]、argv[1]、argv[2]的值得一個(gè)函數(shù)你該如何操作呢?
《可以查看文章《debug —— set args調(diào)試命令(作為程序運(yùn)行時(shí)的參數(shù))》
4》保存斷點(diǎn)
<1>
(gdb) info break??????Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400536 in main at struct.c:12
2 breakpoint keep y 0x0000000000400547 in main at struct.c:13
<2>
(gdb) save breakpoint fig.dpSaved to file 'fig.dp'.
<3>
[root@node-2 jieer]# gdb struct -x fig.dpGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
Copyright (C) 2013 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 WA RRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/jieer/struct...done.
Breakpoint 1 at 0x400536: file struct.c, line 12.
Breakpoint 2 at 0x400547: file struct.c, line 13.
5》運(yùn)行控制
<1>讓程序從斷點(diǎn)開(kāi)始,再多運(yùn)行一行(可以看函數(shù)內(nèi)嵌套的另一個(gè)函數(shù)的內(nèi)容):
(gdb) step (可以用簡(jiǎn)寫 s) 源代碼:21 int ccc(){22 int total = 0;23 char other[512] = {'\0'};24 25 bbb(&total);26 printf("ccc:total=%d\n",total);27 printf("ccc:total=%p\n",&total);28 ddd(other);29 }30 int main(){31 ccc();32 return 0;33 }步驟1:在文件內(nèi)第30行設(shè)置一個(gè)斷點(diǎn),即在test.c文件,mian函數(shù)中第30 行。
步驟2:運(yùn)行程序:可以看到執(zhí)行到ccc()函數(shù)哪一行了;
步驟3:執(zhí)行s命令:從此處開(kāi)始多運(yùn)行一行,進(jìn)入ccc()函數(shù)內(nèi)部;
步驟4,5:繼續(xù)調(diào)試函數(shù):看到bbb()函數(shù),這是很確定我們進(jìn)入了ccc()函數(shù)中了。
那么不用s命令會(huì)有怎么的結(jié)果呢?我們來(lái)看一下
<2>也可以使用下面命令,從斷點(diǎn)恢復(fù)運(yùn)行,直到下一個(gè)斷點(diǎn):
(gdb) continue<3>使用run重新開(kāi)始運(yùn)行
(gdb) run程序正常結(jié)束。
6》退出
使用下面命令退出gdb:
(gdb) quit (可以使用簡(jiǎn)寫 q 或者 .qu)三、舉例分析:
1》struct.c文件的源碼如下:
1 #include<stdio.h>2 #include<stdlib.h>3 #include<string.h>4 struct student5 {6 char *name;7 int score;8 }stu,*pstu;9 10 int main()11 {12 stu.name = (char *)malloc(20*sizeof(char));13 strcpy(stu.name,"jie");14 stu.score = 90;15 16 pstu = (struct student *)malloc(sizeof(struct student));17 pstu->name = (char *)malloc(20*sizeof(char));18 strcpy(pstu->name,"jieer");19 pstu->score = 9;20 21 return 0;22 }2》具體操作如下:
[root@node-2 jieer]# gcc -g struct.c -o struct[root@node-2 jieer]# gdb struct (gdb) break 12Breakpoint 1 at 0x400536: file struct.c, line 12.
(gdb) break 13Breakpoint 2 at 0x400547: file struct.c, line 13.
(gdb) rStarting program: /root/jieer/struct
Breakpoint 1, main () at struct.c:12
12 stu.name = (char *)malloc(20*sizeof(char));
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64
(gdb) p stu.name$1 = 0x0
(gdb) cContinuing.
Breakpoint 2, main () at struct.c:13
13 strcpy(stu.name,"jie");
(gdb) p stu.name$2 = 0x602010 ""
(gdb) quitA debugging session is active.
?
Inferior 1 [process 12909] will be killed.
?
Quit anyway? (y or n) y
總結(jié)
以上是生活随笔為你收集整理的debug —— C语言 编译时候进行debug的调试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 对C语言main函数中argc和argv
- 下一篇: malloc和free——结构体中动态内