Commit | Line | Data |
---|---|---|
ac264b3b MS |
1 | /* GNU/Linux native-dependent code for debugging multiple forks. |
2 | ||
9b254dd1 | 3 | Copyright (C) 2005, 2006, 2007, 2008 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" | |
21 | #include "inferior.h" | |
22 | #include "regcache.h" | |
23 | #include "gdbcmd.h" | |
24 | #include "infcall.h" | |
791b663b | 25 | #include "gdb_assert.h" |
ac264b3b MS |
26 | #include "gdb_string.h" |
27 | #include "linux-fork.h" | |
f973ed9c | 28 | #include "linux-nat.h" |
ac264b3b MS |
29 | |
30 | #include <sys/ptrace.h> | |
3c61c145 | 31 | #include "gdb_wait.h" |
ac264b3b | 32 | #include <sys/param.h> |
91c06669 | 33 | #include "gdb_dirent.h" |
ac264b3b MS |
34 | #include <ctype.h> |
35 | ||
36 | struct fork_info *fork_list; | |
37 | static int highest_fork_num; | |
38 | ||
39 | /* Prevent warning from -Wmissing-prototypes. */ | |
40 | extern void _initialize_linux_fork (void); | |
41 | ||
42 | int detach_fork = 1; /* Default behavior is to detach | |
43 | newly forked processes (legacy). */ | |
44 | ||
45 | /* Fork list data structure: */ | |
46 | struct fork_info | |
47 | { | |
48 | struct fork_info *next; | |
49 | ptid_t 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 | ULONGEST pc; /* PC for info fork. */ | |
55 | off_t *filepos; /* Set of open file descriptors' offsets. */ | |
56 | int maxfd; | |
57 | }; | |
58 | ||
59 | /* Fork list methods: */ | |
60 | ||
61 | extern int | |
62 | forks_exist_p (void) | |
63 | { | |
64 | return (fork_list != NULL); | |
65 | } | |
66 | ||
67 | /* Add a fork to internal fork list. | |
68 | Called from linux child_follow_fork. */ | |
69 | ||
70 | extern struct fork_info * | |
71 | add_fork (pid_t pid) | |
72 | { | |
73 | struct fork_info *fp; | |
74 | ||
56aac7e8 | 75 | if (fork_list == NULL && pid != PIDGET (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; | |
82 | add_fork (PIDGET (inferior_ptid)); /* safe recursion */ | |
83 | } | |
84 | ||
85 | fp = XZALLOC (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 | ||
93 | static void | |
94 | free_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 | ||
118 | static void | |
119 | delete_fork (ptid_t ptid) | |
120 | { | |
121 | struct fork_info *fp, *fpprev; | |
122 | ||
123 | fpprev = NULL; | |
124 | ||
125 | for (fp = fork_list; fp; fpprev = fp, fp = fp->next) | |
126 | if (ptid_equal (fp->ptid, ptid)) | |
127 | break; | |
128 | ||
129 | if (!fp) | |
130 | return; | |
131 | ||
132 | if (fpprev) | |
133 | fpprev->next = fp->next; | |
134 | else | |
135 | fork_list = fp->next; | |
136 | ||
137 | free_fork (fp); | |
138 | ||
139 | /* Special case: if there is now only one process in the list, | |
140 | and if it is (hopefully!) the current inferior_ptid, then | |
141 | remove it, leaving the list empty -- we're now down to the | |
142 | default case of debugging a single process. */ | |
143 | if (fork_list != NULL && fork_list->next == NULL && | |
144 | ptid_equal (fork_list->ptid, inferior_ptid)) | |
145 | { | |
146 | /* Last fork -- delete from list and handle as solo process | |
147 | (should be a safe recursion). */ | |
148 | delete_fork (inferior_ptid); | |
149 | } | |
150 | } | |
151 | ||
152 | /* Find a fork_info by matching PTID. */ | |
153 | static struct fork_info * | |
154 | find_fork_ptid (ptid_t ptid) | |
155 | { | |
156 | struct fork_info *fp; | |
157 | ||
158 | for (fp = fork_list; fp; fp = fp->next) | |
159 | if (ptid_equal (fp->ptid, ptid)) | |
160 | return fp; | |
161 | ||
162 | return NULL; | |
163 | } | |
164 | ||
165 | /* Find a fork_info by matching ID. */ | |
166 | static struct fork_info * | |
167 | find_fork_id (int num) | |
168 | { | |
169 | struct fork_info *fp; | |
170 | ||
171 | for (fp = fork_list; fp; fp = fp->next) | |
172 | if (fp->num == num) | |
173 | return fp; | |
174 | ||
175 | return NULL; | |
176 | } | |
177 | ||
178 | /* Find a fork_info by matching pid. */ | |
179 | extern struct fork_info * | |
180 | find_fork_pid (pid_t pid) | |
181 | { | |
182 | struct fork_info *fp; | |
183 | ||
184 | for (fp = fork_list; fp; fp = fp->next) | |
185 | if (pid == ptid_get_pid (fp->ptid)) | |
186 | return fp; | |
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 pid_to_ptid (-1); | |
199 | } | |
200 | ||
201 | static void | |
202 | init_fork_list (void) | |
203 | { | |
204 | struct fork_info *fp, *fpnext; | |
205 | ||
206 | if (!fork_list) | |
207 | return; | |
208 | ||
209 | for (fp = fork_list; fp; fp = fpnext) | |
210 | { | |
211 | fpnext = fp->next; | |
212 | free_fork (fp); | |
213 | } | |
214 | ||
215 | fork_list = NULL; | |
216 | } | |
217 | ||
218 | /* Fork list <-> gdb interface. */ | |
219 | ||
220 | /* Utility function for fork_load/fork_save. | |
221 | Calls lseek in the (current) inferior process. */ | |
222 | ||
223 | static off_t | |
224 | call_lseek (int fd, off_t offset, int whence) | |
225 | { | |
226 | char exp[80]; | |
227 | ||
228 | snprintf (&exp[0], sizeof (exp), "lseek (%d, %ld, %d)", | |
229 | fd, (long) offset, whence); | |
230 | return (off_t) parse_and_eval_long (&exp[0]); | |
231 | } | |
232 | ||
233 | /* Load infrun state for the fork PTID. */ | |
234 | ||
235 | static void | |
236 | fork_load_infrun_state (struct fork_info *fp) | |
237 | { | |
238 | extern void nullify_last_target_wait_ptid (); | |
239 | int i; | |
240 | ||
791b663b DJ |
241 | inferior_ptid = fp->ptid; |
242 | ||
f973ed9c DJ |
243 | linux_nat_switch_fork (inferior_ptid); |
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 | ||
791b663b | 251 | stop_pc = read_pc (); |
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 | ||
269 | extern void | |
270 | fork_save_infrun_state (struct fork_info *fp, int clobber_regs) | |
271 | { | |
272 | char path[MAXPATHLEN]; | |
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 MS |
280 | fp->clobber_regs = clobber_regs; |
281 | fp->pc = read_pc (); | |
282 | ||
283 | if (clobber_regs) | |
284 | { | |
285 | /* Now save the 'state' (file position) of all open file descriptors. | |
286 | Unfortunately fork does not take care of that for us... */ | |
287 | snprintf (path, MAXPATHLEN, "/proc/%ld/fd", (long) PIDGET (fp->ptid)); | |
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. */ | |
302 | fp->filepos = xrealloc (fp->filepos, | |
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 | ||
324 | extern void | |
325 | linux_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 | { | |
338 | pid = PIDGET (fp->ptid); | |
339 | do { | |
340 | ptrace (PT_KILL, pid, 0, 0); | |
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 | ||
354 | extern void | |
355 | linux_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 | ||
384 | /* Fork list <-> user interface. */ | |
385 | ||
386 | static void | |
387 | delete_fork_command (char *args, int from_tty) | |
388 | { | |
389 | ptid_t ptid; | |
390 | ||
391 | if (!args || !*args) | |
392 | error (_("Requires argument (fork/checkpoint id to delete)")); | |
393 | ||
394 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
395 | if (ptid_equal (ptid, minus_one_ptid)) | |
396 | error (_("No such fork/checkpoint id, %s"), args); | |
397 | ||
398 | if (ptid_equal (ptid, inferior_ptid)) | |
399 | error (_("Please switch to another fork/checkpoint before deleting the current one")); | |
400 | ||
1dce6535 | 401 | if (ptrace (PTRACE_KILL, PIDGET (ptid), 0, 0)) |
ac264b3b MS |
402 | error (_("Unable to kill pid %s"), target_tid_to_str (ptid)); |
403 | ||
404 | if (from_tty) | |
405 | printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid)); | |
406 | ||
407 | delete_fork (ptid); | |
408 | } | |
409 | ||
410 | static void | |
411 | detach_fork_command (char *args, int from_tty) | |
412 | { | |
413 | ptid_t ptid; | |
414 | ||
415 | if (!args || !*args) | |
416 | error (_("Requires argument (fork id to detach)")); | |
417 | ||
418 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
419 | if (ptid_equal (ptid, minus_one_ptid)) | |
420 | error (_("No such fork id, %s"), args); | |
421 | ||
422 | if (ptid_equal (ptid, inferior_ptid)) | |
423 | error (_("Please switch to another fork before detaching the current one")); | |
424 | ||
1dce6535 | 425 | if (ptrace (PTRACE_DETACH, PIDGET (ptid), 0, 0)) |
ac264b3b MS |
426 | error (_("Unable to detach %s"), target_pid_to_str (ptid)); |
427 | ||
428 | if (from_tty) | |
429 | printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid)); | |
430 | ||
431 | delete_fork (ptid); | |
432 | } | |
433 | ||
434 | /* Print information about currently known forks. */ | |
435 | ||
436 | static void | |
437 | info_forks_command (char *arg, int from_tty) | |
438 | { | |
439 | struct frame_info *cur_frame; | |
440 | struct symtab_and_line sal; | |
441 | struct symtab *cur_symtab; | |
442 | struct fork_info *fp; | |
443 | int cur_line; | |
444 | ULONGEST pc; | |
b8db102d MS |
445 | int requested = -1; |
446 | struct fork_info *printed = NULL; | |
447 | ||
448 | if (arg && *arg) | |
449 | requested = (int) parse_and_eval_long (arg); | |
ac264b3b MS |
450 | |
451 | for (fp = fork_list; fp; fp = fp->next) | |
452 | { | |
b8db102d MS |
453 | if (requested > 0 && fp->num != requested) |
454 | continue; | |
455 | ||
456 | printed = fp; | |
ac264b3b MS |
457 | if (ptid_equal (fp->ptid, inferior_ptid)) |
458 | { | |
459 | printf_filtered ("* "); | |
460 | pc = read_pc (); | |
461 | } | |
462 | else | |
463 | { | |
464 | printf_filtered (" "); | |
465 | pc = fp->pc; | |
466 | } | |
467 | printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid)); | |
468 | if (fp->num == 0) | |
469 | printf_filtered (_(" (main process)")); | |
470 | printf_filtered (_(" at ")); | |
ed49a04f | 471 | fputs_filtered (paddress (pc), gdb_stdout); |
ac264b3b MS |
472 | |
473 | sal = find_pc_line (pc, 0); | |
474 | if (sal.symtab) | |
475 | { | |
476 | char *tmp = strrchr (sal.symtab->filename, '/'); | |
477 | ||
478 | if (tmp) | |
479 | printf_filtered (_(", file %s"), tmp + 1); | |
480 | else | |
481 | printf_filtered (_(", file %s"), sal.symtab->filename); | |
482 | } | |
483 | if (sal.line) | |
484 | printf_filtered (_(", line %d"), sal.line); | |
485 | if (!sal.symtab && !sal.line) | |
486 | { | |
487 | struct minimal_symbol *msym; | |
488 | ||
489 | msym = lookup_minimal_symbol_by_pc (pc); | |
490 | if (msym) | |
491 | printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym)); | |
492 | } | |
493 | ||
494 | putchar_filtered ('\n'); | |
495 | } | |
b8db102d MS |
496 | if (printed == NULL) |
497 | { | |
498 | if (requested > 0) | |
499 | printf_filtered (_("No fork number %d.\n"), requested); | |
500 | else | |
501 | printf_filtered (_("No forks.\n")); | |
502 | } | |
ac264b3b MS |
503 | } |
504 | ||
505 | /* Save/restore mode variable 'detach_fork': | |
506 | We need to temporarily take over this mode variable, while | |
507 | preserving the user-specified state, and make sure that it | |
508 | gets restored in case of error. | |
509 | ||
510 | The int pointer that we use comes from the caller, so we can | |
511 | be called more than once (even though currently we don't need to). */ | |
512 | ||
513 | static void | |
514 | restore_detach_fork (void *arg) | |
515 | { | |
516 | detach_fork = *(int *) arg; | |
517 | } | |
518 | ||
519 | static struct cleanup * | |
520 | save_detach_fork (int *saved_val) | |
521 | { | |
522 | *saved_val = detach_fork; | |
523 | return make_cleanup (restore_detach_fork, (void *) saved_val); | |
524 | } | |
525 | ||
526 | static void | |
527 | checkpoint_command (char *args, int from_tty) | |
528 | { | |
529 | struct target_waitstatus last_target_waitstatus; | |
530 | ptid_t last_target_ptid; | |
531 | struct value *fork_fn = NULL, *ret; | |
532 | struct fork_info *fp; | |
533 | pid_t retpid; | |
534 | struct cleanup *old_chain; | |
535 | long i; | |
536 | /* Make this temp var static, 'cause it's used in the error context. */ | |
537 | static int temp_detach_fork; | |
538 | ||
74960c60 VP |
539 | /* Remove breakpoints, so that they are not inserted |
540 | in the forked process. */ | |
541 | remove_breakpoints (); | |
542 | ||
ac264b3b MS |
543 | /* Make the inferior fork, record its (and gdb's) state. */ |
544 | ||
545 | if (lookup_minimal_symbol ("fork", NULL, NULL) != NULL) | |
546 | fork_fn = find_function_in_inferior ("fork"); | |
547 | if (!fork_fn) | |
548 | if (lookup_minimal_symbol ("_fork", NULL, NULL) != NULL) | |
549 | fork_fn = find_function_in_inferior ("fork"); | |
550 | if (!fork_fn) | |
551 | error (_("checkpoint: can't find fork function in inferior.")); | |
552 | ||
553 | ret = value_from_longest (builtin_type_int, 0); | |
554 | old_chain = save_detach_fork (&temp_detach_fork); | |
555 | detach_fork = 0; | |
556 | ret = call_function_by_hand (fork_fn, 0, &ret); | |
557 | do_cleanups (old_chain); | |
558 | if (!ret) /* Probably can't happen. */ | |
559 | error (_("checkpoint: call_function_by_hand returned null.")); | |
560 | ||
561 | retpid = value_as_long (ret); | |
562 | get_last_target_status (&last_target_ptid, &last_target_waitstatus); | |
563 | if (from_tty) | |
564 | { | |
565 | int parent_pid; | |
566 | ||
567 | printf_filtered (_("checkpoint: fork returned pid %ld.\n"), | |
568 | (long) retpid); | |
569 | if (info_verbose) | |
570 | { | |
571 | parent_pid = ptid_get_lwp (last_target_ptid); | |
572 | if (parent_pid == 0) | |
573 | parent_pid = ptid_get_pid (last_target_ptid); | |
574 | printf_filtered (_(" gdb says parent = %ld.\n"), | |
575 | (long) parent_pid); | |
576 | } | |
577 | } | |
578 | ||
579 | fp = find_fork_pid (retpid); | |
580 | if (!fp) | |
581 | error (_("Failed to find new fork")); | |
582 | fork_save_infrun_state (fp, 1); | |
74960c60 | 583 | insert_breakpoints (); |
ac264b3b MS |
584 | } |
585 | ||
586 | static void | |
587 | linux_fork_context (struct fork_info *newfp, int from_tty) | |
588 | { | |
589 | /* Now we attempt to switch processes. */ | |
590 | struct fork_info *oldfp = find_fork_ptid (inferior_ptid); | |
591 | ptid_t ptid; | |
592 | int id, i; | |
593 | ||
594 | if (!newfp) | |
595 | error (_("No such fork/process")); | |
596 | ||
597 | if (!oldfp) | |
791b663b | 598 | oldfp = add_fork (ptid_get_pid (inferior_ptid)); |
ac264b3b MS |
599 | |
600 | fork_save_infrun_state (oldfp, 1); | |
74960c60 | 601 | remove_breakpoints (); |
ac264b3b | 602 | fork_load_infrun_state (newfp); |
74960c60 | 603 | insert_breakpoints (); |
ac264b3b MS |
604 | |
605 | printf_filtered (_("Switching to %s\n"), | |
606 | target_pid_to_str (inferior_ptid)); | |
607 | ||
608 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); | |
609 | } | |
610 | ||
611 | /* Switch inferior process (fork) context, by process id. */ | |
612 | static void | |
613 | process_command (char *args, int from_tty) | |
614 | { | |
615 | struct fork_info *fp; | |
616 | ||
617 | if (!args || !*args) | |
618 | error (_("Requires argument (process id to switch to)")); | |
619 | ||
620 | if ((fp = find_fork_pid (parse_and_eval_long (args))) == NULL) | |
621 | error (_("Not found: process id %s"), args); | |
622 | ||
623 | linux_fork_context (fp, from_tty); | |
624 | } | |
625 | ||
626 | /* Switch inferior process (fork) context, by fork id. */ | |
627 | static void | |
628 | fork_command (char *args, int from_tty) | |
629 | { | |
630 | struct fork_info *fp; | |
631 | ||
632 | if (!args || !*args) | |
633 | error (_("Requires argument (fork id to switch to)")); | |
634 | ||
635 | if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) | |
636 | error (_("Not found: fork id %s"), args); | |
637 | ||
638 | linux_fork_context (fp, from_tty); | |
639 | } | |
640 | ||
641 | /* Switch inferior process (fork) context, by checkpoint id. */ | |
642 | static void | |
643 | restart_command (char *args, int from_tty) | |
644 | { | |
645 | struct fork_info *fp; | |
646 | ||
647 | if (!args || !*args) | |
648 | error (_("Requires argument (checkpoint id to restart)")); | |
649 | ||
650 | if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) | |
651 | error (_("Not found: checkpoint id %s"), args); | |
652 | ||
653 | linux_fork_context (fp, from_tty); | |
654 | } | |
655 | ||
656 | void | |
657 | _initialize_linux_fork (void) | |
658 | { | |
659 | init_fork_list (); | |
660 | ||
661 | /* Set/show detach-on-fork: user-settable mode. */ | |
662 | ||
663 | add_setshow_boolean_cmd ("detach-on-fork", class_obscure, &detach_fork, _("\ | |
664 | Set whether gdb will detach the child of a fork."), _("\ | |
665 | Show whether gdb will detach the child of a fork."), _("\ | |
666 | Tells gdb whether to detach the child of a fork."), | |
667 | NULL, NULL, &setlist, &showlist); | |
668 | ||
669 | /* Set/show restart-auto-finish: user-settable count. Causes the | |
670 | first "restart" of a fork to do some number of "finish" commands | |
671 | before returning to user. | |
672 | ||
673 | Useful because otherwise the virgin fork process will be stopped | |
674 | somewhere in the un-interesting fork system call. */ | |
675 | ||
676 | /* Checkpoint command: create a fork of the inferior process | |
677 | and set it aside for later debugging. */ | |
678 | ||
679 | add_com ("checkpoint", class_obscure, checkpoint_command, _("\ | |
680 | Fork a duplicate process (experimental).")); | |
681 | ||
682 | /* Restart command: restore the context of a specified fork | |
683 | process. May be used for "program forks" as well as for | |
684 | "debugger forks" (checkpoints). */ | |
685 | ||
686 | add_com ("restart", class_obscure, restart_command, _("\ | |
687 | restart <n>: restore program context from a checkpoint.\n\ | |
688 | Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'.")); | |
689 | ||
b8db102d | 690 | /* Delete checkpoint command: kill the process and remove it from |
ac264b3b MS |
691 | fork list. */ |
692 | ||
b8db102d MS |
693 | add_cmd ("checkpoint", class_obscure, delete_fork_command, _("\ |
694 | Delete a fork/checkpoint (experimental)."), | |
695 | &deletelist); | |
ac264b3b | 696 | |
f73adfeb | 697 | /* Detach checkpoint command: release the process to run independently, |
ac264b3b MS |
698 | and remove it from the fork list. */ |
699 | ||
f73adfeb AS |
700 | add_cmd ("checkpoint", class_obscure, detach_fork_command, _("\ |
701 | Detach from a fork/checkpoint (experimental)."), | |
702 | &detachlist); | |
ac264b3b MS |
703 | |
704 | /* Info checkpoints command: list all forks/checkpoints | |
705 | currently under gdb's control. */ | |
706 | ||
707 | add_info ("checkpoints", info_forks_command, | |
708 | _("IDs of currently known forks/checkpoints.")); | |
709 | ||
710 | /* Command aliases (let "fork" and "checkpoint" be used | |
711 | interchangeably). */ | |
712 | ||
b8db102d | 713 | add_alias_cmd ("fork", "checkpoint", class_obscure, 1, &deletelist); |
f73adfeb | 714 | add_alias_cmd ("fork", "checkpoint", class_obscure, 1, &detachlist); |
ac264b3b MS |
715 | add_info_alias ("forks", "checkpoints", 0); |
716 | ||
717 | /* "fork <n>" (by analogy to "thread <n>"). */ | |
718 | add_com ("fork", class_obscure, fork_command, _("\ | |
719 | fork <n>: Switch between forked processes.\n\ | |
720 | Argument 'n' is fork ID, as displayed by 'info forks'.")); | |
721 | ||
722 | /* "process <proc id>" as opposed to "fork <fork id>". */ | |
723 | add_com ("process", class_obscure, process_command, _("\ | |
724 | process <pid>: Switch between forked processes.\n\ | |
725 | Argument 'pid' is process ID, as displayed by 'info forks' or 'shell ps'.")); | |
726 | } |