debug.sh (aa3b7a2fbc4687c0a09b6166aa2c2d117989d8fa) | debug.sh (7e1c014a9e3a632099efd1fa34a583b627b71bb3) |
---|---|
1: 2# SPDX-License-Identifier: BSD-2-Clause 3 4# NAME: 5# debug.sh - selectively debug scripts 6# 7# SYNOPSIS: 8# $_DEBUG_SH . debug.sh --- 60 unchanged lines hidden (view full) --- 69# If a function turns tracing on, ksh turns it off when the 70# function returns - useless. 71# PD ksh works ok ;-) 72# 73# AUTHOR: 74# Simon J. Gerraty <sjg@crufty.net> 75 76# RCSid: | 1: 2# SPDX-License-Identifier: BSD-2-Clause 3 4# NAME: 5# debug.sh - selectively debug scripts 6# 7# SYNOPSIS: 8# $_DEBUG_SH . debug.sh --- 60 unchanged lines hidden (view full) --- 69# If a function turns tracing on, ksh turns it off when the 70# function returns - useless. 71# PD ksh works ok ;-) 72# 73# AUTHOR: 74# Simon J. Gerraty <sjg@crufty.net> 75 76# RCSid: |
77# $Id: debug.sh,v 1.35 2024/02/03 19:04:47 sjg Exp $ | 77# $Id: debug.sh,v 1.40 2024/09/09 20:06:00 sjg Exp $ |
78# 79# @(#) Copyright (c) 1994-2024 Simon J. Gerraty 80# 81# This file is provided in the hope that it will 82# be of use. There is absolutely NO WARRANTY. 83# Permission to copy, redistribute or otherwise 84# use this file is hereby granted provided that 85# the above copyright notice and this notice are 86# left intact. 87# 88# Please send copies of changes and bug-fixes to: 89# sjg@crufty.net 90# 91 92_DEBUG_SH=: 93 94Myname=${Myname:-`basename $0 .sh`} 95 | 78# 79# @(#) Copyright (c) 1994-2024 Simon J. Gerraty 80# 81# This file is provided in the hope that it will 82# be of use. There is absolutely NO WARRANTY. 83# Permission to copy, redistribute or otherwise 84# use this file is hereby granted provided that 85# the above copyright notice and this notice are 86# left intact. 87# 88# Please send copies of changes and bug-fixes to: 89# sjg@crufty.net 90# 91 92_DEBUG_SH=: 93 94Myname=${Myname:-`basename $0 .sh`} 95 |
96# We want to use local if we can 97# if isposix-shell.sh has been sourced isPOSIX_SHELL will be set 98# as will local 99case "$local" in 100local|:) ;; 101*) 102 if (echo ${PATH%:*}) > /dev/null 2>&1; then 103 local=local 104 else 105 local=: 106 fi 107 ;; 108esac 109 |
|
96DEBUGGING= 97DEBUG_DO=: 98DEBUG_SKIP= 99export DEBUGGING DEBUG_DO DEBUG_SKIP 100 | 110DEBUGGING= 111DEBUG_DO=: 112DEBUG_SKIP= 113export DEBUGGING DEBUG_DO DEBUG_SKIP 114 |
115## 116# _debugOn match first 117# 118# Actually turn on tracing, set $DEBUG_ON=$match 119# 120# If we have included hooks.sh $_HOOKS_SH will be set 121# and if $first (the first arg to DebugOn) is suitable as a variable 122# name we will run ${first}_debugOn_hooks. 123# 124# We disable tracing for hooks_run itself but functions can trace 125# if they want based on DEBUG_DO 126# |
|
101_debugOn() { 102 DEBUG_OFF= 103 DEBUG_DO= 104 DEBUG_SKIP=: 105 DEBUG_X=-x 106 set -x 107 DEBUG_ON=$1 | 127_debugOn() { 128 DEBUG_OFF= 129 DEBUG_DO= 130 DEBUG_SKIP=: 131 DEBUG_X=-x 132 set -x 133 DEBUG_ON=$1 |
134 case "$_HOOKS_SH,$2" in 135 ,*|:,|:,*[${CASE_CLASS_NEG:-!}A-Za-z0-9_]*) ;; 136 *) # avoid noise from hooks_run 137 set +x 138 hooks_run ${2}_debugOn_hooks 139 set -x 140 ;; 141 esac |
|
108} 109 | 142} 143 |
144## 145# _debugOff match $DEBUG_ON $first 146# 147# Actually turn off tracing, set $DEBUG_OFF=$match 148# 149# If we have included hooks.sh $_HOOKS_SH will be set 150# and if $first (the first arg to DebugOff) is suitable as a variable 151# name we will run ${first}_debugOff_hooks. 152# 153# We do hooks_run after turning off tracing, but before resetting 154# DEBUG_DO so functions can trace if they want 155# |
|
110_debugOff() { 111 DEBUG_OFF=$1 112 set +x | 156_debugOff() { 157 DEBUG_OFF=$1 158 set +x |
159 case "$_HOOKS_SH,$3" in 160 ,*|:,|:,*[${CASE_CLASS_NEG:-!}A-Za-z0-9_]*) ;; 161 *) hooks_run ${3}_debugOff_hooks;; 162 esac 163 set +x # just to be sure |
|
113 DEBUG_ON=$2 114 DEBUG_DO=: 115 DEBUG_SKIP= 116 DEBUG_X= 117} 118 119DebugEcho() { 120 $DEBUG_DO echo "$@" 121} 122 | 164 DEBUG_ON=$2 165 DEBUG_DO=: 166 DEBUG_SKIP= 167 DEBUG_X= 168} 169 170DebugEcho() { 171 $DEBUG_DO echo "$@" 172} 173 |
174## 175# Debugging 176# 177# return 0 if we are debugging. 178# |
|
123Debugging() { 124 test "$DEBUG_SKIP" 125} 126 | 179Debugging() { 180 test "$DEBUG_SKIP" 181} 182 |
183## 184# DebugLog message 185# 186# Outout message with timestamp if we are debugging 187# |
|
127DebugLog() { 128 $DEBUG_SKIP return 0 129 echo `date '+@ %s [%Y-%m-%d %H:%M:%S %Z]'` "$@" 130} 131 | 188DebugLog() { 189 $DEBUG_SKIP return 0 190 echo `date '+@ %s [%Y-%m-%d %H:%M:%S %Z]'` "$@" 191} 192 |
132# something hard to miss when wading through huge -x output | 193## 194# DebugTrace message 195# 196# Something hard to miss when wading through huge -x output 197# |
133DebugTrace() { 134 $DEBUG_SKIP return 0 135 set +x 136 echo "@ ==================== [ $DEBUG_ON ] ====================" 137 DebugLog "$@" 138 echo "@ ==================== [ $DEBUG_ON ] ====================" 139 set -x 140} 141 | 198DebugTrace() { 199 $DEBUG_SKIP return 0 200 set +x 201 echo "@ ==================== [ $DEBUG_ON ] ====================" 202 DebugLog "$@" 203 echo "@ ==================== [ $DEBUG_ON ] ====================" 204 set -x 205} 206 |
142# Turn on debugging if appropriate | 207## 208# DebugOn [-e] [-o] match ... 209# 210# Turn on debugging if any $match is found in $DEBUG_SH. 211# |
143DebugOn() { | 212DebugOn() { |
213 eval ${local:-:} _e _match _off _rc |
|
144 _rc=0 # avoid problems with set -e 145 _off=: 146 while : 147 do 148 case "$1" in 149 -e) _rc=1; shift;; # caller ok with return 1 150 -o) _off=; shift;; # off unless we have a match 151 *) break;; --- 13 unchanged lines hidden (view full) --- 165 esac 166 for _e in ${*:-$Myname} $_e 167 do 168 : $_e in ,${DEBUG_SH:-$DEBUG}, 169 case ",${DEBUG_SH:-$DEBUG}," in 170 *,!$_e,*|*,!$Myname:$_e,*) 171 # only turn it off if it was on 172 _rc=0 | 214 _rc=0 # avoid problems with set -e 215 _off=: 216 while : 217 do 218 case "$1" in 219 -e) _rc=1; shift;; # caller ok with return 1 220 -o) _off=; shift;; # off unless we have a match 221 *) break;; --- 13 unchanged lines hidden (view full) --- 235 esac 236 for _e in ${*:-$Myname} $_e 237 do 238 : $_e in ,${DEBUG_SH:-$DEBUG}, 239 case ",${DEBUG_SH:-$DEBUG}," in 240 *,!$_e,*|*,!$Myname:$_e,*) 241 # only turn it off if it was on 242 _rc=0 |
173 $DEBUG_DO _debugOff $_e $DEBUG_ON | 243 $DEBUG_DO _debugOff $_e $DEBUG_ON $1 |
174 break 175 ;; 176 *,$_e,*|*,$Myname:$_e,*) 177 # only turn it on if it was off 178 _rc=0 179 _match=$_e | 244 break 245 ;; 246 *,$_e,*|*,$Myname:$_e,*) 247 # only turn it on if it was off 248 _rc=0 249 _match=$_e |
180 $DEBUG_SKIP _debugOn $_e | 250 $DEBUG_SKIP _debugOn $_e $1 |
181 break 182 ;; 183 esac 184 done 185 if test -z "$_off$_match"; then 186 # off unless explicit match, but 187 # only turn it off if it was on | 251 break 252 ;; 253 esac 254 done 255 if test -z "$_off$_match"; then 256 # off unless explicit match, but 257 # only turn it off if it was on |
188 $DEBUG_DO _debugOff $_e $DEBUG_ON | 258 $DEBUG_DO _debugOff $_e $DEBUG_ON $1 |
189 fi 190 DEBUGGING=$DEBUG_SKIP # backwards compatability 191 $DEBUG_DO set -x # back on if needed 192 $DEBUG_DO set -x # make sure we see it in trace 193 return $_rc 194} 195 | 259 fi 260 DEBUGGING=$DEBUG_SKIP # backwards compatability 261 $DEBUG_DO set -x # back on if needed 262 $DEBUG_DO set -x # make sure we see it in trace 263 return $_rc 264} 265 |
266## 267# DebugOff [-e] [-o] [rc=$?] match ... 268# |
|
196# Only turn debugging off if one of our args was the reason it 197# was turned on. | 269# Only turn debugging off if one of our args was the reason it 270# was turned on. |
271# |
|
198# We normally return 0, but caller can pass rc=$? as first arg 199# so that we preserve the status of last statement. | 272# We normally return 0, but caller can pass rc=$? as first arg 273# so that we preserve the status of last statement. |
274# 275# The options '-e' and '-o' are ignored, they just make it easier to 276# keep DebugOn and DebugOff lines in sync. 277# |
|
200DebugOff() { | 278DebugOff() { |
279 eval ${local:-:} _e _rc |
|
201 case ",${DEBUG_SH:-$DEBUG}," in 202 *,[Dd]ebug,*) ;; 203 *) $DEBUG_DO set +x;; # reduce the noise 204 esac 205 _rc=0 # always happy 206 while : 207 do 208 case "$1" in 209 -[eo]) shift;; # ignore it 210 rc=*) eval "_$1"; shift;; 211 *) break;; 212 esac 213 done 214 for _e in $* 215 do 216 : $_e==$DEBUG_OFF DEBUG_OFF 217 case "$DEBUG_OFF" in 218 "") break;; | 280 case ",${DEBUG_SH:-$DEBUG}," in 281 *,[Dd]ebug,*) ;; 282 *) $DEBUG_DO set +x;; # reduce the noise 283 esac 284 _rc=0 # always happy 285 while : 286 do 287 case "$1" in 288 -[eo]) shift;; # ignore it 289 rc=*) eval "_$1"; shift;; 290 *) break;; 291 esac 292 done 293 for _e in $* 294 do 295 : $_e==$DEBUG_OFF DEBUG_OFF 296 case "$DEBUG_OFF" in 297 "") break;; |
219 $_e) _debugOn $DEBUG_ON; return $_rc;; | 298 $_e) _debugOn $DEBUG_ON $1; return $_rc;; |
220 esac 221 done 222 for _e in $* 223 do 224 : $_e==$DEBUG_ON DEBUG_ON 225 case "$DEBUG_ON" in 226 "") break;; | 299 esac 300 done 301 for _e in $* 302 do 303 : $_e==$DEBUG_ON DEBUG_ON 304 case "$DEBUG_ON" in 305 "") break;; |
227 $_e) _debugOff; return $_rc;; | 306 $_e) _debugOff "" "" $1; return $_rc;; |
228 esac 229 done 230 DEBUGGING=$DEBUG_SKIP # backwards compatability 231 $DEBUG_DO set -x # back on if needed 232 $DEBUG_DO set -x # make sure we see it in trace 233 return $_rc 234} 235 236_TTY=${_TTY:-`test -t 0 && tty`}; export _TTY 237 238# override this if you like 239_debugShell() { | 307 esac 308 done 309 DEBUGGING=$DEBUG_SKIP # backwards compatability 310 $DEBUG_DO set -x # back on if needed 311 $DEBUG_DO set -x # make sure we see it in trace 312 return $_rc 313} 314 315_TTY=${_TTY:-`test -t 0 && tty`}; export _TTY 316 317# override this if you like 318_debugShell() { |
319 test "x$_TTY" != x || return 0 |
|
240 { 241 echo DebugShell "$@" 242 echo "Type 'exit' to continue..." 243 } > $_TTY 244 ${DEBUG_SHELL:-${SHELL:-/bin/sh}} < $_TTY > $_TTY 2>&1 245} 246 247# Run an interactive shell if appropriate 248# Note: you can use $DEBUG_SKIP DebugShell ... to skip unless debugOn 249DebugShell() { | 320 { 321 echo DebugShell "$@" 322 echo "Type 'exit' to continue..." 323 } > $_TTY 324 ${DEBUG_SHELL:-${SHELL:-/bin/sh}} < $_TTY > $_TTY 2>&1 325} 326 327# Run an interactive shell if appropriate 328# Note: you can use $DEBUG_SKIP DebugShell ... to skip unless debugOn 329DebugShell() { |
330 eval ${local:-:} _e |
|
250 case "$_TTY%${DEBUG_INTERACTIVE}" in 251 *%|%*) return 0;; # no tty or no spec 252 esac 253 for _e in ${*:-$Myname} all 254 do 255 case ",${DEBUG_INTERACTIVE}," in 256 *,!$_e,*|*,!$Myname:$_e,*) 257 return 0 --- 21 unchanged lines hidden --- | 331 case "$_TTY%${DEBUG_INTERACTIVE}" in 332 *%|%*) return 0;; # no tty or no spec 333 esac 334 for _e in ${*:-$Myname} all 335 do 336 case ",${DEBUG_INTERACTIVE}," in 337 *,!$_e,*|*,!$Myname:$_e,*) 338 return 0 --- 21 unchanged lines hidden --- |