1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # demo/bash-call-stack.sh <function name>
|
5 |
|
6 | set -o nounset
|
7 | set -o pipefail
|
8 | set -o errexit
|
9 |
|
10 | print-stack() {
|
11 | local n=${#BASH_SOURCE[@]}
|
12 | for (( i = 0; i < n; ++i)); do
|
13 | echo "STACK:${BASH_SOURCE[i]}:${FUNCNAME[i]}:${BASH_LINENO[i]}"
|
14 | done
|
15 | }
|
16 |
|
17 |
|
18 | f() {
|
19 | echo 'hi from f'
|
20 | g
|
21 | }
|
22 |
|
23 | g() {
|
24 | echo 'hi from g'
|
25 | print-stack
|
26 | }
|
27 |
|
28 | no-error() {
|
29 | # -1 position is the bottom of the stack
|
30 | #
|
31 | # It has has demo/bash-stack.sh:main:0
|
32 | #
|
33 | # This is tested in spec/introspect.sh
|
34 |
|
35 | #PS4='+ ${BASH_SOURCE[-1]}:${FUNCNAME[-1]}:${BASH_LINENO[-1]} '
|
36 | #set -x
|
37 | f
|
38 | }
|
39 |
|
40 | h() {
|
41 | echo 'hi from h'
|
42 | false
|
43 | #echo $x
|
44 | }
|
45 |
|
46 | error() {
|
47 | set -o errtrace # needed to keep it active
|
48 | trap 'print-stack' ERR
|
49 |
|
50 | h
|
51 | }
|
52 |
|
53 | python() {
|
54 | cat >_tmp/callstack.py <<EOF
|
55 | def f():
|
56 | print("hi from f")
|
57 | exec("g()")
|
58 |
|
59 | def g():
|
60 | print("hi from g")
|
61 | raise RuntimeError()
|
62 |
|
63 | f()
|
64 | EOF
|
65 |
|
66 | echo
|
67 | echo
|
68 | set +o errexit
|
69 |
|
70 | # 3 frames: <module> f g
|
71 | # and quotes the code
|
72 | python2 _tmp/callstack.py
|
73 |
|
74 | cat >_tmp/callmain.py <<EOF
|
75 | def h():
|
76 | import callstack
|
77 |
|
78 | h()
|
79 | EOF
|
80 |
|
81 | # 5 frames: h() imprt callstack f() g() raise
|
82 | python2 _tmp/callmain.py
|
83 | }
|
84 |
|
85 | "$@"
|
86 |
|