Skip to content

Commit 713bb93

Browse files
committed
Trap debugged-program exit
1 parent b77a6cd commit 713bb93

38 files changed

+346
-121
lines changed

command/quit.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ _Dbg_do_quit() {
7474
# Get the last command into the history
7575
# set -o incappendhistory
7676
print -s -- $_Dbg_orig_cmd >/dev/null
77+
if (($_Dbg_in_exit_handler != 0)); then
78+
_Dbg_exit_from_exit_handler=1
79+
return 1
80+
fi
81+
7782
_Dbg_cleanup
7883
fi
7984

command/run.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- shell-script -*-
22
# run command.
33
#
4-
# Copyright (C) 2008-2010, 2016 Rocky Bernstein [email protected]
4+
# Copyright (C) 2008-2010, 2016, 2023 Rocky Bernstein [email protected]
55
#
66
# This program is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU General Public License as
@@ -80,7 +80,9 @@ _Dbg_do_run() {
8080

8181
builtin cd $_Dbg_init_cwd
8282

83-
_Dbg_cleanup
83+
if (( $_Dbg_in_exit_handler != 0)); then
84+
_Dbg_cleanup
85+
fi
8486
eval "exec $exec_cmd"
8587
}
8688

lib/sig.sh

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Signal handling routines
33
#
44
# Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2010,
5-
# 2011 Rocky Bernstein <[email protected]>
5+
# 2011, 2023 Rocky Bernstein <[email protected]>
66
#
77
# This program is free software; you can redistribute it and/or
88
# modify it under the terms of the GNU General Public License as
@@ -32,6 +32,11 @@ typeset -i _Dbg_QUIT_ON_QUIT=0
3232
# Return code that debugged program reports
3333
typeset -i _Dbg_program_exit_code=0
3434

35+
# 1 if we we running post-mortem in exit handler?
36+
typeset -i _Dbg_in_exit_handler=0
37+
# Allow Quit and things like that to signal a final quit.
38+
typeset -i _Dbg_exit_from_exit_handler=0
39+
3540
############################################################
3641
## Signal arrays: These are indexed by the signal number. ##
3742
############################################################
@@ -109,39 +114,15 @@ _Dbg_subst_handler_var() {
109114
# command loop
110115
_Dbg_exit_handler() {
111116

112-
# Consider putting the following line(s) in a routine.
113-
# Ditto for the restore environment
114117
typeset -i _Dbg_debugged_exit_code=$?
115118

116119
# Turn off line and variable trace listing; allow unset parameter expansion.
117120
set +x +v +u
118121

119-
if [[ ${_Dbg_sig_print[0]} == "print" ]] ; then
120-
# Note: use the same message that gdb does for this.
121-
_Dbg_msg "Program received signal EXIT (0)..."
122-
if [[ ${_Dbg_sig_show_stack[0]} == "showstack" ]] ; then
123-
_Dbg_do_backtrace 0
124-
fi
125-
fi
126-
127122
if [[ $_Dbg_old_EXIT_handler != '' ]] ; then
128123
eval $_Dbg_old_EXIT_handler
129124
fi
130125

131-
# If we've set the QUIT signal handler not to stop, or we've in the
132-
# middle of leaving so many (subshell) levels or we have set to
133-
# leave quietly on termination, then do it!
134-
135-
if [[ ${_Dbg_sig_stop[0]} != "stop" ]] \
136-
|| (( _Dbg_QUIT_LEVELS != 0 )) \
137-
|| (( _Dbg_QUIT_ON_QUIT )) ; then
138-
_Dbg_do_quit
139-
# We don't return from here.
140-
fi
141-
142-
# We've tested for all the quitting conditions above.
143-
# Even though this is an exit handler, don't exit!
144-
145126
typeset term_msg="normally"
146127
if [[ $_Dbg_debugged_exit_code != 0 ]] ; then
147128
term_msg="with code $_Dbg_debugged_exit_code"
@@ -154,10 +135,16 @@ _Dbg_exit_handler() {
154135
"Debugged program terminated $term_msg. Use q to quit or R to restart."
155136

156137
_Dbg_running=0
138+
_Dbg_exit_from_exit_handler=0
139+
_Dbg_in_exit_handler=1
157140
while : ; do
158-
_Dbg_process_commands
141+
_Dbg_process_commands
142+
if (($_Dbg_exit_from_exit_handler != 0)); then
143+
break
144+
fi
159145
done
160146
fi
147+
return $((128+$_Dbg_debugged_exit_code))
161148
}
162149

163150
# Generic signal handler. We consult global array _Dbg_sig_* for how
@@ -280,3 +267,7 @@ _Dbg_init_default_traps() {
280267
_Dbg_init_trap TERM "print" "showstack" "stop"
281268
# _Dbg_init_trap TRAP "print" "showstack" "stop"
282269
}
270+
271+
272+
set -o localtraps
273+
trap '_Dbg_exit_handler' EXIT

test/data/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ check_DATA = \
3535
export.cmd \
3636
frame.cmd \
3737
frame.right \
38+
frame-5.8.1.right \
3839
list.cmd \
3940
list.right \
4041
multi1.cmd \

test/data/action.right

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(dbg-test1.sh:4):
22
fn1 () { echo "fn1 here" x=5 fn3 }
33
+set basename on
4-
Show short filenames (the basename) is on.
4+
Show short filenames (the basename) is on.
55
+# Debugger test of action command, and some $ vars
66
+#
77
+# Show actions
@@ -38,3 +38,6 @@ dollar 0 is dbg-test1.sh
3838
$? is 0
3939
+quit
4040
zshdb: That's all, folks...
41+
Debugged program terminated normally. Use q to quit or R to restart.
42+
43+
zshdb: That's all, folks...

test/data/brkpt1.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ Num Type Disp Enb What
6565
+# L
6666
+quit
6767
zshdb: That's all, folks...
68+
Debugged program terminated normally. Use q to quit or R to restart.
69+
70+
zshdb: That's all, folks...

test/data/bug-args.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ Dollar 0 from eval is now foo
4242
$? is 0
4343
+quit
4444
zshdb: That's all, folks...
45+
Debugged program terminated normally. Use q to quit or R to restart.
46+
47+
zshdb: That's all, folks...

test/data/bug-delete.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ echo line 7
2828
+# Should have stopped at line 7 above
2929
+quit
3030
zshdb: That's all, folks...
31+
Debugged program terminated normally. Use q to quit or R to restart.
32+
33+
zshdb: That's all, folks...

test/data/bug-errexit.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ print one
1212
->0 in file `bug-errexit.sh' at line 6
1313
+quit
1414
zshdb: That's all, folks...
15+
Debugged program terminated normally. Use q to quit or R to restart.
16+
17+
zshdb: That's all, folks...

test/data/bug-ksharrays.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ shwordsplit
2020
$? is 0
2121
+quit
2222
zshdb: That's all, folks...
23+
Debugged program terminated normally. Use q to quit or R to restart.
24+
25+
zshdb: That's all, folks...

0 commit comments

Comments
 (0)