Debian Codegraph @

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

raw
1 set test "modules_out_of_tree"
2 set am_root [expr 0 == [exec id -u]]
3
4 # In this test, we verify support for out-of-tree kernel kernel modules.
5 # We do this by first compiling our very own kernel module and then
6 # listing and probing it.
7
8 # Create the temporary directory.
9 #
10 # Notice we're using a '-' in the directory name. This will help test
11 # the problem noted in PR19265 comment #1, where systemtap can't find
12 # out-of-tree modules when the directory has a '-' in the name.
13 if {[catch {exec mktemp -d -t staptest-XXXXXX} tmpdir]} {
14 verbose -log "Failed to create temporary directory: $tmpdir"
15 return
16 }
17
18 # Save the current directory and switch to our temporary directory.
19 set curdir [pwd]
20 cd ${tmpdir}
21
22 proc test_cleanup {} {
23 global curdir
24 global verbose
25 global tmpdir
26
27 # Restore current directory
28 cd ${curdir}
29
30 # Cleanup (if we're not verbose)
31 if { $verbose == 0 } { catch { exec rm -f ${tmpdir} } }
32 }
33
34 proc write_to_file {file data} {
35 global test
36
37 set data [string map "TEST $test" $data]
38 set file [open "$file" w]
39 puts $file $data
40 close $file
41 }
42
43 write_to_file "stap_$test.c" {
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47
48 MODULE_LICENSE("GPL");
49
50 static int __init stap_TEST_init(void) {
51 return 0;
52 }
53
54 static void __exit stap_TEST_cleanup(void) {
55 return;
56 }
57
58 module_init(stap_TEST_init);
59 module_exit(stap_TEST_cleanup);
60 }
61
62 write_to_file "Makefile" {
63 obj-m := stap_TEST.o
64 default:
65 make -C /lib/modules/$(shell uname -r)/build M=$$PWD
66 }
67
68 set modname "stap_${test}.ko"
69 set modpath "${tmpdir}/${modname}"
70
71 set subtest "compilation"
72 if {[catch {exec make} err] || ![file exists ${modpath}]} {
73 fail "$test ($subtest)"
74 test_cleanup
75 return
76 }
77 pass "$test ($subtest)"
78
79 set pp "module(\"${modpath}\").function(\"stap_${test}_init\")"
80 set reg "^module\\\(\"${modpath}\"\\\)"
81 set reg "$reg\\\.function\\\(\"stap_${test}_init@${tmpdir}/stap_${test}.c:8\"\\\)$"
82
83 # Let's try listing a function first
84 set subtest "listing"
85 if {[catch {exec stap -l $pp} out] || ![regexp $reg $out]} {
86 fail "$test ($subtest)"
87 test_cleanup
88 return
89 }
90 pass "$test ($subtest)"
91
92 # OK now let's actually go for a run
93 set subtest "probing"
94 if {![installtest_p] || !$am_root || ![module_refresh_p]} {
95 untested "$test ($subtest)"
96 } else {
97 set script_template "probe %s { println(\"hit\") }"
98 set command "insmod ${modpath} && rmmod ${modpath}"
99
100 # Spawn stap
101 set failed 1
102 set script [format $script_template $pp]
103 spawn stap -e $script -c $command
104 expect {
105 -timeout 30
106 hit {
107 set failed 0
108 }
109 timeout {
110 verbose -log "$test timed out"
111 kill -INT -[exp_pid] 2
112 }
113 }
114 catch {close}; catch {wait}
115
116 if {$failed} {
117 fail "$test ($subtest)"
118 } else {
119 pass "$test ($subtest)"
120 }
121 }
122
123 # Now let's try the same thing, but with dwarfless-probing.
124 set subtest "dwarfless probing"
125 set pp "kprobe.module(\"${modpath}\").function(\"stap_${test}_init\")"
126 if {![installtest_p] || !$am_root || ![module_refresh_p]} {
127 untested "$test ($subtest)"
128 } else {
129 # Spawn stap
130 set failed 1
131 set script [format $script_template $pp]
132 spawn stap -e $script -c $command
133 expect {
134 -timeout 30
135 hit {
136 set failed 0
137 }
138 timeout {
139 verbose -log "$test timed out"
140 kill -INT -[exp_pid] 2
141 }
142 }
143 catch {close}; catch {wait}
144
145 if {$failed} {
146 fail "$test ($subtest)"
147 } else {
148 pass "$test ($subtest)"
149 }
150 }
151
152 # Now let's try with a relative path (instead of an absolute path).
153 set subtest "relative-path probing"
154 set pp "module(\"./${modname}\").function(\"stap_${test}_init\")"
155 if {![installtest_p] || !$am_root || ![module_refresh_p]} {
156 untested "$test ($subtest)"
157 } else {
158 # Spawn stap
159 set failed 1
160 set script [format $script_template $pp]
161 spawn stap -e $script -c $command
162 expect {
163 -timeout 30
164 hit {
165 set failed 0
166 }
167 timeout {
168 verbose -log "$test timed out"
169 kill -INT -[exp_pid] 2
170 }
171 }
172 catch {close}; catch {wait}
173
174 if {$failed} {
175 fail "$test ($subtest)"
176 } else {
177 pass "$test ($subtest)"
178 }
179 }
180
181 # Now let's try dwarfless probing with a relative path (instead of an
182 # absolute path).
183 set subtest "dwarfless relative-path probing"
184 set pp "kprobe.module(\"./${modname}\").function(\"stap_${test}_init\")"
185 if {![installtest_p] || !$am_root || ![module_refresh_p]} {
186 untested "$test ($subtest)"
187 } else {
188 # Spawn stap
189 set failed 1
190 set script [format $script_template $pp]
191 spawn stap -e $script -c $command
192 expect {
193 -timeout 30
194 hit {
195 set failed 0
196 }
197 timeout {
198 verbose -log "$test timed out"
199 kill -INT -[exp_pid] 2
200 }
201 }
202 catch {close}; catch {wait}
203
204 if {$failed} {
205 fail "$test ($subtest)"
206 } else {
207 pass "$test ($subtest)"
208 }
209 }
210
211 test_cleanup