]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/catch-syscall.exp
Update Copyright year range in all files maintained by GDB.
[thirdparty/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*"]
9f948660 37 && ![istarget "mips*-linux*"] && ![istarget "arm*-linux*"] } {
2d4e0376
YQ
38 continue
39}
fbbe92c5 40
f76495c8 41standard_testfile
fbbe92c5 42
2e0d821f
SDJ
43if { [prepare_for_testing ${testfile}.exp $testfile ${testfile}.c] } {
44 untested catch-syscall.exp
45 return -1
46}
47
fbbe92c5
SDJ
48# All (but the last) syscalls from the example code
49# They are ordered according to the file, so do not change this.
4924df79 50set all_syscalls { "close" "chroot" "pipe" "write" "read" }
fbbe92c5 51set all_syscalls_numbers { }
2e0d821f 52
fbbe92c5
SDJ
53# The last syscall (exit()) does not return, so
54# we cannot expect the catchpoint to be triggered
55# twice. It is a special case.
56set last_syscall "exit_group"
2e0d821f 57set last_syscall_number { }
fbbe92c5 58
fbbe92c5
SDJ
59# Internal procedure used to check if, after issuing a 'catch syscall'
60# command (without arguments), the 'info breakpoints' command displays
61# that '"any syscall"' is to be caught.
62proc check_info_bp_any_syscall {} {
fbbe92c5
SDJ
63 # Verifying that the catchpoint appears in the 'info breakpoints'
64 # command, but with "<any syscall>".
65 set thistest "catch syscall appears in 'info breakpoints'"
66 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest
67}
68
69# Internal procedure used to check if, after issuing a 'catch syscall X'
70# command (with arguments), the 'info breakpoints' command displays
71# that the syscall 'X' is to be caught.
72proc check_info_bp_specific_syscall { syscall } {
fbbe92c5
SDJ
73 set thistest "syscall(s) $syscall appears in 'info breakpoints'"
74 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
75}
76
77# Internal procedure used to check if, after issuing a 'catch syscall X'
78# command (with many arguments), the 'info breakpoints' command displays
79# that the syscalls 'X' are to be caught.
80proc check_info_bp_many_syscalls { syscalls } {
fbbe92c5
SDJ
81 set filter_str ""
82
83 foreach name $syscalls {
84 set filter_str "${filter_str}${name}, "
85 }
86
87 set filter_str [ string trimright $filter_str ", " ]
88
89 set thistest "syscalls $filter_str appears in 'info breakpoints'"
90 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
91}
92
93# This procedure checks if there was a call to a syscall.
94proc check_call_to_syscall { syscall } {
2e0d821f 95 global decimal
fbbe92c5
SDJ
96
97 set thistest "program has called $syscall"
2e0d821f 98 gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${syscall}.?\\).*" $thistest
fbbe92c5
SDJ
99}
100
101# This procedure checks if the syscall returned.
102proc check_return_from_syscall { syscall } {
2e0d821f 103 global decimal
fbbe92c5
SDJ
104
105 set thistest "syscall $syscall has returned"
2e0d821f 106 gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${syscall}\\).*" $thistest
fbbe92c5
SDJ
107}
108
109# Internal procedure that performs two 'continue' commands and checks if
110# a syscall call AND return occur.
111proc check_continue { syscall } {
fbbe92c5
SDJ
112 # Testing if the 'continue' stops at the
113 # specified syscall_name. If it does, then it should
114 # first print that the infeior has called the syscall,
115 # and after print that the syscall has returned.
116
117 # Testing if the inferiorr has called the syscall.
118 check_call_to_syscall $syscall
119 # And now, that the syscall has returned.
120 check_return_from_syscall $syscall
121}
122
123# Inserts a syscall catchpoint with an argument.
124proc insert_catch_syscall_with_arg { syscall } {
2e0d821f 125 global decimal
fbbe92c5
SDJ
126
127 # Trying to set the catchpoint
128 set thistest "catch syscall with arguments ($syscall)"
2e0d821f 129 gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest
fbbe92c5
SDJ
130
131 check_info_bp_specific_syscall $syscall
132}
133
134# Inserts a syscall catchpoint with many arguments.
135proc insert_catch_syscall_with_many_args { syscalls numbers } {
2e0d821f
SDJ
136 global decimal
137
fbbe92c5
SDJ
138 set catch [ join $syscalls " " ]
139 set filter_str ""
140
141 foreach name $syscalls number $numbers {
2e0d821f 142 set filter_str "${filter_str}'${name}' \\\[${number}\\\] "
fbbe92c5
SDJ
143 }
144
145 set filter_str [ string trimright $filter_str " " ]
146
147 # Trying to set the catchpoint
148 set thistest "catch syscall with arguments ($filter_str)"
2e0d821f 149 gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest
fbbe92c5
SDJ
150
151 check_info_bp_many_syscalls $syscalls
152}
153
154proc check_for_program_end {} {
fbbe92c5
SDJ
155 # Deleting the catchpoints
156 delete_breakpoints
157
fda326dd 158 gdb_continue_to_end
fbbe92c5
SDJ
159}
160
161proc test_catch_syscall_without_args {} {
2e0d821f 162 global all_syscalls last_syscall decimal
fbbe92c5 163
eb4ca471
PA
164 with_test_prefix "without arguments" {
165 # Trying to set the syscall.
2e0d821f 166 gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)"
fbbe92c5 167
eb4ca471 168 check_info_bp_any_syscall
fbbe92c5 169
eb4ca471
PA
170 # We have to check every syscall.
171 foreach name $all_syscalls {
172 check_continue $name
173 }
fbbe92c5 174
eb4ca471
PA
175 # At last but not least, we check if the inferior has called
176 # the last (exit) syscall.
177 check_call_to_syscall $last_syscall
fbbe92c5 178
eb4ca471
PA
179 # Now let's see if the inferior correctly finishes.
180 check_for_program_end
181 }
fbbe92c5
SDJ
182}
183
184proc test_catch_syscall_with_args {} {
eb4ca471 185 with_test_prefix "with arguments" {
eb4ca471
PA
186 set syscall_name "close"
187 insert_catch_syscall_with_arg $syscall_name
fbbe92c5 188
eb4ca471
PA
189 # Can we continue until we catch the syscall?
190 check_continue $syscall_name
fbbe92c5 191
eb4ca471
PA
192 # Now let's see if the inferior correctly finishes.
193 check_for_program_end
194 }
fbbe92c5
SDJ
195}
196
197proc test_catch_syscall_with_many_args {} {
eb4ca471 198 with_test_prefix "with many arguments" {
2e0d821f 199 global all_syscalls all_syscalls_numbers
fbbe92c5 200
eb4ca471 201 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
fbbe92c5 202
eb4ca471
PA
203 # Can we continue until we catch the syscalls?
204 foreach name $all_syscalls {
205 check_continue $name
206 }
fbbe92c5 207
eb4ca471
PA
208 # Now let's see if the inferior correctly finishes.
209 check_for_program_end
210 }
fbbe92c5
SDJ
211}
212
213proc test_catch_syscall_with_wrong_args {} {
eb4ca471 214 with_test_prefix "wrong args" {
eb4ca471
PA
215 # mlock is not called from the source
216 set syscall_name "mlock"
217 insert_catch_syscall_with_arg $syscall_name
218
219 # Now, we must verify if the program stops with a continue.
220 # If it doesn't, everything is right (since we don't have
221 # a syscall named "mlock" in it). Otherwise, this is a failure.
222 set thistest "catch syscall with unused syscall ($syscall_name)"
223 gdb_continue_to_end $thistest
224 }
fbbe92c5
SDJ
225}
226
227proc test_catch_syscall_restarting_inferior {} {
eb4ca471 228 with_test_prefix "restarting inferior" {
eb4ca471 229 set syscall_name "chroot"
fbbe92c5 230
eb4ca471
PA
231 with_test_prefix "entry" {
232 insert_catch_syscall_with_arg $syscall_name
fbbe92c5 233
eb4ca471
PA
234 # Let's first reach the entry of the syscall.
235 check_call_to_syscall $syscall_name
236 }
fbbe92c5 237
eb4ca471
PA
238 with_test_prefix "entry/return" {
239 # Now, restart the program.
240 rerun_to_main
fbbe92c5 241
eb4ca471
PA
242 # And check for entry/return.
243 check_continue $syscall_name
fbbe92c5 244
eb4ca471
PA
245 # Can we finish?
246 check_for_program_end
247 }
248 }
fbbe92c5
SDJ
249}
250
bccd0dd2 251proc test_catch_syscall_fail_nodatadir {} {
eb4ca471 252 with_test_prefix "fail no datadir" {
eb4ca471
PA
253 # Sanitizing.
254 delete_breakpoints
bccd0dd2 255
eb4ca471
PA
256 # Make sure GDB doesn't load the syscalls xml from the system
257 # data directory.
258 gdb_test_no_output "set data-directory /the/path/to/nowhere"
fc30d5e0 259
eb4ca471
PA
260 # Testing to see if we receive a warning when calling "catch
261 # syscall" without XML support (without datadir).
262 set thistest "catch syscall displays a warning when there is no XML support"
263 gdb_test "catch syscall" \
264 "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).*" \
265 $thistest
bccd0dd2 266
eb4ca471
PA
267 # Since the catchpoint was set, we must check if it's present
268 # in "info breakpoints" output.
269 check_info_bp_any_syscall
bccd0dd2 270
eb4ca471
PA
271 # Sanitizing.
272 delete_breakpoints
273 }
bccd0dd2
SDJ
274}
275
fbbe92c5 276proc do_syscall_tests {} {
aae1c79a
DE
277 # NOTE: We don't have to point gdb at the correct data-directory.
278 # For the build tree that is handled by INTERNAL_GDBFLAGS.
fbbe92c5
SDJ
279
280 # Verify that the 'catch syscall' help is available
281 set thistest "help catch syscall"
282 gdb_test "help catch syscall" "Catch system calls.*" $thistest
283
284 # Try to set a catchpoint to a nonsense syscall
285 set thistest "catch syscall to a nonsense syscall is prohibited"
286 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
287
b45627a0
TT
288 # Regression test for syscall completer bug.
289 gdb_test "complete catch syscall close chroo" \
290 "catch syscall close chroot" \
291 "complete catch syscall with multiple words"
292
fbbe92c5
SDJ
293 # Testing the 'catch syscall' command without arguments.
294 # This test should catch any syscalls.
295 if [runto_main] then { test_catch_syscall_without_args }
296
297 # Testing the 'catch syscall' command with arguments.
298 # This test should only catch the specified syscall.
299 if [runto_main] then { test_catch_syscall_with_args }
300
301 # Testing the 'catch syscall' command with many arguments.
302 # This test should catch $all_syscalls.
303 if [runto_main] then { test_catch_syscall_with_many_args }
304
305 # Testing the 'catch syscall' command with WRONG arguments.
306 # This test should not trigger any catchpoints.
307 if [runto_main] then { test_catch_syscall_with_wrong_args }
308
309 # Testing the 'catch' syscall command during a restart of
310 # the inferior.
311 if [runto_main] then { test_catch_syscall_restarting_inferior }
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
373proc do_syscall_tests_without_xml {} {
fc30d5e0
PA
374 # Make sure GDB doesn't load the syscalls xml from the system data
375 # directory.
27d3a1a2 376 gdb_test_no_output "set data-directory /the/path/to/nowhere"
fbbe92c5 377
bccd0dd2 378 # Let's test if we can catch syscalls without XML support.
fbbe92c5
SDJ
379 # We should succeed, but GDB is not supposed to print syscall names.
380 if [runto_main] then { test_catch_syscall_without_args_noxml }
381
382 # The only valid argument "catch syscall" should accept is the
383 # syscall number, and not the name (since it can't translate a
384 # name to a number).
fbbe92c5
SDJ
385 if [runto_main] then { test_catch_syscall_with_args_noxml }
386
387 # Now, we'll try to provide a syscall name (valid or not) to the command,
388 # and expect it to fail.
389 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
390}
391
392# This procedure fills the vector "all_syscalls_numbers" with the proper
393# numbers for the used syscalls according to the architecture.
394proc fill_all_syscalls_numbers {} {
4924df79
GKB
395 global all_syscalls_numbers last_syscall_number all_syscalls
396
397 foreach syscall $all_syscalls {
398 lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1]
399 }
fbbe92c5 400
2e0d821f
SDJ
401 set last_syscall_number [get_integer_valueof "exit_group_syscall" -1]
402}
fbbe92c5 403
2e0d821f
SDJ
404# Fill all the syscalls numbers before starting anything.
405fill_all_syscalls_numbers
fbbe92c5
SDJ
406
407# Execute the tests, using XML support
2e0d821f
SDJ
408if { ![gdb_skip_xml_test] } {
409 clean_restart $binfile
bccd0dd2
SDJ
410 do_syscall_tests
411
412 # Now, we have to see if GDB displays a warning when we
413 # don't set the data-directory but try to use catch syscall
414 # anyway. For that, we must restart GDB first.
2e0d821f 415 clean_restart $binfile
bccd0dd2
SDJ
416 test_catch_syscall_fail_nodatadir
417}
fbbe92c5
SDJ
418
419# Restart gdb
2e0d821f 420clean_restart $binfile
fbbe92c5
SDJ
421
422# Execute the tests, without XML support. In this case, GDB will
423# only display syscall numbers, and not syscall names.
424do_syscall_tests_without_xml