2009-04-30 Doug Evans <dje@google.com>
+ * inferiors.c (started_inferior_callback): New function.
+ (attached_inferior_callback): New function.
+ (have_started_inferiors_p, have_attached_inferiors_p): New functions.
+ * server.c (print_started_pid, print_attached_pid): New functions.
+ (detach_or_kill_for_exit): New function.
+ (main): Call it instead of for_each_inferior (kill_inferior_callback).
+ * server.h (have_started_inferiors_p): Declare.
+ (have_attached_inferiors_p): Declare.
+
* inferiors.c (remove_process): Fix memory leak, free process.
* linux-low.c (linux_remove_process): New function.
(linux_kill): Call it instead of remove_process.
list->tail = new_inferior;
}
+/* Invoke ACTION for each inferior in LIST. */
+
void
for_each_inferior (struct inferior_list *list,
void (*action) (struct inferior_list_entry *))
find_inferior_id (&all_processes, pid_to_ptid (pid));
}
+/* Return non-zero if INF, a struct process_info, was started by us,
+ i.e. not attached to. */
+
+static int
+started_inferior_callback (struct inferior_list_entry *entry, void *args)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ return ! process->attached;
+}
+
+/* Return non-zero if there are any inferiors that we have created
+ (as opposed to attached-to). */
+
+int
+have_started_inferiors_p (void)
+{
+ return (find_inferior (&all_processes, started_inferior_callback, NULL)
+ != NULL);
+}
+
+/* Return non-zero if INF, a struct process_info, was attached to. */
+
+static int
+attached_inferior_callback (struct inferior_list_entry *entry, void *args)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ return process->attached;
+}
+
+/* Return non-zero if there are any inferiors that we have attached to. */
+
+int
+have_attached_inferiors_p (void)
+{
+ return (find_inferior (&all_processes, attached_inferior_callback, NULL)
+ != NULL);
+}
+
struct process_info *
get_thread_process (struct thread_info *thread)
{
discard_queued_stop_replies (pid);
}
+/* Callback for for_each_inferior to detach or kill the inferior,
+ depending on whether we attached to it or not.
+ We inform the user whether we're detaching or killing the process
+ as this is only called when gdbserver is about to exit. */
+
static void
detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
{
discard_queued_stop_replies (pid);
}
+/* for_each_inferior callback for detach_or_kill_for_exit to print
+ the pids of started inferiors. */
+
+static void
+print_started_pid (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ if (! process->attached)
+ {
+ int pid = ptid_get_pid (process->head.id);
+ fprintf (stderr, " %d", pid);
+ }
+}
+
+/* for_each_inferior callback for detach_or_kill_for_exit to print
+ the pids of attached inferiors. */
+
+static void
+print_attached_pid (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ if (process->attached)
+ {
+ int pid = ptid_get_pid (process->head.id);
+ fprintf (stderr, " %d", pid);
+ }
+}
+
+/* Call this when exiting gdbserver with possible inferiors that need
+ to be killed or detached from. */
+
+static void
+detach_or_kill_for_exit (void)
+{
+ /* First print a list of the inferiors we will be killing/detaching.
+ This is to assist the user, for example, in case the inferior unexpectedly
+ dies after we exit: did we screw up or did the inferior exit on its own?
+ Having this info will save some head-scratching. */
+
+ if (have_started_inferiors_p ())
+ {
+ fprintf (stderr, "Killing process(es):");
+ for_each_inferior (&all_processes, print_started_pid);
+ fprintf (stderr, "\n");
+ }
+ if (have_attached_inferiors_p ())
+ {
+ fprintf (stderr, "Detaching process(es):");
+ for_each_inferior (&all_processes, print_attached_pid);
+ fprintf (stderr, "\n");
+ }
+
+ /* Now we can kill or detach the inferiors. */
+
+ for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
+}
+
static void
join_inferiors_callback (struct inferior_list_entry *entry)
{
if (setjmp (toplevel))
{
- fprintf (stderr, "Killing all inferiors\n");
- for_each_inferior (&all_processes,
- kill_inferior_callback);
+ detach_or_kill_for_exit ();
exit (1);
}
if (exit_requested)
{
- for_each_inferior (&all_processes,
- detach_or_kill_inferior_callback);
+ detach_or_kill_for_exit ();
exit (0);
}
else