diff options
author | Don Breazeal <donb@codesourcery.com> | 2015-05-12 09:52:43 -0700 |
---|---|---|
committer | Don Breazeal <donb@codesourcery.com> | 2015-05-12 09:52:43 -0700 |
commit | de0d863ec3fda88e488cee568f943c7998b68862 (patch) | |
tree | ba0c782f7e34cc80b37f9a704f71e897364f364c /gdb/nat | |
parent | ddcbc3975fffffdb5bb3bfb05b1f0ef3d67be3ee (diff) | |
download | gdb-de0d863ec3fda88e488cee568f943c7998b68862.zip gdb-de0d863ec3fda88e488cee568f943c7998b68862.tar.gz gdb-de0d863ec3fda88e488cee568f943c7998b68862.tar.bz2 |
Extended-remote Linux follow fork
This patch implements basic support for follow-fork and detach-on-fork on
extended-remote Linux targets. Only 'fork' is supported in this patch;
'vfork' support is added n a subsequent patch. This patch depends on
the previous patches in the patch series.
Sufficient extended-remote functionality has been implemented here to pass
gdb.base/multi-forks.exp, as well as gdb.base/foll-fork.exp with the
catchpoint tests commented out. Some other fork tests fail with this
patch because it doesn't provide the architecture support needed for
watchpoint inheritance or fork catchpoints.
The implementation follows the same general structure as for the native
implementation as much as possible.
This implementation includes:
* enabling fork events in linux-low.c in initialize_low and
linux_enable_extended_features
* handling fork events in gdbserver/linux-low.c:handle_extended_wait
- when a fork event occurs in gdbserver, we must do the full creation
of the new process, thread, lwp, and breakpoint lists. This is
required whether or not the new child is destined to be
detached-on-fork, because GDB will make target calls that require all
the structures. In particular we need the breakpoint lists in order
to remove the breakpoints from a detaching child. If we are not
detaching the child we will need all these structures anyway.
- as part of this event handling we store the target_waitstatus in a new
member of the parent lwp_info structure, 'waitstatus'. This
is used to store extended event information for reporting to GDB.
- handle_extended_wait is given a return value, denoting whether the
handled event should be reported to GDB. Previously it had only
handled clone events, which were never reported.
* using a new predicate in gdbserver to control handling of the fork event
(and eventually all extended events) in linux_wait_1. The predicate,
extended_event_reported, checks a target_waitstatus.kind for an
extended ptrace event.
* implementing a new RSP 'T' Stop Reply Packet stop reason: "fork", in
gdbserver/remote-utils.c and remote.c.
* implementing new target and RSP support for target_follow_fork with
target extended-remote. (The RSP components were actually defined in
patch 1, but they see their first use here).
- remote target routine remote_follow_fork, which just sends the 'D;pid'
detach packet to detach the new fork child cleanly. We can't just
call target_detach because the data structures for the forked child
have not been allocated on the host side.
Tested on x64 Ubuntu Lucid, native, remote, extended-remote.
gdb/gdbserver/ChangeLog:
* linux-low.c (handle_extended_wait): Implement return value,
rename argument 'event_child' to 'event_lwp', handle
PTRACE_EVENT_FORK, call internal_error for unrecognized event.
(linux_low_ptrace_options): New function.
(linux_low_filter_event): Call linux_low_ptrace_options,
use different argument fo linux_enable_event_reporting,
use return value from handle_extended_wait.
(extended_event_reported): New function.
(linux_wait_1): Call extended_event_reported and set
status to report fork events.
(linux_write_memory): Add pid to debug message.
(reset_lwp_ptrace_options_callback): New function.
(linux_handle_new_gdb_connection): New function.
(linux_target_ops): Initialize new structure member.
* linux-low.h (struct lwp_info) <waitstatus>: New member.
* lynx-low.c: Initialize new structure member.
* remote-utils.c (prepare_resume_reply): Implement stop reason
"fork" for "T" stop message.
* server.c (handle_query): Call handle_new_gdb_connection.
* server.h (report_fork_events): Declare global flag.
* target.h (struct target_ops) <handle_new_gdb_connection>:
New member.
(target_handle_new_gdb_connection): New macro.
* win32-low.c: Initialize new structure member.
gdb/ChangeLog:
* linux-nat.c (linux_nat_ptrace_options): New function.
(linux_init_ptrace, wait_lwp, linux_nat_filter_event):
Call linux_nat_ptrace_options and use different argument to
linux_enable_event_reporting.
(_initialize_linux_nat): Delete call to
linux_ptrace_set_additional_flags.
* nat/linux-ptrace.c (current_ptrace_options): Rename to
supported_ptrace_options.
(additional_flags): Delete variable.
(linux_check_ptrace_features): Use supported_ptrace_options.
(linux_test_for_tracesysgood, linux_test_for_tracefork):
Likewise, and remove additional_flags check.
(linux_enable_event_reporting): Change 'attached' argument to
'options'. Use supported_ptrace_options.
(ptrace_supports_feature): Change comment. Use
supported_ptrace_options.
(linux_ptrace_set_additional_flags): Delete function.
* nat/linux-ptrace.h (linux_ptrace_set_additional_flags):
Delete function prototype.
* remote.c (remote_fork_event_p): New function.
(remote_detach_pid): New function.
(remote_detach_1): Call remote_detach_pid, don't mourn inferior
if doing detach-on-fork.
(remote_follow_fork): New function.
(remote_parse_stop_reply): Handle new "T" stop reason "fork".
(remote_pid_to_str): Print "process" strings for pid/0/0 ptids.
(init_extended_remote_ops): Initialize to_follow_fork.
Diffstat (limited to 'gdb/nat')
-rw-r--r-- | gdb/nat/linux-ptrace.c | 85 | ||||
-rw-r--r-- | gdb/nat/linux-ptrace.h | 1 |
2 files changed, 31 insertions, 55 deletions
diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c index aba3da8..fb12606 100644 --- a/gdb/nat/linux-ptrace.c +++ b/gdb/nat/linux-ptrace.c @@ -25,14 +25,10 @@ #include <stdint.h> -/* Stores the currently supported ptrace options. A value of - -1 means we did not check for features yet. A value of 0 means - there are no supported features. */ -static int current_ptrace_options = -1; - -/* Additional flags to test. */ - -static int additional_flags; +/* Stores the ptrace options supported by the running kernel. + A value of -1 means we did not check for features yet. A value + of 0 means there are no supported features. */ +static int supported_ptrace_options = -1; /* Find all possible reasons we could fail to attach PID and append these as strings to the already initialized BUFFER. '\0' @@ -343,7 +339,7 @@ linux_check_ptrace_features (void) int child_pid, ret, status; /* Initialize the options. */ - current_ptrace_options = 0; + supported_ptrace_options = 0; /* Fork a child so we can do some testing. The child will call linux_child_function and will get traced. The child will @@ -387,14 +383,11 @@ linux_test_for_tracesysgood (int child_pid) { int ret; - if ((additional_flags & PTRACE_O_TRACESYSGOOD) == 0) - return; - ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD); if (ret == 0) - current_ptrace_options |= PTRACE_O_TRACESYSGOOD; + supported_ptrace_options |= PTRACE_O_TRACESYSGOOD; } /* Determine if PTRACE_O_TRACEFORK can be used to follow fork @@ -414,15 +407,12 @@ linux_test_for_tracefork (int child_pid) if (ret != 0) return; - if ((additional_flags & PTRACE_O_TRACEVFORKDONE) != 0) - { - /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */ - ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, - (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK - | PTRACE_O_TRACEVFORKDONE)); - if (ret == 0) - current_ptrace_options |= PTRACE_O_TRACEVFORKDONE; - } + /* Check if the target supports PTRACE_O_TRACEVFORKDONE. */ + ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0, + (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK + | PTRACE_O_TRACEVFORKDONE)); + if (ret == 0) + supported_ptrace_options |= PTRACE_O_TRACEVFORKDONE; /* Setting PTRACE_O_TRACEFORK did not cause an error, however we don't know for sure that the feature is available; old @@ -458,10 +448,10 @@ linux_test_for_tracefork (int child_pid) /* We got the PID from the grandchild, which means fork tracing is supported. */ - current_ptrace_options |= PTRACE_O_TRACECLONE; - current_ptrace_options |= (additional_flags & (PTRACE_O_TRACEFORK - | PTRACE_O_TRACEVFORK - | PTRACE_O_TRACEEXEC)); + supported_ptrace_options |= PTRACE_O_TRACECLONE; + supported_ptrace_options |= (PTRACE_O_TRACEFORK + | PTRACE_O_TRACEVFORK + | PTRACE_O_TRACEEXEC); /* Do some cleanup and kill the grandchild. */ my_waitpid (second_pid, &second_status, 0); @@ -489,33 +479,31 @@ linux_test_for_exitkill (int child_pid) (PTRACE_TYPE_ARG4) PTRACE_O_EXITKILL); if (ret == 0) - current_ptrace_options |= PTRACE_O_EXITKILL; + supported_ptrace_options |= PTRACE_O_EXITKILL; } /* Enable reporting of all currently supported ptrace events. - ATTACHED should be nonzero if we have attached to the inferior. */ + OPTIONS is a bit mask of extended features we want enabled, + if supported by the kernel. PTRACE_O_TRACECLONE is always + enabled, if supported. */ void -linux_enable_event_reporting (pid_t pid, int attached) +linux_enable_event_reporting (pid_t pid, int options) { - int ptrace_options; - /* Check if we have initialized the ptrace features for this target. If not, do it now. */ - if (current_ptrace_options == -1) + if (supported_ptrace_options == -1) linux_check_ptrace_features (); - ptrace_options = current_ptrace_options; - if (attached) - { - /* When attached to our inferior, we do not want the inferior - to die with us if we terminate unexpectedly. */ - ptrace_options &= ~PTRACE_O_EXITKILL; - } + /* We always want clone events. */ + options |= PTRACE_O_TRACECLONE; + + /* Filter out unsupported options. */ + options &= supported_ptrace_options; /* Set the options. */ ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, - (PTRACE_TYPE_ARG4) (uintptr_t) ptrace_options); + (PTRACE_TYPE_ARG4) (uintptr_t) options); } /* Disable reporting of all currently supported ptrace events. */ @@ -528,16 +516,16 @@ linux_disable_event_reporting (pid_t pid) } /* Returns non-zero if PTRACE_OPTIONS is contained within - CURRENT_PTRACE_OPTIONS, therefore supported. Returns 0 + SUPPORTED_PTRACE_OPTIONS, therefore supported. Returns 0 otherwise. */ static int ptrace_supports_feature (int ptrace_options) { - if (current_ptrace_options == -1) + if (supported_ptrace_options == -1) linux_check_ptrace_features (); - return ((current_ptrace_options & ptrace_options) == ptrace_options); + return ((supported_ptrace_options & ptrace_options) == ptrace_options); } /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace, @@ -595,17 +583,6 @@ linux_ptrace_init_warnings (void) linux_ptrace_test_ret_to_nx (); } -/* Set additional ptrace flags to use. Some such flags may be checked - by the implementation above. This function must be called before - any other function in this file; otherwise the flags may not take - effect appropriately. */ - -void -linux_ptrace_set_additional_flags (int flags) -{ - additional_flags = flags; -} - /* Extract extended ptrace event from wait status. */ int diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h index 03d98c9..1db0cde 100644 --- a/gdb/nat/linux-ptrace.h +++ b/gdb/nat/linux-ptrace.h @@ -156,7 +156,6 @@ extern int linux_supports_tracefork (void); extern int linux_supports_traceclone (void); extern int linux_supports_tracevforkdone (void); extern int linux_supports_tracesysgood (void); -extern void linux_ptrace_set_additional_flags (int); extern int linux_ptrace_get_extended_event (int wstat); extern int linux_is_extended_waitstatus (int wstat); extern int linux_wstatus_maybe_breakpoint (int wstat); |