Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show call stack in Bash

Tags:

linux

bash

trace

To start with, I do not know if this is something trivial and common knowledge; I don't know, so am asking here.

I am running a 3rd party app and trying to nail down a problem for which I need to find out the call stack.

When I call a Bash script, it calls a number of other scripts and binaries, processing stuff and exits.

What I need is a way of finding out exactly which scripts and binaries it called. pstree shows a stack but only for a process that is currently executing.

like image 675
Hussain Akbar Avatar asked Apr 16 '26 18:04

Hussain Akbar


2 Answers

function stacktrace { 
   local i=1 line file func
   while read -r line func file < <(caller $i); do
      echo >&2 "[$i] $file:$line $func(): $(sed -n ${line}p $file)"
      ((i++))
   done
}

from https://gitlab.com/kyb/autorsync/-/blob/master/utils.bash#L84

like image 100
kyb Avatar answered Apr 18 '26 09:04

kyb


Run the script with bash -x, which will produce output on standard error to show each command that is executed.

like image 40
chepner Avatar answered Apr 18 '26 09:04

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!