Stack Trace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stack Trace
# 1  
Old 06-05-2013
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 -

Code:
#! /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 -

Code:
=======================
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
# 2  
Old 06-05-2013
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.
Code:
#!/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.
# 3  
Old 06-05-2013
Not sure what you mean

Sorry

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

If there are no errors -

Code:
#! /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 -

Code:
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

# 4  
Old 06-10-2013
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.
# 5  
Old 06-14-2013
Smilie

Yes that is a nicer syntax

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Java stack trace parser in awk

I want the developers to get a mail with Java stack traces on a daily bases. When something is flaged as known issue and will get a fix but mean while this does not need to get sent each dayl. This is what I got so far. It's a bash script that runs some AWK in it. To get the files that needs to... (6 Replies)
Discussion started by: chipmunken
6 Replies

2. Linux

Server hung, is this a stack trace?

Hi everyone, Our Red Hat server hung yesterday, and I managed to log into the console and see the following message: RIP: 0010: mwait_idle_with_hints+0x66/ 0x67 RSP: 0018:ffffffff80457f40 EFLAGS: 00000046 RAX: 0000000000000010 RBX: ffff810c20075910 RCX: 0000000000000001 RDX:... (6 Replies)
Discussion started by: badoshi
6 Replies

3. Programming

View Stack Trace of different cores

I have a C program which is running as daemon and has some threads. The program is running on dual core cpu and it may happen that different threads may run on different cpu core. The problem is sometimes it crashes with some heap memory corruption probably between threads. GDB command(t a a... (2 Replies)
Discussion started by: rupeshkp728
2 Replies

4. AIX

How to get the stack trace using kdb?

Hi All, I am trying to debug my core file using kdb. When I try to get the stack trace I am facing this error. core mapped from @ 700000000000000 to @ 70000000306fc04 Preserving 1680415 bytes of symbol table Dump does not start with valid magic number WARNING: Possibly truncated or... (2 Replies)
Discussion started by: Sachin1987
2 Replies

5. Solaris

Need to retrieve stack trace from core using mdb

Hello , I use Solaris 5.10 . I have huge core file , 48 GB , resulted from an application that was running and got crashed with SIGSEGV. On my system only mdb works. Please help me to retrieve the stack trace from this core file. I am novice to mdb and its nuaunces. Please help me with... (2 Replies)
Discussion started by: rmv
2 Replies

6. Filesystems, Disks and Memory

get stack trace from C program on Solaris 8

I'm on solaris 8. I need to check the stack trace inside my C program. I don't have printstack or walkstack. I tested getcontext and it works. But how do I get the symbols from "stack_t" ? Help please. Many thanks! (4 Replies)
Discussion started by: rydahl
4 Replies

7. UNIX for Advanced & Expert Users

help required - stack trace

Hi all, One of our programs written in Java, produced this logfile. This job runs 48 threads and only one thread failed with this error. The code is a blackbox(external product), so cant look at the source code. From what I can infer from the log, the job was trying to write the log messages into... (9 Replies)
Discussion started by: ranj@chn
9 Replies

8. Shell Programming and Scripting

Need to capture ERROR msg and stack trace

Hi all, I was hoping someone can point me in the right direction. I'm trying to filter out errors from a web log- any lines with ERROR in it. I know I could simply use the grep command to do this. However, there are times when a stack trace follows the error line. I would like to capture these... (2 Replies)
Discussion started by: gswhoops
2 Replies

9. Programming

printing a stack trace with backtrace

I am trying to print a stack trace programatically using backtrace and backtrace_symbols. The problem is that the stack being printed in a mangled format. Is there a way to get the output in more of a human readable form? I am using Red Hat and the program is written in c++. (2 Replies)
Discussion started by: dmirza
2 Replies

10. Programming

Core file without a readable stack trace

I am using gdb to examine a core file but the output contains only the method addresses in hex. Is there anyway to translate these addresses to a human-readable form? :confused: (0 Replies)
Discussion started by: ciregbu
0 Replies
Login or Register to Ask a Question