Debian Codegraph @

testsuite/systemtap.onthefly/kprobes_onthefly.exp

raw
1 source $srcdir/$subdir/onthefly_common.tcl
2
3 set test "kprobes_onthefly"
4 if {![installtest_p] || ![kprobes_disabling_p]} {
5 untested "$test"
6 return
7 }
8
9 # Goal: Ensure that kprobes properly support on-the-fly
10 # arming/disarming. The script consists of a vfs_read() probe and a
11 # vfs_read() return probe whose conditions are toggled by a timer
12 # probe. We ensure that the kprobes will always be triggered by
13 # running 'echo' in the background. We then analyze the actual debug
14 # statements printed out to ensure the toggled probes are armed and
15 # disarmed as expected.
16 #
17 # PR17831: Note that originally this script probed 'vfs.read' and
18 # 'vfs.read.return', but this picked up inlined versions of vfs_read()
19 # on some kernels (like 3.10.0-229.el7.ppc64), which made the output
20 # different than we expected. So, we've switched to probing
21 # 'kernel.function("vfs_read").call' and
22 # 'kernel.function("vfs_read").return' which should return consistent
23 # results.
24
25 # Runs stap on a script with the given arguments
26 proc run_stap {subtest args script} {
27 global test
28
29 # Start the background echos
30 set echo_loop "while \[ true \]; do /bin/echo 1>/dev/null; done"
31 set echo_pid [exec sh -c $echo_loop &]
32
33 # We always need the DEBUG flag, and -u to avoid probe elision
34 set args "$args -DDEBUG_STP_ON_THE_FLY -u"
35
36 # The script we use uses @entry(), which uses maps underneath. Since we will
37 # be stressing the system, we need to ensure we will have enough space for
38 # many map items.
39 set args "$args -DMAXMAPENTRIES=100000"
40
41 # This test seems especially prone to exceeding the default overhead
42 # threshold in the harder subtests. Disable overload checking.
43 set args "$args -DSTP_NO_OVERLOAD"
44
45 # Run stap and catch any errors (we will rethrow it later)
46 set err [catch {eval exec stap -e {$script} $args} out]
47
48 # Kill echo
49 kill -INT $echo_pid 1
50
51 if {$err} {error $out}
52 return $out
53 }
54
55 # Returns a script with some variables replaced.
56 # SUBTEST name of subtest
57 # ENABLED set to 1 to start the target probe as enabled, 0 as disabled
58 # MAXTOGGLES number of times we toggle the target probe condition
59 # TIMER if a number, then it's the period of the timer probe which
60 # toggles the target probes, if set to 'profile' then instead a
61 # timer.profile probe is used to toggle the target probe, if set
62 # to 'hardcore' then both a timer.profile probe and a
63 # kernel.trace("*") probe is used for toggling, if set to 'max'
64 # then a lot of various probe points are used for toggling
65 proc make_script {SUBTEST ENABLED MAXTOGGLES TIMER} {
66 global test
67
68 set script {
69
70 # We want these probes to fire as deterministically as possible so that
71 # their outputs can easily be predicted and compared. Unfortunately this
72 # is complicated by multiple facts, including
73 # (1) the timer might be so fast that the probes don't have time to
74 # fire and print something,
75 # (2) the kprobe might print something after it is re-enabled, but
76 # before the kretprobe is re-enabled,
77 # (3) disabling a kretprobe won't stop an already running handler.
78 #
79 # To get around these issues, we use a simple state machine. The states
80 # are as follow:
81 #
82 # 0 = cond disabled
83 # 1 = cond enabled, but kprobe && kretprobe not yet enabled
84 # 2 = cond enabled, kprobe enabled, kretprobe not yet enabled
85 # 3 = cond enabled, kprobe && kretprobe enabled, nothing printed yet
86 # 4 = cond enabled, 'hit' printed but not 'rethit'
87 # 5 = cond enabled, 'rethit' printed
88
89 global state = ENABLED
90 global toggles = 0
91
92 probe kernel.function("vfs_read").call if (state > 0) {
93 if (state == 3) {
94 println("hit")
95 state++;
96 } else if (state == 1) {
97 state++;
98 }
99 }
100
101 probe kernel.function("vfs_read").return if (state > 0) {
102 # ensure that nothing changed during the vfs_read body
103 if (state != @entry(state))
104 next
105 if (state == 4) {
106 println("rethit")
107 state++;
108 } else if (state == 2) {
109 state++;
110 }
111 }
112
113 probe timer.us(TIMER) {
114 if (state != 0 && state != 5)
115 next # give probes more time to move through the states
116 toggles++
117 if (toggles > MAXTOGGLES)
118 exit()
119 else {
120 println("toggling")
121 state = !state
122 }
123 }
124
125 probe timer.s(360) {
126 println("timed out")
127 exit()
128 }
129 }
130
131 # On ppc64, you can't probe the hcall_* tracepoints (PR17126:
132 # tracepoints.exp testcase causing stalls/hang on ppc64). This was
133 # worked around by making a tracepoint blocklist. However, this
134 # test is using the '-g' (guru) argument to systemtap, which causes
135 # systemtap to ignore the tracepoint blocklist. So, on ppc64, we'll
136 # probe all the kernel tracepoints *except* for the ones starting
137 # with 'h'.
138 #
139 # XXX: Also skip lock:lock_* tracepoints due to issues on recent rawhide.
140 # TODOXXX: Recent ppc64 also has issues with one of the tracepoints under
141 # 'm'. Needs further investigation.
142 if {[string match ppc* $::tcl_platform(machine)]} {
143 set kernel_trace {kernel.trace("[a-gi-kn-z]*")}
144 } else {
145 set kernel_trace {kernel.trace("[a-km-z]*")}
146 }
147
148 # Set in values
149 set script [string map "TEST $test \
150 SUBTEST $SUBTEST \
151 ENABLED $ENABLED \
152 MAXTOGGLES $MAXTOGGLES" $script]
153 if {[string match $TIMER "profile"]} {
154 set script [string map "timer.us(TIMER) timer.profile" $script]
155 } elseif {[string match $TIMER "hardcore"]} {
156 set script [string map "timer.us(TIMER) \
157 {timer.profile, $kernel_trace}" $script]
158 } elseif {[string match $TIMER "max"]} {
159 set max_pps "begin, end, error, kernel.function(\"*@workqueue.c\"), \
160 process.begin, process.end, $kernel_trace"
161 if {[uprobes_p]} {
162 set max_pps "$max_pps, process(\"echo\").function(\"*\")"
163 }
164 set max_pps "$max_pps, \
165 netfilter.pf(\"NFPROTO_IPV4\").hook(\"NF_INET_LOCAL_IN\")?, \
166 netfilter.pf(\"NFPROTO_IPV4\").hook(\"NF_INET_LOCAL_OUT\")?, \
167 perf.sw.cpu_clock.sample(1000000)?, timer.profile.tick?"
168 set script [string map "timer.us(TIMER) {$max_pps}" $script]
169 } else {
170 set script [string map "TIMER $TIMER" $script]
171 }
172
173 return $script
174 }
175
176 # Creates the pattern which will be used by the is_valid_output proc to
177 # determine if the output is valid. In other words, given the initial value of
178 # enabled, the maximum number of toggles, and whether on-the-fly is turned on or
179 # not, this proc determines what the output of stap will be.
180 proc make_pattern {subtest start_enabled maxtoggles} {
181 set pattern ""
182
183 # If we're not starting as enabled, then the kprobes will be registered as
184 # disabled.
185 if {!$start_enabled} {
186 lappend pattern "* registering as disabled (k*probe) pidx ?"
187 lappend pattern "* registering as disabled (k*probe) pidx ?"
188 lappend pattern "* registering as disabled (k*probe) pidx ?"
189 }
190
191 # For each toggle:
192 # - if we were enabled, then we would have had 'hit' and 'rethit' lines
193 # - if i == maxtoggles, then we'll exit so nothing more will be printed
194 # - if i < maxtoggles, then the timer probe will output 'toggling' followed
195 # by 'disabling' or 'enabling' lines if on-the-fly is turned on
196 for {set i 0} {$i <= $maxtoggles} {incr i} {
197 if {$start_enabled} {
198 lappend pattern "hit" "rethit"
199 }
200 if {$i < $maxtoggles} {
201 lappend pattern "toggling"
202 if {$start_enabled} {
203 set start_enabled 0
204 lappend pattern "* disabling (k*probe) pidx ?"
205 lappend pattern "* disabling (k*probe) pidx ?"
206 lappend pattern "* disabling (k*probe) pidx ?"
207 } else {
208 set start_enabled 1
209 if {$i < $maxtoggles} {
210 lappend pattern "* enabling (k*probe) pidx ?"
211 lappend pattern "* enabling (k*probe) pidx ?"
212 lappend pattern "* enabling (k*probe) pidx ?"
213 }
214 }
215 }
216 }
217
218 return $pattern
219 }
220
221 # Various on-the-fly tests
222
223 # Toggle 0, 1, 2, 3, 4, and 5 times. This is important since we test if the
224 # probes finish well when disabled/enabled.
225
226 run_subtest_valid "otf_finish_at_start_disabled" 0 0 500000
227 run_subtest_valid "otf_finish_at_start_enabled" 1 0 500000
228 run_subtest_valid "otf_start_disabled_iter_1" 0 1 500000
229 run_subtest_valid "otf_start_enabled_iter_1" 1 1 500000
230 run_subtest_valid "otf_start_disabled_iter_2" 0 2 500000
231 run_subtest_valid "otf_start_enabled_iter_2" 1 2 500000
232 run_subtest_valid "otf_start_disabled_iter_3" 0 3 500000
233 run_subtest_valid "otf_start_enabled_iter_3" 1 3 500000
234 run_subtest_valid "otf_start_disabled_iter_4" 0 4 500000
235 run_subtest_valid "otf_start_enabled_iter_4" 1 4 500000
236 run_subtest_valid "otf_start_disabled_iter_5" 0 5 500000
237 run_subtest_valid "otf_start_enabled_iter_5" 1 5 500000
238
239 # Small interval tests
240
241 run_subtest_valid "otf_timer_100ms" 1 5 100000 \
242 -D STP_ON_THE_FLY_INTERVAL=1000000
243 run_subtest_valid "otf_timer_50ms" 1 5 50000 \
244 -D STP_ON_THE_FLY_INTERVAL=1000000
245 run_subtest_valid "otf_timer_10ms" 1 5 10000 \
246 -D STP_ON_THE_FLY_INTERVAL=1000000
247
248 # NB: if we go any shorter, the kernel might not have a chance to run
249 # systemtap_module_refresh() before the next toggling occurs, throwing off the
250 # expected output.
251
252 # Stress tests
253
254 # These tests are not validated, i.e. their outputs are not checked against a
255 # pattern (see NB above why). Instead, they're just meant to stress the
256 # on-the-fly mechanism to ensure stap/the kernel doesn't die. So if stap is
257 # successfully run and terminated, we just default to a pass.
258
259 run_subtest_stress "otf_stress_5ms_iter_50" 1 50 5000 \
260 -D STP_ON_THE_FLY_INTERVAL=100000
261 run_subtest_stress "otf_stress_1ms_iter_50" 1 50 1000 \
262 -D STP_ON_THE_FLY_INTERVAL=100000
263 run_subtest_stress "otf_stress_500us_iter_50" 1 50 500 \
264 -D STP_ON_THE_FLY_INTERVAL=100000
265 run_subtest_stress "otf_stress_100us_iter_50" 1 50 100 \
266 -D STP_ON_THE_FLY_INTERVAL=100000
267
268 run_subtest_stress "otf_stress_prof_iter_2000" 1 2000 profile \
269 -D STP_ON_THE_FLY_INTERVAL=100000
270
271 run_subtest_stress "otf_stress_hard_iter_2000" 1 2000 hardcore \
272 -D STP_ON_THE_FLY_INTERVAL=100000
273
274 run_subtest_stress "otf_stress_max_iter_5000" 1 5000 max \
275 -D STP_ON_THE_FLY_INTERVAL=100000