views:

488

answers:

5
+5  Q: 

bash stacktrace

I need something like a stacktrace in bash, is that possible?

A script is misbehaving. I need to know who calls that script, and who calls the calling script, and so on, only by modifying the misbehaving script.

What do you say?

+6  A: 

If you can edit the script itself, simply put a:

ps -ef >/tmp/bash_stack_trace.$$

in it, where the problem is occurring.

This will create a number of files in you tmp directory that show the entire process list at the time it happened.

You can work out which process called which other process by examining the PID and PPID columns.

Keep an eye on the files, you'll get one per failing process so they may have to be managed.

You can automate the analysis of the process "stack trace" using a bit of awk/perl trickery.

paxdiablo
+1  A: 

adding pstree -p -u `whoami` >>output in your script will probably get you the information you need.

Brian Mitchell
A: 
~$ help caller
caller: caller [EXPR]
    Returns the context of the current subroutine call.

    Without EXPR, returns "$line $filename".  With EXPR,
    returns "$line $subroutine $filename"; this extra information
    can be used to provide a stack trace.

    The value of EXPR indicates how many call frames to go back before the
    current one; the top frame is frame 0.
Juliano
While this and `-x` are useful, it sounds like it wouldn't help OP, as this only works on function calls within the same script.
ephemient
A: 

You could try something like

strace -f -e execve script.sh
sigjuice
+1  A: 
Gian Paolo Ghilardi