GDB (API)
/home/stan/gdb/src/gdb/inferior.c
Go to the documentation of this file.
00001 /* Multi-process control for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "exec.h"
00022 #include "inferior.h"
00023 #include "target.h"
00024 #include "command.h"
00025 #include "completer.h"
00026 #include "gdbcmd.h"
00027 #include "gdbthread.h"
00028 #include "ui-out.h"
00029 #include "observer.h"
00030 #include "gdbthread.h"
00031 #include "gdbcore.h"
00032 #include "symfile.h"
00033 #include "environ.h"
00034 #include "cli/cli-utils.h"
00035 #include "continuations.h"
00036 #include "arch-utils.h"
00037 #include "target-descriptions.h"
00038 #include "readline/tilde.h"
00039 
00040 void _initialize_inferiors (void);
00041 
00042 /* Keep a registry of per-inferior data-pointers required by other GDB
00043    modules.  */
00044 
00045 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
00046 
00047 struct inferior *inferior_list = NULL;
00048 static int highest_inferior_num;
00049 
00050 /* Print notices on inferior events (attach, detach, etc.), set with
00051    `set print inferior-events'.  */
00052 static int print_inferior_events = 0;
00053 
00054 /* The Current Inferior.  */
00055 static struct inferior *current_inferior_ = NULL;
00056 
00057 struct inferior*
00058 current_inferior (void)
00059 {
00060   return current_inferior_;
00061 }
00062 
00063 void
00064 set_current_inferior (struct inferior *inf)
00065 {
00066   /* There's always an inferior.  */
00067   gdb_assert (inf != NULL);
00068 
00069   current_inferior_ = inf;
00070 }
00071 
00072 /* A cleanups callback, helper for save_current_program_space
00073    below.  */
00074 
00075 static void
00076 restore_inferior (void *arg)
00077 {
00078   struct inferior *saved_inferior = arg;
00079 
00080   set_current_inferior (saved_inferior);
00081 }
00082 
00083 /* Save the current program space so that it may be restored by a later
00084    call to do_cleanups.  Returns the struct cleanup pointer needed for
00085    later doing the cleanup.  */
00086 
00087 struct cleanup *
00088 save_current_inferior (void)
00089 {
00090   struct cleanup *old_chain = make_cleanup (restore_inferior,
00091                                             current_inferior_);
00092 
00093   return old_chain;
00094 }
00095 
00096 static void
00097 free_inferior (struct inferior *inf)
00098 {
00099   discard_all_inferior_continuations (inf);
00100   inferior_free_data (inf);
00101   xfree (inf->args);
00102   xfree (inf->terminal);
00103   free_environ (inf->environment);
00104   target_desc_info_free (inf->tdesc_info);
00105   xfree (inf->private);
00106   xfree (inf);
00107 }
00108 
00109 void
00110 init_inferior_list (void)
00111 {
00112   struct inferior *inf, *infnext;
00113 
00114   highest_inferior_num = 0;
00115   if (!inferior_list)
00116     return;
00117 
00118   for (inf = inferior_list; inf; inf = infnext)
00119     {
00120       infnext = inf->next;
00121       free_inferior (inf);
00122     }
00123 
00124   inferior_list = NULL;
00125 }
00126 
00127 struct inferior *
00128 add_inferior_silent (int pid)
00129 {
00130   struct inferior *inf;
00131 
00132   inf = xmalloc (sizeof (*inf));
00133   memset (inf, 0, sizeof (*inf));
00134   inf->pid = pid;
00135 
00136   inf->control.stop_soon = NO_STOP_QUIETLY;
00137 
00138   inf->num = ++highest_inferior_num;
00139   inf->next = inferior_list;
00140   inferior_list = inf;
00141 
00142   inf->environment = make_environ ();
00143   init_environ (inf->environment);
00144 
00145   inferior_alloc_data (inf);
00146 
00147   observer_notify_inferior_added (inf);
00148 
00149   if (pid != 0)
00150     inferior_appeared (inf, pid);
00151 
00152   return inf;
00153 }
00154 
00155 struct inferior *
00156 add_inferior (int pid)
00157 {
00158   struct inferior *inf = add_inferior_silent (pid);
00159 
00160   if (print_inferior_events)
00161     printf_unfiltered (_("[New inferior %d]\n"), pid);
00162 
00163   return inf;
00164 }
00165 
00166 struct delete_thread_of_inferior_arg
00167 {
00168   int pid;
00169   int silent;
00170 };
00171 
00172 static int
00173 delete_thread_of_inferior (struct thread_info *tp, void *data)
00174 {
00175   struct delete_thread_of_inferior_arg *arg = data;
00176 
00177   if (ptid_get_pid (tp->ptid) == arg->pid)
00178     {
00179       if (arg->silent)
00180         delete_thread_silent (tp->ptid);
00181       else
00182         delete_thread (tp->ptid);
00183     }
00184 
00185   return 0;
00186 }
00187 
00188 /* If SILENT then be quiet -- don't announce a inferior death, or the
00189    exit of its threads.  */
00190 
00191 void
00192 delete_inferior_1 (struct inferior *todel, int silent)
00193 {
00194   struct inferior *inf, *infprev;
00195   struct delete_thread_of_inferior_arg arg;
00196 
00197   infprev = NULL;
00198 
00199   for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
00200     if (inf == todel)
00201       break;
00202 
00203   if (!inf)
00204     return;
00205 
00206   arg.pid = inf->pid;
00207   arg.silent = silent;
00208 
00209   iterate_over_threads (delete_thread_of_inferior, &arg);
00210 
00211   if (infprev)
00212     infprev->next = inf->next;
00213   else
00214     inferior_list = inf->next;
00215 
00216   observer_notify_inferior_removed (inf);
00217 
00218   free_inferior (inf);
00219 }
00220 
00221 void
00222 delete_inferior (int pid)
00223 {
00224   struct inferior *inf = find_inferior_pid (pid);
00225 
00226   delete_inferior_1 (inf, 0);
00227 
00228   if (print_inferior_events)
00229     printf_unfiltered (_("[Inferior %d exited]\n"), pid);
00230 }
00231 
00232 void
00233 delete_inferior_silent (int pid)
00234 {
00235   struct inferior *inf = find_inferior_pid (pid);
00236 
00237   delete_inferior_1 (inf, 1);
00238 }
00239 
00240 
00241 /* If SILENT then be quiet -- don't announce a inferior exit, or the
00242    exit of its threads.  */
00243 
00244 static void
00245 exit_inferior_1 (struct inferior *inftoex, int silent)
00246 {
00247   struct inferior *inf;
00248   struct delete_thread_of_inferior_arg arg;
00249 
00250   for (inf = inferior_list; inf; inf = inf->next)
00251     if (inf == inftoex)
00252       break;
00253 
00254   if (!inf)
00255     return;
00256 
00257   arg.pid = inf->pid;
00258   arg.silent = silent;
00259 
00260   iterate_over_threads (delete_thread_of_inferior, &arg);
00261 
00262   /* Notify the observers before removing the inferior from the list,
00263      so that the observers have a chance to look it up.  */
00264   observer_notify_inferior_exit (inf);
00265 
00266   inf->pid = 0;
00267   inf->fake_pid_p = 0;
00268   if (inf->vfork_parent != NULL)
00269     {
00270       inf->vfork_parent->vfork_child = NULL;
00271       inf->vfork_parent = NULL;
00272     }
00273   if (inf->vfork_child != NULL)
00274     {
00275       inf->vfork_child->vfork_parent = NULL;
00276       inf->vfork_child = NULL;
00277     }
00278 
00279   inf->has_exit_code = 0;
00280   inf->exit_code = 0;
00281   inf->pending_detach = 0;
00282 }
00283 
00284 void
00285 exit_inferior (int pid)
00286 {
00287   struct inferior *inf = find_inferior_pid (pid);
00288 
00289   exit_inferior_1 (inf, 0);
00290 
00291   if (print_inferior_events)
00292     printf_unfiltered (_("[Inferior %d exited]\n"), pid);
00293 }
00294 
00295 void
00296 exit_inferior_silent (int pid)
00297 {
00298   struct inferior *inf = find_inferior_pid (pid);
00299 
00300   exit_inferior_1 (inf, 1);
00301 }
00302 
00303 void
00304 exit_inferior_num_silent (int num)
00305 {
00306   struct inferior *inf = find_inferior_id (num);
00307 
00308   exit_inferior_1 (inf, 1);
00309 }
00310 
00311 void
00312 detach_inferior (int pid)
00313 {
00314   struct inferior *inf = find_inferior_pid (pid);
00315 
00316   exit_inferior_1 (inf, 1);
00317 
00318   if (print_inferior_events)
00319     printf_unfiltered (_("[Inferior %d detached]\n"), pid);
00320 }
00321 
00322 void
00323 inferior_appeared (struct inferior *inf, int pid)
00324 {
00325   inf->pid = pid;
00326 
00327   observer_notify_inferior_appeared (inf);
00328 }
00329 
00330 void
00331 discard_all_inferiors (void)
00332 {
00333   struct inferior *inf;
00334 
00335   for (inf = inferior_list; inf; inf = inf->next)
00336     {
00337       if (inf->pid != 0)
00338         exit_inferior_silent (inf->pid);
00339     }
00340 }
00341 
00342 struct inferior *
00343 find_inferior_id (int num)
00344 {
00345   struct inferior *inf;
00346 
00347   for (inf = inferior_list; inf; inf = inf->next)
00348     if (inf->num == num)
00349       return inf;
00350 
00351   return NULL;
00352 }
00353 
00354 struct inferior *
00355 find_inferior_pid (int pid)
00356 {
00357   struct inferior *inf;
00358 
00359   /* Looking for inferior pid == 0 is always wrong, and indicative of
00360      a bug somewhere else.  There may be more than one with pid == 0,
00361      for instance.  */
00362   gdb_assert (pid != 0);
00363 
00364   for (inf = inferior_list; inf; inf = inf->next)
00365     if (inf->pid == pid)
00366       return inf;
00367 
00368   return NULL;
00369 }
00370 
00371 /* Find an inferior bound to PSPACE.  */
00372 
00373 struct inferior *
00374 find_inferior_for_program_space (struct program_space *pspace)
00375 {
00376   struct inferior *inf;
00377 
00378   for (inf = inferior_list; inf != NULL; inf = inf->next)
00379     {
00380       if (inf->pspace == pspace)
00381         return inf;
00382     }
00383 
00384   return NULL;
00385 }
00386 
00387 struct inferior *
00388 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
00389                         void *data)
00390 {
00391   struct inferior *inf, *infnext;
00392 
00393   for (inf = inferior_list; inf; inf = infnext)
00394     {
00395       infnext = inf->next;
00396       if ((*callback) (inf, data))
00397         return inf;
00398     }
00399 
00400   return NULL;
00401 }
00402 
00403 int
00404 valid_gdb_inferior_id (int num)
00405 {
00406   struct inferior *inf;
00407 
00408   for (inf = inferior_list; inf; inf = inf->next)
00409     if (inf->num == num)
00410       return 1;
00411 
00412   return 0;
00413 }
00414 
00415 int
00416 pid_to_gdb_inferior_id (int pid)
00417 {
00418   struct inferior *inf;
00419 
00420   for (inf = inferior_list; inf; inf = inf->next)
00421     if (inf->pid == pid)
00422       return inf->num;
00423 
00424   return 0;
00425 }
00426 
00427 int
00428 gdb_inferior_id_to_pid (int num)
00429 {
00430   struct inferior *inferior = find_inferior_id (num);
00431   if (inferior)
00432     return inferior->pid;
00433   else
00434     return -1;
00435 }
00436 
00437 int
00438 in_inferior_list (int pid)
00439 {
00440   struct inferior *inf;
00441 
00442   for (inf = inferior_list; inf; inf = inf->next)
00443     if (inf->pid == pid)
00444       return 1;
00445 
00446   return 0;
00447 }
00448 
00449 int
00450 have_inferiors (void)
00451 {
00452   struct inferior *inf;
00453 
00454   for (inf = inferior_list; inf; inf = inf->next)
00455     if (inf->pid != 0)
00456       return 1;
00457 
00458   return 0;
00459 }
00460 
00461 int
00462 have_live_inferiors (void)
00463 {
00464   struct inferior *inf;
00465 
00466   for (inf = inferior_list; inf; inf = inf->next)
00467     if (inf->pid != 0)
00468       {
00469         struct thread_info *tp;
00470         
00471         tp = any_thread_of_process (inf->pid);
00472         if (tp && target_has_execution_1 (tp->ptid))
00473           break;
00474       }
00475 
00476   return inf != NULL;
00477 }
00478 
00479 /* Prune away automatically added program spaces that aren't required
00480    anymore.  */
00481 
00482 void
00483 prune_inferiors (void)
00484 {
00485   struct inferior *ss, **ss_link;
00486   struct inferior *current = current_inferior ();
00487 
00488   ss = inferior_list;
00489   ss_link = &inferior_list;
00490   while (ss)
00491     {
00492       if (ss == current
00493           || !ss->removable
00494           || ss->pid != 0)
00495         {
00496           ss_link = &ss->next;
00497           ss = *ss_link;
00498           continue;
00499         }
00500 
00501       *ss_link = ss->next;
00502       delete_inferior_1 (ss, 1);
00503       ss = *ss_link;
00504     }
00505 
00506   prune_program_spaces ();
00507 }
00508 
00509 /* Simply returns the count of inferiors.  */
00510 
00511 int
00512 number_of_inferiors (void)
00513 {
00514   struct inferior *inf;
00515   int count = 0;
00516 
00517   for (inf = inferior_list; inf != NULL; inf = inf->next)
00518     count++;
00519 
00520   return count;
00521 }
00522 
00523 /* Converts an inferior process id to a string.  Like
00524    target_pid_to_str, but special cases the null process.  */
00525 
00526 static char *
00527 inferior_pid_to_str (int pid)
00528 {
00529   if (pid != 0)
00530     return target_pid_to_str (pid_to_ptid (pid));
00531   else
00532     return _("<null>");
00533 }
00534 
00535 /* Prints the list of inferiors and their details on UIOUT.  This is a
00536    version of 'info_inferior_command' suitable for use from MI.
00537 
00538    If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
00539    inferiors that should be printed.  Otherwise, all inferiors are
00540    printed.  */
00541 
00542 static void
00543 print_inferior (struct ui_out *uiout, char *requested_inferiors)
00544 {
00545   struct inferior *inf;
00546   struct cleanup *old_chain;
00547   int inf_count = 0;
00548 
00549   /* Compute number of inferiors we will print.  */
00550   for (inf = inferior_list; inf; inf = inf->next)
00551     {
00552       if (!number_is_in_list (requested_inferiors, inf->num))
00553         continue;
00554 
00555       ++inf_count;
00556     }
00557 
00558   if (inf_count == 0)
00559     {
00560       ui_out_message (uiout, 0, "No inferiors.\n");
00561       return;
00562     }
00563 
00564   old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
00565                                                    "inferiors");
00566   ui_out_table_header (uiout, 1, ui_left, "current", "");
00567   ui_out_table_header (uiout, 4, ui_left, "number", "Num");
00568   ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
00569   ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
00570 
00571   ui_out_table_body (uiout);
00572   for (inf = inferior_list; inf; inf = inf->next)
00573     {
00574       struct cleanup *chain2;
00575 
00576       if (!number_is_in_list (requested_inferiors, inf->num))
00577         continue;
00578 
00579       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
00580 
00581       if (inf == current_inferior ())
00582         ui_out_field_string (uiout, "current", "*");
00583       else
00584         ui_out_field_skip (uiout, "current");
00585 
00586       ui_out_field_int (uiout, "number", inf->num);
00587 
00588       ui_out_field_string (uiout, "target-id",
00589                            inferior_pid_to_str (inf->pid));
00590 
00591       if (inf->pspace->pspace_exec_filename != NULL)
00592         ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
00593       else
00594         ui_out_field_skip (uiout, "exec");
00595 
00596       /* Print extra info that isn't really fit to always present in
00597          tabular form.  Currently we print the vfork parent/child
00598          relationships, if any.  */
00599       if (inf->vfork_parent)
00600         {
00601           ui_out_text (uiout, _("\n\tis vfork child of inferior "));
00602           ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
00603         }
00604       if (inf->vfork_child)
00605         {
00606           ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
00607           ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
00608         }
00609 
00610       ui_out_text (uiout, "\n");
00611       do_cleanups (chain2);
00612     }
00613 
00614   do_cleanups (old_chain);
00615 }
00616 
00617 static void
00618 detach_inferior_command (char *args, int from_tty)
00619 {
00620   int num, pid;
00621   struct thread_info *tp;
00622   struct get_number_or_range_state state;
00623 
00624   if (!args || !*args)
00625     error (_("Requires argument (inferior id(s) to detach)"));
00626 
00627   init_number_or_range (&state, args);
00628   while (!state.finished)
00629     {
00630       num = get_number_or_range (&state);
00631 
00632       if (!valid_gdb_inferior_id (num))
00633         {
00634           warning (_("Inferior ID %d not known."), num);
00635           continue;
00636         }
00637 
00638       pid = gdb_inferior_id_to_pid (num);
00639 
00640       tp = any_thread_of_process (pid);
00641       if (!tp)
00642         {
00643           warning (_("Inferior ID %d has no threads."), num);
00644           continue;
00645         }
00646 
00647       switch_to_thread (tp->ptid);
00648 
00649       detach_command (NULL, from_tty);
00650     }
00651 }
00652 
00653 static void
00654 kill_inferior_command (char *args, int from_tty)
00655 {
00656   int num, pid;
00657   struct thread_info *tp;
00658   struct get_number_or_range_state state;
00659 
00660   if (!args || !*args)
00661     error (_("Requires argument (inferior id(s) to kill)"));
00662 
00663   init_number_or_range (&state, args);
00664   while (!state.finished)
00665     {
00666       num = get_number_or_range (&state);
00667 
00668       if (!valid_gdb_inferior_id (num))
00669         {
00670           warning (_("Inferior ID %d not known."), num);
00671           continue;
00672         }
00673 
00674       pid = gdb_inferior_id_to_pid (num);
00675 
00676       tp = any_thread_of_process (pid);
00677       if (!tp)
00678         {
00679           warning (_("Inferior ID %d has no threads."), num);
00680           continue;
00681         }
00682 
00683       switch_to_thread (tp->ptid);
00684 
00685       target_kill ();
00686     }
00687 
00688   bfd_cache_close_all ();
00689 }
00690 
00691 static void
00692 inferior_command (char *args, int from_tty)
00693 {
00694   struct inferior *inf;
00695   int num;
00696 
00697   num = parse_and_eval_long (args);
00698 
00699   inf = find_inferior_id (num);
00700   if (inf == NULL)
00701     error (_("Inferior ID %d not known."), num);
00702 
00703   printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
00704                    inf->num,
00705                    inferior_pid_to_str (inf->pid),
00706                    (inf->pspace->pspace_exec_filename != NULL
00707                     ? inf->pspace->pspace_exec_filename
00708                     : _("<noexec>")));
00709 
00710   if (inf->pid != 0)
00711     {
00712       if (inf->pid != ptid_get_pid (inferior_ptid))
00713         {
00714           struct thread_info *tp;
00715 
00716           tp = any_thread_of_process (inf->pid);
00717           if (!tp)
00718             error (_("Inferior has no threads."));
00719 
00720           switch_to_thread (tp->ptid);
00721         }
00722 
00723       printf_filtered (_("[Switching to thread %d (%s)] "),
00724                        pid_to_thread_id (inferior_ptid),
00725                        target_pid_to_str (inferior_ptid));
00726     }
00727   else
00728     {
00729       struct inferior *inf;
00730 
00731       inf = find_inferior_id (num);
00732       set_current_inferior (inf);
00733       switch_to_thread (null_ptid);
00734       set_current_program_space (inf->pspace);
00735     }
00736 
00737   if (inf->pid != 0 && is_running (inferior_ptid))
00738     ui_out_text (current_uiout, "(running)\n");
00739   else if (inf->pid != 0)
00740     {
00741       ui_out_text (current_uiout, "\n");
00742       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
00743     }
00744 }
00745 
00746 /* Print information about currently known inferiors.  */
00747 
00748 static void
00749 info_inferiors_command (char *args, int from_tty)
00750 {
00751   print_inferior (current_uiout, args);
00752 }
00753 
00754 /* remove-inferior ID */
00755 
00756 static void
00757 remove_inferior_command (char *args, int from_tty)
00758 {
00759   int num;
00760   struct inferior *inf;
00761   struct get_number_or_range_state state;
00762 
00763   if (args == NULL || *args == '\0')
00764     error (_("Requires an argument (inferior id(s) to remove)"));
00765 
00766   init_number_or_range (&state, args);
00767   while (!state.finished)
00768     {
00769       num = get_number_or_range (&state);
00770       inf = find_inferior_id (num);
00771 
00772       if (inf == NULL)
00773         {
00774           warning (_("Inferior ID %d not known."), num);
00775           continue;
00776         }
00777 
00778       if (inf == current_inferior ())
00779         {
00780           warning (_("Can not remove current symbol inferior %d."), num);
00781           continue;
00782         }
00783     
00784       if (inf->pid != 0)
00785         {
00786           warning (_("Can not remove active inferior %d."), num);
00787           continue;
00788         }
00789 
00790       delete_inferior_1 (inf, 1);
00791     }
00792 }
00793 
00794 struct inferior *
00795 add_inferior_with_spaces (void)
00796 {
00797   struct address_space *aspace;
00798   struct program_space *pspace;
00799   struct inferior *inf;
00800   struct gdbarch_info info;
00801 
00802   /* If all inferiors share an address space on this system, this
00803      doesn't really return a new address space; otherwise, it
00804      really does.  */
00805   aspace = maybe_new_address_space ();
00806   pspace = add_program_space (aspace);
00807   inf = add_inferior (0);
00808   inf->pspace = pspace;
00809   inf->aspace = pspace->aspace;
00810 
00811   /* Setup the inferior's initial arch, based on information obtained
00812      from the global "set ..." options.  */
00813   gdbarch_info_init (&info);
00814   inf->gdbarch = gdbarch_find_by_info (info);
00815   /* The "set ..." options reject invalid settings, so we should
00816      always have a valid arch by now.  */
00817   gdb_assert (inf->gdbarch != NULL);
00818 
00819   return inf;
00820 }
00821 
00822 /* add-inferior [-copies N] [-exec FILENAME]  */
00823 
00824 static void
00825 add_inferior_command (char *args, int from_tty)
00826 {
00827   int i, copies = 1;
00828   char *exec = NULL;
00829   char **argv;
00830   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
00831 
00832   if (args)
00833     {
00834       argv = gdb_buildargv (args);
00835       make_cleanup_freeargv (argv);
00836 
00837       for (; *argv != NULL; argv++)
00838         {
00839           if (**argv == '-')
00840             {
00841               if (strcmp (*argv, "-copies") == 0)
00842                 {
00843                   ++argv;
00844                   if (!*argv)
00845                     error (_("No argument to -copies"));
00846                   copies = parse_and_eval_long (*argv);
00847                 }
00848               else if (strcmp (*argv, "-exec") == 0)
00849                 {
00850                   ++argv;
00851                   if (!*argv)
00852                     error (_("No argument to -exec"));
00853                   exec = tilde_expand (*argv);
00854                   make_cleanup (xfree, exec);
00855                 }
00856             }
00857           else
00858             error (_("Invalid argument"));
00859         }
00860     }
00861 
00862   save_current_space_and_thread ();
00863 
00864   for (i = 0; i < copies; ++i)
00865     {
00866       struct inferior *inf = add_inferior_with_spaces ();
00867 
00868       printf_filtered (_("Added inferior %d\n"), inf->num);
00869 
00870       if (exec != NULL)
00871         {
00872           /* Switch over temporarily, while reading executable and
00873              symbols.q.  */
00874           set_current_program_space (inf->pspace);
00875           set_current_inferior (inf);
00876           switch_to_thread (null_ptid);
00877 
00878           exec_file_attach (exec, from_tty);
00879           symbol_file_add_main (exec, from_tty);
00880         }
00881     }
00882 
00883   do_cleanups (old_chain);
00884 }
00885 
00886 /* clone-inferior [-copies N] [ID] */
00887 
00888 static void
00889 clone_inferior_command (char *args, int from_tty)
00890 {
00891   int i, copies = 1;
00892   char **argv;
00893   struct inferior *orginf = NULL;
00894   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
00895 
00896   if (args)
00897     {
00898       argv = gdb_buildargv (args);
00899       make_cleanup_freeargv (argv);
00900 
00901       for (; *argv != NULL; argv++)
00902         {
00903           if (**argv == '-')
00904             {
00905               if (strcmp (*argv, "-copies") == 0)
00906                 {
00907                   ++argv;
00908                   if (!*argv)
00909                     error (_("No argument to -copies"));
00910                   copies = parse_and_eval_long (*argv);
00911 
00912                   if (copies < 0)
00913                     error (_("Invalid copies number"));
00914                 }
00915             }
00916           else
00917             {
00918               if (orginf == NULL)
00919                 {
00920                   int num;
00921 
00922                   /* The first non-option (-) argument specified the
00923                      program space ID.  */
00924                   num = parse_and_eval_long (*argv);
00925                   orginf = find_inferior_id (num);
00926 
00927                   if (orginf == NULL)
00928                     error (_("Inferior ID %d not known."), num);
00929                   continue;
00930                 }
00931               else
00932                 error (_("Invalid argument"));
00933             }
00934         }
00935     }
00936 
00937   /* If no inferior id was specified, then the user wants to clone the
00938      current inferior.  */
00939   if (orginf == NULL)
00940     orginf = current_inferior ();
00941 
00942   save_current_space_and_thread ();
00943 
00944   for (i = 0; i < copies; ++i)
00945     {
00946       struct address_space *aspace;
00947       struct program_space *pspace;
00948       struct inferior *inf;
00949 
00950       /* If all inferiors share an address space on this system, this
00951          doesn't really return a new address space; otherwise, it
00952          really does.  */
00953       aspace = maybe_new_address_space ();
00954       pspace = add_program_space (aspace);
00955       inf = add_inferior (0);
00956       inf->pspace = pspace;
00957       inf->aspace = pspace->aspace;
00958       inf->gdbarch = orginf->gdbarch;
00959 
00960       /* If the original inferior had a user specified target
00961          description, make the clone use it too.  */
00962       if (target_desc_info_from_user_p (inf->tdesc_info))
00963         copy_inferior_target_desc_info (inf, orginf);
00964 
00965       printf_filtered (_("Added inferior %d.\n"), inf->num);
00966 
00967       set_current_inferior (inf);
00968       switch_to_thread (null_ptid);
00969       clone_program_space (pspace, orginf->pspace);
00970     }
00971 
00972   do_cleanups (old_chain);
00973 }
00974 
00975 /* Print notices when new inferiors are created and die.  */
00976 static void
00977 show_print_inferior_events (struct ui_file *file, int from_tty,
00978                            struct cmd_list_element *c, const char *value)
00979 {
00980   fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
00981 }
00982 
00983 
00984 
00985 void
00986 initialize_inferiors (void)
00987 {
00988   struct cmd_list_element *c = NULL;
00989 
00990   /* There's always one inferior.  Note that this function isn't an
00991      automatic _initialize_foo function, since other _initialize_foo
00992      routines may need to install their per-inferior data keys.  We
00993      can only allocate an inferior when all those modules have done
00994      that.  Do this after initialize_progspace, due to the
00995      current_program_space reference.  */
00996   current_inferior_ = add_inferior (0);
00997   current_inferior_->pspace = current_program_space;
00998   current_inferior_->aspace = current_program_space->aspace;
00999   /* The architecture will be initialized shortly, by
01000      initialize_current_architecture.  */
01001 
01002   add_info ("inferiors", info_inferiors_command, 
01003             _("IDs of specified inferiors (all inferiors if no argument)."));
01004 
01005   c = add_com ("add-inferior", no_class, add_inferior_command, _("\
01006 Add a new inferior.\n\
01007 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
01008 N is the optional number of inferiors to add, default is 1.\n\
01009 FILENAME is the file name of the executable to use\n\
01010 as main program."));
01011   set_cmd_completer (c, filename_completer);
01012 
01013   add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
01014 Remove inferior ID (or list of IDs).\n\
01015 Usage: remove-inferiors ID..."));
01016 
01017   add_com ("clone-inferior", no_class, clone_inferior_command, _("\
01018 Clone inferior ID.\n\
01019 Usage: clone-inferior [-copies <N>] [ID]\n\
01020 Add N copies of inferior ID.  The new inferior has the same\n\
01021 executable loaded as the copied inferior.  If -copies is not specified,\n\
01022 adds 1 copy.  If ID is not specified, it is the current inferior\n\
01023 that is cloned."));
01024 
01025   add_cmd ("inferiors", class_run, detach_inferior_command, _("\
01026 Detach from inferior ID (or list of IDS)."),
01027            &detachlist);
01028 
01029   add_cmd ("inferiors", class_run, kill_inferior_command, _("\
01030 Kill inferior ID (or list of IDs)."),
01031            &killlist);
01032 
01033   add_cmd ("inferior", class_run, inferior_command, _("\
01034 Use this command to switch between inferiors.\n\
01035 The new inferior ID must be currently known."),
01036            &cmdlist);
01037 
01038   add_setshow_boolean_cmd ("inferior-events", no_class,
01039          &print_inferior_events, _("\
01040 Set printing of inferior events (e.g., inferior start and exit)."), _("\
01041 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
01042          NULL,
01043          show_print_inferior_events,
01044          &setprintlist, &showprintlist);
01045 
01046 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines