arc: Fix ARI warning for printf(%p)
[deliverable/binutils-gdb.git] / gdb / inferior.c
index 28a520052ccd98f86639b0e3cb40061b906c0f18..47d91c7037e6563f72a7e7acd008c3871088d8d8 100644 (file)
@@ -1,6 +1,6 @@
 /* Multi-process control for GDB, the GNU debugger.
 
-   Copyright (C) 2008-2013 Free Software Foundation, Inc.
+   Copyright (C) 2008-2016 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -27,7 +27,6 @@
 #include "gdbthread.h"
 #include "ui-out.h"
 #include "observer.h"
-#include "gdbthread.h"
 #include "gdbcore.h"
 #include "symfile.h"
 #include "environ.h"
@@ -75,7 +74,7 @@ set_current_inferior (struct inferior *inf)
 static void
 restore_inferior (void *arg)
 {
-  struct inferior *saved_inferior = arg;
+  struct inferior *saved_inferior = (struct inferior *) arg;
 
   set_current_inferior (saved_inferior);
 }
@@ -102,7 +101,7 @@ free_inferior (struct inferior *inf)
   xfree (inf->terminal);
   free_environ (inf->environment);
   target_desc_info_free (inf->tdesc_info);
-  xfree (inf->private);
+  xfree (inf->priv);
   xfree (inf);
 }
 
@@ -129,15 +128,24 @@ add_inferior_silent (int pid)
 {
   struct inferior *inf;
 
-  inf = xmalloc (sizeof (*inf));
+  inf = XNEW (struct inferior);
   memset (inf, 0, sizeof (*inf));
   inf->pid = pid;
 
   inf->control.stop_soon = NO_STOP_QUIETLY;
 
   inf->num = ++highest_inferior_num;
-  inf->next = inferior_list;
-  inferior_list = inf;
+
+  if (inferior_list == NULL)
+    inferior_list = inf;
+  else
+    {
+      struct inferior *last;
+
+      for (last = inferior_list; last->next != NULL; last = last->next)
+       ;
+      last->next = inf;
+    }
 
   inf->environment = make_environ ();
   init_environ (inf->environment);
@@ -172,7 +180,8 @@ struct delete_thread_of_inferior_arg
 static int
 delete_thread_of_inferior (struct thread_info *tp, void *data)
 {
-  struct delete_thread_of_inferior_arg *arg = data;
+  struct delete_thread_of_inferior_arg *arg
+    = (struct delete_thread_of_inferior_arg *) data;
 
   if (ptid_get_pid (tp->ptid) == arg->pid)
     {
@@ -185,11 +194,8 @@ delete_thread_of_inferior (struct thread_info *tp, void *data)
   return 0;
 }
 
-/* If SILENT then be quiet -- don't announce a inferior death, or the
-   exit of its threads.  */
-
 void
-delete_inferior_1 (struct inferior *todel, int silent)
+delete_inferior (struct inferior *todel)
 {
   struct inferior *inf, *infprev;
   struct delete_thread_of_inferior_arg arg;
@@ -204,7 +210,7 @@ delete_inferior_1 (struct inferior *todel, int silent)
     return;
 
   arg.pid = inf->pid;
-  arg.silent = silent;
+  arg.silent = 1;
 
   iterate_over_threads (delete_thread_of_inferior, &arg);
 
@@ -215,29 +221,13 @@ delete_inferior_1 (struct inferior *todel, int silent)
 
   observer_notify_inferior_removed (inf);
 
-  free_inferior (inf);
-}
-
-void
-delete_inferior (int pid)
-{
-  struct inferior *inf = find_inferior_pid (pid);
-
-  delete_inferior_1 (inf, 0);
+  /* If this program space is rendered useless, remove it. */
+  if (program_space_empty_p (inf->pspace))
+    delete_program_space (inf->pspace);
 
-  if (print_inferior_events)
-    printf_unfiltered (_("[Inferior %d exited]\n"), pid);
-}
-
-void
-delete_inferior_silent (int pid)
-{
-  struct inferior *inf = find_inferior_pid (pid);
-
-  delete_inferior_1 (inf, 1);
+  free_inferior (inf);
 }
 
-
 /* If SILENT then be quiet -- don't announce a inferior exit, or the
    exit of its threads.  */
 
@@ -259,8 +249,6 @@ exit_inferior_1 (struct inferior *inftoex, int silent)
 
   iterate_over_threads (delete_thread_of_inferior, &arg);
 
-  /* Notify the observers before removing the inferior from the list,
-     so that the observers have a chance to look it up.  */
   observer_notify_inferior_exit (inf);
 
   inf->pid = 0;
@@ -276,8 +264,6 @@ exit_inferior_1 (struct inferior *inftoex, int silent)
       inf->vfork_child = NULL;
     }
 
-  inf->has_exit_code = 0;
-  inf->exit_code = 0;
   inf->pending_detach = 0;
 }
 
@@ -313,7 +299,7 @@ detach_inferior (int pid)
 {
   struct inferior *inf = find_inferior_pid (pid);
 
-  exit_inferior_1 (inf, 1);
+  exit_inferior_1 (inf, 0);
 
   if (print_inferior_events)
     printf_unfiltered (_("[Inferior %d detached]\n"), pid);
@@ -323,6 +309,8 @@ void
 inferior_appeared (struct inferior *inf, int pid)
 {
   inf->pid = pid;
+  inf->has_exit_code = 0;
+  inf->exit_code = 0;
 
   observer_notify_inferior_appeared (inf);
 }
@@ -368,12 +356,23 @@ find_inferior_pid (int pid)
   return NULL;
 }
 
-/* Find an inferior bound to PSPACE.  */
+/* See inferior.h */
+
+struct inferior *
+find_inferior_ptid (ptid_t ptid)
+{
+  return find_inferior_pid (ptid_get_pid (ptid));
+}
+
+/* See inferior.h.  */
 
 struct inferior *
 find_inferior_for_program_space (struct program_space *pspace)
 {
-  struct inferior *inf;
+  struct inferior *inf = current_inferior ();
+
+  if (inf->pspace == pspace)
+    return inf;
 
   for (inf = inferior_list; inf != NULL; inf = inf->next)
     {
@@ -458,26 +457,45 @@ have_inferiors (void)
   return 0;
 }
 
+/* Return the number of live inferiors.  We account for the case
+   where an inferior might have a non-zero pid but no threads, as
+   in the middle of a 'mourn' operation.  */
+
 int
-have_live_inferiors (void)
+number_of_live_inferiors (void)
 {
   struct inferior *inf;
+  int num_inf = 0;
 
   for (inf = inferior_list; inf; inf = inf->next)
     if (inf->pid != 0)
       {
        struct thread_info *tp;
-       
-       tp = any_thread_of_process (inf->pid);
-       if (tp && target_has_execution_1 (tp->ptid))
-         break;
+
+       ALL_NON_EXITED_THREADS (tp)
+        if (tp && ptid_get_pid (tp->ptid) == inf->pid)
+          if (target_has_execution_1 (tp->ptid))
+            {
+              /* Found a live thread in this inferior, go to the next
+                 inferior.  */
+              ++num_inf;
+              break;
+            }
       }
 
-  return inf != NULL;
+  return num_inf;
+}
+
+/* Return true if there is at least one live inferior.  */
+
+int
+have_live_inferiors (void)
+{
+  return number_of_live_inferiors () > 0;
 }
 
-/* Prune away automatically added program spaces that aren't required
-   anymore.  */
+/* Prune away any unused inferiors, and then prune away no longer used
+   program spaces.  */
 
 void
 prune_inferiors (void)
@@ -499,11 +517,9 @@ prune_inferiors (void)
        }
 
       *ss_link = ss->next;
-      delete_inferior_1 (ss, 1);
+      delete_inferior (ss);
       ss = *ss_link;
     }
-
-  prune_program_spaces ();
 }
 
 /* Simply returns the count of inferiors.  */
@@ -636,6 +652,11 @@ detach_inferior_command (char *args, int from_tty)
        }
 
       pid = gdb_inferior_id_to_pid (num);
+      if (pid == 0)
+       {
+         warning (_("Inferior ID %d is not running."), num);
+         continue;
+       }
 
       tp = any_thread_of_process (pid);
       if (!tp)
@@ -672,6 +693,11 @@ kill_inferior_command (char *args, int from_tty)
        }
 
       pid = gdb_inferior_id_to_pid (num);
+      if (pid == 0)
+       {
+         warning (_("Inferior ID %d is not running."), num);
+         continue;
+       }
 
       tp = any_thread_of_process (pid);
       if (!tp)
@@ -720,8 +746,8 @@ inferior_command (char *args, int from_tty)
          switch_to_thread (tp->ptid);
        }
 
-      printf_filtered (_("[Switching to thread %d (%s)] "),
-                      pid_to_thread_id (inferior_ptid),
+      printf_filtered (_("[Switching to thread %s (%s)] "),
+                      print_thread_id (inferior_thread ()),
                       target_pid_to_str (inferior_ptid));
     }
   else
@@ -777,7 +803,7 @@ remove_inferior_command (char *args, int from_tty)
 
       if (inf == current_inferior ())
        {
-         warning (_("Can not remove current symbol inferior %d."), num);
+         warning (_("Can not remove current inferior %d."), num);
          continue;
        }
     
@@ -787,7 +813,7 @@ remove_inferior_command (char *args, int from_tty)
          continue;
        }
 
-      delete_inferior_1 (inf, 1);
+      delete_inferior (inf);
     }
 }
 
@@ -980,6 +1006,26 @@ show_print_inferior_events (struct ui_file *file, int from_tty,
   fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
 }
 
+/* Return a new value for the selected inferior's id.  */
+
+static struct value *
+inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
+                       void *ignore)
+{
+  struct inferior *inf = current_inferior ();
+
+  return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
+}
+
+/* Implementation of `$_inferior' variable.  */
+
+static const struct internalvar_funcs inferior_funcs =
+{
+  inferior_id_make_value,
+  NULL,
+  NULL
+};
+
 \f
 
 void
@@ -1043,4 +1089,5 @@ Show printing of inferior events (e.g., inferior start and exit)."), NULL,
          show_print_inferior_events,
          &setprintlist, &showprintlist);
 
+  create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);
 }
This page took 0.030158 seconds and 4 git commands to generate.