]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/catch-syscall.exp
test suite update - gdb.base/[cd]
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / catch-syscall.exp
CommitLineData
28e7fd62 1# Copyright 1997-2013 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# Until "catch syscall" is implemented on other targets...
27if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
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*"]
37 && ![istarget "mips*-linux*"] } {
38 continue
39}
fbbe92c5 40
f76495c8 41standard_testfile
fbbe92c5
SDJ
42
43# All (but the last) syscalls from the example code
44# They are ordered according to the file, so do not change this.
45set all_syscalls { "close" "chroot" }
46set all_syscalls_numbers { }
47# The last syscall (exit()) does not return, so
48# we cannot expect the catchpoint to be triggered
49# twice. It is a special case.
50set last_syscall "exit_group"
51
52if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
53 untested catch-syscall.exp
54 return -1
55}
56
fbbe92c5
SDJ
57# Internal procedure used to check if, after issuing a 'catch syscall'
58# command (without arguments), the 'info breakpoints' command displays
59# that '"any syscall"' is to be caught.
60proc check_info_bp_any_syscall {} {
61 global gdb_prompt
62
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 } {
73 global gdb_prompt
74
75 set thistest "syscall(s) $syscall appears in 'info breakpoints'"
76 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest
77}
78
79# Internal procedure used to check if, after issuing a 'catch syscall X'
80# command (with many arguments), the 'info breakpoints' command displays
81# that the syscalls 'X' are to be caught.
82proc check_info_bp_many_syscalls { syscalls } {
83 global gdb_prompt
84 set filter_str ""
85
86 foreach name $syscalls {
87 set filter_str "${filter_str}${name}, "
88 }
89
90 set filter_str [ string trimright $filter_str ", " ]
91
92 set thistest "syscalls $filter_str appears in 'info breakpoints'"
93 gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest
94}
95
96# This procedure checks if there was a call to a syscall.
97proc check_call_to_syscall { syscall } {
98 global gdb_prompt
99
100 set thistest "program has called $syscall"
101 gdb_test "continue" "Catchpoint .*(call to syscall .?${syscall}.?).*" $thistest
102}
103
104# This procedure checks if the syscall returned.
105proc check_return_from_syscall { syscall } {
106 global gdb_prompt
107
108 set thistest "syscall $syscall has returned"
109 gdb_test "continue" "Catchpoint .*(returned from syscall (.)?${syscall}(.)?).*" $thistest
110}
111
112# Internal procedure that performs two 'continue' commands and checks if
113# a syscall call AND return occur.
114proc check_continue { syscall } {
115 global gdb_prompt
116
117 # Testing if the 'continue' stops at the
118 # specified syscall_name. If it does, then it should
119 # first print that the infeior has called the syscall,
120 # and after print that the syscall has returned.
121
122 # Testing if the inferiorr has called the syscall.
123 check_call_to_syscall $syscall
124 # And now, that the syscall has returned.
125 check_return_from_syscall $syscall
126}
127
128# Inserts a syscall catchpoint with an argument.
129proc insert_catch_syscall_with_arg { syscall } {
130 global gdb_prompt
131
132 # Trying to set the catchpoint
133 set thistest "catch syscall with arguments ($syscall)"
134 gdb_test "catch syscall $syscall" "Catchpoint .*(syscall (.)?${syscall}(.)?( \[\[0-9\]+\])?).*" $thistest
135
136 check_info_bp_specific_syscall $syscall
137}
138
139# Inserts a syscall catchpoint with many arguments.
140proc insert_catch_syscall_with_many_args { syscalls numbers } {
141 global gdb_prompt
142 set catch [ join $syscalls " " ]
143 set filter_str ""
144
145 foreach name $syscalls number $numbers {
146 set filter_str "${filter_str}'${name}' \[${number}\] "
147 }
148
149 set filter_str [ string trimright $filter_str " " ]
150
151 # Trying to set the catchpoint
152 set thistest "catch syscall with arguments ($filter_str)"
153 gdb_test "catch syscall $catch" "Catchpoint .*(syscalls (.)?${filter_str}(.)?).*" $thistest
154
155 check_info_bp_many_syscalls $syscalls
156}
157
158proc check_for_program_end {} {
159 global gdb_prompt
160
161 # Deleting the catchpoints
162 delete_breakpoints
163
fda326dd 164 gdb_continue_to_end
fbbe92c5
SDJ
165
166}
167
168proc test_catch_syscall_without_args {} {
169 global gdb_prompt all_syscalls last_syscall
170
171 # Trying to set the syscall
172 set thistest "setting catch syscall without arguments"
173 gdb_test "catch syscall" "Catchpoint .*(syscall).*" $thistest
174
175 check_info_bp_any_syscall
176
177 # We have to check every syscall
178 foreach name $all_syscalls {
179 check_continue $name
180 }
181
182 # At last but not least, we check if the inferior
183 # has called the last (exit) syscall.
184 check_call_to_syscall $last_syscall
185
186 # Now let's see if the inferior correctly finishes.
187 check_for_program_end
188}
189
190proc test_catch_syscall_with_args {} {
191 global gdb_prompt
192 set syscall_name "close"
193
194 insert_catch_syscall_with_arg $syscall_name
195
196 # Can we continue until we catch the syscall?
197 check_continue $syscall_name
198
199 # Now let's see if the inferior correctly finishes.
200 check_for_program_end
201}
202
203proc test_catch_syscall_with_many_args {} {
204 global gdb_prompt all_syscalls all_syscalls_numbers
205
206 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
207
208 # Can we continue until we catch the syscalls?
209 foreach name $all_syscalls {
210 check_continue $name
211 }
212
213 # Now let's see if the inferior correctly finishes.
214 check_for_program_end
215}
216
217proc test_catch_syscall_with_wrong_args {} {
218 global gdb_prompt
219 # mlock is not called from the source
220 set syscall_name "mlock"
221
222 insert_catch_syscall_with_arg $syscall_name
223
224 # Now, we must verify if the program stops with a continue.
225 # If it doesn't, everything is right (since we don't have
226 # a syscall named "mlock" in it). Otherwise, this is a failure.
227 set thistest "catch syscall with unused syscall ($syscall_name)"
fda326dd 228 gdb_continue_to_end $thistest
fbbe92c5
SDJ
229}
230
231proc test_catch_syscall_restarting_inferior {} {
232 global gdb_prompt
233 set syscall_name "chroot"
234
235 insert_catch_syscall_with_arg $syscall_name
236
237 # Let's first reach the call of the syscall.
238 check_call_to_syscall $syscall_name
239
240 # Now, restart the program
241 rerun_to_main
242
243 # And check for call/return
244 check_continue $syscall_name
245
246 # Can we finish?
247 check_for_program_end
248}
249
bccd0dd2
SDJ
250proc test_catch_syscall_fail_nodatadir {} {
251 global gdb_prompt
252
253 # Sanitizing.
254 delete_breakpoints
255
fc30d5e0
PA
256 # Make sure GDB doesn't load the syscalls xml from the system data
257 # directory.
27d3a1a2 258 gdb_test_no_output "set data-directory /the/path/to/nowhere"
fc30d5e0 259
bccd0dd2
SDJ
260 # Testing to see if we receive a warning when calling "catch syscall"
261 # without XML support (without datadir).
262 set thistest "Catch syscall displays a warning when there is no XML support (no datadir set)"
263 gdb_test "catch syscall" "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).*" $thistest
264
265 # Since the catchpoint was set, we must check if it's present at
266 # "info breakpoints"
267 check_info_bp_any_syscall
268
269 # Sanitizing.
270 delete_breakpoints
271}
272
fbbe92c5
SDJ
273proc do_syscall_tests {} {
274 global gdb_prompt srcdir
275
aae1c79a
DE
276 # NOTE: We don't have to point gdb at the correct data-directory.
277 # For the build tree that is handled by INTERNAL_GDBFLAGS.
fbbe92c5
SDJ
278
279 # Verify that the 'catch syscall' help is available
280 set thistest "help catch syscall"
281 gdb_test "help catch syscall" "Catch system calls.*" $thistest
282
283 # Try to set a catchpoint to a nonsense syscall
284 set thistest "catch syscall to a nonsense syscall is prohibited"
285 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
286
b45627a0
TT
287 # Regression test for syscall completer bug.
288 gdb_test "complete catch syscall close chroo" \
289 "catch syscall close chroot" \
290 "complete catch syscall with multiple words"
291
fbbe92c5
SDJ
292 # Testing the 'catch syscall' command without arguments.
293 # This test should catch any syscalls.
294 if [runto_main] then { test_catch_syscall_without_args }
295
296 # Testing the 'catch syscall' command with arguments.
297 # This test should only catch the specified syscall.
298 if [runto_main] then { test_catch_syscall_with_args }
299
300 # Testing the 'catch syscall' command with many arguments.
301 # This test should catch $all_syscalls.
302 if [runto_main] then { test_catch_syscall_with_many_args }
303
304 # Testing the 'catch syscall' command with WRONG arguments.
305 # This test should not trigger any catchpoints.
306 if [runto_main] then { test_catch_syscall_with_wrong_args }
307
308 # Testing the 'catch' syscall command during a restart of
309 # the inferior.
310 if [runto_main] then { test_catch_syscall_restarting_inferior }
311}
312
fbbe92c5
SDJ
313proc test_catch_syscall_without_args_noxml {} {
314 # We will need the syscall names even not using it
315 # because we need to know know many syscalls are in
316 # the example file.
317 global gdb_prompt all_syscalls last_syscall
318
319 delete_breakpoints
320
321 set thistest "Catch syscall without arguments and without XML support"
322 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
323
324 # Now, we should be able to set a catchpoint,
325 # and GDB shall not display the warning anymore.
326 foreach name $all_syscalls {
327 # Unfortunately, we don't know the syscall number
328 # that will be caught because this information is
329 # arch-dependent. Thus, we try to catch anything
330 # similar to a number.
331 check_continue "\[0-9\]*"
332 }
333
334 # At last but not least, we check if the inferior
335 # has called the last (exit) syscall.
336 check_call_to_syscall "\[0-9\]*"
337
338 delete_breakpoints
339}
340
341proc test_catch_syscall_with_args_noxml {} {
342 global gdb_prompt
343
344 # The number of the "close" syscall. This is our
345 # option for a "long-estabilished" syscall in all
346 # Linux architectures, but unfortunately x86_64 and
347 # a few other platforms don't "follow the convention".
348 # Because of this, we need this ugly check :-(.
349 set close_number ""
350 if { [istarget "x86_64-*-linux*"] } {
351 set close_number "3"
352 } else {
353 set close_number "6"
354 }
355
356 delete_breakpoints
357
358 insert_catch_syscall_with_arg $close_number
359
360 check_continue $close_number
361
362 delete_breakpoints
363}
364
365proc test_catch_syscall_with_wrong_args_noxml {} {
366 global gdb_prompt
367
368 delete_breakpoints
369
370 # Even without XML support, GDB should not accept unknown
371 # syscall names for the catchpoint.
372 set thistest "Catch a nonsense syscall without XML support"
373 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .nonsense_syscall.*" $thistest
374
375 delete_breakpoints
376}
377
378proc do_syscall_tests_without_xml {} {
379 global gdb_prompt srcdir
380
fc30d5e0
PA
381 # Make sure GDB doesn't load the syscalls xml from the system data
382 # directory.
27d3a1a2 383 gdb_test_no_output "set data-directory /the/path/to/nowhere"
fbbe92c5 384
bccd0dd2 385 # Let's test if we can catch syscalls without XML support.
fbbe92c5
SDJ
386 # We should succeed, but GDB is not supposed to print syscall names.
387 if [runto_main] then { test_catch_syscall_without_args_noxml }
388
389 # The only valid argument "catch syscall" should accept is the
390 # syscall number, and not the name (since it can't translate a
391 # name to a number).
392 #
393 # It's worth mentioning that we only try to catch the syscall
394 # close(). This is because the syscall number is an arch-dependent
395 # information, so we can't assume that we know every syscall number
396 # in this system. Therefore, we have decided to use a "long-estabilished"
397 # system call, and close() just sounded the right choice :-).
398 if [runto_main] then { test_catch_syscall_with_args_noxml }
399
400 # Now, we'll try to provide a syscall name (valid or not) to the command,
401 # and expect it to fail.
402 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
403}
404
405# This procedure fills the vector "all_syscalls_numbers" with the proper
406# numbers for the used syscalls according to the architecture.
407proc fill_all_syscalls_numbers {} {
408 global all_syscalls_numbers
409
624f1a65
DM
410 # For Linux on x86, PPC, PPC64, SPARC and SPARC64, the numbers for the syscalls
411 # "close" and "chroot" are the same.
412 if { [istarget "i\[34567\]86-*-linux*"]
413 || [istarget "powerpc-*-linux*"] || [istarget "powerpc64-*-linux*"]
414 || [istarget "sparc-*-linux*"] || [istarget "sparc64-*-linux*"] } {
fbbe92c5
SDJ
415 set all_syscalls_numbers { "6" "61" }
416 }
417}
418
419# Start with a fresh gdb
420
421gdb_exit
bccd0dd2 422set do_xml_test ![gdb_skip_xml_test]
fbbe92c5
SDJ
423gdb_start
424gdb_reinitialize_dir $srcdir/$subdir
425gdb_load ${binfile}
426
427# Execute the tests, using XML support
bccd0dd2
SDJ
428if $do_xml_test {
429 do_syscall_tests
430
431 # Now, we have to see if GDB displays a warning when we
432 # don't set the data-directory but try to use catch syscall
433 # anyway. For that, we must restart GDB first.
434 gdb_exit
435 gdb_start
436 gdb_reinitialize_dir $srcdir/$subdir
437 gdb_load ${binfile}
438 test_catch_syscall_fail_nodatadir
439}
fbbe92c5
SDJ
440
441# Restart gdb
442
443gdb_exit
444gdb_start
445gdb_reinitialize_dir $srcdir/$subdir
446gdb_load ${binfile}
447
448# Execute the tests, without XML support. In this case, GDB will
449# only display syscall numbers, and not syscall names.
450do_syscall_tests_without_xml