From e6d1e24d398a5ac47793fc9842c1b49c135bf4f0 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Sun, 15 Feb 2015 16:43:27 -0800 Subject: [PATCH] Do stack trace + more --- README | 9 +++------ bash-trace-function | 43 +++++++++++++++++++++++++++++++++++++++++++ errallow-function | 2 +- errcatch-function | 22 +++++++++++++--------- errexit-function | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 bash-trace-function create mode 100644 errexit-function diff --git a/README b/README index a960550..b3829e2 100644 --- a/README +++ b/README @@ -4,12 +4,9 @@ script files which sit next to this file. Files ending in -function are for sourcing then calling as a function. Files without -function are exactly the same except they are for calling as a script. -Patches and bugs are very welcome via gitlab. - -Questions, feedback, etc are very welcome via email to Ian Kelling -. I will add any useful questions, answers, etc. to this -file. If a mailing list / forum is ever called for, I will start one. +Patches, bugs, and any feedback is very welcome via gitorious or email to +Ian Kelling . This program is also part of a collection of programs, -https://gitlab.com/iankelling/bash-programs-by-ian, which are unrelated except +https://gitorious.org/bash-programs-by-ian, which are unrelated except having the same author and being being bash programs. diff --git a/bash-trace-function b/bash-trace-function new file mode 100644 index 0000000..5c304ae --- /dev/null +++ b/bash-trace-function @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright (C) 2015 Ian Kelling + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +shopt -s extdebug +bash-trace() { + # shows function args when: shopt -s extdebug + local -i argc_index=0 arg frame i start=${1:-1} max_indent=8 indent + local source + local extdebug=false + if [[ $(shopt -p extdebug) == *-s* ]]; then + extdebug=true + fi + + for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do + argc=${BASH_ARGC[frame]} + argc_index+=$argc + ((frame < start)) && continue + if (( ${#BASH_SOURCE[@]} > 1 )); then + source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:" + fi + indent=$((frame-start+1)) + indent=$((indent < max_indent ? indent : max_indent)) + printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}" + if $extdebug; then + for ((i=argc_index-1; i >= argc_index-argc; i--)); do + printf " %s" "${BASH_ARGV[i]}" + done + fi + echo \' + done +} diff --git a/errallow-function b/errallow-function index d44247b..aff8593 100644 --- a/errallow-function +++ b/errallow-function @@ -17,6 +17,6 @@ errallow() { if [[ $1 ]]; then echo "errallow help: Undo the complimentary errcatch function." else - set +eE +o pipefail; trap ERR + set +E +o pipefail; trap ERR fi } diff --git a/errcatch-function b/errcatch-function index a46dfbc..b6489ac 100644 --- a/errcatch-function +++ b/errcatch-function @@ -13,14 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +# on non-zero return codes outside of if/else,etc, print failure, stack trace, and exit +# depends on bash-trace function errcatch() { - if [[ $1 ]]; then - echo "errcatch help: Enable general purpose bash error handling. -Exit on all errors and print useful information." - else - set -eE; - trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" } -${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"' ERR - set -o pipefail - fi + set -E; shopt -s extdebug + _err-trap() { + err=$? + exec >&2 + echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err" + bash-trace 2 + echo "$0: exiting with code $err" + exit $err + } + trap _err-trap ERR + set -o pipefail } diff --git a/errexit-function b/errexit-function new file mode 100644 index 0000000..cbe2118 --- /dev/null +++ b/errexit-function @@ -0,0 +1,34 @@ +#!/bin/bash +# Copyright (C) 2015 Ian Kelling + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +errexit() { + # usage: errexit [EXIT_CODE] [MESSAGE] + exec >&2 + code=1 + if [[ $@ ]]; then + if [[ ${1/[^0-9]/} == "$1" ]]; then + code=$1 + if [[ $2 ]]; then + echo "$2" + fi + else + echo "$0: $1" + fi + fi + echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}" + bash-trace 2 + echo "$0: exiting with code $code" + exit $err +} -- 2.25.1