GNU gdb 應用例項

2021-06-26 11:37:44 字數 2488 閱讀 2624

gnu gdb 應用例項

一、用乙個簡單的例子說明如何使用gdb

除錯程式:

[root@localhost ~]# vi hello.c

#include 

#include 

#include 

#include 

int main()

char *p = (char *)malloc(20);

strcpy(p,"123");

pid_t pid = getpid();

printf("%d",pid);

pause();

return 0;

該程式的功能是獲取當前程序號,並把該程序號輸出。

在除錯之前,首先要利用gcc

編譯器編譯程式,命令如下:

[root@localhost ~]# gcc -g hello.c -o t

其中,-g

引數表示在編譯過程中,編譯器加入一些用於

gdb除錯的資訊,否則,無法進行

gdb除錯。

編譯成功之後生成t

的可執行檔案,可以進入

gdb除錯介面:

1>在

shell

中輸入以下命令進入

gdb除錯介面

[root@localhost ~]# gdb

gnu gdb (gdb) fedora (7.2-16.fc14)

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:

.(gdb) file t

reading symbols from /root/t...done.

(gdb) 

2>先輸入

gdb命令,再輸入

file t

命令進入除錯介面

[root@localhost ~]# gdb t

gnu gdb (gdb) fedora (7.2-16.fc14)

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 /root/t...done.

(gdb) 

list命令可以

設定斷點開始除錯:

(gdb) break 8

breakpoint 1 at 0x804848d: file hello.c, line 8.

(gdb) run

starting program: /root/t 

breakpoint 1, main () at hello.c:8

8 char *p = (char *)malloc(20);

missing separate debuginfos, use: debuginfo-install glibc-2.12.90-17.i686

(gdb) print *p

$1 = 124 '|'

(gdb) print p

$2 = 0x97dff4 "|ݗ"

(gdb) next

9 strcpy(p,"123");

(gdb) next

10 pid_t pid = getpid();

(gdb) print pid

$3 = 134513915

(gdb) next

11 printf("%d",pid);

(gdb) print pid

$4 = 2982

(gdb) kill

kill the program being debugged? (y or n) y

(gdb) quit

[root@localhost ~]# 

以上只是gdb除錯工具的一小部分,希望起到作用。

應用例項 最新電工電路應用例項

電工學習網 www.diangon.com 最新電工電路應用例項 是根據最新國家職業標準,結合生產實際,以操作技能為主,以解決實際工作中的技術問題為目標而編寫的。本書的主要內容包括 照明 配電及單相電動機電路,電動機典型控制電路,電動機保護電路,工具機控制電路,穩壓電路及充電電路,電氣測量電路,pl...

mysql 應用例項 MySQL多例項應用

國內映象 2.上傳 解壓 tar xf mysql 5.6.46 linux glibc2.12 x86 64.tar.gz tar xf mysql 5.7.12 linux glibc2.12 x86 64.tar.xz tar xf mysql 8.0.18 linux glibc2.12 x...

APN應用例項

gprs撥號上網時,移動ggsn分配手機模組乙個ip位址 靜態或動態 公用或私有 目前中國移動提供的是動態的ip。為了提高資料傳輸的可靠性和穩定性,我們可以向移動公司申請apn服務。移動公司可以為接入使用者建立vpn x虛擬專用網 而且移動公司為每個apn客戶提供乙個繫結網內ip的sim卡。以廣州為...