1 # Copyright 1997-2019 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.
29 clean_restart $binfile
30 if ![runto_main] then {
31 fail "can't run to main"
35 set test "catch syscall"
36 gdb_test_multiple $test $test {
37 -re "The feature \'catch syscall\' is not supported.*\r\n$gdb_prompt $" {
38 unsupported "catch syscall isn't supported"
41 -re ".*$gdb_prompt $" {
46 set test "check catch syscall"
47 gdb_test_multiple "continue" $test {
48 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
49 unsupported "catch syscall isn't supported"
52 -re ".*Catchpoint.*$gdb_prompt $" {
57 # All (but the last) syscalls from the example code. It is filled in
58 # proc setup_all_syscalls.
60 set all_syscalls_numbers { }
62 # The last syscall (exit()) does not return, so
63 # we cannot expect the catchpoint to be triggered
64 # twice. It is a special case.
65 set last_syscall "exit_group"
66 set last_syscall_number { }
68 set vfork_syscalls "(vfork|clone2?)"
70 set unknown_syscall_number { }
72 # Internal procedure used to check if, after issuing a 'catch syscall'
73 # command (without arguments), the 'info breakpoints' command displays
74 # that '"any syscall"' is to be caught.
75 proc check_info_bp_any_syscall {} {
76 # Verifying that the catchpoint appears in the 'info breakpoints'
77 # command, but with "<any syscall>".
78 set thistest "catch syscall appears in 'info breakpoints'"
79 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest
82 # Internal procedure used to check if, after issuing a 'catch syscall X'
83 # command (with arguments), the 'info breakpoints' command displays
84 # that the syscall 'X' is to be caught.
85 proc check_info_bp_specific_syscall { syscall } {
86 set thistest "syscall(s) $syscall appears in 'info breakpoints'"
87 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
90 # Internal procedure used to check if, after issuing a 'catch syscall X'
91 # command (with many arguments), the 'info breakpoints' command displays
92 # that the syscalls 'X' are to be caught.
93 proc check_info_bp_many_syscalls { syscalls } {
96 foreach name $syscalls {
97 set filter_str "${filter_str}${name}, "
100 set filter_str [ string trimright $filter_str ", " ]
102 set thistest "syscalls $filter_str appears in 'info breakpoints'"
103 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
106 # This procedure checks if there was a call to a syscall. The optional
107 # pattern can match syscalls that vary in implementation, like vfork.
108 proc check_call_to_syscall { syscall { pattern "" } } {
111 if { $pattern eq "" } {
112 set pattern "${syscall}"
115 set thistest "program has called $syscall"
116 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${pattern}.?\\).*" $thistest
119 # This procedure checks if the syscall returned. The optional pattern
120 # can match syscalls that vary in implementation, like vfork.
121 proc check_return_from_syscall { syscall { pattern "" } } {
124 if { $pattern eq "" } {
125 set pattern "${syscall}"
128 set thistest "syscall $syscall has returned"
129 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" $thistest
132 # Internal procedure that performs two 'continue' commands and checks if
133 # a syscall call AND return occur. The optional pattern can match
134 # syscalls that vary in implementation, like vfork.
135 proc check_continue { syscall { pattern "" } } {
136 # Testing if the 'continue' stops at the
137 # specified syscall_name. If it does, then it should
138 # first print that the infeior has called the syscall,
139 # and after print that the syscall has returned.
141 # Testing if the inferior has called the syscall.
142 check_call_to_syscall $syscall $pattern
143 # And now, that the syscall has returned.
144 check_return_from_syscall $syscall $pattern
147 # Inserts a syscall catchpoint with an argument.
148 proc insert_catch_syscall_with_arg { syscall } {
151 # Trying to set the catchpoint
152 set thistest "catch syscall with arguments ($syscall)"
153 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
155 check_info_bp_specific_syscall $syscall
158 # Inserts a syscall catchpoint with many arguments.
159 proc insert_catch_syscall_with_many_args { syscalls numbers } {
162 set catch [ join $syscalls " " ]
165 foreach name $syscalls number $numbers {
166 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
169 set filter_str [ string trimright $filter_str " " ]
171 # Trying to set the catchpoint
172 set thistest "catch syscall with arguments ($filter_str)"
173 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
175 check_info_bp_many_syscalls $syscalls
178 proc check_for_program_end {} {
179 # Deleting the catchpoints
182 gdb_continue_to_end "" continue 1
185 proc test_catch_syscall_without_args {} {
186 global all_syscalls last_syscall vfork_syscalls unknown_syscall_number decimal
188 with_test_prefix "without arguments" {
189 # Trying to set the syscall.
190 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
192 check_info_bp_any_syscall
194 # We have to check every syscall.
195 foreach name $all_syscalls {
199 check_continue "vfork" $vfork_syscalls
201 with_test_prefix "ENOSYS" {
202 check_continue $unknown_syscall_number
205 # At last but not least, we check if the inferior has called
206 # the last (exit) syscall.
207 check_call_to_syscall $last_syscall
209 # Now let's see if the inferior correctly finishes.
210 check_for_program_end
214 proc test_catch_syscall_with_args {} {
215 with_test_prefix "with arguments" {
216 set syscall_name "close"
217 insert_catch_syscall_with_arg $syscall_name
219 # Can we continue until we catch the syscall?
220 check_continue $syscall_name
222 # Now let's see if the inferior correctly finishes.
223 check_for_program_end
227 proc test_catch_syscall_with_many_args {} {
228 with_test_prefix "with many arguments" {
229 global all_syscalls all_syscalls_numbers
231 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
233 # Can we continue until we catch the syscalls?
234 foreach name $all_syscalls {
238 # Now let's see if the inferior correctly finishes.
239 check_for_program_end
243 proc test_catch_syscall_with_wrong_args {} {
244 with_test_prefix "wrong args" {
245 # mlock is not called from the source
246 set syscall_name "mlock"
247 insert_catch_syscall_with_arg $syscall_name
249 # Now, we must verify if the program stops with a continue.
250 # If it doesn't, everything is right (since we don't have
251 # a syscall named "mlock" in it). Otherwise, this is a failure.
252 set thistest "catch syscall with unused syscall ($syscall_name)"
253 gdb_continue_to_end $thistest continue 1
257 proc test_catch_syscall_restarting_inferior {} {
258 with_test_prefix "restarting inferior" {
259 set syscall_name "chroot"
261 with_test_prefix "entry" {
262 insert_catch_syscall_with_arg $syscall_name
264 # Let's first reach the entry of the syscall.
265 check_call_to_syscall $syscall_name
268 with_test_prefix "entry/return" {
269 # Now, restart the program.
272 # And check for entry/return.
273 check_continue $syscall_name
276 check_for_program_end
281 proc test_catch_syscall_skipping_return {} {
282 with_test_prefix "skipping return" {
283 with_test_prefix "entry" {
284 set syscall_name "write"
286 insert_catch_syscall_with_arg $syscall_name
288 # Let's first reach the entry of the syscall.
289 check_call_to_syscall $syscall_name
291 # Now purposely skip the syscall return.
293 gdb_test "stepi" ".*" "step over syscall return"
296 # With a naive entry/return toggle, gdb will still think
297 # the target is due for a syscall return.
299 with_test_prefix "entry/return" {
300 set syscall_name "read"
302 insert_catch_syscall_with_arg $syscall_name
304 # Check for entry first, then return.
305 check_continue $syscall_name
308 check_for_program_end
313 proc test_catch_syscall_mid_vfork {} {
314 global gdb_prompt decimal vfork_syscalls
316 with_test_prefix "mid-vfork" {
317 # Verify that the system supports "catch vfork".
318 gdb_test "catch vfork" "Catchpoint $decimal \\(vfork\\)" "insert first vfork catchpoint"
319 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
320 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
321 unsupported "continue to first vfork catchpoint"
324 -re ".*Catchpoint $decimal \\(vforked process $decimal\\).*$gdb_prompt $" {
325 pass "continue to first vfork catchpoint"
329 # Check that we now reach vfork return only.
330 # (The actual syscall used varies by architecture.)
331 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
332 check_return_from_syscall "vfork" $vfork_syscalls
335 check_for_program_end
339 proc test_catch_syscall_execve {} {
340 global gdb_prompt decimal
342 with_test_prefix "execve" {
344 # Tell the test program we want an execve.
345 gdb_test_no_output "set do_execve = 1"
347 # Check for entry/return across the execve, making sure that the
348 # syscall_state isn't lost when turning into a new process.
349 insert_catch_syscall_with_arg "execve"
350 check_continue "execve"
352 # Continue to main so extended-remote can read files as needed.
353 # (Otherwise that "Reading" output confuses gdb_continue_to_end.)
357 check_for_program_end
361 proc test_catch_syscall_fail_nodatadir {} {
362 with_test_prefix "fail no datadir" {
366 # Make sure GDB doesn't load the syscalls xml from the system
368 gdb_test "set data-directory /the/path/to/nowhere" \
369 "Warning: /the/path/to/nowhere: .*"
371 # Testing to see if we receive a warning when calling "catch
372 # syscall" without XML support (without datadir).
373 set thistest "catch syscall displays a warning when there is no XML support"
374 gdb_test "catch syscall" \
375 "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).*" \
378 # Since the catchpoint was set, we must check if it's present
379 # in "info breakpoints" output.
380 check_info_bp_any_syscall
387 proc test_catch_syscall_group {} {
390 set sysnum "\\\[${decimal}\\\]"
392 gdb_test "catch syscall g:process" \
393 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*" \
394 "set catchpoint on a group of syscalls"
396 gdb_test "catch syscall group:process read" \
397 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*read.*\\)" \
398 "set catchpoints on a group of syscalls and on a single syscall"
400 gdb_test "catch syscall group:" \
401 "Unknown syscall group ''\." \
402 "set catchpoints on an invalid group"
404 gdb_test "catch syscall g:junk" \
405 "Unknown syscall group 'junk'\." \
406 "set catchpoints on an unknown group."
408 gdb_test "complete catch syscall g:proc" \
409 "catch syscall g:process" \
410 "complete catch syscall group with 'g:' prefix"
412 gdb_test "complete catch syscall group:proc" \
413 "catch syscall group:process" \
414 "complete catch syscall group with 'group:' prefix"
416 gdb_test_sequence "complete catch syscall g" \
417 "complete catch syscall group suggests 'group:' prefix" {
418 "group:descriptor" "group:file" "group:ipc" "group:memory"
419 "group:network" "group:process" "group:signal"
423 proc do_syscall_tests {} {
424 # NOTE: We don't have to point gdb at the correct data-directory.
425 # For the build tree that is handled by INTERNAL_GDBFLAGS.
427 # Verify that the 'catch syscall' help is available
428 set thistest "help catch syscall"
429 gdb_test "help catch syscall" "Catch system calls.*" $thistest
431 # Try to set a catchpoint to a nonsense syscall
432 set thistest "catch syscall to a nonsense syscall is prohibited"
433 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
435 # Regression test for syscall completer bug.
436 gdb_test "complete catch syscall close chroo" \
437 "catch syscall close chroot" \
438 "complete catch syscall with multiple words"
440 # Testing the 'catch syscall' command without arguments.
441 # This test should catch any syscalls.
442 if [runto_main] then { test_catch_syscall_without_args }
444 # Testing the 'catch syscall' command with arguments.
445 # This test should only catch the specified syscall.
446 if [runto_main] then { test_catch_syscall_with_args }
448 # Testing the 'catch syscall' command with many arguments.
449 # This test should catch $all_syscalls.
450 if [runto_main] then { test_catch_syscall_with_many_args }
452 # Testing the 'catch syscall' command with WRONG arguments.
453 # This test should not trigger any catchpoints.
454 if [runto_main] then { test_catch_syscall_with_wrong_args }
456 # Testing the 'catch syscall' command during a restart of
458 if [runto_main] then { test_catch_syscall_restarting_inferior }
460 # Testing the 'catch syscall' command toggling off past a
461 # syscall return, then resuming entry/return as normal.
462 if [runto_main] then { test_catch_syscall_skipping_return }
464 # Testing the 'catch syscall' command starting mid-vfork.
465 if [runto_main] then { test_catch_syscall_mid_vfork }
467 # Testing that 'catch syscall' entry/return tracks across execve.
468 if [runto_main] then { test_catch_syscall_execve }
470 # Testing if the 'catch syscall' command works when switching to
471 # different architectures on-the-fly (PR gdb/10737).
472 if [runto_main] then { test_catch_syscall_multi_arch }
474 # Testing the 'catch' syscall command for a group of syscalls.
475 if [runto_main] then { test_catch_syscall_group }
478 proc test_catch_syscall_without_args_noxml {} {
479 with_test_prefix "without args noxml" {
480 # We will need the syscall names even not using it because we
481 # need to know know many syscalls are in the example file.
482 global decimal all_syscalls last_syscall_number unknown_syscall_number all_syscalls_numbers
486 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
488 # Now, we should be able to set a catchpoint, and GDB shall
489 # not display the warning anymore.
490 foreach name $all_syscalls number $all_syscalls_numbers {
491 with_test_prefix "$name" {
492 check_continue $number
496 check_continue "vfork" $decimal
498 with_test_prefix "ENOSYS" {
499 check_continue $unknown_syscall_number
502 # At last but not least, we check if the inferior has called
503 # the last (exit) syscall.
504 check_call_to_syscall $last_syscall_number
510 proc test_catch_syscall_with_args_noxml {} {
511 with_test_prefix "with args noxml" {
512 global all_syscalls_numbers
516 # Inserting all syscalls numbers to be caught
517 foreach syscall_number $all_syscalls_numbers {
518 insert_catch_syscall_with_arg $syscall_number
521 # Checking that all syscalls are caught.
522 foreach syscall_number $all_syscalls_numbers {
523 check_continue $syscall_number
530 proc test_catch_syscall_with_wrong_args_noxml {} {
531 with_test_prefix "with wrong args noxml" {
534 # Even without XML support, GDB should not accept unknown
535 # syscall names for the catchpoint.
536 gdb_test "catch syscall nonsense_syscall" \
537 "Unknown syscall name .nonsense_syscall.*"
543 proc test_catch_syscall_multi_arch {} {
544 global decimal binfile
546 if { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } {
548 set arch2 "i386:x86-64"
549 set syscall1_name "exit"
550 set syscall2_name "write"
552 } elseif { [istarget "powerpc-*-linux*"] \
553 || [istarget "powerpc64-*-linux*"] } {
554 set arch1 "powerpc:common"
555 set arch2 "powerpc:common64"
556 set syscall1_name "openat"
557 set syscall2_name "unlinkat"
558 set syscall_number 286
559 } elseif { [istarget "sparc-*-linux*"] \
560 || [istarget "sparc64-*-linux*"] } {
563 set syscall1_name "setresuid32"
564 set syscall2_name "setresuid"
565 set syscall_number 108
566 } elseif { [istarget "mips*-linux*"] } {
567 # MIPS does not use the same numbers for syscalls on 32 and 64
569 verbose "Not testing MIPS for multi-arch syscall support"
571 } elseif { [istarget "arm*-linux*"] } {
572 # catch syscall supports only 32-bit ARM for now.
573 verbose "Not testing ARM for multi-arch syscall support"
575 } elseif { [istarget "aarch64*-linux*"] } {
578 set syscall1_name "reboot"
579 set syscall2_name "_newselect"
580 set syscall_number 142
581 } elseif { [istarget "s390*-linux*"] } {
582 set arch1 "s390:31-bit"
583 set arch2 "s390:64-bit"
584 set syscall1_name "_newselect"
585 set syscall2_name "select"
586 set syscall_number 142
589 with_test_prefix "multiple targets" {
590 # We are not interested in loading any binary here, and in
591 # some systems (PowerPC, for example), if we load a binary
592 # there is no way to set other architecture.
596 gdb_test "set architecture $arch1" \
597 "The target architecture is assumed to be $arch1" \
600 gdb_test "catch syscall $syscall_number" \
601 "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \
602 "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1"
604 gdb_test "set architecture $arch2" \
605 "The target architecture is assumed to be $arch2" \
608 gdb_test "catch syscall $syscall_number" \
609 "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \
610 "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2"
612 clean_restart $binfile
616 proc do_syscall_tests_without_xml {} {
617 # Make sure GDB doesn't load the syscalls xml from the system data
619 gdb_test "set data-directory /the/path/to/nowhere" \
620 "Warning: /the/path/to/nowhere: .*"
622 # Let's test if we can catch syscalls without XML support.
623 # We should succeed, but GDB is not supposed to print syscall names.
624 if [runto_main] then { test_catch_syscall_without_args_noxml }
626 # The only valid argument "catch syscall" should accept is the
627 # syscall number, and not the name (since it can't translate a
629 if [runto_main] then { test_catch_syscall_with_args_noxml }
631 # Now, we'll try to provide a syscall name (valid or not) to the command,
632 # and expect it to fail.
633 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
636 # This procedure fills the vector "all_syscalls_numbers" with the proper
637 # numbers for the used syscalls according to the architecture.
638 proc fill_all_syscalls_numbers {} {
639 global all_syscalls_numbers last_syscall_number unknown_syscall_number all_syscalls
641 foreach syscall $all_syscalls {
642 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
645 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
646 set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1]
649 # Set up the vector all_syscalls.
651 proc setup_all_syscalls {} {
655 # They are ordered according to the file, so do not change this.
656 lappend all_syscalls "close"
657 lappend all_syscalls "chroot"
659 # SYS_pipe doesn't exist on aarch64 kernel.
660 set test "check SYS_pipe"
661 gdb_test_multiple "p pipe_syscall" $test {
662 -re " = .*$gdb_prompt $" {
664 lappend all_syscalls "pipe"
666 -re "No symbol .*$gdb_prompt $" {
668 # SYS_pipe isn't defined, use SYS_pipe2 instead.
669 lappend all_syscalls "pipe2"
673 lappend all_syscalls "write"
674 lappend all_syscalls "read"
679 # Fill all the syscalls numbers before starting anything.
680 fill_all_syscalls_numbers
682 # Execute the tests, using XML support
684 if { ![gdb_skip_xml_test] } {
685 clean_restart $binfile
688 # Now, we have to see if GDB displays a warning when we
689 # don't set the data-directory but try to use catch syscall
690 # anyway. For that, we must restart GDB first.
691 clean_restart $binfile
692 test_catch_syscall_fail_nodatadir
696 clean_restart $binfile
698 # Execute the tests, without XML support. In this case, GDB will
699 # only display syscall numbers, and not syscall names.
700 do_syscall_tests_without_xml