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

78 lines
2.1 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>
gdb: Improve syscall entry/return tracking on Linux The existing logic was simply to flip syscall entry/return state when a syscall trap was seen, and even then only with active 'catch syscall'. That can get out of sync if 'catch syscall' is toggled at odd times. This patch updates the entry/return state for all syscall traps, regardless of catching state, and also updates known syscall state for other kinds of traps. Almost all PTRACE_EVENT stops are delivered from the middle of a syscall, so this can act like an entry. Every other kind of ptrace stop is only delivered outside of syscall event pairs, so marking them ignored ensures the next syscall trap looks like an entry. Three new test scenarios are added to catch-syscall.exp: - Disable 'catch syscall' from an entry to deliberately miss the return event, then re-enable to make sure a new entry is recognized. - Enable 'catch syscall' for the first time from a vfork event, which is a PTRACE_EVENT_VFORK in the middle of the syscall. Make sure the next syscall event is recognized as the return. - Make sure entry and return are recognized for an ENOSYS syscall. This is to defeat a common x86 hack that uses the pre-filled ENOSYS return value as a sign of being on the entry side. gdb/ChangeLog: 2015-10-19 Josh Stone <jistone@redhat.com> * linux-nat.c (linux_handle_syscall_trap): Always update entry/ return state, even when not actively catching syscalls at all. (linux_handle_extended_wait): Mark syscall_state like an entry. (wait_lwp): Set syscall_state ignored for other traps. (linux_nat_filter_event): Likewise. gdb/testsuite/ChangeLog: 2015-10-19 Josh Stone <jistone@redhat.com> * gdb.base/catch-syscall.c: Include <sched.h>. (unknown_syscall): New variable. (main): Trigger a vfork and an unknown syscall. * gdb.base/catch-syscall.exp (vfork_syscalls): New variable. (unknown_syscall_number): Likewise. (check_call_to_syscall): Accept an optional syscall pattern. (check_return_from_syscall): Likewise. (check_continue): Likewise. (test_catch_syscall_without_args): Check for vfork and ENOSYS. (test_catch_syscall_skipping_return): New test toggling off 'catch syscall' to step over the syscall return, then toggling back on. (test_catch_syscall_mid_vfork): New test turning on 'catch syscall' during a PTRACE_EVENT_VFORK stop, in the middle of a vfork syscall. (do_syscall_tests): Call test_catch_syscall_without_args and test_catch_syscall_mid_vfork. (test_catch_syscall_without_args_noxml): Check for vfork and ENOSYS. (fill_all_syscalls_numbers): Initialize unknown_syscall_number.
2015-10-20 02:59:38 +02:00
#include <sched.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;
#if defined(__arm__)
/* Although 123456789 is an illegal syscall umber on arm linux, kernel
sends SIGILL rather than returns -ENOSYS. However, arm linux kernel
returns -ENOSYS if syscall number is within 0xf0001..0xf07ff, so we
can use 0xf07ff for unknown_syscall in test. */
int unknown_syscall = 0x0f07ff;
#else
gdb: Improve syscall entry/return tracking on Linux The existing logic was simply to flip syscall entry/return state when a syscall trap was seen, and even then only with active 'catch syscall'. That can get out of sync if 'catch syscall' is toggled at odd times. This patch updates the entry/return state for all syscall traps, regardless of catching state, and also updates known syscall state for other kinds of traps. Almost all PTRACE_EVENT stops are delivered from the middle of a syscall, so this can act like an entry. Every other kind of ptrace stop is only delivered outside of syscall event pairs, so marking them ignored ensures the next syscall trap looks like an entry. Three new test scenarios are added to catch-syscall.exp: - Disable 'catch syscall' from an entry to deliberately miss the return event, then re-enable to make sure a new entry is recognized. - Enable 'catch syscall' for the first time from a vfork event, which is a PTRACE_EVENT_VFORK in the middle of the syscall. Make sure the next syscall event is recognized as the return. - Make sure entry and return are recognized for an ENOSYS syscall. This is to defeat a common x86 hack that uses the pre-filled ENOSYS return value as a sign of being on the entry side. gdb/ChangeLog: 2015-10-19 Josh Stone <jistone@redhat.com> * linux-nat.c (linux_handle_syscall_trap): Always update entry/ return state, even when not actively catching syscalls at all. (linux_handle_extended_wait): Mark syscall_state like an entry. (wait_lwp): Set syscall_state ignored for other traps. (linux_nat_filter_event): Likewise. gdb/testsuite/ChangeLog: 2015-10-19 Josh Stone <jistone@redhat.com> * gdb.base/catch-syscall.c: Include <sched.h>. (unknown_syscall): New variable. (main): Trigger a vfork and an unknown syscall. * gdb.base/catch-syscall.exp (vfork_syscalls): New variable. (unknown_syscall_number): Likewise. (check_call_to_syscall): Accept an optional syscall pattern. (check_return_from_syscall): Likewise. (check_continue): Likewise. (test_catch_syscall_without_args): Check for vfork and ENOSYS. (test_catch_syscall_skipping_return): New test toggling off 'catch syscall' to step over the syscall return, then toggling back on. (test_catch_syscall_mid_vfork): New test turning on 'catch syscall' during a PTRACE_EVENT_VFORK stop, in the middle of a vfork syscall. (do_syscall_tests): Call test_catch_syscall_without_args and test_catch_syscall_mid_vfork. (test_catch_syscall_without_args_noxml): Check for vfork and ENOSYS. (fill_all_syscalls_numbers): Initialize unknown_syscall_number.
2015-10-20 02:59:38 +02:00
int unknown_syscall = 123456789;
#endif
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
Implement 'catch syscall' for gdbserver This adds a new QCatchSyscalls packet to enable 'catch syscall', and new stop reasons "syscall_entry" and "syscall_return" for those events. It is currently only supported on Linux x86 and x86_64. gdb/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * NEWS (Changes since GDB 7.10): Mention QCatchSyscalls and the syscall_entry and syscall_return stop reasons. Mention GDB support for remote catch syscall. * remote.c (PACKET_QCatchSyscalls): New enum. (remote_set_syscall_catchpoint): New function. (remote_protocol_features): New element for QCatchSyscalls. (remote_parse_stop_reply): Parse syscall_entry/return stops. (init_remote_ops): Install remote_set_syscall_catchpoint. (_initialize_remote): Config QCatchSyscalls. * linux-nat.h (struct lwp_info) <syscall_state>: Comment typo. gdb/doc/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * gdb.texinfo (Remote Configuration): List the QCatchSyscalls packet. (Stop Reply Packets): List the syscall entry and return stop reasons. (General Query Packets): Describe QCatchSyscalls, and add it to the table and the detailed list of stub features. gdb/gdbserver/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * inferiors.h: Include "gdb_vecs.h". (struct process_info): Add syscalls_to_catch. * inferiors.c (remove_process): Free syscalls_to_catch. * remote-utils.c (prepare_resume_reply): Report syscall_entry and syscall_return stops. * server.h (UNKNOWN_SYSCALL, ANY_SYSCALL): Define. * server.c (handle_general_set): Handle QCatchSyscalls. (handle_query): Report support for QCatchSyscalls. * target.h (struct target_ops): Add supports_catch_syscall. (target_supports_catch_syscall): New macro. * linux-low.h (struct linux_target_ops): Add get_syscall_trapinfo. (struct lwp_info): Add syscall_state. * linux-low.c (handle_extended_wait): Mark syscall_state as an entry. Maintain syscall_state and syscalls_to_catch across exec. (get_syscall_trapinfo): New function, proxy to the_low_target. (linux_low_ptrace_options): Enable PTRACE_O_TRACESYSGOOD. (linux_low_filter_event): Toggle syscall_state entry/return for syscall traps, and set it ignored for all others. (gdb_catching_syscalls_p): New function. (gdb_catch_this_syscall_p): New function. (linux_wait_1): Handle SYSCALL_SIGTRAP. (linux_resume_one_lwp_throw): Add PTRACE_SYSCALL possibility. (linux_supports_catch_syscall): New function. (linux_target_ops): Install it. * linux-x86-low.c (x86_get_syscall_trapinfo): New function. (the_low_target): Install it. gdb/testsuite/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * gdb.base/catch-syscall.c (do_execve): New variable. (main): Conditionally trigger an execve. * gdb.base/catch-syscall.exp: Enable testing for remote targets. (test_catch_syscall_execve): New, check entry/return across execve. (do_syscall_tests): Call test_catch_syscall_execve.
2016-01-12 21:27:27 +01:00
/* Set by the test when it wants execve. */
int do_execve = 0;
int
Implement 'catch syscall' for gdbserver This adds a new QCatchSyscalls packet to enable 'catch syscall', and new stop reasons "syscall_entry" and "syscall_return" for those events. It is currently only supported on Linux x86 and x86_64. gdb/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * NEWS (Changes since GDB 7.10): Mention QCatchSyscalls and the syscall_entry and syscall_return stop reasons. Mention GDB support for remote catch syscall. * remote.c (PACKET_QCatchSyscalls): New enum. (remote_set_syscall_catchpoint): New function. (remote_protocol_features): New element for QCatchSyscalls. (remote_parse_stop_reply): Parse syscall_entry/return stops. (init_remote_ops): Install remote_set_syscall_catchpoint. (_initialize_remote): Config QCatchSyscalls. * linux-nat.h (struct lwp_info) <syscall_state>: Comment typo. gdb/doc/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * gdb.texinfo (Remote Configuration): List the QCatchSyscalls packet. (Stop Reply Packets): List the syscall entry and return stop reasons. (General Query Packets): Describe QCatchSyscalls, and add it to the table and the detailed list of stub features. gdb/gdbserver/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * inferiors.h: Include "gdb_vecs.h". (struct process_info): Add syscalls_to_catch. * inferiors.c (remove_process): Free syscalls_to_catch. * remote-utils.c (prepare_resume_reply): Report syscall_entry and syscall_return stops. * server.h (UNKNOWN_SYSCALL, ANY_SYSCALL): Define. * server.c (handle_general_set): Handle QCatchSyscalls. (handle_query): Report support for QCatchSyscalls. * target.h (struct target_ops): Add supports_catch_syscall. (target_supports_catch_syscall): New macro. * linux-low.h (struct linux_target_ops): Add get_syscall_trapinfo. (struct lwp_info): Add syscall_state. * linux-low.c (handle_extended_wait): Mark syscall_state as an entry. Maintain syscall_state and syscalls_to_catch across exec. (get_syscall_trapinfo): New function, proxy to the_low_target. (linux_low_ptrace_options): Enable PTRACE_O_TRACESYSGOOD. (linux_low_filter_event): Toggle syscall_state entry/return for syscall traps, and set it ignored for all others. (gdb_catching_syscalls_p): New function. (gdb_catch_this_syscall_p): New function. (linux_wait_1): Handle SYSCALL_SIGTRAP. (linux_resume_one_lwp_throw): Add PTRACE_SYSCALL possibility. (linux_supports_catch_syscall): New function. (linux_target_ops): Install it. * linux-x86-low.c (x86_get_syscall_trapinfo): New function. (the_low_target): Install it. gdb/testsuite/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * gdb.base/catch-syscall.c (do_execve): New variable. (main): Conditionally trigger an execve. * gdb.base/catch-syscall.exp: Enable testing for remote targets. (test_catch_syscall_execve): New, check entry/return across execve. (do_syscall_tests): Call test_catch_syscall_execve.
2016-01-12 21:27:27 +01:00
main (int argc, char *const argv[])
{
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];
Implement 'catch syscall' for gdbserver This adds a new QCatchSyscalls packet to enable 'catch syscall', and new stop reasons "syscall_entry" and "syscall_return" for those events. It is currently only supported on Linux x86 and x86_64. gdb/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * NEWS (Changes since GDB 7.10): Mention QCatchSyscalls and the syscall_entry and syscall_return stop reasons. Mention GDB support for remote catch syscall. * remote.c (PACKET_QCatchSyscalls): New enum. (remote_set_syscall_catchpoint): New function. (remote_protocol_features): New element for QCatchSyscalls. (remote_parse_stop_reply): Parse syscall_entry/return stops. (init_remote_ops): Install remote_set_syscall_catchpoint. (_initialize_remote): Config QCatchSyscalls. * linux-nat.h (struct lwp_info) <syscall_state>: Comment typo. gdb/doc/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * gdb.texinfo (Remote Configuration): List the QCatchSyscalls packet. (Stop Reply Packets): List the syscall entry and return stop reasons. (General Query Packets): Describe QCatchSyscalls, and add it to the table and the detailed list of stub features. gdb/gdbserver/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * inferiors.h: Include "gdb_vecs.h". (struct process_info): Add syscalls_to_catch. * inferiors.c (remove_process): Free syscalls_to_catch. * remote-utils.c (prepare_resume_reply): Report syscall_entry and syscall_return stops. * server.h (UNKNOWN_SYSCALL, ANY_SYSCALL): Define. * server.c (handle_general_set): Handle QCatchSyscalls. (handle_query): Report support for QCatchSyscalls. * target.h (struct target_ops): Add supports_catch_syscall. (target_supports_catch_syscall): New macro. * linux-low.h (struct linux_target_ops): Add get_syscall_trapinfo. (struct lwp_info): Add syscall_state. * linux-low.c (handle_extended_wait): Mark syscall_state as an entry. Maintain syscall_state and syscalls_to_catch across exec. (get_syscall_trapinfo): New function, proxy to the_low_target. (linux_low_ptrace_options): Enable PTRACE_O_TRACESYSGOOD. (linux_low_filter_event): Toggle syscall_state entry/return for syscall traps, and set it ignored for all others. (gdb_catching_syscalls_p): New function. (gdb_catch_this_syscall_p): New function. (linux_wait_1): Handle SYSCALL_SIGTRAP. (linux_resume_one_lwp_throw): Add PTRACE_SYSCALL possibility. (linux_supports_catch_syscall): New function. (linux_target_ops): Install it. * linux-x86-low.c (x86_get_syscall_trapinfo): New function. (the_low_target): Install it. gdb/testsuite/ChangeLog: 2016-01-12 Josh Stone <jistone@redhat.com> Philippe Waroquiers <philippe.waroquiers@skynet.be> * gdb.base/catch-syscall.c (do_execve): New variable. (main): Conditionally trigger an execve. * gdb.base/catch-syscall.exp: Enable testing for remote targets. (test_catch_syscall_execve): New, check entry/return across execve. (do_syscall_tests): Call test_catch_syscall_execve.
2016-01-12 21:27:27 +01:00
/* Test a simple self-exec, but only on request. */
if (do_execve)
execv (*argv, argv);
/* 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));
gdb: Improve syscall entry/return tracking on Linux The existing logic was simply to flip syscall entry/return state when a syscall trap was seen, and even then only with active 'catch syscall'. That can get out of sync if 'catch syscall' is toggled at odd times. This patch updates the entry/return state for all syscall traps, regardless of catching state, and also updates known syscall state for other kinds of traps. Almost all PTRACE_EVENT stops are delivered from the middle of a syscall, so this can act like an entry. Every other kind of ptrace stop is only delivered outside of syscall event pairs, so marking them ignored ensures the next syscall trap looks like an entry. Three new test scenarios are added to catch-syscall.exp: - Disable 'catch syscall' from an entry to deliberately miss the return event, then re-enable to make sure a new entry is recognized. - Enable 'catch syscall' for the first time from a vfork event, which is a PTRACE_EVENT_VFORK in the middle of the syscall. Make sure the next syscall event is recognized as the return. - Make sure entry and return are recognized for an ENOSYS syscall. This is to defeat a common x86 hack that uses the pre-filled ENOSYS return value as a sign of being on the entry side. gdb/ChangeLog: 2015-10-19 Josh Stone <jistone@redhat.com> * linux-nat.c (linux_handle_syscall_trap): Always update entry/ return state, even when not actively catching syscalls at all. (linux_handle_extended_wait): Mark syscall_state like an entry. (wait_lwp): Set syscall_state ignored for other traps. (linux_nat_filter_event): Likewise. gdb/testsuite/ChangeLog: 2015-10-19 Josh Stone <jistone@redhat.com> * gdb.base/catch-syscall.c: Include <sched.h>. (unknown_syscall): New variable. (main): Trigger a vfork and an unknown syscall. * gdb.base/catch-syscall.exp (vfork_syscalls): New variable. (unknown_syscall_number): Likewise. (check_call_to_syscall): Accept an optional syscall pattern. (check_return_from_syscall): Likewise. (check_continue): Likewise. (test_catch_syscall_without_args): Check for vfork and ENOSYS. (test_catch_syscall_skipping_return): New test toggling off 'catch syscall' to step over the syscall return, then toggling back on. (test_catch_syscall_mid_vfork): New test turning on 'catch syscall' during a PTRACE_EVENT_VFORK stop, in the middle of a vfork syscall. (do_syscall_tests): Call test_catch_syscall_without_args and test_catch_syscall_mid_vfork. (test_catch_syscall_without_args_noxml): Check for vfork and ENOSYS. (fill_all_syscalls_numbers): Initialize unknown_syscall_number.
2015-10-20 02:59:38 +02:00
/* Test vfork-event interactions. Child exits immediately.
(Plain fork won't work on no-mmu kernel configurations.) */
if (vfork () == 0)
_exit (0);
/* Trigger an intentional ENOSYS. */
syscall (unknown_syscall);
/* The last syscall. Do not change this. */
_exit (0);
}