linux下 gdb安裝與使用簡介
gdb簡介
GDB,又稱GNU調(diào)試器,是用來幫助調(diào)試我們程序的工具。gdb可以設(shè)置斷點、查看變量、堆棧空間的值、設(shè)置執(zhí)行條件的值等。
一般來說,GDB主要幫助你完成下面四個方面的功能:
1、啟動你的程序,可以按照你的自定義的要求隨心所欲的運行程序。
2、可讓被調(diào)試的程序在你所指定的調(diào)置的斷點處停住。(斷點可以是條件表達式)
3、當(dāng)程序被停住時,可以檢查此時你的程序中所發(fā)生的事。
4、你可以改變你的程序,將一個BUG產(chǎn)生的影響修正從而測試其他BUG。
red hat6.3下安裝gdb調(diào)試工具

拷貝gdb軟件包到用戶目錄下,切換到用戶目錄下,安裝gdb工具。
[xsw@xsw Packages]$ cp ./gdb-7.2-56.el6.i686.rpm ~
[xsw@xsw Packages]$ cd ~
[xsw@xsw ~]$ sudo rpm -ivh gdb-7.2-56.el6.i686.rpm

Ubuntu下安裝gdb
[wbyq@wbyq ~]$ sudo apt-get install gdb
gdb工具常用命令
參數(shù) | 功能 |
r | run, 直接調(diào)到斷點處,沒有設(shè)置斷點的話直接運行程序 |
b func | 設(shè)置一個斷點breakpoint在函數(shù)”func”的最開始 |
b N | 在當(dāng)前函數(shù)的第N行設(shè)置斷點 |
d N | 刪除第N行的斷點 |
info b | 顯示所有斷點 |
c | 繼續(xù)運行到下一個斷點或程序結(jié)束 |
f | 運行到當(dāng)前程序結(jié)束 |
s | 單步調(diào)試,進入函數(shù) |
s N | 執(zhí)行接下來的N行程序 |
n | 單步調(diào)試,不進入函數(shù) |
p data | 輸出變量data的值 |
bt | 查看調(diào)用的堆棧 |
l | 一次列出10行源碼(從mian.c開始) |
l func | 列出函數(shù)func的10行源碼 |
condition | 設(shè)置條件斷點,condition 1 i=5,設(shè)置斷點1的條件斷點為i=5 |
q | 退出gdb |
示例
#include
int main()
{
int a;
int *p=&a;
printf("請輸入val:\n");
scanf("%d",&a);
int i=0;
for(i=0;i<5;i++)
{
*p+=i;
}
printf("a=%d\n",*p);
}
調(diào)試步驟
1.編譯程序,使用gdb調(diào)試時需加上-g,進入調(diào)試模式。
[xsw@xsw cc]$ gcc main.c -g
[xsw@xsw cc]$ gdb ./a.out
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-56.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/xsw/xsw_work/cc/a.out...done.
(gdb)
2.列出源碼
(gdb) l
1 #include
2 int main()
3 {
4 int a;
5 int *p=&a;
6 printf("請輸入val:\n");
7 scanf("%d",&a);
8 int i=0;
9 for(i=0;i<5;i++)
10 {
(gdb)
3.設(shè)置斷點,開始運行
(gdb) b 4
Breakpoint 1 at 0x804844d: file main.c, line 4.
(gdb) r
Starting program: /home/xsw/xsw_work/cc/a.out
Breakpoint 1, main () at main.c:5
5 int *p=&a;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.i686
(gdb)
4.單步執(zhí)行
(gdb) s
6 printf("請輸入val:\n");
(gdb) s
請輸入val:
7 scanf("%d",&a);
(gdb) s
5
8 int i=0;
5.查看變量值
(gdb) p a
$1 = 5
-
Linux
+關(guān)注
關(guān)注
87文章
11469瀏覽量
212908 -
調(diào)試器
+關(guān)注
關(guān)注
1文章
312瀏覽量
24195 -
gdb
+關(guān)注
關(guān)注
0文章
60瀏覽量
13551
發(fā)布評論請先 登錄
使用GDB調(diào)試Linux應(yīng)用程序
嵌入式arm linux環(huán)境中gdb+gdbserver的安裝
linux 3 - gdb的安裝和使用介紹
DM8168 gdb調(diào)試需要單獨再安裝嗎
如何搭建嵌入式Linux的GDB調(diào)試環(huán)境
ubuntu下GDB調(diào)試GDB簡介
嵌入式Linux的GDB調(diào)試環(huán)境建立
實例演示GDB的使用

嵌入式Linux GDB調(diào)試環(huán)境搭建與使用

Linux嵌入式 gdb VSCode圖形化調(diào)試教程

評論