天天看點

gdb調試



1 gdb的主要功能:

a

啟動你的程式,可以按照你的自定義的要求随心所欲的運作程式。

b

可以讓被調試的程式在你所指定的調試的斷點處停住。(斷點可以是條件表達式)

c

當程式被停止住是,可以檢查此時你的程式中所發生的事。

d

動态的改變你程式的執行環境。

2

載入程式的兩種方式

前提:編譯程式的時候加上了調試指令 

-g,比如gcc demo.c–g app

在啟動gdb後(也就是說在終端視窗先執行gdb這個指令),接着執行以下指令:

file

可執行檔案路徑  

比如:在目前路徑下有app,那麼指令就是:fileapp

toto@toto-pc:~/test$ gdb

gnu gdb (ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1

copyright (c) 2014 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".

(gdb) file app

在gdb啟動時就載入程式:

toto@toto-pc:~/test$ gdb app

type "apropos word" to search for commands related to "word"...

reading symbols from app...done.

(gdb)

3

一個調試示例(test.c)

#include<stdio.h>

 int func(int n) {

    int sum = 0,i;

    for(i = 0;i < n;i++) {

       sum += i;

    }

    return sum;

}

int main(void)

  {

int i;

long result = 0;

for(i = 1; i <= 100; i++) {

result += i;

printf("result[1-100] = %d \n",result);

printf("result[1-250] = %d \n",func(250));

編譯生成執行檔案(linux):

gcc -g test.c -o test

使用gdb調試:

gdb test

toto/test> gdb tst  <----------

啟動gdb

gnu gdb 5.1.1

copyright 2002 free software foundation, inc.

gdb is free software, covered by the gnu general public license, and you are

welcome to change it and/or distribute copies of it under certain conditions.

type "show copying" to see the conditions.

there is absolutely no warranty for gdb.  type "show warranty" for details.

this gdb was configured as "i386-suse-linux"...

(gdb) l     <-------------- l指令相當于list,從第一行開始例出原碼。

1        #include <stdio.h>

3        int func(int n)

4        {

5                int sum=0,i;

6                for(i=0; i<n; i++)

7                {

8                        sum+=i;

9                }

10               return sum;

(gdb)       <-------------------- 直接回車表示,重複上一次指令

11       }

12

13

14       main()

15       {

16               int i;

17               long result = 0;

18               for(i=1; i<=100; i++)

19               {

20                       result += i;   

(gdb) break 16    <-------------------- 設定斷點,在源程式第16行處。

breakpoint 1 at 0x8048496: file tst.c, line 16.

(gdb) break func  <-------------------- 設定斷點,在函數func()入口處。

breakpoint 2 at 0x8048456: file tst.c, line 5.

(gdb) info break  <-------------------- 檢視斷點資訊。

num type           disp enb address    what

1   breakpoint     keep y   0x08048496 in main at tst.c:16

2   breakpoint     keep y   0x08048456 in func at tst.c:5

(gdb) r           <--------------------- 運作程式,run指令簡寫

starting program: /home/toto/test/test

breakpoint 1, main () at tst.c:17    <----------

在斷點處停住。

(gdb) n          <--------------------- 單條語句執行,next指令簡寫。

(gdb) n

20                       result += i;

(gdb) c          <--------------------- 繼續運作程式,continue指令簡寫。

continuing.

result[1-100] = 5050       <----------程式輸出。

breakpoint 2, func (n=250) at tst.c:5

6                for(i=1; i<=n; i++)

(gdb) p i        <--------------------- 列印變量i的值,print指令簡寫。

$1 = 134513808

(gdb) p sum

$2 = 1

(gdb) p i

$3 = 2

$4 = 3

(gdb) bt        <--------------------- 檢視函數堆棧。

#0  func (n=250) at tst.c:5

#1  0x080484e4 in main () at tst.c:24

#2  0x400409ed in __libc_start_main () from /lib/libc.so.6

(gdb) finish    <--------------------- 退出函數。

run till exit from #0  func (n=250) at tst.c:5

0x080484e4 in main () at tst.c:24

24              printf("result[1-250] = %d /n", func(250) );

value returned is $6 = 31375

(gdb) c     <--------------------- 繼續運作。

result[1-250] = 31375    <----------程式輸出。

program exited with code 027. <--------程式退出,調試結束。

(gdb) q     <--------------------- 退出gdb。

toto/test>

運作gdb

gdb <program>program也就是你的執行檔案,一般在目前目錄下。

gdb <program>core

用dbc同時調用一個運作程式和core檔案,core是程式非法執行後core

dump後産生的檔案。

gdb <program><pid> 

-- 調用正在運作的程式。program為需要調用的程式檔案,pid為目前正在運作的程式。或是先用gdb

<program>關聯源代碼進入gdb,後用attatch指令來挂接程序pid,并用detach來取消挂接的程序。

5

常用指令

指令

簡寫

作用

help

h

按子產品列出指令類

help class

檢視某一類型的具體命名

list

l

檢視代碼,可跟行号和函數名

quit

q

退出gdb

run

r

全速運作程式

step

s

逐語句執行,遇到函數,跳到函數内執行

backtrace

bt

檢視函數的調用的棧幀和層級關系。

info

i

檢視函數内部局部變量的數值

frame

f

切換函數的棧幀

finish

結束目前函數,傳回函數調用點

set

設定變量的值

run argv[1] argv[2]

調試時指令行傳參

print

p

列印變量和位址

break

設定斷點,可根據行号和函數名

delete

删除斷點,d breakpoints num

display

設定觀察變量

undisplay

取消觀察變量

continue

繼續全速運作剩下的代碼

enable breakpoints

啟用斷點

disable breakpoints

禁用斷點

x

檢視沒存 

x/20xw  顯示20個單元,16進制,4位元組每單元

watch

被設定觀察點的變量發生修改時,列印顯示。

i watch

現實觀察點

core

ulimit –c 1024

開啟core檔案,調試時

gdb a.out core

gdb調試模式

全速運作

start

單步調試