Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/buildid.exp

raw
1 set test "buildid"
2 set bid_stp $srcdir/$subdir/buildid.stp
3
4 if {![installtest_p]} { untested $test; return }
5 if {![uprobes_p]} { untested "$test (uprobes)"; return }
6
7 proc cleanup_handler { verbose } {
8 if { $verbose == 0 } {
9 catch {exec rm -f bid.hex bid.ko bid_nocheck.ko bid_shared.ko bid.stp buildid1.ro buildid2.ro buildid.c buildid.o buildid.ro buildid.x libbid1.so libbid.so
10 }
11 }
12 }
13
14 proc error_handler { res message } {
15 global verbose
16 if { $res == 0 } {
17 verbose $message 2
18 fail $message
19 cleanup_handler $verbose
20 return 1
21 } else {
22 pass $message
23 return 0
24 }
25 }
26
27 # Not Shared
28
29 set bid_srcpath $srcdir/$subdir/buildid.c
30 set res [target_compile $bid_srcpath "[pwd]/buildid.o" object "additional_flags=-g"]
31 if { [error_handler [expr {$res==""}] "buildid.o"] } { return }
32
33 set res [target_link "buildid.o" "buildid.ro" "-r"]
34 if { [error_handler [expr {$res==""}] "buildid.ro"] } { return }
35
36 set bid_exepath ./buildid.x
37 set res [target_compile "" $bid_exepath executable "additional_flags=buildid.ro"]
38 if { [error_handler [expr {$res==""}] "$bid_exepath"] } { return }
39
40 # Check that the build ID exists on this system
41 set bid_check_passed 0
42 set cmd [concat {/usr/bin/eu-readelf -n $bid_exepath}]
43 eval spawn $cmd
44 expect {
45 -timeout 180
46 -re {^[^\r\n]*GNU_BUILD_ID[^\r\n]*\r\n} {
47 set bid_check_passed 1
48 exp_continue
49 }
50 -re {^[^\r\n]*\r\n} {
51 exp_continue
52 }
53 timeout {
54 set bid_check_passed 0
55 }
56 }
57 catch {close}; catch {wait}
58
59 if {$bid_check_passed == 0} {
60 untested "Build IDs do not exist on this system"
61 cleanup_handler $verbose
62 return
63 }
64
65
66 set test "nonshared build id"
67
68 # Since uprobes.ko may not be prebuilt in the runtime, and we don't
69 # yet have a way to discover the path to the cache-build uprobes.ko,
70 # we need to preload it. This dummy run lets us go through the
71 # motions to get uprobes loaded, and then we don't need to worry about
72 # each staprun -u finding its module.
73 #
74 # PR13536: it turns out PR5163 makes staprun -u (without /path)
75 # impossible, so stop using it in this test case. Instead we hope and
76 # pray and beseech the almighty spaghetti monster that uprobes stay
77 # loaded after the following ditty:
78 set bid_ko [pwd]/bid.ko
79 catch {exec rm -f $bid_ko}
80 set status 0
81 if {[catch {exec stap -m bid $bid_stp $bid_exepath -c /bin/true} res]} {
82 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
83 set status [lindex $::errorCode 2]
84 }
85 }
86 if {$status == 0 && [file exists $bid_ko]} {
87 pass "$test (module compilation)"
88 } else {
89 verbose -log $res
90 fail "$test (module compilation)"
91 cleanup_handler $verbose
92 return
93 }
94
95 set ok 0
96 spawn staprun $bid_ko -c $bid_exepath
97 expect {
98 -timeout 180
99 -re {x=1} { incr ok; exp_continue }
100 timeout { fail "$test (timeout)" }
101 eof { }
102 }
103 catch {close}; catch {wait}
104 if { [error_handler [expr $ok==1] "$test ok ($ok)"] } { return }
105
106 # Alter the build id using objcopy. First, remove the build id from
107 # the target executable. (Since the next test relinks the target
108 # executable, this is OK.)
109 set status 0
110 if {[catch {exec objcopy --remove-section=.note.gnu.build-id $bid_exepath} \
111 res]} {
112 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
113 set status [lindex $::errorCode 2]
114 }
115 }
116 if {$status == 0 && [file exists $bid_exepath]} {
117 pass "$test (build id removal)"
118 } else {
119 verbose -log $res
120 fail "$test (build id removal)"
121 cleanup_handler $verbose
122 return
123 }
124
125 set ok 0
126 spawn staprun $bid_ko -c $bid_exepath
127 expect {
128 -timeout 180
129 -re {ERROR: Build-id mismatch} { incr ok; exp_continue }
130 timeout { fail "$test (timeout)" }
131 eof { }
132 }
133 catch {close}; catch {wait}
134 if { [error_handler [expr $ok==1] "$test !ok - removed ($ok)"] } { return }
135
136 # Alter the build id using objcopy. Add in an additional build id section.
137 set bid_bid $srcdir/$subdir/buildid.hex
138 system "objcopy --remove-section=.note.gnu.build-id buildid.ro buildid1.ro"
139 system "objcopy --add-section .note.gnu.build-id=$bid_bid buildid1.ro buildid2.ro"
140 set res [target_compile "" $bid_exepath executable "additional_flags=buildid2.ro"]
141 if { [error_handler [expr {$res==""}] "$test (build id modified)"] } { return }
142
143 set ok 0
144 spawn staprun $bid_ko -c $bid_exepath
145 expect {
146 -timeout 180
147 -re {ERROR: Build-id mismatch} { incr ok; exp_continue }
148 timeout { fail "$test (timeout)" }
149 eof { }
150 }
151 catch {close}; catch {wait}
152 error_handler [expr $ok==1] "$test !ok - modified ($ok)"
153
154 # stap -DSTP_NO_BUILDID_CHECK with altered build id
155
156 set test "STP_NO_BUILDID_CHECK"
157
158 # Compile a stap module with STP_NO_BUILDID_CHECK.
159 set ok 0
160 set bid_nocheck_ko [pwd]/bid_nocheck.ko
161 catch {exec rm -f $bid_nocheck_ko}
162 set status 0
163 if {[catch {exec stap -m bid_nocheck -p4 -DSTP_NO_BUILDID_CHECK $bid_stp $bid_exepath} res]} {
164 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
165 set status [lindex $::errorCode 2]
166 }
167 }
168 if {$status == 0 && [file exists $bid_nocheck_ko]} {
169 pass "$test (module compilation)"
170 } else {
171 verbose -log $res
172 fail "$test (module compilation)"
173 cleanup_handler $verbose
174 return
175 }
176
177 spawn staprun $bid_nocheck_ko -c $bid_exepath
178 expect {
179 -timeout 180
180 -re {x=1} { incr ok; exp_continue }
181 timeout { fail "$test (timeout)" }
182 eof { }
183 }
184 catch {close}; catch {wait}
185 error_handler [expr $ok==1] "$test ok ($ok)"
186
187 # Shared
188
189 set test "shared build id"
190 set bid_flags "additional_flags=-DNO_MAIN"
191 set bid_flags "$bid_flags additional_flags=-shared"
192 set bid_flags "$bid_flags additional_flags=-g"
193 set res [target_compile $bid_srcpath "libbid.so" executable $bid_flags]
194 if { [error_handler [expr {$res==""}] "libbid.so -DNO_MAIN"] } { return }
195
196 set bid_flags "additional_flags=-DONLY_MAIN"
197 set bid_flags "$bid_flags additional_flags=-Wl,-rpath,[pwd]"
198 set bid_flags "$bid_flags additional_flags=-L[pwd]"
199 set bid_flags "$bid_flags additional_flags=-lbid"
200 set res [target_compile $bid_srcpath "$bid_exepath" executable $bid_flags]
201 if { [error_handler [expr {$res==""}] "$bid_exepath -DONLY_MAIN"] } { return }
202
203 # Compile a stap module for the shared library testcase
204 set bid_shared_ko [pwd]/bid_shared.ko
205 catch {exec rm -f $bid_shared_ko}
206 set status 0
207 if {[catch {exec stap -m bid_shared -p4 $bid_stp libbid.so} res]} {
208 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
209 set status [lindex $::errorCode 2]
210 }
211 }
212 if {$status == 0 && [file exists $bid_shared_ko]} {
213 pass "$test (module compilation)"
214 } else {
215 verbose -log $res
216 fail "$test (module compilation)"
217 cleanup_handler $verbose
218 return
219 }
220
221 set ok 0
222 spawn staprun $bid_shared_ko -c $bid_exepath
223 expect {
224 -timeout 180
225 -re {x=1} { incr ok; exp_continue }
226 timeout { fail "$test (timeout)" }
227 eof { }
228 }
229 catch {close}; catch {wait}
230 if { [error_handler [expr $ok==1] "$test ok ($ok)"] } { return }
231
232 # Alter the build id using objcopy. First, remove the build id from
233 # the target library.
234 set status 0
235 if {[catch {exec objcopy --remove-section=.note.gnu.build-id libbid.so} res]} {
236 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
237 set status [lindex $::errorCode 2]
238 }
239 }
240 if {$status == 0 && [file exists libbid.so]} {
241 pass "$test (build id removal)"
242 } else {
243 verbose -log $res
244 fail "$test (build id removal)"
245 cleanup_handler $verbose
246 return
247 }
248
249 set ok 0
250 spawn staprun $bid_shared_ko -c $bid_exepath
251 expect {
252 -timeout 180
253 -re {ERROR: Build-id mismatch} { incr ok; exp_continue }
254 timeout { fail "$test (timeout)" }
255 eof { }
256 }
257 catch {close}; catch {wait}
258 if { [error_handler [expr $ok==1] "$test !ok - removed ($ok)"] } { return }
259
260 # Alter the build id using objcopy. Add in an "bad" build id
261 # section.
262 catch {exec objcopy --add-section .note.gnu.build-id=$bid_bid libbid.so} res
263 if {[file exists libbid.so]} {
264 pass "$test (build id modified)"
265 } else {
266 verbose -log $res
267 fail "$test (build id modified)"
268 cleanup_handler $verbose
269 return
270 }
271
272 set ok 0
273 spawn staprun $bid_shared_ko -c $bid_exepath
274 expect {
275 -timeout 180
276 -re {ERROR: Build-id mismatch} { incr ok; exp_continue }
277 timeout { fail "$test (timeout)" }
278 eof { }
279 }
280 catch {close}; catch {wait}
281 error_handler [expr $ok==1] "$test !ok - modified ($ok)"
282
283 cleanup_handler $verbose