Skip to content

Commit

Permalink
Extended-remote follow-exec
Browse files Browse the repository at this point in the history
This patch implements support for exec events on extended-remote Linux
targets.  Follow-exec-mode and rerun behave as expected.  Catchpoints and
test updates are implemented in subsequent patches.

This patch was derived from a patch posted last October:
https://sourceware.org/ml/gdb-patches/2014-10/msg00877.html.
It was originally based on some work done by Luis Machado in 2013.

IMPLEMENTATION
----------------
Exec events are enabled via ptrace options.

When an exec event is detected by gdbserver, the existing process
data, along with all its associated lwp and thread data, is deleted
and replaced by data for a new single-threaded process.  The new
process data is initialized with the appropriate parts of the state
of the execing process.  This approach takes care of several potential
pitfalls, including:

 * deleting the data for an execing non-leader thread before any
   wait/sigsuspend occurs
 * correctly initializing the architecture of the execed process

We then report the exec event using a new RSP stop reason, "exec".

When GDB receives an "exec" event, it saves the status in the event
structure's target_waitstatus field, like what is done for remote fork
events.  Because the original and execed programs may have different
architectures, we skip parsing the section of the stop reply packet
that contains register data.  The register data will be retrieved
later after the inferior's architecture has been set up by
infrun.c:follow_exec.

At that point the exec event is handled by the existing event handling
in GDB.  However, a few changes were necessary so that
infrun.c:follow_exec could accommodate the remote target.

 * Where follow-exec-mode "new" is handled, we now call
   add_inferior_with_spaces instead of add_inferior with separate calls
   to set up the program and address spaces.  The motivation for this
   is that add_inferior_with_spaces also sets up the initial architecture
   for the inferior, which is needed later by target_find_description
   when it calls target_gdbarch.

 * We call a new target function, target_follow_exec.  This function
   allows us to store the execd_pathname in the inferior, instead of
   using the static string remote_exec_file from remote.c.  The static
   string didn't work for follow-exec-mode "new", since once you switched
   to the execed program, the original remote exec-file was lost.  The
   execd_pathname is now stored in the inferior's program space as a
   REGISTRY field.  All of the requisite mechanisms for this are
   defined in remote.c.

gdb/gdbserver/ChangeLog:

	* linux-low.c (linux_mourn): Static declaration.
	(linux_arch_setup): Move in front of
	handle_extended_wait.
	(linux_arch_setup_thread): New function.
	(handle_extended_wait): Handle exec events.  Call
	linux_arch_setup_thread.  Make event_lwp argument a
	pointer-to-a-pointer.
	(check_zombie_leaders): Do not check stopped threads.
	(linux_low_ptrace_options): Add PTRACE_O_TRACEEXEC.
	(linux_low_filter_event): Add lwp and thread for exec'ing
	non-leader thread if leader thread has been deleted.
	Refactor code into linux_arch_setup_thread and call it.
	Pass child lwp pointer by reference to handle_extended_wait.
	(linux_wait_for_event_filtered): Update comment.
	(linux_wait_1): Prevent clobbering exec event status.
	(linux_supports_exec_events): New function.
	(linux_target_ops) <supports_exec_events>: Initialize new member.
	* lynx-low.c (lynx_target_ops) <supports_exec_events>: Initialize
	new member.
	* remote-utils.c (prepare_resume_reply): New stop reason 'exec'.
	* server.c (report_exec_events): New global variable.
	(handle_query): Handle qSupported query for exec-events feature.
	(captured_main): Initialize report_exec_events.
	* server.h (report_exec_events): Declare new global variable.
	* target.h (struct target_ops) <supports_exec_events>: New
	member.
	(target_supports_exec_events): New macro.
	* win32-low.c (win32_target_ops) <supports_exec_events>:
	Initialize new member.

gdb/ChangeLog:

	* infrun.c (follow_exec): Use process-style ptid for
	exec message.  Call add_inferior_with_spaces and
	target_follow_exec.
	* nat/linux-ptrace.c (linux_supports_traceexec): New function.
	* nat/linux-ptrace.h (linux_supports_traceexec): Declare.
	* remote.c (remote_pspace_data): New static variable.
	(remote_pspace_data_cleanup): New function.
	(get_remote_exec_file): New function.
	(set_remote_exec_file_1): New function.
	(set_remote_exec_file): New function.
	(show_remote_exec_file): New function.
	(remote_exec_file): Delete static variable.
	(anonymous enum) <PACKET_exec_event_feature> New
	enumeration constant.
	(remote_protocol_features): Add entry for exec-events feature.
	(remote_query_supported): Add client side of qSupported query
	for exec-events feature.
	(remote_follow_exec): New function.
	(remote_parse_stop_reply): Handle 'exec' stop reason.
	(extended_remote_run, extended_remote_create_inferior): Call
	get_remote_exec_file and set_remote_exec_file_1.
	(init_extended_remote_ops) <to_follow_exec>: Initialize new
	member.
	(_initialize_remote): Call
	register_program_space_data_with_cleanup.  Call
	add_packet_config_cmd for remote exec-events feature.
	Modify call to add_setshow_string_noescape_cmd for exec-file
	to use new functions set_remote_exec_file and
	show_remote_exec_file.
	* target-debug.h, target-delegates.c: Regenerated.
	* target.c (target_follow_exec): New function.
	* target.h (struct target_ops) <to_follow_exec>: New member.
	(target_follow_exec): Declare new function.
  • Loading branch information
donb1999 committed Sep 11, 2015
1 parent 6d636d8 commit 9458516
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 41 deletions.
37 changes: 37 additions & 0 deletions gdb/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
2015-09-11 Don Breazeal <donb@codesourcery.com>
Luis Machado <lgustavo@codesourcery.com>

* infrun.c (follow_exec): Use process-style ptid for
exec message. Call add_inferior_with_spaces and
target_follow_exec.
* nat/linux-ptrace.c (linux_supports_traceexec): New function.
* nat/linux-ptrace.h (linux_supports_traceexec): Declare.
* remote.c (remote_pspace_data): New static variable.
(remote_pspace_data_cleanup): New function.
(get_remote_exec_file): New function.
(set_remote_exec_file_1): New function.
(set_remote_exec_file): New function.
(show_remote_exec_file): New function.
(remote_exec_file): Delete static variable.
(anonymous enum) <PACKET_exec_event_feature>: New
enumeration constant.
(remote_protocol_features): Add entry for exec-events feature.
(remote_query_supported): Add client side of qSupported query
for exec-events feature.
(remote_follow_exec): New function.
(remote_parse_stop_reply): Handle 'exec' stop reason.
(extended_remote_run, extended_remote_create_inferior): Call
get_remote_exec_file and set_remote_exec_file_1.
(init_extended_remote_ops) <to_follow_exec>: Initialize new
member.
(_initialize_remote): Call
register_program_space_data_with_cleanup. Call
add_packet_config_cmd for remote exec-events feature.
Modify call to add_setshow_string_noescape_cmd for exec-file
to use new functions set_remote_exec_file and
show_remote_exec_file.
* target-debug.h, target-delegates.c: Regenerated.
* target.c (target_follow_exec): New function.
* target.h (struct target_ops) <to_follow_exec>: New member.
(target_follow_exec): Declare new function.

2015-09-11 Pierre Langlois <pierre.langlois@arm.com>

* aarch64-tdep.c (decode_cb): Move up comment describing the
Expand Down
33 changes: 33 additions & 0 deletions gdb/gdbserver/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
2015-09-11 Don Breazeal <donb@codesourcery.com>
Luis Machado <lgustavo@codesourcery.com>

* linux-low.c (linux_mourn): Static declaration.
(linux_arch_setup): Move in front of
handle_extended_wait.
(linux_arch_setup_thread): New function.
(handle_extended_wait): Handle exec events. Call
linux_arch_setup_thread. Make event_lwp argument a
pointer-to-a-pointer.
(check_zombie_leaders): Do not check stopped threads.
(linux_low_ptrace_options): Add PTRACE_O_TRACEEXEC.
(linux_low_filter_event): Add lwp and thread for exec'ing
non-leader thread if leader thread has been deleted.
Refactor code into linux_arch_setup_thread and call it.
Pass child lwp pointer by reference to handle_extended_wait.
(linux_wait_for_event_filtered): Update comment.
(linux_wait_1): Prevent clobbering exec event status.
(linux_supports_exec_events): New function.
(linux_target_ops) <supports_exec_events>: Initialize new member.
* lynx-low.c (lynx_target_ops) <supports_exec_events>: Initialize
new member.
* remote-utils.c (prepare_resume_reply): New stop reason 'exec'.
* server.c (report_exec_events): New global variable.
(handle_query): Handle qSupported query for exec-events feature.
(captured_main): Initialize report_exec_events.
* server.h (report_exec_events): Declare new global variable.
* target.h (struct target_ops) <supports_exec_events>: New
member.
(target_supports_exec_events): New macro.
* win32-low.c (win32_target_ops) <supports_exec_events>:
Initialize new member.

2015-09-09 Markus Metzger <markus.t.metzger@intel.com>

* linux-low.c (linux_low_enable_btrace): Remove.
Expand Down
147 changes: 123 additions & 24 deletions gdb/gdbserver/linux-low.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ static int linux_wait_for_event_filtered (ptid_t wait_ptid, ptid_t filter_ptid,
int *wstat, int options);
static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
static struct lwp_info *add_lwp (ptid_t ptid);
static void linux_mourn (struct process_info *process);
static int linux_stopped_by_watchpoint (void);
static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
static int lwp_is_marked_dead (struct lwp_info *lwp);
Expand Down Expand Up @@ -419,13 +420,39 @@ linux_add_process (int pid, int attached)

static CORE_ADDR get_pc (struct lwp_info *lwp);

/* Handle a GNU/Linux extended wait response. If we see a clone
event, we need to add the new LWP to our list (and return 0 so as
not to report the trap to higher layers). */
/* Implement the arch_setup target_ops method. */

static void
linux_arch_setup (void)
{
the_low_target.arch_setup ();
}

/* Call the target arch_setup function on THREAD. */

static void
linux_arch_setup_thread (struct thread_info *thread)
{
struct thread_info *saved_thread;

saved_thread = current_thread;
current_thread = thread;

linux_arch_setup ();

current_thread = saved_thread;
}

/* Handle a GNU/Linux extended wait response. If we see a clone,
fork, or vfork event, we need to add the new LWP to our list
(and return 0 so as not to report the trap to higher layers).
If we see an exec event, we will modify ORIG_EVENT_LWP to point
to a new LWP representing the new program. */

static int
handle_extended_wait (struct lwp_info *event_lwp, int wstat)
handle_extended_wait (struct lwp_info **orig_event_lwp, int wstat)
{
struct lwp_info *event_lwp = *orig_event_lwp;
int event = linux_ptrace_get_extended_event (wstat);
struct thread_info *event_thr = get_lwp_thread (event_lwp);
struct lwp_info *new_lwp;
Expand Down Expand Up @@ -571,6 +598,50 @@ handle_extended_wait (struct lwp_info *event_lwp, int wstat)
/* Report the event. */
return 0;
}
else if (event == PTRACE_EVENT_EXEC && report_exec_events)
{
struct process_info *proc;
ptid_t event_ptid;
pid_t event_pid;

if (debug_threads)
{
debug_printf ("HEW: Got exec event from LWP %ld\n",
lwpid_of (event_thr));
}

/* Get the event ptid. */
event_ptid = ptid_of (event_thr);
event_pid = ptid_get_pid (event_ptid);

/* Delete the execing process and all its threads. */
proc = get_thread_process (event_thr);
linux_mourn (proc);
current_thread = NULL;

/* Create a new process/lwp/thread. */
proc = linux_add_process (event_pid, 0);
event_lwp = add_lwp (event_ptid);
event_thr = get_lwp_thread (event_lwp);
gdb_assert (current_thread == event_thr);
linux_arch_setup_thread (event_thr);

/* Set the event status. */
event_lwp->waitstatus.kind = TARGET_WAITKIND_EXECD;
event_lwp->waitstatus.value.execd_pathname
= xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr)));

/* Mark the exec status as pending. */
event_lwp->stopped = 1;
event_lwp->status_pending_p = 1;
event_lwp->status_pending = wstat;
event_thr->last_resume_kind = resume_continue;
event_thr->last_status.kind = TARGET_WAITKIND_IGNORE;

/* Report the event. */
*orig_event_lwp = event_lwp;
return 0;
}

internal_error (__FILE__, __LINE__, _("unknown ptrace event %d"), event);
}
Expand Down Expand Up @@ -839,14 +910,6 @@ linux_create_inferior (char *program, char **allargs)
return pid;
}

/* Implement the arch_setup target_ops method. */

static void
linux_arch_setup (void)
{
the_low_target.arch_setup ();
}

/* Attach to an inferior process. Returns 0 on success, ERRNO on
error. */

Expand Down Expand Up @@ -1639,7 +1702,7 @@ check_zombie_leaders (void)
leader_pid, leader_lp!= NULL, num_lwps (leader_pid),
linux_proc_pid_is_zombie (leader_pid));

if (leader_lp != NULL
if (leader_lp != NULL && !leader_lp->stopped
/* Check if there are other threads in the group, as we may
have raced with the inferior simply exiting. */
&& !last_thread_of_process_p (leader_pid)
Expand Down Expand Up @@ -2098,6 +2161,9 @@ linux_low_ptrace_options (int attached)
if (report_vfork_events)
options |= (PTRACE_O_TRACEVFORK | PTRACE_O_TRACEVFORKDONE);

if (report_exec_events)
options |= PTRACE_O_TRACEEXEC;

return options;
}

Expand All @@ -2114,6 +2180,38 @@ linux_low_filter_event (int lwpid, int wstat)

child = find_lwp_pid (pid_to_ptid (lwpid));

/* Check for stop events reported by a process we didn't already
know about - anything not already in our LWP list.
If we're expecting to receive stopped processes after
fork, vfork, and clone events, then we'll just add the
new one to our list and go back to waiting for the event
to be reported - the stopped process might be returned
from waitpid before or after the event is.
But note the case of a non-leader thread exec'ing after the
leader having exited, and gone from our lists (because
check_zombie_leaders deleted it). The non-leader thread
changes its tid to the tgid. */

if (WIFSTOPPED (wstat) && child == NULL && WSTOPSIG (wstat) == SIGTRAP
&& linux_ptrace_get_extended_event (wstat) == PTRACE_EVENT_EXEC)
{
ptid_t child_ptid;

/* A multi-thread exec after we had seen the leader exiting. */
if (debug_threads)
{
debug_printf ("LLW: Re-adding thread group leader LWP %d"
"after exec.\n", lwpid);
}

child_ptid = ptid_build (lwpid, lwpid, 0);
child = add_lwp (child_ptid);
child->stopped = 1;
current_thread = child->thread;
}

/* If we didn't find a process, one of two things presumably happened:
- A process we started and then detached from has exited. Ignore it.
- A process we are controlling has forked and the new child's stop
Expand Down Expand Up @@ -2171,17 +2269,10 @@ linux_low_filter_event (int lwpid, int wstat)
{
if (proc->attached)
{
struct thread_info *saved_thread;

/* This needs to happen after we have attached to the
inferior and it is stopped for the first time, but
before we access any inferior registers. */
saved_thread = current_thread;
current_thread = thread;

the_low_target.arch_setup ();

current_thread = saved_thread;
linux_arch_setup_thread (thread);
}
else
{
Expand Down Expand Up @@ -2210,7 +2301,7 @@ linux_low_filter_event (int lwpid, int wstat)
&& linux_is_extended_waitstatus (wstat))
{
child->stop_pc = get_pc (child);
if (handle_extended_wait (child, wstat))
if (handle_extended_wait (&child, wstat))
{
/* The event has been handled, so just return without
reporting it. */
Expand Down Expand Up @@ -2419,8 +2510,7 @@ linux_wait_for_event_filtered (ptid_t wait_ptid, ptid_t filter_ptid,
- When a non-leader thread execs, that thread just vanishes
without reporting an exit (so we'd hang if we waited for it
explicitly in that case). The exec event is reported to
the TGID pid (although we don't currently enable exec
events). */
the TGID pid. */
errno = 0;
ret = my_waitpid (-1, wstatp, options | WNOHANG);

Expand Down Expand Up @@ -5801,6 +5891,14 @@ linux_supports_vfork_events (void)
return linux_supports_tracefork ();
}

/* Check if exec events are supported. */

static int
linux_supports_exec_events (void)
{
return linux_supports_traceexec ();
}

/* Callback for 'find_inferior'. Set the (possibly changed) ptrace
options for the specified lwp. */

Expand Down Expand Up @@ -6891,6 +6989,7 @@ static struct target_ops linux_target_ops = {
linux_supports_multi_process,
linux_supports_fork_events,
linux_supports_vfork_events,
linux_supports_exec_events,
linux_handle_new_gdb_connection,
#ifdef USE_THREAD_DB
thread_db_handle_monitor_command,
Expand Down
1 change: 1 addition & 0 deletions gdb/gdbserver/lynx-low.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ static struct target_ops lynx_target_ops = {
NULL, /* supports_multi_process */
NULL, /* supports_fork_events */
NULL, /* supports_vfork_events */
NULL, /* supports_exec_events */
NULL, /* handle_new_gdb_connection */
NULL, /* handle_monitor_command */
};
Expand Down
20 changes: 20 additions & 0 deletions gdb/gdbserver/remote-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@ prepare_resume_reply (char *buf, ptid_t ptid,
case TARGET_WAITKIND_STOPPED:
case TARGET_WAITKIND_FORKED:
case TARGET_WAITKIND_VFORKED:
case TARGET_WAITKIND_EXECD:
{
struct thread_info *saved_thread;
const char **regp;
Expand All @@ -1134,6 +1135,25 @@ prepare_resume_reply (char *buf, ptid_t ptid,
buf = write_ptid (buf, status->value.related_pid);
strcat (buf, ";");
}
else if (status->kind == TARGET_WAITKIND_EXECD && multi_process)
{
enum gdb_signal signal = GDB_SIGNAL_TRAP;
const char *event = "exec";
char hexified_pathname[PATH_MAX * 2];

sprintf (buf, "T%02x%s:", signal, event);
buf += strlen (buf);

/* Encode pathname to hexified format. */
bin2hex ((const gdb_byte *) status->value.execd_pathname,
hexified_pathname,
strlen (status->value.execd_pathname));

sprintf (buf, "%s;", hexified_pathname);
xfree (status->value.execd_pathname);
status->value.execd_pathname = NULL;
buf += strlen (buf);
}
else
sprintf (buf, "T%02x", status->value.sig);

Expand Down
11 changes: 11 additions & 0 deletions gdb/gdbserver/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int run_once;
int multi_process;
int report_fork_events;
int report_vfork_events;
int report_exec_events;
int non_stop;
int swbreak_feature;
int hwbreak_feature;
Expand Down Expand Up @@ -2111,6 +2112,12 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
if (target_supports_vfork_events ())
report_vfork_events = 1;
}
if (strcmp (p, "exec-events+") == 0)
{
/* GDB supports and wants exec events if possible. */
if (target_supports_exec_events ())
report_exec_events = 1;
}
else
target_process_qsupported (p);

Expand Down Expand Up @@ -2167,6 +2174,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
if (target_supports_vfork_events ())
strcat (own_buf, ";vfork-events+");

if (target_supports_exec_events ())
strcat (own_buf, ";exec-events+");

if (target_supports_non_stop ())
strcat (own_buf, ";QNonStop+");

Expand Down Expand Up @@ -3544,6 +3554,7 @@ captured_main (int argc, char *argv[])
multi_process = 0;
report_fork_events = 0;
report_vfork_events = 0;
report_exec_events = 0;
/* Be sure we're out of tfind mode. */
current_traceframe = -1;
cont_thread = null_ptid;
Expand Down
Loading

0 comments on commit 9458516

Please sign in to comment.