Debian Codegraph @

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

raw
1 set test "sysroot_sysenv"
2
3 if {![installtest_p]} { untested "$test"; return }
4
5 if {[utrace_p] && [uprobes_p]} {
6 spawn stap --sysroot=/ --sysenv=PATH=$env(PATH) $srcdir/$subdir/$test.stp ls -c "ls > /dev/null"
7 expect {
8 -timeout 180
9 -re {^process begin\r\n} { pass "$test" }
10 timeout { fail "$test (timeout)" }
11 eof { fail "$test" }
12 }
13 catch {close}
14 wait
15 } else {
16 untested "$test"
17 }
18
19 # Functions to handle creating, deleting, and using the fake sysroot
20 # directory we're creating. By using bind mounts, we're "moving" the
21 # /lib/modules and /usr/lib/debug directories of this system into our
22 # fake sysroot.
23
24 # Clean up the sysroot_sysenv namespace configuration.
25 proc server_ns_ns_cleanup {} {
26 global sysroot_base_dir new_modules_dir new_debug_dir
27
28 if {[info exists sysroot_base_dir]
29 && [string length $sysroot_base_dir]
30 && [file isdirectory $sysroot_base_dir]} {
31 catch {exec rm -rf $sysroot_base_dir}
32 unset sysroot_base_dir
33 }
34 if {[info exists new_modules_dir]
35 && [string length $new_modules_dir]
36 && [file isdirectory $new_modules_dir]} {
37 catch {exec rm -rf $new_modules_dir}
38 unset new_modules_dir
39 }
40 if {[info exists new_debug_dir]
41 && [string length $new_debug_dir]
42 && [file isdirectory $new_debug_dir]} {
43 catch {exec rm -rf $new_debug_dir}
44 unset new_debug_dir
45 }
46 }
47
48 # Set up the sysroot_sysenv namespace configuration.
49 proc sysroot_sysenv_ns_setup {} {
50 global sysroot_base_dir new_modules_dir new_debug_dir
51
52 # If we've already created the directories, just return.
53 if {[info exists sysroot_base_dir]
54 && [string length $sysroot_base_dir]
55 && [info exists new_modules_dir]
56 && [string length $new_modules_dir]
57 && [info exists new_debug_dir]
58 && [string length $new_debug_dir]} {
59 return 0
60 }
61
62 # Create the directories. We delete them in
63 # sysroot_sysenv_ns_cleanups().
64 if {[catch {exec mktemp -d} sysroot_base_dir]} {
65 verbose -log "sysroot_sysenv_setup - Failed to create sysroot directory: $sysroot_base_dir"
66 unset sysroot_base_dir
67 return 1
68 }
69 if {[catch {exec mktemp -d} new_modules_dir]} {
70 verbose -log "sysroot_sysenv_setup - Failed to create modules directory: $new_modules_dir"
71 unset new_modules_dir
72 sysroot_sysenv_ns_cleanup
73 return 1
74 }
75 if {[catch {exec mktemp -d} new_debug_dir]} {
76 verbose -log "sysroot_sysenv_setup - Failed to create debug directory: $new_debug_dir"
77 unset new_debug_dir
78 sysroot_sysenv_ns_cleanup
79 return 1
80 }
81
82 # OK, we've got our sysroot. Populate it with a couple of
83 # directories.
84 exec mkdir -p ${sysroot_base_dir}/lib/modules
85 exec mkdir -p ${sysroot_base_dir}/usr/lib/debug
86 exec mkdir -p ${sysroot_base_dir}/usr/src/kernels
87 exec mkdir -p ${sysroot_base_dir}/home
88 return 0
89 }
90
91 # sysroot_sysenv_ns_run: run 'command' in a custom mount
92 # namespace. Returns a list of [ RETURN_STATUS OUTPUT ].
93 proc sysroot_sysenv_ns_run { command } {
94 global sysroot_base_dir new_modules_dir new_debug_dir
95
96 if {[sysroot_sysenv_ns_setup]} { return [list 1 "server_ns_setup failure"] }
97
98 # We're going to call "unshare" to create a new mount
99 # namespace. Then we'll bind mount several host directories down
100 # into the fake sysroot directory, and bind mount empty
101 # directories on top of the host directories (to "replace" them).
102 #
103 # Note we're not "replacing" /usr/src/kernels, but leaving it on
104 # the host and putting in the sysroot. This is because there are
105 # symlinks to it from /usr/lib/debug.
106 set modules_dir "/lib/modules"
107 set debug_dir "/usr/lib/debug"
108 set src_dir "/usr/src/kernels"
109 set cmd {sudo unshare --mount bash -c\
110 "mount --bind ${modules_dir} ${sysroot_base_dir}${modules_dir}\
111 && mount --bind ${new_modules_dir} ${modules_dir}\
112 && mount --bind ${debug_dir} ${sysroot_base_dir}${debug_dir}\
113 && mount --bind ${new_debug_dir} ${debug_dir}\
114 && mount --bind ${src_dir} ${sysroot_base_dir}${src_dir}\
115 && $command"}
116 verbose -log "running: [subst $cmd]"
117 set rc 0
118 if {[catch {eval exec $cmd} output]} {
119 # non-zero exit status, get it:
120 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
121 set rc [lindex $::errorCode 2]
122 }
123 }
124 verbose -log "RC: $rc"
125 verbose -log "$output"
126
127 return [list $rc $output]
128 }
129
130 if {[sysroot_sysenv_ns_setup]} { fail "$test - namespace setup" }
131
132 # Make sure a basic kernel function probe works with our "sysroot".
133 set subtest "kernel function probe"
134 set script "probe kernel.function(\"sys_read\") { println(\"called\") }"
135 set stap_path $env(SYSTEMTAP_PATH)/stap
136 lassign [sysroot_sysenv_ns_run "${stap_path} -p4 --sysroot=${sysroot_base_dir} -e '$script'"] rc output
137 if {$rc == 0} {
138 pass "$test - $subtest"
139 } else {
140 fail "$test - $subtest"
141 }
142
143 # Compile a test executable.
144 set exepath "${sysroot_base_dir}/home/towers.x"
145 set res [target_compile $srcdir/$subdir/towers.c $exepath executable \
146 "additional_flags=-O2 additional_flags=-g"]
147 if { $res != "" } {
148 verbose "target_compile failed: $res" 2
149 fail "$test - compiling towers.c"
150 } else {
151 pass "$test compiling towers.c"
152 }
153
154 # Probe our test executable (listed with a full path relative to the
155 # sysroot).
156 set subtest "process function list"
157 set script "probe process(\"/home/towers.x\").function(\"*\") { println(ppfunc()) }"
158 lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p4 -e '$script'"] rc output
159 if {$rc == 0} {
160 pass "$test - $subtest"
161 } else {
162 fail "$test - $subtest"
163 }
164
165 # Probe our test executable (listed with a wildcard path relative to
166 # the sysroot).
167 set subtest "process wildcard function"
168 set script "probe process(\"/home/tow*\").function(\"*\") {println(ppfunc()) }"
169 lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir}\
170 -p4 -e '$script'"] rc output
171 if {$rc == 0} {
172 pass "$test - $subtest"
173 } else {
174 fail "$test - $subtest"
175 }
176
177 # Here we're not using an absolute path, so stap will search the
178 # path. We'll use the --sysenv option to put the sysroot's /home
179 # directory in PATH.
180 set subtest "process path search"
181 set script "probe process(\"towers.x\").function(\"*\") { println(ppfunc()) }"
182 lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir}\
183 --sysenv=PATH=/usr/bin:/bin:/usr/sbin:/sbin:/home -p4\
184 -e '$script'"] rc output
185 if {$rc == 0} {
186 pass "$test - $subtest"
187 } else {
188 fail "$test - $subtest"
189 }
190
191 # Make sure that all the paths in the generated module source are
192 # relative to the target sysroot - there should be no host paths
193 # present.
194 set subtest "process host path"
195 set script "probe process(\"/home/towers.x\").function(\"push\").call { printf(\"%s: %d %d\\n\", ppfunc(), \$i, \$s) }"
196 lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p3 -e '$script' | grep '/home/towers.x'"] rc output
197 if {$rc == 0} {
198 pass "$test - $subtest - output"
199 # We've got any mention of the target exe from the stap module C
200 # source in $output. Search it for any mention of the host path of
201 # the target executable ("${sysroot_base_dir}/home/towers.x"),
202 # instead of the target path ("/home/towers.x").
203 set matches 0
204 foreach line [split $output "\n"] {
205 if {[regexp {[^\"]/home/towers.x\"} $line]} {
206 incr matches
207 if {$matches == 1} {
208 verbose -log "the following line(s) have a host path:"
209 }
210 verbose -log "$line"
211 }
212 }
213 if {$matches == 0} {
214 pass "$test - $subtest - no host paths found"
215 } else {
216 fail "$test - $subtest - host paths found ($matches)"
217 }
218 } else {
219 fail "$test - $subtest - output"
220 }
221
222 # Now we're going to set up some symbolic links that are only valid
223 # inside the sysroot:
224 # /home/towers.lnk2 -> /home/towers.lnk -> /home/towers.x
225 set subtest "link creation"
226 lassign [sysroot_sysenv_ns_run "mount --bind ${sysroot_base_dir}/home /home\
227 && ln -s /home/towers.x /home/towers.lnk\
228 && ln -s /home/towers.lnk /home/towers.lnk2"] rc output
229 if {$rc == 0} {
230 pass "$test - $subtest"
231 } else {
232 fail "$test - $subtest"
233 }
234
235 # Make sure we can handle the symbolic link relative to the sysroot.
236 set subtest "symbolic link process function list"
237 set script "probe process(\"/home/towers.lnk\").function(\"*\") { println(ppfunc()) }"
238 lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p4 -e '$script'"] rc output
239 if {$rc == 0} {
240 pass "$test - $subtest"
241 } else {
242 fail "$test - $subtest"
243 }
244
245 # Make sure we can handle an indirect symbolic link relative to the
246 # sysroot.
247 set subtest "double symbolic link process function list"
248 set script "probe process(\"/home/towers.lnk2\").function(\"*\") { println(ppfunc()) }"
249 lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p4 -e '$script'"] rc output
250 if {$rc == 0} {
251 pass "$test - $subtest"
252 } else {
253 fail "$test - $subtest"
254 }
255
256 # Clean everything up.
257 server_ns_ns_cleanup