linux-gdb調試之一

學習鏈接:

1、http://www.cnblogs.com/hankers/archive/2012/12/07/2806836.html

2、http://www.cnblogs.com/lidabo/p/4234362.html

gdb調試

1、gdb常用命令:

命令 描述
backtrace(或bt) 查看各級函數調用及參數
finish 連續運行到當前函數返回爲止,然後停下來等待命令
frame(或f) 幀編號 選擇棧幀
info(或i) locals 查看當前棧幀局部變量的值
list(或l) 列出源代碼,接着上次的位置往下列,每次列10行
list 行號 列出從第幾行開始的源代碼
list 函數名 列出某個函數的源代碼
next(或n) 執行下一行語句
print(或p) 打印表達式的值,通過表達式可以修改變量的值或者調用函數
quit(或q) 退出gdb調試環境
set var 修改變量的值
start 開始執行程序,停在main函數第一行語句前面等待命令
step(或s) 執行下一行語句,如果有函數調用則進入到函數中

2、GDB 概述

GDB 是 GNU開源組織發佈的一個強大的 UNIX下的程序調試工具。

一般來說, GDB主要幫忙你完成下面四個方面的功能:

    1 、啓動你的程序,可以按照你的自定義的要求隨心所欲的運行程序。
    2 、可讓被調試的程序在你所指定的調置的斷點處停住。(斷點可以是條件表達式)
    3 、當程序被停住時,可以檢查此時你的程序中所發生的事。
    4 、動態的改變你程序的執行環境。

3、示例程序

源程序: tst.c

     1 #include <stdio.h>
     2
     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;
    11 }
    12
    13
    14 main()
    15 {
    16         int i;
    17         long result = 0;
    18         for(i=1; i<=100; i++)
    19         {
    20                 result += i;
    21         }
    22
    23        printf("result[1-100] = %d \n", result );
    24        printf("result[1-250] = %d \n", func(250) );
    25 }

編譯生成執行文件:( Linux下)
    hchen/test> cc -g tst.c -o tst

使用 GDB調試:

hchen/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>
2
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/hchen/test/tst

Breakpoint 1, main () at tst.c:17    <---------- 在斷點處停住。
17               long result = 0;
(gdb) n          <--------------------- 單條語句執行, next命令簡寫。
18               for(i=1; i<=100; i++)
(gdb) n
20                       result += i;
(gdb) n
18               for(i=1; i<=100; i++)
(gdb) n
20                       result += i;
(gdb) c          <--------------------- 繼續運行程序, continue命令簡寫。
Continuing.
result[1-100] = 5050       <---------- 程序輸出。

Breakpoint 2, func (n=250) at tst.c:5
5                int sum=0,i;
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p i        <--------------------- 打印變量 i的值, print命令簡寫。
$1 = 134513808
(gdb) n
8                        sum+=i;
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8                        sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p sum
$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     <--------------------- 繼續運行。
Continuing.
result[1-250] = 31375    <---------- 程序輸出。

Program exited with code 027. <-------- 程序退出,調試結束。
(gdb) q     <--------------------- 退出 gdb。
hchen/test>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章