1# Copyright 1997-2016 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 22standard_testfile 23 24if { [prepare_for_testing ${testfile}.exp $testfile ${testfile}.c] } { 25 untested catch-syscall.exp 26 return -1 27} 28 29# Check target supports catch syscall or not. 30clean_restart $binfile 31if ![runto_main] then { 32 fail "Can't run to main" 33 return 34} 35 36set test "catch syscall" 37gdb_test_multiple $test $test { 38 -re "The feature \'catch syscall\' is not supported.*\r\n$gdb_prompt $" { 39 unsupported "catch syscall isn't supported" 40 return -1 41 } 42 -re ".*$gdb_prompt $" { 43 pass $test 44 } 45} 46 47set test "check catch syscall" 48gdb_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" 51 return -1 52 } 53 -re ".*Catchpoint.*$gdb_prompt $" { 54 pass $test 55 } 56} 57 58# All (but the last) syscalls from the example code. It is filled in 59# proc setup_all_syscalls. 60set all_syscalls { } 61set all_syscalls_numbers { } 62 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. 66set last_syscall "exit_group" 67set last_syscall_number { } 68 69set vfork_syscalls "(vfork|clone2?)" 70 71set unknown_syscall_number { } 72 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. 76proc 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 81} 82 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. 86proc 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 89} 90 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. 94proc check_info_bp_many_syscalls { syscalls } { 95 set filter_str "" 96 97 foreach name $syscalls { 98 set filter_str "${filter_str}${name}, " 99 } 100 101 set filter_str [ string trimright $filter_str ", " ] 102 103 set thistest "syscalls $filter_str appears in 'info breakpoints'" 104 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest 105} 106 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. 109proc check_call_to_syscall { syscall { pattern "" } } { 110 global decimal 111 112 if { $pattern eq "" } { 113 set pattern "${syscall}" 114 } 115 116 set thistest "program has called $syscall" 117 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${pattern}.?\\).*" $thistest 118} 119 120# This procedure checks if the syscall returned. The optional pattern 121# can match syscalls that vary in implementation, like vfork. 122proc check_return_from_syscall { syscall { pattern "" } } { 123 global decimal 124 125 if { $pattern eq "" } { 126 set pattern "${syscall}" 127 } 128 129 set thistest "syscall $syscall has returned" 130 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" $thistest 131} 132 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. 136proc 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. 141 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 146} 147 148# Inserts a syscall catchpoint with an argument. 149proc insert_catch_syscall_with_arg { syscall } { 150 global decimal 151 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 155 156 check_info_bp_specific_syscall $syscall 157} 158 159# Inserts a syscall catchpoint with many arguments. 160proc insert_catch_syscall_with_many_args { syscalls numbers } { 161 global decimal 162 163 set catch [ join $syscalls " " ] 164 set filter_str "" 165 166 foreach name $syscalls number $numbers { 167 set filter_str "${filter_str}'${name}' \\\[${number}\\\] " 168 } 169 170 set filter_str [ string trimright $filter_str " " ] 171 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 175 176 check_info_bp_many_syscalls $syscalls 177} 178 179proc check_for_program_end {} { 180 # Deleting the catchpoints 181 delete_breakpoints 182 183 gdb_continue_to_end 184} 185 186proc test_catch_syscall_without_args {} { 187 global all_syscalls last_syscall vfork_syscalls unknown_syscall_number decimal 188 189 with_test_prefix "without arguments" { 190 # Trying to set the syscall. 191 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)" 192 193 check_info_bp_any_syscall 194 195 # We have to check every syscall. 196 foreach name $all_syscalls { 197 check_continue $name 198 } 199 200 check_continue "vfork" $vfork_syscalls 201 202 with_test_prefix "ENOSYS" { 203 check_continue $unknown_syscall_number 204 } 205 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 209 210 # Now let's see if the inferior correctly finishes. 211 check_for_program_end 212 } 213} 214 215proc test_catch_syscall_with_args {} { 216 with_test_prefix "with arguments" { 217 set syscall_name "close" 218 insert_catch_syscall_with_arg $syscall_name 219 220 # Can we continue until we catch the syscall? 221 check_continue $syscall_name 222 223 # Now let's see if the inferior correctly finishes. 224 check_for_program_end 225 } 226} 227 228proc test_catch_syscall_with_many_args {} { 229 with_test_prefix "with many arguments" { 230 global all_syscalls all_syscalls_numbers 231 232 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers 233 234 # Can we continue until we catch the syscalls? 235 foreach name $all_syscalls { 236 check_continue $name 237 } 238 239 # Now let's see if the inferior correctly finishes. 240 check_for_program_end 241 } 242} 243 244proc 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 249 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 255 } 256} 257 258proc test_catch_syscall_restarting_inferior {} { 259 with_test_prefix "restarting inferior" { 260 set syscall_name "chroot" 261 262 with_test_prefix "entry" { 263 insert_catch_syscall_with_arg $syscall_name 264 265 # Let's first reach the entry of the syscall. 266 check_call_to_syscall $syscall_name 267 } 268 269 with_test_prefix "entry/return" { 270 # Now, restart the program. 271 rerun_to_main 272 273 # And check for entry/return. 274 check_continue $syscall_name 275 276 # Can we finish? 277 check_for_program_end 278 } 279 } 280} 281 282proc test_catch_syscall_skipping_return {} { 283 with_test_prefix "skipping return" { 284 with_test_prefix "entry" { 285 set syscall_name "write" 286 287 insert_catch_syscall_with_arg $syscall_name 288 289 # Let's first reach the entry of the syscall. 290 check_call_to_syscall $syscall_name 291 292 # Now purposely skip the syscall return. 293 delete_breakpoints 294 gdb_test "stepi" ".*" "step over syscall return" 295 } 296 297 # With a naive entry/return toggle, gdb will still think 298 # the target is due for a syscall return. 299 300 with_test_prefix "entry/return" { 301 set syscall_name "read" 302 303 insert_catch_syscall_with_arg $syscall_name 304 305 # Check for entry first, then return. 306 check_continue $syscall_name 307 308 # Can we finish? 309 check_for_program_end 310 } 311 } 312} 313 314proc test_catch_syscall_mid_vfork {} { 315 global gdb_prompt decimal vfork_syscalls 316 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" 323 return 324 } 325 -re ".*Catchpoint $decimal \\(vforked process $decimal\\).*$gdb_prompt $" { 326 pass "continue to first vfork catchpoint" 327 } 328 } 329 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 334 335 # Can we finish? 336 check_for_program_end 337 } 338} 339 340proc test_catch_syscall_execve {} { 341 global gdb_prompt decimal 342 343 with_test_prefix "execve" { 344 345 # Tell the test program we want an execve. 346 gdb_test_no_output "set do_execve = 1" 347 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" 352 353 # Continue to main so extended-remote can read files as needed. 354 # (Otherwise that "Reading" output confuses gdb_continue_to_end.) 355 gdb_continue "main" 356 357 # Now can we finish? 358 check_for_program_end 359 } 360} 361 362proc test_catch_syscall_fail_nodatadir {} { 363 with_test_prefix "fail no datadir" { 364 # Sanitizing. 365 delete_breakpoints 366 367 # Make sure GDB doesn't load the syscalls xml from the system 368 # data directory. 369 gdb_test "set data-directory /the/path/to/nowhere" \ 370 "Warning: /the/path/to/nowhere: .*" 371 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).*" \ 377 $thistest 378 379 # Since the catchpoint was set, we must check if it's present 380 # in "info breakpoints" output. 381 check_info_bp_any_syscall 382 383 # Sanitizing. 384 delete_breakpoints 385 } 386} 387 388proc test_catch_syscall_group {} { 389 global decimal 390 391 set sysnum "\\\[${decimal}\\\]" 392 393 gdb_test "catch syscall g:process" \ 394 "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*" \ 395 "set catchpoint on a group of syscalls" 396 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" 400 401 gdb_test "catch syscall group:" \ 402 "Unknown syscall group ''\." \ 403 "set catchpoints on an invalid group" 404 405 gdb_test "catch syscall g:junk" \ 406 "Unknown syscall group 'junk'\." \ 407 "set catchpoints on an unknown group." 408 409 gdb_test "complete catch syscall g:proc" \ 410 "catch syscall g:process" \ 411 "complete catch syscall group with 'g:' prefix" 412 413 gdb_test "complete catch syscall group:proc" \ 414 "catch syscall group:process" \ 415 "complete catch syscall group with 'group:' prefix" 416 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" 421 } 422} 423 424proc 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. 427 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 431 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 435 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" 440 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 } 444 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 } 448 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 } 452 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 } 456 457 # Testing the 'catch syscall' command during a restart of 458 # the inferior. 459 if [runto_main] then { test_catch_syscall_restarting_inferior } 460 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 } 464 465 # Testing the 'catch syscall' command starting mid-vfork. 466 if [runto_main] then { test_catch_syscall_mid_vfork } 467 468 # Testing that 'catch syscall' entry/return tracks across execve. 469 if [runto_main] then { test_catch_syscall_execve } 470 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 } 474 475 # Testing the 'catch' syscall command for a group of syscalls. 476 if [runto_main] then { test_catch_syscall_group } 477} 478 479proc 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 484 485 delete_breakpoints 486 487 gdb_test "catch syscall" "Catchpoint .*(syscall).*" 488 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 494 } 495 } 496 497 check_continue "vfork" $decimal 498 499 with_test_prefix "ENOSYS" { 500 check_continue $unknown_syscall_number 501 } 502 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 506 507 delete_breakpoints 508 } 509} 510 511proc test_catch_syscall_with_args_noxml {} { 512 with_test_prefix "with args noxml" { 513 global all_syscalls_numbers 514 515 delete_breakpoints 516 517 # Inserting all syscalls numbers to be caught 518 foreach syscall_number $all_syscalls_numbers { 519 insert_catch_syscall_with_arg $syscall_number 520 } 521 522 # Checking that all syscalls are caught. 523 foreach syscall_number $all_syscalls_numbers { 524 check_continue $syscall_number 525 } 526 527 delete_breakpoints 528 } 529} 530 531proc test_catch_syscall_with_wrong_args_noxml {} { 532 with_test_prefix "with wrong args noxml" { 533 delete_breakpoints 534 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.*" 539 540 delete_breakpoints 541 } 542} 543 544proc test_catch_syscall_multi_arch {} { 545 global decimal binfile 546 547 if { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } { 548 set arch1 "i386" 549 set arch2 "i386:x86-64" 550 set syscall1_name "exit" 551 set syscall2_name "write" 552 set syscall_number 1 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*"] } { 562 set arch1 "sparc" 563 set arch2 "sparc:v9" 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 569 # bits. 570 verbose "Not testing MIPS for multi-arch syscall support" 571 return 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" 575 return 576 } elseif { [istarget "aarch64*-linux*"] } { 577 set arch1 "aarch64" 578 set arch2 "arm" 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 588 } 589 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. 594 gdb_exit 595 gdb_start 596 597 gdb_test "set architecture $arch1" \ 598 "The target architecture is assumed to be $arch1" \ 599 "set arch to $arch1" 600 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" 604 605 gdb_test "set architecture $arch2" \ 606 "The target architecture is assumed to be $arch2" \ 607 "set arch to $arch2" 608 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" 612 613 clean_restart $binfile 614 } 615} 616 617proc do_syscall_tests_without_xml {} { 618 # Make sure GDB doesn't load the syscalls xml from the system data 619 # directory. 620 gdb_test "set data-directory /the/path/to/nowhere" \ 621 "Warning: /the/path/to/nowhere: .*" 622 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 } 626 627 # The only valid argument "catch syscall" should accept is the 628 # syscall number, and not the name (since it can't translate a 629 # name to a number). 630 if [runto_main] then { test_catch_syscall_with_args_noxml } 631 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 } 635} 636 637# This procedure fills the vector "all_syscalls_numbers" with the proper 638# numbers for the used syscalls according to the architecture. 639proc fill_all_syscalls_numbers {} { 640 global all_syscalls_numbers last_syscall_number unknown_syscall_number all_syscalls 641 642 foreach syscall $all_syscalls { 643 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1] 644 } 645 646 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1] 647 set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1] 648} 649 650# Set up the vector all_syscalls. 651 652proc setup_all_syscalls {} { 653 global all_syscalls 654 global gdb_prompt 655 656 # They are ordered according to the file, so do not change this. 657 lappend all_syscalls "close" 658 lappend all_syscalls "chroot" 659 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 $" { 664 pass $test 665 lappend all_syscalls "pipe" 666 } 667 -re "No symbol .*$gdb_prompt $" { 668 pass $test 669 # SYS_pipe isn't defined, use SYS_pipe2 instead. 670 lappend all_syscalls "pipe2" 671 } 672 } 673 674 lappend all_syscalls "write" 675 lappend all_syscalls "read" 676} 677 678setup_all_syscalls 679 680# Fill all the syscalls numbers before starting anything. 681fill_all_syscalls_numbers 682 683# Execute the tests, using XML support 684gdb_exit 685if { ![gdb_skip_xml_test] } { 686 clean_restart $binfile 687 do_syscall_tests 688 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 694} 695 696# Restart gdb 697clean_restart $binfile 698 699# Execute the tests, without XML support. In this case, GDB will 700# only display syscall numbers, and not syscall names. 701do_syscall_tests_without_xml 702