debug —— C语言 编译时候进行debug的调试
gdb是the GNU Debugger的簡稱。它是一款UNIX平臺的調試器(debugger),可用于為C, C++, Objective-C, Java, Fortran等程序debug。
在gdb中,你可以通過設置斷點(break point)來控制程序運行的進度,并查看斷點時的變量和函數調用狀況,從而發現可能的問題。在許多IDE中,gdb擁有圖形化界面。
?
一、初次使用gdb調試器,出現的No symbol table is loaded. Use the "file" command.問題:
首先使用gcc ? -g ? ?.c文件 ? -o ?可執行文件名 ?進行編譯;再使用gdb + 可執行文件名進入gdb環境,進行調試。
為了使用gdb對進行調試,必須使用-g選項(在編譯時生成debug信息)
命令如下如:
#gcc -g test.c -o test #gdb test? ?如果上一步gdb + 不是 -g編譯后的 可執行文件,而是 gdb? ./a.out ,則會出現?Use the "file" command.問題
(gdb)list? ?list命令是用來列出源碼的。
? ?詳細的list的使用查看文章《?debug —— list調試命令》
(2) ? gdb ?test
(3) ?list等gdb命令;
(如有必要,使用:$chmod +x test來增加用戶的執行權限。)
?
二、使用gdb調試:
1》啟動gdb:
[root@node-2 jieer]# gcc -g struct.c -o struct [root@node-2 jieer]# gdb struct2》顯示程序:詳細的list的使用查看文章《?debug —— list調試命令》
<1> 將顯示以第3行為中心,總共10行的程序。
如果要查看某個文件中的內容,需要說明文件名(例如:(gdb) list struct.c:12)。
(gdb) list 3<2>可以具體說明所要列出的程序行的范圍(即 顯示5-15行的程序).
(gdb) list 5,15<3>顯示某個函數.
(gdb) list main3》設置斷點
<1>我們可以在程序的某一行設置斷點,比如:
(gdb) break 16 (或者是簡寫 b 16)將在test.c的第16行設置斷點。
<2>你可以查看自己設置的斷點:
(gdb) info break<3>每個斷點有一個識別序號。我們可以根據序號刪除某個斷點:
(gdb) delete 1<4>也可以刪除所有斷點:
(gdb) delete breakpoints到目前為止,程序內變量的賦值都是在程序內部完成的,如果程序內的一些變量需要執行文件的時候,用命令行傳入呢?
例如:需要你打印出argv[0]、argv[1]、argv[2]的值得一個函數你該如何操作呢?
《可以查看文章《debug —— set args調試命令(作為程序運行時的參數)》
4》保存斷點
<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》運行控制
<1>讓程序從斷點開始,再多運行一行(可以看函數內嵌套的另一個函數的內容):
(gdb) step (可以用簡寫 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:在文件內第30行設置一個斷點,即在test.c文件,mian函數中第30 行。
步驟2:運行程序:可以看到執行到ccc()函數哪一行了;
步驟3:執行s命令:從此處開始多運行一行,進入ccc()函數內部;
步驟4,5:繼續調試函數:看到bbb()函數,這是很確定我們進入了ccc()函數中了。
那么不用s命令會有怎么的結果呢?我們來看一下
<2>也可以使用下面命令,從斷點恢復運行,直到下一個斷點:
(gdb) continue<3>使用run重新開始運行
(gdb) run程序正常結束。
6》退出
使用下面命令退出gdb:
(gdb) quit (可以使用簡寫 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
總結
以上是生活随笔為你收集整理的debug —— C语言 编译时候进行debug的调试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对C语言main函数中argc和argv
- 下一篇: malloc和free——结构体中动态内