summaryrefslogtreecommitdiffstats
path: root/testsuite/lib/systemtap.exp
blob: e74bd13cfdf8a79b272718380eca1be2488331f0 (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
load_lib site.exp

proc installtest_p {} {
    global TOOL_OPTIONS
    if {[info exists TOOL_OPTIONS] && [string match "*install*" $TOOL_OPTIONS]} {
        return 1
    } else { return 0 }
}


proc use_server_p {} {
    global TOOL_OPTIONS
    if {[info exists TOOL_OPTIONS] && [string match "*server*" $TOOL_OPTIONS]} {
        return 1
    } else { return 0 }
}


proc stap_exec {} {
    if {[info procs use_server_p] != "" && [use_server_p]} then {
	return "stap-client"
    } else {
	return "stap"
    }
}


proc print_systemtap_version {} {
    set version [exec /bin/uname -r]
    set location "/boot/vmlinux-$version"
    if {! [file exists $location]} {
	# try the debuginfo location
	set location "/usr/lib/debug/lib/modules/$version/vmlinux"
	if {! [file exists $location]} { set location "" }
    }

    print "kernel location: $location"
    print "kernel version: $version"

    set location [exec /usr/bin/which stap]
    regexp {version [^)]*} [exec stap -V 2>@ stdout] version

    print "systemtap location: $location"
    print "systemtap version: $version"
}


proc setup_systemtap_environment {} {
    global srcdir prefix env

    # need an absolute SRCDIR for the top-level src/ tree
    # XXX: or, we could change nearby uses of ${SRCDIR}/testsuite to ${SRCDIR}
    if {[string index $srcdir 0] != "/"} then {
        set env(SRCDIR) [exec pwd]/$srcdir/..
    } else {
        set env(SRCDIR) $srcdir/..
    }

    # Use a local systemtap directory and cache.  Add user name so
    # make check and sudo make check don't clobber each other.
    set env(SYSTEMTAP_DIR) [exec pwd]/.systemtap-[exec whoami]

    # PATH, SYSTEMTAP_TAPSET, SYSTEMTAP_RUNTIME, LD_LIBRARY_PATH are already set.
    foreach var {PATH STAP SRCDIR SYSTEMTAP_TAPSET SYSTEMTAP_RUNTIME SYSTEMTAP_DIR LD_LIBRARY_PATH} {
        if [info exists env($var)] {
            verbose -log "env $var = $env($var)"
        }
    }
}

proc get_system_info {} {
    global Host Snapshot Distro env
    
    set Host [exec /bin/uname -a] 
    if [file exists ../SNAPSHOT] {
	set Snapshot [exec /bin/cat ../SNAPSHOT]
    } elseif [file exists $env(SRCDIR)/../SNAPSHOT] {
	set Snapshot [exec /bin/cat $env(SRCDIR)/../SNAPSHOT]
    } else {
        regexp {version [^)]*} [exec stap -V 2>@ stdout] version
	set Snapshot $version
    }
    set Distro "Linux"
    if [file exists /etc/fedora-release] {set Distro [exec /bin/cat /etc/fedora-release]}
    if [file exists /etc/redhat-release] {set Distro [exec /bin/cat /etc/redhat-release]}
    if [file exists /etc/suse-release] {set Distro [exec /bin/cat /etc/suse-release]}
    if [file exists /etc/debian_version] {set Distro [exec /bin/cat /etc/debian_version]}
}

setup_systemtap_environment
print_systemtap_version
get_system_info

proc systemtap_init {args} {}
proc systemtap_version {} {}
proc systemtap_exit {} {}


proc stap_run_batch {args} {
    verbose -log "starting $args"

    # Many of our test cases use "#! stap ...".  Since these lack
    # /full/paths, they are not really executable.  (We can't have
    # /full/paths because the choice of systemtap interpreter is set
    # at "make check" time.)

    # So we cheat.  If the file begins with "#! stap", we will spawn
    # stap manually here (relying on $PATH).  Otherwise, we presume
    # the file properly executable.

    set file [open [lindex $args 0] r]
    set firstbits [gets $file]
    close $file
    if [regexp -line {\#! stap (.*)} $firstbits -> stap_args] {
        verbose -log "spawn1 stap $stap_args [lindex $args 0]"
        spawn stap $stap_args [lindex $args 0]
    } else {
        verbose -log "spawn2 $args"
        spawn $args
    }

    expect { 
	-timeout -1
        -re {[^\r]*\r} { verbose -log $expect_out(0,string); exp_continue } 
        eof { }
    }
    set results [wait]
    verbose -log "wait results: $results"
    if {[llength $results] >= 5} {
	# Unexpected output. stap must have crashed
	return -1
    } else {
	return [lindex $results 3]
    }
}

proc as_root { command } {

     set effective_pid [exec /usr/bin/id -u]

     if {$effective_pid != 0} {
	 set command "sudo $command"
     }
     verbose -log "as_root $command"
     set res [catch {eval exec $command} value]
     verbose -log "OUT $value"
     verbose -log "RC $res"
     return  $res
 }