Stack Trace

Hi All

Thought it would be kind of fun to implement a stack trace for a shell script that calls functions within a sub shell. This is for bash under Linux and probably not portable -

#! /bin/bash

error_exit()
{
    echo "======================="
    echo $1
    echo "======================="
    cat_stack
    exit 1
}

cat_stack()
{
    P=">"
    while read LINE
    do
        printf "%s\n" "${P}${LINE}"
        P="-"${P}
    done < ${STACK}
}

add_trace()
{
    sed "s/^$1/$1: $2 - /" ${STACK} > ${SWP}
    mv ${SWP} ${STACK}
    exit 1
}

f1()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    R=$(f2) || error_exit "Exception raised below call to f2() in (${FUNCNAME})"
    pop
}

f2()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f2"
    R=$(f3) || add_trace ${FUNCNAME} "Call to f3() failed"
    pop
}

f4()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f4"
    slurp 1 2>/dev/null || add_trace ${FUNCNAME} "Sleep failed"
    pop
}

f3()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f3"
    R=$(f5) || add_trace ${FUNCNAME} "Call to f5() failed"
    R=$(f4) || add_trace ${FUNCNAME} "Call to f4() failed"
    pop
}

f5()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f5"
    pop
}
push()
{
    echo $1 >> ${STACK}
    printf "%s%s\n" "$(seq -s " " ${SSO} | sed 's/[0-9]//g')" "${1}" >> ${TRACE}
    SSO=$(($SSO + 2))
}

pop()
{
    SSO=$((${SSO} - 2))
    TMP="Returned from $(tail -n 1 ${STACK} | cut -d" " -f1)"
    printf "%s%s\n" "$(seq -s " " ${SSO} | sed 's/[0-9]//g')"  "${TMP}" >> ${TRACE}
    head -n -1 ${STACK} > ${SWP}
    mv ${SWP} ${STACK}
}

STACK=stack
:>${STACK}
TRACE=trace
:>${TRACE}

SWP=/tmp/swpx
SSO=1

f1

Output -

=======================
Exception raised below call to f2() in (f1)
=======================
>f1 called from line 91 main
->f2: Call to f3() failed -  called from line 32 f1
-->f3: Call to f4() failed -  called from line 40 f2
--->f4: Sleep failed -  called from line 57 f3

I'd be interested in any improvements anyone can think of ...

Brad

You lack an unpush if things go ok in the first leven N function and bad in the second level N function. You might put the message on the end of an exported env. variable in a subshell, so when you return it removes your addition.

#!/usr/bin/ksh
xxx(){
 ( a=$a/xxx 
 return 1 )
  return $!
}
a=
xxx
echo "xxx returns $?
a='$a'"
~
~
~
~
~
~
~
~
~
"funcewrite" 15 lines, 102 characters 
$ funcewrite    
xxx returns 1
a=''
$ 

This is much nicer in JAVA/C++ (Abstract Class method inheritance)! I wrote a similar bit for XML schema parse, where the containing element could call its parent first to print out his path, which rippled up to root where it started printing because there was no parent and then unrolled back printing each level to the original caller to print his bit last.

Sorry

Can't make out what you mean by an "unpush"

If there are no errors -

#! /bin/bash

error_exit()
{
    echo "======================="
    echo $1
    echo "======================="
    cat_stack
    exit 1
}

cat_stack()
{
    P=">"
    while read LINE
    do
        printf "%s\n" "${P}${LINE}"
        P="-"${P}
    done < ${STACK}
}

add_trace()
{
    sed "s/^$1/$1: $2 - /" ${STACK} > ${SWP}
    mv ${SWP} ${STACK}
    exit 1
}

f1()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    R=$(f2) || error_exit "Exception raised below call to f2() in (${FUNCNAME})"
    pop
}

f2()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f2"
    R=$(f3) || add_trace ${FUNCNAME} "Call to f3() failed"
    pop
}

f4()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f4"
    sleep 1 2>/dev/null || add_trace ${FUNCNAME} "Sleep failed"
    pop
}

f3()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f3"
    R=$(f5) || add_trace ${FUNCNAME} "Call to f5() failed"
    R=$(f4) || add_trace ${FUNCNAME} "Call to f4() failed"
    pop
}

f5()
{
    push "${FUNCNAME} called from line $(caller 0 | cut -d" " -f1,2)"
    echo "In f5"
    pop
}
push()
{
    echo $1 >> ${STACK}
    printf "%s%s\n" "$(seq -s " " ${SSO} | sed 's/[0-9]//g')" "${1}" >> ${TRACE}
    SSO=$(($SSO + 2))
}

pop()
{
    SSO=$((${SSO} - 2))
    TMP="Returned from $(tail -n 1 ${STACK} | cut -d" " -f1)"
    printf "%s%s\n" "$(seq -s " " ${SSO} | sed 's/[0-9]//g')"  "${TMP}" >> ${TRACE}
    head -n -1 ${STACK} > ${SWP}
    mv ${SWP} ${STACK}
}

STACK=stack
:>${STACK}
TRACE=trace
:>${TRACE}

SWP=/tmp/swpx
SSO=1

f1

then the trace file -

cat trace 
f1 called from line 91 main
  f2 called from line 32 f1
    f3 called from line 40 f2
      f5 called from line 56 f3
      Returned from f5
      f4 called from line 57 f3
      Returned from f4
    Returned from f3
  Returned from f2
Returned from f1

Your example does a V trip, but for a W trip you need to forget previous deeper levels and learn new deeper levels. So, you need something to trim the suffix of the path where you are, or if using indent, trim the indent back. I did not see that in pop, but since you are using indent, that is the SSO bit. "(( SSO -= 2 ))" is simpler.

:slight_smile:

Yes that is a nicer syntax

Thanks