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