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