1 # Copyright 1997, 1999, 2007, 2008, 2010, 2011
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # This program tests the 'catch syscall' functionality.
20 # It was written by Sergio Durigan Junior <sergiodj@linux.vnet.ibm.com>
23 if { [is_remote target] || ![isnative] } then {
29 set testfile "catch-syscall"
30 set srcfile ${testfile}.c
31 set binfile ${objdir}/${subdir}/${testfile}
33 # All (but the last) syscalls from the example code
34 # They are ordered according to the file, so do not change this.
35 set all_syscalls { "close" "chroot" }
36 set all_syscalls_numbers { }
37 # The last syscall (exit()) does not return, so
38 # we cannot expect the catchpoint to be triggered
39 # twice. It is a special case.
40 set last_syscall "exit_group"
42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
43 untested catch-syscall.exp
47 # Until "catch syscall" is implemented on other targets...
48 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
52 # This shall be updated whenever 'catch syscall' is implemented
53 # on some architecture.
54 #if { ![istarget "i\[34567\]86-*-linux*"]
55 if { ![istarget "x86_64-*-linux*"] && ![istarget "i\[34567\]86-*-linux*"]
56 && ![istarget "powerpc-*-linux*"] && ![istarget "powerpc64-*-linux*"]
57 && ![istarget "sparc-*-linux*"] && ![istarget "sparc64-*-linux*"]
58 && ![istarget "mips*-linux*"] } {
62 # Internal procedure used to check if, after issuing a 'catch syscall'
63 # command (without arguments), the 'info breakpoints' command displays
64 # that '"any syscall"' is to be caught.
65 proc check_info_bp_any_syscall {} {
68 # Verifying that the catchpoint appears in the 'info breakpoints'
69 # command, but with "<any syscall>".
70 set thistest "catch syscall appears in 'info breakpoints'"
71 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest
74 # Internal procedure used to check if, after issuing a 'catch syscall X'
75 # command (with arguments), the 'info breakpoints' command displays
76 # that the syscall 'X' is to be caught.
77 proc check_info_bp_specific_syscall { syscall } {
80 set thistest "syscall(s) $syscall appears in 'info breakpoints'"
81 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
84 # Internal procedure used to check if, after issuing a 'catch syscall X'
85 # command (with many arguments), the 'info breakpoints' command displays
86 # that the syscalls 'X' are to be caught.
87 proc check_info_bp_many_syscalls { syscalls } {
91 foreach name $syscalls {
92 set filter_str "${filter_str}${name}, "
95 set filter_str [ string trimright $filter_str ", " ]
97 set thistest "syscalls $filter_str appears in 'info breakpoints'"
98 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
101 # This procedure checks if there was a call to a syscall.
102 proc check_call_to_syscall { syscall } {
105 set thistest "program has called $syscall"
106 gdb_test "continue" "Catchpoint .*(call to syscall .?${syscall}.?).*" $thistest
109 # This procedure checks if the syscall returned.
110 proc check_return_from_syscall { syscall } {
113 set thistest "syscall $syscall has returned"
114 gdb_test "continue" "Catchpoint .*(returned from syscall (.)?${syscall}(.)?).*" $thistest
117 # Internal procedure that performs two 'continue' commands and checks if
118 # a syscall call AND return occur.
119 proc check_continue { syscall } {
122 # Testing if the 'continue' stops at the
123 # specified syscall_name. If it does, then it should
124 # first print that the infeior has called the syscall,
125 # and after print that the syscall has returned.
127 # Testing if the inferiorr has called the syscall.
128 check_call_to_syscall $syscall
129 # And now, that the syscall has returned.
130 check_return_from_syscall $syscall
133 # Inserts a syscall catchpoint with an argument.
134 proc insert_catch_syscall_with_arg { syscall } {
137 # Trying to set the catchpoint
138 set thistest "catch syscall with arguments ($syscall)"
139 gdb_test "catch syscall $syscall" "Catchpoint .*(syscall (.)?${syscall}(.)?( \[\[0-9\]+\])?).*" $thistest
141 check_info_bp_specific_syscall $syscall
144 # Inserts a syscall catchpoint with many arguments.
145 proc insert_catch_syscall_with_many_args { syscalls numbers } {
147 set catch [ join $syscalls " " ]
150 foreach name $syscalls number $numbers {
151 set filter_str "${filter_str}'${name}' \[${number}\] "
154 set filter_str [ string trimright $filter_str " " ]
156 # Trying to set the catchpoint
157 set thistest "catch syscall with arguments ($filter_str)"
158 gdb_test "catch syscall $catch" "Catchpoint .*(syscalls (.)?${filter_str}(.)?).*" $thistest
160 check_info_bp_many_syscalls $syscalls
163 proc check_for_program_end {} {
166 # Deleting the catchpoints
169 set thistest "successful program end"
170 gdb_test "continue" "Program exited normally.*" $thistest
174 proc test_catch_syscall_without_args {} {
175 global gdb_prompt all_syscalls last_syscall
177 # Trying to set the syscall
178 set thistest "setting catch syscall without arguments"
179 gdb_test "catch syscall" "Catchpoint .*(syscall).*" $thistest
181 check_info_bp_any_syscall
183 # We have to check every syscall
184 foreach name $all_syscalls {
188 # At last but not least, we check if the inferior
189 # has called the last (exit) syscall.
190 check_call_to_syscall $last_syscall
192 # Now let's see if the inferior correctly finishes.
193 check_for_program_end
196 proc test_catch_syscall_with_args {} {
198 set syscall_name "close"
200 insert_catch_syscall_with_arg $syscall_name
202 # Can we continue until we catch the syscall?
203 check_continue $syscall_name
205 # Now let's see if the inferior correctly finishes.
206 check_for_program_end
209 proc test_catch_syscall_with_many_args {} {
210 global gdb_prompt all_syscalls all_syscalls_numbers
212 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
214 # Can we continue until we catch the syscalls?
215 foreach name $all_syscalls {
219 # Now let's see if the inferior correctly finishes.
220 check_for_program_end
223 proc test_catch_syscall_with_wrong_args {} {
225 # mlock is not called from the source
226 set syscall_name "mlock"
228 insert_catch_syscall_with_arg $syscall_name
230 # Now, we must verify if the program stops with a continue.
231 # If it doesn't, everything is right (since we don't have
232 # a syscall named "mlock" in it). Otherwise, this is a failure.
233 set thistest "catch syscall with unused syscall ($syscall_name)"
234 gdb_test "continue" "Program exited normally.*" $thistest
237 proc test_catch_syscall_restarting_inferior {} {
239 set syscall_name "chroot"
241 insert_catch_syscall_with_arg $syscall_name
243 # Let's first reach the call of the syscall.
244 check_call_to_syscall $syscall_name
246 # Now, restart the program
249 # And check for call/return
250 check_continue $syscall_name
253 check_for_program_end
256 proc test_catch_syscall_fail_nodatadir {} {
262 # Make sure GDB doesn't load the syscalls xml from the system data
264 gdb_test_no_output "set data-directory /the/path/to/nowhere"
266 # Testing to see if we receive a warning when calling "catch syscall"
267 # without XML support (without datadir).
268 set thistest "Catch syscall displays a warning when there is no XML support (no datadir set)"
269 gdb_test "catch syscall" "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).*" $thistest
271 # Since the catchpoint was set, we must check if it's present at
273 check_info_bp_any_syscall
279 proc do_syscall_tests {} {
280 global gdb_prompt srcdir
282 # NOTE: We don't have to point gdb at the correct data-directory.
283 # For the build tree that is handled by INTERNAL_GDBFLAGS.
285 # Verify that the 'catch syscall' help is available
286 set thistest "help catch syscall"
287 gdb_test "help catch syscall" "Catch system calls.*" $thistest
289 # Try to set a catchpoint to a nonsense syscall
290 set thistest "catch syscall to a nonsense syscall is prohibited"
291 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
293 # Testing the 'catch syscall' command without arguments.
294 # This test should catch any syscalls.
295 if [runto_main] then { test_catch_syscall_without_args }
297 # Testing the 'catch syscall' command with arguments.
298 # This test should only catch the specified syscall.
299 if [runto_main] then { test_catch_syscall_with_args }
301 # Testing the 'catch syscall' command with many arguments.
302 # This test should catch $all_syscalls.
303 if [runto_main] then { test_catch_syscall_with_many_args }
305 # Testing the 'catch syscall' command with WRONG arguments.
306 # This test should not trigger any catchpoints.
307 if [runto_main] then { test_catch_syscall_with_wrong_args }
309 # Testing the 'catch' syscall command during a restart of
311 if [runto_main] then { test_catch_syscall_restarting_inferior }
314 proc test_catch_syscall_without_args_noxml {} {
315 # We will need the syscall names even not using it
316 # because we need to know know many syscalls are in
318 global gdb_prompt all_syscalls last_syscall
322 set thistest "Catch syscall without arguments and without XML support"
323 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
325 # Now, we should be able to set a catchpoint,
326 # and GDB shall not display the warning anymore.
327 foreach name $all_syscalls {
328 # Unfortunately, we don't know the syscall number
329 # that will be caught because this information is
330 # arch-dependent. Thus, we try to catch anything
331 # similar to a number.
332 check_continue "\[0-9\]*"
335 # At last but not least, we check if the inferior
336 # has called the last (exit) syscall.
337 check_call_to_syscall "\[0-9\]*"
342 proc test_catch_syscall_with_args_noxml {} {
345 # The number of the "close" syscall. This is our
346 # option for a "long-estabilished" syscall in all
347 # Linux architectures, but unfortunately x86_64 and
348 # a few other platforms don't "follow the convention".
349 # Because of this, we need this ugly check :-(.
351 if { [istarget "x86_64-*-linux*"] } {
359 insert_catch_syscall_with_arg $close_number
361 check_continue $close_number
366 proc test_catch_syscall_with_wrong_args_noxml {} {
371 # Even without XML support, GDB should not accept unknown
372 # syscall names for the catchpoint.
373 set thistest "Catch a nonsense syscall without XML support"
374 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .nonsense_syscall.*" $thistest
379 proc do_syscall_tests_without_xml {} {
380 global gdb_prompt srcdir
382 # Make sure GDB doesn't load the syscalls xml from the system data
384 gdb_test_no_output "set data-directory /the/path/to/nowhere"
386 # Let's test if we can catch syscalls without XML support.
387 # We should succeed, but GDB is not supposed to print syscall names.
388 if [runto_main] then { test_catch_syscall_without_args_noxml }
390 # The only valid argument "catch syscall" should accept is the
391 # syscall number, and not the name (since it can't translate a
394 # It's worth mentioning that we only try to catch the syscall
395 # close(). This is because the syscall number is an arch-dependent
396 # information, so we can't assume that we know every syscall number
397 # in this system. Therefore, we have decided to use a "long-estabilished"
398 # system call, and close() just sounded the right choice :-).
399 if [runto_main] then { test_catch_syscall_with_args_noxml }
401 # Now, we'll try to provide a syscall name (valid or not) to the command,
402 # and expect it to fail.
403 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
406 # This procedure fills the vector "all_syscalls_numbers" with the proper
407 # numbers for the used syscalls according to the architecture.
408 proc fill_all_syscalls_numbers {} {
409 global all_syscalls_numbers
411 # For Linux on x86, PPC, PPC64, SPARC and SPARC64, the numbers for the syscalls
412 # "close" and "chroot" are the same.
413 if { [istarget "i\[34567\]86-*-linux*"]
414 || [istarget "powerpc-*-linux*"] || [istarget "powerpc64-*-linux*"]
415 || [istarget "sparc-*-linux*"] || [istarget "sparc64-*-linux*"] } {
416 set all_syscalls_numbers { "6" "61" }
420 # Start with a fresh gdb
423 set do_xml_test ![gdb_skip_xml_test]
425 gdb_reinitialize_dir $srcdir/$subdir
428 # Execute the tests, using XML support
432 # Now, we have to see if GDB displays a warning when we
433 # don't set the data-directory but try to use catch syscall
434 # anyway. For that, we must restart GDB first.
437 gdb_reinitialize_dir $srcdir/$subdir
439 test_catch_syscall_fail_nodatadir
446 gdb_reinitialize_dir $srcdir/$subdir
449 # Execute the tests, without XML support. In this case, GDB will
450 # only display syscall numbers, and not syscall names.
451 do_syscall_tests_without_xml