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