サンプル集  >  LinuxC  >  デバッグ(gdb)
デバッグ(gdb)
2005/11/28

gdbを使用したデバッグの例です。 関数 FuncA は 0 しか返さないため、16行目のif文の結果は常に偽になります。 gdbを使用し、値の再設定を行い、if文の中を通します。

◆環境
OS Linux 2.6.9-5.0.3.EL
gcc 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)

LC048.c
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
/* 2005/11/04 (c) ymlib.com */
/* gdbのテスト */
#include <stdio.h>
#include <stdlib.h>

int FuncA( void );

int main( int argc, char** argv )
{
    int iRet;

    printf( "LC048 start!!\n" );

    /* 関数呼び出し */
    iRet = FuncA();
    if ( iRet != 0 )
    {
        printf( "LC048 - FuncA Error!!\n" );
        exit( -999 );
    }

    printf( "LC048 end!!\n" );

    return( 0 );
}

int FuncA( void )
{
    return( 0 );
}

makefile
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
# 2005/11/04 (c) ymlib.com
TARGET = LC048
OBJS = LC048.o
CC = /usr/bin/gcc

$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS)
.SUFFIXES: .c.o
.c.o:
$(CC) -Wall -c $< -g

clean:
rm -f $(TARGET) $(OBJS)

◆よく使う gdbのコマンド
run実行。
s1ステップ実行。
nブレイクポイントを n 行目に設定。
metブレイクポイントを met 関数に設定。
varvar の値を表示。
var = valvar に val の値をセット。
qgdb 終了。

◆実行結果
$ ./LC048
LC048 start!!
LC048 end!!

$ gdb LC048
GNU gdb Red Hat Linux (6.3.0.0-0.31.fj1rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and y
ou are
welcome to change it and/or distribute copies of it under certain cond
itions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for det
ails.
This GDB was configured as "ia64-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) run  ←プログラムを実行
Starting program: /home/ymusr/work/W4/LC048/LC048
Failed to read a valid object file image from memory.
LC048 start!!
LC048 end!!

Program exited normally.
(gdb) b 16  ←ブレイクポイントを16行目に設定
Breakpoint 1 at 0x40000000000007e2: file LC048.c, line 16.
(gdb) run  ←プログラムを実行
Starting program: /home/ymusr/work/W4/LC048/LC048
Failed to read a valid object file image from memory.
LC048 start!!

Breakpoint 1, main (argc=1, argv=0x60000fffffffb498) at LC048.c:16
16          if ( iRet != 0 )  ←16行目で止まる
(gdb) p iRet  ←変数 iRet の値を表示
$1 = 0
(gdb) p iRet=1  ←変数 iRet に 1 を設定
$2 = 1
(gdb) s  ←1ステップ実行
18              printf( "LC048 - FuncA Error!!\n" );
(gdb) s
LC048 - FuncA Error!!
19              exit( -999 );
(gdb) s

Program exited with code 031.
(gdb) q  ←gdb終了

$ gdb LC048
GNU gdb Red Hat Linux (6.3.0.0-0.31.fj1rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and y
ou are
welcome to change it and/or distribute copies of it under certain cond
itions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for det
ails.
This GDB was configured as "ia64-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) s
The program is not being run.
(gdb) b main  ←main 関数をブレイクポイントに設定
Breakpoint 1 at 0x4000000000000792: file LC048.c, line 12.
(gdb) run
Starting program: /home/ymusr/work/W4/LC048/LC048
Failed to read a valid object file image from memory.

Breakpoint 1, main (argc=1, argv=0x60000fffffffb498) at LC048.c:12
12          printf( "LC048 start!!\n" );←main関数の最初の実行文で停止
(gdb) s
LC048 start!!
15          iRet = FuncA();
(gdb) s
FuncA () at LC048.c:29
29          return( 0 );
(gdb) s
30      }
(gdb) s
30      }
(gdb) s
main (argc=1, argv=0x60000fffffffb498) at LC048.c:15
15          iRet = FuncA();
(gdb) s
16          if ( iRet != 0 )
(gdb) s
22          printf( "LC048 end!!\n" );
(gdb) s
LC048 end!!
24          return( 0 );
(gdb) s
25      }
(gdb) s
25      }
(gdb) s
0x2000000000081550 in __libc_start_main () from /lib/tls/libc.so.6.1
(gdb) s
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
(gdb) q

▲ PageTop  ■ Home


Copyright (C) 2012 ymlib.com