Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / catch-syscall.exp
CommitLineData
32d0add0 1# Copyright 1997-2015 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
YQ
26# This shall be updated whenever 'catch syscall' is implemented
27# on some architecture.
2d4e0376
YQ
28if { ![istarget "x86_64-*-linux*"] && ![istarget "i\[34567\]86-*-linux*"]
29 && ![istarget "powerpc-*-linux*"] && ![istarget "powerpc64-*-linux*"]
30 && ![istarget "sparc-*-linux*"] && ![istarget "sparc64-*-linux*"]
237b092b 31 && ![istarget "mips*-linux*"] && ![istarget "arm*-linux*"]
f68f11b7 32 && ![istarget "s390*-linux*"] && ![istarget "aarch64*-*-linux*"] } {
2d4e0376
YQ
33 continue
34}
fbbe92c5 35
f76495c8 36standard_testfile
fbbe92c5 37
2e0d821f
SDJ
38if { [prepare_for_testing ${testfile}.exp $testfile ${testfile}.c] } {
39 untested catch-syscall.exp
40 return -1
41}
42
f68f11b7
YQ
43# All (but the last) syscalls from the example code. It is filled in
44# proc setup_all_syscalls.
45set all_syscalls { }
fbbe92c5 46set all_syscalls_numbers { }
2e0d821f 47
fbbe92c5
SDJ
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"
2e0d821f 52set last_syscall_number { }
fbbe92c5 53
fbbe92c5
SDJ
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 {} {
fbbe92c5
SDJ
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 } {
fbbe92c5
SDJ
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 } {
fbbe92c5
SDJ
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 } {
2e0d821f 90 global decimal
fbbe92c5
SDJ
91
92 set thistest "program has called $syscall"
2e0d821f 93 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${syscall}.?\\).*" $thistest
fbbe92c5
SDJ
94}
95
96# This procedure checks if the syscall returned.
97proc check_return_from_syscall { syscall } {
2e0d821f 98 global decimal
fbbe92c5
SDJ
99
100 set thistest "syscall $syscall has returned"
2e0d821f 101 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${syscall}\\).*" $thistest
fbbe92c5
SDJ
102}
103
104# Internal procedure that performs two 'continue' commands and checks if
105# a syscall call AND return occur.
106proc check_continue { syscall } {
fbbe92c5
SDJ
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
e03f9645 112 # Testing if the inferior has called the syscall.
fbbe92c5
SDJ
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 } {
2e0d821f 120 global decimal
fbbe92c5
SDJ
121
122 # Trying to set the catchpoint
123 set thistest "catch syscall with arguments ($syscall)"
2e0d821f 124 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
fbbe92c5
SDJ
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 } {
2e0d821f
SDJ
131 global decimal
132
fbbe92c5
SDJ
133 set catch [ join $syscalls " " ]
134 set filter_str ""
135
136 foreach name $syscalls number $numbers {
2e0d821f 137 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
fbbe92c5
SDJ
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)"
2e0d821f 144 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
fbbe92c5
SDJ
145
146 check_info_bp_many_syscalls $syscalls
147}
148
149proc check_for_program_end {} {
fbbe92c5
SDJ
150 # Deleting the catchpoints
151 delete_breakpoints
152
fda326dd 153 gdb_continue_to_end
fbbe92c5
SDJ
154}
155
156proc test_catch_syscall_without_args {} {
2e0d821f 157 global all_syscalls last_syscall decimal
fbbe92c5 158
eb4ca471
PA
159 with_test_prefix "without arguments" {
160 # Trying to set the syscall.
2e0d821f 161 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
fbbe92c5 162
eb4ca471 163 check_info_bp_any_syscall
fbbe92c5 164
eb4ca471
PA
165 # We have to check every syscall.
166 foreach name $all_syscalls {
167 check_continue $name
168 }
fbbe92c5 169
eb4ca471
PA
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
fbbe92c5 173
eb4ca471
PA
174 # Now let's see if the inferior correctly finishes.
175 check_for_program_end
176 }
fbbe92c5
SDJ
177}
178
179proc test_catch_syscall_with_args {} {
eb4ca471 180 with_test_prefix "with arguments" {
eb4ca471
PA
181 set syscall_name "close"
182 insert_catch_syscall_with_arg $syscall_name
fbbe92c5 183
eb4ca471
PA
184 # Can we continue until we catch the syscall?
185 check_continue $syscall_name
fbbe92c5 186
eb4ca471
PA
187 # Now let's see if the inferior correctly finishes.
188 check_for_program_end
189 }
fbbe92c5
SDJ
190}
191
192proc test_catch_syscall_with_many_args {} {
eb4ca471 193 with_test_prefix "with many arguments" {
2e0d821f 194 global all_syscalls all_syscalls_numbers
fbbe92c5 195
eb4ca471 196 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
fbbe92c5 197
eb4ca471
PA
198 # Can we continue until we catch the syscalls?
199 foreach name $all_syscalls {
200 check_continue $name
201 }
fbbe92c5 202
eb4ca471
PA
203 # Now let's see if the inferior correctly finishes.
204 check_for_program_end
205 }
fbbe92c5
SDJ
206}
207
208proc test_catch_syscall_with_wrong_args {} {
eb4ca471 209 with_test_prefix "wrong args" {
eb4ca471
PA
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 }
fbbe92c5
SDJ
220}
221
222proc test_catch_syscall_restarting_inferior {} {
eb4ca471 223 with_test_prefix "restarting inferior" {
eb4ca471 224 set syscall_name "chroot"
fbbe92c5 225
eb4ca471
PA
226 with_test_prefix "entry" {
227 insert_catch_syscall_with_arg $syscall_name
fbbe92c5 228
eb4ca471
PA
229 # Let's first reach the entry of the syscall.
230 check_call_to_syscall $syscall_name
231 }
fbbe92c5 232
eb4ca471
PA
233 with_test_prefix "entry/return" {
234 # Now, restart the program.
235 rerun_to_main
fbbe92c5 236
eb4ca471
PA
237 # And check for entry/return.
238 check_continue $syscall_name
fbbe92c5 239
eb4ca471
PA
240 # Can we finish?
241 check_for_program_end
242 }
243 }
fbbe92c5
SDJ
244}
245
bccd0dd2 246proc test_catch_syscall_fail_nodatadir {} {
eb4ca471 247 with_test_prefix "fail no datadir" {
eb4ca471
PA
248 # Sanitizing.
249 delete_breakpoints
bccd0dd2 250
eb4ca471
PA
251 # Make sure GDB doesn't load the syscalls xml from the system
252 # data directory.
8d551b02
DE
253 gdb_test "set data-directory /the/path/to/nowhere" \
254 "Warning: /the/path/to/nowhere: .*"
fc30d5e0 255
eb4ca471
PA
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
bccd0dd2 262
eb4ca471
PA
263 # Since the catchpoint was set, we must check if it's present
264 # in "info breakpoints" output.
265 check_info_bp_any_syscall
bccd0dd2 266
eb4ca471
PA
267 # Sanitizing.
268 delete_breakpoints
269 }
bccd0dd2
SDJ
270}
271
fbbe92c5 272proc do_syscall_tests {} {
aae1c79a
DE
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.
fbbe92c5
SDJ
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
b45627a0
TT
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
fbbe92c5
SDJ
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 }
458c8db8
SDJ
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 }
fbbe92c5
SDJ
312}
313
fbbe92c5 314proc test_catch_syscall_without_args_noxml {} {
eb4ca471
PA
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.
2e0d821f 318 global all_syscalls last_syscall_number all_syscalls_numbers
eb4ca471
PA
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.
2e0d821f 326 foreach name $all_syscalls number $all_syscalls_numbers {
eb4ca471 327 with_test_prefix "$name" {
2e0d821f 328 check_continue $number
eb4ca471
PA
329 }
330 }
331
332 # At last but not least, we check if the inferior has called
333 # the last (exit) syscall.
2e0d821f 334 check_call_to_syscall $last_syscall_number
eb4ca471
PA
335
336 delete_breakpoints
fbbe92c5 337 }
fbbe92c5
SDJ
338}
339
340proc test_catch_syscall_with_args_noxml {} {
eb4ca471 341 with_test_prefix "with args noxml" {
2e0d821f 342 global all_syscalls_numbers
fbbe92c5 343
eb4ca471 344 delete_breakpoints
fbbe92c5 345
2e0d821f
SDJ
346 # Inserting all syscalls numbers to be caught
347 foreach syscall_number $all_syscalls_numbers {
348 insert_catch_syscall_with_arg $syscall_number
349 }
fbbe92c5 350
2e0d821f
SDJ
351 # Checking that all syscalls are caught.
352 foreach syscall_number $all_syscalls_numbers {
353 check_continue $syscall_number
354 }
fbbe92c5 355
eb4ca471
PA
356 delete_breakpoints
357 }
fbbe92c5
SDJ
358}
359
360proc test_catch_syscall_with_wrong_args_noxml {} {
eb4ca471 361 with_test_prefix "with wrong args noxml" {
eb4ca471 362 delete_breakpoints
fbbe92c5 363
eb4ca471
PA
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.*"
fbbe92c5 368
eb4ca471
PA
369 delete_breakpoints
370 }
fbbe92c5
SDJ
371}
372
458c8db8
SDJ
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
f68f11b7 405 } elseif { [istarget "aarch64*-linux*"] } {
fbd8d50d
YQ
406 set arch1 "aarch64"
407 set arch2 "arm"
408 set syscall1_name "reboot"
409 set syscall2_name "_newselect"
410 set syscall_number 142
458c8db8 411 } elseif { [istarget "s390*-linux*"] } {
6d74a497 412 set arch1 "s390:31-bit"
458c8db8
SDJ
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
f68f11b7
YQ
478# Set up the vector all_syscalls.
479
480proc setup_all_syscalls {} {
481 global all_syscalls
482 global gdb_prompt
483
484 # They are ordered according to the file, so do not change this.
485 lappend all_syscalls "close"
486 lappend all_syscalls "chroot"
487
488 # SYS_pipe doesn't exist on aarch64 kernel.
489 set test "check SYS_pipe"
490 gdb_test_multiple "p pipe_syscall" $test {
491 -re " = .*$gdb_prompt $" {
492 pass $test
493 lappend all_syscalls "pipe"
494 }
495 -re "No symbol .*$gdb_prompt $" {
496 pass $test
497 # SYS_pipe isn't defined, use SYS_pipe2 instead.
498 lappend all_syscalls "pipe2"
499 }
500 }
501
502 lappend all_syscalls "write"
503 lappend all_syscalls "read"
504}
505
506setup_all_syscalls
507
2e0d821f
SDJ
508# Fill all the syscalls numbers before starting anything.
509fill_all_syscalls_numbers
fbbe92c5
SDJ
510
511# Execute the tests, using XML support
1e76a7e9 512gdb_exit
2e0d821f
SDJ
513if { ![gdb_skip_xml_test] } {
514 clean_restart $binfile
bccd0dd2
SDJ
515 do_syscall_tests
516
517 # Now, we have to see if GDB displays a warning when we
518 # don't set the data-directory but try to use catch syscall
519 # anyway. For that, we must restart GDB first.
2e0d821f 520 clean_restart $binfile
bccd0dd2
SDJ
521 test_catch_syscall_fail_nodatadir
522}
fbbe92c5
SDJ
523
524# Restart gdb
2e0d821f 525clean_restart $binfile
fbbe92c5
SDJ
526
527# Execute the tests, without XML support. In this case, GDB will
528# only display syscall numbers, and not syscall names.
529do_syscall_tests_without_xml
This page took 0.745135 seconds and 4 git commands to generate.