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