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.
This commit is contained in:
Don Breazeal
2015-05-12 09:52:43 -07:00
parent ddcbc3975f
commit de0d863ec3
14 changed files with 415 additions and 106 deletions

View File

@@ -1462,6 +1462,14 @@ remote_multi_process_p (struct remote_state *rs)
return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
}
/* Returns true if fork events are supported. */
static int
remote_fork_event_p (struct remote_state *rs)
{
return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
}
/* Tokens for use by the asynchronous signal handlers for SIGINT. */
static struct async_signal_handler *async_sigint_remote_twice_token;
static struct async_signal_handler *async_sigint_remote_token;
@@ -4430,16 +4438,42 @@ remote_open_1 (const char *name, int from_tty,
wait_forever_enabled_p = 1;
}
/* This takes a program previously attached to and detaches it. After
this is done, GDB can be used to debug some other program. We
better not have left any breakpoints in the target program or it'll
die when it hits one. */
/* Detach the specified process. */
static void
remote_detach_1 (const char *args, int from_tty, int extended)
remote_detach_pid (int pid)
{
struct remote_state *rs = get_remote_state ();
if (remote_multi_process_p (rs))
xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
else
strcpy (rs->buf, "D");
putpkt (rs->buf);
getpkt (&rs->buf, &rs->buf_size, 0);
if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
;
else if (rs->buf[0] == '\0')
error (_("Remote doesn't know how to detach"));
else
error (_("Can't detach process."));
}
/* This detaches a program to which we previously attached, using
inferior_ptid to identify the process. After this is done, GDB
can be used to debug some other program. We better not have left
any breakpoints in the target program or it'll die when it hits
one. */
static void
remote_detach_1 (const char *args, int from_tty)
{
int pid = ptid_get_pid (inferior_ptid);
struct remote_state *rs = get_remote_state ();
struct thread_info *tp = find_thread_ptid (inferior_ptid);
int is_fork_parent;
if (args)
error (_("Argument given to \"detach\" when remotely debugging."));
@@ -4458,37 +4492,74 @@ remote_detach_1 (const char *args, int from_tty, int extended)
}
/* Tell the remote target to detach. */
if (remote_multi_process_p (rs))
xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
else
strcpy (rs->buf, "D");
remote_detach_pid (pid);
putpkt (rs->buf);
getpkt (&rs->buf, &rs->buf_size, 0);
if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
;
else if (rs->buf[0] == '\0')
error (_("Remote doesn't know how to detach"));
else
error (_("Can't detach process."));
if (from_tty && !extended)
if (from_tty && !rs->extended)
puts_filtered (_("Ending remote debugging.\n"));
target_mourn_inferior ();
/* Check to see if we are detaching a fork parent. Note that if we
are detaching a fork child, tp == NULL. */
is_fork_parent = (tp != NULL
&& tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
/* If doing detach-on-fork, we don't mourn, because that will delete
breakpoints that should be available for the followed inferior. */
if (!is_fork_parent)
target_mourn_inferior ();
else
{
inferior_ptid = null_ptid;
detach_inferior (pid);
}
}
static void
remote_detach (struct target_ops *ops, const char *args, int from_tty)
{
remote_detach_1 (args, from_tty, 0);
remote_detach_1 (args, from_tty);
}
static void
extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
{
remote_detach_1 (args, from_tty, 1);
remote_detach_1 (args, from_tty);
}
/* Target follow-fork function for remote targets. On entry, and
at return, the current inferior is the fork parent.
Note that although this is currently only used for extended-remote,
it is named remote_follow_fork in anticipation of using it for the
remote target as well. */
static int
remote_follow_fork (struct target_ops *ops, int follow_child,
int detach_fork)
{
struct remote_state *rs = get_remote_state ();
if (remote_fork_event_p (rs))
{
/* When following the parent and detaching the child, we detach
the child here. For the case of following the child and
detaching the parent, the detach is done in the target-
independent follow fork code in infrun.c. We can't use
target_detach when detaching an unfollowed child because
the client side doesn't know anything about the child. */
if (detach_fork && !follow_child)
{
/* Detach the fork child. */
ptid_t child_ptid;
pid_t child_pid;
child_ptid = inferior_thread ()->pending_follow.value.related_pid;
child_pid = ptid_get_pid (child_ptid);
remote_detach_pid (child_pid);
detach_inferior (child_pid);
}
}
return 0;
}
/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
@@ -5651,6 +5722,11 @@ Packet: '%s'\n"),
p = unpack_varlen_hex (++p1, &c);
event->core = c;
}
else if (strncmp (p, "fork", p1 - p) == 0)
{
event->ws.value.related_pid = read_ptid (++p1, &p);
event->ws.kind = TARGET_WAITKIND_FORKED;
}
else
{
ULONGEST pnum;
@@ -9505,8 +9581,11 @@ remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
if (ptid_equal (magic_null_ptid, ptid))
xsnprintf (buf, sizeof buf, "Thread <main>");
else if (rs->extended && remote_multi_process_p (rs))
xsnprintf (buf, sizeof buf, "Thread %d.%ld",
ptid_get_pid (ptid), ptid_get_lwp (ptid));
if (ptid_get_lwp (ptid) == 0)
return normal_pid_to_str (ptid);
else
xsnprintf (buf, sizeof buf, "Thread %d.%ld",
ptid_get_pid (ptid), ptid_get_lwp (ptid));
else
xsnprintf (buf, sizeof buf, "Thread %ld",
ptid_get_lwp (ptid));
@@ -11870,6 +11949,7 @@ Specify the serial device it is connected to (e.g. /dev/ttya).";
extended_remote_ops.to_kill = extended_remote_kill;
extended_remote_ops.to_supports_disable_randomization
= extended_remote_supports_disable_randomization;
extended_remote_ops.to_follow_fork = remote_follow_fork;
}
static int