サンプル集 |
■VC ■C# ■Java ■LinuxC ■MS-DOS ■bash ■Excel VBA ■VBScript ■PHP ■HTML ■perl ■iPhone ■Android ■Lua ■other |
Linuxコマンド |
debian メモ |
apache2 メモ |
MySQL メモ |
Oracle メモ |
HOME |
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 | ||
|
makefile | ||
|
◆よく使う gdbのコマンド
run | 実行。 |
s | 1ステップ実行。 |
b n | ブレイクポイントを n 行目に設定。 |
b met | ブレイクポイントを met 関数に設定。 |
p var | var の値を表示。 |
p var = val | var に val の値をセット。 |
q | gdb 終了。 |
◆実行結果
$ ./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
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
Copyright (C) 2012 ymlib.com