gdb/ChangeLog:
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / catch-syscall.exp
1 # Copyright 1997-2014 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 # This program tests the 'catch syscall' functionality.
18 #
19 # It was written by Sergio Durigan Junior <sergiodj@linux.vnet.ibm.com>
20 # on September/2008.
21
22 if { [is_remote target] || ![isnative] } then {
23 continue
24 }
25
26 # Until "catch syscall" is implemented on other targets...
27 if { ![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"] } {
28 continue
29 }
30
31 # This shall be updated whenever 'catch syscall' is implemented
32 # on some architecture.
33 #if { ![istarget "i\[34567\]86-*-linux*"]
34 if { ![istarget "x86_64-*-linux*"] && ![istarget "i\[34567\]86-*-linux*"]
35 && ![istarget "powerpc-*-linux*"] && ![istarget "powerpc64-*-linux*"]
36 && ![istarget "sparc-*-linux*"] && ![istarget "sparc64-*-linux*"]
37 && ![istarget "mips*-linux*"] && ![istarget "arm*-linux*"]
38 && ![istarget "s390*-linux*"] } {
39 continue
40 }
41
42 standard_testfile
43
44 if { [prepare_for_testing ${testfile}.exp $testfile ${testfile}.c] } {
45 untested catch-syscall.exp
46 return -1
47 }
48
49 # All (but the last) syscalls from the example code
50 # They are ordered according to the file, so do not change this.
51 set all_syscalls { "close" "chroot" "pipe" "write" "read" }
52 set all_syscalls_numbers { }
53
54 # The last syscall (exit()) does not return, so
55 # we cannot expect the catchpoint to be triggered
56 # twice. It is a special case.
57 set last_syscall "exit_group"
58 set last_syscall_number { }
59
60 # Internal procedure used to check if, after issuing a 'catch syscall'
61 # command (without arguments), the 'info breakpoints' command displays
62 # that '"any syscall"' is to be caught.
63 proc check_info_bp_any_syscall {} {
64 # Verifying that the catchpoint appears in the 'info breakpoints'
65 # command, but with "<any syscall>".
66 set thistest "catch syscall appears in 'info breakpoints'"
67 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest
68 }
69
70 # Internal procedure used to check if, after issuing a 'catch syscall X'
71 # command (with arguments), the 'info breakpoints' command displays
72 # that the syscall 'X' is to be caught.
73 proc check_info_bp_specific_syscall { syscall } {
74 set thistest "syscall(s) $syscall appears in 'info breakpoints'"
75 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
76 }
77
78 # Internal procedure used to check if, after issuing a 'catch syscall X'
79 # command (with many arguments), the 'info breakpoints' command displays
80 # that the syscalls 'X' are to be caught.
81 proc check_info_bp_many_syscalls { syscalls } {
82 set filter_str ""
83
84 foreach name $syscalls {
85 set filter_str "${filter_str}${name}, "
86 }
87
88 set filter_str [ string trimright $filter_str ", " ]
89
90 set thistest "syscalls $filter_str appears in 'info breakpoints'"
91 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
92 }
93
94 # This procedure checks if there was a call to a syscall.
95 proc check_call_to_syscall { syscall } {
96 global decimal
97
98 set thistest "program has called $syscall"
99 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${syscall}.?\\).*" $thistest
100 }
101
102 # This procedure checks if the syscall returned.
103 proc check_return_from_syscall { syscall } {
104 global decimal
105
106 set thistest "syscall $syscall has returned"
107 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${syscall}\\).*" $thistest
108 }
109
110 # Internal procedure that performs two 'continue' commands and checks if
111 # a syscall call AND return occur.
112 proc check_continue { syscall } {
113 # Testing if the 'continue' stops at the
114 # specified syscall_name. If it does, then it should
115 # first print that the infeior has called the syscall,
116 # and after print that the syscall has returned.
117
118 # Testing if the inferiorr has called the syscall.
119 check_call_to_syscall $syscall
120 # And now, that the syscall has returned.
121 check_return_from_syscall $syscall
122 }
123
124 # Inserts a syscall catchpoint with an argument.
125 proc insert_catch_syscall_with_arg { syscall } {
126 global decimal
127
128 # Trying to set the catchpoint
129 set thistest "catch syscall with arguments ($syscall)"
130 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
131
132 check_info_bp_specific_syscall $syscall
133 }
134
135 # Inserts a syscall catchpoint with many arguments.
136 proc insert_catch_syscall_with_many_args { syscalls numbers } {
137 global decimal
138
139 set catch [ join $syscalls " " ]
140 set filter_str ""
141
142 foreach name $syscalls number $numbers {
143 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
144 }
145
146 set filter_str [ string trimright $filter_str " " ]
147
148 # Trying to set the catchpoint
149 set thistest "catch syscall with arguments ($filter_str)"
150 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
151
152 check_info_bp_many_syscalls $syscalls
153 }
154
155 proc check_for_program_end {} {
156 # Deleting the catchpoints
157 delete_breakpoints
158
159 gdb_continue_to_end
160 }
161
162 proc test_catch_syscall_without_args {} {
163 global all_syscalls last_syscall decimal
164
165 with_test_prefix "without arguments" {
166 # Trying to set the syscall.
167 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
168
169 check_info_bp_any_syscall
170
171 # We have to check every syscall.
172 foreach name $all_syscalls {
173 check_continue $name
174 }
175
176 # At last but not least, we check if the inferior has called
177 # the last (exit) syscall.
178 check_call_to_syscall $last_syscall
179
180 # Now let's see if the inferior correctly finishes.
181 check_for_program_end
182 }
183 }
184
185 proc test_catch_syscall_with_args {} {
186 with_test_prefix "with arguments" {
187 set syscall_name "close"
188 insert_catch_syscall_with_arg $syscall_name
189
190 # Can we continue until we catch the syscall?
191 check_continue $syscall_name
192
193 # Now let's see if the inferior correctly finishes.
194 check_for_program_end
195 }
196 }
197
198 proc test_catch_syscall_with_many_args {} {
199 with_test_prefix "with many arguments" {
200 global all_syscalls all_syscalls_numbers
201
202 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
203
204 # Can we continue until we catch the syscalls?
205 foreach name $all_syscalls {
206 check_continue $name
207 }
208
209 # Now let's see if the inferior correctly finishes.
210 check_for_program_end
211 }
212 }
213
214 proc test_catch_syscall_with_wrong_args {} {
215 with_test_prefix "wrong args" {
216 # mlock is not called from the source
217 set syscall_name "mlock"
218 insert_catch_syscall_with_arg $syscall_name
219
220 # Now, we must verify if the program stops with a continue.
221 # If it doesn't, everything is right (since we don't have
222 # a syscall named "mlock" in it). Otherwise, this is a failure.
223 set thistest "catch syscall with unused syscall ($syscall_name)"
224 gdb_continue_to_end $thistest
225 }
226 }
227
228 proc test_catch_syscall_restarting_inferior {} {
229 with_test_prefix "restarting inferior" {
230 set syscall_name "chroot"
231
232 with_test_prefix "entry" {
233 insert_catch_syscall_with_arg $syscall_name
234
235 # Let's first reach the entry of the syscall.
236 check_call_to_syscall $syscall_name
237 }
238
239 with_test_prefix "entry/return" {
240 # Now, restart the program.
241 rerun_to_main
242
243 # And check for entry/return.
244 check_continue $syscall_name
245
246 # Can we finish?
247 check_for_program_end
248 }
249 }
250 }
251
252 proc test_catch_syscall_fail_nodatadir {} {
253 with_test_prefix "fail no datadir" {
254 # Sanitizing.
255 delete_breakpoints
256
257 # Make sure GDB doesn't load the syscalls xml from the system
258 # data directory.
259 gdb_test_no_output "set data-directory /the/path/to/nowhere"
260
261 # Testing to see if we receive a warning when calling "catch
262 # syscall" without XML support (without datadir).
263 set thistest "catch syscall displays a warning when there is no XML support"
264 gdb_test "catch syscall" \
265 "warning: Could not load the syscall XML file.*warning: GDB will not be able to display syscall names nor to verify if.*any provided syscall numbers are valid.*Catchpoint .*(syscall).*" \
266 $thistest
267
268 # Since the catchpoint was set, we must check if it's present
269 # in "info breakpoints" output.
270 check_info_bp_any_syscall
271
272 # Sanitizing.
273 delete_breakpoints
274 }
275 }
276
277 proc do_syscall_tests {} {
278 # NOTE: We don't have to point gdb at the correct data-directory.
279 # For the build tree that is handled by INTERNAL_GDBFLAGS.
280
281 # Verify that the 'catch syscall' help is available
282 set thistest "help catch syscall"
283 gdb_test "help catch syscall" "Catch system calls.*" $thistest
284
285 # Try to set a catchpoint to a nonsense syscall
286 set thistest "catch syscall to a nonsense syscall is prohibited"
287 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
288
289 # Regression test for syscall completer bug.
290 gdb_test "complete catch syscall close chroo" \
291 "catch syscall close chroot" \
292 "complete catch syscall with multiple words"
293
294 # Testing the 'catch syscall' command without arguments.
295 # This test should catch any syscalls.
296 if [runto_main] then { test_catch_syscall_without_args }
297
298 # Testing the 'catch syscall' command with arguments.
299 # This test should only catch the specified syscall.
300 if [runto_main] then { test_catch_syscall_with_args }
301
302 # Testing the 'catch syscall' command with many arguments.
303 # This test should catch $all_syscalls.
304 if [runto_main] then { test_catch_syscall_with_many_args }
305
306 # Testing the 'catch syscall' command with WRONG arguments.
307 # This test should not trigger any catchpoints.
308 if [runto_main] then { test_catch_syscall_with_wrong_args }
309
310 # Testing the 'catch' syscall command during a restart of
311 # the inferior.
312 if [runto_main] then { test_catch_syscall_restarting_inferior }
313 }
314
315 proc test_catch_syscall_without_args_noxml {} {
316 with_test_prefix "without args noxml" {
317 # We will need the syscall names even not using it because we
318 # need to know know many syscalls are in the example file.
319 global all_syscalls last_syscall_number all_syscalls_numbers
320
321 delete_breakpoints
322
323 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
324
325 # Now, we should be able to set a catchpoint, and GDB shall
326 # not display the warning anymore.
327 foreach name $all_syscalls number $all_syscalls_numbers {
328 with_test_prefix "$name" {
329 check_continue $number
330 }
331 }
332
333 # At last but not least, we check if the inferior has called
334 # the last (exit) syscall.
335 check_call_to_syscall $last_syscall_number
336
337 delete_breakpoints
338 }
339 }
340
341 proc test_catch_syscall_with_args_noxml {} {
342 with_test_prefix "with args noxml" {
343 global all_syscalls_numbers
344
345 delete_breakpoints
346
347 # Inserting all syscalls numbers to be caught
348 foreach syscall_number $all_syscalls_numbers {
349 insert_catch_syscall_with_arg $syscall_number
350 }
351
352 # Checking that all syscalls are caught.
353 foreach syscall_number $all_syscalls_numbers {
354 check_continue $syscall_number
355 }
356
357 delete_breakpoints
358 }
359 }
360
361 proc test_catch_syscall_with_wrong_args_noxml {} {
362 with_test_prefix "with wrong args noxml" {
363 delete_breakpoints
364
365 # Even without XML support, GDB should not accept unknown
366 # syscall names for the catchpoint.
367 gdb_test "catch syscall nonsense_syscall" \
368 "Unknown syscall name .nonsense_syscall.*"
369
370 delete_breakpoints
371 }
372 }
373
374 proc do_syscall_tests_without_xml {} {
375 # Make sure GDB doesn't load the syscalls xml from the system data
376 # directory.
377 gdb_test_no_output "set data-directory /the/path/to/nowhere"
378
379 # Let's test if we can catch syscalls without XML support.
380 # We should succeed, but GDB is not supposed to print syscall names.
381 if [runto_main] then { test_catch_syscall_without_args_noxml }
382
383 # The only valid argument "catch syscall" should accept is the
384 # syscall number, and not the name (since it can't translate a
385 # name to a number).
386 if [runto_main] then { test_catch_syscall_with_args_noxml }
387
388 # Now, we'll try to provide a syscall name (valid or not) to the command,
389 # and expect it to fail.
390 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
391 }
392
393 # This procedure fills the vector "all_syscalls_numbers" with the proper
394 # numbers for the used syscalls according to the architecture.
395 proc fill_all_syscalls_numbers {} {
396 global all_syscalls_numbers last_syscall_number all_syscalls
397
398 foreach syscall $all_syscalls {
399 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
400 }
401
402 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
403 }
404
405 # Fill all the syscalls numbers before starting anything.
406 fill_all_syscalls_numbers
407
408 # Execute the tests, using XML support
409 if { ![gdb_skip_xml_test] } {
410 clean_restart $binfile
411 do_syscall_tests
412
413 # Now, we have to see if GDB displays a warning when we
414 # don't set the data-directory but try to use catch syscall
415 # anyway. For that, we must restart GDB first.
416 clean_restart $binfile
417 test_catch_syscall_fail_nodatadir
418 }
419
420 # Restart gdb
421 clean_restart $binfile
422
423 # Execute the tests, without XML support. In this case, GDB will
424 # only display syscall numbers, and not syscall names.
425 do_syscall_tests_without_xml
This page took 0.078287 seconds and 5 git commands to generate.