gdb/linux-fork.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* GNU/Linux native-dependent code for debugging multiple forks.

  2.    Copyright (C) 2005-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 "arch-utils.h"
  16. #include "inferior.h"
  17. #include "infrun.h"
  18. #include "regcache.h"
  19. #include "gdbcmd.h"
  20. #include "infcall.h"
  21. #include "objfiles.h"
  22. #include "linux-fork.h"
  23. #include "linux-nat.h"
  24. #include "gdbthread.h"
  25. #include "source.h"

  26. #include <sys/ptrace.h>
  27. #include "gdb_wait.h"
  28. #include <dirent.h>
  29. #include <ctype.h>

  30. struct fork_info *fork_list;
  31. static int highest_fork_num;

  32. /* Prevent warning from -Wmissing-prototypes.  */
  33. extern void _initialize_linux_fork (void);

  34. /* Fork list data structure:  */
  35. struct fork_info
  36. {
  37.   struct fork_info *next;
  38.   ptid_t ptid;
  39.   ptid_t parent_ptid;
  40.   int num;                        /* Convenient handle (GDB fork id).  */
  41.   struct regcache *savedregs;        /* Convenient for info fork, saves
  42.                                    having to actually switch contexts.  */
  43.   int clobber_regs;                /* True if we should restore saved regs.  */
  44.   off_t *filepos;                /* Set of open file descriptors' offsets.  */
  45.   int maxfd;
  46. };

  47. /* Fork list methods:  */

  48. int
  49. forks_exist_p (void)
  50. {
  51.   return (fork_list != NULL);
  52. }

  53. /* Add a fork to the internal fork list.  */

  54. struct fork_info *
  55. add_fork (pid_t pid)
  56. {
  57.   struct fork_info *fp;

  58.   if (fork_list == NULL && pid != ptid_get_pid (inferior_ptid))
  59.     {
  60.       /* Special case -- if this is the first fork in the list
  61.          (the list is hitherto empty), and if this new fork is
  62.          NOT the current inferior_ptid, then add inferior_ptid
  63.          first, as a special zeroeth fork id.  */
  64.       highest_fork_num = -1;
  65.       add_fork (ptid_get_pid (inferior_ptid));        /* safe recursion */
  66.     }

  67.   fp = XCNEW (struct fork_info);
  68.   fp->ptid = ptid_build (pid, pid, 0);
  69.   fp->num = ++highest_fork_num;
  70.   fp->next = fork_list;
  71.   fork_list = fp;
  72.   return fp;
  73. }

  74. static void
  75. free_fork (struct fork_info *fp)
  76. {
  77.   /* Notes on step-resume breakpoints: since this is a concern for
  78.      threads, let's convince ourselves that it's not a concern for
  79.      forks.  There are two ways for a fork_info to be created.  First,
  80.      by the checkpoint command, in which case we're at a gdb prompt
  81.      and there can't be any step-resume breakpoint.  Second, by a fork
  82.      in the user program, in which case we *may* have stepped into the
  83.      fork call, but regardless of whether we follow the parent or the
  84.      child, we will return to the same place and the step-resume
  85.      breakpoint, if any, will take care of itself as usual.  And
  86.      unlike threads, we do not save a private copy of the step-resume
  87.      breakpoint -- so we're OK.  */

  88.   if (fp)
  89.     {
  90.       if (fp->savedregs)
  91.         regcache_xfree (fp->savedregs);
  92.       if (fp->filepos)
  93.         xfree (fp->filepos);
  94.       xfree (fp);
  95.     }
  96. }

  97. static void
  98. delete_fork (ptid_t ptid)
  99. {
  100.   struct fork_info *fp, *fpprev;

  101.   fpprev = NULL;

  102.   linux_nat_forget_process (ptid_get_pid (ptid));

  103.   for (fp = fork_list; fp; fpprev = fp, fp = fp->next)
  104.     if (ptid_equal (fp->ptid, ptid))
  105.       break;

  106.   if (!fp)
  107.     return;

  108.   if (fpprev)
  109.     fpprev->next = fp->next;
  110.   else
  111.     fork_list = fp->next;

  112.   free_fork (fp);

  113.   /* Special case: if there is now only one process in the list,
  114.      and if it is (hopefully!) the current inferior_ptid, then
  115.      remove it, leaving the list empty -- we're now down to the
  116.      default case of debugging a single process.  */
  117.   if (fork_list != NULL && fork_list->next == NULL &&
  118.       ptid_equal (fork_list->ptid, inferior_ptid))
  119.     {
  120.       /* Last fork -- delete from list and handle as solo process
  121.          (should be a safe recursion).  */
  122.       delete_fork (inferior_ptid);
  123.     }
  124. }

  125. /* Find a fork_info by matching PTID.  */
  126. static struct fork_info *
  127. find_fork_ptid (ptid_t ptid)
  128. {
  129.   struct fork_info *fp;

  130.   for (fp = fork_list; fp; fp = fp->next)
  131.     if (ptid_equal (fp->ptid, ptid))
  132.       return fp;

  133.   return NULL;
  134. }

  135. /* Find a fork_info by matching ID.  */
  136. static struct fork_info *
  137. find_fork_id (int num)
  138. {
  139.   struct fork_info *fp;

  140.   for (fp = fork_list; fp; fp = fp->next)
  141.     if (fp->num == num)
  142.       return fp;

  143.   return NULL;
  144. }

  145. /* Find a fork_info by matching pid.  */
  146. extern struct fork_info *
  147. find_fork_pid (pid_t pid)
  148. {
  149.   struct fork_info *fp;

  150.   for (fp = fork_list; fp; fp = fp->next)
  151.     if (pid == ptid_get_pid (fp->ptid))
  152.       return fp;

  153.   return NULL;
  154. }

  155. static ptid_t
  156. fork_id_to_ptid (int num)
  157. {
  158.   struct fork_info *fork = find_fork_id (num);
  159.   if (fork)
  160.     return fork->ptid;
  161.   else
  162.     return pid_to_ptid (-1);
  163. }

  164. static void
  165. init_fork_list (void)
  166. {
  167.   struct fork_info *fp, *fpnext;

  168.   if (!fork_list)
  169.     return;

  170.   for (fp = fork_list; fp; fp = fpnext)
  171.     {
  172.       fpnext = fp->next;
  173.       free_fork (fp);
  174.     }

  175.   fork_list = NULL;
  176. }

  177. /* Fork list <-> gdb interface.  */

  178. /* Utility function for fork_load/fork_save.
  179.    Calls lseek in the (current) inferior process.  */

  180. static off_t
  181. call_lseek (int fd, off_t offset, int whence)
  182. {
  183.   char exp[80];

  184.   snprintf (&exp[0], sizeof (exp), "lseek (%d, %ld, %d)",
  185.             fd, (long) offset, whence);
  186.   return (off_t) parse_and_eval_long (&exp[0]);
  187. }

  188. /* Load infrun state for the fork PTID.  */

  189. static void
  190. fork_load_infrun_state (struct fork_info *fp)
  191. {
  192.   extern void nullify_last_target_wait_ptid ();
  193.   int i;

  194.   linux_nat_switch_fork (fp->ptid);

  195.   if (fp->savedregs && fp->clobber_regs)
  196.     regcache_cpy (get_current_regcache (), fp->savedregs);

  197.   registers_changed ();
  198.   reinit_frame_cache ();

  199.   stop_pc = regcache_read_pc (get_current_regcache ());
  200.   nullify_last_target_wait_ptid ();

  201.   /* Now restore the file positions of open file descriptors.  */
  202.   if (fp->filepos)
  203.     {
  204.       for (i = 0; i <= fp->maxfd; i++)
  205.         if (fp->filepos[i] != (off_t) -1)
  206.           call_lseek (i, fp->filepos[i], SEEK_SET);
  207.       /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
  208.          this is native-only.  If it ever has to be cross, we'll have
  209.          to rethink this.  */
  210.     }
  211. }

  212. /* Save infrun state for the fork PTID.
  213.    Exported for use by linux child_follow_fork.  */

  214. static void
  215. fork_save_infrun_state (struct fork_info *fp, int clobber_regs)
  216. {
  217.   char path[PATH_MAX];
  218.   struct dirent *de;
  219.   DIR *d;

  220.   if (fp->savedregs)
  221.     regcache_xfree (fp->savedregs);

  222.   fp->savedregs = regcache_dup (get_current_regcache ());
  223.   fp->clobber_regs = clobber_regs;

  224.   if (clobber_regs)
  225.     {
  226.       /* Now save the 'state' (file position) of all open file descriptors.
  227.          Unfortunately fork does not take care of that for us...  */
  228.       snprintf (path, PATH_MAX, "/proc/%ld/fd",
  229.                 (long) ptid_get_pid (fp->ptid));
  230.       if ((d = opendir (path)) != NULL)
  231.         {
  232.           long tmp;

  233.           fp->maxfd = 0;
  234.           while ((de = readdir (d)) != NULL)
  235.             {
  236.               /* Count open file descriptors (actually find highest
  237.                  numbered).  */
  238.               tmp = strtol (&de->d_name[0], NULL, 10);
  239.               if (fp->maxfd < tmp)
  240.                 fp->maxfd = tmp;
  241.             }
  242.           /* Allocate array of file positions.  */
  243.           fp->filepos = xrealloc (fp->filepos,
  244.                                   (fp->maxfd + 1) * sizeof (*fp->filepos));

  245.           /* Initialize to -1 (invalid).  */
  246.           for (tmp = 0; tmp <= fp->maxfd; tmp++)
  247.             fp->filepos[tmp] = -1;

  248.           /* Now find actual file positions.  */
  249.           rewinddir (d);
  250.           while ((de = readdir (d)) != NULL)
  251.             if (isdigit (de->d_name[0]))
  252.               {
  253.                 tmp = strtol (&de->d_name[0], NULL, 10);
  254.                 fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR);
  255.               }
  256.           closedir (d);
  257.         }
  258.     }
  259. }

  260. /* Kill 'em all, let God sort 'em out...  */

  261. void
  262. linux_fork_killall (void)
  263. {
  264.   /* Walk list and kill every pid.  No need to treat the
  265.      current inferior_ptid as special (we do not return a
  266.      status for it) -- however any process may be a child
  267.      or a parent, so may get a SIGCHLD from a previously
  268.      killed child.  Wait them all out.  */
  269.   struct fork_info *fp;
  270.   pid_t pid, ret;
  271.   int status;

  272.   for (fp = fork_list; fp; fp = fp->next)
  273.     {
  274.       pid = ptid_get_pid (fp->ptid);
  275.       do {
  276.         /* Use SIGKILL instead of PTRACE_KILL because the former works even
  277.            if the thread is running, while the later doesn't.  */
  278.         kill (pid, SIGKILL);
  279.         ret = waitpid (pid, &status, 0);
  280.         /* We might get a SIGCHLD instead of an exit status.  This is
  281.          aggravated by the first kill above - a child has just
  282.          died.  MVS comment cut-and-pasted from linux-nat.  */
  283.       } while (ret == pid && WIFSTOPPED (status));
  284.     }
  285.   init_fork_list ();        /* Clear list, prepare to start fresh.  */
  286. }

  287. /* The current inferior_ptid has exited, but there are other viable
  288.    forks to debug.  Delete the exiting one and context-switch to the
  289.    first available.  */

  290. void
  291. linux_fork_mourn_inferior (void)
  292. {
  293.   /* Wait just one more time to collect the inferior's exit status.
  294.      Do not check whether this succeeds though, since we may be
  295.      dealing with a process that we attached to.  Such a process will
  296.      only report its exit status to its original parent.  */
  297.   int status;

  298.   waitpid (ptid_get_pid (inferior_ptid), &status, 0);

  299.   /* OK, presumably inferior_ptid is the one who has exited.
  300.      We need to delete that one from the fork_list, and switch
  301.      to the next available fork.  */
  302.   delete_fork (inferior_ptid);

  303.   /* There should still be a fork - if there's only one left,
  304.      delete_fork won't remove it, because we haven't updated
  305.      inferior_ptid yet.  */
  306.   gdb_assert (fork_list);

  307.   fork_load_infrun_state (fork_list);
  308.   printf_filtered (_("[Switching to %s]\n"),
  309.                    target_pid_to_str (inferior_ptid));

  310.   /* If there's only one fork, switch back to non-fork mode.  */
  311.   if (fork_list->next == NULL)
  312.     delete_fork (inferior_ptid);
  313. }

  314. /* The current inferior_ptid is being detached, but there are other
  315.    viable forks to debug.  Detach and delete it and context-switch to
  316.    the first available.  */

  317. void
  318. linux_fork_detach (const char *args, int from_tty)
  319. {
  320.   /* OK, inferior_ptid is the one we are detaching from.  We need to
  321.      delete it from the fork_list, and switch to the next available
  322.      fork.  */

  323.   if (ptrace (PTRACE_DETACH, ptid_get_pid (inferior_ptid), 0, 0))
  324.     error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid));

  325.   delete_fork (inferior_ptid);

  326.   /* There should still be a fork - if there's only one left,
  327.      delete_fork won't remove it, because we haven't updated
  328.      inferior_ptid yet.  */
  329.   gdb_assert (fork_list);

  330.   fork_load_infrun_state (fork_list);

  331.   if (from_tty)
  332.     printf_filtered (_("[Switching to %s]\n"),
  333.                      target_pid_to_str (inferior_ptid));

  334.   /* If there's only one fork, switch back to non-fork mode.  */
  335.   if (fork_list->next == NULL)
  336.     delete_fork (inferior_ptid);
  337. }

  338. static void
  339. inferior_call_waitpid_cleanup (void *fp)
  340. {
  341.   struct fork_info *oldfp = fp;

  342.   if (oldfp)
  343.     {
  344.       /* Switch back to inferior_ptid.  */
  345.       remove_breakpoints ();
  346.       fork_load_infrun_state (oldfp);
  347.       insert_breakpoints ();
  348.     }
  349. }

  350. static int
  351. inferior_call_waitpid (ptid_t pptid, int pid)
  352. {
  353.   struct objfile *waitpid_objf;
  354.   struct value *waitpid_fn = NULL;
  355.   struct value *argv[4], *retv;
  356.   struct gdbarch *gdbarch = get_current_arch ();
  357.   struct fork_info *oldfp = NULL, *newfp = NULL;
  358.   struct cleanup *old_cleanup;
  359.   int ret = -1;

  360.   if (!ptid_equal (pptid, inferior_ptid))
  361.     {
  362.       /* Switch to pptid.  */
  363.       oldfp = find_fork_ptid (inferior_ptid);
  364.       gdb_assert (oldfp != NULL);
  365.       newfp = find_fork_ptid (pptid);
  366.       gdb_assert (newfp != NULL);
  367.       fork_save_infrun_state (oldfp, 1);
  368.       remove_breakpoints ();
  369.       fork_load_infrun_state (newfp);
  370.       insert_breakpoints ();
  371.     }

  372.   old_cleanup = make_cleanup (inferior_call_waitpid_cleanup, oldfp);

  373.   /* Get the waitpid_fn.  */
  374.   if (lookup_minimal_symbol ("waitpid", NULL, NULL).minsym != NULL)
  375.     waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf);
  376.   if (!waitpid_fn
  377.       && lookup_minimal_symbol ("_waitpid", NULL, NULL).minsym != NULL)
  378.     waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf);
  379.   if (!waitpid_fn)
  380.     goto out;

  381.   /* Get the argv.  */
  382.   argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid);
  383.   argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0);
  384.   argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
  385.   argv[3] = 0;

  386.   retv = call_function_by_hand (waitpid_fn, 3, argv);
  387.   if (value_as_long (retv) < 0)
  388.     goto out;

  389.   ret = 0;

  390. out:
  391.   do_cleanups (old_cleanup);
  392.   return ret;
  393. }

  394. /* Fork list <-> user interface.  */

  395. static void
  396. delete_checkpoint_command (char *args, int from_tty)
  397. {
  398.   ptid_t ptid, pptid;
  399.   struct fork_info *fi;

  400.   if (!args || !*args)
  401.     error (_("Requires argument (checkpoint id to delete)"));

  402.   ptid = fork_id_to_ptid (parse_and_eval_long (args));
  403.   if (ptid_equal (ptid, minus_one_ptid))
  404.     error (_("No such checkpoint id, %s"), args);

  405.   if (ptid_equal (ptid, inferior_ptid))
  406.     error (_("\
  407. Please switch to another checkpoint before deleting the current one"));

  408.   if (ptrace (PTRACE_KILL, ptid_get_pid (ptid), 0, 0))
  409.     error (_("Unable to kill pid %s"), target_pid_to_str (ptid));

  410.   fi = find_fork_ptid (ptid);
  411.   gdb_assert (fi);
  412.   pptid = fi->parent_ptid;

  413.   if (from_tty)
  414.     printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid));

  415.   delete_fork (ptid);

  416.   /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint
  417.      list, waitpid the ptid.
  418.      If fi->parent_ptid is a part of lwp and it is stoped, waitpid the
  419.      ptid.  */
  420.   if ((!find_thread_ptid (pptid) && find_fork_ptid (pptid))
  421.       || (find_thread_ptid (pptid) && is_stopped (pptid)))
  422.     {
  423.       if (inferior_call_waitpid (pptid, ptid_get_pid (ptid)))
  424.         warning (_("Unable to wait pid %s"), target_pid_to_str (ptid));
  425.     }
  426. }

  427. static void
  428. detach_checkpoint_command (char *args, int from_tty)
  429. {
  430.   ptid_t ptid;

  431.   if (!args || !*args)
  432.     error (_("Requires argument (checkpoint id to detach)"));

  433.   ptid = fork_id_to_ptid (parse_and_eval_long (args));
  434.   if (ptid_equal (ptid, minus_one_ptid))
  435.     error (_("No such checkpoint id, %s"), args);

  436.   if (ptid_equal (ptid, inferior_ptid))
  437.     error (_("\
  438. Please switch to another checkpoint before detaching the current one"));

  439.   if (ptrace (PTRACE_DETACH, ptid_get_pid (ptid), 0, 0))
  440.     error (_("Unable to detach %s"), target_pid_to_str (ptid));

  441.   if (from_tty)
  442.     printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid));

  443.   delete_fork (ptid);
  444. }

  445. /* Print information about currently known checkpoints.  */

  446. static void
  447. info_checkpoints_command (char *arg, int from_tty)
  448. {
  449.   struct gdbarch *gdbarch = get_current_arch ();
  450.   struct symtab_and_line sal;
  451.   struct fork_info *fp;
  452.   ULONGEST pc;
  453.   int requested = -1;
  454.   struct fork_info *printed = NULL;

  455.   if (arg && *arg)
  456.     requested = (int) parse_and_eval_long (arg);

  457.   for (fp = fork_list; fp; fp = fp->next)
  458.     {
  459.       if (requested > 0 && fp->num != requested)
  460.         continue;

  461.       printed = fp;
  462.       if (ptid_equal (fp->ptid, inferior_ptid))
  463.         {
  464.           printf_filtered ("* ");
  465.           pc = regcache_read_pc (get_current_regcache ());
  466.         }
  467.       else
  468.         {
  469.           printf_filtered ("  ");
  470.           pc = regcache_read_pc (fp->savedregs);
  471.         }
  472.       printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid));
  473.       if (fp->num == 0)
  474.         printf_filtered (_(" (main process)"));
  475.       printf_filtered (_(" at "));
  476.       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);

  477.       sal = find_pc_line (pc, 0);
  478.       if (sal.symtab)
  479.         printf_filtered (_(", file %s"),
  480.                          symtab_to_filename_for_display (sal.symtab));
  481.       if (sal.line)
  482.         printf_filtered (_(", line %d"), sal.line);
  483.       if (!sal.symtab && !sal.line)
  484.         {
  485.           struct bound_minimal_symbol msym;

  486.           msym = lookup_minimal_symbol_by_pc (pc);
  487.           if (msym.minsym)
  488.             printf_filtered (", <%s>", MSYMBOL_LINKAGE_NAME (msym.minsym));
  489.         }

  490.       putchar_filtered ('\n');
  491.     }
  492.   if (printed == NULL)
  493.     {
  494.       if (requested > 0)
  495.         printf_filtered (_("No checkpoint number %d.\n"), requested);
  496.       else
  497.         printf_filtered (_("No checkpoints.\n"));
  498.     }
  499. }

  500. /* The PID of the process we're checkpointing.  */
  501. static int checkpointing_pid = 0;

  502. int
  503. linux_fork_checkpointing_p (int pid)
  504. {
  505.   return (checkpointing_pid == pid);
  506. }

  507. /* Callback for iterate over threads.  Used to check whether
  508.    the current inferior is multi-threaded.  Returns true as soon
  509.    as it sees the second thread of the current inferior.  */

  510. static int
  511. inf_has_multiple_thread_cb (struct thread_info *tp, void *data)
  512. {
  513.   int *count_p = (int *) data;

  514.   if (current_inferior ()->pid == ptid_get_pid (tp->ptid))
  515.     (*count_p)++;

  516.   /* Stop the iteration if multiple threads have been detected.  */
  517.   return *count_p > 1;
  518. }

  519. /* Return true if the current inferior is multi-threaded.  */

  520. static int
  521. inf_has_multiple_threads (void)
  522. {
  523.   int count = 0;

  524.   iterate_over_threads (inf_has_multiple_thread_cb, &count);
  525.   return (count > 1);
  526. }

  527. static void
  528. checkpoint_command (char *args, int from_tty)
  529. {
  530.   struct objfile *fork_objf;
  531.   struct gdbarch *gdbarch;
  532.   struct target_waitstatus last_target_waitstatus;
  533.   ptid_t last_target_ptid;
  534.   struct value *fork_fn = NULL, *ret;
  535.   struct fork_info *fp;
  536.   pid_t retpid;
  537.   struct cleanup *old_chain;

  538.   if (!target_has_execution)
  539.     error (_("The program is not being run."));

  540.   /* Ensure that the inferior is not multithreaded.  */
  541.   update_thread_list ();
  542.   if (inf_has_multiple_threads ())
  543.     error (_("checkpoint: can't checkpoint multiple threads."));

  544.   /* Make the inferior fork, record its (and gdb's) state.  */

  545.   if (lookup_minimal_symbol ("fork", NULL, NULL).minsym != NULL)
  546.     fork_fn = find_function_in_inferior ("fork", &fork_objf);
  547.   if (!fork_fn)
  548.     if (lookup_minimal_symbol ("_fork", NULL, NULL).minsym != NULL)
  549.       fork_fn = find_function_in_inferior ("fork", &fork_objf);
  550.   if (!fork_fn)
  551.     error (_("checkpoint: can't find fork function in inferior."));

  552.   gdbarch = get_objfile_arch (fork_objf);
  553.   ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);

  554.   /* Tell linux-nat.c that we're checkpointing this inferior.  */
  555.   old_chain = make_cleanup_restore_integer (&checkpointing_pid);
  556.   checkpointing_pid = ptid_get_pid (inferior_ptid);

  557.   ret = call_function_by_hand (fork_fn, 0, &ret);
  558.   do_cleanups (old_chain);
  559.   if (!ret)        /* Probably can't happen.  */
  560.     error (_("checkpoint: call_function_by_hand returned null."));

  561.   retpid = value_as_long (ret);
  562.   get_last_target_status (&last_target_ptid, &last_target_waitstatus);

  563.   fp = find_fork_pid (retpid);

  564.   if (from_tty)
  565.     {
  566.       int parent_pid;

  567.       printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"),
  568.                        fp != NULL ? fp->num : -1, (long) retpid);
  569.       if (info_verbose)
  570.         {
  571.           parent_pid = ptid_get_lwp (last_target_ptid);
  572.           if (parent_pid == 0)
  573.             parent_pid = ptid_get_pid (last_target_ptid);
  574.           printf_filtered (_("   gdb says parent = %ld.\n"),
  575.                            (long) parent_pid);
  576.         }
  577.     }

  578.   if (!fp)
  579.     error (_("Failed to find new fork"));
  580.   fork_save_infrun_state (fp, 1);
  581.   fp->parent_ptid = last_target_ptid;
  582. }

  583. static void
  584. linux_fork_context (struct fork_info *newfp, int from_tty)
  585. {
  586.   /* Now we attempt to switch processes.  */
  587.   struct fork_info *oldfp;

  588.   gdb_assert (newfp != NULL);

  589.   oldfp = find_fork_ptid (inferior_ptid);
  590.   gdb_assert (oldfp != NULL);

  591.   fork_save_infrun_state (oldfp, 1);
  592.   remove_breakpoints ();
  593.   fork_load_infrun_state (newfp);
  594.   insert_breakpoints ();

  595.   printf_filtered (_("Switching to %s\n"),
  596.                    target_pid_to_str (inferior_ptid));

  597.   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  598. }

  599. /* Switch inferior process (checkpoint) context, by checkpoint id.  */
  600. static void
  601. restart_command (char *args, int from_tty)
  602. {
  603.   struct fork_info *fp;

  604.   if (!args || !*args)
  605.     error (_("Requires argument (checkpoint id to restart)"));

  606.   if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL)
  607.     error (_("Not found: checkpoint id %s"), args);

  608.   linux_fork_context (fp, from_tty);
  609. }

  610. void
  611. _initialize_linux_fork (void)
  612. {
  613.   init_fork_list ();

  614.   /* Checkpoint command: create a fork of the inferior process
  615.      and set it aside for later debugging.  */

  616.   add_com ("checkpoint", class_obscure, checkpoint_command, _("\
  617. Fork a duplicate process (experimental)."));

  618.   /* Restart command: restore the context of a specified checkpoint
  619.      process.  */

  620.   add_com ("restart", class_obscure, restart_command, _("\
  621. restart <n>: restore program context from a checkpoint.\n\
  622. Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));

  623.   /* Delete checkpoint command: kill the process and remove it from
  624.      the fork list.  */

  625.   add_cmd ("checkpoint", class_obscure, delete_checkpoint_command, _("\
  626. Delete a checkpoint (experimental)."),
  627.            &deletelist);

  628.   /* Detach checkpoint command: release the process to run independently,
  629.      and remove it from the fork list.  */

  630.   add_cmd ("checkpoint", class_obscure, detach_checkpoint_command, _("\
  631. Detach from a checkpoint (experimental)."),
  632.            &detachlist);

  633.   /* Info checkpoints command: list all forks/checkpoints
  634.      currently under gdb's control.  */

  635.   add_info ("checkpoints", info_checkpoints_command,
  636.             _("IDs of currently known checkpoints."));
  637. }