binutils-gdb/gdb/testsuite/gdb.base/catch-syscall.c

53 lines
1.3 KiB
C
Raw Normal View History

/* This file is used to test the 'catch syscall' feature on GDB.
Please, if you are going to edit this file DO NOT change the syscalls
being called (nor the order of them). If you really must do this, then
take a look at catch-syscall.exp and modify there too.
Written by Sergio Durigan Junior <sergiodj@linux.vnet.ibm.com>
September, 2008 */
#include <unistd.h>
Improve and fix catch-syscall.exp While fixing another bug, I found that the current gdb.base/catch-syscall.exp is kind of messy, could use some improvements, and is not correctly testing some things. I've made the following patch to address all the issues I found. On the organization side, it does a cleanup and removes unecessary imports of gdb_prompt, uses prepare_for_testing and clean_restart where needed, and fixes some comments. The testcase was also not correctly testing catching syscalls using only numbers, or catching many syscalls at once. I fixed that. The patch also uses a new method for obtaining the syscalls numbers: it relies on the C source file to get them, via <sys/syscall.h> and SYS_* macros. This makes the .exp file simpler because there is no need to include target conditionals there. I tested this on x86_64 Fedora 18. gdb/testsuite/ChangeLog: 2013-12-18 Sergio Durigan Junior <sergiodj@redhat.com> * gdb.base/catch-syscall.c: Include <sys/syscall.h>. (close_syscall, chroot_syscall, exit_group_syscall): New variables. * gdb.base/catch-syscall.exp: Replace gdb_compile by prepare_for_testing. Call fill_all_syscalls_numbers before starting. Replace gdb_exit, gdb_start, gdb_reinitialize_dir and gdb_load by clean_restart. (check_info_bp_any_syscall, check_info_bp_specific_syscall) (check_info_bp_many_syscalls): Remove global gdb_prompt. (check_call_to_syscall): Likewise. Add global decimal. Improve testing regex. (check_return_from_syscall): Likewise. (check_continue, insert_catch_syscall_with_arg): Remove global gdb_prompt. (insert_catch_syscall_with_many_args): Likewise. Add global decimal. Fix $filter_str. Improve testing regex. (check_for_program_end): Remove global gdb_prompt. (test_catch_syscall_without_args): Likewise. Add global decimal. Improve testing regex. (test_catch_syscall_with_args, test_catch_syscall_with_many_args) (test_catch_syscall_with_wrong_args) (test_catch_syscall_restarting_inferior) (test_catch_syscall_fail_nodatadir): Remove global gdb_prompt. (do_syscall_tests): Likewise. Remove global srcdir. (test_catch_syscall_without_args_noxml): Remove global gdb_prompt. Add global last_syscall_number. Test for the exact syscall number to be caught. (test_catch_syscall_with_args_noxml): Remove global gdb_prompt. Add global all_syscalls_numbers. Test each syscall number to be caught, instead of only testing "close". (test_catch_syscall_with_wrong_args_noxml): Remove global gdb_prompt. (do_syscall_tests_without_xml): Likewise. Remove global srcdir. Remove stale comment. (fill_all_syscalls_numbers): Add global last_syscall_number. Fill the correct syscall numbers using information from the inferior.
2013-12-18 23:19:01 +01:00
#include <sys/syscall.h>
#include <fcntl.h>
#include <sys/stat.h>
Improve and fix catch-syscall.exp While fixing another bug, I found that the current gdb.base/catch-syscall.exp is kind of messy, could use some improvements, and is not correctly testing some things. I've made the following patch to address all the issues I found. On the organization side, it does a cleanup and removes unecessary imports of gdb_prompt, uses prepare_for_testing and clean_restart where needed, and fixes some comments. The testcase was also not correctly testing catching syscalls using only numbers, or catching many syscalls at once. I fixed that. The patch also uses a new method for obtaining the syscalls numbers: it relies on the C source file to get them, via <sys/syscall.h> and SYS_* macros. This makes the .exp file simpler because there is no need to include target conditionals there. I tested this on x86_64 Fedora 18. gdb/testsuite/ChangeLog: 2013-12-18 Sergio Durigan Junior <sergiodj@redhat.com> * gdb.base/catch-syscall.c: Include <sys/syscall.h>. (close_syscall, chroot_syscall, exit_group_syscall): New variables. * gdb.base/catch-syscall.exp: Replace gdb_compile by prepare_for_testing. Call fill_all_syscalls_numbers before starting. Replace gdb_exit, gdb_start, gdb_reinitialize_dir and gdb_load by clean_restart. (check_info_bp_any_syscall, check_info_bp_specific_syscall) (check_info_bp_many_syscalls): Remove global gdb_prompt. (check_call_to_syscall): Likewise. Add global decimal. Improve testing regex. (check_return_from_syscall): Likewise. (check_continue, insert_catch_syscall_with_arg): Remove global gdb_prompt. (insert_catch_syscall_with_many_args): Likewise. Add global decimal. Fix $filter_str. Improve testing regex. (check_for_program_end): Remove global gdb_prompt. (test_catch_syscall_without_args): Likewise. Add global decimal. Improve testing regex. (test_catch_syscall_with_args, test_catch_syscall_with_many_args) (test_catch_syscall_with_wrong_args) (test_catch_syscall_restarting_inferior) (test_catch_syscall_fail_nodatadir): Remove global gdb_prompt. (do_syscall_tests): Likewise. Remove global srcdir. (test_catch_syscall_without_args_noxml): Remove global gdb_prompt. Add global last_syscall_number. Test for the exact syscall number to be caught. (test_catch_syscall_with_args_noxml): Remove global gdb_prompt. Add global all_syscalls_numbers. Test each syscall number to be caught, instead of only testing "close". (test_catch_syscall_with_wrong_args_noxml): Remove global gdb_prompt. (do_syscall_tests_without_xml): Likewise. Remove global srcdir. Remove stale comment. (fill_all_syscalls_numbers): Add global last_syscall_number. Fill the correct syscall numbers using information from the inferior.
2013-12-18 23:19:01 +01:00
/* These are the syscalls numbers used by the test. */
int close_syscall = SYS_close;
int chroot_syscall = SYS_chroot;
Fix PR breakpoints/16297: catch syscall with syscall 0 Code rationale ============== by: Gabriel Krisman Bertazi This is a fix for bug 16297. The problem occurs when the user attempts to catch any syscall 0 (such as syscall read on Linux/x86_64). GDB was not able to catch the syscall and was missing the breakpoint. Now, breakpoint_hit_catch_syscall returns immediately when it finds the correct syscall number, avoiding a following check for the end of the search vector, that returns a no hit if the syscall number was zero. Testcase rationale ================== by: Sergio Durigan Junior This testcase is a little difficult to write. By doing a quick inspection at the Linux source, one can see that, in many targets, the syscall number 0 is restart_syscall, which is forbidden to be called from userspace. Therefore, on many targets, there's just no way to test this safely. My decision was to take the simpler route and just adds the "read" syscall on the default test. Its number on x86_64 is zero, which is "good enough" since many people here do their tests on x86_64 anyway and it is a popular architecture. However, there was another little gotcha. When using "read" passing 0 as the third parameter (i.e., asking it to read 0 bytes), current libc implementations could choose not to effectively call the syscall. Therefore, the best solution was to create a temporary pipe, write 1 byte into it, and then read this byte from it. gdb/ChangeLog 2013-12-19 Gabriel Krisman Bertazi <gabriel@krisman.be> PR breakpoints/16297 * breakpoint.c (breakpoint_hit_catch_syscall): Return immediately when expected syscall is hit. gdb/testsuite/ChangeLog 2013-12-19 Sergio Durigan Junior <sergiodj@redhat.com> PR breakpoints/16297 * gdb.base/catch-syscall.c (read_syscall, pipe_syscall) (write_syscall): New variables. (main): Create a pipe, write 1 byte in it, and read 1 byte from it. * gdb.base/catch-syscall.exp (all_syscalls): Include "pipe, "write" and "read" syscalls. (fill_all_syscalls_numbers): Improve the way to obtain syscalls numbers.
2013-12-19 20:01:49 +01:00
/* GDB had a bug where it couldn't catch syscall number 0 (PR 16297).
In most GNU/Linux architectures, syscall number 0 is
restart_syscall, which can't be called from userspace. However,
the "read" syscall is zero on x86_64. */
int read_syscall = SYS_read;
Support catch syscall on aarch64 linux Hi, This patch is to support catch syscall on aarch64 linux. We implement gdbarch method get_syscall_number for aarch64-linux, and add aarch64-linux.xml file, which looks straightforward, however the changes to test case doesn't. First of all, we enable catch-syscall.exp on aarch64-linux target, but skip the multi_arch testing on current stage. I plan to touch multi arch debugging on aarch64-linux later. Then, when I run catch-syscall.exp on aarch64-linux, gcc errors that SYS_pipe isn't defined. We find that aarch64 kernel only has pipe2 syscall and libc already convert pipe to pipe2. As a result, I change catch-syscall.c to use SYS_pipe if it is defined, otherwise use SYS_pipe2 instead. The vector all_syscalls in catch-syscall.exp can't be pre-determined, so I add a new proc setup_all_syscalls to fill it, according to the availability of SYS_pipe. Regression tested on {x86_64, aarch64}-linux x {native, gdbserver}. gdb: 2015-03-18 Yao Qi <yao.qi@linaro.org> PR tdep/18107 * aarch64-linux-tdep.c: Include xml-syscall.h (aarch64_linux_get_syscall_number): New function. (aarch64_linux_init_abi): Call set_gdbarch_get_syscall_number. * syscalls/aarch64-linux.xml: New file. gdb/testsuite: 2015-03-18 Yao Qi <yao.qi@linaro.org> PR tdep/18107 * gdb.base/catch-syscall.c [!SYS_pipe] (pipe2_syscall): New variable. * gdb.base/catch-syscall.exp: Don't skip it on aarch64*-*-linux* target. Remove elements in all_syscalls. (test_catch_syscall_multi_arch): Skip it on aarch64*-linux* target. (setup_all_syscalls): New proc.
2015-03-18 11:47:45 +01:00
#ifdef SYS_pipe
int pipe_syscall = SYS_pipe;
Support catch syscall on aarch64 linux Hi, This patch is to support catch syscall on aarch64 linux. We implement gdbarch method get_syscall_number for aarch64-linux, and add aarch64-linux.xml file, which looks straightforward, however the changes to test case doesn't. First of all, we enable catch-syscall.exp on aarch64-linux target, but skip the multi_arch testing on current stage. I plan to touch multi arch debugging on aarch64-linux later. Then, when I run catch-syscall.exp on aarch64-linux, gcc errors that SYS_pipe isn't defined. We find that aarch64 kernel only has pipe2 syscall and libc already convert pipe to pipe2. As a result, I change catch-syscall.c to use SYS_pipe if it is defined, otherwise use SYS_pipe2 instead. The vector all_syscalls in catch-syscall.exp can't be pre-determined, so I add a new proc setup_all_syscalls to fill it, according to the availability of SYS_pipe. Regression tested on {x86_64, aarch64}-linux x {native, gdbserver}. gdb: 2015-03-18 Yao Qi <yao.qi@linaro.org> PR tdep/18107 * aarch64-linux-tdep.c: Include xml-syscall.h (aarch64_linux_get_syscall_number): New function. (aarch64_linux_init_abi): Call set_gdbarch_get_syscall_number. * syscalls/aarch64-linux.xml: New file. gdb/testsuite: 2015-03-18 Yao Qi <yao.qi@linaro.org> PR tdep/18107 * gdb.base/catch-syscall.c [!SYS_pipe] (pipe2_syscall): New variable. * gdb.base/catch-syscall.exp: Don't skip it on aarch64*-*-linux* target. Remove elements in all_syscalls. (test_catch_syscall_multi_arch): Skip it on aarch64*-linux* target. (setup_all_syscalls): New proc.
2015-03-18 11:47:45 +01:00
#else
int pipe2_syscall = SYS_pipe2;
#endif
int write_syscall = SYS_write;
int exit_group_syscall = SYS_exit_group;
Improve and fix catch-syscall.exp While fixing another bug, I found that the current gdb.base/catch-syscall.exp is kind of messy, could use some improvements, and is not correctly testing some things. I've made the following patch to address all the issues I found. On the organization side, it does a cleanup and removes unecessary imports of gdb_prompt, uses prepare_for_testing and clean_restart where needed, and fixes some comments. The testcase was also not correctly testing catching syscalls using only numbers, or catching many syscalls at once. I fixed that. The patch also uses a new method for obtaining the syscalls numbers: it relies on the C source file to get them, via <sys/syscall.h> and SYS_* macros. This makes the .exp file simpler because there is no need to include target conditionals there. I tested this on x86_64 Fedora 18. gdb/testsuite/ChangeLog: 2013-12-18 Sergio Durigan Junior <sergiodj@redhat.com> * gdb.base/catch-syscall.c: Include <sys/syscall.h>. (close_syscall, chroot_syscall, exit_group_syscall): New variables. * gdb.base/catch-syscall.exp: Replace gdb_compile by prepare_for_testing. Call fill_all_syscalls_numbers before starting. Replace gdb_exit, gdb_start, gdb_reinitialize_dir and gdb_load by clean_restart. (check_info_bp_any_syscall, check_info_bp_specific_syscall) (check_info_bp_many_syscalls): Remove global gdb_prompt. (check_call_to_syscall): Likewise. Add global decimal. Improve testing regex. (check_return_from_syscall): Likewise. (check_continue, insert_catch_syscall_with_arg): Remove global gdb_prompt. (insert_catch_syscall_with_many_args): Likewise. Add global decimal. Fix $filter_str. Improve testing regex. (check_for_program_end): Remove global gdb_prompt. (test_catch_syscall_without_args): Likewise. Add global decimal. Improve testing regex. (test_catch_syscall_with_args, test_catch_syscall_with_many_args) (test_catch_syscall_with_wrong_args) (test_catch_syscall_restarting_inferior) (test_catch_syscall_fail_nodatadir): Remove global gdb_prompt. (do_syscall_tests): Likewise. Remove global srcdir. (test_catch_syscall_without_args_noxml): Remove global gdb_prompt. Add global last_syscall_number. Test for the exact syscall number to be caught. (test_catch_syscall_with_args_noxml): Remove global gdb_prompt. Add global all_syscalls_numbers. Test each syscall number to be caught, instead of only testing "close". (test_catch_syscall_with_wrong_args_noxml): Remove global gdb_prompt. (do_syscall_tests_without_xml): Likewise. Remove global srcdir. Remove stale comment. (fill_all_syscalls_numbers): Add global last_syscall_number. Fill the correct syscall numbers using information from the inferior.
2013-12-18 23:19:01 +01:00
int
main (void)
{
Fix PR breakpoints/16297: catch syscall with syscall 0 Code rationale ============== by: Gabriel Krisman Bertazi This is a fix for bug 16297. The problem occurs when the user attempts to catch any syscall 0 (such as syscall read on Linux/x86_64). GDB was not able to catch the syscall and was missing the breakpoint. Now, breakpoint_hit_catch_syscall returns immediately when it finds the correct syscall number, avoiding a following check for the end of the search vector, that returns a no hit if the syscall number was zero. Testcase rationale ================== by: Sergio Durigan Junior This testcase is a little difficult to write. By doing a quick inspection at the Linux source, one can see that, in many targets, the syscall number 0 is restart_syscall, which is forbidden to be called from userspace. Therefore, on many targets, there's just no way to test this safely. My decision was to take the simpler route and just adds the "read" syscall on the default test. Its number on x86_64 is zero, which is "good enough" since many people here do their tests on x86_64 anyway and it is a popular architecture. However, there was another little gotcha. When using "read" passing 0 as the third parameter (i.e., asking it to read 0 bytes), current libc implementations could choose not to effectively call the syscall. Therefore, the best solution was to create a temporary pipe, write 1 byte into it, and then read this byte from it. gdb/ChangeLog 2013-12-19 Gabriel Krisman Bertazi <gabriel@krisman.be> PR breakpoints/16297 * breakpoint.c (breakpoint_hit_catch_syscall): Return immediately when expected syscall is hit. gdb/testsuite/ChangeLog 2013-12-19 Sergio Durigan Junior <sergiodj@redhat.com> PR breakpoints/16297 * gdb.base/catch-syscall.c (read_syscall, pipe_syscall) (write_syscall): New variables. (main): Create a pipe, write 1 byte in it, and read 1 byte from it. * gdb.base/catch-syscall.exp (all_syscalls): Include "pipe, "write" and "read" syscalls. (fill_all_syscalls_numbers): Improve the way to obtain syscalls numbers.
2013-12-19 20:01:49 +01:00
int fd[2];
char buf1[2] = "a";
char buf2[2];
/* A close() with a wrong argument. We are only
interested in the syscall. */
close (-1);
chroot (".");
Fix PR breakpoints/16297: catch syscall with syscall 0 Code rationale ============== by: Gabriel Krisman Bertazi This is a fix for bug 16297. The problem occurs when the user attempts to catch any syscall 0 (such as syscall read on Linux/x86_64). GDB was not able to catch the syscall and was missing the breakpoint. Now, breakpoint_hit_catch_syscall returns immediately when it finds the correct syscall number, avoiding a following check for the end of the search vector, that returns a no hit if the syscall number was zero. Testcase rationale ================== by: Sergio Durigan Junior This testcase is a little difficult to write. By doing a quick inspection at the Linux source, one can see that, in many targets, the syscall number 0 is restart_syscall, which is forbidden to be called from userspace. Therefore, on many targets, there's just no way to test this safely. My decision was to take the simpler route and just adds the "read" syscall on the default test. Its number on x86_64 is zero, which is "good enough" since many people here do their tests on x86_64 anyway and it is a popular architecture. However, there was another little gotcha. When using "read" passing 0 as the third parameter (i.e., asking it to read 0 bytes), current libc implementations could choose not to effectively call the syscall. Therefore, the best solution was to create a temporary pipe, write 1 byte into it, and then read this byte from it. gdb/ChangeLog 2013-12-19 Gabriel Krisman Bertazi <gabriel@krisman.be> PR breakpoints/16297 * breakpoint.c (breakpoint_hit_catch_syscall): Return immediately when expected syscall is hit. gdb/testsuite/ChangeLog 2013-12-19 Sergio Durigan Junior <sergiodj@redhat.com> PR breakpoints/16297 * gdb.base/catch-syscall.c (read_syscall, pipe_syscall) (write_syscall): New variables. (main): Create a pipe, write 1 byte in it, and read 1 byte from it. * gdb.base/catch-syscall.exp (all_syscalls): Include "pipe, "write" and "read" syscalls. (fill_all_syscalls_numbers): Improve the way to obtain syscalls numbers.
2013-12-19 20:01:49 +01:00
pipe (fd);
write (fd[1], buf1, sizeof (buf1));
read (fd[0], buf2, sizeof (buf2));
/* The last syscall. Do not change this. */
_exit (0);
}