Debian Codegraph @

testsuite/lib/systemtap.exp

raw
1 load_lib site.exp
2 load_lib "compile_flags.exp"
3 # Since 'server_ns_cleanup' is called in shutdown_server, we need to
4 # include server_ns.exp here.
5 load_lib "server_ns.exp"
6
7 proc installtest_p {} {
8 global TOOL_OPTIONS
9 if {[info exists TOOL_OPTIONS] && [string match "*install*" $TOOL_OPTIONS]} {
10 return 1
11 } else { return 0 }
12 }
13
14
15 proc grep_kallsyms { pattern } {
16 if {! [catch {exec grep -q "$pattern" "/proc/kallsyms"} dummy]} {
17 return 1
18 }
19 return 0
20 }
21
22
23 # Test for tracepoint-based utrace, or rather the in-kernel
24 # facilities which enable our emulation of utrace.
25 proc utrace_emu_p {} {
26 # Check for the set of 5 tracepoints we need and task_work_add()
27 # (see runtime/autoconf-utrace-via-tracepoints.c for details).
28 return [expr [grep_kallsyms task_work_add] \
29 && [grep_kallsyms tracepoint_sched_process_fork] \
30 && [grep_kallsyms tracepoint_sched_process_exec] \
31 && [grep_kallsyms tracepoint_sched_process_exit] \
32 && [grep_kallsyms tracepoint_sys_enter] \
33 && [grep_kallsyms tracepoint_sys_exit] \
34 ]
35 }
36
37
38 # Test for utrace - any flavor will do...
39 proc utrace_p {} {
40 return [utrace_emu_p]
41 }
42
43
44 proc classic_uprobes_p {} {
45 # If this is a utrace kernel, then we can use our version of uprobes.
46 # No need to build it now, stap will handle that itself.
47 #
48 # Although ia64 has classical utrace, uprobes hasn't been
49 # ported there (PR7081).
50 return [expr ! [istarget ia64-*-*] ]
51 }
52
53 proc inode_uprobes_p {} {
54 # inode-uprobes (or unlikely compiled-in classical uprobes?)
55 #
56 # Note we're looking for " uprobe_register" to avoid finding
57 # 'kallsyms_uprobe_register' from a loaded systemtap module.
58 return [expr ([grep_kallsyms register_uprobe] \
59 || [grep_kallsyms " uprobe_register"]) \
60 && ! [classic_uprobes_p] \
61 ]
62 }
63
64
65 proc uprobes_p {} {
66 return [expr [classic_uprobes_p] || [inode_uprobes_p] ]
67 }
68
69
70 proc classic_uretprobes_p {} {
71 # Classic uprobes always has uretprobes
72 return [classic_uprobes_p]
73 }
74
75 proc inode_uretprobes_p {} {
76 # inode-uprobes, now with return probes! Looking for any mention of
77 # uretprobe, notably arch_uretprobe_hijack_return_addr
78 return [expr [inode_uprobes_p] && [grep_kallsyms uretprobe] ]
79 }
80
81 proc uretprobes_p {} {
82 return [expr [classic_uretprobes_p] || [inode_uretprobes_p] ]
83 }
84
85 proc plt_probes_p {} {
86 # .plt probes need uprobes and a supported arch (x86 and arm)
87 return [expr [uprobes_p] \
88 && [regexp "^(x86_64|i.86|arm.*|aarch64)$" $::tcl_platform(machine)] ]
89 }
90
91 proc perf_probes_p {} {
92 # perf probes need and exported perf_event_create_kernel_counter
93 return [grep_kallsyms perf_event_create_kernel_counter]
94 }
95
96 proc hwbkpt_probes_p {} {
97 global have_hw_breakpoint_p
98 return $have_hw_breakpoint_p
99 }
100
101 proc readline_p {} {
102 global systemtap_readline_p
103 return $systemtap_readline_p
104 }
105
106 # Callee probes require GCC v4.7 and stap compiled
107 # with elfutils 0.153
108 proc callee_probes_p {} {
109 global GCC_Version ELF_Version
110 return [expr [strverscmp $GCC_Version 4.7] >= 0 && \
111 [strverscmp $ELF_Version 0.153] >= 0]
112 }
113
114 proc dyninst_p {} {
115 global systemtap_dyninst_p
116 return $systemtap_dyninst_p
117 }
118
119 proc nss_p {} {
120 global systemtap_nss_p
121 return $systemtap_nss_p
122 }
123
124 proc java_p {} {
125 global systemtap_java_p
126 return $systemtap_java_p
127 }
128
129 proc python2_p {} {
130 global systemtap_python2_p
131 return $systemtap_python2_p
132 }
133
134 proc python3_p {} {
135 global systemtap_python3_p
136 return $systemtap_python3_p
137 }
138
139 proc bootprobing_p {} {
140 global Dracut_Version
141 return [expr [strverscmp $Dracut_Version 025] >= 0 && \
142 [file exists "/sbin/new-kernel-pkg"]]
143 }
144
145 proc module_refresh_p {} {
146 return [min_kernel_vers_p 2.6.29]
147 }
148
149 proc hrtimer_p {} {
150 return [min_kernel_vers_p 2.6.17]
151 }
152
153 proc kprobes_disabling_p {} {
154 return [min_kernel_vers_p 2.6.30]
155 }
156
157 # Returns 1 if kernel vers >= @min, 0 otherwise. The @min parameter must be in
158 # the format x.y.z (e.g. 2.6.18).
159 proc min_kernel_vers_p {min} {
160
161 # Check proper format
162 if {![regexp {^[0-9]+(\.[0-9]+)*$} $min]} {
163 error "$min is not a valid version number"
164 }
165
166 set uname [exec uname -r]
167
168 if {![regexp {^([0-9]+\.[0-9]+\.[0-9]+).*} $uname dummy cur]} {
169 error "can't parse version number from uname -r"
170 }
171
172 return [expr [strverscmp $cur $min] >= 0]
173 }
174
175 proc bpf_p {} {
176 # configure.ac checks for <linux/bpf.h>, so we will too.
177 return [file exists "/usr/include/linux/bpf.h"]
178 }
179
180 proc get_runtime_list {} {
181 # Always return the default runtime.
182 set runtime_list [list ""]
183
184 if {[dyninst_p]} {
185 lappend runtime_list "dyninst"
186 }
187 return $runtime_list
188 }
189
190
191 proc print_systemtap_version {} {
192 set version [exec /bin/uname -r]
193 set location "/boot/vmlinux-$version"
194 if {! [file exists $location]} {
195 # try the debuginfo location
196 set location "/usr/lib/debug/lib/modules/$version/vmlinux"
197 if {! [file exists $location]} { set location "" }
198 }
199
200 print "kernel location: $location"
201 print "kernel version: $version"
202
203 set location [exec /usr/bin/which stap]
204 regexp {version [^)]*} [exec stap -V 2>@ stdout] version
205
206 print "systemtap location: $location"
207 print "systemtap version: $version"
208
209 set location [exec /usr/bin/which gcc]
210 set version [exec gcc --version | head -1]
211
212 print "gcc location: $location"
213 print "gcc version: $version"
214 }
215
216
217 # Check for the optional 'stapusr', 'stapsys', and 'stapdev' users
218 # (and make sure they belong to the correct groups). Check for the
219 # optional 'stap-server' user.
220 proc systemtap_check_users {} {
221 global systemtap_stapusr_user_exists
222 global systemtap_stapsys_user_exists
223 global systemtap_stapdev_user_exists
224 global systemtap_stap_server_user_exists
225
226 # The optional 'stapusr', 'stapsys', and 'stapdev' users are used
227 # in server testing. If they don't exist, those tests won't get
228 # run.
229
230 # If we have the optional 'stapusr' user, does he belong to the
231 # 'stapusr' group (and not the 'stapdev' or 'stapsys' groups)?
232 set systemtap_stapusr_user_exists 0
233 if {! [catch {exec /usr/bin/id -nG stapusr} user_info]} {
234 if {[regexp {.*stapusr.*} "$user_info"]
235 && ! [regexp {.*stapsys.*} "$user_info"]
236 && ! [regexp {.*stapdev.*} "$user_info"]} {
237 set systemtap_stapusr_user_exists 1
238 }
239 }
240
241 # If we have the optional 'stapsys' user, does he belong to the
242 # 'stapusr' and 'stapsys' groups (and not the 'stapdev' group)?
243 set systemtap_stapsys_user_exists 0
244 if {! [catch {exec /usr/bin/id -nG stapsys} user_info]} {
245 if {[regexp {.*stapusr.*} "$user_info"]
246 && [regexp {.*stapsys.*} "$user_info"]
247 && ! [regexp {.*stapdev.*} "$user_info"]} {
248 set systemtap_stapsys_user_exists 1
249 }
250 }
251
252 # If we have the optional 'stapdev' user, does he belong to the
253 # 'stapusr' and 'stapdev' groups (it doesn't really matter about
254 # the 'stapsys' group)?
255 set systemtap_stapdev_user_exists 0
256 if {! [catch {exec /usr/bin/id -nG stapdev} user_info]} {
257 if {[regexp {.*stapusr.*} "$user_info"]
258 && [regexp {.*stapdev.*} "$user_info"]} {
259 set systemtap_stapdev_user_exists 1
260 }
261 }
262
263 # The optional 'stap-server' user is used to run the http
264 # server. If it doesn't exist, we can't run the http server.
265 set systemtap_stap_server_user_exists 0
266 if {! [catch {exec /usr/bin/id stap-server} user_info]} {
267 set systemtap_stap_server_user_exists 1
268 }
269 }
270
271
272 proc setup_systemtap_environment {} {
273 global srcdir env server_pid systemtap_dyninst_p
274 global systemtap_nss_p systemtap_java_p
275 global systemtap_python2_p systemtap_python3_p
276 global have_hw_breakpoint_p systemtap_readline_p
277 global kill_needs_doubledash
278
279 # need an absolute SRCDIR for the top-level src/ tree
280 # XXX: or, we could change nearby uses of ${SRCDIR}/testsuite to ${SRCDIR}
281 set env(SRCDIR) [fullpath $srcdir/..]
282
283 # pretend to be a dumb terminal so that coloring is always turned off
284 # otherwise, we will have problems with expect
285 set env(TERM) dumb
286
287 # Use a local systemtap directory and cache. Add user name so
288 # make check and sudo make check don't clobber each other.
289 set env(SYSTEMTAP_DIR) [pwd]/.systemtap-[exec whoami]
290 exec mkdir -p $env(SYSTEMTAP_DIR)
291
292 # Start with fresh server certificates
293 exec rm -fr $env(SYSTEMTAP_DIR)/ssl
294
295 # Remove the rc file
296 exec rm -f $env(SYSTEMTAP_DIR)/rc
297
298 # All hail the prophet lockdep
299 set chan [open $env(SYSTEMTAP_DIR)/rc w]
300 # Set the --rlimit-cpu just slightly under the 15 minutes, so that following
301 # two independent timeout mechanisms do not coincide.
302 puts $chan "--rlimit-cpu=850"
303 puts $chan "-E 'probe timer.s(900){error(\"probe timeout after 15 minutes\")}'"
304 close $chan
305
306 # Zap any previous uprobes, if any
307 catch { exec /sbin/rmmod uprobes }
308
309 # No compile-server started yet.
310 set server_pid 0
311
312 # If the environment variable ARCH exists, this can throw off our
313 # custom module building (when set to the wrong value). To be
314 # safe, remove it.
315 if [info exists env(ARCH)] {
316 verbose -log "clearing env ARCH (was $env(ARCH))"
317 unset env(ARCH)
318 }
319
320 # PATH, SYSTEMTAP_TAPSET, SYSTEMTAP_RUNTIME, LD_LIBRARY_PATH are already set.
321 foreach var {PATH STAP SRCDIR SYSTEMTAP_TAPSET SYSTEMTAP_RUNTIME SYSTEMTAP_DIR LD_LIBRARY_PATH} {
322 if [info exists env($var)] {
323 verbose -log "env $var = $env($var)"
324 }
325 }
326
327 # Remember if this very version of systemtap compiled with dyninst support
328 if {! [catch {exec sh -c "stap -V 2>&1 | grep -q DYNINST"} dummy]} {
329 set systemtap_dyninst_p 1
330 } else {
331 set systemtap_dyninst_p 0
332 }
333
334 # Remember if this selfsame version of systemtap compiled with nss support
335 if {! [catch {exec sh -c "stap -V 2>&1 | grep -q NSS"} dummy]} {
336 set systemtap_nss_p 1
337 } else {
338 set systemtap_nss_p 0
339 }
340
341 # Remember if this selfsame version of systemtap compiled with java support
342 if {! [catch {exec sh -c "stap -V 2>&1 | grep -q JAVA"} dummy]} {
343 set systemtap_java_p 1
344 } else {
345 set systemtap_java_p 0
346 }
347
348 # Remember if this selfsame version of systemtap compiled with
349 # python2 support
350 if {! [catch {exec sh -c "stap -V 2>&1 | grep -q PYTHON2"} dummy]} {
351 set systemtap_python2_p 1
352 } else {
353 set systemtap_python2_p 0
354 }
355
356 # Remember if this selfsame version of systemtap compiled with
357 # python3 support
358 if {! [catch {exec sh -c "stap -V 2>&1 | grep -q PYTHON3"} dummy]} {
359 set systemtap_python3_p 1
360 } else {
361 set systemtap_python3_p 0
362 }
363
364 # Remember if this selfsame version of systemtap supports HW breakpoints
365 if {! [catch {exec stap -l {kernel.data(0x123).rw}}]} {
366 set have_hw_breakpoint_p 1
367 } else {
368 set have_hw_breakpoint_p 0
369 }
370
371 # Remember if this selfsame version of systemtap compiled with
372 # readline support
373 if {! [catch {exec sh -c "stap -V 2>&1 | grep -q READLINE"} dummy]} {
374 set systemtap_readline_p 1
375 } else {
376 set systemtap_readline_p 0
377 }
378
379 # There are some kill executables that had issues with -SIG -PID, thinking
380 # that the -PID was an option (procps-ng v3.3.2 and v3.3.3) and thus require
381 # a -- before -PID. So check for those, otherwise, just assume that no -- is
382 # needed.
383 if {![catch {exec kill --version 2>@1} killver]} {
384 if {[string match "*procps-ng 3.3.\[23\]" $killver]} {
385 set kill_needs_doubledash 1
386 } else {
387 # any other procps versions support the standard convention
388 # and anything not procps (e.g. util-linux) supports both
389 set kill_needs_doubledash 0
390 }
391 } else {
392 # kill doesn't support --version (e.g. busybox), just assume standard
393 # convention
394 set kill_needs_doubledash 0
395 }
396
397 systemtap_check_users
398 arch_compile_init
399 return 1
400 }
401
402 proc shutdown_server {} {
403 global server_pid server_logfile
404 if { $server_pid != 0 } then {
405 verbose -log "Stopping the systemtap server with PID==$server_pid"
406 catch {exec stap-stop-server $server_pid}
407 set server_pid 0
408
409 # Collect then delete the temporary server log file
410 set output [exec cat $server_logfile]
411 verbose -log $output
412 catch {exec rm -f $server_logfile}
413 }
414
415 foreach module [glob -nocomplain [pwd]/stap_*.ko] {
416 exec /bin/rm -f $module
417 }
418 foreach sig [glob -nocomplain [pwd]/stap_*.ko.sgn] {
419 exec /bin/rm -f $sig
420 }
421
422 # Make sure that stap can no longer find the server.
423 set waited 0
424 set use_server --use-server
425 set res 0
426 set listRes 1
427 while { $listRes > 0 && $waited < 30 } {
428 catch exec sleep 1
429 incr waited
430 set res [catch { exec stap --list-servers=online,trusted,compatible >& stap-list-servers.out } seen_servers]
431 set listRes [catch { exec grep "^ host" stap-list-servers.out } looksee]
432 }
433 if { $listRes > 0 } {
434 verbose -log "shutdown_server: warning - stap can still find a server"
435 } else {
436 verbose -log "shutdown_server: no servers (after $waited tries)"
437 }
438
439 # Cleanup the custom server namespace (if needed).
440 server_ns_cleanup
441 }
442
443 # Set up the environment so that tests will be performed using the systemtap
444 # client and server.
445 proc setup_server { args } {
446 global srcdir env installed_stap use_server server_spec avahi_ok_p
447 global server_logfile
448
449 # Create a temporary server log file. Note that we'll delete it
450 # when shutting the server down.
451 if {[catch {exec mktemp} server_logfile]} {
452 verbose -log "Failed to create temporary server log file: $server_logfile"
453 exit 1
454 }
455
456 # Start the server
457 if {! [start_server $args]} then {
458 return 0
459 }
460
461 # Make sure that stap can find the server.
462 #
463 # Why is the server immediately trusted as an SSL peer without
464 # running "stap --trust-servers=ssl ..."? The server is always
465 # trusted by the user that started it. Technically, the
466 # certificates get saved under ~/.systemtap/ssl (of the user that
467 # started the server).
468 set waited 0
469 set use_server --use-server
470 set res 0
471 set listRes 1
472 while { $listRes != 0 && $waited < 15 } {
473 catch exec sleep 1
474 incr waited
475 set res [catch { exec stap --list-servers=online,trusted,compatible >& stap-list-servers.out } seen_servers]
476 set listRes [catch { exec grep "^ host" stap-list-servers.out } looksee]
477 }
478 verbose -log "stap --list-servers returned: res==$res"
479 verbose -log $seen_servers[exec cat stap-list-servers.out]
480 verbose -log "grep for servers returned: res==$res\n$looksee"
481
482 # Try to discover the port the server is listening on from the server log.
483 # We're matching a line of the form:
484 # "Thu Jul 25 11:58:11 2013: Using network address <addr>:37804"
485 set res [catch { exec /bin/cat $server_logfile | awk "/Using network address/ {print \$9}" } server_address]
486 send_log "server_address=='$server_address'\n"
487 if {$res != 0} then {
488 verbose -log "Unable to discover the address used by the systemtap server"
489 shutdown_server
490 return 0
491 }
492 set res [regsub ".*:(\[0-9\]\[0-9\]*)\$" $server_address {\1} server_port]
493 send_log "server_port=='$server_port'\n"
494 if {$res != 1} then {
495 verbose -log "Unable to discover the port used by the systemtap server"
496 shutdown_server
497 return 0
498 }
499 set server_spec [info hostname]:$server_port
500 send_log "server_spec=='$server_spec'\n"
501
502 set avahi_ok_p 1
503 if {$listRes != 0} then {
504 verbose -log "Unable to automatically find the systemtap server -- check firewall settings for mDNS"
505 set avahi_ok_p 0
506 verbose -log "Client/Server tests will be run by contacting the server directly as $server_spec"
507
508 # Make sure stap can contact the server directly
509 set use_server --use-server=$server_spec
510 set res [catch {exec stap $use_server -p2 -e {probe begin {exit()}}} looksee]
511 if {$res != 0} then {
512 verbose -log "Unable to contact the server at $server_spec directly"
513 shutdown_server
514 return 0
515 }
516 }
517
518 return 1
519 }
520
521 proc start_server { options } {
522 global srcdir env server_pid installed_stap server_logfile
523
524 if {! [nss_p]} { return 0 }
525
526 # Server management scripts and data are installed if this is an
527 # install test, otherwise there is some setup to do.
528 # Make sure the server management scripts and tools are on the $PATH.
529 if {! [installtest_p]} then {
530 set env(PATH) "$srcdir/..:[pwd]/..:$env(PATH)"
531 set installed_stap "[pwd]/../stap"
532 set env(SYSTEMTAP_SERVER_SCRIPTS) "$srcdir/.."
533 } else {
534 set env(PATH) "$env(PKGLIBDIR):$env(PATH)"
535 set installed_stap "$env(SYSTEMTAP_PATH)/stap"
536 set env(SYSTEMTAP_SERVER_SCRIPTS) $env(PKGLIBDIR)
537 }
538
539 # Try to start the server.
540 set status 0
541 if {[catch {eval {exec env STAP_PR11197_OVERRIDE=1 \
542 env SYSTEMTAP_STAP=[exec which stap] stap-start-server \
543 --log=$server_logfile} $options} server_pid]} {
544 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
545 set status [lindex $::errorCode 2]
546 }
547 }
548 verbose -log "output: $server_pid"
549 if { "$server_pid" == "" || $status != 0 } then {
550 verbose -log "Cannot start a systemtap server"
551 set server_pid 0
552 return 0
553 } else {
554 verbose -log "Started a systemtap server as PID==$server_pid"
555 }
556
557 return 1
558 }
559
560 proc cleanup {} {
561 # Stop the stap server, if we started it.
562 shutdown_server
563 }
564
565 proc normalize_arch { arch } {
566 if {$arch == "ppc64"} then {return "powerpc"}
567 if {$arch == "s390x"} then {return "s390"}
568 if {$arch == "i686"} then {return "i386"}
569 if {$arch == "armv5tel"} then {return "arm"}
570 if {$arch == "armv7l"} then {return "arm"}
571 if {$arch == "armv7lh"} then {return "arm"}
572 if {$arch == "aarch64"} then {return "arm64"}
573 if {$arch == "ppc64le"} then {return "powerpc"}
574 if {$arch == "mips64"} then {return "mips"}
575 return $arch
576 }
577
578 proc fullpath { path } {
579 if {[string index $path 0] != "/"} then {
580 # relative paths are anchored to the current directory
581 return [pwd]/$path
582 } else {
583 return $path
584 }
585 }
586
587 proc get_system_info {} {
588 global Host Snapshot Distro DistroID DistroVer GCC_Version GCC_FullVersion ELF_Version env SElinux CPUFlags Lockdown Dracut_Version
589
590 set Host [exec /bin/uname -a]
591 if [file exists ../SNAPSHOT] {
592 set Snapshot [exec /bin/cat ../SNAPSHOT]
593 } elseif [file exists $env(SRCDIR)/../SNAPSHOT] {
594 set Snapshot [exec /bin/cat $env(SRCDIR)/../SNAPSHOT]
595 } else {
596 regexp {version [^)]*} [exec stap -V 2>@ stdout] version
597 set Snapshot $version
598 }
599 set Distro "Linux"
600 if {[file exists /usr/bin/lsb_release] \
601 && ! [catch {exec /usr/bin/lsb_release -d} dummy]} {
602 # this produces one line of this format:
603 # Distribution:\tSTRING
604 set Distro [lrange $dummy 1 end]
605 } else {
606 foreach f {/etc/fedora-release /etc/enterprise-release /etc/redhat-release /etc/suse-release /etc/debian_version} {
607 if [file exists $f] then {set Distro [exec /bin/cat $f]; break }
608 }
609 }
610
611 # http://0pointer.de/blog/projects/os-release.html
612 # TODO: For distros not having /etc/os-release, we can set DistroID and DistroVer
613 # based on parsing the above Distro string if needed.
614 set DistroID "unknown"
615 set DistroVer "unknown"
616 if {[file exists /etc/os-release]} {
617 set DistroID [exec bash -c {source /etc/os-release; echo $ID}]
618 set DistroVer [exec bash -c {source /etc/os-release; echo $VERSION_ID}]
619 }
620
621 # Easily parsable version first major minor patch level
622 set n [exec echo "__GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__" | cpp -P]
623 set n [string map {" " "."} $n]
624 set n [string map {"\n" ""} $n]
625 set GCC_Version "$n"
626 # Plus full version between square brackets for GCC_FullVersion
627 set full [exec gcc --version | head -1]
628 set GCC_FullVersion "$n \[$full\]"
629 # Parse elfutils version stap was compiled with
630 regexp {version [^/]*/([^,/]*)} [exec stap -V 2>@ stdout] all ELF_Version
631 # selinux status
632 if [file exists /usr/sbin/getenforce] {
633 set SElinux [exec /usr/sbin/getenforce]
634 } else {
635 set SElinux "unknown"
636 }
637 # Get cpu flags
638 if {! [catch {exec grep flags /proc/cpuinfo | sort -u} dummy]} {
639 set CPUFlags [exec grep flags /proc/cpuinfo | sort -u]
640 } else {
641 set CPUFlags ""
642 }
643 if [file exists /sys/kernel/security/lockdown] {
644 set Lockdown [exec cat /sys/kernel/security/lockdown]
645 } else {
646 set Lockdown ""
647 }
648 # Parse dracut version
649 regexp {\d[^\-]*} [exec sh -c "dracut --help 2>&1 | grep Version || echo 0"] Dracut_Version
650 }
651
652 proc environment_sanity_test {} {
653 # PR11798: die if kernel-devel is not sufficient to build any modules
654 if {[catch {exec stap -p4 -e {probe begin {exit()}} 2>@ stdout} result]} {
655 puts "\n\n\n**** failed systemtap kernel-devel smoke test:\n"
656 puts $result
657 # puts "****\n"
658 # puts $options
659 puts "\n**** aborting testing.\n"
660 cleanup
661 exit 1
662 }
663
664 # PR11798: die also if kernel-debuginfo is not available
665 # NB: if one introduced a [kernel_debuginfo_p] proc like the
666 # ones for uprobes/utrace above, and sprinkled it throughout,
667 # then this wouldn't have to be a failing condition.
668 # Note the --skip-badvars -w, we just care there is some debuginfo,
669 # it is allowed have bad var location descriptors (which will cause
670 # some tests to fail of course). Just test -p2, kernel-devel smoke
671 # test above does a full module build, we don't need another one.
672 if {[catch {exec stap --skip-badvars -w -p2 -e {probe kernel.function("do_exit") {println ($code)}} 2>@ stdout} result]} {
673 puts "\n\n\n**** failed systemtap kernel-debuginfo smoke test:\n"
674 puts $result
675 # puts "****\n"
676 # puts $options
677 puts "\n**** aborting testing.\n"
678 cleanup
679 exit 1
680 }
681
682 # Create a tempory directory.
683 if {[catch {exec mktemp -d} tmpdir]} {
684 verbose -log "Failed to create temporary directory: $tmpdir"
685 exit 1
686 }
687 set curdir [pwd]
688 cd ${tmpdir}
689
690 # Make more likely that all development packages for the supported ABIs (-m64 -m32/-m31)
691 # are installed by building a quick hello.c and hello.cxx program with both.
692 set source "hello.c"
693 set hello_file [open $source "w"]
694 puts $hello_file "#include <stdio.h>"
695 puts $hello_file "int main () { printf(\"Hello World!\"); return 0; }"
696 close $hello_file
697 for {set i 0} {$i < [arch_compile_flags]} {incr i} {
698 set flags "additional_flags=-g compiler=gcc [arch_compile_flag $i]"
699 set exe "hello-[arch_compile_flag_name $i]"
700 set result [target_compile $source $exe executable $flags]
701 if { $result != "" } {
702 puts "\n\n\n**** failed gcc [arch_compile_flag_name $i] smoke test:\n"
703 puts $result
704 puts "Please install libgcc and glibc development packages for [arch_compile_flag_name $i]\n"
705 exit 1
706 }
707 }
708 set source "hello.cxx"
709 set hello_file [open $source "w"]
710 puts $hello_file "#include <iostream>"
711 puts $hello_file "using namespace std;"
712 puts $hello_file "int main () { cout << \"Hello World!\" << endl; return 0; }"
713 close $hello_file
714 for {set i 0} {$i < [arch_compile_flags]} {incr i} {
715 set flags "additional_flags=-g compiler=g++ [arch_compile_flag $i]"
716 set exe "hello-[arch_compile_flag_name $i]"
717 set result [target_compile $source $exe executable $flags]
718 if { $result != "" } {
719 puts "\n\n\n**** failed g++ [arch_compile_flag_name $i] smoke test:\n"
720 puts $result
721 puts "Please install libstdc++-devel package for [arch_compile_flag_name $i]\n"
722 exit 1
723 }
724 }
725
726 cd ${curdir}
727 catch {exec rm -rf $tmpdir}
728 }
729
730 if {! [setup_systemtap_environment]} then {
731 cleanup
732 exit 1
733 }
734
735 print_systemtap_version
736 get_system_info
737
738 proc systemtap_version {} {
739 set location [exec /usr/bin/which stap]
740 regexp {version [^)]*} [exec stap -V 2>@ stdout] version
741
742 clone_output "$location $version"
743 }
744
745 proc stap_run_batch {filename args} {
746 if {[string length $filename]} {
747 # Many of our test cases use "#! stap ...". Since these lack
748 # /full/paths, they are not really executable. (We can't have
749 # /full/paths because the choice of systemtap interpreter is set
750 # at "make check" time.)
751
752 # So we cheat. If the file begins with "#! stap", we will spawn
753 # stap manually here (relying on $PATH). Otherwise, we presume
754 # the file properly executable.
755 set file [open $filename r]
756 set firstbits [gets $file]
757 close $file
758 if {[regexp -line {\#! stap (.*)} $firstbits -> stap_args]} {
759 verbose -log "spawn1 stap $stap_args $filename $args"
760 # Make sure we don't accidentially add an extra empty argument.
761 if {$args == ""} {
762 eval spawn stap $stap_args $filename
763 } else {
764 eval spawn stap $stap_args $filename $args
765 }
766 } else {
767 verbose -log "spawn2 $filename $args"
768 # Make sure we don't accidentially add an extra empty argument.
769 if {$args == ""} {
770 spawn $filename
771 } else {
772 spawn $filename $args
773 }
774 }
775 } else {
776 verbose -log "spawn3 stap $args"
777 eval spawn stap $args
778 }
779
780 expect {
781 -timeout -1
782 -re {[^\r\n]*\r\n} { exp_continue }
783 eof { }
784 }
785 catch { close }
786 set results [wait]
787 verbose -log "wait results: $results"
788 if {[llength $results] >= 5} {
789 # Unexpected output. stap must have crashed
790 return -1
791 } else {
792 return [lindex $results 3]
793 }
794 }
795
796 proc as_root { command } {
797 set effective_uid [exec /usr/bin/id -u]
798
799 if {$effective_uid != 0} {
800 set command "sudo $command"
801 }
802 verbose -log "as_root $command"
803 set res [catch {eval exec $command} value]
804 verbose -log "OUT $value"
805 verbose -log "RC $res"
806 return $res
807 }
808
809 proc as_non_root { command } {
810 set effective_uid [exec /usr/bin/id -u]
811
812 if {$effective_uid == 0} {
813 # If logname fails (which it can if we're not in a login
814 # shell) or if we're root, use user 'nobody'.
815 #
816 # Note that user 'nobody' can't use systemtap to load kernel
817 # modules, since he isn't a member of the stapusr/stapdev
818 # groups. But, 'nobody' can use systemtap to compile a kernel
819 # module.
820 set logname "root"
821 if {[catch {exec /usr/bin/logname} logname] || $logname == "root"} {
822 set logname "nobody"
823 }
824
825 # User 'nobody' doesn't have a home directory, which means we
826 # can't create ~nobody/.systemtap. So, temporarily override
827 # SYSTEMTAP_DIR.
828 if {$logname == "nobody"} {
829 set stap_dir "/tmp/.systemtap-nobody"
830 set command "su -s /bin/sh $logname -c \"SYSTEMTAP_DIR=$stap_dir $command\""
831 } else {
832 set command "su -s /bin/sh $logname -c \"$command\""
833 }
834 }
835 verbose -log "as_non_root $command"
836 set res [catch {eval exec $command} value]
837 if {$effective_uid == 0 && $logname == "nobody"} {
838 # If we created a SYSTEMTAP_DIR for user 'nobody' above, we
839 # can now remove it.
840 catch {exec rm -rf $stap_dir}
841 }
842 verbose -log "OUT $value"
843 verbose -log "RC $res"
844 return $res
845 }
846
847 proc sdt_includes {} {
848 global srcdir env
849
850 # The wrapper sys/sdt.h for testing STAP_SDT_V[12] is here.
851 set dirs [list $srcdir]
852
853 if {[installtest_p]} {
854 # Use the installed <sys/sdt.h>.
855 lappend dirs $env(SYSTEMTAP_INCLUDES)
856 } else {
857 # Find <sys/sdt.h> in the source tree.
858 lappend dirs $srcdir/../includes
859 # The uninstalled, configured sdt-config.h has to be found here.
860 lappend dirs ../includes/sys
861 }
862
863 set flags ""
864 foreach dir $dirs {
865 set flags "$flags additional_flags=-isystem${dir}"
866 }
867
868 return $flags
869 }
870
871 proc stripped_p { EXE } {
872 if { [catch {eval exec "file $EXE | grep -q \"not stripped\""} dummy] } {
873 return 1
874 }
875 return 0
876 }
877
878 proc prelink_p {} {
879 global prelink_bin
880 set prelink_bin "/usr/sbin/prelink"
881 return [file exists "$prelink_bin"]
882 }
883
884 # Run prelink via a temp file, so it can succeed even when running from an nfs
885 # $HOME, for instance. Otherwise, prelink complains that it can't restore context.
886 proc prelink { FILE ADDR } {
887 global prelink_bin
888 if {![prelink_p]} {
889 verbose -log "Prelink is not found on this system"
890 return 0
891 }
892 if {[catch {exec mktemp} tmpfile]} {
893 verbose -log "Failed to create prelink temporary file: $tmpfile"
894 return 0
895 }
896 if {[catch {exec cp -a "$FILE" "$tmpfile"} result]} {
897 verbose -log "Failed to copy file for prelink: $result"
898 catch {exec rm -rf "$tmpfile"}
899 return 0
900 }
901 set prelink_cmd [concat "$prelink_bin" -vfNR -r "$ADDR" "$tmpfile"]
902 send_log "Executing: $prelink_cmd\n"
903 if {[catch {eval exec $prelink_cmd} result]} {
904 verbose -log "Prelink failed: $result"
905 catch {exec rm -rf "$tmpfile"}
906 return 0
907 }
908 if {[catch {exec mv "$tmpfile" "$FILE"} result]} {
909 verbose -log "Failed to move prelinked file back: $result"
910 catch {exec rm -rf "$tmpfile"}
911 return 0
912 }
913 return 1
914 }
915
916 # Send signal SIG to PID. If KILL_TIMEOUT > 0, waits KILL_TIMEOUT
917 # seconds before also sending a SIGKILL signal to PID. SIG can be with
918 # or without leading '-'. If AS_ROOT isn't 0, send the signal as root.
919 proc kill { SIG PID {KILL_TIMEOUT 0} {AS_ROOT 0} } {
920 global kill_needs_doubledash
921
922 if {$kill_needs_doubledash} {
923 set PID "-- $PID"
924 }
925 if {![string match -* $SIG]} {
926 set SIG "-$SIG"
927 }
928
929 if {$AS_ROOT != 0} {
930 as_root [list kill $SIG $PID]
931 } else {
932 verbose -log "Executing: kill $SIG $PID"
933 if {[catch {eval exec kill $SIG $PID} killout]} {
934 verbose -log "kill: $killout"
935 }
936 }
937
938 if {$KILL_TIMEOUT > 0} {
939 while {$KILL_TIMEOUT > 0} {
940 if {![file isdirectory /proc/$PID]} {
941 return
942 }
943 sleep 1
944 incr KILL_TIMEOUT -1
945 }
946 if {$AS_ROOT != 0} {
947 as_root [list kill -KILL $PID]
948 } else {
949 verbose -log "Executing: kill -KILL $PID"
950 if {[catch {eval exec kill -KILL $PID} killout]} {
951 verbose -log "kill: $killout"
952 }
953 }
954 }
955 }
956
957 # Compares vers1 to vers2. Returns an integer less than, equal to, or greater
958 # than zero if vers1 is before, equal, or after vers2. Similar to the GNU
959 # extension strverscmp(3), but restricted to simple M.N... version numbers.
960 proc strverscmp { vers1 vers2 } {
961
962 # Check that both versions are valid M.N... format
963 if {![regexp {^[0-9]+(\.[0-9]+)*$} $vers1]} {
964 error "$vers1 is not a valid version number"
965 }
966 if {![regexp {^[0-9]+(\.[0-9]+)*$} $vers2]} {
967 error "$vers2 is not a valid version number"
968 }
969
970 while {1} {
971
972 # Get the next M for each as well as following N...
973 regexp {^([0-9]+)(\.(.*))?} $vers1 dummy subvers1 dummy nextvers1
974 regexp {^([0-9]+)(\.(.*))?} $vers2 dummy subvers2 dummy nextvers2
975
976 # Compare M
977 if {$subvers1 < $subvers2} { return -1 }
978 if {$subvers1 > $subvers2} { return 1 }
979 # The M parts are equal
980
981 # Check if one (or both) of them don't have a next subversion
982 if {![string length $nextvers1] \
983 && ![string length $nextvers2]} { return 0 }
984 if {![string length $nextvers1] \
985 && [string length $nextvers2]} { return -1 }
986 if {[string length $nextvers1] \
987 && ![string length $nextvers2]} { return 1 }
988
989 # They both have a subversion following, let's re-iterate
990
991 # Sanity check that we're making progress and not looping forever
992 if {[string length $nextvers1] >= [string length $vers1]} \
993 { error "$vers1 not converging" }
994 if {[string length $nextvers2] >= [string length $vers2]} \
995 { error "$vers2 not converging" }
996
997 set vers1 $nextvers1
998 set vers2 $nextvers2
999 }
1000 }
1001
1002 # The original DejaGnu setup_kfail()'s target triplet e.g. ('s390x-ibm-linux-gnu',
1003 # or 'x86_64-pc-linux-gnu', or even 'rs6000*-*-aix*') as implemented in dejagnu's
1004 # framework.exp, doesn't offer fine enough granularity.
1005 proc _setup_kfail { bugid {arches "-"} {distroids "-"} {maxkernelver "-"} {maxdistrover "-"} } {
1006 global DistroID DistroVer
1007
1008 if { "$arches" != "-" } {
1009 if {[string first ,$::tcl_platform(machine), ,$arches,] == -1} {
1010 return 0
1011 }
1012 }
1013 if { "$distroids" != "-" } {
1014 if {[string first ,$DistroID, ,$distroids,] == -1} {
1015 return 0
1016 }
1017 }
1018 if { "$maxkernelver" != "-" } {
1019 if { [min_kernel_vers_p $maxkernelver] <= 0 } {
1020 return 0
1021 }
1022 }
1023 if { "$maxdistrover" != "-" } {
1024 if { [strverscmp $DistroVer $maxdistrover] > 0 } {
1025 return 0
1026 }
1027 }
1028
1029 setup_kfail $bugid *-*-*
1030 }