Implement 'catch syscall' for gdbserver
[external/binutils.git] / gdb / testsuite / gdb.base / catch-syscall.exp
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
22 if { ![isnative] } then {
23     continue
24 }
25
26 # This shall be updated whenever 'catch syscall' is implemented
27 # on some architecture.
28 if { ![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
36 standard_testfile
37
38 if  { [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.
45 set all_syscalls { }
46 set 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.
51 set last_syscall "exit_group"
52 set last_syscall_number { }
53
54 set vfork_syscalls "(vfork|clone2?)"
55
56 set unknown_syscall_number { }
57
58 # Internal procedure used to check if, after issuing a 'catch syscall'
59 # command (without arguments), the 'info breakpoints' command displays
60 # that '"any syscall"' is to be caught.
61 proc check_info_bp_any_syscall {} {
62     # Verifying that the catchpoint appears in the 'info breakpoints'
63     # command, but with "<any syscall>".
64     set thistest "catch syscall appears in 'info breakpoints'"
65     gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest
66 }
67
68 # Internal procedure used to check if, after issuing a 'catch syscall X'
69 # command (with arguments), the 'info breakpoints' command displays
70 # that the syscall 'X' is to be caught.
71 proc check_info_bp_specific_syscall { syscall } {
72     set thistest "syscall(s) $syscall appears in 'info breakpoints'"
73     gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
74 }
75
76 # Internal procedure used to check if, after issuing a 'catch syscall X'
77 # command (with many arguments), the 'info breakpoints' command displays
78 # that the syscalls 'X' are to be caught.
79 proc check_info_bp_many_syscalls { syscalls } {
80     set filter_str ""
81
82     foreach name $syscalls {
83       set filter_str "${filter_str}${name}, "
84     }
85
86     set filter_str [ string trimright $filter_str ", " ]
87
88     set thistest "syscalls $filter_str appears in 'info breakpoints'"
89     gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
90 }
91
92 # This procedure checks if there was a call to a syscall.  The optional
93 # pattern can match syscalls that vary in implementation, like vfork.
94 proc check_call_to_syscall { syscall { pattern "" } } {
95     global decimal
96
97     if { $pattern eq "" } {
98       set pattern "${syscall}"
99     }
100
101     set thistest "program has called $syscall"
102     gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${pattern}.?\\).*" $thistest
103 }
104
105 # This procedure checks if the syscall returned.  The optional pattern
106 # can match syscalls that vary in implementation, like vfork.
107 proc check_return_from_syscall { syscall { pattern "" } } {
108     global decimal
109
110     if { $pattern eq "" } {
111       set pattern "${syscall}"
112     }
113
114     set thistest "syscall $syscall has returned"
115     gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" $thistest
116 }
117
118 # Internal procedure that performs two 'continue' commands and checks if
119 # a syscall call AND return occur.  The optional pattern can match
120 # syscalls that vary in implementation, like vfork.
121 proc check_continue { syscall { pattern "" } } {
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.
126
127     # Testing if the inferior has called the syscall.
128     check_call_to_syscall $syscall $pattern
129     # And now, that the syscall has returned.
130     check_return_from_syscall $syscall $pattern
131 }
132
133 # Inserts a syscall catchpoint with an argument.
134 proc insert_catch_syscall_with_arg { syscall } {
135     global decimal
136
137     # Trying to set the catchpoint
138     set thistest "catch syscall with arguments ($syscall)"
139     gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
140
141     check_info_bp_specific_syscall $syscall
142 }
143
144 # Inserts a syscall catchpoint with many arguments.
145 proc insert_catch_syscall_with_many_args { syscalls numbers } {
146     global decimal
147
148     set catch [ join $syscalls " " ]
149     set filter_str ""
150
151     foreach name $syscalls number $numbers {
152       set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
153     }
154
155     set filter_str [ string trimright $filter_str " " ]
156
157     # Trying to set the catchpoint
158     set thistest "catch syscall with arguments ($filter_str)"
159     gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
160
161     check_info_bp_many_syscalls $syscalls
162 }
163
164 proc check_for_program_end {} {
165     # Deleting the catchpoints
166     delete_breakpoints
167
168     gdb_continue_to_end
169 }
170
171 proc test_catch_syscall_without_args {} {
172     global all_syscalls last_syscall vfork_syscalls unknown_syscall_number decimal
173
174     with_test_prefix "without arguments" {
175         # Trying to set the syscall.
176         gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
177
178         check_info_bp_any_syscall
179
180         # We have to check every syscall.
181         foreach name $all_syscalls {
182             check_continue $name
183         }
184
185         check_continue "vfork" $vfork_syscalls
186
187         with_test_prefix "ENOSYS" {
188             check_continue $unknown_syscall_number
189         }
190
191         # At last but not least, we check if the inferior has called
192         # the last (exit) syscall.
193         check_call_to_syscall $last_syscall
194
195         # Now let's see if the inferior correctly finishes.
196         check_for_program_end
197     }
198 }
199
200 proc test_catch_syscall_with_args {} {
201     with_test_prefix "with arguments" {
202         set syscall_name "close"
203         insert_catch_syscall_with_arg $syscall_name
204
205         # Can we continue until we catch the syscall?
206         check_continue $syscall_name
207
208         # Now let's see if the inferior correctly finishes.
209         check_for_program_end
210     }
211 }
212
213 proc test_catch_syscall_with_many_args {} {
214     with_test_prefix "with many arguments" {
215         global all_syscalls all_syscalls_numbers
216
217         insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
218
219         # Can we continue until we catch the syscalls?
220         foreach name $all_syscalls {
221             check_continue $name
222         }
223
224         # Now let's see if the inferior correctly finishes.
225         check_for_program_end
226     }
227 }
228
229 proc test_catch_syscall_with_wrong_args {} {
230     with_test_prefix "wrong args" {
231         # mlock is not called from the source
232         set syscall_name "mlock"
233         insert_catch_syscall_with_arg $syscall_name
234
235         # Now, we must verify if the program stops with a continue.
236         # If it doesn't, everything is right (since we don't have
237         # a syscall named "mlock" in it).  Otherwise, this is a failure.
238         set thistest "catch syscall with unused syscall ($syscall_name)"
239         gdb_continue_to_end $thistest
240     }
241 }
242
243 proc test_catch_syscall_restarting_inferior {} {
244     with_test_prefix "restarting inferior" {
245         set syscall_name "chroot"
246
247         with_test_prefix "entry" {
248             insert_catch_syscall_with_arg $syscall_name
249
250             # Let's first reach the entry of the syscall.
251             check_call_to_syscall $syscall_name
252         }
253
254         with_test_prefix "entry/return" {
255             # Now, restart the program.
256             rerun_to_main
257
258             # And check for entry/return.
259             check_continue $syscall_name
260
261             # Can we finish?
262             check_for_program_end
263         }
264     }
265 }
266
267 proc test_catch_syscall_skipping_return {} {
268     with_test_prefix "skipping return" {
269         with_test_prefix "entry" {
270             set syscall_name "write"
271
272             insert_catch_syscall_with_arg $syscall_name
273
274             # Let's first reach the entry of the syscall.
275             check_call_to_syscall $syscall_name
276
277             # Now purposely skip the syscall return.
278             delete_breakpoints
279             gdb_test "stepi" ".*" "step over syscall return"
280         }
281
282         # With a naive entry/return toggle, gdb will still think
283         # the target is due for a syscall return.
284
285         with_test_prefix "entry/return" {
286             set syscall_name "read"
287
288             insert_catch_syscall_with_arg $syscall_name
289
290             # Check for entry first, then return.
291             check_continue $syscall_name
292
293             # Can we finish?
294             check_for_program_end
295         }
296     }
297 }
298
299 proc test_catch_syscall_mid_vfork {} {
300     global gdb_prompt decimal vfork_syscalls
301
302     with_test_prefix "mid-vfork" {
303         # Verify that the system supports "catch vfork".
304         gdb_test "catch vfork" "Catchpoint $decimal \\(vfork\\)" "insert first vfork catchpoint"
305         gdb_test_multiple "continue" "continue to first vfork catchpoint" {
306             -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
307                 unsupported "continue to first vfork catchpoint"
308                 return
309             }
310             -re ".*Catchpoint $decimal \\(vforked process $decimal\\).*$gdb_prompt $" {
311                 pass "continue to first vfork catchpoint"
312             }
313         }
314
315         # Check that we now reach vfork return only.
316         # (The actual syscall used varies by architecture.)
317         gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
318         check_return_from_syscall "vfork" $vfork_syscalls
319
320         # Can we finish?
321         check_for_program_end
322     }
323 }
324
325 proc test_catch_syscall_execve {} {
326     global gdb_prompt decimal
327
328     with_test_prefix "execve" {
329
330         # Tell the test program we want an execve.
331         gdb_test_no_output "set do_execve = 1"
332
333         # Check for entry/return across the execve, making sure that the
334         # syscall_state isn't lost when turning into a new process.
335         insert_catch_syscall_with_arg "execve"
336         check_continue "execve"
337
338         # Continue to main so extended-remote can read files as needed.
339         # (Otherwise that "Reading" output confuses gdb_continue_to_end.)
340         gdb_continue "main"
341
342         # Now can we finish?
343         check_for_program_end
344     }
345 }
346
347 proc test_catch_syscall_fail_nodatadir {} {
348     with_test_prefix "fail no datadir" {
349         # Sanitizing.
350         delete_breakpoints
351
352         # Make sure GDB doesn't load the syscalls xml from the system
353         # data directory.
354         gdb_test "set data-directory /the/path/to/nowhere" \
355             "Warning: /the/path/to/nowhere: .*"
356
357         # Testing to see if we receive a warning when calling "catch
358         # syscall" without XML support (without datadir).
359         set thistest "catch syscall displays a warning when there is no XML support"
360         gdb_test "catch syscall" \
361             "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).*" \
362             $thistest
363
364         # Since the catchpoint was set, we must check if it's present
365         # in "info breakpoints" output.
366         check_info_bp_any_syscall
367
368         # Sanitizing.
369         delete_breakpoints
370     }
371 }
372
373 proc do_syscall_tests {} {
374     # NOTE: We don't have to point gdb at the correct data-directory.
375     # For the build tree that is handled by INTERNAL_GDBFLAGS.
376
377     # Verify that the 'catch syscall' help is available
378     set thistest "help catch syscall"
379     gdb_test "help catch syscall" "Catch system calls.*" $thistest
380
381     # Try to set a catchpoint to a nonsense syscall
382     set thistest "catch syscall to a nonsense syscall is prohibited"
383     gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
384
385     # Regression test for syscall completer bug.
386     gdb_test "complete catch syscall close chroo" \
387         "catch syscall close chroot" \
388         "complete catch syscall with multiple words"
389
390     # Testing the 'catch syscall' command without arguments.
391     # This test should catch any syscalls.
392     if [runto_main] then { test_catch_syscall_without_args }
393
394     # Testing the 'catch syscall' command with arguments.
395     # This test should only catch the specified syscall.
396     if [runto_main] then { test_catch_syscall_with_args }
397
398     # Testing the 'catch syscall' command with many arguments.
399     # This test should catch $all_syscalls.
400     if [runto_main] then { test_catch_syscall_with_many_args }
401
402     # Testing the 'catch syscall' command with WRONG arguments.
403     # This test should not trigger any catchpoints.
404     if [runto_main] then { test_catch_syscall_with_wrong_args }
405
406     # Testing the 'catch syscall' command during a restart of
407     # the inferior.
408     if [runto_main] then { test_catch_syscall_restarting_inferior }
409
410     # Testing the 'catch syscall' command toggling off past a
411     # syscall return, then resuming entry/return as normal.
412     if [runto_main] then { test_catch_syscall_skipping_return }
413
414     # Testing the 'catch syscall' command starting mid-vfork.
415     if [runto_main] then { test_catch_syscall_mid_vfork }
416
417     # Testing that 'catch syscall' entry/return tracks across execve.
418     if [runto_main] then { test_catch_syscall_execve }
419
420     # Testing if the 'catch syscall' command works when switching to
421     # different architectures on-the-fly (PR gdb/10737).
422     if [runto_main] then { test_catch_syscall_multi_arch }
423 }
424
425 proc test_catch_syscall_without_args_noxml {} {
426     with_test_prefix "without args noxml" {
427         # We will need the syscall names even not using it because we
428         # need to know know many syscalls are in the example file.
429         global decimal all_syscalls last_syscall_number unknown_syscall_number all_syscalls_numbers
430
431         delete_breakpoints
432
433         gdb_test "catch syscall" "Catchpoint .*(syscall).*"
434
435         # Now, we should be able to set a catchpoint, and GDB shall
436         # not display the warning anymore.
437         foreach name $all_syscalls number $all_syscalls_numbers {
438             with_test_prefix "$name" {
439                 check_continue $number
440             }
441         }
442
443         check_continue "vfork" $decimal
444
445         with_test_prefix "ENOSYS" {
446             check_continue $unknown_syscall_number
447         }
448
449         # At last but not least, we check if the inferior has called
450         # the last (exit) syscall.
451         check_call_to_syscall $last_syscall_number
452
453         delete_breakpoints
454     }
455 }
456
457 proc test_catch_syscall_with_args_noxml {} {
458     with_test_prefix "with args noxml" {
459         global all_syscalls_numbers
460
461         delete_breakpoints
462
463         # Inserting all syscalls numbers to be caught
464         foreach syscall_number $all_syscalls_numbers {
465             insert_catch_syscall_with_arg $syscall_number
466         }
467
468         # Checking that all syscalls are caught.
469         foreach syscall_number $all_syscalls_numbers {
470             check_continue $syscall_number
471         }
472
473         delete_breakpoints
474     }
475 }
476
477 proc test_catch_syscall_with_wrong_args_noxml {} {
478     with_test_prefix "with wrong args noxml" {
479         delete_breakpoints
480
481         # Even without XML support, GDB should not accept unknown
482         # syscall names for the catchpoint.
483         gdb_test "catch syscall nonsense_syscall" \
484             "Unknown syscall name .nonsense_syscall.*"
485
486         delete_breakpoints
487     }
488 }
489
490 proc test_catch_syscall_multi_arch {} {
491     global decimal binfile
492
493     if { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } {
494         set arch1 "i386"
495         set arch2 "i386:x86-64"
496         set syscall1_name "exit"
497         set syscall2_name "write"
498         set syscall_number 1
499     } elseif { [istarget "powerpc-*-linux*"] \
500                    || [istarget "powerpc64-*-linux*"] } {
501         set arch1 "powerpc:common"
502         set arch2 "powerpc:common64"
503         set syscall1_name "openat"
504         set syscall2_name "unlinkat"
505         set syscall_number 286
506     } elseif { [istarget "sparc-*-linux*"] \
507                    || [istarget "sparc64-*-linux*"] } {
508         set arch1 "sparc"
509         set arch2 "sparc:v9"
510         set syscall1_name "setresuid32"
511         set syscall2_name "setresuid"
512         set syscall_number 108
513     } elseif { [istarget "mips*-linux*"] } {
514         # MIPS does not use the same numbers for syscalls on 32 and 64
515         # bits.
516         verbose "Not testing MIPS for multi-arch syscall support"
517         return
518     } elseif { [istarget "arm*-linux*"] } {
519         # catch syscall supports only 32-bit ARM for now.
520         verbose "Not testing ARM for multi-arch syscall support"
521         return
522     } elseif { [istarget "aarch64*-linux*"] } {
523         set arch1 "aarch64"
524         set arch2 "arm"
525         set syscall1_name "reboot"
526         set syscall2_name "_newselect"
527         set syscall_number 142
528     } elseif { [istarget "s390*-linux*"] } {
529         set arch1 "s390:31-bit"
530         set arch2 "s390:64-bit"
531         set syscall1_name "_newselect"
532         set syscall2_name "select"
533         set syscall_number 142
534     }
535
536     with_test_prefix "multiple targets" {
537         # We are not interested in loading any binary here, and in
538         # some systems (PowerPC, for example), if we load a binary
539         # there is no way to set other architecture.
540         gdb_exit
541         gdb_start
542
543         gdb_test "set architecture $arch1" \
544             "The target architecture is assumed to be $arch1" \
545             "set arch to $arch1"
546
547         gdb_test "catch syscall $syscall_number" \
548             "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \
549             "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1"
550
551         gdb_test "set architecture $arch2" \
552             "The target architecture is assumed to be $arch2" \
553             "set arch to $arch2"
554
555         gdb_test "catch syscall $syscall_number" \
556             "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \
557             "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2"
558
559         clean_restart $binfile
560     }
561 }
562
563 proc do_syscall_tests_without_xml {} {
564     # Make sure GDB doesn't load the syscalls xml from the system data
565     # directory.
566     gdb_test "set data-directory /the/path/to/nowhere" \
567         "Warning: /the/path/to/nowhere: .*"
568
569     # Let's test if we can catch syscalls without XML support.
570     # We should succeed, but GDB is not supposed to print syscall names.
571     if [runto_main] then { test_catch_syscall_without_args_noxml }
572
573     # The only valid argument "catch syscall" should accept is the
574     # syscall number, and not the name (since it can't translate a
575     # name to a number).
576     if [runto_main] then { test_catch_syscall_with_args_noxml }
577
578     # Now, we'll try to provide a syscall name (valid or not) to the command,
579     # and expect it to fail.
580     if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
581 }
582
583 # This procedure fills the vector "all_syscalls_numbers" with the proper
584 # numbers for the used syscalls according to the architecture.
585 proc fill_all_syscalls_numbers {} {
586     global all_syscalls_numbers last_syscall_number unknown_syscall_number all_syscalls
587
588     foreach syscall $all_syscalls {
589         lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
590     }
591
592     set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
593     set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1]
594 }
595
596 # Set up the vector all_syscalls.
597
598 proc setup_all_syscalls {} {
599     global all_syscalls
600     global gdb_prompt
601
602     # They are ordered according to the file, so do not change this.
603     lappend all_syscalls "close"
604     lappend all_syscalls "chroot"
605
606     # SYS_pipe doesn't exist on aarch64 kernel.
607     set test "check SYS_pipe"
608     gdb_test_multiple "p pipe_syscall" $test {
609         -re " = .*$gdb_prompt $" {
610             pass $test
611             lappend all_syscalls "pipe"
612         }
613         -re "No symbol .*$gdb_prompt $" {
614             pass $test
615             # SYS_pipe isn't defined, use SYS_pipe2 instead.
616             lappend all_syscalls "pipe2"
617         }
618     }
619
620     lappend all_syscalls "write"
621     lappend all_syscalls "read"
622 }
623
624 setup_all_syscalls
625
626 # Fill all the syscalls numbers before starting anything.
627 fill_all_syscalls_numbers
628
629 # Execute the tests, using XML support
630 gdb_exit
631 if { ![gdb_skip_xml_test] } {
632   clean_restart $binfile
633   do_syscall_tests
634
635   # Now, we have to see if GDB displays a warning when we
636   # don't set the data-directory but try to use catch syscall
637   # anyway.  For that, we must restart GDB first.
638   clean_restart $binfile
639   test_catch_syscall_fail_nodatadir
640 }
641
642 # Restart gdb
643 clean_restart $binfile
644
645 # Execute the tests, without XML support.  In this case, GDB will
646 # only display syscall numbers, and not syscall names.
647 do_syscall_tests_without_xml