forked from Trepan-Debuggers/bashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashdb-trace.in
executable file
·186 lines (163 loc) · 5.68 KB
/
bashdb-trace.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- shell-script -*-
# This program needs to be SOURCE'd and is not called as an executable
# Note: bashdb-trace is created from bashdb-trace.in
#
# Copyright (C) 2006, 2007, 2008, 2010, 2012, 2019
# Rocky Bernstein <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 59 Temple Place, Suite 330, Boston,
# MA 02111 USA.
#
# The alternate way to invoke debugger, "bash --debugger", has some
# advantages: it sets $0 correctly and doesn't show this script in
# the call trace. However the bash has been a bit inflexible and
# quirky so sadly this script seems to be needed more than it would
# normally.
typeset _Dbg_trace_old_set_opts
_Dbg_trace_old_set_opts=$-
set +u
if [[ ! $BASH_VERSION =~ ^5. ]] ; then
echo "This debugger needs bash version 5 or greater. You have ${BASH_VERSION} " >&2
exit 1
fi
# Name we refer to ourselves by
typeset _Dbg_debugger_name='@PACKAGE@'
# The shell we are configured to run under.
typeset _Dbg_shell='@SH_PROG@'
# The short shell name. Helps keep code common in bash, zsh, and ksh debuggers.
typeset _Dbg_shell_name=${_Dbg_shell##*/}
# Original $0. Note we can't set this in an include.
typeset _Dbg_orig_0=$0
# Equivalent to basename $0; the short program name
typeset _Dbg_pname=${0##*/}
# Show basename only in location listing. This is needed in regression tests
typeset -i _Dbg_set_basename=${BASHDB_BASENAME_ONLY:-0}
typeset _Dbg_main=dbg-main.sh
typeset prefix=@prefix@ # cygwin gets PKGDATADIR wrong
typeset _Dbg_libdir=@PKGDATADIR@
typeset _Dbg_bindir=$(dirname $0)
typeset _Dbg_tmpdir=/tmp
# What to set for location of helper routines?
if [[ ! -e $_Dbg_libdir/$_Dbg_main ]] ; then
# Use bindir/../share as fallback
_Dbg_libdir=
if [[ -d $_Dbg_bindir/../share/bashdb ]] ; then
_Dbg_libdir=$_Dbg_bindir/../share/bashdb
fi
fi
# Parse just the library option
typeset -ax _Dbg_script_args=("$@")
typeset -i i
for ((i=0; $i<${#_Dbg_script_args[@]}-1; i++)) ; do
typeset arg=${_Dbg_script_args[$i]}
if [[ $arg == '-L' || $arg == '--library' ]] ; then
((i++))
_Dbg_libdir="${_Dbg_script_args[$i]}"
break
fi
done
if [[ ! -d $_Dbg_libdir ]] || [[ ! -r $_Dbg_libdir ]] ; then
if [[ ! -d $_Dbg_libdir ]] ; then
echo "${_Dbg_pname}: '${_Dbg_libdir}' is not a directory." 1>&2
elif [[ ! -r $_Dbg_libdir ]] ; then
echo "${_Dbg_pname}: Can't read debugger library directory '${_Dbg_libdir}'."
fi
echo "${_Dbg_pname}: Perhaps @PACKAGE@ is installed wrong (if its installed)." >&2
echo "${_Dbg_pname}: Try running @PACKAGE@ using -L (with a different directory)." >&2
echo "${_Dbg_pname}: Run @PACKAGE@ --help for a list and explanation of options." >&2
exit 1
fi
[[ -r $_Dbg_libdir/dbg-main.sh ]] || {
echo "${_Dbg_pname}: cannot read debugger file $_Dbg_libdir/dbg-main.sh" >&2
echo "${_Dbg_pname}: Perhaps bashdb is installed incorrectly." >&2
exit 1
}
. $_Dbg_libdir/dbg-main.sh
if [[ ! -d $_Dbg_tmpdir ]] && [[ ! -w $_Dbg_tmpdir ]] ; then
echo "${_Dbg_pname}: cannot write to temp directory $_Dbg_tmpdir." >&2
echo "${_Dbg_pname}: Use -T try directory location." >&2
exit 1
fi
# Note that this is called via bashdb rather than "bash --debugger"
_Dbg_script=1
_Dbg_set_trace_init=1
# Older alias for _Dbg_debugger()
_Dbg_set_trace() {
set -o functrace
while (( $# > 0 )) ; do
eval $1
shift
done
if [[ -z $_Dbg_set_trace_init ]] ; then
_Dbg_set_trace_init=1
_Dbg_step_ignore=${step_ignore:-0}
_Dbg_write_journal "_Dbg_step_ignore=0"
else
_Dbg_step_ignore=${1:-2}
fi
trap '_Dbg_debug_trap_handler 0 "$BASH_COMMAND" "$@"' DEBUG
}
# Turn on line tracing. Sort of a nicer replacement for
# set -x
#
# Example:
# source /usr/local/share/bashdb/bashdb-trace -q
# ...
# _Dbg_linetrace_on
# for i in `seq 10` ; do
# echo $i
# done
# _Dbg_linetrace_off
# _Dbg_QUIT_ON_QUIT=1 # Set this to make sure not to stay in debugger
# # after program terminates. Might also do earlier.
_Dbg_linetrace_on() {
set -o functrace
_Dbg_set_linetrace=1
_Dbg_step_ignore=-1
trap '_Dbg_debug_trap_handler 0 "$BASH_COMMAND" "$@"' DEBUG
}
# Turn off line tracing. Sort of a nicer replacement for
# set +x.
#
# See _Dbg_linetrace_on() for an example.
_Dbg_linetrace_off() {
_Dbg_set_linetrace=0
}
# Use the debugger signal handler when getting the signal specified.
# additional arguments can be the values for "print" "stack" and "stop"
# Examples:
# _Dbg_handler INT print stack nostop # this is the default
# _Dbg_handler INT # same thing
# _Dbg_hander # same thing
# _Dbg_handler HUP print stop # stop in debugger when getting
# # a HUP signal
_Dbg_handler() {
local signame=${1:INT}
shift
local -a rest=$*
if [[ -z $rest ]]; then
rest=("print" "stack" "stop")
fi
_Dbg_init_trap $signame
for attr in ${rest[@]}; do
_Dbg_do_handle $signame $attr
done
_Dbg_set_linetrace=0
_Dbg_step_ignore=-1
}
_Dbg_init_default_traps
set -${_Dbg_trace_old_set_opts}
unset _Dbg_trace_old_set_opts
# end of bashdb-trace