gdb/inferior.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Multi-process control for GDB, the GNU debugger.

  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "exec.h"
  16. #include "inferior.h"
  17. #include "target.h"
  18. #include "command.h"
  19. #include "completer.h"
  20. #include "gdbcmd.h"
  21. #include "gdbthread.h"
  22. #include "ui-out.h"
  23. #include "observer.h"
  24. #include "gdbcore.h"
  25. #include "symfile.h"
  26. #include "environ.h"
  27. #include "cli/cli-utils.h"
  28. #include "continuations.h"
  29. #include "arch-utils.h"
  30. #include "target-descriptions.h"
  31. #include "readline/tilde.h"

  32. void _initialize_inferiors (void);

  33. /* Keep a registry of per-inferior data-pointers required by other GDB
  34.    modules.  */

  35. DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)

  36. struct inferior *inferior_list = NULL;
  37. static int highest_inferior_num;

  38. /* Print notices on inferior events (attach, detach, etc.), set with
  39.    `set print inferior-events'.  */
  40. static int print_inferior_events = 0;

  41. /* The Current Inferior.  */
  42. static struct inferior *current_inferior_ = NULL;

  43. struct inferior*
  44. current_inferior (void)
  45. {
  46.   return current_inferior_;
  47. }

  48. void
  49. set_current_inferior (struct inferior *inf)
  50. {
  51.   /* There's always an inferior.  */
  52.   gdb_assert (inf != NULL);

  53.   current_inferior_ = inf;
  54. }

  55. /* A cleanups callback, helper for save_current_program_space
  56.    below.  */

  57. static void
  58. restore_inferior (void *arg)
  59. {
  60.   struct inferior *saved_inferior = arg;

  61.   set_current_inferior (saved_inferior);
  62. }

  63. /* Save the current program space so that it may be restored by a later
  64.    call to do_cleanups.  Returns the struct cleanup pointer needed for
  65.    later doing the cleanup.  */

  66. struct cleanup *
  67. save_current_inferior (void)
  68. {
  69.   struct cleanup *old_chain = make_cleanup (restore_inferior,
  70.                                             current_inferior_);

  71.   return old_chain;
  72. }

  73. static void
  74. free_inferior (struct inferior *inf)
  75. {
  76.   discard_all_inferior_continuations (inf);
  77.   inferior_free_data (inf);
  78.   xfree (inf->args);
  79.   xfree (inf->terminal);
  80.   free_environ (inf->environment);
  81.   target_desc_info_free (inf->tdesc_info);
  82.   xfree (inf->private);
  83.   xfree (inf);
  84. }

  85. void
  86. init_inferior_list (void)
  87. {
  88.   struct inferior *inf, *infnext;

  89.   highest_inferior_num = 0;
  90.   if (!inferior_list)
  91.     return;

  92.   for (inf = inferior_list; inf; inf = infnext)
  93.     {
  94.       infnext = inf->next;
  95.       free_inferior (inf);
  96.     }

  97.   inferior_list = NULL;
  98. }

  99. struct inferior *
  100. add_inferior_silent (int pid)
  101. {
  102.   struct inferior *inf;

  103.   inf = xmalloc (sizeof (*inf));
  104.   memset (inf, 0, sizeof (*inf));
  105.   inf->pid = pid;

  106.   inf->control.stop_soon = NO_STOP_QUIETLY;

  107.   inf->num = ++highest_inferior_num;
  108.   inf->next = inferior_list;
  109.   inferior_list = inf;

  110.   inf->environment = make_environ ();
  111.   init_environ (inf->environment);

  112.   inferior_alloc_data (inf);

  113.   observer_notify_inferior_added (inf);

  114.   if (pid != 0)
  115.     inferior_appeared (inf, pid);

  116.   return inf;
  117. }

  118. struct inferior *
  119. add_inferior (int pid)
  120. {
  121.   struct inferior *inf = add_inferior_silent (pid);

  122.   if (print_inferior_events)
  123.     printf_unfiltered (_("[New inferior %d]\n"), pid);

  124.   return inf;
  125. }

  126. struct delete_thread_of_inferior_arg
  127. {
  128.   int pid;
  129.   int silent;
  130. };

  131. static int
  132. delete_thread_of_inferior (struct thread_info *tp, void *data)
  133. {
  134.   struct delete_thread_of_inferior_arg *arg = data;

  135.   if (ptid_get_pid (tp->ptid) == arg->pid)
  136.     {
  137.       if (arg->silent)
  138.         delete_thread_silent (tp->ptid);
  139.       else
  140.         delete_thread (tp->ptid);
  141.     }

  142.   return 0;
  143. }

  144. /* If SILENT then be quiet -- don't announce a inferior death, or the
  145.    exit of its threads.  */

  146. void
  147. delete_inferior_1 (struct inferior *todel, int silent)
  148. {
  149.   struct inferior *inf, *infprev;
  150.   struct delete_thread_of_inferior_arg arg;

  151.   infprev = NULL;

  152.   for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
  153.     if (inf == todel)
  154.       break;

  155.   if (!inf)
  156.     return;

  157.   arg.pid = inf->pid;
  158.   arg.silent = silent;

  159.   iterate_over_threads (delete_thread_of_inferior, &arg);

  160.   if (infprev)
  161.     infprev->next = inf->next;
  162.   else
  163.     inferior_list = inf->next;

  164.   observer_notify_inferior_removed (inf);

  165.   free_inferior (inf);
  166. }

  167. void
  168. delete_inferior (int pid)
  169. {
  170.   struct inferior *inf = find_inferior_pid (pid);

  171.   delete_inferior_1 (inf, 0);

  172.   if (print_inferior_events)
  173.     printf_unfiltered (_("[Inferior %d exited]\n"), pid);
  174. }

  175. void
  176. delete_inferior_silent (int pid)
  177. {
  178.   struct inferior *inf = find_inferior_pid (pid);

  179.   delete_inferior_1 (inf, 1);
  180. }


  181. /* If SILENT then be quiet -- don't announce a inferior exit, or the
  182.    exit of its threads.  */

  183. static void
  184. exit_inferior_1 (struct inferior *inftoex, int silent)
  185. {
  186.   struct inferior *inf;
  187.   struct delete_thread_of_inferior_arg arg;

  188.   for (inf = inferior_list; inf; inf = inf->next)
  189.     if (inf == inftoex)
  190.       break;

  191.   if (!inf)
  192.     return;

  193.   arg.pid = inf->pid;
  194.   arg.silent = silent;

  195.   iterate_over_threads (delete_thread_of_inferior, &arg);

  196.   /* Notify the observers before removing the inferior from the list,
  197.      so that the observers have a chance to look it up.  */
  198.   observer_notify_inferior_exit (inf);

  199.   inf->pid = 0;
  200.   inf->fake_pid_p = 0;
  201.   if (inf->vfork_parent != NULL)
  202.     {
  203.       inf->vfork_parent->vfork_child = NULL;
  204.       inf->vfork_parent = NULL;
  205.     }
  206.   if (inf->vfork_child != NULL)
  207.     {
  208.       inf->vfork_child->vfork_parent = NULL;
  209.       inf->vfork_child = NULL;
  210.     }

  211.   inf->pending_detach = 0;
  212. }

  213. void
  214. exit_inferior (int pid)
  215. {
  216.   struct inferior *inf = find_inferior_pid (pid);

  217.   exit_inferior_1 (inf, 0);

  218.   if (print_inferior_events)
  219.     printf_unfiltered (_("[Inferior %d exited]\n"), pid);
  220. }

  221. void
  222. exit_inferior_silent (int pid)
  223. {
  224.   struct inferior *inf = find_inferior_pid (pid);

  225.   exit_inferior_1 (inf, 1);
  226. }

  227. void
  228. exit_inferior_num_silent (int num)
  229. {
  230.   struct inferior *inf = find_inferior_id (num);

  231.   exit_inferior_1 (inf, 1);
  232. }

  233. void
  234. detach_inferior (int pid)
  235. {
  236.   struct inferior *inf = find_inferior_pid (pid);

  237.   exit_inferior_1 (inf, 1);

  238.   if (print_inferior_events)
  239.     printf_unfiltered (_("[Inferior %d detached]\n"), pid);
  240. }

  241. void
  242. inferior_appeared (struct inferior *inf, int pid)
  243. {
  244.   inf->pid = pid;
  245.   inf->has_exit_code = 0;
  246.   inf->exit_code = 0;

  247.   observer_notify_inferior_appeared (inf);
  248. }

  249. void
  250. discard_all_inferiors (void)
  251. {
  252.   struct inferior *inf;

  253.   for (inf = inferior_list; inf; inf = inf->next)
  254.     {
  255.       if (inf->pid != 0)
  256.         exit_inferior_silent (inf->pid);
  257.     }
  258. }

  259. struct inferior *
  260. find_inferior_id (int num)
  261. {
  262.   struct inferior *inf;

  263.   for (inf = inferior_list; inf; inf = inf->next)
  264.     if (inf->num == num)
  265.       return inf;

  266.   return NULL;
  267. }

  268. struct inferior *
  269. find_inferior_pid (int pid)
  270. {
  271.   struct inferior *inf;

  272.   /* Looking for inferior pid == 0 is always wrong, and indicative of
  273.      a bug somewhere else.  There may be more than one with pid == 0,
  274.      for instance.  */
  275.   gdb_assert (pid != 0);

  276.   for (inf = inferior_list; inf; inf = inf->next)
  277.     if (inf->pid == pid)
  278.       return inf;

  279.   return NULL;
  280. }

  281. /* See inferior.h */

  282. struct inferior *
  283. find_inferior_ptid (ptid_t ptid)
  284. {
  285.   return find_inferior_pid (ptid_get_pid (ptid));
  286. }

  287. /* See inferior.h.  */

  288. struct inferior *
  289. find_inferior_for_program_space (struct program_space *pspace)
  290. {
  291.   struct inferior *inf = current_inferior ();

  292.   if (inf->pspace == pspace)
  293.     return inf;

  294.   for (inf = inferior_list; inf != NULL; inf = inf->next)
  295.     {
  296.       if (inf->pspace == pspace)
  297.         return inf;
  298.     }

  299.   return NULL;
  300. }

  301. struct inferior *
  302. iterate_over_inferiors (int (*callback) (struct inferior *, void *),
  303.                         void *data)
  304. {
  305.   struct inferior *inf, *infnext;

  306.   for (inf = inferior_list; inf; inf = infnext)
  307.     {
  308.       infnext = inf->next;
  309.       if ((*callback) (inf, data))
  310.         return inf;
  311.     }

  312.   return NULL;
  313. }

  314. int
  315. valid_gdb_inferior_id (int num)
  316. {
  317.   struct inferior *inf;

  318.   for (inf = inferior_list; inf; inf = inf->next)
  319.     if (inf->num == num)
  320.       return 1;

  321.   return 0;
  322. }

  323. int
  324. pid_to_gdb_inferior_id (int pid)
  325. {
  326.   struct inferior *inf;

  327.   for (inf = inferior_list; inf; inf = inf->next)
  328.     if (inf->pid == pid)
  329.       return inf->num;

  330.   return 0;
  331. }

  332. int
  333. gdb_inferior_id_to_pid (int num)
  334. {
  335.   struct inferior *inferior = find_inferior_id (num);
  336.   if (inferior)
  337.     return inferior->pid;
  338.   else
  339.     return -1;
  340. }

  341. int
  342. in_inferior_list (int pid)
  343. {
  344.   struct inferior *inf;

  345.   for (inf = inferior_list; inf; inf = inf->next)
  346.     if (inf->pid == pid)
  347.       return 1;

  348.   return 0;
  349. }

  350. int
  351. have_inferiors (void)
  352. {
  353.   struct inferior *inf;

  354.   for (inf = inferior_list; inf; inf = inf->next)
  355.     if (inf->pid != 0)
  356.       return 1;

  357.   return 0;
  358. }

  359. int
  360. have_live_inferiors (void)
  361. {
  362.   struct inferior *inf;

  363.   for (inf = inferior_list; inf; inf = inf->next)
  364.     if (inf->pid != 0)
  365.       {
  366.         struct thread_info *tp;

  367.         tp = any_thread_of_process (inf->pid);
  368.         if (tp && target_has_execution_1 (tp->ptid))
  369.           break;
  370.       }

  371.   return inf != NULL;
  372. }

  373. /* Prune away any unused inferiors, and then prune away no longer used
  374.    program spaces.  */

  375. void
  376. prune_inferiors (void)
  377. {
  378.   struct inferior *ss, **ss_link;
  379.   struct inferior *current = current_inferior ();

  380.   ss = inferior_list;
  381.   ss_link = &inferior_list;
  382.   while (ss)
  383.     {
  384.       if (ss == current
  385.           || !ss->removable
  386.           || ss->pid != 0)
  387.         {
  388.           ss_link = &ss->next;
  389.           ss = *ss_link;
  390.           continue;
  391.         }

  392.       *ss_link = ss->next;
  393.       delete_inferior_1 (ss, 1);
  394.       ss = *ss_link;
  395.     }

  396.   prune_program_spaces ();
  397. }

  398. /* Simply returns the count of inferiors.  */

  399. int
  400. number_of_inferiors (void)
  401. {
  402.   struct inferior *inf;
  403.   int count = 0;

  404.   for (inf = inferior_list; inf != NULL; inf = inf->next)
  405.     count++;

  406.   return count;
  407. }

  408. /* Converts an inferior process id to a string.  Like
  409.    target_pid_to_str, but special cases the null process.  */

  410. static char *
  411. inferior_pid_to_str (int pid)
  412. {
  413.   if (pid != 0)
  414.     return target_pid_to_str (pid_to_ptid (pid));
  415.   else
  416.     return _("<null>");
  417. }

  418. /* Prints the list of inferiors and their details on UIOUT.  This is a
  419.    version of 'info_inferior_command' suitable for use from MI.

  420.    If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
  421.    inferiors that should be printed.  Otherwise, all inferiors are
  422.    printed.  */

  423. static void
  424. print_inferior (struct ui_out *uiout, char *requested_inferiors)
  425. {
  426.   struct inferior *inf;
  427.   struct cleanup *old_chain;
  428.   int inf_count = 0;

  429.   /* Compute number of inferiors we will print.  */
  430.   for (inf = inferior_list; inf; inf = inf->next)
  431.     {
  432.       if (!number_is_in_list (requested_inferiors, inf->num))
  433.         continue;

  434.       ++inf_count;
  435.     }

  436.   if (inf_count == 0)
  437.     {
  438.       ui_out_message (uiout, 0, "No inferiors.\n");
  439.       return;
  440.     }

  441.   old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
  442.                                                    "inferiors");
  443.   ui_out_table_header (uiout, 1, ui_left, "current", "");
  444.   ui_out_table_header (uiout, 4, ui_left, "number", "Num");
  445.   ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
  446.   ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");

  447.   ui_out_table_body (uiout);
  448.   for (inf = inferior_list; inf; inf = inf->next)
  449.     {
  450.       struct cleanup *chain2;

  451.       if (!number_is_in_list (requested_inferiors, inf->num))
  452.         continue;

  453.       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  454.       if (inf == current_inferior ())
  455.         ui_out_field_string (uiout, "current", "*");
  456.       else
  457.         ui_out_field_skip (uiout, "current");

  458.       ui_out_field_int (uiout, "number", inf->num);

  459.       ui_out_field_string (uiout, "target-id",
  460.                            inferior_pid_to_str (inf->pid));

  461.       if (inf->pspace->pspace_exec_filename != NULL)
  462.         ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
  463.       else
  464.         ui_out_field_skip (uiout, "exec");

  465.       /* Print extra info that isn't really fit to always present in
  466.          tabular form.  Currently we print the vfork parent/child
  467.          relationships, if any.  */
  468.       if (inf->vfork_parent)
  469.         {
  470.           ui_out_text (uiout, _("\n\tis vfork child of inferior "));
  471.           ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
  472.         }
  473.       if (inf->vfork_child)
  474.         {
  475.           ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
  476.           ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
  477.         }

  478.       ui_out_text (uiout, "\n");
  479.       do_cleanups (chain2);
  480.     }

  481.   do_cleanups (old_chain);
  482. }

  483. static void
  484. detach_inferior_command (char *args, int from_tty)
  485. {
  486.   int num, pid;
  487.   struct thread_info *tp;
  488.   struct get_number_or_range_state state;

  489.   if (!args || !*args)
  490.     error (_("Requires argument (inferior id(s) to detach)"));

  491.   init_number_or_range (&state, args);
  492.   while (!state.finished)
  493.     {
  494.       num = get_number_or_range (&state);

  495.       if (!valid_gdb_inferior_id (num))
  496.         {
  497.           warning (_("Inferior ID %d not known."), num);
  498.           continue;
  499.         }

  500.       pid = gdb_inferior_id_to_pid (num);

  501.       tp = any_thread_of_process (pid);
  502.       if (!tp)
  503.         {
  504.           warning (_("Inferior ID %d has no threads."), num);
  505.           continue;
  506.         }

  507.       switch_to_thread (tp->ptid);

  508.       detach_command (NULL, from_tty);
  509.     }
  510. }

  511. static void
  512. kill_inferior_command (char *args, int from_tty)
  513. {
  514.   int num, pid;
  515.   struct thread_info *tp;
  516.   struct get_number_or_range_state state;

  517.   if (!args || !*args)
  518.     error (_("Requires argument (inferior id(s) to kill)"));

  519.   init_number_or_range (&state, args);
  520.   while (!state.finished)
  521.     {
  522.       num = get_number_or_range (&state);

  523.       if (!valid_gdb_inferior_id (num))
  524.         {
  525.           warning (_("Inferior ID %d not known."), num);
  526.           continue;
  527.         }

  528.       pid = gdb_inferior_id_to_pid (num);

  529.       tp = any_thread_of_process (pid);
  530.       if (!tp)
  531.         {
  532.           warning (_("Inferior ID %d has no threads."), num);
  533.           continue;
  534.         }

  535.       switch_to_thread (tp->ptid);

  536.       target_kill ();
  537.     }

  538.   bfd_cache_close_all ();
  539. }

  540. static void
  541. inferior_command (char *args, int from_tty)
  542. {
  543.   struct inferior *inf;
  544.   int num;

  545.   num = parse_and_eval_long (args);

  546.   inf = find_inferior_id (num);
  547.   if (inf == NULL)
  548.     error (_("Inferior ID %d not known."), num);

  549.   printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
  550.                    inf->num,
  551.                    inferior_pid_to_str (inf->pid),
  552.                    (inf->pspace->pspace_exec_filename != NULL
  553.                     ? inf->pspace->pspace_exec_filename
  554.                     : _("<noexec>")));

  555.   if (inf->pid != 0)
  556.     {
  557.       if (inf->pid != ptid_get_pid (inferior_ptid))
  558.         {
  559.           struct thread_info *tp;

  560.           tp = any_thread_of_process (inf->pid);
  561.           if (!tp)
  562.             error (_("Inferior has no threads."));

  563.           switch_to_thread (tp->ptid);
  564.         }

  565.       printf_filtered (_("[Switching to thread %d (%s)] "),
  566.                        pid_to_thread_id (inferior_ptid),
  567.                        target_pid_to_str (inferior_ptid));
  568.     }
  569.   else
  570.     {
  571.       struct inferior *inf;

  572.       inf = find_inferior_id (num);
  573.       set_current_inferior (inf);
  574.       switch_to_thread (null_ptid);
  575.       set_current_program_space (inf->pspace);
  576.     }

  577.   if (inf->pid != 0 && is_running (inferior_ptid))
  578.     ui_out_text (current_uiout, "(running)\n");
  579.   else if (inf->pid != 0)
  580.     {
  581.       ui_out_text (current_uiout, "\n");
  582.       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  583.     }
  584. }

  585. /* Print information about currently known inferiors.  */

  586. static void
  587. info_inferiors_command (char *args, int from_tty)
  588. {
  589.   print_inferior (current_uiout, args);
  590. }

  591. /* remove-inferior ID */

  592. static void
  593. remove_inferior_command (char *args, int from_tty)
  594. {
  595.   int num;
  596.   struct inferior *inf;
  597.   struct get_number_or_range_state state;

  598.   if (args == NULL || *args == '\0')
  599.     error (_("Requires an argument (inferior id(s) to remove)"));

  600.   init_number_or_range (&state, args);
  601.   while (!state.finished)
  602.     {
  603.       num = get_number_or_range (&state);
  604.       inf = find_inferior_id (num);

  605.       if (inf == NULL)
  606.         {
  607.           warning (_("Inferior ID %d not known."), num);
  608.           continue;
  609.         }

  610.       if (inf == current_inferior ())
  611.         {
  612.           warning (_("Can not remove current symbol inferior %d."), num);
  613.           continue;
  614.         }

  615.       if (inf->pid != 0)
  616.         {
  617.           warning (_("Can not remove active inferior %d."), num);
  618.           continue;
  619.         }

  620.       delete_inferior_1 (inf, 1);
  621.     }

  622.   prune_program_spaces ();
  623. }

  624. struct inferior *
  625. add_inferior_with_spaces (void)
  626. {
  627.   struct address_space *aspace;
  628.   struct program_space *pspace;
  629.   struct inferior *inf;
  630.   struct gdbarch_info info;

  631.   /* If all inferiors share an address space on this system, this
  632.      doesn't really return a new address space; otherwise, it
  633.      really does.  */
  634.   aspace = maybe_new_address_space ();
  635.   pspace = add_program_space (aspace);
  636.   inf = add_inferior (0);
  637.   inf->pspace = pspace;
  638.   inf->aspace = pspace->aspace;

  639.   /* Setup the inferior's initial arch, based on information obtained
  640.      from the global "set ..." options.  */
  641.   gdbarch_info_init (&info);
  642.   inf->gdbarch = gdbarch_find_by_info (info);
  643.   /* The "set ..." options reject invalid settings, so we should
  644.      always have a valid arch by now.  */
  645.   gdb_assert (inf->gdbarch != NULL);

  646.   return inf;
  647. }

  648. /* add-inferior [-copies N] [-exec FILENAME]  */

  649. static void
  650. add_inferior_command (char *args, int from_tty)
  651. {
  652.   int i, copies = 1;
  653.   char *exec = NULL;
  654.   char **argv;
  655.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);

  656.   if (args)
  657.     {
  658.       argv = gdb_buildargv (args);
  659.       make_cleanup_freeargv (argv);

  660.       for (; *argv != NULL; argv++)
  661.         {
  662.           if (**argv == '-')
  663.             {
  664.               if (strcmp (*argv, "-copies") == 0)
  665.                 {
  666.                   ++argv;
  667.                   if (!*argv)
  668.                     error (_("No argument to -copies"));
  669.                   copies = parse_and_eval_long (*argv);
  670.                 }
  671.               else if (strcmp (*argv, "-exec") == 0)
  672.                 {
  673.                   ++argv;
  674.                   if (!*argv)
  675.                     error (_("No argument to -exec"));
  676.                   exec = tilde_expand (*argv);
  677.                   make_cleanup (xfree, exec);
  678.                 }
  679.             }
  680.           else
  681.             error (_("Invalid argument"));
  682.         }
  683.     }

  684.   save_current_space_and_thread ();

  685.   for (i = 0; i < copies; ++i)
  686.     {
  687.       struct inferior *inf = add_inferior_with_spaces ();

  688.       printf_filtered (_("Added inferior %d\n"), inf->num);

  689.       if (exec != NULL)
  690.         {
  691.           /* Switch over temporarily, while reading executable and
  692.              symbols.q.  */
  693.           set_current_program_space (inf->pspace);
  694.           set_current_inferior (inf);
  695.           switch_to_thread (null_ptid);

  696.           exec_file_attach (exec, from_tty);
  697.           symbol_file_add_main (exec, from_tty);
  698.         }
  699.     }

  700.   do_cleanups (old_chain);
  701. }

  702. /* clone-inferior [-copies N] [ID] */

  703. static void
  704. clone_inferior_command (char *args, int from_tty)
  705. {
  706.   int i, copies = 1;
  707.   char **argv;
  708.   struct inferior *orginf = NULL;
  709.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);

  710.   if (args)
  711.     {
  712.       argv = gdb_buildargv (args);
  713.       make_cleanup_freeargv (argv);

  714.       for (; *argv != NULL; argv++)
  715.         {
  716.           if (**argv == '-')
  717.             {
  718.               if (strcmp (*argv, "-copies") == 0)
  719.                 {
  720.                   ++argv;
  721.                   if (!*argv)
  722.                     error (_("No argument to -copies"));
  723.                   copies = parse_and_eval_long (*argv);

  724.                   if (copies < 0)
  725.                     error (_("Invalid copies number"));
  726.                 }
  727.             }
  728.           else
  729.             {
  730.               if (orginf == NULL)
  731.                 {
  732.                   int num;

  733.                   /* The first non-option (-) argument specified the
  734.                      program space ID.  */
  735.                   num = parse_and_eval_long (*argv);
  736.                   orginf = find_inferior_id (num);

  737.                   if (orginf == NULL)
  738.                     error (_("Inferior ID %d not known."), num);
  739.                   continue;
  740.                 }
  741.               else
  742.                 error (_("Invalid argument"));
  743.             }
  744.         }
  745.     }

  746.   /* If no inferior id was specified, then the user wants to clone the
  747.      current inferior.  */
  748.   if (orginf == NULL)
  749.     orginf = current_inferior ();

  750.   save_current_space_and_thread ();

  751.   for (i = 0; i < copies; ++i)
  752.     {
  753.       struct address_space *aspace;
  754.       struct program_space *pspace;
  755.       struct inferior *inf;

  756.       /* If all inferiors share an address space on this system, this
  757.          doesn't really return a new address space; otherwise, it
  758.          really does.  */
  759.       aspace = maybe_new_address_space ();
  760.       pspace = add_program_space (aspace);
  761.       inf = add_inferior (0);
  762.       inf->pspace = pspace;
  763.       inf->aspace = pspace->aspace;
  764.       inf->gdbarch = orginf->gdbarch;

  765.       /* If the original inferior had a user specified target
  766.          description, make the clone use it too.  */
  767.       if (target_desc_info_from_user_p (inf->tdesc_info))
  768.         copy_inferior_target_desc_info (inf, orginf);

  769.       printf_filtered (_("Added inferior %d.\n"), inf->num);

  770.       set_current_inferior (inf);
  771.       switch_to_thread (null_ptid);
  772.       clone_program_space (pspace, orginf->pspace);
  773.     }

  774.   do_cleanups (old_chain);
  775. }

  776. /* Print notices when new inferiors are created and die.  */
  777. static void
  778. show_print_inferior_events (struct ui_file *file, int from_tty,
  779.                            struct cmd_list_element *c, const char *value)
  780. {
  781.   fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
  782. }



  783. void
  784. initialize_inferiors (void)
  785. {
  786.   struct cmd_list_element *c = NULL;

  787.   /* There's always one inferior.  Note that this function isn't an
  788.      automatic _initialize_foo function, since other _initialize_foo
  789.      routines may need to install their per-inferior data keys.  We
  790.      can only allocate an inferior when all those modules have done
  791.      that.  Do this after initialize_progspace, due to the
  792.      current_program_space reference.  */
  793.   current_inferior_ = add_inferior (0);
  794.   current_inferior_->pspace = current_program_space;
  795.   current_inferior_->aspace = current_program_space->aspace;
  796.   /* The architecture will be initialized shortly, by
  797.      initialize_current_architecture.  */

  798.   add_info ("inferiors", info_inferiors_command,
  799.             _("IDs of specified inferiors (all inferiors if no argument)."));

  800.   c = add_com ("add-inferior", no_class, add_inferior_command, _("\
  801. Add a new inferior.\n\
  802. Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
  803. N is the optional number of inferiors to add, default is 1.\n\
  804. FILENAME is the file name of the executable to use\n\
  805. as main program."));
  806.   set_cmd_completer (c, filename_completer);

  807.   add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
  808. Remove inferior ID (or list of IDs).\n\
  809. Usage: remove-inferiors ID..."));

  810.   add_com ("clone-inferior", no_class, clone_inferior_command, _("\
  811. Clone inferior ID.\n\
  812. Usage: clone-inferior [-copies <N>] [ID]\n\
  813. Add N copies of inferior ID.  The new inferior has the same\n\
  814. executable loaded as the copied inferior.  If -copies is not specified,\n\
  815. adds 1 copy.  If ID is not specified, it is the current inferior\n\
  816. that is cloned."));

  817.   add_cmd ("inferiors", class_run, detach_inferior_command, _("\
  818. Detach from inferior ID (or list of IDS)."),
  819.            &detachlist);

  820.   add_cmd ("inferiors", class_run, kill_inferior_command, _("\
  821. Kill inferior ID (or list of IDs)."),
  822.            &killlist);

  823.   add_cmd ("inferior", class_run, inferior_command, _("\
  824. Use this command to switch between inferiors.\n\
  825. The new inferior ID must be currently known."),
  826.            &cmdlist);

  827.   add_setshow_boolean_cmd ("inferior-events", no_class,
  828.          &print_inferior_events, _("\
  829. Set printing of inferior events (e.g., inferior start and exit)."), _("\
  830. Show printing of inferior events (e.g., inferior start and exit)."), NULL,
  831.          NULL,
  832.          show_print_inferior_events,
  833.          &setprintlist, &showprintlist);

  834. }