1#!../bash
2#
3# Test correct functioning bash debug support not via the bashdb
4# debugger but merely by printing via print_trap()
5# $Id: dbg-support.tests,v 1.13 2003/02/17 22:02:25 rockyb Exp $
6shopt -s extdebug
7print_debug_trap() {
8   echo "debug lineno: $1 ${FUNCNAME[1]}"
9   return
10}
11
12print_return_trap() {
13   echo "return lineno: $1 ${FUNCNAME[1]}"
14   return
15}
16
17fn1() {
18    echo "LINENO $LINENO"
19    echo "LINENO $LINENO"
20    echo "BASH_SOURCE[0]" ${BASH_SOURCE[0]}
21    echo "FUNCNAME[0]" ${FUNCNAME[0]}
22    echo `caller`
23    echo `caller 0`
24    echo `caller 1`
25    echo `caller foo`
26}    
27
28fn2() {
29    echo "fn2 here. Calling fn1..."
30    fn1
31}    
32
33fn3() {
34    echo "LINENO $LINENO"
35    echo "BASH_SOURCE[0]" ${BASH_SOURCE[0]}
36    
37    # Print a stack trace
38    declare -i n
39    n=${#FUNCNAME[@]}
40    for (( i=0 ; (( i < $n )) ; i++ )) ; do 
41        local -i j=i+1
42	[ $j -eq $n ] && j=i  # main()'s file is the same as the first caller
43	echo "${FUNCNAME[$i]} called from file " \
44	    "\`${BASH_SOURCE[$j]}' at line ${BASH_LINENO[$j]}"
45    done
46    source ./dbg-support.sub
47}    
48
49fn4() {
50    echo "fn4 here. Calling fn3..."
51    fn3
52}    
53
54
55#!../bash
56#
57# Test of support for debugging facilities in bash
58# 
59# Test debugger set option functrace - set on. Not in vanilla Bash 2.05
60#
61set -o functrace
62trap 'print_debug_trap $LINENO' DEBUG
63trap 'print_return_trap $LINENO' RETURN
64
65# Funcname is now an array, but you still can't see it outside a function
66echo "FUNCNAME" ${FUNCNAME[0]:-main}
67
68# We should trace into the below. 
69# Start easy with a simple function.
70fn1
71fn2
72fn3
73source ./dbg-support.sub
74
75# Test debugger set option functrace - set off
76set +T
77
78# We should not trace into this.
79fn1
80fn2
81fn3
82fn4
83source ./dbg-support.sub
84
85# Another way to say: set -o functrace
86set -T
87
88# We should trace into this.
89source ./dbg-support.sub
90set +T
91
92# Test that the line numbers in the presence of conditionals are correct.
93for (( i=0 ; (( i <= 2 )) ; i++ )) ; do 
94    if [ $i -eq 2 ] ; then
95	echo "Hit 2"
96    fi
97    j=4
98done
99
100#
101# Check line numbers in command substitution
102#
103echo $(sourced_fn)
104echo `sourced_fn`
105x=$((sourced_fn))
106x={ sourced_fn }
107
108# Make sure we step into sourced_fn as a comand when we request to do so.
109# Vanilla bash 2.0 doesn't do.
110set -o functrace
111x={ sourced_fn }
112
113# Should see line number of xyzzy below. Vanilla bash 2.05b doesn't do
114case xyzzy in
115 a )
116    x=5
117    ;; 
118 xyzz? )
119    case 3 in 
120      2 ) 
121	x=6 ;;
122      3 ) 
123	echo "got it" ;;
124      * ) echo "no good" ;;
125      esac
126    ;;
127 * )
128esac
129
130# Should see line numbers for initial for lines.
131for i in 0 1 ; do
132  for j in 3 4 ; do
133    ((x=i+j))
134  done
135done
136#;;; Local Variables: ***
137#;;; mode:shell-script ***
138#;;; eval: (sh-set-shell "bash") ***
139#;;; End: ***
140