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