Fix compile error with use of 'typename' outside of template
[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 { [is_remote target] || ![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_fail_nodatadir {} {
326     with_test_prefix "fail no datadir" {
327         # Sanitizing.
328         delete_breakpoints
329
330         # Make sure GDB doesn't load the syscalls xml from the system
331         # data directory.
332         gdb_test "set data-directory /the/path/to/nowhere" \
333             "Warning: /the/path/to/nowhere: .*"
334
335         # Testing to see if we receive a warning when calling "catch
336         # syscall" without XML support (without datadir).
337         set thistest "catch syscall displays a warning when there is no XML support"
338         gdb_test "catch syscall" \
339             "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).*" \
340             $thistest
341
342         # Since the catchpoint was set, we must check if it's present
343         # in "info breakpoints" output.
344         check_info_bp_any_syscall
345
346         # Sanitizing.
347         delete_breakpoints
348     }
349 }
350
351 proc do_syscall_tests {} {
352     # NOTE: We don't have to point gdb at the correct data-directory.
353     # For the build tree that is handled by INTERNAL_GDBFLAGS.
354
355     # Verify that the 'catch syscall' help is available
356     set thistest "help catch syscall"
357     gdb_test "help catch syscall" "Catch system calls.*" $thistest
358
359     # Try to set a catchpoint to a nonsense syscall
360     set thistest "catch syscall to a nonsense syscall is prohibited"
361     gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
362
363     # Regression test for syscall completer bug.
364     gdb_test "complete catch syscall close chroo" \
365         "catch syscall close chroot" \
366         "complete catch syscall with multiple words"
367
368     # Testing the 'catch syscall' command without arguments.
369     # This test should catch any syscalls.
370     if [runto_main] then { test_catch_syscall_without_args }
371
372     # Testing the 'catch syscall' command with arguments.
373     # This test should only catch the specified syscall.
374     if [runto_main] then { test_catch_syscall_with_args }
375
376     # Testing the 'catch syscall' command with many arguments.
377     # This test should catch $all_syscalls.
378     if [runto_main] then { test_catch_syscall_with_many_args }
379
380     # Testing the 'catch syscall' command with WRONG arguments.
381     # This test should not trigger any catchpoints.
382     if [runto_main] then { test_catch_syscall_with_wrong_args }
383
384     # Testing the 'catch syscall' command during a restart of
385     # the inferior.
386     if [runto_main] then { test_catch_syscall_restarting_inferior }
387
388     # Testing the 'catch syscall' command toggling off past a
389     # syscall return, then resuming entry/return as normal.
390     if [runto_main] then { test_catch_syscall_skipping_return }
391
392     # Testing the 'catch syscall' command starting mid-vfork.
393     if [runto_main] then { test_catch_syscall_mid_vfork }
394
395     # Testing if the 'catch syscall' command works when switching to
396     # different architectures on-the-fly (PR gdb/10737).
397     if [runto_main] then { test_catch_syscall_multi_arch }
398 }
399
400 proc test_catch_syscall_without_args_noxml {} {
401     with_test_prefix "without args noxml" {
402         # We will need the syscall names even not using it because we
403         # need to know know many syscalls are in the example file.
404         global decimal all_syscalls last_syscall_number unknown_syscall_number all_syscalls_numbers
405
406         delete_breakpoints
407
408         gdb_test "catch syscall" "Catchpoint .*(syscall).*"
409
410         # Now, we should be able to set a catchpoint, and GDB shall
411         # not display the warning anymore.
412         foreach name $all_syscalls number $all_syscalls_numbers {
413             with_test_prefix "$name" {
414                 check_continue $number
415             }
416         }
417
418         check_continue "vfork" $decimal
419
420         with_test_prefix "ENOSYS" {
421             check_continue $unknown_syscall_number
422         }
423
424         # At last but not least, we check if the inferior has called
425         # the last (exit) syscall.
426         check_call_to_syscall $last_syscall_number
427
428         delete_breakpoints
429     }
430 }
431
432 proc test_catch_syscall_with_args_noxml {} {
433     with_test_prefix "with args noxml" {
434         global all_syscalls_numbers
435
436         delete_breakpoints
437
438         # Inserting all syscalls numbers to be caught
439         foreach syscall_number $all_syscalls_numbers {
440             insert_catch_syscall_with_arg $syscall_number
441         }
442
443         # Checking that all syscalls are caught.
444         foreach syscall_number $all_syscalls_numbers {
445             check_continue $syscall_number
446         }
447
448         delete_breakpoints
449     }
450 }
451
452 proc test_catch_syscall_with_wrong_args_noxml {} {
453     with_test_prefix "with wrong args noxml" {
454         delete_breakpoints
455
456         # Even without XML support, GDB should not accept unknown
457         # syscall names for the catchpoint.
458         gdb_test "catch syscall nonsense_syscall" \
459             "Unknown syscall name .nonsense_syscall.*"
460
461         delete_breakpoints
462     }
463 }
464
465 proc test_catch_syscall_multi_arch {} {
466     global decimal binfile
467
468     if { [istarget "i*86-*-*"] || [istarget "x86_64-*-*"] } {
469         set arch1 "i386"
470         set arch2 "i386:x86-64"
471         set syscall1_name "exit"
472         set syscall2_name "write"
473         set syscall_number 1
474     } elseif { [istarget "powerpc-*-linux*"] \
475                    || [istarget "powerpc64-*-linux*"] } {
476         set arch1 "powerpc:common"
477         set arch2 "powerpc:common64"
478         set syscall1_name "openat"
479         set syscall2_name "unlinkat"
480         set syscall_number 286
481     } elseif { [istarget "sparc-*-linux*"] \
482                    || [istarget "sparc64-*-linux*"] } {
483         set arch1 "sparc"
484         set arch2 "sparc:v9"
485         set syscall1_name "setresuid32"
486         set syscall2_name "setresuid"
487         set syscall_number 108
488     } elseif { [istarget "mips*-linux*"] } {
489         # MIPS does not use the same numbers for syscalls on 32 and 64
490         # bits.
491         verbose "Not testing MIPS for multi-arch syscall support"
492         return
493     } elseif { [istarget "arm*-linux*"] } {
494         # catch syscall supports only 32-bit ARM for now.
495         verbose "Not testing ARM for multi-arch syscall support"
496         return
497     } elseif { [istarget "aarch64*-linux*"] } {
498         set arch1 "aarch64"
499         set arch2 "arm"
500         set syscall1_name "reboot"
501         set syscall2_name "_newselect"
502         set syscall_number 142
503     } elseif { [istarget "s390*-linux*"] } {
504         set arch1 "s390:31-bit"
505         set arch2 "s390:64-bit"
506         set syscall1_name "_newselect"
507         set syscall2_name "select"
508         set syscall_number 142
509     }
510
511     with_test_prefix "multiple targets" {
512         # We are not interested in loading any binary here, and in
513         # some systems (PowerPC, for example), if we load a binary
514         # there is no way to set other architecture.
515         gdb_exit
516         gdb_start
517
518         gdb_test "set architecture $arch1" \
519             "The target architecture is assumed to be $arch1" \
520             "set arch to $arch1"
521
522         gdb_test "catch syscall $syscall_number" \
523             "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \
524             "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1"
525
526         gdb_test "set architecture $arch2" \
527             "The target architecture is assumed to be $arch2" \
528             "set arch to $arch2"
529
530         gdb_test "catch syscall $syscall_number" \
531             "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \
532             "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2"
533
534         clean_restart $binfile
535     }
536 }
537
538 proc do_syscall_tests_without_xml {} {
539     # Make sure GDB doesn't load the syscalls xml from the system data
540     # directory.
541     gdb_test "set data-directory /the/path/to/nowhere" \
542         "Warning: /the/path/to/nowhere: .*"
543
544     # Let's test if we can catch syscalls without XML support.
545     # We should succeed, but GDB is not supposed to print syscall names.
546     if [runto_main] then { test_catch_syscall_without_args_noxml }
547
548     # The only valid argument "catch syscall" should accept is the
549     # syscall number, and not the name (since it can't translate a
550     # name to a number).
551     if [runto_main] then { test_catch_syscall_with_args_noxml }
552
553     # Now, we'll try to provide a syscall name (valid or not) to the command,
554     # and expect it to fail.
555     if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
556 }
557
558 # This procedure fills the vector "all_syscalls_numbers" with the proper
559 # numbers for the used syscalls according to the architecture.
560 proc fill_all_syscalls_numbers {} {
561     global all_syscalls_numbers last_syscall_number unknown_syscall_number all_syscalls
562
563     foreach syscall $all_syscalls {
564         lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
565     }
566
567     set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
568     set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1]
569 }
570
571 # Set up the vector all_syscalls.
572
573 proc setup_all_syscalls {} {
574     global all_syscalls
575     global gdb_prompt
576
577     # They are ordered according to the file, so do not change this.
578     lappend all_syscalls "close"
579     lappend all_syscalls "chroot"
580
581     # SYS_pipe doesn't exist on aarch64 kernel.
582     set test "check SYS_pipe"
583     gdb_test_multiple "p pipe_syscall" $test {
584         -re " = .*$gdb_prompt $" {
585             pass $test
586             lappend all_syscalls "pipe"
587         }
588         -re "No symbol .*$gdb_prompt $" {
589             pass $test
590             # SYS_pipe isn't defined, use SYS_pipe2 instead.
591             lappend all_syscalls "pipe2"
592         }
593     }
594
595     lappend all_syscalls "write"
596     lappend all_syscalls "read"
597 }
598
599 setup_all_syscalls
600
601 # Fill all the syscalls numbers before starting anything.
602 fill_all_syscalls_numbers
603
604 # Execute the tests, using XML support
605 gdb_exit
606 if { ![gdb_skip_xml_test] } {
607   clean_restart $binfile
608   do_syscall_tests
609
610   # Now, we have to see if GDB displays a warning when we
611   # don't set the data-directory but try to use catch syscall
612   # anyway.  For that, we must restart GDB first.
613   clean_restart $binfile
614   test_catch_syscall_fail_nodatadir
615 }
616
617 # Restart gdb
618 clean_restart $binfile
619
620 # Execute the tests, without XML support.  In this case, GDB will
621 # only display syscall numbers, and not syscall names.
622 do_syscall_tests_without_xml