Do stack trace + more
[errhandle.git] / bash-trace-function
CommitLineData
e6d1e24d
IK
1#!/bin/bash
2# Copyright (C) 2015 Ian Kelling
3
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7
8# http://www.apache.org/licenses/LICENSE-2.0
9
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16shopt -s extdebug
17bash-trace() {
18 # shows function args when: shopt -s extdebug
19 local -i argc_index=0 arg frame i start=${1:-1} max_indent=8 indent
20 local source
21 local extdebug=false
22 if [[ $(shopt -p extdebug) == *-s* ]]; then
23 extdebug=true
24 fi
25
26 for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
27 argc=${BASH_ARGC[frame]}
28 argc_index+=$argc
29 ((frame < start)) && continue
30 if (( ${#BASH_SOURCE[@]} > 1 )); then
31 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
32 fi
33 indent=$((frame-start+1))
34 indent=$((indent < max_indent ? indent : max_indent))
35 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
36 if $extdebug; then
37 for ((i=argc_index-1; i >= argc_index-argc; i--)); do
38 printf " %s" "${BASH_ARGV[i]}"
39 done
40 fi
41 echo \'
42 done
43}