Skip to content

Commit 7d0f975

Browse files
authored
Implement #67 (#68)
- Preset breakpoints in per project .bashdbrc - Fix failure to set conditional breakpoint using condition command - Document startup behavior under "entry-exit" section Signed-off-by: Justin Zhang <[email protected]>
1 parent eae5e90 commit 7d0f975

File tree

10 files changed

+129
-8
lines changed

10 files changed

+129
-8
lines changed

command/condition.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function _Dbg_do_condition {
6565
return 3
6666
fi
6767

68+
condition=$@
6869
if [[ -z $condition ]] ; then
6970
condition=1
7071
_Dbg_msg "Breakpoint $n now unconditional."

dbg-main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ if (( 0 == _Dbg_o_nx)) && [[ -r "$_Dbg_startup_cmdfile" ]] ; then
5959
_Dbg_do_source "$_Dbg_startup_cmdfile"
6060
fi
6161

62+
typeset _Dbg_per_project_cmdfile=$(pwd)/.${_Dbg_debugger_name}rc
63+
if (( 0 == _Dbg_o_nx)) && [[ -r "$_Dbg_per_project_cmdfile" ]] ; then
64+
_Dbg_do_source "$_Dbg_per_project_cmdfile"
65+
fi
66+
6267
# _Dbg_DEBUGGER_LEVEL is the number of times we are nested inside a debugger
6368
# by virtue of running "debug" for example.
6469
if [[ -z "${_Dbg_DEBUGGER_LEVEL}" ]] ; then

docs/entry-exit.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,61 @@ Entering the Bash Debugger
44
.. toctree::
55
.. contents::
66

7+
Startup Behavior
8+
================
9+
10+
You can customize bashdb's initialization process such as setting up
11+
breakpoints to facilitate complex issue trouble shooting. This can be achieved
12+
by creating per-user or per-project rc files named ``.bashdbrc`` under your
13+
home directory or any directory in your project. The per-user rc file is loaded
14+
first, followed by the per-project rc file. Therefore, you can override
15+
settings in your per-project ``.bashdbrc`` file.
16+
17+
The follow code snippet demonstrates a per-project ``.bashdbrc`` with a few
18+
breakpoints configured:
19+
20+
.. code:: console
21+
22+
$ cd my-project
23+
$ cat .bashdbrc
24+
25+
# explicit load is required to make
26+
# code in this file available to bashdb
27+
load ./libs/functions.sh
28+
break ./main.sh:13 $cmd == "start"
29+
break ./libs/functions.sh:332
30+
31+
Currently explicit loading of programs invoked by main script is required.
32+
Therefore, in this example, the ``load`` command makes the code defined in the
33+
``libs/functions.sh`` available to the debugging session. Once the per-project
34+
``.bashdbrc`` is configured, you can launch the debugger under the directory
35+
where the ``.bashdbrc`` located as follows:
36+
37+
.. code:: console
38+
39+
$ bashdb main.sh start
40+
41+
bash debugger, bashdb, release 5.2-1.1.2
42+
43+
Copyright 2002-2004, 2006-2012, 2014, 2016-2019, 2021, 2023-2024 Rocky Bernstein
44+
This is free software, covered by the GNU General Public License, and you are
45+
welcome to change it and/or distribute copies of it under certain conditions.
46+
47+
(/home/user/my-project/main.sh:3):
48+
3: source ./libs/functions.sh
49+
File /home/user/my-project/libs/functions.sh loaded.
50+
Breakpoint 1 set in file /home/user/my-project/libs/functions.sh, line 332.
51+
Breakpoint 2 set in file /home/user/my-project/main.sh, line 13.
52+
bashdb<4> info breakpoints
53+
Num Type Disp Enb What
54+
1 breakpoint keep y /home/user/my-project/libs/functions.sh:332
55+
2 breakpoint keep y /home/user/my-project/main.sh:13
56+
stop only if $cmd == "start"
57+
58+
In this example, bashdb shows the two breakpoints presetted by the
59+
``.bashdbrc`` file when it finishes startup. The ``info breakpoints`` command,
60+
abbreviated as ``i b``, reveals the second breakpoint is a conditional
61+
breakpoint.
762

863
Invoking the Debugger Initially
964
===============================

lib/filecache.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ _Dbg_filecache_reset() {
3838
}
3939
_Dbg_filecache_reset
4040

41-
# Check that line $2 is not greater than the number of lines in
42-
# file $1
41+
# Check that line $1 is not greater than the number of lines in
42+
# file $2
4343
_Dbg_check_line() {
4444
(( $# != 2 )) && return 1
4545
typeset -i line_number=$1

test/data/brkpt1.right

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ Num Type Disp Enb What
9696
+condition x==1
9797
** condition: Bad breakpoint number: x==1
9898
+condition 4 x==1
99-
Breakpoint 4 now unconditional.
10099
+condition bad
101100
** condition: Bad breakpoint number: bad
102101
+condition 30 y==1
@@ -109,6 +108,7 @@ Num Type Disp Enb What
109108
----------------------------
110109
2 breakpoint keep n dbg-test1.sh:22
111110
4 breakpoint keep y dbg-test1.sh:23
111+
stop only if x==1
112112
5 breakpoint keep n dbg-test1.sh:23
113113
stop only if x==0
114114
6 breakpoint keep y dbg-test1.sh:24

test/data/preset-brkpt.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
info break

test/data/preset-brkpt.right

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(gcd.sh:24):
2+
24: gcd $1 $2
3+
Breakpoint 1 set in file gcd.sh, line 8.
4+
Breakpoint 2 set in file gcd.sh, line 11.
5+
Breakpoint 3 set in file gcd.sh, line 15.
6+
Num Type Disp Enb What
7+
----------------------------
8+
1 breakpoint keep y gcd.sh:8
9+
2 breakpoint keep y gcd.sh:11
10+
3 breakpoint keep y gcd.sh:15
11+
stop only if a==1
12+
13+
bashdb: That's all, folks...

test/integration/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ TESTS = \
7676
test-tbreak \
7777
test-trace \
7878
test-watch1 \
79-
test-watch2
79+
test-watch2 \
80+
test-preset-brkpt
8081

8182
TESTS_ENVIRONMENT = \
8283
srcdir="$(abs_srcdir)" \

test/integration/check-common.sh.in

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ TEST_FILTERED_FILE="/tmp/${TEST_NAME}-filtered.check"
6262
RIGHT_FILTERED_FILE="/tmp/${TEST_NAME}-filtered.right"
6363

6464
run_test_check() {
65-
short_script_name=${1:-$TEST_NAME}
66-
short_test_name=${2:-$TEST_NAME}
67-
debugged_script=${3:-"${top_srcdir}/test/example/${short_script_name}.sh"}
65+
short_script_name=$1
66+
short_test_name=$2
67+
debugged_script=$3
68+
with_init=$4
69+
[[ -z $short_script_name ]] && short_script_name=$TEST_NAME
70+
[[ -z $short_test_name ]] && short_test_name=$TEST_NAME
71+
[[ -z $debugged_script ]] && debugged_script="${top_srcdir}/test/example/${short_script_name}.sh"
72+
[[ -z $with_init ]] && with_init="no"
6873

6974
# Reassign variables to allow overrides via the above parameters
7075
TEST_FILE="${top_builddir}/test/integration/${short_test_name}.check"
@@ -78,7 +83,12 @@ run_test_check() {
7883
print -r -- "You need to set srcdir before running this."
7984
exit 10
8085
fi
81-
(cd $srcdir && run_debugger "$debugged_script" 2>&1 >"$TEST_FILE" </dev/null)
86+
if [[ $with_init == "no" ]]; then
87+
(cd $srcdir && run_debugger "$debugged_script" 2>&1 >"$TEST_FILE" </dev/null)
88+
else
89+
dbg_opts="-L ${top_srcdir} ${run_debugger_opts// --no-init/}"
90+
(cd $srcdir && run_debugger "$debugged_script" "$dbg_opts" 2>&1 >"$TEST_FILE" </dev/null)
91+
fi
8292
check_output "$TEST_FILE" "$RIGHT_FILE"
8393
# Return code tells testing mechanism whether passed or not.
8494
return $?

test/integration/test-preset-brkpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
t=${0##*/}; TEST_NAME=${t:5} # basename $0 with 'test-' stripped off
3+
4+
[ -z "$builddir" ] && builddir="$PWD"
5+
. "${builddir}/check-common.sh"
6+
7+
function _preset_brkpt_cleanup {
8+
[[ -f "${builddir}/.bashdbrc" ]] && rm -f "${builddir}/.bashdbrc"
9+
if [[ -f "${HOME}/.bashdbrc-test-rename" ]]; then
10+
mv "${HOME}/.bashdbrc-test-rename" "${HOME}/.bashdbrc"
11+
fi
12+
}
13+
# setup trap to ensure cleanup of .bashdbrc
14+
trap _preset_brkpt_cleanup EXIT INT TERM
15+
16+
# rename the ~/.bashdbrc to avoid interference
17+
if [[ -f "${HOME}/.bashdbrc" ]]; then
18+
mv "${HOME}/.bashdbrc" "${HOME}/.bashdbrc-test-rename"
19+
fi
20+
21+
# create a per-project .bashdbrc under working directory
22+
cat<<EOF > "${builddir}/.bashdbrc"
23+
break gcd
24+
break 11
25+
break 15 if a==1
26+
EOF
27+
28+
script=gcd
29+
# The fourth argument loads per-project .bashdbrc
30+
run_test_check $script "" "" "yes"
31+
rc=$?
32+
(( $rc != 0 )) && exit $rc
33+
34+
# Return code tells testing mechanism whether passed or not.
35+
exit 0

0 commit comments

Comments
 (0)