1 # Copyright 1997-2016 Free Software Foundation, Inc.
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.
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.
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/>.
17 # This program tests the 'catch syscall' functionality.
19 # It was written by Sergio Durigan Junior <sergiodj@linux.vnet.ibm.com>
24 if { [prepare_for_testing ${testfile}.exp $testfile ${testfile}.c] } {
25 untested catch-syscall.exp
29 # Check target supports catch syscall or not.
30 clean_restart $binfile
31 if ![runto_main] then {
32 fail "can't run to main"
36 set test "catch syscall"
37 gdb_test_multiple $test $test {
38 -re "The feature \'catch syscall\' is not supported.*\r\n$gdb_prompt $" {
39 unsupported "catch syscall isn't supported"
42 -re ".*$gdb_prompt $" {
47 set test "check catch syscall"
48 gdb_test_multiple "continue" $test {
49 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
50 unsupported "catch syscall isn't supported"
53 -re ".*Catchpoint.*$gdb_prompt $" {
58 # All (but the last) syscalls from the example code. It is filled in
59 # proc setup_all_syscalls.
61 set all_syscalls_numbers { }
63 # The last syscall (exit()) does not return, so
64 # we cannot expect the catchpoint to be triggered
65 # twice. It is a special case.
66 set last_syscall "exit_group"
67 set last_syscall_number { }
69 set vfork_syscalls "(vfork|clone2?)"
71 set unknown_syscall_number { }
73 # Internal procedure used to check if, after issuing a 'catch syscall'
74 # command (without arguments), the 'info breakpoints' command displays
75 # that '"any syscall"' is to be caught.
76 proc check_info_bp_any_syscall {} {
77 # Verifying that the catchpoint appears in the 'info breakpoints'
78 # command, but with "<any syscall>".
79 set thistest "catch syscall appears in 'info breakpoints'"
80 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest
83 # Internal procedure used to check if, after issuing a 'catch syscall X'
84 # command (with arguments), the 'info breakpoints' command displays
85 # that the syscall 'X' is to be caught.
86 proc check_info_bp_specific_syscall { syscall } {
87 set thistest "syscall(s) $syscall appears in 'info breakpoints'"
88 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
91 # Internal procedure used to check if, after issuing a 'catch syscall X'
92 # command (with many arguments), the 'info breakpoints' command displays
93 # that the syscalls 'X' are to be caught.
94 proc check_info_bp_many_syscalls { syscalls } {
97 foreach name $syscalls {
98 set filter_str "${filter_str}${name}, "
101 set filter_str [ string trimright $filter_str ", " ]
103 set thistest "syscalls $filter_str appears in 'info breakpoints'"
104 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
107 # This procedure checks if there was a call to a syscall. The optional
108 # pattern can match syscalls that vary in implementation, like vfork.
109 proc check_call_to_syscall { syscall { pattern "" } } {
112 if { $pattern eq "" } {
113 set pattern "${syscall}"
116 set thistest "program has called $syscall"
117 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${pattern}.?\\).*" $thistest
120 # This procedure checks if the syscall returned. The optional pattern
121 # can match syscalls that vary in implementation, like vfork.
122 proc check_return_from_syscall { syscall { pattern "" } } {
125 if { $pattern eq "" } {
126 set pattern "${syscall}"
129 set thistest "syscall $syscall has returned"
130 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" $thistest
133 # Internal procedure that performs two 'continue' commands and checks if
134 # a syscall call AND return occur. The optional pattern can match
135 # syscalls that vary in implementation, like vfork.
136 proc check_continue { syscall { pattern "" } } {
137 # Testing if the 'continue' stops at the
138 # specified syscall_name. If it does, then it should
139 # first print that the infeior has called the syscall,
140 # and after print that the syscall has returned.
142 # Testing if the inferior has called the syscall.
143 check_call_to_syscall $syscall $pattern
144 # And now, that the syscall has returned.
145 check_return_from_syscall $syscall $pattern
148 # Inserts a syscall catchpoint with an argument.
149 proc insert_catch_syscall_with_arg { syscall } {
152 # Trying to set the catchpoint
153 set thistest "catch syscall with arguments ($syscall)"
154 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
156 check_info_bp_specific_syscall $syscall
159 # Inserts a syscall catchpoint with many arguments.
160 proc insert_catch_syscall_with_many_args { syscalls numbers } {
163 set catch [ join $syscalls " " ]
166 foreach name $syscalls number $numbers {
167 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
170 set filter_str [ string trimright $filter_str " " ]
172 # Trying to set the catchpoint
173 set thistest "catch syscall with arguments ($filter_str)"
174 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
176 check_info_bp_many_syscalls $syscalls
179 proc check_for_program_end {} {
180 # Deleting the catchpoints
186 proc test_catch_syscall_without_args {} {
187 global all_syscalls last_syscall vfork_syscalls unknown_syscall_number decimal
189 with_test_prefix "without arguments" {
190 # Trying to set the syscall.
191 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
193 check_info_bp_any_syscall
195 # We have to check every syscall.
196 foreach name $all_syscalls {
200 check_continue "vfork" $vfork_syscalls
202 with_test_prefix "ENOSYS" {
203 check_continue $unknown_syscall_number
206 # At last but not least, we check if the inferior has called
207 # the last (exit) syscall.
208 check_call_to_syscall $last_syscall
210 # Now let's see if the inferior correctly finishes.
211 check_for_program_end
215 proc test_catch_syscall_with_args {} {
216 with_test_prefix "with arguments" {
217 set syscall_name "close"
218 insert_catch_syscall_with_arg $syscall_name
220 # Can we continue until we catch the syscall?
221 check_continue $syscall_name
223 # Now let's see if the inferior correctly finishes.
224 check_for_program_end
228 proc test_catch_syscall_with_many_args {} {
229 with_test_prefix "with many arguments" {
230 global all_syscalls all_syscalls_numbers
232 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
234 # Can we continue until we catch the syscalls?
235 foreach name $all_syscalls {
239 # Now let's see if the inferior correctly finishes.
240 check_for_program_end
244 proc test_catch_syscall_with_wrong_args {} {
245 with_test_prefix "wrong args" {
246 # mlock is not called from the source
247 set syscall_name "mlock"
248 insert_catch_syscall_with_arg $syscall_name
250 # Now, we must verify if the program stops with a continue.
251 # If it doesn't, everything is right (since we don't have
252 # a syscall named "mlock" in it). Otherwise, this is a failure.
253 set thistest "catch syscall with unused syscall ($syscall_name)"
254 gdb_continue_to_end $thistest
258 proc test_catch_syscall_restarting_inferior {} {
259 with_test_prefix "restarting inferior" {
260 set syscall_name "chroot"
262 with_test_prefix "entry" {
263 insert_catch_syscall_with_arg $syscall_name
265 # Let's first reach the entry of the syscall.
266 check_call_to_syscall $syscall_name
269 with_test_prefix "entry/return" {
270 # Now, restart the program.
273 # And check for entry/return.
274 check_continue $syscall_name
277 check_for_program_end
282 proc test_catch_syscall_skipping_return {} {
283 with_test_prefix "skipping return" {
284 with_test_prefix "entry" {
285 set syscall_name "write"
287 insert_catch_syscall_with_arg $syscall_name
289 # Let's first reach the entry of the syscall.
290 check_call_to_syscall $syscall_name
292 # Now purposely skip the syscall return.
294 gdb_test "stepi" ".*" "step over syscall return"
297 # With a naive entry/return toggle, gdb will still think
298 # the target is due for a syscall return.
300 with_test_prefix "entry/return" {
301 set syscall_name "read"
303 insert_catch_syscall_with_arg $syscall_name
305 # Check for entry first, then return.
306 check_continue $syscall_name
309 check_for_program_end
314 proc test_catch_syscall_mid_vfork {} {
315 global gdb_prompt decimal vfork_syscalls
317 with_test_prefix "mid-vfork" {
318 # Verify that the system supports "catch vfork".
319 gdb_test "catch vfork" "Catchpoint $decimal \\(vfork\\)" "insert first vfork catchpoint"
320 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
321 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
322 unsupported "continue to first vfork catchpoint"
325 -re ".*Catchpoint $decimal \\(vforked process $decimal\\).*$gdb_prompt $" {
326 pass "continue to first vfork catchpoint"
330 # Check that we now reach vfork return only.
331 # (The actual syscall used varies by architecture.)
332 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
333 check_return_from_syscall "vfork" $vfork_syscalls
336 check_for_program_end
340 proc test_catch_syscall_execve {} {
341 global gdb_prompt decimal
343 with_test_prefix "execve" {
345 # Tell the test program we want an execve.
346 gdb_test_no_output "set do_execve = 1"
348 # Check for entry/return across the execve, making sure that the
349 # syscall_state isn't lost when turning into a new process.
350 insert_catch_syscall_with_arg "execve"
351 check_continue "execve"
353 # Continue to main so extended-remote can read files as needed.
354 # (Otherwise that "Reading" output confuses gdb_continue_to_end.)
358 check_for_program_end
362 proc test_catch_syscall_fail_nodatadir {} {
363 with_test_prefix "fail no datadir" {
367 # Make sure GDB doesn't load the syscalls xml from the system
369 gdb_test "set data-directory /the/path/to/nowhere" \
370 "Warning: /the/path/to/nowhere: .*"
372 # Testing to see if we receive a warning when calling "catch
373 # syscall" without XML support (without datadir).
374 set thistest "catch syscall displays a warning when there is no XML support"
375 gdb_test "catch syscall" \
376 "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).*" \
379 # Since the catchpoint was set, we must check if it's present
380 # in "info breakpoints" output.
381 check_info_bp_any_syscall
388 proc test_catch_syscall_group {} {
391 set sysnum "\\\[${decimal}\\\]"
393 gdb_test "catch syscall g:process" \
394 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*" \
395 "set catchpoint on a group of syscalls"
397 gdb_test "catch syscall group:process read" \
398 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*read.*\\)" \
399 "set catchpoints on a group of syscalls and on a single syscall"
401 gdb_test "catch syscall group:" \
402 "Unknown syscall group ''\." \
403 "set catchpoints on an invalid group"
405 gdb_test "catch syscall g:junk" \
406 "Unknown syscall group 'junk'\." \
407 "set catchpoints on an unknown group."
409 gdb_test "complete catch syscall g:proc" \
410 "catch syscall g:process" \
411 "complete catch syscall group with 'g:' prefix"
413 gdb_test "complete catch syscall group:proc" \
414 "catch syscall group:process" \
415 "complete catch syscall group with 'group:' prefix"
417 gdb_test_sequence "complete catch syscall g" \
418 "complete catch syscall group suggests 'group:' prefix" {
419 "group:descriptor" "group:file" "group:ipc" "group:memory"
420 "group:network" "group:process" "group:signal"
424 proc do_syscall_tests {} {
425 # NOTE: We don't have to point gdb at the correct data-directory.
426 # For the build tree that is handled by INTERNAL_GDBFLAGS.
428 # Verify that the 'catch syscall' help is available
429 set thistest "help catch syscall"
430 gdb_test "help catch syscall" "Catch system calls.*" $thistest
432 # Try to set a catchpoint to a nonsense syscall
433 set thistest "catch syscall to a nonsense syscall is prohibited"
434 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
436 # Regression test for syscall completer bug.
437 gdb_test "complete catch syscall close chroo" \
438 "catch syscall close chroot" \
439 "complete catch syscall with multiple words"
441 # Testing the 'catch syscall' command without arguments.
442 # This test should catch any syscalls.
443 if [runto_main] then { test_catch_syscall_without_args }
445 # Testing the 'catch syscall' command with arguments.
446 # This test should only catch the specified syscall.
447 if [runto_main] then { test_catch_syscall_with_args }
449 # Testing the 'catch syscall' command with many arguments.
450 # This test should catch $all_syscalls.
451 if [runto_main] then { test_catch_syscall_with_many_args }
453 # Testing the 'catch syscall' command with WRONG arguments.
454 # This test should not trigger any catchpoints.
455 if [runto_main] then { test_catch_syscall_with_wrong_args }
457 # Testing the 'catch syscall' command during a restart of
459 if [runto_main] then { test_catch_syscall_restarting_inferior }
461 # Testing the 'catch syscall' command toggling off past a
462 # syscall return, then resuming entry/return as normal.
463 if [runto_main] then { test_catch_syscall_skipping_return }
465 # Testing the 'catch syscall' command starting mid-vfork.
466 if [runto_main] then { test_catch_syscall_mid_vfork }
468 # Testing that 'catch syscall' entry/return tracks across execve.
469 if [runto_main] then { test_catch_syscall_execve }
471 # Testing if the 'catch syscall' command works when switching to
472 # different architectures on-the-fly (PR gdb/10737).
473 if [runto_main] then { test_catch_syscall_multi_arch }
475 # Testing the 'catch' syscall command for a group of syscalls.
476 if [runto_main] then { test_catch_syscall_group }
479 proc test_catch_syscall_without_args_noxml {} {
480 with_test_prefix "without args noxml" {
481 # We will need the syscall names even not using it because we
482 # need to know know many syscalls are in the example file.
483 global decimal all_syscalls last_syscall_number unknown_syscall_number all_syscalls_numbers
487 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
489 # Now, we should be able to set a catchpoint, and GDB shall
490 # not display the warning anymore.
491 foreach name $all_syscalls number $all_syscalls_numbers {
492 with_test_prefix "$name" {
493 check_continue $number
497 check_continue "vfork" $decimal
499 with_test_prefix "ENOSYS" {
500 check_continue $unknown_syscall_number
503 # At last but not least, we check if the inferior has called
504 # the last (exit) syscall.
505 check_call_to_syscall $last_syscall_number
511 proc test_catch_syscall_with_args_noxml {} {
512 with_test_prefix "with args noxml" {
513 global all_syscalls_numbers
517 # Inserting all syscalls numbers to be caught
518 foreach syscall_number $all_syscalls_numbers {
519 insert_catch_syscall_with_arg $syscall_number
522 # Checking that all syscalls are caught.
523 foreach syscall_number $all_syscalls_numbers {
524 check_continue $syscall_number
531 proc test_catch_syscall_with_wrong_args_noxml {} {
532 with_test_prefix "with wrong args noxml" {
535 # Even without XML support, GDB should not accept unknown
536 # syscall names for the catchpoint.
537 gdb_test "catch syscall nonsense_syscall" \
538 "Unknown syscall name .nonsense_syscall.*"
544 proc test_catch_syscall_multi_arch {} {
545 global decimal binfile
547 if { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } {
549 set arch2 "i386:x86-64"
550 set syscall1_name "exit"
551 set syscall2_name "write"
553 } elseif { [istarget "powerpc-*-linux*"] \
554 || [istarget "powerpc64-*-linux*"] } {
555 set arch1 "powerpc:common"
556 set arch2 "powerpc:common64"
557 set syscall1_name "openat"
558 set syscall2_name "unlinkat"
559 set syscall_number 286
560 } elseif { [istarget "sparc-*-linux*"] \
561 || [istarget "sparc64-*-linux*"] } {
564 set syscall1_name "setresuid32"
565 set syscall2_name "setresuid"
566 set syscall_number 108
567 } elseif { [istarget "mips*-linux*"] } {
568 # MIPS does not use the same numbers for syscalls on 32 and 64
570 verbose "Not testing MIPS for multi-arch syscall support"
572 } elseif { [istarget "arm*-linux*"] } {
573 # catch syscall supports only 32-bit ARM for now.
574 verbose "Not testing ARM for multi-arch syscall support"
576 } elseif { [istarget "aarch64*-linux*"] } {
579 set syscall1_name "reboot"
580 set syscall2_name "_newselect"
581 set syscall_number 142
582 } elseif { [istarget "s390*-linux*"] } {
583 set arch1 "s390:31-bit"
584 set arch2 "s390:64-bit"
585 set syscall1_name "_newselect"
586 set syscall2_name "select"
587 set syscall_number 142
590 with_test_prefix "multiple targets" {
591 # We are not interested in loading any binary here, and in
592 # some systems (PowerPC, for example), if we load a binary
593 # there is no way to set other architecture.
597 gdb_test "set architecture $arch1" \
598 "The target architecture is assumed to be $arch1" \
601 gdb_test "catch syscall $syscall_number" \
602 "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \
603 "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1"
605 gdb_test "set architecture $arch2" \
606 "The target architecture is assumed to be $arch2" \
609 gdb_test "catch syscall $syscall_number" \
610 "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \
611 "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2"
613 clean_restart $binfile
617 proc do_syscall_tests_without_xml {} {
618 # Make sure GDB doesn't load the syscalls xml from the system data
620 gdb_test "set data-directory /the/path/to/nowhere" \
621 "Warning: /the/path/to/nowhere: .*"
623 # Let's test if we can catch syscalls without XML support.
624 # We should succeed, but GDB is not supposed to print syscall names.
625 if [runto_main] then { test_catch_syscall_without_args_noxml }
627 # The only valid argument "catch syscall" should accept is the
628 # syscall number, and not the name (since it can't translate a
630 if [runto_main] then { test_catch_syscall_with_args_noxml }
632 # Now, we'll try to provide a syscall name (valid or not) to the command,
633 # and expect it to fail.
634 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
637 # This procedure fills the vector "all_syscalls_numbers" with the proper
638 # numbers for the used syscalls according to the architecture.
639 proc fill_all_syscalls_numbers {} {
640 global all_syscalls_numbers last_syscall_number unknown_syscall_number all_syscalls
642 foreach syscall $all_syscalls {
643 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
646 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
647 set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1]
650 # Set up the vector all_syscalls.
652 proc setup_all_syscalls {} {
656 # They are ordered according to the file, so do not change this.
657 lappend all_syscalls "close"
658 lappend all_syscalls "chroot"
660 # SYS_pipe doesn't exist on aarch64 kernel.
661 set test "check SYS_pipe"
662 gdb_test_multiple "p pipe_syscall" $test {
663 -re " = .*$gdb_prompt $" {
665 lappend all_syscalls "pipe"
667 -re "No symbol .*$gdb_prompt $" {
669 # SYS_pipe isn't defined, use SYS_pipe2 instead.
670 lappend all_syscalls "pipe2"
674 lappend all_syscalls "write"
675 lappend all_syscalls "read"
680 # Fill all the syscalls numbers before starting anything.
681 fill_all_syscalls_numbers
683 # Execute the tests, using XML support
685 if { ![gdb_skip_xml_test] } {
686 clean_restart $binfile
689 # Now, we have to see if GDB displays a warning when we
690 # don't set the data-directory but try to use catch syscall
691 # anyway. For that, we must restart GDB first.
692 clean_restart $binfile
693 test_catch_syscall_fail_nodatadir
697 clean_restart $binfile
699 # Execute the tests, without XML support. In this case, GDB will
700 # only display syscall numbers, and not syscall names.
701 do_syscall_tests_without_xml