OILS / demo / bash-call-stack.sh View on Github | oils.pub

86 lines, 38 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# demo/bash-call-stack.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9
10print-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
18f() {
19 echo 'hi from f'
20 g
21}
22
23g() {
24 echo 'hi from g'
25 print-stack
26}
27
28no-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
40h() {
41 echo 'hi from h'
42 false
43 #echo $x
44}
45
46error() {
47 set -o errtrace # needed to keep it active
48 trap 'print-stack' ERR
49
50 h
51}
52
53python() {
54 cat >_tmp/callstack.py <<EOF
55def f():
56 print("hi from f")
57 exec("g()")
58
59def g():
60 print("hi from g")
61 raise RuntimeError()
62
63f()
64EOF
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
75def h():
76 import callstack
77
78h()
79EOF
80
81 # 5 frames: h() imprt callstack f() g() raise
82 python2 _tmp/callmain.py
83}
84
85"$@"
86