1 # Copyright 1997-2023 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 "failed to prepare" $testfile ${testfile}.c] } {
28 # Check target supports catch syscall or not.
33 set test "catch syscall"
34 gdb_test_multiple $test $test {
35 -re "The feature \'catch syscall\' is not supported.*\r\n$gdb_prompt $" {
36 unsupported "catch syscall isn't supported"
39 -re ".*$gdb_prompt $" {
44 set test "check catch syscall"
45 gdb_test_multiple "continue" $test {
46 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
47 unsupported "catch syscall isn't supported"
50 -re ".*Catchpoint.*$gdb_prompt $" {
55 # Test-case for PR27313. Verify that negative syscall numbers are refused.
56 gdb_test "catch syscall -1" "Unknown syscall number '-1'\\."
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 if { $pattern eq "execve" } {
131 gdb_test_multiple "continue" $thistest {
132 -re -wrap "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" {
136 -re -wrap ".*Breakpoint $decimal, main .*" {
137 # On Powerpc the kernel does not report the returned from
138 # syscall as expected by the test. GDB bugzilla 28623.
139 if { [istarget "powerpc64*-linux*"] } {
149 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" $thistest
154 # Internal procedure that performs two 'continue' commands and checks if
155 # a syscall call AND return occur. The optional pattern can match
156 # syscalls that vary in implementation, like vfork.
157 proc check_continue { syscall { pattern "" } } {
158 # Testing if the 'continue' stops at the
159 # specified syscall_name. If it does, then it should
160 # first print that the infeior has called the syscall,
161 # and after print that the syscall has returned.
163 # Testing if the inferior has called the syscall.
164 check_call_to_syscall $syscall $pattern
165 # And now, that the syscall has returned.
166 return [check_return_from_syscall $syscall $pattern]
169 # Inserts a syscall catchpoint with an argument.
170 proc insert_catch_syscall_with_arg { syscall } {
173 # Trying to set the catchpoint
174 set thistest "catch syscall with arguments ($syscall)"
175 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
177 check_info_bp_specific_syscall $syscall
180 # Inserts a syscall catchpoint with many arguments.
181 proc insert_catch_syscall_with_many_args { syscalls numbers } {
184 set catch [ join $syscalls " " ]
187 foreach name $syscalls number $numbers {
188 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
191 set filter_str [ string trimright $filter_str " " ]
193 # Trying to set the catchpoint
194 set thistest "catch syscall with arguments ($filter_str)"
195 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
197 check_info_bp_many_syscalls $syscalls
200 proc check_for_program_end {} {
201 # Deleting the catchpoints
204 gdb_continue_to_end "" continue 1
207 proc test_catch_syscall_without_args {} {
208 global all_syscalls last_syscall vfork_syscalls unknown_syscall_number decimal
210 with_test_prefix "without arguments" {
211 # Trying to set the syscall.
212 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
214 check_info_bp_any_syscall
216 # We have to check every syscall.
217 foreach name $all_syscalls {
221 check_continue "vfork" $vfork_syscalls
223 with_test_prefix "ENOSYS" {
224 check_continue $unknown_syscall_number
227 # At last but not least, we check if the inferior has called
228 # the last (exit) syscall.
229 check_call_to_syscall $last_syscall
231 # Now let's see if the inferior correctly finishes.
232 check_for_program_end
236 proc test_catch_syscall_with_args {} {
237 with_test_prefix "with arguments" {
238 set syscall_name "close"
239 insert_catch_syscall_with_arg $syscall_name
241 # Can we continue until we catch the syscall?
242 check_continue $syscall_name
244 # Now let's see if the inferior correctly finishes.
245 check_for_program_end
249 proc test_catch_syscall_with_many_args {} {
250 with_test_prefix "with many arguments" {
251 global all_syscalls all_syscalls_numbers
253 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
255 # Can we continue until we catch the syscalls?
256 foreach name $all_syscalls {
260 # Now let's see if the inferior correctly finishes.
261 check_for_program_end
265 proc test_catch_syscall_with_wrong_args {} {
266 with_test_prefix "wrong args" {
267 # mlock is not called from the source
268 set syscall_name "mlock"
269 insert_catch_syscall_with_arg $syscall_name
271 # Now, we must verify if the program stops with a continue.
272 # If it doesn't, everything is right (since we don't have
273 # a syscall named "mlock" in it). Otherwise, this is a failure.
274 set thistest "catch syscall with unused syscall ($syscall_name)"
275 gdb_continue_to_end $thistest continue 1
279 proc test_catch_syscall_restarting_inferior {} {
280 with_test_prefix "restarting inferior" {
281 set syscall_name "chroot"
283 with_test_prefix "entry" {
284 insert_catch_syscall_with_arg $syscall_name
286 # Let's first reach the entry of the syscall.
287 check_call_to_syscall $syscall_name
290 with_test_prefix "entry/return" {
291 # Now, restart the program.
294 # And check for entry/return.
295 check_continue $syscall_name
298 check_for_program_end
303 proc test_catch_syscall_skipping_return {} {
304 with_test_prefix "skipping return" {
305 with_test_prefix "entry" {
306 set syscall_name "write"
308 insert_catch_syscall_with_arg $syscall_name
310 # Let's first reach the entry of the syscall.
311 check_call_to_syscall $syscall_name
313 # Now purposely skip the syscall return.
315 gdb_test "stepi" ".*" "step over syscall return"
318 # With a naive entry/return toggle, gdb will still think
319 # the target is due for a syscall return.
321 with_test_prefix "entry/return" {
322 set syscall_name "read"
324 insert_catch_syscall_with_arg $syscall_name
326 # Check for entry first, then return.
327 check_continue $syscall_name
330 check_for_program_end
335 proc test_catch_syscall_mid_vfork {} {
336 global gdb_prompt decimal vfork_syscalls
338 with_test_prefix "mid-vfork" {
339 # Verify that the system supports "catch vfork".
340 gdb_test "catch vfork" "Catchpoint $decimal \\(vfork\\)" "insert first vfork catchpoint"
341 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
342 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
343 unsupported "continue to first vfork catchpoint"
346 -re ".*Catchpoint $decimal \\(vforked process $decimal\\).*$gdb_prompt $" {
347 pass "continue to first vfork catchpoint"
351 # Check that we now reach vfork return only.
352 # (The actual syscall used varies by architecture.)
353 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
354 check_return_from_syscall "vfork" $vfork_syscalls
357 check_for_program_end
361 proc test_catch_syscall_execve {} {
362 global gdb_prompt decimal
364 with_test_prefix "execve" {
366 # Tell the test program we want an execve.
367 gdb_test_no_output "set do_execve = 1"
369 # Check for entry/return across the execve, making sure that the
370 # syscall_state isn't lost when turning into a new process.
371 insert_catch_syscall_with_arg "execve"
372 if [check_continue "execve"] {
373 # The check_continue test generates an XFAIL on Powerpc. In
374 # that case, gdb is already at main so don't do the continue.
377 # Continue to main so extended-remote can read files as needed.
378 # (Otherwise that "Reading" output confuses gdb_continue_to_end.)
383 check_for_program_end
387 proc test_catch_syscall_fail_nodatadir {} {
388 with_test_prefix "fail no datadir" {
392 # Make sure GDB doesn't load the syscalls xml from the system
394 gdb_test "set data-directory /the/path/to/nowhere" \
395 "Warning: /the/path/to/nowhere: .*"
397 # Testing to see if we receive a warning when calling "catch
398 # syscall" without XML support (without datadir).
399 set thistest "catch syscall displays a warning when there is no XML support"
400 gdb_test "catch syscall" \
401 "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).*" \
404 # Since the catchpoint was set, we must check if it's present
405 # in "info breakpoints" output.
406 check_info_bp_any_syscall
413 proc test_catch_syscall_group {} {
416 set sysnum "\\\[${decimal}\\\]"
418 gdb_test "catch syscall g:process" \
419 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*" \
420 "set catchpoint on a group of syscalls"
422 gdb_test "catch syscall group:process read" \
423 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*read.*\\)" \
424 "set catchpoints on a group of syscalls and on a single syscall"
426 gdb_test "catch syscall group:" \
427 "Unknown syscall group ''\." \
428 "set catchpoints on an invalid group"
430 gdb_test "catch syscall g:junk" \
431 "Unknown syscall group 'junk'\." \
432 "set catchpoints on an unknown group."
434 gdb_test "complete catch syscall g:proc" \
435 "catch syscall g:process" \
436 "complete catch syscall group with 'g:' prefix"
438 gdb_test "complete catch syscall group:proc" \
439 "catch syscall group:process" \
440 "complete catch syscall group with 'group:' prefix"
442 gdb_test_sequence "complete catch syscall g" \
443 "complete catch syscall group suggests 'group:' prefix" {
444 "group:descriptor" "group:file" "group:ipc" "group:memory"
445 "group:network" "group:process" "group:signal"
449 proc do_syscall_tests {} {
450 # NOTE: We don't have to point gdb at the correct data-directory.
451 # For the build tree that is handled by INTERNAL_GDBFLAGS.
453 # Verify that the 'catch syscall' help is available
454 gdb_test "help catch syscall" "Catch system calls.*"
456 # Try to set a catchpoint to a nonsense syscall
457 set thistest "catch syscall to a nonsense syscall is prohibited"
458 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
460 # Regression test for syscall completer bug.
461 gdb_test "complete catch syscall close chroo" \
462 "catch syscall close chroot" \
463 "complete catch syscall with multiple words"
465 # Testing the 'catch syscall' command without arguments.
466 # This test should catch any syscalls.
467 if {[runto_main]} { test_catch_syscall_without_args }
469 # Testing the 'catch syscall' command with arguments.
470 # This test should only catch the specified syscall.
471 if {[runto_main]} { test_catch_syscall_with_args }
473 # Testing the 'catch syscall' command with many arguments.
474 # This test should catch $all_syscalls.
475 if {[runto_main]} { test_catch_syscall_with_many_args }
477 # Testing the 'catch syscall' command with WRONG arguments.
478 # This test should not trigger any catchpoints.
479 if {[runto_main]} { test_catch_syscall_with_wrong_args }
481 # Testing the 'catch syscall' command during a restart of
483 if {[runto_main]} { test_catch_syscall_restarting_inferior }
485 # Testing the 'catch syscall' command toggling off past a
486 # syscall return, then resuming entry/return as normal.
487 if {[runto_main]} { test_catch_syscall_skipping_return }
489 # Testing the 'catch syscall' command starting mid-vfork.
490 if {[runto_main]} { test_catch_syscall_mid_vfork }
492 # Testing that 'catch syscall' entry/return tracks across execve.
493 if {[runto_main]} { test_catch_syscall_execve }
495 # Testing if the 'catch syscall' command works when switching to
496 # different architectures on-the-fly (PR gdb/10737).
497 if {[runto_main]} { test_catch_syscall_multi_arch }
499 # Testing the 'catch' syscall command for a group of syscalls.
500 if {[runto_main]} { test_catch_syscall_group }
503 proc test_catch_syscall_without_args_noxml {} {
504 with_test_prefix "without args noxml" {
505 # We will need the syscall names even not using it because we
506 # need to know know many syscalls are in the example file.
507 global decimal all_syscalls last_syscall_number unknown_syscall_number all_syscalls_numbers
511 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
513 # Now, we should be able to set a catchpoint, and GDB shall
514 # not display the warning anymore.
515 foreach name $all_syscalls number $all_syscalls_numbers {
516 with_test_prefix "$name" {
517 check_continue $number
521 check_continue "vfork" $decimal
523 with_test_prefix "ENOSYS" {
524 check_continue $unknown_syscall_number
527 # At last but not least, we check if the inferior has called
528 # the last (exit) syscall.
529 check_call_to_syscall $last_syscall_number
535 proc test_catch_syscall_with_args_noxml {} {
536 with_test_prefix "with args noxml" {
537 global all_syscalls_numbers
541 # Inserting all syscalls numbers to be caught
542 foreach syscall_number $all_syscalls_numbers {
543 insert_catch_syscall_with_arg $syscall_number
546 # Checking that all syscalls are caught.
547 foreach syscall_number $all_syscalls_numbers {
548 check_continue $syscall_number
555 proc test_catch_syscall_with_wrong_args_noxml {} {
556 with_test_prefix "with wrong args noxml" {
559 # Even without XML support, GDB should not accept unknown
560 # syscall names for the catchpoint.
561 gdb_test "catch syscall nonsense_syscall" \
562 "Unknown syscall name .nonsense_syscall.*"
568 proc test_catch_syscall_multi_arch_1 {
569 arch1 arch2 syscall1_name syscall2_name syscall_number
573 with_test_prefix "multiple targets: $arch1 vs $arch2" {
574 # We are not interested in loading any binary here, and in
575 # some systems (PowerPC, for example), if we load a binary
576 # there is no way to set other architecture.
581 foreach arch [list $arch1 $arch2] {
582 gdb_test_multiple "set architecture $arch" "" {
583 -re -wrap "Undefined item: \"$arch\"\\." {
585 unsupported $gdb_test_name
587 -re -wrap "The target architecture is set to \"$arch\"\\." {
591 if { $supported == 0 } {
595 gdb_test "set architecture $arch1" \
596 "The target architecture is set to \"$arch1\"\\."
598 gdb_test "catch syscall $syscall_number" \
599 "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \
600 "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1"
602 gdb_test "set architecture $arch2" \
603 "The target architecture is set to \"$arch2\"\\."
605 gdb_test "catch syscall $syscall_number" \
606 "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \
607 "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2"
611 proc test_catch_syscall_multi_arch {} {
615 set arch2 "i386:x86-64"
616 set syscall1_name "exit"
617 set syscall2_name "write"
619 test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \
620 $syscall2_name $syscall_number
622 set arch1 "powerpc:common"
623 set arch2 "powerpc:common64"
624 set syscall1_name "fstatat64"
625 set syscall2_name "newfstatat"
626 set syscall_number 291
627 test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \
628 $syscall2_name $syscall_number
632 set syscall1_name "setresuid32"
633 set syscall2_name "setresuid"
634 set syscall_number 108
635 test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \
636 $syscall2_name $syscall_number
640 set syscall1_name "reboot"
641 set syscall2_name "_newselect"
642 set syscall_number 142
643 test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \
644 $syscall2_name $syscall_number
646 set arch1 "s390:31-bit"
647 set arch2 "s390:64-bit"
648 set syscall1_name "_newselect"
649 set syscall2_name "select"
650 set syscall_number 142
651 test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \
652 $syscall2_name $syscall_number
654 clean_restart $binfile
657 proc do_syscall_tests_without_xml {} {
658 # Make sure GDB doesn't load the syscalls xml from the system data
660 gdb_test "set data-directory /the/path/to/nowhere" \
661 "Warning: /the/path/to/nowhere: .*"
663 # Let's test if we can catch syscalls without XML support.
664 # We should succeed, but GDB is not supposed to print syscall names.
665 if {[runto_main]} { test_catch_syscall_without_args_noxml }
667 # The only valid argument "catch syscall" should accept is the
668 # syscall number, and not the name (since it can't translate a
670 if {[runto_main]} { test_catch_syscall_with_args_noxml }
672 # Now, we'll try to provide a syscall name (valid or not) to the command,
673 # and expect it to fail.
674 if {[runto_main]} { test_catch_syscall_with_wrong_args_noxml }
677 # This procedure fills the vector "all_syscalls_numbers" with the proper
678 # numbers for the used syscalls according to the architecture.
679 proc fill_all_syscalls_numbers {} {
680 global all_syscalls_numbers last_syscall_number unknown_syscall_number all_syscalls
682 foreach syscall $all_syscalls {
683 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
686 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
687 set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1]
690 # Set up the vector all_syscalls. Returns 1 upon success, 0 upon failure.
692 proc setup_all_syscalls {} {
697 # They are ordered according to the file, so do not change this.
698 lappend all_syscalls "close"
699 lappend all_syscalls "chroot"
701 if { ![runto_main] } {
705 # SYS_pipe doesn't exist on aarch64 kernel.
706 set test "check SYS_pipe"
709 gdb_test_multiple "p pipe_syscall" $test {
710 -re -wrap " = ($decimal)" {
713 set SYS_pipe $expect_out(1,string)
715 -re -wrap "No symbol .*" {
720 set test "check SYS_pipe2"
723 gdb_test_multiple "p pipe2_syscall" $test {
724 -re -wrap " = ($decimal)" {
727 set SYS_pipe2 $expect_out(1,string)
729 -re -wrap "No symbol .*" {
734 if { $have_SYS_pipe == 0 && $have_SYS_pipe2 == 0 } {
738 with_test_prefix "determine pipe syscall" {
739 set line [gdb_get_line_number "pipe (fd)"]
740 gdb_test "break $line"
741 gdb_continue_to_breakpoint "before pipe call"
742 if { $have_SYS_pipe } {
743 gdb_test "catch syscall $SYS_pipe"
745 if { $have_SYS_pipe2 } {
746 gdb_test "catch syscall $SYS_pipe2"
749 gdb_test_multiple "continue" "" {
750 -re -wrap "Catchpoint $decimal \\(call to syscall (pipe|$SYS_pipe)\\).*" {
751 lappend all_syscalls pipe
755 -re -wrap "Catchpoint $decimal \\(call to syscall (pipe2|$SYS_pipe)\\).*" {
756 lappend all_syscalls pipe2
769 lappend all_syscalls "write"
770 lappend all_syscalls "read"
775 if { ![setup_all_syscalls] } {
779 # Fill all the syscalls numbers before starting anything.
780 fill_all_syscalls_numbers
782 # Execute the tests, using XML support
784 if { ![gdb_skip_xml_test] } {
785 clean_restart $binfile
788 # Now, we have to see if GDB displays a warning when we
789 # don't set the data-directory but try to use catch syscall
790 # anyway. For that, we must restart GDB first.
791 clean_restart $binfile
792 test_catch_syscall_fail_nodatadir
796 clean_restart $binfile
798 # Execute the tests, without XML support. In this case, GDB will
799 # only display syscall numbers, and not syscall names.
800 do_syscall_tests_without_xml