1# Copyright 1997-2015 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 22if { [is_remote target] || ![isnative] } then { 23 continue 24} 25 26# This shall be updated whenever 'catch syscall' is implemented 27# on some architecture. 28if { ![istarget "x86_64-*-linux*"] && ![istarget "i\[34567\]86-*-linux*"] 29 && ![istarget "powerpc-*-linux*"] && ![istarget "powerpc64-*-linux*"] 30 && ![istarget "sparc-*-linux*"] && ![istarget "sparc64-*-linux*"] 31 && ![istarget "mips*-linux*"] && ![istarget "arm*-linux*"] 32 && ![istarget "s390*-linux*"] && ![istarget "aarch64*-*-linux*"] } { 33 continue 34} 35 36standard_testfile 37 38if { [prepare_for_testing ${testfile}.exp $testfile ${testfile}.c] } { 39 untested catch-syscall.exp 40 return -1 41} 42 43# All (but the last) syscalls from the example code. It is filled in 44# proc setup_all_syscalls. 45set all_syscalls { } 46set all_syscalls_numbers { } 47 48# The last syscall (exit()) does not return, so 49# we cannot expect the catchpoint to be triggered 50# twice. It is a special case. 51set last_syscall "exit_group" 52set last_syscall_number { } 53 54# Internal procedure used to check if, after issuing a 'catch syscall' 55# command (without arguments), the 'info breakpoints' command displays 56# that '"any syscall"' is to be caught. 57proc check_info_bp_any_syscall {} { 58 # Verifying that the catchpoint appears in the 'info breakpoints' 59 # command, but with "<any syscall>". 60 set thistest "catch syscall appears in 'info breakpoints'" 61 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest 62} 63 64# Internal procedure used to check if, after issuing a 'catch syscall X' 65# command (with arguments), the 'info breakpoints' command displays 66# that the syscall 'X' is to be caught. 67proc check_info_bp_specific_syscall { syscall } { 68 set thistest "syscall(s) $syscall appears in 'info breakpoints'" 69 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest 70} 71 72# Internal procedure used to check if, after issuing a 'catch syscall X' 73# command (with many arguments), the 'info breakpoints' command displays 74# that the syscalls 'X' are to be caught. 75proc check_info_bp_many_syscalls { syscalls } { 76 set filter_str "" 77 78 foreach name $syscalls { 79 set filter_str "${filter_str}${name}, " 80 } 81 82 set filter_str [ string trimright $filter_str ", " ] 83 84 set thistest "syscalls $filter_str appears in 'info breakpoints'" 85 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest 86} 87 88# This procedure checks if there was a call to a syscall. 89proc check_call_to_syscall { syscall } { 90 global decimal 91 92 set thistest "program has called $syscall" 93 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${syscall}.?\\).*" $thistest 94} 95 96# This procedure checks if the syscall returned. 97proc check_return_from_syscall { syscall } { 98 global decimal 99 100 set thistest "syscall $syscall has returned" 101 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${syscall}\\).*" $thistest 102} 103 104# Internal procedure that performs two 'continue' commands and checks if 105# a syscall call AND return occur. 106proc check_continue { syscall } { 107 # Testing if the 'continue' stops at the 108 # specified syscall_name. If it does, then it should 109 # first print that the infeior has called the syscall, 110 # and after print that the syscall has returned. 111 112 # Testing if the inferior has called the syscall. 113 check_call_to_syscall $syscall 114 # And now, that the syscall has returned. 115 check_return_from_syscall $syscall 116} 117 118# Inserts a syscall catchpoint with an argument. 119proc insert_catch_syscall_with_arg { syscall } { 120 global decimal 121 122 # Trying to set the catchpoint 123 set thistest "catch syscall with arguments ($syscall)" 124 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest 125 126 check_info_bp_specific_syscall $syscall 127} 128 129# Inserts a syscall catchpoint with many arguments. 130proc insert_catch_syscall_with_many_args { syscalls numbers } { 131 global decimal 132 133 set catch [ join $syscalls " " ] 134 set filter_str "" 135 136 foreach name $syscalls number $numbers { 137 set filter_str "${filter_str}'${name}' \\\[${number}\\\] " 138 } 139 140 set filter_str [ string trimright $filter_str " " ] 141 142 # Trying to set the catchpoint 143 set thistest "catch syscall with arguments ($filter_str)" 144 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest 145 146 check_info_bp_many_syscalls $syscalls 147} 148 149proc check_for_program_end {} { 150 # Deleting the catchpoints 151 delete_breakpoints 152 153 gdb_continue_to_end 154} 155 156proc test_catch_syscall_without_args {} { 157 global all_syscalls last_syscall decimal 158 159 with_test_prefix "without arguments" { 160 # Trying to set the syscall. 161 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)" 162 163 check_info_bp_any_syscall 164 165 # We have to check every syscall. 166 foreach name $all_syscalls { 167 check_continue $name 168 } 169 170 # At last but not least, we check if the inferior has called 171 # the last (exit) syscall. 172 check_call_to_syscall $last_syscall 173 174 # Now let's see if the inferior correctly finishes. 175 check_for_program_end 176 } 177} 178 179proc test_catch_syscall_with_args {} { 180 with_test_prefix "with arguments" { 181 set syscall_name "close" 182 insert_catch_syscall_with_arg $syscall_name 183 184 # Can we continue until we catch the syscall? 185 check_continue $syscall_name 186 187 # Now let's see if the inferior correctly finishes. 188 check_for_program_end 189 } 190} 191 192proc test_catch_syscall_with_many_args {} { 193 with_test_prefix "with many arguments" { 194 global all_syscalls all_syscalls_numbers 195 196 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers 197 198 # Can we continue until we catch the syscalls? 199 foreach name $all_syscalls { 200 check_continue $name 201 } 202 203 # Now let's see if the inferior correctly finishes. 204 check_for_program_end 205 } 206} 207 208proc test_catch_syscall_with_wrong_args {} { 209 with_test_prefix "wrong args" { 210 # mlock is not called from the source 211 set syscall_name "mlock" 212 insert_catch_syscall_with_arg $syscall_name 213 214 # Now, we must verify if the program stops with a continue. 215 # If it doesn't, everything is right (since we don't have 216 # a syscall named "mlock" in it). Otherwise, this is a failure. 217 set thistest "catch syscall with unused syscall ($syscall_name)" 218 gdb_continue_to_end $thistest 219 } 220} 221 222proc test_catch_syscall_restarting_inferior {} { 223 with_test_prefix "restarting inferior" { 224 set syscall_name "chroot" 225 226 with_test_prefix "entry" { 227 insert_catch_syscall_with_arg $syscall_name 228 229 # Let's first reach the entry of the syscall. 230 check_call_to_syscall $syscall_name 231 } 232 233 with_test_prefix "entry/return" { 234 # Now, restart the program. 235 rerun_to_main 236 237 # And check for entry/return. 238 check_continue $syscall_name 239 240 # Can we finish? 241 check_for_program_end 242 } 243 } 244} 245 246proc test_catch_syscall_fail_nodatadir {} { 247 with_test_prefix "fail no datadir" { 248 # Sanitizing. 249 delete_breakpoints 250 251 # Make sure GDB doesn't load the syscalls xml from the system 252 # data directory. 253 gdb_test "set data-directory /the/path/to/nowhere" \ 254 "Warning: /the/path/to/nowhere: .*" 255 256 # Testing to see if we receive a warning when calling "catch 257 # syscall" without XML support (without datadir). 258 set thistest "catch syscall displays a warning when there is no XML support" 259 gdb_test "catch syscall" \ 260 "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).*" \ 261 $thistest 262 263 # Since the catchpoint was set, we must check if it's present 264 # in "info breakpoints" output. 265 check_info_bp_any_syscall 266 267 # Sanitizing. 268 delete_breakpoints 269 } 270} 271 272proc do_syscall_tests {} { 273 # NOTE: We don't have to point gdb at the correct data-directory. 274 # For the build tree that is handled by INTERNAL_GDBFLAGS. 275 276 # Verify that the 'catch syscall' help is available 277 set thistest "help catch syscall" 278 gdb_test "help catch syscall" "Catch system calls.*" $thistest 279 280 # Try to set a catchpoint to a nonsense syscall 281 set thistest "catch syscall to a nonsense syscall is prohibited" 282 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest 283 284 # Regression test for syscall completer bug. 285 gdb_test "complete catch syscall close chroo" \ 286 "catch syscall close chroot" \ 287 "complete catch syscall with multiple words" 288 289 # Testing the 'catch syscall' command without arguments. 290 # This test should catch any syscalls. 291 if [runto_main] then { test_catch_syscall_without_args } 292 293 # Testing the 'catch syscall' command with arguments. 294 # This test should only catch the specified syscall. 295 if [runto_main] then { test_catch_syscall_with_args } 296 297 # Testing the 'catch syscall' command with many arguments. 298 # This test should catch $all_syscalls. 299 if [runto_main] then { test_catch_syscall_with_many_args } 300 301 # Testing the 'catch syscall' command with WRONG arguments. 302 # This test should not trigger any catchpoints. 303 if [runto_main] then { test_catch_syscall_with_wrong_args } 304 305 # Testing the 'catch' syscall command during a restart of 306 # the inferior. 307 if [runto_main] then { test_catch_syscall_restarting_inferior } 308 309 # Testing if the 'catch syscall' command works when switching to 310 # different architectures on-the-fly (PR gdb/10737). 311 if [runto_main] then { test_catch_syscall_multi_arch } 312} 313 314proc test_catch_syscall_without_args_noxml {} { 315 with_test_prefix "without args noxml" { 316 # We will need the syscall names even not using it because we 317 # need to know know many syscalls are in the example file. 318 global all_syscalls last_syscall_number all_syscalls_numbers 319 320 delete_breakpoints 321 322 gdb_test "catch syscall" "Catchpoint .*(syscall).*" 323 324 # Now, we should be able to set a catchpoint, and GDB shall 325 # not display the warning anymore. 326 foreach name $all_syscalls number $all_syscalls_numbers { 327 with_test_prefix "$name" { 328 check_continue $number 329 } 330 } 331 332 # At last but not least, we check if the inferior has called 333 # the last (exit) syscall. 334 check_call_to_syscall $last_syscall_number 335 336 delete_breakpoints 337 } 338} 339 340proc test_catch_syscall_with_args_noxml {} { 341 with_test_prefix "with args noxml" { 342 global all_syscalls_numbers 343 344 delete_breakpoints 345 346 # Inserting all syscalls numbers to be caught 347 foreach syscall_number $all_syscalls_numbers { 348 insert_catch_syscall_with_arg $syscall_number 349 } 350 351 # Checking that all syscalls are caught. 352 foreach syscall_number $all_syscalls_numbers { 353 check_continue $syscall_number 354 } 355 356 delete_breakpoints 357 } 358} 359 360proc test_catch_syscall_with_wrong_args_noxml {} { 361 with_test_prefix "with wrong args noxml" { 362 delete_breakpoints 363 364 # Even without XML support, GDB should not accept unknown 365 # syscall names for the catchpoint. 366 gdb_test "catch syscall nonsense_syscall" \ 367 "Unknown syscall name .nonsense_syscall.*" 368 369 delete_breakpoints 370 } 371} 372 373proc test_catch_syscall_multi_arch {} { 374 global decimal binfile 375 376 if { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } { 377 set arch1 "i386" 378 set arch2 "i386:x86-64" 379 set syscall1_name "exit" 380 set syscall2_name "write" 381 set syscall_number 1 382 } elseif { [istarget "powerpc-*-linux*"] \ 383 || [istarget "powerpc64-*-linux*"] } { 384 set arch1 "powerpc:common" 385 set arch2 "powerpc:common64" 386 set syscall1_name "openat" 387 set syscall2_name "unlinkat" 388 set syscall_number 286 389 } elseif { [istarget "sparc-*-linux*"] \ 390 || [istarget "sparc64-*-linux*"] } { 391 set arch1 "sparc" 392 set arch2 "sparc:v9" 393 set syscall1_name "setresuid32" 394 set syscall2_name "setresuid" 395 set syscall_number 108 396 } elseif { [istarget "mips*-linux*"] } { 397 # MIPS does not use the same numbers for syscalls on 32 and 64 398 # bits. 399 verbose "Not testing MIPS for multi-arch syscall support" 400 return 401 } elseif { [istarget "arm*-linux*"] } { 402 # catch syscall supports only 32-bit ARM for now. 403 verbose "Not testing ARM for multi-arch syscall support" 404 return 405 } elseif { [istarget "aarch64*-linux*"] } { 406 verbose "Not testing AARCH64 for multi-arch syscall support" 407 return 408 } elseif { [istarget "s390*-linux*"] } { 409 set arch1 "s390:31-bit" 410 set arch2 "s390:64-bit" 411 set syscall1_name "_newselect" 412 set syscall2_name "select" 413 set syscall_number 142 414 } 415 416 with_test_prefix "multiple targets" { 417 # We are not interested in loading any binary here, and in 418 # some systems (PowerPC, for example), if we load a binary 419 # there is no way to set other architecture. 420 gdb_exit 421 gdb_start 422 423 gdb_test "set architecture $arch1" \ 424 "The target architecture is assumed to be $arch1" \ 425 "set arch to $arch1" 426 427 gdb_test "catch syscall $syscall_number" \ 428 "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \ 429 "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1" 430 431 gdb_test "set architecture $arch2" \ 432 "The target architecture is assumed to be $arch2" \ 433 "set arch to $arch2" 434 435 gdb_test "catch syscall $syscall_number" \ 436 "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \ 437 "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2" 438 439 clean_restart $binfile 440 } 441} 442 443proc do_syscall_tests_without_xml {} { 444 # Make sure GDB doesn't load the syscalls xml from the system data 445 # directory. 446 gdb_test "set data-directory /the/path/to/nowhere" \ 447 "Warning: /the/path/to/nowhere: .*" 448 449 # Let's test if we can catch syscalls without XML support. 450 # We should succeed, but GDB is not supposed to print syscall names. 451 if [runto_main] then { test_catch_syscall_without_args_noxml } 452 453 # The only valid argument "catch syscall" should accept is the 454 # syscall number, and not the name (since it can't translate a 455 # name to a number). 456 if [runto_main] then { test_catch_syscall_with_args_noxml } 457 458 # Now, we'll try to provide a syscall name (valid or not) to the command, 459 # and expect it to fail. 460 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml } 461} 462 463# This procedure fills the vector "all_syscalls_numbers" with the proper 464# numbers for the used syscalls according to the architecture. 465proc fill_all_syscalls_numbers {} { 466 global all_syscalls_numbers last_syscall_number all_syscalls 467 468 foreach syscall $all_syscalls { 469 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1] 470 } 471 472 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1] 473} 474 475# Set up the vector all_syscalls. 476 477proc setup_all_syscalls {} { 478 global all_syscalls 479 global gdb_prompt 480 481 # They are ordered according to the file, so do not change this. 482 lappend all_syscalls "close" 483 lappend all_syscalls "chroot" 484 485 # SYS_pipe doesn't exist on aarch64 kernel. 486 set test "check SYS_pipe" 487 gdb_test_multiple "p pipe_syscall" $test { 488 -re " = .*$gdb_prompt $" { 489 pass $test 490 lappend all_syscalls "pipe" 491 } 492 -re "No symbol .*$gdb_prompt $" { 493 pass $test 494 # SYS_pipe isn't defined, use SYS_pipe2 instead. 495 lappend all_syscalls "pipe2" 496 } 497 } 498 499 lappend all_syscalls "write" 500 lappend all_syscalls "read" 501} 502 503setup_all_syscalls 504 505# Fill all the syscalls numbers before starting anything. 506fill_all_syscalls_numbers 507 508# Execute the tests, using XML support 509if { ![gdb_skip_xml_test] } { 510 clean_restart $binfile 511 do_syscall_tests 512 513 # Now, we have to see if GDB displays a warning when we 514 # don't set the data-directory but try to use catch syscall 515 # anyway. For that, we must restart GDB first. 516 clean_restart $binfile 517 test_catch_syscall_fail_nodatadir 518} 519 520# Restart gdb 521clean_restart $binfile 522 523# Execute the tests, without XML support. In this case, GDB will 524# only display syscall numbers, and not syscall names. 525do_syscall_tests_without_xml 526