1.\" Copyright (c) 1994-2021 Simon J. Gerraty 2.\" 3.\" SPDX-License-Identifier: BSD-2-Clause 4.\" 5.\" This file is provided in the hope that it will 6.\" be of use. There is absolutely NO WARRANTY. 7.\" Permission to copy, redistribute or otherwise 8.\" use this file is hereby granted provided that 9.\" the above copyright notice and this notice are 10.\" left intact. 11.\" 12.\" Please send copies of changes and bug-fixes to: 13.\" sjg@crufty.net 14.\" 15.Dd October 22, 2024 16.Dt DEBUG.SH 8 17.Os 18.Sh NAME 19.Nm debug.sh 20.Nd selectively debug scripts 21.Sh SYNOPSIS 22.Bl -item -compact 23.It 24.Ic $_DEBUG_SH .\& Pa debug.sh 25.Pp 26.It 27.Ic DebugOn Oo Fl eo Oc Ar tag ... 28.It 29.Ic DebugOff Oo Fl eo Oc Oo Cm rc= Ns Ar rc Oc Ar tag ... 30.It 31.Ic Debugging 32.It 33.Ic DebugAdd Ar tag 34.It 35.Ic DebugEcho Op Ar message 36.It 37.Ic DebugLog Op Ar message 38.It 39.Ic DebugShell Ar tag ... 40.It 41.Ic DebugTrace Ar message 42.It 43.Ic Debug Ar tag ... 44.El 45.Sh DESCRIPTION 46.Nm 47provides the following functions to facilitate flexible 48run-time tracing of complicated shell scripts. 49.Bl -tag -width 4n 50.It Ic DebugOn Oo Fl eo Oc Ar tag ... 51turns tracing on if any 52.Ar tag 53is found in 54.Va DEBUG_SH 55(a comma separated list of tags). 56.Pp 57It turns tracing off if 58.Ar !tag 59is found in 60.Va DEBUG_SH . 61.Pp 62It sets 63.Va DEBUG_ON 64to the 65.Ar tag 66that caused tracing to be enabled, or 67.Va DEBUG_OFF 68if we matched 69.Ar !tag . 70.Pp 71If 72.Fl e 73option is present, returns 1 if no 74.Ar tag 75matched. 76.Pp 77If 78.Fl o 79option is present, tracing is turned off unless there 80was a matched 81.Ar tag , 82useful for functions too noisy to tace. 83.It Ic DebugOff Oo Fl eo Oc Oo Cm rc= Ns Ar rc Oc Ar tag ... 84turns tracing on if any 85.Ar tag 86matches 87.Va DEBUG_OFF 88or off if any 89.Ar tag 90matches 91.Va DEBUG_ON . 92This allows nested functions to not interfere with each other. 93.Pp 94The flags 95.Fl e 96and 97.Fl o 98are ignored, they just allow for symmetry with calls to 99.Fn DebugOn . 100.Pp 101The optional 102.Ar rc 103value will be returned rather than the default of 0. 104Thus if 105.Fn DebugOff 106is the last operation in a function, 107.Ar rc 108will be the return code of the function. 109.It Ic Debugging 110returns true if tracing is enabled. 111It is useful for bounding complex debug actions, rather than 112using lots of 113.Ic $DEBUG_DO 114lines. 115.It Ic DebugAdd Ar tag 116Add 117.Ar tag 118to 119.Va DEBUG_SH 120to influence later output, 121possibly in a child process. 122.It Ic DebugEcho 123is just shorthand for: 124.Bd -literal -offset indent 125$DEBUG_DO echo "$@" 126.Ed 127.It Ic DebugLog Op Ar message 128If debugging is enabled, output 129.Ar message 130prefixed with a time-stamp. 131.It Ic DebugShell Ar tag ... 132runs an interactive shell if any 133.Ar tag 134is found in 135.Va DEBUG_INTERACTIVE , 136and there is a tty available. 137The shell used is defined by 138.Va DEBUG_SHELL 139or 140.Va SHELL 141and defaults to 142.Pa /bin/sh . 143.It Ic DebugTrace Ar message 144Debug output can be very noisy, and it can be tricky 145to align with the script. 146This function outputs a very noticable banner indicating the value of 147.Va DEBUG_ON , 148and 149.Ar message 150is passed to 151.Fn DebugLog , 152finally the banner is repeated. 153.It Ic Debug Ar tag ... 154For backwards compatibility, calls 155.Fn DebugOn 156and if that does not turn tracing on, 157it calls 158.Fn DebugOff 159to turn it off. 160.El 161.Pp 162The variables 163.Va DEBUG_SKIP 164and 165.Va DEBUG_DO 166are set so as to enable/disable code that should be 167skipped/run when debugging is turned on. 168.Va DEBUGGING 169is the same as 170.Va DEBUG_SKIP 171for backwards compatability and is only set by 172.Fn Debug . 173.Pp 174The use of 175.Ic $_DEBUG_SH 176is to prevent multiple inclusion, 177though it does no harm in this case. 178.Sh BUGS 179Does not work with some versions of 180.Xr ksh 1 . 181If a function turns tracing on, ksh turns it off when the 182function returns - useless. 183.Pp 184PD ksh works ok ;-) 185.Sh AUTHOR 186.An -nosplit 187.Nm 188was written by 189.An Simon J Gerraty Aq Mt sjg@crufty.net . 190 191 192