理想未来ってなんやねん

娘可愛い。お父さん頑張る。

bashdbによるシェルスクリプトのデバッグ

本日のbashメモ
bashdbを使用することでbashスクリプトgdbライクにデバッグすることができます。

インストール

Mac OS Xの場合

MacPortsにパッケージが用意されているのでportコマンドでインストールする

$ sudo port install bashdb
CentOS 5の場合

CentOS 5の場合、yumではインストール出来ないためソースからビルドしてインストールする。

$ sudo yum -y install texi2html
$ wget http://ftp.jaist.ac.jp/pub/sourceforge/b/project/ba/bashdb/bashdb/4.0-0.4/bashdb-4.0-0.4.tar.gz
$ tar xvzf bashdb-4.1-0.4.tar.gz
$ cd bashdb-4.1-0.4
$ ./configure
$ make
$ sudo make check
$ sudo make install

使い方

起動

bashdbの後にデバッグするスクリプトファイル名を入力する

bashdb スクリプト名

-cオプションで文字列を指定して実行できる。

bashdb -c "count=1; while [ \$count -le 5 ]; do echo \"\$count\"; count=\`expr \$count + 1\`; done; "
helpを表示

helpと入力することでコマンドの一覧を表示できる。
helpのエイリアスはh

bashdb<0> help
Available commands:
  /          debug    enable   help     next     show    step-      untrace
  alias      delete   eval     history  print    signal  tbreak     up     
  break      disable  examine  info     pwd      skip    trace      watch  
  commands   display  file     kill     quit     source  tty        where  
  condition  down     frame    list     restart  step    unalias  
  continue   edit     handle   load     set      step+   undisplay


helpの後にコマンド名を入力するとコマンドのhelpが表示できる。

bashdb<1> help help
help    -- Print list of commands.

Aliases for help: h

step

stepで一行実行を進める。
エイリアスはs

bashdb<0> s
(/tmp/bashdbcmd4012:1):
1:      count=1; while [ $count -le 5 ]; do echo "$count"; count=`expr $count + 1`; done;
[ $count -le 5 ]


sの後に続けて数値を入力すると、入力した値分実行を進める

bashdb<0> s 2
(/tmp/bashdbcmd3818:1):
1:      count=1; while [ $count -le 5 ]; do echo "$count"; count=`expr $count + 1`; done;
echo "$count"

continue

continueで実行を継続できる
エイリアスはcもしくはcont

bashdb<1> continue
1
2
3
4
5
Debugged program terminated normally. Use q to quit or R to restart.

watche

watcheコマンドでプログラムを停止する条件を指定できる。
エイリアスはWe

bashdb<0> watche count == 3
 0: (count == 3)==0 arith: 1
bashdb<1> continue
1
2
Watchpoint 0: count == 3 changed:
  old value: '0'
  new value: '1'
(/tmp/bashdbcmd14159:1):
1:      count=1; while [ $count -le 5 ]; do echo "$count"; count=`expr $count + 1`; done;
[ $count -le 5 ]

info

infoコマンドで現在の状態を表示できる。

infoコマンドの後に続けてサブコマンドを指定する。
以下は、よく使いそうなサブコマンドの一つvariablesの例

bashdb<20> info variables
BASH="/bin/bash"
〜〜〜中略〜〜〜
text_cell="watchpoints"
text_row="breakpoints files program sources terminal watchpoints"

setコマンドで表示される内容と同じ


その他のinfoコマンドはhelp参照

bashdb<19> help info
List of info subcommands:

info args -- Argument variables (e.g. $1, $2, ...) of the current stack frame.
info breakpoints -- Status of user-settable breakpoints
info display -- Show all display expressions
info files -- Source files in the program
info functions -- All function names
info line -- list current line number and and file name
info program -- Execution status of the program.
info signals -- What debugger does when program gets various signals
info source -- Information about the current source file
info stack -- Backtrace of the stack
info terminal -- Print terminal device
info variables -- All global and static variable names
info warranty -- Various kinds of warranty you do not have

print

printで現在の変数の値を表示する
エイリアスはp

pの後に続けて変数名を入力することで変数の値を表示できる。

bashdb<1> p $count
2

再実行

restartコマンドで最初から実行できる
エイリアスはR

bashdb<0> restart
Restarting with: /usr/local/bin/bashdb -c count=1\;\ while\ \[\ \$count\ -le\ 5\ \]\;\ do\ echo\ \"\$count\"\;\ count=\`expr\ \$count\ +\ 1\`\;\ done\;\  
bash Shell Debugger, release 4.0-0.4

Copyright 2002, 2003, 2004, 2006, 2007, 2008, 2009 Rocky Bernstein
This 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.

(/tmp/bashdbcmd6254:1):
1:      count=1; while [ $count -le 5 ]; do echo "$count"; count=`expr $count + 1`; done;
quit 終了

quitでbashdbを終了できる。
エイリアスはq