]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/catch-syscall.exp
Skip catch-syscall.exp on HP-UX target
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / catch-syscall.exp
1 # Copyright 1997-2015 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*"] } {
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
44 # They are ordered according to the file, so do not change this.
45 set all_syscalls { "close" "chroot" "pipe" "write" "read" }
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 # 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.
57 proc check_info_bp_any_syscall {} {
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.
67 proc check_info_bp_specific_syscall { syscall } {
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.
75 proc check_info_bp_many_syscalls { syscalls } {
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.
89 proc check_call_to_syscall { syscall } {
90 global decimal
91
92 set thistest "program has called $syscall"
93 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${syscall}.?\\).*" $thistest
94 }
95
96 # This procedure checks if the syscall returned.
97 proc check_return_from_syscall { syscall } {
98 global decimal
99
100 set thistest "syscall $syscall has returned"
101 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${syscall}\\).*" $thistest
102 }
103
104 # Internal procedure that performs two 'continue' commands and checks if
105 # a syscall call AND return occur.
106 proc check_continue { syscall } {
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
112 # Testing if the inferiorr has called the syscall.
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.
119 proc insert_catch_syscall_with_arg { syscall } {
120 global decimal
121
122 # Trying to set the catchpoint
123 set thistest "catch syscall with arguments ($syscall)"
124 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
125
126 check_info_bp_specific_syscall $syscall
127 }
128
129 # Inserts a syscall catchpoint with many arguments.
130 proc insert_catch_syscall_with_many_args { syscalls numbers } {
131 global decimal
132
133 set catch [ join $syscalls " " ]
134 set filter_str ""
135
136 foreach name $syscalls number $numbers {
137 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
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)"
144 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
145
146 check_info_bp_many_syscalls $syscalls
147 }
148
149 proc check_for_program_end {} {
150 # Deleting the catchpoints
151 delete_breakpoints
152
153 gdb_continue_to_end
154 }
155
156 proc test_catch_syscall_without_args {} {
157 global all_syscalls last_syscall decimal
158
159 with_test_prefix "without arguments" {
160 # Trying to set the syscall.
161 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
162
163 check_info_bp_any_syscall
164
165 # We have to check every syscall.
166 foreach name $all_syscalls {
167 check_continue $name
168 }
169
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
173
174 # Now let's see if the inferior correctly finishes.
175 check_for_program_end
176 }
177 }
178
179 proc test_catch_syscall_with_args {} {
180 with_test_prefix "with arguments" {
181 set syscall_name "close"
182 insert_catch_syscall_with_arg $syscall_name
183
184 # Can we continue until we catch the syscall?
185 check_continue $syscall_name
186
187 # Now let's see if the inferior correctly finishes.
188 check_for_program_end
189 }
190 }
191
192 proc test_catch_syscall_with_many_args {} {
193 with_test_prefix "with many arguments" {
194 global all_syscalls all_syscalls_numbers
195
196 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
197
198 # Can we continue until we catch the syscalls?
199 foreach name $all_syscalls {
200 check_continue $name
201 }
202
203 # Now let's see if the inferior correctly finishes.
204 check_for_program_end
205 }
206 }
207
208 proc test_catch_syscall_with_wrong_args {} {
209 with_test_prefix "wrong args" {
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 }
220 }
221
222 proc test_catch_syscall_restarting_inferior {} {
223 with_test_prefix "restarting inferior" {
224 set syscall_name "chroot"
225
226 with_test_prefix "entry" {
227 insert_catch_syscall_with_arg $syscall_name
228
229 # Let's first reach the entry of the syscall.
230 check_call_to_syscall $syscall_name
231 }
232
233 with_test_prefix "entry/return" {
234 # Now, restart the program.
235 rerun_to_main
236
237 # And check for entry/return.
238 check_continue $syscall_name
239
240 # Can we finish?
241 check_for_program_end
242 }
243 }
244 }
245
246 proc test_catch_syscall_fail_nodatadir {} {
247 with_test_prefix "fail no datadir" {
248 # Sanitizing.
249 delete_breakpoints
250
251 # Make sure GDB doesn't load the syscalls xml from the system
252 # data directory.
253 gdb_test "set data-directory /the/path/to/nowhere" \
254 "Warning: /the/path/to/nowhere: .*"
255
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
262
263 # Since the catchpoint was set, we must check if it's present
264 # in "info breakpoints" output.
265 check_info_bp_any_syscall
266
267 # Sanitizing.
268 delete_breakpoints
269 }
270 }
271
272 proc do_syscall_tests {} {
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.
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
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
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 }
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 }
312 }
313
314 proc test_catch_syscall_without_args_noxml {} {
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.
318 global all_syscalls last_syscall_number all_syscalls_numbers
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.
326 foreach name $all_syscalls number $all_syscalls_numbers {
327 with_test_prefix "$name" {
328 check_continue $number
329 }
330 }
331
332 # At last but not least, we check if the inferior has called
333 # the last (exit) syscall.
334 check_call_to_syscall $last_syscall_number
335
336 delete_breakpoints
337 }
338 }
339
340 proc test_catch_syscall_with_args_noxml {} {
341 with_test_prefix "with args noxml" {
342 global all_syscalls_numbers
343
344 delete_breakpoints
345
346 # Inserting all syscalls numbers to be caught
347 foreach syscall_number $all_syscalls_numbers {
348 insert_catch_syscall_with_arg $syscall_number
349 }
350
351 # Checking that all syscalls are caught.
352 foreach syscall_number $all_syscalls_numbers {
353 check_continue $syscall_number
354 }
355
356 delete_breakpoints
357 }
358 }
359
360 proc test_catch_syscall_with_wrong_args_noxml {} {
361 with_test_prefix "with wrong args noxml" {
362 delete_breakpoints
363
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.*"
368
369 delete_breakpoints
370 }
371 }
372
373 proc 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
405 } elseif { [istarget "s390*-linux*"] } {
406 set arch1 "s390:31-bit"
407 set arch2 "s390:64-bit"
408 set syscall1_name "_newselect"
409 set syscall2_name "select"
410 set syscall_number 142
411 }
412
413 with_test_prefix "multiple targets" {
414 # We are not interested in loading any binary here, and in
415 # some systems (PowerPC, for example), if we load a binary
416 # there is no way to set other architecture.
417 gdb_exit
418 gdb_start
419
420 gdb_test "set architecture $arch1" \
421 "The target architecture is assumed to be $arch1" \
422 "set arch to $arch1"
423
424 gdb_test "catch syscall $syscall_number" \
425 "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \
426 "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1"
427
428 gdb_test "set architecture $arch2" \
429 "The target architecture is assumed to be $arch2" \
430 "set arch to $arch2"
431
432 gdb_test "catch syscall $syscall_number" \
433 "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \
434 "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2"
435
436 clean_restart $binfile
437 }
438 }
439
440 proc do_syscall_tests_without_xml {} {
441 # Make sure GDB doesn't load the syscalls xml from the system data
442 # directory.
443 gdb_test "set data-directory /the/path/to/nowhere" \
444 "Warning: /the/path/to/nowhere: .*"
445
446 # Let's test if we can catch syscalls without XML support.
447 # We should succeed, but GDB is not supposed to print syscall names.
448 if [runto_main] then { test_catch_syscall_without_args_noxml }
449
450 # The only valid argument "catch syscall" should accept is the
451 # syscall number, and not the name (since it can't translate a
452 # name to a number).
453 if [runto_main] then { test_catch_syscall_with_args_noxml }
454
455 # Now, we'll try to provide a syscall name (valid or not) to the command,
456 # and expect it to fail.
457 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
458 }
459
460 # This procedure fills the vector "all_syscalls_numbers" with the proper
461 # numbers for the used syscalls according to the architecture.
462 proc fill_all_syscalls_numbers {} {
463 global all_syscalls_numbers last_syscall_number all_syscalls
464
465 foreach syscall $all_syscalls {
466 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
467 }
468
469 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
470 }
471
472 # Fill all the syscalls numbers before starting anything.
473 fill_all_syscalls_numbers
474
475 # Execute the tests, using XML support
476 if { ![gdb_skip_xml_test] } {
477 clean_restart $binfile
478 do_syscall_tests
479
480 # Now, we have to see if GDB displays a warning when we
481 # don't set the data-directory but try to use catch syscall
482 # anyway. For that, we must restart GDB first.
483 clean_restart $binfile
484 test_catch_syscall_fail_nodatadir
485 }
486
487 # Restart gdb
488 clean_restart $binfile
489
490 # Execute the tests, without XML support. In this case, GDB will
491 # only display syscall numbers, and not syscall names.
492 do_syscall_tests_without_xml