#! /bin/bash
echo 'Bash Debugger version 1.2.4'
export _dbname=${0
if test $ echo "$_dbname: Usage: $_dbname filename" >&2
exit 1
fi
_guineapig=$1
if test ! -r $1; then
echo "$_dbname: Cannot read file '$_guineapig'." >&2
exit 1
fi
shift
__debug=${TMPDIR-/tmp}/bashdb.$$
sed -e '/^cat $_guineapig >> $__debug
exec ${DEBUG_SHELL-bash} $__debug $_guineapig "$@"
exit 1
__debug=$0
_guineapig=$1
__steptrap_calls=0
shift
shopt -s extglob
function _steptrap
{
local i=0
_curline=$1
if (( ++__steptrap_calls > 1 && $_curline == 1 )); then
return
fi
if [ -n "$_disps" ]; then
while (( $i < ${ do
if [ -n "${_disps[$i]}" ]; then
_msg "${_disps[$i]}: \c"
eval _msg ${_disps[$i]}
fi
let i=$i+1
done
fi
if (( $_trace )); then
_showline $_curline
fi
if (( $_steps >= 0 )); then
let _steps="$_steps - 1"
fi
if _at_linenumbp ; then
_msg "Reached breakpoint at line $_curline"
_showline $_curline
_cmdloop
elif [ -n "$_brcond" ] && eval $_brcond; then
_msg "Break condition $_brcond true at line $_curline"
_showline $_curline
_cmdloop
elif (( $_steps == 0 )); then
if (( ! $_trace )); then
_msg "Stopped at line $_curline"
_showline $_curline
fi
_cmdloop
fi
}
function _setbp
{
local i f line _x
if [ -z "$1" ]; then
_listbp
return
fi
eval "$_seteglob"
if [[ $1 == *(\+)[1-9]*([0-9]) ]]; then
case $1 in
+*)
_x=${1 _x=${x%%*([!0-9])} f=$(( $1 + $_x ))
;;
*)
f=$(( $1 ))
;;
esac
line="${_lines[$f]}"
while _invalidbreakp $f
do
(( f++ ))
line="${_lines[$f]}"
done
if (( $f != $1 ))
then
_msg "Line $1 is not a valid breakpoint"
fi
if [ -n "${_lines[$f]}" ]; then
_linebp[$1]=$1;
_msg "Breakpoint set at line $f"
else
_msg "Breakpoints can only be set on executable lines"
fi
else
_msg "Please specify a numeric line number"
fi
eval "$_resteglob"
}
function _listbp
{
local i
if [ -n "$_linebp" ]; then
_msg "Breakpoints:"
for i in ${_linebp[*]}; do
_showline $i
done
else
_msg "No breakpoints have been set"
fi
}
function _clearbp
{
local i
if [ -z "$1" ]; then
read -e -p "Delete all breakpoints? "
case $REPLY in
[yY]*)
unset _linebp[*]
_msg "All breakpoints have been cleared"
;;
esac
return 0
fi
eval "$_seteglob"
if [[ $1 == [1-9]*([0-9]) ]]; then
unset _linebp[$1]
_msg "Breakpoint cleared at line $1"
else
_msg "Please specify a numeric line number"
fi
eval "$_resteglob"
}
function _setbc
{
if (( $ _brcond=$@
_msg "Break when true: $_brcond"
else
_brcond=
_msg "Break condition cleared"
fi
}
function _setdisp
{
if [ -z "$1" ]; then
_listdisp
else
_disps[${ if (( ${ then
_msg " ${#_disps[@]}: $1"
else
_msg "${#_disps[@]}: $1"
fi
fi
}
function _listdisp
{
local i=0 j
if [ -n "$_disps" ]; then
while (( $i < ${ do
let j=$i+1
if (( ${ then
_msg " $j: ${_disps[$i]}"
else
_msg "$j: ${_disps[$i]}"
fi
let i=$j
done
else
_msg "No displays have been set"
fi
}
function _cleardisp
{
if (( $ read -e -p "Delete all display expressions? "
case $REPLY in
[Yy]*)
unset _disps[*]
_msg "All breakpoints have been cleared"
;;
esac
return 0
fi
eval "$_seteglob"
if [[ $1 == [1-9]*([0-9]) ]]; then
unset _disps[$1]
_msg "Display $i has been cleared"
else
_listdisp
_msg "Please specify a numeric display number"
fi
eval "$_resteglob"
}
function _ftrace
{
local _opt=-t _tmsg="enabled" _func
if [[ $1 == -u ]]; then
_opt=+t
_tmsg="disabled"
shift
fi
for _func; do
declare -f $_opt $_func
_msg "Tracing $_tmsg for function $_func"
done
}
function _cmdloop
{
local cmd args
while read -e -p "bashdb> " cmd args; do
test -n "$cmd" && history -s "$cmd $args" test -n "$cmd" || { set $_lastcmd; cmd=$1; shift; args=$*; }
if [ -n "$cmd" ]
then
case $cmd in
b|br|bre|brea|break)
_setbp $args
_lastcmd="break $args"
;;
co|con)
_msg "ambiguous command: '$cmd', condition, continue?"
;;
cond|condi|condit|conditi|conditio|condition)
_setbc $args
_lastcmd="condition $args"
;;
c|cont|conti|contin|continu|continue)
_lastcmd="continue"
return
;;
d)
_msg "ambiguous command: '$cmd', delete, display?"
;;
de|del|dele|delet|delete)
_clearbp $args
_lastcmd="delete $args"
;;
di|dis|disp|displ|displa|display)
_setdisp $args
_lastcmd="display $args"
;;
f|ft|ftr|ftra|ftrace)
_ftrace $args
_lastcmd="ftrace $args"
;;
\?|h|he|hel|help)
_menu
_lastcmd="help"
;;
l|li|lis|list)
_displayscript $args
;;
p|pr|pri|prin|print)
_examine $args
_lastcmd="print $args"
;;
q|qu|qui|quit)
exit
;;
s|st|ste|step|n|ne|nex|next)
let _steps=${args:-1}
_lastcmd="next $args"
return
;;
t|tr|tra|trac|trace)
_xtrace
;;
u|un|und|undi|undis|undisp|undispl|undispla|undisplay)
_cleardisp $args
_lastcmd="undisplay $args"
;;
!*)
eval ${cmd _lastcmd="$cmd $args"
;;
*)
_msg "Invalid command: '$cmd'"
;;
esac
fi
done
}
function _at_linenumbp
{
[[ -n ${_linebp[$_curline]} ]]
}
function _invalidbreakp
{
local line=${_lines[$1]}
if test -z "$line" \
|| expr "$line" : '[ \t]* || expr "$line" : '[ \t]*;;[ \t]*$' > /dev/null \
|| expr "$line" : '[ \t]*[^)]*)[ \t]*$' > /dev/null \
|| expr "$line" : '[ \t]*;;[ \t]* || expr "$line" : '[ \t]*[^)]*)[ \t]*;;[ \t]*$' > /dev/null \
|| expr "$line" : '[ \t]*[^)]*)[ \t]*;;*[ \t]* then
return 0
fi
return 1
}
function _examine
{
if [ -n "$*" ]; then
_msg "$args: \c"
eval _msg $args
else
_msg "Nothing to print"
fi
}
function _displayscript
{
local i j start end bp cl
if (( $ if [ $1 = "%" ]; then
let start=1
let end=${ else
let start=$1-5
let end=$1+5
fi
elif (( $ if [ $1 = "^" ]; then
let start=1
else
let start=$1
fi
if [ $2 = "\$" ]; then
let end=${ else
let end=$2
fi
else let start=$_curline-5
let end=$_curline+5
fi
if (( $start < 1 )); then
start=1
fi
if (( $end > ${ end=${ fi
cl=$(( $end - $start ))
if (( $cl > ${LINES-24} )); then
pager=${PAGER-more}
else
pager=cat
fi
i=$start
( while (( $i <= $end )); do
_showline $i
let i=$i+1
done ) 2>&1 | $pager
start=$(( $end + 1 ))
end=$(( $start + 11 ))
if (( $end > ${ then
end=${ fi
_lastcmd="list $start $end"
}
function _xtrace
{
let _trace="! $_trace"
if (( $_trace )); then
_msg "Execution trace on"
else
_msg "Execution trace off"
fi
}
function _msg
{
echo -e "$@" >&2
}
function _showline
{
local i=0 bp=' ' line=$1 cl=' '
if [[ -n ${_linebp[$line]} ]]; then
bp='*'
fi
if (( $_curline == $line )); then
cl=">"
fi
if (( $line < 100 )); then
_msg "${_guineapig/*\//}:$line $bp $cl${_lines[$line]}"
elif (( $line < 10 )); then
_msg "${_guineapig/*\//}:$line $bp $cl${_lines[$line]}"
elif (( $line > 0 )); then
_msg "${_guineapig/*\//}:$line $bp $cl${_lines[$line]}"
fi
}
function _cleanup
{
rm -f $__debug $_potbelliedpig 2> /dev/null
}
function _menu
{
_msg 'bashdb commands:
break N set breakpoint at line N
break list breakpoints & break condition
condition foo set break condition to foo
condition clear break condition
delete N clear breakpoint at line N
delete clear all breakpoints
display EXP evaluate and display EXP for each debug step
display show a list of display expressions
undisplay N remove display expression N
list N M display all lines of script between N and M
list N display 5 lines of script either side of line N
list display 5 lines if script either side of current line
continue continue execution upto next breakpoint
next [N] execute [N] statements (default 1)
print expr prints the value of an expression
trace toggle execution trace on/off
ftrace [-u] func make the debugger step into function FUNC
(-u turns off tracing FUNC)
help print this menu
! string passes string to a shell
quit quit'
}
shopt -u extglob
HISTFILE=~/.bashdb_history
set -o history
set +H
_seteglob='local __eopt=-u ; shopt -q extglob && __eopt=-s ; shopt -s extglob'
_resteglob='shopt $__eopt extglob'
_linebp=()
let _trace=0
let _i=1
_potbelliedpig=${TMPDIR-/tmp}/${_guineapig/*\//}.$$
sed 's,\\$,\\\\,' $_guineapig > $_potbelliedpig
_msg "Reading source from file: $_guineapig"
while read; do
_lines[$_i]=$REPLY
let _i=$_i+1
done < $_potbelliedpig
trap _cleanup EXIT
let _steps=1
LINENO=-1
trap '_steptrap $LINENO' DEBUG