Commit | Line | Data |
---|---|---|
ac264b3b MS |
1 | /* GNU/Linux native-dependent code for debugging multiple forks. |
2 | ||
7b6bb8da | 3 | Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
4c38e0a4 | 4 | Free Software Foundation, Inc. |
ac264b3b MS |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
ac264b3b MS |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
ac264b3b MS |
20 | |
21 | #include "defs.h" | |
5af949e3 | 22 | #include "arch-utils.h" |
ac264b3b MS |
23 | #include "inferior.h" |
24 | #include "regcache.h" | |
25 | #include "gdbcmd.h" | |
26 | #include "infcall.h" | |
3e3b026f | 27 | #include "objfiles.h" |
791b663b | 28 | #include "gdb_assert.h" |
ac264b3b MS |
29 | #include "gdb_string.h" |
30 | #include "linux-fork.h" | |
f973ed9c | 31 | #include "linux-nat.h" |
7a298875 | 32 | #include "gdbthread.h" |
ac264b3b MS |
33 | |
34 | #include <sys/ptrace.h> | |
3c61c145 | 35 | #include "gdb_wait.h" |
ac264b3b | 36 | #include <sys/param.h> |
91c06669 | 37 | #include "gdb_dirent.h" |
ac264b3b MS |
38 | #include <ctype.h> |
39 | ||
40 | struct fork_info *fork_list; | |
41 | static int highest_fork_num; | |
42 | ||
43 | /* Prevent warning from -Wmissing-prototypes. */ | |
44 | extern void _initialize_linux_fork (void); | |
45 | ||
ac264b3b MS |
46 | /* Fork list data structure: */ |
47 | struct 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 | 62 | int |
ac264b3b MS |
63 | forks_exist_p (void) |
64 | { | |
65 | return (fork_list != NULL); | |
66 | } | |
67 | ||
3cb5bea9 | 68 | /* Add a fork to the internal fork list. */ |
ac264b3b | 69 | |
3cb5bea9 | 70 | struct fork_info * |
ac264b3b MS |
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 | ||
3cb5bea9 | 139 | /* Special case: if there is now only one process in the list, |
ac264b3b MS |
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 | ||
3cb5bea9 | 220 | /* Utility function for fork_load/fork_save. |
ac264b3b MS |
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 | ||
2277426b | 241 | linux_nat_switch_fork (fp->ptid); |
f973ed9c | 242 | |
ac264b3b | 243 | if (fp->savedregs && fp->clobber_regs) |
594f7785 | 244 | regcache_cpy (get_current_regcache (), fp->savedregs); |
ac264b3b | 245 | |
791b663b DJ |
246 | registers_changed (); |
247 | reinit_frame_cache (); | |
248 | ||
fb14de7b | 249 | stop_pc = regcache_read_pc (get_current_regcache ()); |
ac264b3b MS |
250 | nullify_last_target_wait_ptid (); |
251 | ||
252 | /* Now restore the file positions of open file descriptors. */ | |
253 | if (fp->filepos) | |
254 | { | |
255 | for (i = 0; i <= fp->maxfd; i++) | |
256 | if (fp->filepos[i] != (off_t) -1) | |
257 | call_lseek (i, fp->filepos[i], SEEK_SET); | |
258 | /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because | |
259 | this is native-only. If it ever has to be cross, we'll have | |
260 | to rethink this. */ | |
261 | } | |
262 | } | |
263 | ||
264 | /* Save infrun state for the fork PTID. | |
265 | Exported for use by linux child_follow_fork. */ | |
266 | ||
2277426b | 267 | static void |
ac264b3b MS |
268 | fork_save_infrun_state (struct fork_info *fp, int clobber_regs) |
269 | { | |
270 | char path[MAXPATHLEN]; | |
271 | struct dirent *de; | |
272 | DIR *d; | |
273 | ||
274 | if (fp->savedregs) | |
275 | regcache_xfree (fp->savedregs); | |
276 | ||
594f7785 | 277 | fp->savedregs = regcache_dup (get_current_regcache ()); |
ac264b3b | 278 | fp->clobber_regs = clobber_regs; |
ac264b3b MS |
279 | |
280 | if (clobber_regs) | |
281 | { | |
282 | /* Now save the 'state' (file position) of all open file descriptors. | |
283 | Unfortunately fork does not take care of that for us... */ | |
284 | snprintf (path, MAXPATHLEN, "/proc/%ld/fd", (long) PIDGET (fp->ptid)); | |
285 | if ((d = opendir (path)) != NULL) | |
286 | { | |
287 | long tmp; | |
288 | ||
289 | fp->maxfd = 0; | |
290 | while ((de = readdir (d)) != NULL) | |
291 | { | |
292 | /* Count open file descriptors (actually find highest | |
293 | numbered). */ | |
294 | tmp = strtol (&de->d_name[0], NULL, 10); | |
295 | if (fp->maxfd < tmp) | |
296 | fp->maxfd = tmp; | |
297 | } | |
298 | /* Allocate array of file positions. */ | |
3cb5bea9 | 299 | fp->filepos = xrealloc (fp->filepos, |
ac264b3b MS |
300 | (fp->maxfd + 1) * sizeof (*fp->filepos)); |
301 | ||
302 | /* Initialize to -1 (invalid). */ | |
303 | for (tmp = 0; tmp <= fp->maxfd; tmp++) | |
304 | fp->filepos[tmp] = -1; | |
305 | ||
306 | /* Now find actual file positions. */ | |
307 | rewinddir (d); | |
308 | while ((de = readdir (d)) != NULL) | |
309 | if (isdigit (de->d_name[0])) | |
310 | { | |
311 | tmp = strtol (&de->d_name[0], NULL, 10); | |
312 | fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR); | |
313 | } | |
314 | closedir (d); | |
315 | } | |
316 | } | |
317 | } | |
318 | ||
319 | /* Kill 'em all, let God sort 'em out... */ | |
320 | ||
3cb5bea9 | 321 | void |
ac264b3b MS |
322 | linux_fork_killall (void) |
323 | { | |
324 | /* Walk list and kill every pid. No need to treat the | |
325 | current inferior_ptid as special (we do not return a | |
326 | status for it) -- however any process may be a child | |
327 | or a parent, so may get a SIGCHLD from a previously | |
328 | killed child. Wait them all out. */ | |
56aac7e8 | 329 | struct fork_info *fp; |
ac264b3b MS |
330 | pid_t pid, ret; |
331 | int status; | |
332 | ||
56aac7e8 MS |
333 | for (fp = fork_list; fp; fp = fp->next) |
334 | { | |
335 | pid = PIDGET (fp->ptid); | |
336 | do { | |
4c28f408 PA |
337 | /* Use SIGKILL instead of PTRACE_KILL because the former works even |
338 | if the thread is running, while the later doesn't. */ | |
339 | kill (pid, SIGKILL); | |
56aac7e8 MS |
340 | ret = waitpid (pid, &status, 0); |
341 | /* We might get a SIGCHLD instead of an exit status. This is | |
342 | aggravated by the first kill above - a child has just | |
343 | died. MVS comment cut-and-pasted from linux-nat. */ | |
344 | } while (ret == pid && WIFSTOPPED (status)); | |
345 | } | |
346 | init_fork_list (); /* Clear list, prepare to start fresh. */ | |
ac264b3b MS |
347 | } |
348 | ||
349 | /* The current inferior_ptid has exited, but there are other viable | |
350 | forks to debug. Delete the exiting one and context-switch to the | |
351 | first available. */ | |
352 | ||
3cb5bea9 | 353 | void |
ac264b3b MS |
354 | linux_fork_mourn_inferior (void) |
355 | { | |
356 | /* Wait just one more time to collect the inferior's exit status. | |
357 | Do not check whether this succeeds though, since we may be | |
358 | dealing with a process that we attached to. Such a process will | |
359 | only report its exit status to its original parent. */ | |
360 | int status; | |
361 | ||
362 | waitpid (ptid_get_pid (inferior_ptid), &status, 0); | |
363 | ||
364 | /* OK, presumably inferior_ptid is the one who has exited. | |
365 | We need to delete that one from the fork_list, and switch | |
366 | to the next available fork. */ | |
367 | delete_fork (inferior_ptid); | |
791b663b DJ |
368 | |
369 | /* There should still be a fork - if there's only one left, | |
370 | delete_fork won't remove it, because we haven't updated | |
371 | inferior_ptid yet. */ | |
372 | gdb_assert (fork_list); | |
373 | ||
374 | fork_load_infrun_state (fork_list); | |
375 | printf_filtered (_("[Switching to %s]\n"), | |
376 | target_pid_to_str (inferior_ptid)); | |
377 | ||
378 | /* If there's only one fork, switch back to non-fork mode. */ | |
379 | if (fork_list->next == NULL) | |
380 | delete_fork (inferior_ptid); | |
ac264b3b MS |
381 | } |
382 | ||
7a7d3353 PA |
383 | /* The current inferior_ptid is being detached, but there are other |
384 | viable forks to debug. Detach and delete it and context-switch to | |
385 | the first available. */ | |
386 | ||
3cb5bea9 | 387 | void |
7a7d3353 PA |
388 | linux_fork_detach (char *args, int from_tty) |
389 | { | |
390 | /* OK, inferior_ptid is the one we are detaching from. We need to | |
391 | delete it from the fork_list, and switch to the next available | |
392 | fork. */ | |
393 | ||
394 | if (ptrace (PTRACE_DETACH, PIDGET (inferior_ptid), 0, 0)) | |
395 | error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid)); | |
396 | ||
397 | delete_fork (inferior_ptid); | |
7a7d3353 PA |
398 | |
399 | /* There should still be a fork - if there's only one left, | |
400 | delete_fork won't remove it, because we haven't updated | |
401 | inferior_ptid yet. */ | |
402 | gdb_assert (fork_list); | |
403 | ||
404 | fork_load_infrun_state (fork_list); | |
405 | ||
406 | if (from_tty) | |
407 | printf_filtered (_("[Switching to %s]\n"), | |
408 | target_pid_to_str (inferior_ptid)); | |
409 | ||
410 | /* If there's only one fork, switch back to non-fork mode. */ | |
411 | if (fork_list->next == NULL) | |
412 | delete_fork (inferior_ptid); | |
413 | } | |
414 | ||
7a298875 HZ |
415 | static void |
416 | inferior_call_waitpid_cleanup (void *fp) | |
417 | { | |
418 | struct fork_info *oldfp = fp; | |
419 | ||
e17c9e56 HZ |
420 | if (oldfp) |
421 | { | |
1777feb0 | 422 | /* Switch back to inferior_ptid. */ |
e17c9e56 HZ |
423 | remove_breakpoints (); |
424 | fork_load_infrun_state (oldfp); | |
425 | insert_breakpoints (); | |
426 | } | |
7a298875 HZ |
427 | } |
428 | ||
429 | static int | |
430 | inferior_call_waitpid (ptid_t pptid, int pid) | |
431 | { | |
432 | struct objfile *waitpid_objf; | |
433 | struct value *waitpid_fn = NULL; | |
e17c9e56 | 434 | struct value *argv[4], *retv; |
7a298875 HZ |
435 | struct gdbarch *gdbarch = get_current_arch (); |
436 | struct fork_info *oldfp = NULL, *newfp = NULL; | |
e17c9e56 | 437 | struct cleanup *old_cleanup; |
7a298875 HZ |
438 | int ret = -1; |
439 | ||
440 | if (!ptid_equal (pptid, inferior_ptid)) | |
441 | { | |
442 | /* Switch to pptid. */ | |
443 | oldfp = find_fork_ptid (inferior_ptid); | |
444 | gdb_assert (oldfp != NULL); | |
445 | newfp = find_fork_ptid (pptid); | |
446 | gdb_assert (oldfp != NULL); | |
447 | fork_save_infrun_state (oldfp, 1); | |
448 | remove_breakpoints (); | |
449 | fork_load_infrun_state (newfp); | |
450 | insert_breakpoints (); | |
7a298875 HZ |
451 | } |
452 | ||
e17c9e56 HZ |
453 | old_cleanup = make_cleanup (inferior_call_waitpid_cleanup, oldfp); |
454 | ||
7a298875 HZ |
455 | /* Get the waitpid_fn. */ |
456 | if (lookup_minimal_symbol ("waitpid", NULL, NULL) != NULL) | |
457 | waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf); | |
458 | if (!waitpid_fn && lookup_minimal_symbol ("_waitpid", NULL, NULL) != NULL) | |
459 | waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf); | |
460 | if (!waitpid_fn) | |
461 | goto out; | |
462 | ||
463 | /* Get the argv. */ | |
464 | argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid); | |
465 | argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0); | |
466 | argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); | |
467 | argv[3] = 0; | |
468 | ||
e17c9e56 HZ |
469 | retv = call_function_by_hand (waitpid_fn, 3, argv); |
470 | if (value_as_long (retv) < 0) | |
471 | goto out; | |
7a298875 HZ |
472 | |
473 | ret = 0; | |
474 | ||
475 | out: | |
e17c9e56 | 476 | do_cleanups (old_cleanup); |
7a298875 HZ |
477 | return ret; |
478 | } | |
479 | ||
ac264b3b MS |
480 | /* Fork list <-> user interface. */ |
481 | ||
482 | static void | |
3cb5bea9 | 483 | delete_checkpoint_command (char *args, int from_tty) |
ac264b3b | 484 | { |
e17c9e56 | 485 | ptid_t ptid, pptid; |
7a298875 | 486 | struct fork_info *fi; |
ac264b3b MS |
487 | |
488 | if (!args || !*args) | |
2277426b | 489 | error (_("Requires argument (checkpoint id to delete)")); |
ac264b3b MS |
490 | |
491 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
492 | if (ptid_equal (ptid, minus_one_ptid)) | |
2277426b | 493 | error (_("No such checkpoint id, %s"), args); |
ac264b3b MS |
494 | |
495 | if (ptid_equal (ptid, inferior_ptid)) | |
3cb5bea9 PA |
496 | error (_("\ |
497 | Please switch to another checkpoint before deleting the current one")); | |
ac264b3b | 498 | |
1dce6535 | 499 | if (ptrace (PTRACE_KILL, PIDGET (ptid), 0, 0)) |
54ba13f7 | 500 | error (_("Unable to kill pid %s"), target_pid_to_str (ptid)); |
ac264b3b | 501 | |
7a298875 HZ |
502 | fi = find_fork_ptid (ptid); |
503 | gdb_assert (fi); | |
e17c9e56 | 504 | pptid = fi->parent_ptid; |
7a298875 | 505 | |
ac264b3b MS |
506 | if (from_tty) |
507 | printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid)); | |
508 | ||
509 | delete_fork (ptid); | |
7a298875 HZ |
510 | |
511 | /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint | |
512 | list, waitpid the ptid. | |
513 | If fi->parent_ptid is a part of lwp and it is stoped, waitpid the | |
514 | ptid. */ | |
e17c9e56 HZ |
515 | if ((!find_thread_ptid (pptid) && find_fork_ptid (pptid)) |
516 | || (find_thread_ptid (pptid) && is_stopped (pptid))) | |
7a298875 | 517 | { |
e17c9e56 | 518 | if (inferior_call_waitpid (pptid, PIDGET (ptid))) |
7a298875 HZ |
519 | warning (_("Unable to wait pid %s"), target_pid_to_str (ptid)); |
520 | } | |
ac264b3b MS |
521 | } |
522 | ||
523 | static void | |
3cb5bea9 | 524 | detach_checkpoint_command (char *args, int from_tty) |
ac264b3b MS |
525 | { |
526 | ptid_t ptid; | |
527 | ||
528 | if (!args || !*args) | |
2277426b | 529 | error (_("Requires argument (checkpoint id to detach)")); |
ac264b3b MS |
530 | |
531 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
532 | if (ptid_equal (ptid, minus_one_ptid)) | |
2277426b | 533 | error (_("No such checkpoint id, %s"), args); |
ac264b3b MS |
534 | |
535 | if (ptid_equal (ptid, inferior_ptid)) | |
2277426b PA |
536 | error (_("\ |
537 | Please switch to another checkpoint before detaching the current one")); | |
ac264b3b | 538 | |
1dce6535 | 539 | if (ptrace (PTRACE_DETACH, PIDGET (ptid), 0, 0)) |
ac264b3b MS |
540 | error (_("Unable to detach %s"), target_pid_to_str (ptid)); |
541 | ||
542 | if (from_tty) | |
543 | printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid)); | |
544 | ||
545 | delete_fork (ptid); | |
546 | } | |
547 | ||
3cb5bea9 | 548 | /* Print information about currently known checkpoints. */ |
ac264b3b MS |
549 | |
550 | static void | |
3cb5bea9 | 551 | info_checkpoints_command (char *arg, int from_tty) |
ac264b3b | 552 | { |
5af949e3 | 553 | struct gdbarch *gdbarch = get_current_arch (); |
ac264b3b | 554 | struct symtab_and_line sal; |
ac264b3b | 555 | struct fork_info *fp; |
ac264b3b | 556 | ULONGEST pc; |
b8db102d MS |
557 | int requested = -1; |
558 | struct fork_info *printed = NULL; | |
559 | ||
560 | if (arg && *arg) | |
561 | requested = (int) parse_and_eval_long (arg); | |
ac264b3b MS |
562 | |
563 | for (fp = fork_list; fp; fp = fp->next) | |
564 | { | |
b8db102d MS |
565 | if (requested > 0 && fp->num != requested) |
566 | continue; | |
567 | ||
568 | printed = fp; | |
ac264b3b MS |
569 | if (ptid_equal (fp->ptid, inferior_ptid)) |
570 | { | |
571 | printf_filtered ("* "); | |
fb14de7b | 572 | pc = regcache_read_pc (get_current_regcache ()); |
ac264b3b MS |
573 | } |
574 | else | |
575 | { | |
576 | printf_filtered (" "); | |
2277426b | 577 | pc = regcache_read_pc (fp->savedregs); |
ac264b3b MS |
578 | } |
579 | printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid)); | |
580 | if (fp->num == 0) | |
581 | printf_filtered (_(" (main process)")); | |
582 | printf_filtered (_(" at ")); | |
5af949e3 | 583 | fputs_filtered (paddress (gdbarch, pc), gdb_stdout); |
ac264b3b MS |
584 | |
585 | sal = find_pc_line (pc, 0); | |
586 | if (sal.symtab) | |
587 | { | |
588 | char *tmp = strrchr (sal.symtab->filename, '/'); | |
589 | ||
590 | if (tmp) | |
591 | printf_filtered (_(", file %s"), tmp + 1); | |
592 | else | |
593 | printf_filtered (_(", file %s"), sal.symtab->filename); | |
594 | } | |
595 | if (sal.line) | |
596 | printf_filtered (_(", line %d"), sal.line); | |
597 | if (!sal.symtab && !sal.line) | |
598 | { | |
599 | struct minimal_symbol *msym; | |
600 | ||
601 | msym = lookup_minimal_symbol_by_pc (pc); | |
602 | if (msym) | |
603 | printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym)); | |
604 | } | |
605 | ||
606 | putchar_filtered ('\n'); | |
607 | } | |
b8db102d MS |
608 | if (printed == NULL) |
609 | { | |
610 | if (requested > 0) | |
2277426b | 611 | printf_filtered (_("No checkpoint number %d.\n"), requested); |
b8db102d | 612 | else |
2277426b | 613 | printf_filtered (_("No checkpoints.\n")); |
b8db102d | 614 | } |
ac264b3b MS |
615 | } |
616 | ||
2277426b PA |
617 | /* The PID of the process we're checkpointing. */ |
618 | static int checkpointing_pid = 0; | |
ac264b3b | 619 | |
2277426b PA |
620 | int |
621 | linux_fork_checkpointing_p (int pid) | |
ac264b3b | 622 | { |
2277426b | 623 | return (checkpointing_pid == pid); |
ac264b3b MS |
624 | } |
625 | ||
626 | static void | |
627 | checkpoint_command (char *args, int from_tty) | |
628 | { | |
3e3b026f UW |
629 | struct objfile *fork_objf; |
630 | struct gdbarch *gdbarch; | |
ac264b3b MS |
631 | struct target_waitstatus last_target_waitstatus; |
632 | ptid_t last_target_ptid; | |
633 | struct value *fork_fn = NULL, *ret; | |
634 | struct fork_info *fp; | |
635 | pid_t retpid; | |
636 | struct cleanup *old_chain; | |
74960c60 | 637 | |
ac264b3b MS |
638 | /* Make the inferior fork, record its (and gdb's) state. */ |
639 | ||
640 | if (lookup_minimal_symbol ("fork", NULL, NULL) != NULL) | |
3e3b026f | 641 | fork_fn = find_function_in_inferior ("fork", &fork_objf); |
ac264b3b MS |
642 | if (!fork_fn) |
643 | if (lookup_minimal_symbol ("_fork", NULL, NULL) != NULL) | |
3e3b026f | 644 | fork_fn = find_function_in_inferior ("fork", &fork_objf); |
ac264b3b MS |
645 | if (!fork_fn) |
646 | error (_("checkpoint: can't find fork function in inferior.")); | |
647 | ||
3e3b026f UW |
648 | gdbarch = get_objfile_arch (fork_objf); |
649 | ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); | |
2277426b PA |
650 | |
651 | /* Tell linux-nat.c that we're checkpointing this inferior. */ | |
652 | old_chain = make_cleanup_restore_integer (&checkpointing_pid); | |
653 | checkpointing_pid = PIDGET (inferior_ptid); | |
654 | ||
ac264b3b MS |
655 | ret = call_function_by_hand (fork_fn, 0, &ret); |
656 | do_cleanups (old_chain); | |
657 | if (!ret) /* Probably can't happen. */ | |
658 | error (_("checkpoint: call_function_by_hand returned null.")); | |
659 | ||
660 | retpid = value_as_long (ret); | |
661 | get_last_target_status (&last_target_ptid, &last_target_waitstatus); | |
662 | if (from_tty) | |
663 | { | |
664 | int parent_pid; | |
665 | ||
3cb5bea9 | 666 | printf_filtered (_("checkpoint: fork returned pid %ld.\n"), |
ac264b3b MS |
667 | (long) retpid); |
668 | if (info_verbose) | |
669 | { | |
670 | parent_pid = ptid_get_lwp (last_target_ptid); | |
671 | if (parent_pid == 0) | |
672 | parent_pid = ptid_get_pid (last_target_ptid); | |
3cb5bea9 | 673 | printf_filtered (_(" gdb says parent = %ld.\n"), |
ac264b3b MS |
674 | (long) parent_pid); |
675 | } | |
676 | } | |
677 | ||
678 | fp = find_fork_pid (retpid); | |
679 | if (!fp) | |
680 | error (_("Failed to find new fork")); | |
681 | fork_save_infrun_state (fp, 1); | |
7a298875 | 682 | fp->parent_ptid = last_target_ptid; |
ac264b3b MS |
683 | } |
684 | ||
685 | static void | |
686 | linux_fork_context (struct fork_info *newfp, int from_tty) | |
687 | { | |
688 | /* Now we attempt to switch processes. */ | |
0d14fc63 | 689 | struct fork_info *oldfp; |
ac264b3b | 690 | |
0d14fc63 | 691 | gdb_assert (newfp != NULL); |
ac264b3b | 692 | |
0d14fc63 PA |
693 | oldfp = find_fork_ptid (inferior_ptid); |
694 | gdb_assert (oldfp != NULL); | |
ac264b3b MS |
695 | |
696 | fork_save_infrun_state (oldfp, 1); | |
74960c60 | 697 | remove_breakpoints (); |
ac264b3b | 698 | fork_load_infrun_state (newfp); |
74960c60 | 699 | insert_breakpoints (); |
ac264b3b | 700 | |
3cb5bea9 | 701 | printf_filtered (_("Switching to %s\n"), |
ac264b3b MS |
702 | target_pid_to_str (inferior_ptid)); |
703 | ||
704 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); | |
705 | } | |
706 | ||
2277426b | 707 | /* Switch inferior process (checkpoint) context, by checkpoint id. */ |
ac264b3b MS |
708 | static void |
709 | restart_command (char *args, int from_tty) | |
710 | { | |
711 | struct fork_info *fp; | |
712 | ||
713 | if (!args || !*args) | |
714 | error (_("Requires argument (checkpoint id to restart)")); | |
715 | ||
716 | if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) | |
717 | error (_("Not found: checkpoint id %s"), args); | |
718 | ||
719 | linux_fork_context (fp, from_tty); | |
720 | } | |
721 | ||
722 | void | |
723 | _initialize_linux_fork (void) | |
724 | { | |
725 | init_fork_list (); | |
726 | ||
ac264b3b MS |
727 | /* Checkpoint command: create a fork of the inferior process |
728 | and set it aside for later debugging. */ | |
729 | ||
730 | add_com ("checkpoint", class_obscure, checkpoint_command, _("\ | |
731 | Fork a duplicate process (experimental).")); | |
732 | ||
2277426b PA |
733 | /* Restart command: restore the context of a specified checkpoint |
734 | process. */ | |
ac264b3b MS |
735 | |
736 | add_com ("restart", class_obscure, restart_command, _("\ | |
737 | restart <n>: restore program context from a checkpoint.\n\ | |
738 | Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'.")); | |
739 | ||
b8db102d | 740 | /* Delete checkpoint command: kill the process and remove it from |
3cb5bea9 | 741 | the fork list. */ |
ac264b3b | 742 | |
3cb5bea9 | 743 | add_cmd ("checkpoint", class_obscure, delete_checkpoint_command, _("\ |
2277426b | 744 | Delete a checkpoint (experimental)."), |
b8db102d | 745 | &deletelist); |
ac264b3b | 746 | |
3cb5bea9 | 747 | /* Detach checkpoint command: release the process to run independently, |
ac264b3b MS |
748 | and remove it from the fork list. */ |
749 | ||
3cb5bea9 | 750 | add_cmd ("checkpoint", class_obscure, detach_checkpoint_command, _("\ |
2277426b | 751 | Detach from a checkpoint (experimental)."), |
f73adfeb | 752 | &detachlist); |
ac264b3b | 753 | |
3cb5bea9 | 754 | /* Info checkpoints command: list all forks/checkpoints |
ac264b3b MS |
755 | currently under gdb's control. */ |
756 | ||
3cb5bea9 | 757 | add_info ("checkpoints", info_checkpoints_command, |
2277426b | 758 | _("IDs of currently known checkpoints.")); |
ac264b3b | 759 | } |