2010-05-24 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / catch-syscall.exp
CommitLineData
4c38e0a4 1# Copyright 1997, 1999, 2007, 2008, 2010 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
fbbe92c5
SDJ
26
27global srcfile
28set testfile "catch-syscall"
29set srcfile ${testfile}.c
30set binfile ${objdir}/${subdir}/${testfile}
31
32# All (but the last) syscalls from the example code
33# They are ordered according to the file, so do not change this.
34set all_syscalls { "close" "chroot" }
35set all_syscalls_numbers { }
36# The last syscall (exit()) does not return, so
37# we cannot expect the catchpoint to be triggered
38# twice. It is a special case.
39set last_syscall "exit_group"
40
41if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
42 untested catch-syscall.exp
43 return -1
44}
45
46# Until "catch syscall" is implemented on other targets...
47if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
48 continue
49}
50
51# This shall be updated whenever 'catch syscall' is implemented
52# on some architecture.
53#if { ![istarget "i\[34567\]86-*-linux*"]
54if { ![istarget "x86_64-*-linux*"] && ![istarget "i\[34567\]86-*-linux*"]
624f1a65
DM
55 && ![istarget "powerpc-*-linux*"] && ![istarget "powerpc64-*-linux*"]
56 && ![istarget "sparc-*-linux*"] && ![istarget "sparc64-*-linux*"] } {
fbbe92c5
SDJ
57 continue
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.
63proc 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.
75proc 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.
85proc 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.
100proc 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.
108proc 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.
117proc 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.
132proc 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.
143proc 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
161proc check_for_program_end {} {
162 global gdb_prompt
163
164 # Deleting the catchpoints
165 delete_breakpoints
166
167 set thistest "successful program end"
168 gdb_test "continue" "Program exited normally.*" $thistest
169
170}
171
172proc test_catch_syscall_without_args {} {
173 global gdb_prompt all_syscalls last_syscall
174
175 # Trying to set the syscall
176 set thistest "setting catch syscall without arguments"
177 gdb_test "catch syscall" "Catchpoint .*(syscall).*" $thistest
178
179 check_info_bp_any_syscall
180
181 # We have to check every syscall
182 foreach name $all_syscalls {
183 check_continue $name
184 }
185
186 # At last but not least, we check if the inferior
187 # has called the last (exit) syscall.
188 check_call_to_syscall $last_syscall
189
190 # Now let's see if the inferior correctly finishes.
191 check_for_program_end
192}
193
194proc test_catch_syscall_with_args {} {
195 global gdb_prompt
196 set syscall_name "close"
197
198 insert_catch_syscall_with_arg $syscall_name
199
200 # Can we continue until we catch the syscall?
201 check_continue $syscall_name
202
203 # Now let's see if the inferior correctly finishes.
204 check_for_program_end
205}
206
207proc test_catch_syscall_with_many_args {} {
208 global gdb_prompt all_syscalls all_syscalls_numbers
209
210 insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers
211
212 # Can we continue until we catch the syscalls?
213 foreach name $all_syscalls {
214 check_continue $name
215 }
216
217 # Now let's see if the inferior correctly finishes.
218 check_for_program_end
219}
220
221proc test_catch_syscall_with_wrong_args {} {
222 global gdb_prompt
223 # mlock is not called from the source
224 set syscall_name "mlock"
225
226 insert_catch_syscall_with_arg $syscall_name
227
228 # Now, we must verify if the program stops with a continue.
229 # If it doesn't, everything is right (since we don't have
230 # a syscall named "mlock" in it). Otherwise, this is a failure.
231 set thistest "catch syscall with unused syscall ($syscall_name)"
232 gdb_test "continue" "Program exited normally.*" $thistest
233}
234
235proc test_catch_syscall_restarting_inferior {} {
236 global gdb_prompt
237 set syscall_name "chroot"
238
239 insert_catch_syscall_with_arg $syscall_name
240
241 # Let's first reach the call of the syscall.
242 check_call_to_syscall $syscall_name
243
244 # Now, restart the program
245 rerun_to_main
246
247 # And check for call/return
248 check_continue $syscall_name
249
250 # Can we finish?
251 check_for_program_end
252}
253
bccd0dd2
SDJ
254proc test_catch_syscall_fail_nodatadir {} {
255 global gdb_prompt
256
257 # Sanitizing.
258 delete_breakpoints
259
fc30d5e0
PA
260 # Make sure GDB doesn't load the syscalls xml from the system data
261 # directory.
262 gdb_test "set data-directory /the/path/to/nowhere" ""
263
bccd0dd2
SDJ
264 # Testing to see if we receive a warning when calling "catch syscall"
265 # without XML support (without datadir).
266 set thistest "Catch syscall displays a warning when there is no XML support (no datadir set)"
267 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
268
269 # Since the catchpoint was set, we must check if it's present at
270 # "info breakpoints"
271 check_info_bp_any_syscall
272
273 # Sanitizing.
274 delete_breakpoints
275}
276
fbbe92c5
SDJ
277proc do_syscall_tests {} {
278 global gdb_prompt srcdir
279
280 # First, we need to set GDB datadir.
ad3986f0 281 gdb_test_no_output "set data-directory $srcdir/.."
fbbe92c5
SDJ
282
283 # Verify that the 'catch syscall' help is available
284 set thistest "help catch syscall"
285 gdb_test "help catch syscall" "Catch system calls.*" $thistest
286
287 # Try to set a catchpoint to a nonsense syscall
288 set thistest "catch syscall to a nonsense syscall is prohibited"
289 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest
290
291 # Testing the 'catch syscall' command without arguments.
292 # This test should catch any syscalls.
293 if [runto_main] then { test_catch_syscall_without_args }
294
295 # Testing the 'catch syscall' command with arguments.
296 # This test should only catch the specified syscall.
297 if [runto_main] then { test_catch_syscall_with_args }
298
299 # Testing the 'catch syscall' command with many arguments.
300 # This test should catch $all_syscalls.
301 if [runto_main] then { test_catch_syscall_with_many_args }
302
303 # Testing the 'catch syscall' command with WRONG arguments.
304 # This test should not trigger any catchpoints.
305 if [runto_main] then { test_catch_syscall_with_wrong_args }
306
307 # Testing the 'catch' syscall command during a restart of
308 # the inferior.
309 if [runto_main] then { test_catch_syscall_restarting_inferior }
310}
311
fbbe92c5
SDJ
312proc test_catch_syscall_without_args_noxml {} {
313 # We will need the syscall names even not using it
314 # because we need to know know many syscalls are in
315 # the example file.
316 global gdb_prompt all_syscalls last_syscall
317
318 delete_breakpoints
319
320 set thistest "Catch syscall without arguments and without XML support"
321 gdb_test "catch syscall" "Catchpoint .*(syscall).*"
322
323 # Now, we should be able to set a catchpoint,
324 # and GDB shall not display the warning anymore.
325 foreach name $all_syscalls {
326 # Unfortunately, we don't know the syscall number
327 # that will be caught because this information is
328 # arch-dependent. Thus, we try to catch anything
329 # similar to a number.
330 check_continue "\[0-9\]*"
331 }
332
333 # At last but not least, we check if the inferior
334 # has called the last (exit) syscall.
335 check_call_to_syscall "\[0-9\]*"
336
337 delete_breakpoints
338}
339
340proc test_catch_syscall_with_args_noxml {} {
341 global gdb_prompt
342
343 # The number of the "close" syscall. This is our
344 # option for a "long-estabilished" syscall in all
345 # Linux architectures, but unfortunately x86_64 and
346 # a few other platforms don't "follow the convention".
347 # Because of this, we need this ugly check :-(.
348 set close_number ""
349 if { [istarget "x86_64-*-linux*"] } {
350 set close_number "3"
351 } else {
352 set close_number "6"
353 }
354
355 delete_breakpoints
356
357 insert_catch_syscall_with_arg $close_number
358
359 check_continue $close_number
360
361 delete_breakpoints
362}
363
364proc test_catch_syscall_with_wrong_args_noxml {} {
365 global gdb_prompt
366
367 delete_breakpoints
368
369 # Even without XML support, GDB should not accept unknown
370 # syscall names for the catchpoint.
371 set thistest "Catch a nonsense syscall without XML support"
372 gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .nonsense_syscall.*" $thistest
373
374 delete_breakpoints
375}
376
377proc do_syscall_tests_without_xml {} {
378 global gdb_prompt srcdir
379
fc30d5e0
PA
380 # Make sure GDB doesn't load the syscalls xml from the system data
381 # directory.
382 gdb_test "set data-directory /the/path/to/nowhere" ""
fbbe92c5 383
bccd0dd2 384 # Let's test if we can catch syscalls without XML support.
fbbe92c5
SDJ
385 # We should succeed, but GDB is not supposed to print syscall names.
386 if [runto_main] then { test_catch_syscall_without_args_noxml }
387
388 # The only valid argument "catch syscall" should accept is the
389 # syscall number, and not the name (since it can't translate a
390 # name to a number).
391 #
392 # It's worth mentioning that we only try to catch the syscall
393 # close(). This is because the syscall number is an arch-dependent
394 # information, so we can't assume that we know every syscall number
395 # in this system. Therefore, we have decided to use a "long-estabilished"
396 # system call, and close() just sounded the right choice :-).
397 if [runto_main] then { test_catch_syscall_with_args_noxml }
398
399 # Now, we'll try to provide a syscall name (valid or not) to the command,
400 # and expect it to fail.
401 if [runto_main] then { test_catch_syscall_with_wrong_args_noxml }
402}
403
404# This procedure fills the vector "all_syscalls_numbers" with the proper
405# numbers for the used syscalls according to the architecture.
406proc fill_all_syscalls_numbers {} {
407 global all_syscalls_numbers
408
624f1a65
DM
409 # For Linux on x86, PPC, PPC64, SPARC and SPARC64, the numbers for the syscalls
410 # "close" and "chroot" are the same.
411 if { [istarget "i\[34567\]86-*-linux*"]
412 || [istarget "powerpc-*-linux*"] || [istarget "powerpc64-*-linux*"]
413 || [istarget "sparc-*-linux*"] || [istarget "sparc64-*-linux*"] } {
fbbe92c5
SDJ
414 set all_syscalls_numbers { "6" "61" }
415 }
416}
417
418# Start with a fresh gdb
419
420gdb_exit
bccd0dd2 421set do_xml_test ![gdb_skip_xml_test]
fbbe92c5
SDJ
422gdb_start
423gdb_reinitialize_dir $srcdir/$subdir
424gdb_load ${binfile}
425
426# Execute the tests, using XML support
bccd0dd2
SDJ
427if $do_xml_test {
428 do_syscall_tests
429
430 # Now, we have to see if GDB displays a warning when we
431 # don't set the data-directory but try to use catch syscall
432 # anyway. For that, we must restart GDB first.
433 gdb_exit
434 gdb_start
435 gdb_reinitialize_dir $srcdir/$subdir
436 gdb_load ${binfile}
437 test_catch_syscall_fail_nodatadir
438}
fbbe92c5
SDJ
439
440# Restart gdb
441
442gdb_exit
443gdb_start
444gdb_reinitialize_dir $srcdir/$subdir
445gdb_load ${binfile}
446
447# Execute the tests, without XML support. In this case, GDB will
448# only display syscall numbers, and not syscall names.
449do_syscall_tests_without_xml
This page took 0.133003 seconds and 4 git commands to generate.