Commit | Line | Data |
---|---|---|
ac264b3b MS |
1 | /* GNU/Linux native-dependent code for debugging multiple forks. |
2 | ||
e2882c85 | 3 | Copyright (C) 2005-2018 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 | ||
ac264b3b MS |
41 | /* Fork list data structure: */ |
42 | struct fork_info | |
43 | { | |
44 | struct fork_info *next; | |
45 | ptid_t ptid; | |
7a298875 | 46 | ptid_t parent_ptid; |
1777feb0 | 47 | int num; /* Convenient handle (GDB fork id). */ |
daf6667d | 48 | readonly_detached_regcache *savedregs; /* Convenient for info fork, saves |
ac264b3b | 49 | having to actually switch contexts. */ |
daf6667d | 50 | CORE_ADDR pc; |
ac264b3b | 51 | int clobber_regs; /* True if we should restore saved regs. */ |
ac264b3b MS |
52 | off_t *filepos; /* Set of open file descriptors' offsets. */ |
53 | int maxfd; | |
54 | }; | |
55 | ||
56 | /* Fork list methods: */ | |
57 | ||
3cb5bea9 | 58 | int |
ac264b3b MS |
59 | forks_exist_p (void) |
60 | { | |
61 | return (fork_list != NULL); | |
62 | } | |
63 | ||
2f341b6e PA |
64 | /* Return the last fork in the list. */ |
65 | ||
66 | static struct fork_info * | |
67 | find_last_fork (void) | |
68 | { | |
69 | struct fork_info *last; | |
70 | ||
71 | if (fork_list == NULL) | |
72 | return NULL; | |
73 | ||
74 | for (last = fork_list; last->next != NULL; last = last->next) | |
75 | ; | |
76 | return last; | |
77 | } | |
78 | ||
3cb5bea9 | 79 | /* Add a fork to the internal fork list. */ |
ac264b3b | 80 | |
3cb5bea9 | 81 | struct fork_info * |
ac264b3b MS |
82 | add_fork (pid_t pid) |
83 | { | |
84 | struct fork_info *fp; | |
85 | ||
e99b03dc | 86 | if (fork_list == NULL && pid != inferior_ptid.pid ()) |
ac264b3b MS |
87 | { |
88 | /* Special case -- if this is the first fork in the list | |
89 | (the list is hitherto empty), and if this new fork is | |
90 | NOT the current inferior_ptid, then add inferior_ptid | |
91 | first, as a special zeroeth fork id. */ | |
92 | highest_fork_num = -1; | |
e99b03dc | 93 | add_fork (inferior_ptid.pid ()); /* safe recursion */ |
ac264b3b MS |
94 | } |
95 | ||
41bf6aca | 96 | fp = XCNEW (struct fork_info); |
fd79271b | 97 | fp->ptid = ptid_t (pid, pid, 0); |
ac264b3b | 98 | fp->num = ++highest_fork_num; |
2f341b6e PA |
99 | |
100 | if (fork_list == NULL) | |
101 | fork_list = fp; | |
102 | else | |
103 | { | |
104 | struct fork_info *last = find_last_fork (); | |
105 | ||
106 | last->next = fp; | |
107 | } | |
108 | ||
ac264b3b MS |
109 | return fp; |
110 | } | |
111 | ||
112 | static void | |
113 | free_fork (struct fork_info *fp) | |
114 | { | |
115 | /* Notes on step-resume breakpoints: since this is a concern for | |
116 | threads, let's convince ourselves that it's not a concern for | |
117 | forks. There are two ways for a fork_info to be created. First, | |
118 | by the checkpoint command, in which case we're at a gdb prompt | |
119 | and there can't be any step-resume breakpoint. Second, by a fork | |
120 | in the user program, in which case we *may* have stepped into the | |
121 | fork call, but regardless of whether we follow the parent or the | |
122 | child, we will return to the same place and the step-resume | |
123 | breakpoint, if any, will take care of itself as usual. And | |
124 | unlike threads, we do not save a private copy of the step-resume | |
125 | breakpoint -- so we're OK. */ | |
126 | ||
127 | if (fp) | |
128 | { | |
129 | if (fp->savedregs) | |
c0e383c6 | 130 | delete fp->savedregs; |
ac264b3b MS |
131 | if (fp->filepos) |
132 | xfree (fp->filepos); | |
133 | xfree (fp); | |
134 | } | |
135 | } | |
136 | ||
137 | static void | |
138 | delete_fork (ptid_t ptid) | |
139 | { | |
140 | struct fork_info *fp, *fpprev; | |
141 | ||
142 | fpprev = NULL; | |
143 | ||
e99b03dc | 144 | linux_target->low_forget_process (ptid.pid ()); |
26cb8b7c | 145 | |
ac264b3b | 146 | for (fp = fork_list; fp; fpprev = fp, fp = fp->next) |
d7e15655 | 147 | if (fp->ptid == ptid) |
ac264b3b MS |
148 | break; |
149 | ||
150 | if (!fp) | |
151 | return; | |
152 | ||
153 | if (fpprev) | |
154 | fpprev->next = fp->next; | |
155 | else | |
156 | fork_list = fp->next; | |
157 | ||
158 | free_fork (fp); | |
159 | ||
3cb5bea9 | 160 | /* Special case: if there is now only one process in the list, |
ac264b3b MS |
161 | and if it is (hopefully!) the current inferior_ptid, then |
162 | remove it, leaving the list empty -- we're now down to the | |
163 | default case of debugging a single process. */ | |
164 | if (fork_list != NULL && fork_list->next == NULL && | |
d7e15655 | 165 | fork_list->ptid == inferior_ptid) |
ac264b3b MS |
166 | { |
167 | /* Last fork -- delete from list and handle as solo process | |
168 | (should be a safe recursion). */ | |
169 | delete_fork (inferior_ptid); | |
170 | } | |
171 | } | |
172 | ||
173 | /* Find a fork_info by matching PTID. */ | |
174 | static struct fork_info * | |
175 | find_fork_ptid (ptid_t ptid) | |
176 | { | |
177 | struct fork_info *fp; | |
178 | ||
179 | for (fp = fork_list; fp; fp = fp->next) | |
d7e15655 | 180 | if (fp->ptid == ptid) |
ac264b3b MS |
181 | return fp; |
182 | ||
183 | return NULL; | |
184 | } | |
185 | ||
186 | /* Find a fork_info by matching ID. */ | |
187 | static struct fork_info * | |
188 | find_fork_id (int num) | |
189 | { | |
190 | struct fork_info *fp; | |
191 | ||
192 | for (fp = fork_list; fp; fp = fp->next) | |
193 | if (fp->num == num) | |
194 | return fp; | |
195 | ||
196 | return NULL; | |
197 | } | |
198 | ||
199 | /* Find a fork_info by matching pid. */ | |
200 | extern struct fork_info * | |
201 | find_fork_pid (pid_t pid) | |
202 | { | |
203 | struct fork_info *fp; | |
204 | ||
205 | for (fp = fork_list; fp; fp = fp->next) | |
e99b03dc | 206 | if (pid == fp->ptid.pid ()) |
ac264b3b MS |
207 | return fp; |
208 | ||
209 | return NULL; | |
210 | } | |
211 | ||
212 | static ptid_t | |
213 | fork_id_to_ptid (int num) | |
214 | { | |
215 | struct fork_info *fork = find_fork_id (num); | |
216 | if (fork) | |
217 | return fork->ptid; | |
218 | else | |
f2907e49 | 219 | return ptid_t (-1); |
ac264b3b MS |
220 | } |
221 | ||
222 | static void | |
223 | init_fork_list (void) | |
224 | { | |
225 | struct fork_info *fp, *fpnext; | |
226 | ||
227 | if (!fork_list) | |
228 | return; | |
229 | ||
230 | for (fp = fork_list; fp; fp = fpnext) | |
231 | { | |
232 | fpnext = fp->next; | |
233 | free_fork (fp); | |
234 | } | |
235 | ||
236 | fork_list = NULL; | |
237 | } | |
238 | ||
239 | /* Fork list <-> gdb interface. */ | |
240 | ||
3cb5bea9 | 241 | /* Utility function for fork_load/fork_save. |
ac264b3b MS |
242 | Calls lseek in the (current) inferior process. */ |
243 | ||
244 | static off_t | |
245 | call_lseek (int fd, off_t offset, int whence) | |
246 | { | |
247 | char exp[80]; | |
248 | ||
7022349d | 249 | snprintf (&exp[0], sizeof (exp), "(long) lseek (%d, %ld, %d)", |
ac264b3b MS |
250 | fd, (long) offset, whence); |
251 | return (off_t) parse_and_eval_long (&exp[0]); | |
252 | } | |
253 | ||
254 | /* Load infrun state for the fork PTID. */ | |
255 | ||
256 | static void | |
257 | fork_load_infrun_state (struct fork_info *fp) | |
258 | { | |
259 | extern void nullify_last_target_wait_ptid (); | |
260 | int i; | |
261 | ||
2277426b | 262 | linux_nat_switch_fork (fp->ptid); |
f973ed9c | 263 | |
ac264b3b | 264 | if (fp->savedregs && fp->clobber_regs) |
fc5b8736 | 265 | get_current_regcache ()->restore (fp->savedregs); |
ac264b3b | 266 | |
791b663b DJ |
267 | registers_changed (); |
268 | reinit_frame_cache (); | |
269 | ||
f2ffa92b PA |
270 | inferior_thread ()->suspend.stop_pc |
271 | = regcache_read_pc (get_current_regcache ()); | |
ac264b3b MS |
272 | nullify_last_target_wait_ptid (); |
273 | ||
274 | /* Now restore the file positions of open file descriptors. */ | |
275 | if (fp->filepos) | |
276 | { | |
277 | for (i = 0; i <= fp->maxfd; i++) | |
278 | if (fp->filepos[i] != (off_t) -1) | |
279 | call_lseek (i, fp->filepos[i], SEEK_SET); | |
280 | /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because | |
281 | this is native-only. If it ever has to be cross, we'll have | |
282 | to rethink this. */ | |
283 | } | |
284 | } | |
285 | ||
286 | /* Save infrun state for the fork PTID. | |
287 | Exported for use by linux child_follow_fork. */ | |
288 | ||
2277426b | 289 | static void |
ac264b3b MS |
290 | fork_save_infrun_state (struct fork_info *fp, int clobber_regs) |
291 | { | |
d8d2a3ee | 292 | char path[PATH_MAX]; |
ac264b3b MS |
293 | struct dirent *de; |
294 | DIR *d; | |
295 | ||
296 | if (fp->savedregs) | |
c0e383c6 | 297 | delete fp->savedregs; |
ac264b3b | 298 | |
daf6667d YQ |
299 | fp->savedregs = new readonly_detached_regcache (*get_current_regcache ()); |
300 | fp->pc = regcache_read_pc (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 | 307 | snprintf (path, PATH_MAX, "/proc/%ld/fd", |
e99b03dc | 308 | (long) fp->ptid.pid ()); |
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 | { | |
e99b03dc | 358 | pid = fp->ptid.pid (); |
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. */ | |
e99b03dc | 386 | waitpid (inferior_ptid.pid (), &status, 0); |
ac264b3b MS |
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 |
6bd6f3b6 | 413 | linux_fork_detach (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 | ||
e99b03dc | 419 | if (ptrace (PTRACE_DETACH, inferior_ptid.pid (), 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 | ||
d7e15655 | 465 | if (pptid != inferior_ptid) |
7a298875 HZ |
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 | ||
7022349d | 495 | retv = call_function_by_hand (waitpid_fn, NULL, 3, argv); |
e17c9e56 HZ |
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 | |
5b64bf74 | 509 | delete_checkpoint_command (const 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)); | |
d7e15655 | 518 | if (ptid == minus_one_ptid) |
2277426b | 519 | error (_("No such checkpoint id, %s"), args); |
ac264b3b | 520 | |
d7e15655 | 521 | if (ptid == inferior_ptid) |
3cb5bea9 PA |
522 | error (_("\ |
523 | Please switch to another checkpoint before deleting the current one")); | |
ac264b3b | 524 | |
e99b03dc | 525 | if (ptrace (PTRACE_KILL, ptid.pid (), 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. | |
00431a78 | 539 | If fi->parent_ptid is a part of lwp and it is stopped, waitpid the |
7a298875 | 540 | ptid. */ |
00431a78 PA |
541 | thread_info *parent = find_thread_ptid (pptid); |
542 | if ((parent == NULL && find_fork_ptid (pptid)) | |
543 | || (parent != NULL && parent->state == THREAD_STOPPED)) | |
7a298875 | 544 | { |
e99b03dc | 545 | if (inferior_call_waitpid (pptid, ptid.pid ())) |
7a298875 HZ |
546 | warning (_("Unable to wait pid %s"), target_pid_to_str (ptid)); |
547 | } | |
ac264b3b MS |
548 | } |
549 | ||
550 | static void | |
5b64bf74 | 551 | detach_checkpoint_command (const char *args, int from_tty) |
ac264b3b MS |
552 | { |
553 | ptid_t ptid; | |
554 | ||
555 | if (!args || !*args) | |
2277426b | 556 | error (_("Requires argument (checkpoint id to detach)")); |
ac264b3b MS |
557 | |
558 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
d7e15655 | 559 | if (ptid == minus_one_ptid) |
2277426b | 560 | error (_("No such checkpoint id, %s"), args); |
ac264b3b | 561 | |
d7e15655 | 562 | if (ptid == inferior_ptid) |
2277426b PA |
563 | error (_("\ |
564 | Please switch to another checkpoint before detaching the current one")); | |
ac264b3b | 565 | |
e99b03dc | 566 | if (ptrace (PTRACE_DETACH, ptid.pid (), 0, 0)) |
ac264b3b MS |
567 | error (_("Unable to detach %s"), target_pid_to_str (ptid)); |
568 | ||
569 | if (from_tty) | |
570 | printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid)); | |
571 | ||
572 | delete_fork (ptid); | |
573 | } | |
574 | ||
3cb5bea9 | 575 | /* Print information about currently known checkpoints. */ |
ac264b3b MS |
576 | |
577 | static void | |
1d12d88f | 578 | info_checkpoints_command (const char *arg, int from_tty) |
ac264b3b | 579 | { |
5af949e3 | 580 | struct gdbarch *gdbarch = get_current_arch (); |
ac264b3b | 581 | struct symtab_and_line sal; |
ac264b3b | 582 | struct fork_info *fp; |
ac264b3b | 583 | ULONGEST pc; |
b8db102d MS |
584 | int requested = -1; |
585 | struct fork_info *printed = NULL; | |
586 | ||
587 | if (arg && *arg) | |
588 | requested = (int) parse_and_eval_long (arg); | |
ac264b3b MS |
589 | |
590 | for (fp = fork_list; fp; fp = fp->next) | |
591 | { | |
b8db102d MS |
592 | if (requested > 0 && fp->num != requested) |
593 | continue; | |
594 | ||
595 | printed = fp; | |
d7e15655 | 596 | if (fp->ptid == inferior_ptid) |
daf6667d | 597 | printf_filtered ("* "); |
ac264b3b | 598 | else |
daf6667d YQ |
599 | printf_filtered (" "); |
600 | ||
601 | pc = fp->pc; | |
ac264b3b MS |
602 | printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid)); |
603 | if (fp->num == 0) | |
604 | printf_filtered (_(" (main process)")); | |
605 | printf_filtered (_(" at ")); | |
5af949e3 | 606 | fputs_filtered (paddress (gdbarch, pc), gdb_stdout); |
ac264b3b MS |
607 | |
608 | sal = find_pc_line (pc, 0); | |
609 | if (sal.symtab) | |
05cba821 JK |
610 | printf_filtered (_(", file %s"), |
611 | symtab_to_filename_for_display (sal.symtab)); | |
ac264b3b MS |
612 | if (sal.line) |
613 | printf_filtered (_(", line %d"), sal.line); | |
614 | if (!sal.symtab && !sal.line) | |
615 | { | |
7cbd4a93 | 616 | struct bound_minimal_symbol msym; |
ac264b3b MS |
617 | |
618 | msym = lookup_minimal_symbol_by_pc (pc); | |
7cbd4a93 | 619 | if (msym.minsym) |
efd66ac6 | 620 | printf_filtered (", <%s>", MSYMBOL_LINKAGE_NAME (msym.minsym)); |
ac264b3b MS |
621 | } |
622 | ||
623 | putchar_filtered ('\n'); | |
624 | } | |
b8db102d MS |
625 | if (printed == NULL) |
626 | { | |
627 | if (requested > 0) | |
2277426b | 628 | printf_filtered (_("No checkpoint number %d.\n"), requested); |
b8db102d | 629 | else |
2277426b | 630 | printf_filtered (_("No checkpoints.\n")); |
b8db102d | 631 | } |
ac264b3b MS |
632 | } |
633 | ||
2277426b PA |
634 | /* The PID of the process we're checkpointing. */ |
635 | static int checkpointing_pid = 0; | |
ac264b3b | 636 | |
2277426b PA |
637 | int |
638 | linux_fork_checkpointing_p (int pid) | |
ac264b3b | 639 | { |
2277426b | 640 | return (checkpointing_pid == pid); |
ac264b3b MS |
641 | } |
642 | ||
92f6badc KP |
643 | /* Callback for iterate over threads. Used to check whether |
644 | the current inferior is multi-threaded. Returns true as soon | |
645 | as it sees the second thread of the current inferior. */ | |
646 | ||
647 | static int | |
648 | inf_has_multiple_thread_cb (struct thread_info *tp, void *data) | |
649 | { | |
650 | int *count_p = (int *) data; | |
651 | ||
e99b03dc | 652 | if (current_inferior ()->pid == tp->ptid.pid ()) |
92f6badc KP |
653 | (*count_p)++; |
654 | ||
655 | /* Stop the iteration if multiple threads have been detected. */ | |
656 | return *count_p > 1; | |
657 | } | |
658 | ||
659 | /* Return true if the current inferior is multi-threaded. */ | |
660 | ||
661 | static int | |
662 | inf_has_multiple_threads (void) | |
663 | { | |
664 | int count = 0; | |
665 | ||
666 | iterate_over_threads (inf_has_multiple_thread_cb, &count); | |
667 | return (count > 1); | |
668 | } | |
669 | ||
ac264b3b | 670 | static void |
0b39b52e | 671 | checkpoint_command (const char *args, int from_tty) |
ac264b3b | 672 | { |
3e3b026f UW |
673 | struct objfile *fork_objf; |
674 | struct gdbarch *gdbarch; | |
ac264b3b MS |
675 | struct target_waitstatus last_target_waitstatus; |
676 | ptid_t last_target_ptid; | |
677 | struct value *fork_fn = NULL, *ret; | |
678 | struct fork_info *fp; | |
679 | pid_t retpid; | |
74960c60 | 680 | |
92f6badc KP |
681 | if (!target_has_execution) |
682 | error (_("The program is not being run.")); | |
683 | ||
684 | /* Ensure that the inferior is not multithreaded. */ | |
685 | update_thread_list (); | |
686 | if (inf_has_multiple_threads ()) | |
687 | error (_("checkpoint: can't checkpoint multiple threads.")); | |
688 | ||
ac264b3b MS |
689 | /* Make the inferior fork, record its (and gdb's) state. */ |
690 | ||
3b7344d5 | 691 | if (lookup_minimal_symbol ("fork", NULL, NULL).minsym != NULL) |
3e3b026f | 692 | fork_fn = find_function_in_inferior ("fork", &fork_objf); |
ac264b3b | 693 | if (!fork_fn) |
3b7344d5 | 694 | if (lookup_minimal_symbol ("_fork", NULL, NULL).minsym != NULL) |
3e3b026f | 695 | fork_fn = find_function_in_inferior ("fork", &fork_objf); |
ac264b3b MS |
696 | if (!fork_fn) |
697 | error (_("checkpoint: can't find fork function in inferior.")); | |
698 | ||
3e3b026f UW |
699 | gdbarch = get_objfile_arch (fork_objf); |
700 | ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); | |
2277426b PA |
701 | |
702 | /* Tell linux-nat.c that we're checkpointing this inferior. */ | |
b7b633e9 TT |
703 | { |
704 | scoped_restore save_pid | |
e99b03dc | 705 | = make_scoped_restore (&checkpointing_pid, inferior_ptid.pid ()); |
b7b633e9 | 706 | |
7022349d | 707 | ret = call_function_by_hand (fork_fn, NULL, 0, &ret); |
b7b633e9 | 708 | } |
2277426b | 709 | |
ac264b3b MS |
710 | if (!ret) /* Probably can't happen. */ |
711 | error (_("checkpoint: call_function_by_hand returned null.")); | |
712 | ||
713 | retpid = value_as_long (ret); | |
714 | get_last_target_status (&last_target_ptid, &last_target_waitstatus); | |
6f9d33d8 PP |
715 | |
716 | fp = find_fork_pid (retpid); | |
717 | ||
ac264b3b MS |
718 | if (from_tty) |
719 | { | |
720 | int parent_pid; | |
721 | ||
6f9d33d8 PP |
722 | printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"), |
723 | fp != NULL ? fp->num : -1, (long) retpid); | |
ac264b3b MS |
724 | if (info_verbose) |
725 | { | |
e38504b3 | 726 | parent_pid = last_target_ptid.lwp (); |
ac264b3b | 727 | if (parent_pid == 0) |
e99b03dc | 728 | parent_pid = last_target_ptid.pid (); |
3cb5bea9 | 729 | printf_filtered (_(" gdb says parent = %ld.\n"), |
ac264b3b MS |
730 | (long) parent_pid); |
731 | } | |
732 | } | |
733 | ||
ac264b3b MS |
734 | if (!fp) |
735 | error (_("Failed to find new fork")); | |
736 | fork_save_infrun_state (fp, 1); | |
7a298875 | 737 | fp->parent_ptid = last_target_ptid; |
ac264b3b MS |
738 | } |
739 | ||
740 | static void | |
741 | linux_fork_context (struct fork_info *newfp, int from_tty) | |
742 | { | |
743 | /* Now we attempt to switch processes. */ | |
0d14fc63 | 744 | struct fork_info *oldfp; |
ac264b3b | 745 | |
0d14fc63 | 746 | gdb_assert (newfp != NULL); |
ac264b3b | 747 | |
0d14fc63 PA |
748 | oldfp = find_fork_ptid (inferior_ptid); |
749 | gdb_assert (oldfp != NULL); | |
ac264b3b MS |
750 | |
751 | fork_save_infrun_state (oldfp, 1); | |
74960c60 | 752 | remove_breakpoints (); |
ac264b3b | 753 | fork_load_infrun_state (newfp); |
74960c60 | 754 | insert_breakpoints (); |
ac264b3b | 755 | |
3cb5bea9 | 756 | printf_filtered (_("Switching to %s\n"), |
ac264b3b MS |
757 | target_pid_to_str (inferior_ptid)); |
758 | ||
08d72866 | 759 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
ac264b3b MS |
760 | } |
761 | ||
2277426b | 762 | /* Switch inferior process (checkpoint) context, by checkpoint id. */ |
ac264b3b | 763 | static void |
0b39b52e | 764 | restart_command (const char *args, int from_tty) |
ac264b3b MS |
765 | { |
766 | struct fork_info *fp; | |
767 | ||
768 | if (!args || !*args) | |
769 | error (_("Requires argument (checkpoint id to restart)")); | |
770 | ||
771 | if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) | |
772 | error (_("Not found: checkpoint id %s"), args); | |
773 | ||
774 | linux_fork_context (fp, from_tty); | |
775 | } | |
776 | ||
777 | void | |
778 | _initialize_linux_fork (void) | |
779 | { | |
780 | init_fork_list (); | |
781 | ||
ac264b3b MS |
782 | /* Checkpoint command: create a fork of the inferior process |
783 | and set it aside for later debugging. */ | |
784 | ||
785 | add_com ("checkpoint", class_obscure, checkpoint_command, _("\ | |
786 | Fork a duplicate process (experimental).")); | |
787 | ||
2277426b PA |
788 | /* Restart command: restore the context of a specified checkpoint |
789 | process. */ | |
ac264b3b MS |
790 | |
791 | add_com ("restart", class_obscure, restart_command, _("\ | |
c8a15b78 TT |
792 | restart N: restore program context from a checkpoint.\n\ |
793 | Argument N is checkpoint ID, as displayed by 'info checkpoints'.")); | |
ac264b3b | 794 | |
b8db102d | 795 | /* Delete checkpoint command: kill the process and remove it from |
3cb5bea9 | 796 | the fork list. */ |
ac264b3b | 797 | |
3cb5bea9 | 798 | add_cmd ("checkpoint", class_obscure, delete_checkpoint_command, _("\ |
2277426b | 799 | Delete a checkpoint (experimental)."), |
b8db102d | 800 | &deletelist); |
ac264b3b | 801 | |
3cb5bea9 | 802 | /* Detach checkpoint command: release the process to run independently, |
ac264b3b MS |
803 | and remove it from the fork list. */ |
804 | ||
3cb5bea9 | 805 | add_cmd ("checkpoint", class_obscure, detach_checkpoint_command, _("\ |
2277426b | 806 | Detach from a checkpoint (experimental)."), |
f73adfeb | 807 | &detachlist); |
ac264b3b | 808 | |
3cb5bea9 | 809 | /* Info checkpoints command: list all forks/checkpoints |
ac264b3b MS |
810 | currently under gdb's control. */ |
811 | ||
3cb5bea9 | 812 | add_info ("checkpoints", info_checkpoints_command, |
2277426b | 813 | _("IDs of currently known checkpoints.")); |
ac264b3b | 814 | } |