Stop assuming no-debug-info functions return int
[external/binutils.git] / gdb / linux-fork.c
1 /* GNU/Linux native-dependent code for debugging multiple forks.
2
3    Copyright (C) 2005-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "inferior.h"
23 #include "infrun.h"
24 #include "regcache.h"
25 #include "gdbcmd.h"
26 #include "infcall.h"
27 #include "objfiles.h"
28 #include "linux-fork.h"
29 #include "linux-nat.h"
30 #include "gdbthread.h"
31 #include "source.h"
32
33 #include "nat/gdb_ptrace.h"
34 #include "gdb_wait.h"
35 #include <dirent.h>
36 #include <ctype.h>
37
38 struct fork_info *fork_list;
39 static int highest_fork_num;
40
41 /* Prevent warning from -Wmissing-prototypes.  */
42 extern void _initialize_linux_fork (void);
43
44 /* Fork list data structure:  */
45 struct fork_info
46 {
47   struct fork_info *next;
48   ptid_t ptid;
49   ptid_t parent_ptid;
50   int num;                      /* Convenient handle (GDB fork id).  */
51   struct regcache *savedregs;   /* Convenient for info fork, saves
52                                    having to actually switch contexts.  */
53   int clobber_regs;             /* True if we should restore saved regs.  */
54   off_t *filepos;               /* Set of open file descriptors' offsets.  */
55   int maxfd;
56 };
57
58 /* Fork list methods:  */
59
60 int
61 forks_exist_p (void)
62 {
63   return (fork_list != NULL);
64 }
65
66 /* Return the last fork in the list.  */
67
68 static struct fork_info *
69 find_last_fork (void)
70 {
71   struct fork_info *last;
72
73   if (fork_list == NULL)
74     return NULL;
75
76   for (last = fork_list; last->next != NULL; last = last->next)
77     ;
78   return last;
79 }
80
81 /* Add a fork to the internal fork list.  */
82
83 struct fork_info *
84 add_fork (pid_t pid)
85 {
86   struct fork_info *fp;
87
88   if (fork_list == NULL && pid != ptid_get_pid (inferior_ptid))
89     {
90       /* Special case -- if this is the first fork in the list
91          (the list is hitherto empty), and if this new fork is
92          NOT the current inferior_ptid, then add inferior_ptid
93          first, as a special zeroeth fork id.  */
94       highest_fork_num = -1;
95       add_fork (ptid_get_pid (inferior_ptid));  /* safe recursion */
96     }
97
98   fp = XCNEW (struct fork_info);
99   fp->ptid = ptid_build (pid, pid, 0);
100   fp->num = ++highest_fork_num;
101
102   if (fork_list == NULL)
103     fork_list = fp;
104   else
105     {
106       struct fork_info *last = find_last_fork ();
107
108       last->next = fp;
109     }
110
111   return fp;
112 }
113
114 static void
115 free_fork (struct fork_info *fp)
116 {
117   /* Notes on step-resume breakpoints: since this is a concern for
118      threads, let's convince ourselves that it's not a concern for
119      forks.  There are two ways for a fork_info to be created.  First,
120      by the checkpoint command, in which case we're at a gdb prompt
121      and there can't be any step-resume breakpoint.  Second, by a fork
122      in the user program, in which case we *may* have stepped into the
123      fork call, but regardless of whether we follow the parent or the
124      child, we will return to the same place and the step-resume
125      breakpoint, if any, will take care of itself as usual.  And
126      unlike threads, we do not save a private copy of the step-resume
127      breakpoint -- so we're OK.  */
128
129   if (fp)
130     {
131       if (fp->savedregs)
132         regcache_xfree (fp->savedregs);
133       if (fp->filepos)
134         xfree (fp->filepos);
135       xfree (fp);
136     }
137 }
138
139 static void
140 delete_fork (ptid_t ptid)
141 {
142   struct fork_info *fp, *fpprev;
143
144   fpprev = NULL;
145
146   linux_nat_forget_process (ptid_get_pid (ptid));
147
148   for (fp = fork_list; fp; fpprev = fp, fp = fp->next)
149     if (ptid_equal (fp->ptid, ptid))
150       break;
151
152   if (!fp)
153     return;
154
155   if (fpprev)
156     fpprev->next = fp->next;
157   else
158     fork_list = fp->next;
159
160   free_fork (fp);
161
162   /* Special case: if there is now only one process in the list,
163      and if it is (hopefully!) the current inferior_ptid, then
164      remove it, leaving the list empty -- we're now down to the
165      default case of debugging a single process.  */
166   if (fork_list != NULL && fork_list->next == NULL &&
167       ptid_equal (fork_list->ptid, inferior_ptid))
168     {
169       /* Last fork -- delete from list and handle as solo process
170          (should be a safe recursion).  */
171       delete_fork (inferior_ptid);
172     }
173 }
174
175 /* Find a fork_info by matching PTID.  */
176 static struct fork_info *
177 find_fork_ptid (ptid_t ptid)
178 {
179   struct fork_info *fp;
180
181   for (fp = fork_list; fp; fp = fp->next)
182     if (ptid_equal (fp->ptid, ptid))
183       return fp;
184
185   return NULL;
186 }
187
188 /* Find a fork_info by matching ID.  */
189 static struct fork_info *
190 find_fork_id (int num)
191 {
192   struct fork_info *fp;
193
194   for (fp = fork_list; fp; fp = fp->next)
195     if (fp->num == num)
196       return fp;
197
198   return NULL;
199 }
200
201 /* Find a fork_info by matching pid.  */
202 extern struct fork_info *
203 find_fork_pid (pid_t pid)
204 {
205   struct fork_info *fp;
206
207   for (fp = fork_list; fp; fp = fp->next)
208     if (pid == ptid_get_pid (fp->ptid))
209       return fp;
210
211   return NULL;
212 }
213
214 static ptid_t
215 fork_id_to_ptid (int num)
216 {
217   struct fork_info *fork = find_fork_id (num);
218   if (fork)
219     return fork->ptid;
220   else
221     return pid_to_ptid (-1);
222 }
223
224 static void
225 init_fork_list (void)
226 {
227   struct fork_info *fp, *fpnext;
228
229   if (!fork_list)
230     return;
231
232   for (fp = fork_list; fp; fp = fpnext)
233     {
234       fpnext = fp->next;
235       free_fork (fp);
236     }
237
238   fork_list = NULL;
239 }
240
241 /* Fork list <-> gdb interface.  */
242
243 /* Utility function for fork_load/fork_save.
244    Calls lseek in the (current) inferior process.  */
245
246 static off_t
247 call_lseek (int fd, off_t offset, int whence)
248 {
249   char exp[80];
250
251   snprintf (&exp[0], sizeof (exp), "(long) lseek (%d, %ld, %d)",
252             fd, (long) offset, whence);
253   return (off_t) parse_and_eval_long (&exp[0]);
254 }
255
256 /* Load infrun state for the fork PTID.  */
257
258 static void
259 fork_load_infrun_state (struct fork_info *fp)
260 {
261   extern void nullify_last_target_wait_ptid ();
262   int i;
263
264   linux_nat_switch_fork (fp->ptid);
265
266   if (fp->savedregs && fp->clobber_regs)
267     regcache_cpy (get_current_regcache (), fp->savedregs);
268
269   registers_changed ();
270   reinit_frame_cache ();
271
272   stop_pc = regcache_read_pc (get_current_regcache ());
273   nullify_last_target_wait_ptid ();
274
275   /* Now restore the file positions of open file descriptors.  */
276   if (fp->filepos)
277     {
278       for (i = 0; i <= fp->maxfd; i++)
279         if (fp->filepos[i] != (off_t) -1)
280           call_lseek (i, fp->filepos[i], SEEK_SET);
281       /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
282          this is native-only.  If it ever has to be cross, we'll have
283          to rethink this.  */
284     }
285 }
286
287 /* Save infrun state for the fork PTID.
288    Exported for use by linux child_follow_fork.  */
289
290 static void
291 fork_save_infrun_state (struct fork_info *fp, int clobber_regs)
292 {
293   char path[PATH_MAX];
294   struct dirent *de;
295   DIR *d;
296
297   if (fp->savedregs)
298     regcache_xfree (fp->savedregs);
299
300   fp->savedregs = regcache_dup (get_current_regcache ());
301   fp->clobber_regs = clobber_regs;
302
303   if (clobber_regs)
304     {
305       /* Now save the 'state' (file position) of all open file descriptors.
306          Unfortunately fork does not take care of that for us...  */
307       snprintf (path, PATH_MAX, "/proc/%ld/fd",
308                 (long) ptid_get_pid (fp->ptid));
309       if ((d = opendir (path)) != NULL)
310         {
311           long tmp;
312
313           fp->maxfd = 0;
314           while ((de = readdir (d)) != NULL)
315             {
316               /* Count open file descriptors (actually find highest
317                  numbered).  */
318               tmp = strtol (&de->d_name[0], NULL, 10);
319               if (fp->maxfd < tmp)
320                 fp->maxfd = tmp;
321             }
322           /* Allocate array of file positions.  */
323           fp->filepos = XRESIZEVEC (off_t, fp->filepos, fp->maxfd + 1);
324
325           /* Initialize to -1 (invalid).  */
326           for (tmp = 0; tmp <= fp->maxfd; tmp++)
327             fp->filepos[tmp] = -1;
328
329           /* Now find actual file positions.  */
330           rewinddir (d);
331           while ((de = readdir (d)) != NULL)
332             if (isdigit (de->d_name[0]))
333               {
334                 tmp = strtol (&de->d_name[0], NULL, 10);
335                 fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR);
336               }
337           closedir (d);
338         }
339     }
340 }
341
342 /* Kill 'em all, let God sort 'em out...  */
343
344 void
345 linux_fork_killall (void)
346 {
347   /* Walk list and kill every pid.  No need to treat the
348      current inferior_ptid as special (we do not return a
349      status for it) -- however any process may be a child
350      or a parent, so may get a SIGCHLD from a previously
351      killed child.  Wait them all out.  */
352   struct fork_info *fp;
353   pid_t pid, ret;
354   int status;
355
356   for (fp = fork_list; fp; fp = fp->next)
357     {
358       pid = ptid_get_pid (fp->ptid);
359       do {
360         /* Use SIGKILL instead of PTRACE_KILL because the former works even
361            if the thread is running, while the later doesn't.  */
362         kill (pid, SIGKILL);
363         ret = waitpid (pid, &status, 0);
364         /* We might get a SIGCHLD instead of an exit status.  This is
365          aggravated by the first kill above - a child has just
366          died.  MVS comment cut-and-pasted from linux-nat.  */
367       } while (ret == pid && WIFSTOPPED (status));
368     }
369   init_fork_list ();    /* Clear list, prepare to start fresh.  */
370 }
371
372 /* The current inferior_ptid has exited, but there are other viable
373    forks to debug.  Delete the exiting one and context-switch to the
374    first available.  */
375
376 void
377 linux_fork_mourn_inferior (void)
378 {
379   struct fork_info *last;
380   int status;
381
382   /* Wait just one more time to collect the inferior's exit status.
383      Do not check whether this succeeds though, since we may be
384      dealing with a process that we attached to.  Such a process will
385      only report its exit status to its original parent.  */
386   waitpid (ptid_get_pid (inferior_ptid), &status, 0);
387
388   /* OK, presumably inferior_ptid is the one who has exited.
389      We need to delete that one from the fork_list, and switch
390      to the next available fork.  */
391   delete_fork (inferior_ptid);
392
393   /* There should still be a fork - if there's only one left,
394      delete_fork won't remove it, because we haven't updated
395      inferior_ptid yet.  */
396   gdb_assert (fork_list);
397
398   last = find_last_fork ();
399   fork_load_infrun_state (last);
400   printf_filtered (_("[Switching to %s]\n"),
401                    target_pid_to_str (inferior_ptid));
402
403   /* If there's only one fork, switch back to non-fork mode.  */
404   if (fork_list->next == NULL)
405     delete_fork (inferior_ptid);
406 }
407
408 /* The current inferior_ptid is being detached, but there are other
409    viable forks to debug.  Detach and delete it and context-switch to
410    the first available.  */
411
412 void
413 linux_fork_detach (const char *args, int from_tty)
414 {
415   /* OK, inferior_ptid is the one we are detaching from.  We need to
416      delete it from the fork_list, and switch to the next available
417      fork.  */
418
419   if (ptrace (PTRACE_DETACH, ptid_get_pid (inferior_ptid), 0, 0))
420     error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid));
421
422   delete_fork (inferior_ptid);
423
424   /* There should still be a fork - if there's only one left,
425      delete_fork won't remove it, because we haven't updated
426      inferior_ptid yet.  */
427   gdb_assert (fork_list);
428
429   fork_load_infrun_state (fork_list);
430
431   if (from_tty)
432     printf_filtered (_("[Switching to %s]\n"),
433                      target_pid_to_str (inferior_ptid));
434
435   /* If there's only one fork, switch back to non-fork mode.  */
436   if (fork_list->next == NULL)
437     delete_fork (inferior_ptid);
438 }
439
440 static void
441 inferior_call_waitpid_cleanup (void *fp)
442 {
443   struct fork_info *oldfp = (struct fork_info *) fp;
444
445   if (oldfp)
446     {
447       /* Switch back to inferior_ptid.  */
448       remove_breakpoints ();
449       fork_load_infrun_state (oldfp);
450       insert_breakpoints ();
451     }
452 }
453
454 static int
455 inferior_call_waitpid (ptid_t pptid, int pid)
456 {
457   struct objfile *waitpid_objf;
458   struct value *waitpid_fn = NULL;
459   struct value *argv[4], *retv;
460   struct gdbarch *gdbarch = get_current_arch ();
461   struct fork_info *oldfp = NULL, *newfp = NULL;
462   struct cleanup *old_cleanup;
463   int ret = -1;
464
465   if (!ptid_equal (pptid, inferior_ptid))
466     {
467       /* Switch to pptid.  */
468       oldfp = find_fork_ptid (inferior_ptid);
469       gdb_assert (oldfp != NULL);
470       newfp = find_fork_ptid (pptid);
471       gdb_assert (newfp != NULL);
472       fork_save_infrun_state (oldfp, 1);
473       remove_breakpoints ();
474       fork_load_infrun_state (newfp);
475       insert_breakpoints ();
476     }
477
478   old_cleanup = make_cleanup (inferior_call_waitpid_cleanup, oldfp);
479
480   /* Get the waitpid_fn.  */
481   if (lookup_minimal_symbol ("waitpid", NULL, NULL).minsym != NULL)
482     waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf);
483   if (!waitpid_fn
484       && lookup_minimal_symbol ("_waitpid", NULL, NULL).minsym != NULL)
485     waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf);
486   if (!waitpid_fn)
487     goto out;
488
489   /* Get the argv.  */
490   argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid);
491   argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0);
492   argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
493   argv[3] = 0;
494
495   retv = call_function_by_hand (waitpid_fn, NULL, 3, argv);
496   if (value_as_long (retv) < 0)
497     goto out;
498
499   ret = 0;
500
501 out:
502   do_cleanups (old_cleanup);
503   return ret;
504 }
505
506 /* Fork list <-> user interface.  */
507
508 static void
509 delete_checkpoint_command (char *args, int from_tty)
510 {
511   ptid_t ptid, pptid;
512   struct fork_info *fi;
513
514   if (!args || !*args)
515     error (_("Requires argument (checkpoint id to delete)"));
516
517   ptid = fork_id_to_ptid (parse_and_eval_long (args));
518   if (ptid_equal (ptid, minus_one_ptid))
519     error (_("No such checkpoint id, %s"), args);
520
521   if (ptid_equal (ptid, inferior_ptid))
522     error (_("\
523 Please switch to another checkpoint before deleting the current one"));
524
525   if (ptrace (PTRACE_KILL, ptid_get_pid (ptid), 0, 0))
526     error (_("Unable to kill pid %s"), target_pid_to_str (ptid));
527
528   fi = find_fork_ptid (ptid);
529   gdb_assert (fi);
530   pptid = fi->parent_ptid;
531
532   if (from_tty)
533     printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid));
534
535   delete_fork (ptid);
536
537   /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint
538      list, waitpid the ptid.
539      If fi->parent_ptid is a part of lwp and it is stoped, waitpid the
540      ptid.  */
541   if ((!find_thread_ptid (pptid) && find_fork_ptid (pptid))
542       || (find_thread_ptid (pptid) && is_stopped (pptid)))
543     {
544       if (inferior_call_waitpid (pptid, ptid_get_pid (ptid)))
545         warning (_("Unable to wait pid %s"), target_pid_to_str (ptid));
546     }
547 }
548
549 static void
550 detach_checkpoint_command (char *args, int from_tty)
551 {
552   ptid_t ptid;
553
554   if (!args || !*args)
555     error (_("Requires argument (checkpoint id to detach)"));
556
557   ptid = fork_id_to_ptid (parse_and_eval_long (args));
558   if (ptid_equal (ptid, minus_one_ptid))
559     error (_("No such checkpoint id, %s"), args);
560
561   if (ptid_equal (ptid, inferior_ptid))
562     error (_("\
563 Please switch to another checkpoint before detaching the current one"));
564
565   if (ptrace (PTRACE_DETACH, ptid_get_pid (ptid), 0, 0))
566     error (_("Unable to detach %s"), target_pid_to_str (ptid));
567
568   if (from_tty)
569     printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid));
570
571   delete_fork (ptid);
572 }
573
574 /* Print information about currently known checkpoints.  */
575
576 static void
577 info_checkpoints_command (char *arg, int from_tty)
578 {
579   struct gdbarch *gdbarch = get_current_arch ();
580   struct symtab_and_line sal;
581   struct fork_info *fp;
582   ULONGEST pc;
583   int requested = -1;
584   struct fork_info *printed = NULL;
585
586   if (arg && *arg)
587     requested = (int) parse_and_eval_long (arg);
588
589   for (fp = fork_list; fp; fp = fp->next)
590     {
591       if (requested > 0 && fp->num != requested)
592         continue;
593
594       printed = fp;
595       if (ptid_equal (fp->ptid, inferior_ptid))
596         {
597           printf_filtered ("* ");
598           pc = regcache_read_pc (get_current_regcache ());
599         }
600       else
601         {
602           printf_filtered ("  ");
603           pc = regcache_read_pc (fp->savedregs);
604         }
605       printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid));
606       if (fp->num == 0)
607         printf_filtered (_(" (main process)"));
608       printf_filtered (_(" at "));
609       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
610
611       sal = find_pc_line (pc, 0);
612       if (sal.symtab)
613         printf_filtered (_(", file %s"),
614                          symtab_to_filename_for_display (sal.symtab));
615       if (sal.line)
616         printf_filtered (_(", line %d"), sal.line);
617       if (!sal.symtab && !sal.line)
618         {
619           struct bound_minimal_symbol msym;
620
621           msym = lookup_minimal_symbol_by_pc (pc);
622           if (msym.minsym)
623             printf_filtered (", <%s>", MSYMBOL_LINKAGE_NAME (msym.minsym));
624         }
625
626       putchar_filtered ('\n');
627     }
628   if (printed == NULL)
629     {
630       if (requested > 0)
631         printf_filtered (_("No checkpoint number %d.\n"), requested);
632       else
633         printf_filtered (_("No checkpoints.\n"));
634     }
635 }
636
637 /* The PID of the process we're checkpointing.  */
638 static int checkpointing_pid = 0;
639
640 int
641 linux_fork_checkpointing_p (int pid)
642 {
643   return (checkpointing_pid == pid);
644 }
645
646 /* Callback for iterate over threads.  Used to check whether
647    the current inferior is multi-threaded.  Returns true as soon
648    as it sees the second thread of the current inferior.  */
649
650 static int
651 inf_has_multiple_thread_cb (struct thread_info *tp, void *data)
652 {
653   int *count_p = (int *) data;
654   
655   if (current_inferior ()->pid == ptid_get_pid (tp->ptid))
656     (*count_p)++;
657   
658   /* Stop the iteration if multiple threads have been detected.  */
659   return *count_p > 1;
660 }
661
662 /* Return true if the current inferior is multi-threaded.  */
663
664 static int
665 inf_has_multiple_threads (void)
666 {
667   int count = 0;
668
669   iterate_over_threads (inf_has_multiple_thread_cb, &count);
670   return (count > 1);
671 }
672
673 static void
674 checkpoint_command (char *args, int from_tty)
675 {
676   struct objfile *fork_objf;
677   struct gdbarch *gdbarch;
678   struct target_waitstatus last_target_waitstatus;
679   ptid_t last_target_ptid;
680   struct value *fork_fn = NULL, *ret;
681   struct fork_info *fp;
682   pid_t retpid;
683
684   if (!target_has_execution) 
685     error (_("The program is not being run."));
686
687   /* Ensure that the inferior is not multithreaded.  */
688   update_thread_list ();
689   if (inf_has_multiple_threads ())
690     error (_("checkpoint: can't checkpoint multiple threads."));
691   
692   /* Make the inferior fork, record its (and gdb's) state.  */
693
694   if (lookup_minimal_symbol ("fork", NULL, NULL).minsym != NULL)
695     fork_fn = find_function_in_inferior ("fork", &fork_objf);
696   if (!fork_fn)
697     if (lookup_minimal_symbol ("_fork", NULL, NULL).minsym != NULL)
698       fork_fn = find_function_in_inferior ("fork", &fork_objf);
699   if (!fork_fn)
700     error (_("checkpoint: can't find fork function in inferior."));
701
702   gdbarch = get_objfile_arch (fork_objf);
703   ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
704
705   /* Tell linux-nat.c that we're checkpointing this inferior.  */
706   {
707     scoped_restore save_pid
708       = make_scoped_restore (&checkpointing_pid, ptid_get_pid (inferior_ptid));
709
710     ret = call_function_by_hand (fork_fn, NULL, 0, &ret);
711   }
712
713   if (!ret)     /* Probably can't happen.  */
714     error (_("checkpoint: call_function_by_hand returned null."));
715
716   retpid = value_as_long (ret);
717   get_last_target_status (&last_target_ptid, &last_target_waitstatus);
718
719   fp = find_fork_pid (retpid);
720
721   if (from_tty)
722     {
723       int parent_pid;
724
725       printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"),
726                        fp != NULL ? fp->num : -1, (long) retpid);
727       if (info_verbose)
728         {
729           parent_pid = ptid_get_lwp (last_target_ptid);
730           if (parent_pid == 0)
731             parent_pid = ptid_get_pid (last_target_ptid);
732           printf_filtered (_("   gdb says parent = %ld.\n"),
733                            (long) parent_pid);
734         }
735     }
736
737   if (!fp)
738     error (_("Failed to find new fork"));
739   fork_save_infrun_state (fp, 1);
740   fp->parent_ptid = last_target_ptid;
741 }
742
743 static void
744 linux_fork_context (struct fork_info *newfp, int from_tty)
745 {
746   /* Now we attempt to switch processes.  */
747   struct fork_info *oldfp;
748
749   gdb_assert (newfp != NULL);
750
751   oldfp = find_fork_ptid (inferior_ptid);
752   gdb_assert (oldfp != NULL);
753
754   fork_save_infrun_state (oldfp, 1);
755   remove_breakpoints ();
756   fork_load_infrun_state (newfp);
757   insert_breakpoints ();
758
759   printf_filtered (_("Switching to %s\n"),
760                    target_pid_to_str (inferior_ptid));
761
762   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
763 }
764
765 /* Switch inferior process (checkpoint) context, by checkpoint id.  */
766 static void
767 restart_command (char *args, int from_tty)
768 {
769   struct fork_info *fp;
770
771   if (!args || !*args)
772     error (_("Requires argument (checkpoint id to restart)"));
773
774   if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL)
775     error (_("Not found: checkpoint id %s"), args);
776
777   linux_fork_context (fp, from_tty);
778 }
779
780 void
781 _initialize_linux_fork (void)
782 {
783   init_fork_list ();
784
785   /* Checkpoint command: create a fork of the inferior process
786      and set it aside for later debugging.  */
787
788   add_com ("checkpoint", class_obscure, checkpoint_command, _("\
789 Fork a duplicate process (experimental)."));
790
791   /* Restart command: restore the context of a specified checkpoint
792      process.  */
793
794   add_com ("restart", class_obscure, restart_command, _("\
795 restart <n>: restore program context from a checkpoint.\n\
796 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
797
798   /* Delete checkpoint command: kill the process and remove it from
799      the fork list.  */
800
801   add_cmd ("checkpoint", class_obscure, delete_checkpoint_command, _("\
802 Delete a checkpoint (experimental)."),
803            &deletelist);
804
805   /* Detach checkpoint command: release the process to run independently,
806      and remove it from the fork list.  */
807
808   add_cmd ("checkpoint", class_obscure, detach_checkpoint_command, _("\
809 Detach from a checkpoint (experimental)."),
810            &detachlist);
811
812   /* Info checkpoints command: list all forks/checkpoints
813      currently under gdb's control.  */
814
815   add_info ("checkpoints", info_checkpoints_command,
816             _("IDs of currently known checkpoints."));
817 }