Debian Codegraph @

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

raw
1 # process exec tests.
2
3 # value gets set later
4 set test_exepath ""
5
6 # "load" generation function for stap_run. It spawns our test
7 # executable, which execs our copy of /bin/sleep.
8 proc run_test_exe {} {
9 global test_exepath
10 global exepath
11
12 spawn $test_exepath $exepath
13 set exe_id $spawn_id
14 wait_n_secs 5
15 catch {close -i $exe_id}
16 catch {wait -i $exe_id}
17 return 0;
18 }
19
20 # Set up our own copy of /bin/sleep, to make testing for a particular
21 # executable easy. We can't use 'ln' here, since we might be creating
22 # a cross-device link. We can't use 'ln -s' here, since the kernel
23 # resolves the symbolic link and reports that /bin/sleep is being
24 # exec'ed (instead of our local copy).
25 set exepath "[pwd]/sleep_[pid]"
26 if {[catch {exec cp /bin/sleep $exepath} res]} {
27 fail "unable to copy /bin/sleep: $res"
28 return
29 }
30
31 # Note use of '%1$s' - the 'N$' is the 'argpos', the argument position
32 # passed to 'format'.
33 set test_script {
34 global output
35 global sleep_start, sleep_end
36 probe begin { printf("systemtap starting probe\n") }
37 probe process("%1$s").begin {
38 output .= sprintf("exe(%%d) begin\n", tid())
39 }
40 probe process("%1$s").thread.begin {
41 output .= sprintf("exe(%%d) thread begin\n", tid())
42 }
43 probe process("%1$s").end {
44 output .= sprintf("exe(%%d) end\n", tid())
45 }
46 probe process("%1$s").thread.end {
47 output .= sprintf("exe(%%d) thread end\n", tid())
48 }
49 probe process("%2$s").begin {
50 sleep_start = gettimeofday_s()
51 output .= sprintf("sleep(%%d) begin\n", tid())
52 }
53 probe process("%2$s").end {
54 output .= sprintf("sleep(%%d) end\n", tid())
55 sleep_end = gettimeofday_s()
56 }
57 probe end {
58 printf("systemtap ending probe\n%%s", output)
59 time_slept = sleep_end - sleep_start
60 if (time_slept >= 2 && time_slept <= 4)
61 printf("time slept: OK\n")
62 else
63 printf("time slept: ERROR (%%d)\n", time_slept)
64 }
65 }
66
67 #######
68 #
69 # Test a normal fork/exec pair.
70 #
71
72 set fork_exec_srcpath "$srcdir/systemtap.base/proc_fork_exec.c"
73 set fork_exec_exepath "[pwd]/proc_fork_exec_[pid]"
74 set fork_exec_flags ""
75
76 # We make sure the outputs looks like the following using regexp
77 # subexpressions:
78 # exe(PARENT_PID) begin
79 # exe(CHILD_PID) begin
80 # exe(CHILD_PID) end
81 # sleep(CHILD_PID) begin
82 # sleep(CHILD_PID) end
83 # exe(PARENT_PID) end
84 # time slept: OK
85 #
86 # Accept the 'sleep(CHILD_PID)' end and 'exe(PARENT_PID) end' lines in
87 # any order.
88 set fork_exec_script_output "exe\\((\\d+)\\) begin\r\nexe\\((\\d+)\\) begin\r\nexe\\(\\2\\) end\r\nsleep\\(\\2\\) begin\r\n(sleep\\(\\2\\)|exe\\(\\1\\)) end\r\n(sleep\\(\\2\\)|exe\\(\\1\\)) end\r\ntime slept: OK\r\n"
89
90 # Compile our program that forks/exec's /bin/sleep.
91 set test_exepath $fork_exec_exepath
92 set res [target_compile $fork_exec_srcpath $fork_exec_exepath executable $fork_exec_flags]
93 if { $res != "" } {
94 verbose "target_compile failed: $res" 2
95 fail "unable to compile $fork_exec_srcpath"
96 return
97 }
98
99 set TEST_NAME "PROC_EXEC_01"
100 if {![utrace_p]} {
101 untested "$TEST_NAME : no kernel utrace support found"
102 } elseif {![installtest_p]} {
103 untested "$TEST_NAME"
104 } else {
105 set script [format $test_script $fork_exec_exepath $exepath]
106 stap_run $TEST_NAME run_test_exe $fork_exec_script_output -e $script
107 }
108
109 #######
110 #
111 # Test a straight exec (no fork).
112 #
113
114 set exec_srcpath "$srcdir/systemtap.base/proc_exec.c"
115 set exec_exepath "[pwd]/proc_exec_[pid]"
116 set exec_flags ""
117
118 # We make sure the outputs looks like the following using regexp
119 # subexpressions:
120 # exe(PID) begin
121 # exe(PID) end
122 # sleep(PID) begin
123 # sleep(PID) end
124 # time slept: OK
125 set exec_script_output "exe\\((\\d+)\\) begin\r\nexe\\(\\1\\) end\r\nsleep\\(\\1\\) begin\r\nsleep\\(\\1\\) end\r\ntime slept: OK\r\n"
126
127 # Compile our program that exec's /bin/sleep.
128 set test_exepath $exec_exepath
129 set res [target_compile $exec_srcpath $exec_exepath executable $exec_flags]
130 if { $res != "" } {
131 verbose "target_compile failed: $res" 2
132 fail "unable to compile $exec_srcpath"
133 return
134 }
135
136 set TEST_NAME "PROC_EXEC_02"
137 if {![utrace_p]} {
138 untested "$TEST_NAME : no kernel utrace support found"
139 } elseif {![installtest_p]} {
140 untested "$TEST_NAME"
141 } else {
142 set script [format $test_script $exec_exepath $exepath]
143 stap_run $TEST_NAME run_test_exe $exec_script_output -e $script
144 }
145
146 #######
147 #
148 # Test a multi-threaded exec, which programs aren't supposed to do,
149 # but...
150 #
151
152 set thread_exec_srcpath "$srcdir/systemtap.base/proc_thread_exec.c"
153 set thread_exec_exepath "[pwd]/proc_thread_exec_[pid]"
154 set thread_exec_flags "libs=-lpthread"
155
156 # We make sure the outputs looks like the following using regexp
157 # subexpressions.
158 # exe(PID1) begin
159 # exe(PID2) thread begin
160 # exe(PID1) thread end
161 # exe(PID1) end
162 # sleep(PID1) begin
163 # sleep(PID1) end
164 # time slept: OK
165 #
166 # Why no PID2 thread end and why isn't sleep using PID2? When the
167 # thread calls exec (which it isn't supposed to do), the kernel has to
168 # clean up the best it can. It really kills the original task,
169 # promotes the thread to be the process group leader (and changes the
170 # thread's pid to the original process group leader's pid).
171 set thread_exec_script_output "exe\\((\\d+)\\) begin\r\nexe\\(\\d+\\) thread begin\r\nexe\\(\\1\\)( thread)? end\r\nexe\\(\\1\\) end\r\nsleep\\(\\1\\) begin\r\nsleep\\(\\1\\) end\r\ntime slept: OK\r\n"
172
173 # Compile our multi-threaded program that exec's /bin/sleep.
174 set test_exepath $thread_exec_exepath
175 set res [target_compile $thread_exec_srcpath $thread_exec_exepath executable $thread_exec_flags]
176 if { $res != "" } {
177 verbose "target_compile failed: $res" 2
178 fail "unable to compile $exec_srcpath"
179 return
180 }
181
182 set TEST_NAME "PROC_EXEC_03"
183 if {![utrace_p]} {
184 untested "$TEST_NAME : no kernel utrace support found"
185 } elseif {![installtest_p]} {
186 untested "$TEST_NAME"
187 } else {
188 set script [format $test_script $thread_exec_exepath $exepath]
189 stap_run $TEST_NAME run_test_exe $thread_exec_script_output -e $script
190 }
191
192 # Cleanup
193 exec rm -f $exepath $fork_exec_exepath $exec_exepath $thread_exec_exepath