Skip to content

Commit

Permalink
bash debugger added
Browse files Browse the repository at this point in the history
  • Loading branch information
kt97679 committed Jul 3, 2022
1 parent 8b30058 commit a2b9e07
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bash-debugger/bdb-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -eu

print_arg() {
local j=$((i+1))
echo "$j: $1"
i=$j
}

i=0
while (( $# )); do
print_arg "$1"
shift
done
36 changes: 36 additions & 0 deletions bash-debugger/bdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

__dbg__breakpoints=()
__dbg__trace=2
__dbg__trap() {
local __dbg__cmd __dbg__cmd_args __dbg__set="$(set +o)" \
__dbg__do_break=false
set +eu
((__dbg__trace == 1)) \
&& echo "+(${BASH_SOURCE[1]}:${BASH_LINENO[0]}): $BASH_COMMAND"
for __dbg__breakpoint in "${__dbg__breakpoints[@]}"; do
eval "$__dbg__breakpoint" && __dbg__do_break=true && break
done
((__dbg__trace == 2)) || $__dbg__do_break && {
((__dbg__trace == 0)) \
&& echo "+(${BASH_SOURCE[1]}:${BASH_LINENO[0]}): $BASH_COMMAND"
((__dbg__trace == 2)) && __dbg__trace=0
while read -p "bdb> " __dbg__cmd __dbg__cmd_args; do
case $__dbg__cmd in
'') eval "$__dbg__set" && return 0 ;;
trace) ((__dbg__trace ^= 1)) ;;
bl) printf "%s\n" "${__dbg__breakpoints[@]}" \
| grep . | cat -n ;;
ba) __dbg__breakpoints+=("$__dbg__cmd_args") ;;
bd) unset __dbg__breakpoints[$((__dbg__cmd_args - 1))] \
&& __dbg__breakpoints=("${__dbg__breakpoints[@]}") ;;
*) eval "$__dbg__cmd $__dbg__cmd_args" ;;
esac
done
}
}

set -T
trap "__dbg__trap" debug

. "$@"

0 comments on commit a2b9e07

Please sign in to comment.