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