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