bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to display parameters in a function backtrace?


From: L A Walsh
Subject: How to display parameters in a function backtrace?
Date: Wed, 02 Feb 2022 06:00:21 -0800
User-agent: Thunderbird

I was trying to find parameters to a function that gave an error, so
I 'turned on' (included my backtracing routine), which showed:

./24bc: line 46: printf: `txt=\nRed,': not a valid identifier
STOPPING execution @ "printf ${_:+-v $_} '\x1b[48;2;%s;%s;%sm' $1 $2 $3" in "setBgColor()" at ./24bc #46
 called in setBackBlack() at ./24bc #56
 called in title() at ./24bc #74
 called in main() at ./24bc #81

----
I was all, 'rats, its not showing the params', I wondered why and looked at the code,
and realized that while I can backtrace function, source line+file, I didn't
know how to display the params that were used when the function was called.

Is there, or maybe 'can there', be a BASH var like
FUNCPARMS[1]="(P1 P2 P3)"

Of course it would be handy if bash could parse this w/o using an
intermediate var:

echo "${${FUNCPARMS[@]}[1]}"   =>
  #  (tmp=${FUNCPARMS[@]}   tmp=(P1 P2 P3) )
# echo "${tmp[1]}" P1

---
But even without the enhanced parsing output, it would still
be useful if bash had & filled in the param vars.

FUNCPARMS would parallel 'FUNCNAME', i.e.
FUNCPARMS[1] would hold the quoted array "()" PARMS for FUNCNAME[1].

FWIW, my primitive backtrace function (source or "include")  follows:

----
#!/bin/bash -u

shopt -s expand_aliases
alias my='declare ' int='my -i '

backtrace () {
 trap  ERR
 int   level=0
 my    cmd fn src ln
 int   iter=0
 while {
     cmd=$BASH_COMMAND; fn=${FUNCNAME[level+1]:-}
     src=${BASH_SOURCE[level+1]:-} ln=${BASH_LINENO[level]:-}
 }; do
   [[ $fn && $src && $ln ]] && {
     if ((iter++==0)); then
printf 'STOPPING execution @ "%s" in "%s()" at %s #%s\n' "$cmd" "$fn" "$src" "$ln"
     else
       printf '  called in %s() at %s #%s\n' "$fn"  "$src" "$ln"
     fi
     level+=1
     continue;
   }
   exit 1
 done
}
set -o errtrace
trap backtrace ERR
trap backtrace QUIT
set -E





reply via email to

[Prev in Thread] Current Thread [Next in Thread]