summaryrefslogtreecommitdiffstats
path: root/tests/dbg-support.tests
blob: ad06d011920ef99e8351c79ce0c8147c1bd6fba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!../bash
#
# Test correct functioning bash debug support not via the bashdb
# debugger but merely by printing via print_trap()
# $Id: dbg-support.tests,v 1.13 2003/02/17 22:02:25 rockyb Exp $
shopt -s extdebug
print_debug_trap() {
   echo "debug lineno: $1 ${FUNCNAME[1]}"
   return
}

print_return_trap() {
   echo "return lineno: $1 ${FUNCNAME[1]}"
   return
}

fn1() {
    echo "LINENO $LINENO"
    echo "LINENO $LINENO"
    echo "BASH_SOURCE[0]" ${BASH_SOURCE[0]}
    echo "FUNCNAME[0]" ${FUNCNAME[0]}
    echo `caller`
    echo `caller 0`
    echo `caller 1`
    echo `caller foo`
}    

fn2() {
    echo "fn2 here. Calling fn1..."
    fn1
}    

fn3() {
    echo "LINENO $LINENO"
    echo "BASH_SOURCE[0]" ${BASH_SOURCE[0]}
    
    # Print a stack trace
    declare -i n
    n=${#FUNCNAME[@]}
    for (( i=0 ; (( i < $n )) ; i++ )) ; do 
        local -i j=i+1
	[ $j -eq $n ] && j=i  # main()'s file is the same as the first caller
	echo "${FUNCNAME[$i]} called from file " \
	    "\`${BASH_SOURCE[$j]}' at line ${BASH_LINENO[$j]}"
    done
    source ./dbg-support.sub
}    

fn4() {
    echo "fn4 here. Calling fn3..."
    fn3
}    


#!../bash
#
# Test of support for debugging facilities in bash
# 
# Test debugger set option functrace - set on. Not in vanilla Bash 2.05
#
set -o functrace
trap 'print_debug_trap $LINENO' DEBUG
trap 'print_return_trap $LINENO' RETURN

# Funcname is now an array, but you still can't see it outside a function
echo "FUNCNAME" ${FUNCNAME[0]:-main}

# We should trace into the below. 
# Start easy with a simple function.
fn1
fn2
fn3
source ./dbg-support.sub

# Test debugger set option functrace - set off
set +T

# We should not trace into this.
fn1
fn2
fn3
fn4
source ./dbg-support.sub

# Another way to say: set -o functrace
set -T

# We should trace into this.
source ./dbg-support.sub
set +T

# Test that the line numbers in the presence of conditionals are correct.
for (( i=0 ; (( i <= 2 )) ; i++ )) ; do 
    if [ $i -eq 2 ] ; then
	echo "Hit 2"
    fi
    j=4
done

#
# Check line numbers in command substitution
#
echo $(sourced_fn)
echo `sourced_fn`
x=$((sourced_fn))
x={ sourced_fn }

# Make sure we step into sourced_fn as a comand when we request to do so.
# Vanilla bash 2.0 doesn't do.
set -o functrace
x={ sourced_fn }

# Should see line number of xyzzy below. Vanilla bash 2.05b doesn't do
case xyzzy in
 a )
    x=5
    ;; 
 xyzz? )
    case 3 in 
      2 ) 
	x=6 ;;
      3 ) 
	echo "got it" ;;
      * ) echo "no good" ;;
      esac
    ;;
 * )
esac

# Should see line numbers for initial for lines.
for i in 0 1 ; do
  for j in 3 4 ; do
    ((x=i+j))
  done
done

${THIS_SH} ./dbg-support3.sub

#;;; Local Variables: ***
#;;; mode:shell-script ***
#;;; eval: (sh-set-shell "bash") ***
#;;; End: ***