]>
Commit | Line | Data |
---|---|---|
1d506c26 | 1 | # Copyright 1997-2024 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 | ||
f76495c8 | 22 | standard_testfile |
fbbe92c5 | 23 | |
5b362f04 | 24 | if { [prepare_for_testing "failed to prepare" $testfile ${testfile}.c] } { |
2e0d821f SDJ |
25 | return -1 |
26 | } | |
27 | ||
a31d2f06 | 28 | # Check target supports catch syscall or not. |
65a33d75 | 29 | if {![runto_main]} { |
a31d2f06 YQ |
30 | return |
31 | } | |
32 | ||
33 | set test "catch syscall" | |
34 | gdb_test_multiple $test $test { | |
35 | -re "The feature \'catch syscall\' is not supported.*\r\n$gdb_prompt $" { | |
36 | unsupported "catch syscall isn't supported" | |
37 | return -1 | |
38 | } | |
39 | -re ".*$gdb_prompt $" { | |
40 | pass $test | |
41 | } | |
42 | } | |
43 | ||
44 | set test "check catch syscall" | |
45 | gdb_test_multiple "continue" $test { | |
46 | -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { | |
47 | unsupported "catch syscall isn't supported" | |
48 | return -1 | |
49 | } | |
50 | -re ".*Catchpoint.*$gdb_prompt $" { | |
51 | pass $test | |
52 | } | |
53 | } | |
54 | ||
0e857c82 TV |
55 | # Test-case for PR27313. Verify that negative syscall numbers are refused. |
56 | gdb_test "catch syscall -1" "Unknown syscall number '-1'\\." | |
57 | ||
f68f11b7 YQ |
58 | # All (but the last) syscalls from the example code. It is filled in |
59 | # proc setup_all_syscalls. | |
60 | set all_syscalls { } | |
fbbe92c5 | 61 | set all_syscalls_numbers { } |
2e0d821f | 62 | |
fbbe92c5 SDJ |
63 | # The last syscall (exit()) does not return, so |
64 | # we cannot expect the catchpoint to be triggered | |
65 | # twice. It is a special case. | |
cf622c39 | 66 | set last_syscall { } |
2e0d821f | 67 | set last_syscall_number { } |
fbbe92c5 | 68 | |
bfd09d20 JS |
69 | set vfork_syscalls "(vfork|clone2?)" |
70 | ||
71 | set unknown_syscall_number { } | |
72 | ||
fbbe92c5 SDJ |
73 | # Internal procedure used to check if, after issuing a 'catch syscall' |
74 | # command (without arguments), the 'info breakpoints' command displays | |
75 | # that '"any syscall"' is to be caught. | |
76 | proc check_info_bp_any_syscall {} { | |
fbbe92c5 SDJ |
77 | # Verifying that the catchpoint appears in the 'info breakpoints' |
78 | # command, but with "<any syscall>". | |
79 | set thistest "catch syscall appears in 'info breakpoints'" | |
80 | gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall \"<any syscall>\".*" $thistest | |
81 | } | |
82 | ||
83 | # Internal procedure used to check if, after issuing a 'catch syscall X' | |
84 | # command (with arguments), the 'info breakpoints' command displays | |
85 | # that the syscall 'X' is to be caught. | |
86 | proc check_info_bp_specific_syscall { syscall } { | |
fbbe92c5 SDJ |
87 | set thistest "syscall(s) $syscall appears in 'info breakpoints'" |
88 | gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscall(\[(\]s\[)\])? (.)?${syscall}(.)?.*" $thistest | |
89 | } | |
90 | ||
91 | # Internal procedure used to check if, after issuing a 'catch syscall X' | |
92 | # command (with many arguments), the 'info breakpoints' command displays | |
93 | # that the syscalls 'X' are to be caught. | |
94 | proc check_info_bp_many_syscalls { syscalls } { | |
fbbe92c5 SDJ |
95 | set filter_str "" |
96 | ||
97 | foreach name $syscalls { | |
98 | set filter_str "${filter_str}${name}, " | |
99 | } | |
100 | ||
101 | set filter_str [ string trimright $filter_str ", " ] | |
102 | ||
103 | set thistest "syscalls $filter_str appears in 'info breakpoints'" | |
104 | gdb_test "info breakpoints" ".*catchpoint.*keep y.*syscalls (.)?${filter_str}(.)?.*" $thistest | |
105 | } | |
106 | ||
bfd09d20 JS |
107 | # This procedure checks if there was a call to a syscall. The optional |
108 | # pattern can match syscalls that vary in implementation, like vfork. | |
109 | proc check_call_to_syscall { syscall { pattern "" } } { | |
2e0d821f | 110 | global decimal |
fbbe92c5 | 111 | |
bfd09d20 JS |
112 | if { $pattern eq "" } { |
113 | set pattern "${syscall}" | |
114 | } | |
115 | ||
fbbe92c5 | 116 | set thistest "program has called $syscall" |
bfd09d20 | 117 | gdb_test "continue" "Catchpoint $decimal \\(call to syscall .?${pattern}.?\\).*" $thistest |
fbbe92c5 SDJ |
118 | } |
119 | ||
bfd09d20 JS |
120 | # This procedure checks if the syscall returned. The optional pattern |
121 | # can match syscalls that vary in implementation, like vfork. | |
122 | proc check_return_from_syscall { syscall { pattern "" } } { | |
2e0d821f | 123 | global decimal |
fbbe92c5 | 124 | |
bfd09d20 JS |
125 | if { $pattern eq "" } { |
126 | set pattern "${syscall}" | |
127 | } | |
128 | ||
fbbe92c5 | 129 | set thistest "syscall $syscall has returned" |
408db576 CL |
130 | if { $pattern eq "execve" } { |
131 | gdb_test_multiple "continue" $thistest { | |
132 | -re -wrap "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" { | |
133 | pass $thistest | |
134 | return 1 | |
135 | } | |
136 | -re -wrap ".*Breakpoint $decimal, main .*" { | |
137 | # On Powerpc the kernel does not report the returned from | |
138 | # syscall as expected by the test. GDB bugzilla 28623. | |
139 | if { [istarget "powerpc64*-linux*"] } { | |
140 | xfail $thistest | |
141 | } else { | |
142 | fail $thistest | |
143 | } | |
144 | return 0 | |
145 | } | |
146 | } | |
147 | ||
148 | } else { | |
149 | gdb_test "continue" "Catchpoint $decimal \\(returned from syscall ${pattern}\\).*" $thistest | |
150 | return 1 | |
151 | } | |
fbbe92c5 SDJ |
152 | } |
153 | ||
154 | # Internal procedure that performs two 'continue' commands and checks if | |
bfd09d20 JS |
155 | # a syscall call AND return occur. The optional pattern can match |
156 | # syscalls that vary in implementation, like vfork. | |
157 | proc check_continue { syscall { pattern "" } } { | |
fbbe92c5 SDJ |
158 | # Testing if the 'continue' stops at the |
159 | # specified syscall_name. If it does, then it should | |
160 | # first print that the infeior has called the syscall, | |
161 | # and after print that the syscall has returned. | |
162 | ||
e03f9645 | 163 | # Testing if the inferior has called the syscall. |
bfd09d20 | 164 | check_call_to_syscall $syscall $pattern |
fbbe92c5 | 165 | # And now, that the syscall has returned. |
408db576 | 166 | return [check_return_from_syscall $syscall $pattern] |
fbbe92c5 SDJ |
167 | } |
168 | ||
169 | # Inserts a syscall catchpoint with an argument. | |
170 | proc insert_catch_syscall_with_arg { syscall } { | |
2e0d821f | 171 | global decimal |
fbbe92c5 SDJ |
172 | |
173 | # Trying to set the catchpoint | |
174 | set thistest "catch syscall with arguments ($syscall)" | |
2e0d821f | 175 | gdb_test "catch syscall $syscall" "Catchpoint $decimal \\(syscall \'?${syscall}\'?( \[${decimal}\])?\\)" $thistest |
fbbe92c5 SDJ |
176 | |
177 | check_info_bp_specific_syscall $syscall | |
178 | } | |
179 | ||
180 | # Inserts a syscall catchpoint with many arguments. | |
181 | proc insert_catch_syscall_with_many_args { syscalls numbers } { | |
2e0d821f SDJ |
182 | global decimal |
183 | ||
fbbe92c5 SDJ |
184 | set catch [ join $syscalls " " ] |
185 | set filter_str "" | |
186 | ||
187 | foreach name $syscalls number $numbers { | |
2e0d821f | 188 | set filter_str "${filter_str}'${name}' \\\[${number}\\\] " |
fbbe92c5 SDJ |
189 | } |
190 | ||
191 | set filter_str [ string trimright $filter_str " " ] | |
192 | ||
193 | # Trying to set the catchpoint | |
194 | set thistest "catch syscall with arguments ($filter_str)" | |
2e0d821f | 195 | gdb_test "catch syscall $catch" "Catchpoint $decimal \\(syscalls ${filter_str}\\).*" $thistest |
fbbe92c5 SDJ |
196 | |
197 | check_info_bp_many_syscalls $syscalls | |
198 | } | |
199 | ||
200 | proc check_for_program_end {} { | |
fbbe92c5 SDJ |
201 | # Deleting the catchpoints |
202 | delete_breakpoints | |
203 | ||
f67c0c91 | 204 | gdb_continue_to_end "" continue 1 |
fbbe92c5 SDJ |
205 | } |
206 | ||
207 | proc test_catch_syscall_without_args {} { | |
bfd09d20 | 208 | global all_syscalls last_syscall vfork_syscalls unknown_syscall_number decimal |
fbbe92c5 | 209 | |
eb4ca471 PA |
210 | with_test_prefix "without arguments" { |
211 | # Trying to set the syscall. | |
2e0d821f | 212 | gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)" |
fbbe92c5 | 213 | |
eb4ca471 | 214 | check_info_bp_any_syscall |
fbbe92c5 | 215 | |
eb4ca471 PA |
216 | # We have to check every syscall. |
217 | foreach name $all_syscalls { | |
218 | check_continue $name | |
219 | } | |
fbbe92c5 | 220 | |
bfd09d20 JS |
221 | check_continue "vfork" $vfork_syscalls |
222 | ||
223 | with_test_prefix "ENOSYS" { | |
224 | check_continue $unknown_syscall_number | |
225 | } | |
226 | ||
eb4ca471 PA |
227 | # At last but not least, we check if the inferior has called |
228 | # the last (exit) syscall. | |
229 | check_call_to_syscall $last_syscall | |
fbbe92c5 | 230 | |
eb4ca471 PA |
231 | # Now let's see if the inferior correctly finishes. |
232 | check_for_program_end | |
233 | } | |
fbbe92c5 SDJ |
234 | } |
235 | ||
236 | proc test_catch_syscall_with_args {} { | |
eb4ca471 | 237 | with_test_prefix "with arguments" { |
eb4ca471 PA |
238 | set syscall_name "close" |
239 | insert_catch_syscall_with_arg $syscall_name | |
fbbe92c5 | 240 | |
eb4ca471 PA |
241 | # Can we continue until we catch the syscall? |
242 | check_continue $syscall_name | |
fbbe92c5 | 243 | |
eb4ca471 PA |
244 | # Now let's see if the inferior correctly finishes. |
245 | check_for_program_end | |
246 | } | |
fbbe92c5 SDJ |
247 | } |
248 | ||
249 | proc test_catch_syscall_with_many_args {} { | |
eb4ca471 | 250 | with_test_prefix "with many arguments" { |
2e0d821f | 251 | global all_syscalls all_syscalls_numbers |
fbbe92c5 | 252 | |
eb4ca471 | 253 | insert_catch_syscall_with_many_args $all_syscalls $all_syscalls_numbers |
fbbe92c5 | 254 | |
eb4ca471 PA |
255 | # Can we continue until we catch the syscalls? |
256 | foreach name $all_syscalls { | |
257 | check_continue $name | |
258 | } | |
fbbe92c5 | 259 | |
eb4ca471 PA |
260 | # Now let's see if the inferior correctly finishes. |
261 | check_for_program_end | |
262 | } | |
fbbe92c5 SDJ |
263 | } |
264 | ||
265 | proc test_catch_syscall_with_wrong_args {} { | |
eb4ca471 | 266 | with_test_prefix "wrong args" { |
eb4ca471 PA |
267 | # mlock is not called from the source |
268 | set syscall_name "mlock" | |
269 | insert_catch_syscall_with_arg $syscall_name | |
270 | ||
271 | # Now, we must verify if the program stops with a continue. | |
272 | # If it doesn't, everything is right (since we don't have | |
273 | # a syscall named "mlock" in it). Otherwise, this is a failure. | |
274 | set thistest "catch syscall with unused syscall ($syscall_name)" | |
f67c0c91 | 275 | gdb_continue_to_end $thistest continue 1 |
eb4ca471 | 276 | } |
fbbe92c5 SDJ |
277 | } |
278 | ||
279 | proc test_catch_syscall_restarting_inferior {} { | |
eb4ca471 | 280 | with_test_prefix "restarting inferior" { |
eb4ca471 | 281 | set syscall_name "chroot" |
fbbe92c5 | 282 | |
eb4ca471 PA |
283 | with_test_prefix "entry" { |
284 | insert_catch_syscall_with_arg $syscall_name | |
fbbe92c5 | 285 | |
eb4ca471 PA |
286 | # Let's first reach the entry of the syscall. |
287 | check_call_to_syscall $syscall_name | |
288 | } | |
fbbe92c5 | 289 | |
eb4ca471 PA |
290 | with_test_prefix "entry/return" { |
291 | # Now, restart the program. | |
292 | rerun_to_main | |
fbbe92c5 | 293 | |
eb4ca471 PA |
294 | # And check for entry/return. |
295 | check_continue $syscall_name | |
fbbe92c5 | 296 | |
eb4ca471 PA |
297 | # Can we finish? |
298 | check_for_program_end | |
299 | } | |
300 | } | |
fbbe92c5 SDJ |
301 | } |
302 | ||
bfd09d20 JS |
303 | proc test_catch_syscall_skipping_return {} { |
304 | with_test_prefix "skipping return" { | |
305 | with_test_prefix "entry" { | |
306 | set syscall_name "write" | |
307 | ||
308 | insert_catch_syscall_with_arg $syscall_name | |
309 | ||
310 | # Let's first reach the entry of the syscall. | |
311 | check_call_to_syscall $syscall_name | |
312 | ||
313 | # Now purposely skip the syscall return. | |
314 | delete_breakpoints | |
315 | gdb_test "stepi" ".*" "step over syscall return" | |
316 | } | |
317 | ||
318 | # With a naive entry/return toggle, gdb will still think | |
319 | # the target is due for a syscall return. | |
320 | ||
321 | with_test_prefix "entry/return" { | |
322 | set syscall_name "read" | |
323 | ||
324 | insert_catch_syscall_with_arg $syscall_name | |
325 | ||
326 | # Check for entry first, then return. | |
327 | check_continue $syscall_name | |
328 | ||
329 | # Can we finish? | |
330 | check_for_program_end | |
331 | } | |
332 | } | |
333 | } | |
334 | ||
335 | proc test_catch_syscall_mid_vfork {} { | |
336 | global gdb_prompt decimal vfork_syscalls | |
337 | ||
338 | with_test_prefix "mid-vfork" { | |
339 | # Verify that the system supports "catch vfork". | |
340 | gdb_test "catch vfork" "Catchpoint $decimal \\(vfork\\)" "insert first vfork catchpoint" | |
341 | gdb_test_multiple "continue" "continue to first vfork catchpoint" { | |
342 | -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { | |
343 | unsupported "continue to first vfork catchpoint" | |
344 | return | |
345 | } | |
346 | -re ".*Catchpoint $decimal \\(vforked process $decimal\\).*$gdb_prompt $" { | |
347 | pass "continue to first vfork catchpoint" | |
348 | } | |
349 | } | |
350 | ||
351 | # Check that we now reach vfork return only. | |
352 | # (The actual syscall used varies by architecture.) | |
353 | gdb_test "catch syscall" "Catchpoint $decimal \\(any syscall\\)" | |
354 | check_return_from_syscall "vfork" $vfork_syscalls | |
355 | ||
356 | # Can we finish? | |
357 | check_for_program_end | |
358 | } | |
359 | } | |
360 | ||
82075af2 JS |
361 | proc test_catch_syscall_execve {} { |
362 | global gdb_prompt decimal | |
363 | ||
364 | with_test_prefix "execve" { | |
365 | ||
366 | # Tell the test program we want an execve. | |
367 | gdb_test_no_output "set do_execve = 1" | |
368 | ||
369 | # Check for entry/return across the execve, making sure that the | |
370 | # syscall_state isn't lost when turning into a new process. | |
371 | insert_catch_syscall_with_arg "execve" | |
408db576 CL |
372 | if [check_continue "execve"] { |
373 | # The check_continue test generates an XFAIL on Powerpc. In | |
374 | # that case, gdb is already at main so don't do the continue. | |
375 | ||
82075af2 | 376 | |
408db576 CL |
377 | # Continue to main so extended-remote can read files as needed. |
378 | # (Otherwise that "Reading" output confuses gdb_continue_to_end.) | |
379 | gdb_continue "main" | |
380 | } | |
82075af2 JS |
381 | |
382 | # Now can we finish? | |
383 | check_for_program_end | |
384 | } | |
385 | } | |
386 | ||
bccd0dd2 | 387 | proc test_catch_syscall_fail_nodatadir {} { |
eb4ca471 | 388 | with_test_prefix "fail no datadir" { |
eb4ca471 PA |
389 | # Sanitizing. |
390 | delete_breakpoints | |
bccd0dd2 | 391 | |
eb4ca471 PA |
392 | # Make sure GDB doesn't load the syscalls xml from the system |
393 | # data directory. | |
8d551b02 | 394 | gdb_test "set data-directory /the/path/to/nowhere" \ |
3d38b301 | 395 | "warning: /the/path/to/nowhere: .*" |
fc30d5e0 | 396 | |
eb4ca471 PA |
397 | # Testing to see if we receive a warning when calling "catch |
398 | # syscall" without XML support (without datadir). | |
399 | set thistest "catch syscall displays a warning when there is no XML support" | |
400 | gdb_test "catch syscall" \ | |
401 | "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).*" \ | |
402 | $thistest | |
bccd0dd2 | 403 | |
eb4ca471 PA |
404 | # Since the catchpoint was set, we must check if it's present |
405 | # in "info breakpoints" output. | |
406 | check_info_bp_any_syscall | |
bccd0dd2 | 407 | |
eb4ca471 PA |
408 | # Sanitizing. |
409 | delete_breakpoints | |
410 | } | |
bccd0dd2 SDJ |
411 | } |
412 | ||
e3487908 GKB |
413 | proc test_catch_syscall_group {} { |
414 | global decimal | |
415 | ||
416 | set sysnum "\\\[${decimal}\\\]" | |
417 | ||
418 | gdb_test "catch syscall g:process" \ | |
419 | "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*" \ | |
420 | "set catchpoint on a group of syscalls" | |
421 | ||
422 | gdb_test "catch syscall group:process read" \ | |
423 | "Catchpoint $decimal \\(syscalls (\'(clone|fork|execve|exit)\' $sysnum)+.*read.*\\)" \ | |
424 | "set catchpoints on a group of syscalls and on a single syscall" | |
425 | ||
426 | gdb_test "catch syscall group:" \ | |
427 | "Unknown syscall group ''\." \ | |
428 | "set catchpoints on an invalid group" | |
429 | ||
430 | gdb_test "catch syscall g:junk" \ | |
431 | "Unknown syscall group 'junk'\." \ | |
432 | "set catchpoints on an unknown group." | |
433 | ||
434 | gdb_test "complete catch syscall g:proc" \ | |
435 | "catch syscall g:process" \ | |
436 | "complete catch syscall group with 'g:' prefix" | |
437 | ||
438 | gdb_test "complete catch syscall group:proc" \ | |
439 | "catch syscall group:process" \ | |
440 | "complete catch syscall group with 'group:' prefix" | |
441 | ||
442 | gdb_test_sequence "complete catch syscall g" \ | |
443 | "complete catch syscall group suggests 'group:' prefix" { | |
444 | "group:descriptor" "group:file" "group:ipc" "group:memory" | |
445 | "group:network" "group:process" "group:signal" | |
446 | } | |
447 | } | |
448 | ||
fbbe92c5 | 449 | proc do_syscall_tests {} { |
aae1c79a DE |
450 | # NOTE: We don't have to point gdb at the correct data-directory. |
451 | # For the build tree that is handled by INTERNAL_GDBFLAGS. | |
fbbe92c5 SDJ |
452 | |
453 | # Verify that the 'catch syscall' help is available | |
11af934b | 454 | gdb_test "help catch syscall" "Catch system calls.*" |
fbbe92c5 SDJ |
455 | |
456 | # Try to set a catchpoint to a nonsense syscall | |
457 | set thistest "catch syscall to a nonsense syscall is prohibited" | |
458 | gdb_test "catch syscall nonsense_syscall" "Unknown syscall name .*" $thistest | |
459 | ||
b45627a0 TT |
460 | # Regression test for syscall completer bug. |
461 | gdb_test "complete catch syscall close chroo" \ | |
462 | "catch syscall close chroot" \ | |
463 | "complete catch syscall with multiple words" | |
464 | ||
fbbe92c5 SDJ |
465 | # Testing the 'catch syscall' command without arguments. |
466 | # This test should catch any syscalls. | |
65a33d75 | 467 | if {[runto_main]} { test_catch_syscall_without_args } |
fbbe92c5 SDJ |
468 | |
469 | # Testing the 'catch syscall' command with arguments. | |
470 | # This test should only catch the specified syscall. | |
65a33d75 | 471 | if {[runto_main]} { test_catch_syscall_with_args } |
fbbe92c5 SDJ |
472 | |
473 | # Testing the 'catch syscall' command with many arguments. | |
474 | # This test should catch $all_syscalls. | |
65a33d75 | 475 | if {[runto_main]} { test_catch_syscall_with_many_args } |
fbbe92c5 SDJ |
476 | |
477 | # Testing the 'catch syscall' command with WRONG arguments. | |
478 | # This test should not trigger any catchpoints. | |
65a33d75 | 479 | if {[runto_main]} { test_catch_syscall_with_wrong_args } |
fbbe92c5 | 480 | |
bfd09d20 | 481 | # Testing the 'catch syscall' command during a restart of |
fbbe92c5 | 482 | # the inferior. |
65a33d75 | 483 | if {[runto_main]} { test_catch_syscall_restarting_inferior } |
458c8db8 | 484 | |
bfd09d20 JS |
485 | # Testing the 'catch syscall' command toggling off past a |
486 | # syscall return, then resuming entry/return as normal. | |
65a33d75 | 487 | if {[runto_main]} { test_catch_syscall_skipping_return } |
bfd09d20 JS |
488 | |
489 | # Testing the 'catch syscall' command starting mid-vfork. | |
65a33d75 | 490 | if {[runto_main]} { test_catch_syscall_mid_vfork } |
bfd09d20 | 491 | |
82075af2 | 492 | # Testing that 'catch syscall' entry/return tracks across execve. |
65a33d75 | 493 | if {[runto_main]} { test_catch_syscall_execve } |
82075af2 | 494 | |
458c8db8 SDJ |
495 | # Testing if the 'catch syscall' command works when switching to |
496 | # different architectures on-the-fly (PR gdb/10737). | |
cf622c39 | 497 | if {[istarget *-linux*] && [runto_main]} { test_catch_syscall_multi_arch } |
e3487908 GKB |
498 | |
499 | # Testing the 'catch' syscall command for a group of syscalls. | |
65a33d75 | 500 | if {[runto_main]} { test_catch_syscall_group } |
fbbe92c5 SDJ |
501 | } |
502 | ||
fbbe92c5 | 503 | proc test_catch_syscall_without_args_noxml {} { |
eb4ca471 PA |
504 | with_test_prefix "without args noxml" { |
505 | # We will need the syscall names even not using it because we | |
506 | # need to know know many syscalls are in the example file. | |
bfd09d20 | 507 | global decimal all_syscalls last_syscall_number unknown_syscall_number all_syscalls_numbers |
eb4ca471 PA |
508 | |
509 | delete_breakpoints | |
510 | ||
511 | gdb_test "catch syscall" "Catchpoint .*(syscall).*" | |
512 | ||
513 | # Now, we should be able to set a catchpoint, and GDB shall | |
514 | # not display the warning anymore. | |
2e0d821f | 515 | foreach name $all_syscalls number $all_syscalls_numbers { |
eb4ca471 | 516 | with_test_prefix "$name" { |
2e0d821f | 517 | check_continue $number |
eb4ca471 PA |
518 | } |
519 | } | |
520 | ||
bfd09d20 JS |
521 | check_continue "vfork" $decimal |
522 | ||
523 | with_test_prefix "ENOSYS" { | |
524 | check_continue $unknown_syscall_number | |
525 | } | |
526 | ||
eb4ca471 PA |
527 | # At last but not least, we check if the inferior has called |
528 | # the last (exit) syscall. | |
2e0d821f | 529 | check_call_to_syscall $last_syscall_number |
eb4ca471 PA |
530 | |
531 | delete_breakpoints | |
fbbe92c5 | 532 | } |
fbbe92c5 SDJ |
533 | } |
534 | ||
535 | proc test_catch_syscall_with_args_noxml {} { | |
eb4ca471 | 536 | with_test_prefix "with args noxml" { |
2e0d821f | 537 | global all_syscalls_numbers |
fbbe92c5 | 538 | |
eb4ca471 | 539 | delete_breakpoints |
fbbe92c5 | 540 | |
2e0d821f SDJ |
541 | # Inserting all syscalls numbers to be caught |
542 | foreach syscall_number $all_syscalls_numbers { | |
543 | insert_catch_syscall_with_arg $syscall_number | |
544 | } | |
fbbe92c5 | 545 | |
2e0d821f SDJ |
546 | # Checking that all syscalls are caught. |
547 | foreach syscall_number $all_syscalls_numbers { | |
548 | check_continue $syscall_number | |
549 | } | |
fbbe92c5 | 550 | |
eb4ca471 PA |
551 | delete_breakpoints |
552 | } | |
fbbe92c5 SDJ |
553 | } |
554 | ||
555 | proc test_catch_syscall_with_wrong_args_noxml {} { | |
eb4ca471 | 556 | with_test_prefix "with wrong args noxml" { |
eb4ca471 | 557 | delete_breakpoints |
fbbe92c5 | 558 | |
eb4ca471 PA |
559 | # Even without XML support, GDB should not accept unknown |
560 | # syscall names for the catchpoint. | |
561 | gdb_test "catch syscall nonsense_syscall" \ | |
562 | "Unknown syscall name .nonsense_syscall.*" | |
fbbe92c5 | 563 | |
eb4ca471 PA |
564 | delete_breakpoints |
565 | } | |
fbbe92c5 SDJ |
566 | } |
567 | ||
e21d8399 TV |
568 | proc test_catch_syscall_multi_arch_1 { |
569 | arch1 arch2 syscall1_name syscall2_name syscall_number | |
570 | } { | |
b00d6678 | 571 | global decimal |
458c8db8 | 572 | |
e21d8399 | 573 | with_test_prefix "multiple targets: $arch1 vs $arch2" { |
458c8db8 SDJ |
574 | # We are not interested in loading any binary here, and in |
575 | # some systems (PowerPC, for example), if we load a binary | |
576 | # there is no way to set other architecture. | |
577 | gdb_exit | |
578 | gdb_start | |
579 | ||
e21d8399 TV |
580 | set supported 1 |
581 | foreach arch [list $arch1 $arch2] { | |
582 | gdb_test_multiple "set architecture $arch" "" { | |
583 | -re -wrap "Undefined item: \"$arch\"\\." { | |
584 | set supported 0 | |
585 | unsupported $gdb_test_name | |
586 | } | |
587 | -re -wrap "The target architecture is set to \"$arch\"\\." { | |
588 | } | |
589 | } | |
590 | } | |
591 | if { $supported == 0 } { | |
592 | return | |
593 | } | |
594 | ||
458c8db8 | 595 | gdb_test "set architecture $arch1" \ |
ccb9eba6 | 596 | "The target architecture is set to \"$arch1\"\\." |
458c8db8 SDJ |
597 | |
598 | gdb_test "catch syscall $syscall_number" \ | |
599 | "Catchpoint $decimal \\(syscall .${syscall1_name}. \\\[${syscall_number}\\\]\\)" \ | |
600 | "insert catch syscall on syscall $syscall_number -- $syscall1_name on $arch1" | |
601 | ||
602 | gdb_test "set architecture $arch2" \ | |
ccb9eba6 | 603 | "The target architecture is set to \"$arch2\"\\." |
458c8db8 SDJ |
604 | |
605 | gdb_test "catch syscall $syscall_number" \ | |
606 | "Catchpoint $decimal \\(syscall .${syscall2_name}. \\\[${syscall_number}\\\]\\)" \ | |
607 | "insert catch syscall on syscall $syscall_number -- $syscall2_name on $arch2" | |
458c8db8 SDJ |
608 | } |
609 | } | |
610 | ||
e21d8399 | 611 | proc test_catch_syscall_multi_arch {} { |
b00d6678 TV |
612 | global binfile |
613 | ||
e21d8399 TV |
614 | set arch1 "i386" |
615 | set arch2 "i386:x86-64" | |
616 | set syscall1_name "exit" | |
617 | set syscall2_name "write" | |
618 | set syscall_number 1 | |
619 | test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \ | |
620 | $syscall2_name $syscall_number | |
621 | ||
622 | set arch1 "powerpc:common" | |
623 | set arch2 "powerpc:common64" | |
37b506d3 TV |
624 | set syscall1_name "fstatat64" |
625 | set syscall2_name "newfstatat" | |
626 | set syscall_number 291 | |
e21d8399 TV |
627 | test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \ |
628 | $syscall2_name $syscall_number | |
629 | ||
630 | set arch1 "sparc" | |
631 | set arch2 "sparc:v9" | |
632 | set syscall1_name "setresuid32" | |
633 | set syscall2_name "setresuid" | |
634 | set syscall_number 108 | |
635 | test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \ | |
636 | $syscall2_name $syscall_number | |
637 | ||
638 | set arch1 "aarch64" | |
639 | set arch2 "arm" | |
640 | set syscall1_name "reboot" | |
641 | set syscall2_name "_newselect" | |
642 | set syscall_number 142 | |
643 | test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \ | |
644 | $syscall2_name $syscall_number | |
645 | ||
646 | set arch1 "s390:31-bit" | |
647 | set arch2 "s390:64-bit" | |
648 | set syscall1_name "_newselect" | |
649 | set syscall2_name "select" | |
650 | set syscall_number 142 | |
651 | test_catch_syscall_multi_arch_1 $arch1 $arch2 $syscall1_name \ | |
652 | $syscall2_name $syscall_number | |
b00d6678 TV |
653 | |
654 | clean_restart $binfile | |
e21d8399 TV |
655 | } |
656 | ||
fbbe92c5 | 657 | proc do_syscall_tests_without_xml {} { |
fc30d5e0 PA |
658 | # Make sure GDB doesn't load the syscalls xml from the system data |
659 | # directory. | |
8d551b02 | 660 | gdb_test "set data-directory /the/path/to/nowhere" \ |
3d38b301 | 661 | "warning: /the/path/to/nowhere: .*" |
fbbe92c5 | 662 | |
bccd0dd2 | 663 | # Let's test if we can catch syscalls without XML support. |
fbbe92c5 | 664 | # We should succeed, but GDB is not supposed to print syscall names. |
65a33d75 | 665 | if {[runto_main]} { test_catch_syscall_without_args_noxml } |
fbbe92c5 SDJ |
666 | |
667 | # The only valid argument "catch syscall" should accept is the | |
668 | # syscall number, and not the name (since it can't translate a | |
669 | # name to a number). | |
65a33d75 | 670 | if {[runto_main]} { test_catch_syscall_with_args_noxml } |
fbbe92c5 SDJ |
671 | |
672 | # Now, we'll try to provide a syscall name (valid or not) to the command, | |
673 | # and expect it to fail. | |
65a33d75 | 674 | if {[runto_main]} { test_catch_syscall_with_wrong_args_noxml } |
fbbe92c5 SDJ |
675 | } |
676 | ||
677 | # This procedure fills the vector "all_syscalls_numbers" with the proper | |
678 | # numbers for the used syscalls according to the architecture. | |
679 | proc fill_all_syscalls_numbers {} { | |
cf622c39 | 680 | global all_syscalls_numbers unknown_syscall_number all_syscalls |
4924df79 GKB |
681 | |
682 | foreach syscall $all_syscalls { | |
683 | lappend all_syscalls_numbers [get_integer_valueof "${syscall}_syscall" -1] | |
684 | } | |
fbbe92c5 | 685 | |
bfd09d20 | 686 | set unknown_syscall_number [get_integer_valueof "unknown_syscall" -1] |
2e0d821f | 687 | } |
fbbe92c5 | 688 | |
5463a15c | 689 | # Set up the vector all_syscalls. Returns 1 upon success, 0 upon failure. |
f68f11b7 YQ |
690 | |
691 | proc setup_all_syscalls {} { | |
692 | global all_syscalls | |
693 | global gdb_prompt | |
5463a15c | 694 | global decimal |
cf622c39 | 695 | global last_syscall last_syscall_number |
f68f11b7 YQ |
696 | |
697 | # They are ordered according to the file, so do not change this. | |
698 | lappend all_syscalls "close" | |
699 | lappend all_syscalls "chroot" | |
700 | ||
5463a15c TV |
701 | if { ![runto_main] } { |
702 | return 0 | |
703 | } | |
704 | ||
f68f11b7 YQ |
705 | # SYS_pipe doesn't exist on aarch64 kernel. |
706 | set test "check SYS_pipe" | |
5463a15c | 707 | set have_SYS_pipe 0 |
32412d8e | 708 | set SYS_pipe -1 |
f68f11b7 | 709 | gdb_test_multiple "p pipe_syscall" $test { |
32412d8e | 710 | -re -wrap " = ($decimal)" { |
f68f11b7 | 711 | pass $test |
5463a15c | 712 | set have_SYS_pipe 1 |
32412d8e | 713 | set SYS_pipe $expect_out(1,string) |
f68f11b7 | 714 | } |
5463a15c | 715 | -re -wrap "No symbol .*" { |
f68f11b7 | 716 | pass $test |
5463a15c TV |
717 | } |
718 | } | |
719 | ||
720 | set test "check SYS_pipe2" | |
721 | set have_SYS_pipe2 0 | |
32412d8e | 722 | set SYS_pipe2 -1 |
5463a15c | 723 | gdb_test_multiple "p pipe2_syscall" $test { |
32412d8e | 724 | -re -wrap " = ($decimal)" { |
5463a15c TV |
725 | pass $test |
726 | set have_SYS_pipe2 1 | |
32412d8e | 727 | set SYS_pipe2 $expect_out(1,string) |
5463a15c TV |
728 | } |
729 | -re -wrap "No symbol .*" { | |
730 | pass $test | |
731 | } | |
732 | } | |
733 | ||
734 | if { $have_SYS_pipe == 0 && $have_SYS_pipe2 == 0 } { | |
735 | return 0 | |
736 | } | |
737 | ||
738 | with_test_prefix "determine pipe syscall" { | |
739 | set line [gdb_get_line_number "pipe (fd)"] | |
740 | gdb_test "break $line" | |
741 | gdb_continue_to_breakpoint "before pipe call" | |
742 | if { $have_SYS_pipe } { | |
32412d8e | 743 | gdb_test "catch syscall $SYS_pipe" |
5463a15c TV |
744 | } |
745 | if { $have_SYS_pipe2 } { | |
32412d8e | 746 | gdb_test "catch syscall $SYS_pipe2" |
5463a15c TV |
747 | } |
748 | set ok 0 | |
749 | gdb_test_multiple "continue" "" { | |
32412d8e | 750 | -re -wrap "Catchpoint $decimal \\(call to syscall (pipe|$SYS_pipe)\\).*" { |
5463a15c TV |
751 | lappend all_syscalls pipe |
752 | pass $gdb_test_name | |
753 | set ok 1 | |
754 | } | |
276e7f5c | 755 | -re -wrap "Catchpoint $decimal \\(call to syscall (pipe2|$SYS_pipe2)\\).*" { |
5463a15c TV |
756 | lappend all_syscalls pipe2 |
757 | pass $gdb_test_name | |
758 | set ok 1 | |
759 | } | |
760 | -re -wrap "" { | |
761 | fail $gdb_test_name | |
762 | } | |
763 | } | |
764 | if { ! $ok } { | |
765 | return 0 | |
f68f11b7 YQ |
766 | } |
767 | } | |
768 | ||
769 | lappend all_syscalls "write" | |
770 | lappend all_syscalls "read" | |
5463a15c | 771 | |
cf622c39 JB |
772 | # Determine the right syscall to use for exit() |
773 | set test "check SYS_exit" | |
774 | set have_SYS_exit 0 | |
775 | set SYS_exit -1 | |
776 | gdb_test_multiple "p exit_syscall" $test { | |
777 | -re -wrap " = ($decimal)" { | |
778 | pass $test | |
779 | set have_SYS_exit 1 | |
780 | set SYS_exit $expect_out(1,string) | |
781 | } | |
782 | -re -wrap "No symbol .*" { | |
783 | pass $test | |
784 | } | |
785 | } | |
786 | ||
787 | set test "check SYS_exit_group" | |
788 | set have_SYS_exit_group 0 | |
789 | set SYS_exit_group -1 | |
790 | gdb_test_multiple "p exit_group_syscall" $test { | |
791 | -re -wrap " = ($decimal)" { | |
792 | pass $test | |
793 | set have_SYS_exit_group 1 | |
794 | set SYS_exit_group $expect_out(1,string) | |
795 | } | |
796 | -re -wrap "No symbol .*" { | |
797 | pass $test | |
798 | } | |
799 | } | |
800 | ||
801 | if { $have_SYS_exit == 0 && $have_SYS_exit_group == 0 } { | |
802 | return 0 | |
803 | } | |
804 | ||
805 | if { $have_SYS_exit } { | |
806 | set last_syscall "exit" | |
807 | set last_syscall_number $SYS_exit | |
808 | } else { | |
809 | set last_syscall "exit_group" | |
810 | set last_syscall_number $SYS_exit_group | |
811 | } | |
5463a15c | 812 | return 1 |
f68f11b7 YQ |
813 | } |
814 | ||
5463a15c TV |
815 | if { ![setup_all_syscalls] } { |
816 | return -1 | |
817 | } | |
f68f11b7 | 818 | |
2e0d821f SDJ |
819 | # Fill all the syscalls numbers before starting anything. |
820 | fill_all_syscalls_numbers | |
fbbe92c5 SDJ |
821 | |
822 | # Execute the tests, using XML support | |
1e76a7e9 | 823 | gdb_exit |
b963a97f | 824 | if { [allow_xml_test] } { |
2e0d821f | 825 | clean_restart $binfile |
bccd0dd2 SDJ |
826 | do_syscall_tests |
827 | ||
828 | # Now, we have to see if GDB displays a warning when we | |
829 | # don't set the data-directory but try to use catch syscall | |
830 | # anyway. For that, we must restart GDB first. | |
2e0d821f | 831 | clean_restart $binfile |
bccd0dd2 SDJ |
832 | test_catch_syscall_fail_nodatadir |
833 | } | |
fbbe92c5 SDJ |
834 | |
835 | # Restart gdb | |
2e0d821f | 836 | clean_restart $binfile |
fbbe92c5 SDJ |
837 | |
838 | # Execute the tests, without XML support. In this case, GDB will | |
839 | # only display syscall numbers, and not syscall names. | |
840 | do_syscall_tests_without_xml |