GDB でのコマンド実行を自動化する。
はじめに
本記事は、GDBのコマンドを自動化するやり方を説明するものである。
本記事で使用したファイルは下記に置く。
https://github.com/tomohikoseven/autorun
自動化の前の準備
自動的に実行するコマンドをファイルに記述する必要がある。
下記に、GDBで動かすバイナリファイルのソース( fib.c )と自動実行するコマンドのファイル( autorun_test )を示す。
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ cat fib.c #includeint fib( int n ) { if( n < 0 ){ return -1; } if( n == 0 ){ return 1; } if( n == 1 ){ return 1; } if( n > 1 ){ return fib( n -1 ) + fib( n-2 ); } } int main(void) { int n = 5; printf( "fib(%d)=%d\n", n, fib( n ) ); return 0; }
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ cat autorun_test b fib run list disable 1 c quit
GDBのコマンドについては、他サイトを参照。
GDB自動化 .gdbinit編
autorun_test をコピーして、.gdbinit ファイルを作る。
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ ls -la 合計 40 drwxrwxr-x 2 andre andre 4096 2012-09-09 11:32 . drwxrwxr-x 3 andre andre 4096 2012-09-09 11:07 .. -rw------- 1 andre andre 32 2012-09-09 11:32 .gdbinit ★ autorun_testをコピーして作る -rw-rw-r-- 1 andre andre 275 2012-09-09 11:21 Makefile -rw------- 1 andre andre 32 2012-09-09 11:29 autorun_test -rwxrwxr-x 1 andre andre 8283 2012-09-09 11:21 fib -rw-rw-r-- 1 andre andre 273 2012-09-01 22:06 fib.c -rw-rw-r-- 1 andre andre 2756 2012-09-09 11:21 fib.o
[gdb fib] を実行する。以下にその結果を示す。( fib を作成するMakefileは、最後に載せる )
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ gdb fib GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08 Copyright (C) 2011 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 "i686-linux-gnu". For bug reporting instructions, please see: http://bugs.launchpad.net/gdb-linaro/... Reading symbols from /home/andre/work/gdb/autorun/fib-1.0.0/fib...done. Breakpoint 1 at 0x80483eb: file fib.c, line 6. Breakpoint 1, fib (n=5) at fib.c:6 6 if( n < 0 ){ return -1; } 1 #include2 3 int 4 fib( int n ) 5 { 6 if( n < 0 ){ return -1; } 7 if( n == 0 ){ return 1; } 8 if( n == 1 ){ return 1; } 9 if( n > 1 ){ return fib( n -1 ) + fib( n-2 ); } 10 } fib(5)=8 [Inferior 1 (process 3765) exited normally] andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$
GDB自動化 -xオプション指定、GDBプロンプトでsourceコマンド実行編
-x オプション指定での実行とsourceコマンド実行時の結果を下記に示す。
-x オプション指定
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ gdb fib -x autorun_test GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08 Copyright (C) 2011 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 "i686-linux-gnu". For bug reporting instructions, please see: http://bugs.launchpad.net/gdb-linaro/... Reading symbols from /home/andre/work/gdb/autorun/fib-1.0.0/fib...done. Breakpoint 1 at 0x80483eb: file fib.c, line 6. Breakpoint 1, fib (n=5) at fib.c:6 6 if( n < 0 ){ return -1; } 1 #include2 3 int 4 fib( int n ) 5 { 6 if( n < 0 ){ return -1; } 7 if( n == 0 ){ return 1; } 8 if( n == 1 ){ return 1; } 9 if( n > 1 ){ return fib( n -1 ) + fib( n-2 ); } 10 } fib(5)=8 [Inferior 1 (process 3772) exited normally]
source コマンド実行
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ gdb fib GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08 Copyright (C) 2011 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 "i686-linux-gnu". For bug reporting instructions, please see: http://bugs.launchpad.net/gdb-linaro/... Reading symbols from /home/andre/work/gdb/autorun/fib-1.0.0/fib...done. (gdb) source autorun_test Breakpoint 1 at 0x80483eb: file fib.c, line 6. Breakpoint 1, fib (n=5) at fib.c:6 6 if( n < 0 ){ return -1; } 1 #include2 3 int 4 fib( int n ) 5 { 6 if( n < 0 ){ return -1; } 7 if( n == 0 ){ return 1; } 8 if( n == 1 ){ return 1; } 9 if( n > 1 ){ return fib( n -1 ) + fib( n-2 ); } 10 } fib(5)=8 [Inferior 1 (process 3787) exited normally]
Makefile
Makefileの中身を下記に示す。
andre@andre-VirtualBox:~/work/gdb/autorun/fib-1.0.0$ cat Makefile INSTALLDIR=/home/andre/local CC = gcc CFLAGS=-g all:fib.o $(CC) -o fib fib.o c.o: $(CC) $(CFLAGS) -c $< install: mkdir -p $(DESTDIR)/$(INSTALLDIR)/usr/bin cp fib $(DESTDIR)/$(INSTALLDIR)/usr/bin chmod 755 $(DESTDIR)/$(INSTALLDIR)/usr/bin/fib clean: rm -f fib.o fib