summaryrefslogtreecommitdiffstats
path: root/testsuite/systemtap.base/labels.exp
blob: f759beefb1cf5d484923b2aacf706ea95c1cc07a (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
set test "labels"
if {![installtest_p]} {untested $test; return}
if {![utrace_p]} { untested $test; return }

# Compile a C program to use as the user-space probing target
set label_srcpath "[pwd]/labels.c"
set label_exepath "[pwd]/labels.x"
set label_sopath "[pwd]/labels.so"
set label_flags "additional_flags=-g"
set fp [open $label_srcpath "w"]
puts $fp "
int
foo ()
{
init_an_int:
  return 1;
}
int
main ()
{
  sleep(5);
  foo();
  int a = 0;
  int b = 0;
  char *c;
init_an_int:
  a = 2;
init_an_int_again:
  b = 3;
  c = \"abc\";
ptr_inited:
  return 1;
}
"
close $fp

set label_stppath "[pwd]/labels.stp"
set fp [open $label_stppath "w"]
puts $fp "
probe process(\"labels.x\").function(\"main@labels.c\").label(\"init_*\") {printf (\"VARS %s\\n\",\$\$vars)}
probe process(\"labels.x\").function(\"main@labels.c\").label(\"ptr_inited\") {printf (\"VARS %s\\n\",\$\$vars)}
probe process(\"labels.x\").function(\"main@labels.c\").label(\"init_an_int\") {printf (\"init_an_int\\n\")}
"
close $fp

set ok 0

set res [target_compile $label_srcpath $label_exepath executable $label_flags]
if { $res != "" } {
    verbose "target_compile failed: $res" 2
    fail "compiling labels.c -g"
    catch {exec rm -f $label_srcpath $label_stppath}
    return
} else {
    pass "compiling labels.c -g"
}

# line number error

set ok 0
spawn stap -l "process(\"$label_exepath\").function(\"foo@${label_srcpath}:10\").label(\"*\")"

wait
expect {
    -timeout 180
    -re {no match while resolving probe point} { incr ok; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$ok == 1} { pass "$test :N .label" } { fail "$test :N .label $ok" }

# line number 

set ok 0
spawn stap -l "process(\"$label_exepath\").function(\"foo@${label_srcpath}:4\").label(\"*\")"

wait
expect {
    -timeout 180
    -re {process.*function.*labels.c:5...label..init_an_int} { incr ok; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$ok == 1} { pass "$test :N .label" } { fail "$test :N .label $ok" }

# list of labels

spawn stap -l "process(\"$label_exepath\").function(\"*\").label(\"*\")"

wait
set ok 0
expect {
    -timeout 180
    -re {^process[^\r\n]*function[^\r\n]*labels.c:5...label..init_an_int..\r\n}
	{ incr ok; exp_continue }
    -re {^process[^\r\n]*function[^\r\n]*labels.c:16...label..init_an_int..\r\n}
	{ incr ok; exp_continue }
    -re {^process[^\r\n]*function[^\r\n]*labels.c:18...label..init_an_int_again..\r\n}
	{ incr ok; exp_continue }
    -re {^process[^\r\n]*function[^\r\n]*labels.c:21...label..ptr_inited..\r\n}
	{ incr ok; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$ok == 4} { pass "$test -l .label" } { fail "$test -l .label $ok" }

# label in an executable

set ok 0
verbose -log "spawn stap -c $label_exepath $label_stppath"
spawn stap -c $label_exepath $label_stppath

wait
expect {
    -timeout 180
    -re {VARS a=0x0 b=0x0.*init_an_int.*VARS a=0x2 b=0x0.*VARS a=0x2 b=0x3 c=0x[a-f01-9]} { incr ok; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$ok == 1} { pass "$test exe .label" } { fail "$test exe .label $ok" }

# address of label in an executable

set label_shpath "[pwd]/label.sh"
set fp [open $label_shpath "w"]
puts $fp "
readelf --debug-dump $label_exepath | awk \"
/init_an_int_again/ {have_label=1} 
/DW_AT_low_pc/ {if (have_label) {print \$3;exit;}}
\"
"
close $fp
spawn sh $label_shpath
expect {
    -re {0x[0-9a-f]*} 
}
set nomatch 0
spawn stap -p2 -l "process\(\"$label_exepath\"\).statement($expect_out(0,string))"
expect {
    -timeout 180
    -re {semantic error: no match} { incr nomatch; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$nomatch == 0} { pass "$test exe .statement" } { fail "$test exe .statement" }

set ok 0

set label_flags "additional_flags=-g additional_flags=-shared additional_flags=-fPIC"
set res [target_compile $label_srcpath $label_sopath executable $label_flags]
if { $res != "" } {
    verbose "target_compile failed: $res" 2
    fail "compiling labels.c -g"
    catch {exec rm -f $label_srcpath $label_stppath}
    return
} else {
    pass "compiling labels.c -g"
}

# label in a shared object

spawn stap -p2 -l "process\(\"$label_sopath\"\).function\(\"\*\"\).label\(\"init_an_int_again\"\)"
expect {
    -timeout 180
    -re {process.*function} { incr ok; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$ok == 1} { pass "$test so .label" } { fail "$test so .label $ok" }

# address of label in a shared object

set label_shpath "[pwd]/label.sh"
set fp [open $label_shpath "w"]
puts $fp "
readelf --debug-dump $label_sopath | awk \"
/init_an_int_again/ {have_label=1} 
/DW_AT_low_pc/ {if (have_label) {print \$3;exit;}}
\"
"
close $fp
spawn sh $label_shpath
expect {
    -re {0x[0-9a-f]*} 
}
set nomatch 0
spawn stap -p2 -l "process\(\"$label_sopath\"\).statement($expect_out(0,string))"
expect {
    -timeout 180
    -re {semantic error: no match} { incr nomatch; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}

if {$nomatch == 0} { pass "$test so .statement" } { fail "$test so .statement" }

catch {exec rm -f $label_srcpath $label_stppath $label_exepath $label_shpath $label_sopath}