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 | ||
dfd4cc63 | 86 | if (fork_list == NULL && pid != ptid_get_pid (inferior_ptid)) |
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; | |
dfd4cc63 | 93 | add_fork (ptid_get_pid (inferior_ptid)); /* safe recursion */ |
ac264b3b MS |
94 | } |
95 | ||
41bf6aca | 96 | fp = XCNEW (struct fork_info); |
f973ed9c | 97 | fp->ptid = ptid_build (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 | ||
26cb8b7c PA |
144 | linux_nat_forget_process (ptid_get_pid (ptid)); |
145 | ||
ac264b3b MS |
146 | for (fp = fork_list; fp; fpprev = fp, fp = fp->next) |
147 | if (ptid_equal (fp->ptid, ptid)) | |
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 && | |
165 | ptid_equal (fork_list->ptid, inferior_ptid)) | |
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) | |
180 | if (ptid_equal (fp->ptid, ptid)) | |
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) | |
206 | if (pid == ptid_get_pid (fp->ptid)) | |
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 | |
219 | return pid_to_ptid (-1); | |
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 | ||
fb14de7b | 270 | stop_pc = regcache_read_pc (get_current_regcache ()); |
ac264b3b MS |
271 | nullify_last_target_wait_ptid (); |
272 | ||
273 | /* Now restore the file positions of open file descriptors. */ | |
274 | if (fp->filepos) | |
275 | { | |
276 | for (i = 0; i <= fp->maxfd; i++) | |
277 | if (fp->filepos[i] != (off_t) -1) | |
278 | call_lseek (i, fp->filepos[i], SEEK_SET); | |
279 | /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because | |
280 | this is native-only. If it ever has to be cross, we'll have | |
281 | to rethink this. */ | |
282 | } | |
283 | } | |
284 | ||
285 | /* Save infrun state for the fork PTID. | |
286 | Exported for use by linux child_follow_fork. */ | |
287 | ||
2277426b | 288 | static void |
ac264b3b MS |
289 | fork_save_infrun_state (struct fork_info *fp, int clobber_regs) |
290 | { | |
d8d2a3ee | 291 | char path[PATH_MAX]; |
ac264b3b MS |
292 | struct dirent *de; |
293 | DIR *d; | |
294 | ||
295 | if (fp->savedregs) | |
c0e383c6 | 296 | delete fp->savedregs; |
ac264b3b | 297 | |
daf6667d YQ |
298 | fp->savedregs = new readonly_detached_regcache (*get_current_regcache ()); |
299 | fp->pc = regcache_read_pc (get_current_regcache ()); | |
ac264b3b | 300 | fp->clobber_regs = clobber_regs; |
ac264b3b MS |
301 | |
302 | if (clobber_regs) | |
303 | { | |
304 | /* Now save the 'state' (file position) of all open file descriptors. | |
305 | Unfortunately fork does not take care of that for us... */ | |
dfd4cc63 LM |
306 | snprintf (path, PATH_MAX, "/proc/%ld/fd", |
307 | (long) ptid_get_pid (fp->ptid)); | |
ac264b3b MS |
308 | if ((d = opendir (path)) != NULL) |
309 | { | |
310 | long tmp; | |
311 | ||
312 | fp->maxfd = 0; | |
313 | while ((de = readdir (d)) != NULL) | |
314 | { | |
315 | /* Count open file descriptors (actually find highest | |
316 | numbered). */ | |
317 | tmp = strtol (&de->d_name[0], NULL, 10); | |
318 | if (fp->maxfd < tmp) | |
319 | fp->maxfd = tmp; | |
320 | } | |
321 | /* Allocate array of file positions. */ | |
224c3ddb | 322 | fp->filepos = XRESIZEVEC (off_t, fp->filepos, fp->maxfd + 1); |
ac264b3b MS |
323 | |
324 | /* Initialize to -1 (invalid). */ | |
325 | for (tmp = 0; tmp <= fp->maxfd; tmp++) | |
326 | fp->filepos[tmp] = -1; | |
327 | ||
328 | /* Now find actual file positions. */ | |
329 | rewinddir (d); | |
330 | while ((de = readdir (d)) != NULL) | |
331 | if (isdigit (de->d_name[0])) | |
332 | { | |
333 | tmp = strtol (&de->d_name[0], NULL, 10); | |
334 | fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR); | |
335 | } | |
336 | closedir (d); | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | /* Kill 'em all, let God sort 'em out... */ | |
342 | ||
3cb5bea9 | 343 | void |
ac264b3b MS |
344 | linux_fork_killall (void) |
345 | { | |
346 | /* Walk list and kill every pid. No need to treat the | |
347 | current inferior_ptid as special (we do not return a | |
348 | status for it) -- however any process may be a child | |
349 | or a parent, so may get a SIGCHLD from a previously | |
350 | killed child. Wait them all out. */ | |
56aac7e8 | 351 | struct fork_info *fp; |
ac264b3b MS |
352 | pid_t pid, ret; |
353 | int status; | |
354 | ||
56aac7e8 MS |
355 | for (fp = fork_list; fp; fp = fp->next) |
356 | { | |
dfd4cc63 | 357 | pid = ptid_get_pid (fp->ptid); |
56aac7e8 | 358 | do { |
4c28f408 PA |
359 | /* Use SIGKILL instead of PTRACE_KILL because the former works even |
360 | if the thread is running, while the later doesn't. */ | |
361 | kill (pid, SIGKILL); | |
56aac7e8 MS |
362 | ret = waitpid (pid, &status, 0); |
363 | /* We might get a SIGCHLD instead of an exit status. This is | |
364 | aggravated by the first kill above - a child has just | |
365 | died. MVS comment cut-and-pasted from linux-nat. */ | |
366 | } while (ret == pid && WIFSTOPPED (status)); | |
367 | } | |
368 | init_fork_list (); /* Clear list, prepare to start fresh. */ | |
ac264b3b MS |
369 | } |
370 | ||
371 | /* The current inferior_ptid has exited, but there are other viable | |
372 | forks to debug. Delete the exiting one and context-switch to the | |
373 | first available. */ | |
374 | ||
3cb5bea9 | 375 | void |
ac264b3b MS |
376 | linux_fork_mourn_inferior (void) |
377 | { | |
2f341b6e PA |
378 | struct fork_info *last; |
379 | int status; | |
380 | ||
ac264b3b MS |
381 | /* Wait just one more time to collect the inferior's exit status. |
382 | Do not check whether this succeeds though, since we may be | |
383 | dealing with a process that we attached to. Such a process will | |
384 | only report its exit status to its original parent. */ | |
ac264b3b MS |
385 | waitpid (ptid_get_pid (inferior_ptid), &status, 0); |
386 | ||
387 | /* OK, presumably inferior_ptid is the one who has exited. | |
388 | We need to delete that one from the fork_list, and switch | |
389 | to the next available fork. */ | |
390 | delete_fork (inferior_ptid); | |
791b663b DJ |
391 | |
392 | /* There should still be a fork - if there's only one left, | |
393 | delete_fork won't remove it, because we haven't updated | |
394 | inferior_ptid yet. */ | |
395 | gdb_assert (fork_list); | |
396 | ||
2f341b6e PA |
397 | last = find_last_fork (); |
398 | fork_load_infrun_state (last); | |
791b663b DJ |
399 | printf_filtered (_("[Switching to %s]\n"), |
400 | target_pid_to_str (inferior_ptid)); | |
401 | ||
402 | /* If there's only one fork, switch back to non-fork mode. */ | |
403 | if (fork_list->next == NULL) | |
404 | delete_fork (inferior_ptid); | |
ac264b3b MS |
405 | } |
406 | ||
7a7d3353 PA |
407 | /* The current inferior_ptid is being detached, but there are other |
408 | viable forks to debug. Detach and delete it and context-switch to | |
409 | the first available. */ | |
410 | ||
3cb5bea9 | 411 | void |
6bd6f3b6 | 412 | linux_fork_detach (int from_tty) |
7a7d3353 PA |
413 | { |
414 | /* OK, inferior_ptid is the one we are detaching from. We need to | |
415 | delete it from the fork_list, and switch to the next available | |
416 | fork. */ | |
417 | ||
dfd4cc63 | 418 | if (ptrace (PTRACE_DETACH, ptid_get_pid (inferior_ptid), 0, 0)) |
7a7d3353 PA |
419 | error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid)); |
420 | ||
421 | delete_fork (inferior_ptid); | |
7a7d3353 PA |
422 | |
423 | /* There should still be a fork - if there's only one left, | |
424 | delete_fork won't remove it, because we haven't updated | |
425 | inferior_ptid yet. */ | |
426 | gdb_assert (fork_list); | |
427 | ||
428 | fork_load_infrun_state (fork_list); | |
429 | ||
430 | if (from_tty) | |
431 | printf_filtered (_("[Switching to %s]\n"), | |
432 | target_pid_to_str (inferior_ptid)); | |
433 | ||
434 | /* If there's only one fork, switch back to non-fork mode. */ | |
435 | if (fork_list->next == NULL) | |
436 | delete_fork (inferior_ptid); | |
437 | } | |
438 | ||
7a298875 HZ |
439 | static void |
440 | inferior_call_waitpid_cleanup (void *fp) | |
441 | { | |
9a3c8263 | 442 | struct fork_info *oldfp = (struct fork_info *) fp; |
7a298875 | 443 | |
e17c9e56 HZ |
444 | if (oldfp) |
445 | { | |
1777feb0 | 446 | /* Switch back to inferior_ptid. */ |
e17c9e56 HZ |
447 | remove_breakpoints (); |
448 | fork_load_infrun_state (oldfp); | |
449 | insert_breakpoints (); | |
450 | } | |
7a298875 HZ |
451 | } |
452 | ||
453 | static int | |
454 | inferior_call_waitpid (ptid_t pptid, int pid) | |
455 | { | |
456 | struct objfile *waitpid_objf; | |
457 | struct value *waitpid_fn = NULL; | |
e17c9e56 | 458 | struct value *argv[4], *retv; |
7a298875 HZ |
459 | struct gdbarch *gdbarch = get_current_arch (); |
460 | struct fork_info *oldfp = NULL, *newfp = NULL; | |
e17c9e56 | 461 | struct cleanup *old_cleanup; |
7a298875 HZ |
462 | int ret = -1; |
463 | ||
464 | if (!ptid_equal (pptid, inferior_ptid)) | |
465 | { | |
466 | /* Switch to pptid. */ | |
467 | oldfp = find_fork_ptid (inferior_ptid); | |
468 | gdb_assert (oldfp != NULL); | |
469 | newfp = find_fork_ptid (pptid); | |
da3ecdc6 | 470 | gdb_assert (newfp != NULL); |
7a298875 HZ |
471 | fork_save_infrun_state (oldfp, 1); |
472 | remove_breakpoints (); | |
473 | fork_load_infrun_state (newfp); | |
474 | insert_breakpoints (); | |
7a298875 HZ |
475 | } |
476 | ||
e17c9e56 HZ |
477 | old_cleanup = make_cleanup (inferior_call_waitpid_cleanup, oldfp); |
478 | ||
7a298875 | 479 | /* Get the waitpid_fn. */ |
3b7344d5 | 480 | if (lookup_minimal_symbol ("waitpid", NULL, NULL).minsym != NULL) |
7a298875 | 481 | waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf); |
3b7344d5 TT |
482 | if (!waitpid_fn |
483 | && lookup_minimal_symbol ("_waitpid", NULL, NULL).minsym != NULL) | |
7a298875 HZ |
484 | waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf); |
485 | if (!waitpid_fn) | |
486 | goto out; | |
487 | ||
488 | /* Get the argv. */ | |
489 | argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid); | |
490 | argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0); | |
491 | argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); | |
492 | argv[3] = 0; | |
493 | ||
7022349d | 494 | retv = call_function_by_hand (waitpid_fn, NULL, 3, argv); |
e17c9e56 HZ |
495 | if (value_as_long (retv) < 0) |
496 | goto out; | |
7a298875 HZ |
497 | |
498 | ret = 0; | |
499 | ||
500 | out: | |
e17c9e56 | 501 | do_cleanups (old_cleanup); |
7a298875 HZ |
502 | return ret; |
503 | } | |
504 | ||
ac264b3b MS |
505 | /* Fork list <-> user interface. */ |
506 | ||
507 | static void | |
5b64bf74 | 508 | delete_checkpoint_command (const char *args, int from_tty) |
ac264b3b | 509 | { |
e17c9e56 | 510 | ptid_t ptid, pptid; |
7a298875 | 511 | struct fork_info *fi; |
ac264b3b MS |
512 | |
513 | if (!args || !*args) | |
2277426b | 514 | error (_("Requires argument (checkpoint id to delete)")); |
ac264b3b MS |
515 | |
516 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
517 | if (ptid_equal (ptid, minus_one_ptid)) | |
2277426b | 518 | error (_("No such checkpoint id, %s"), args); |
ac264b3b MS |
519 | |
520 | if (ptid_equal (ptid, inferior_ptid)) | |
3cb5bea9 PA |
521 | error (_("\ |
522 | Please switch to another checkpoint before deleting the current one")); | |
ac264b3b | 523 | |
dfd4cc63 | 524 | if (ptrace (PTRACE_KILL, ptid_get_pid (ptid), 0, 0)) |
54ba13f7 | 525 | error (_("Unable to kill pid %s"), target_pid_to_str (ptid)); |
ac264b3b | 526 | |
7a298875 HZ |
527 | fi = find_fork_ptid (ptid); |
528 | gdb_assert (fi); | |
e17c9e56 | 529 | pptid = fi->parent_ptid; |
7a298875 | 530 | |
ac264b3b MS |
531 | if (from_tty) |
532 | printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid)); | |
533 | ||
534 | delete_fork (ptid); | |
7a298875 HZ |
535 | |
536 | /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint | |
537 | list, waitpid the ptid. | |
538 | If fi->parent_ptid is a part of lwp and it is stoped, waitpid the | |
539 | ptid. */ | |
e17c9e56 HZ |
540 | if ((!find_thread_ptid (pptid) && find_fork_ptid (pptid)) |
541 | || (find_thread_ptid (pptid) && is_stopped (pptid))) | |
7a298875 | 542 | { |
dfd4cc63 | 543 | if (inferior_call_waitpid (pptid, ptid_get_pid (ptid))) |
7a298875 HZ |
544 | warning (_("Unable to wait pid %s"), target_pid_to_str (ptid)); |
545 | } | |
ac264b3b MS |
546 | } |
547 | ||
548 | static void | |
5b64bf74 | 549 | detach_checkpoint_command (const char *args, int from_tty) |
ac264b3b MS |
550 | { |
551 | ptid_t ptid; | |
552 | ||
553 | if (!args || !*args) | |
2277426b | 554 | error (_("Requires argument (checkpoint id to detach)")); |
ac264b3b MS |
555 | |
556 | ptid = fork_id_to_ptid (parse_and_eval_long (args)); | |
557 | if (ptid_equal (ptid, minus_one_ptid)) | |
2277426b | 558 | error (_("No such checkpoint id, %s"), args); |
ac264b3b MS |
559 | |
560 | if (ptid_equal (ptid, inferior_ptid)) | |
2277426b PA |
561 | error (_("\ |
562 | Please switch to another checkpoint before detaching the current one")); | |
ac264b3b | 563 | |
dfd4cc63 | 564 | if (ptrace (PTRACE_DETACH, ptid_get_pid (ptid), 0, 0)) |
ac264b3b MS |
565 | error (_("Unable to detach %s"), target_pid_to_str (ptid)); |
566 | ||
567 | if (from_tty) | |
568 | printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid)); | |
569 | ||
570 | delete_fork (ptid); | |
571 | } | |
572 | ||
3cb5bea9 | 573 | /* Print information about currently known checkpoints. */ |
ac264b3b MS |
574 | |
575 | static void | |
1d12d88f | 576 | info_checkpoints_command (const char *arg, int from_tty) |
ac264b3b | 577 | { |
5af949e3 | 578 | struct gdbarch *gdbarch = get_current_arch (); |
ac264b3b | 579 | struct symtab_and_line sal; |
ac264b3b | 580 | struct fork_info *fp; |
ac264b3b | 581 | ULONGEST pc; |
b8db102d MS |
582 | int requested = -1; |
583 | struct fork_info *printed = NULL; | |
584 | ||
585 | if (arg && *arg) | |
586 | requested = (int) parse_and_eval_long (arg); | |
ac264b3b MS |
587 | |
588 | for (fp = fork_list; fp; fp = fp->next) | |
589 | { | |
b8db102d MS |
590 | if (requested > 0 && fp->num != requested) |
591 | continue; | |
592 | ||
593 | printed = fp; | |
ac264b3b | 594 | if (ptid_equal (fp->ptid, inferior_ptid)) |
daf6667d | 595 | printf_filtered ("* "); |
ac264b3b | 596 | else |
daf6667d YQ |
597 | printf_filtered (" "); |
598 | ||
599 | pc = fp->pc; | |
ac264b3b MS |
600 | printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid)); |
601 | if (fp->num == 0) | |
602 | printf_filtered (_(" (main process)")); | |
603 | printf_filtered (_(" at ")); | |
5af949e3 | 604 | fputs_filtered (paddress (gdbarch, pc), gdb_stdout); |
ac264b3b MS |
605 | |
606 | sal = find_pc_line (pc, 0); | |
607 | if (sal.symtab) | |
05cba821 JK |
608 | printf_filtered (_(", file %s"), |
609 | symtab_to_filename_for_display (sal.symtab)); | |
ac264b3b MS |
610 | if (sal.line) |
611 | printf_filtered (_(", line %d"), sal.line); | |
612 | if (!sal.symtab && !sal.line) | |
613 | { | |
7cbd4a93 | 614 | struct bound_minimal_symbol msym; |
ac264b3b MS |
615 | |
616 | msym = lookup_minimal_symbol_by_pc (pc); | |
7cbd4a93 | 617 | if (msym.minsym) |
efd66ac6 | 618 | printf_filtered (", <%s>", MSYMBOL_LINKAGE_NAME (msym.minsym)); |
ac264b3b MS |
619 | } |
620 | ||
621 | putchar_filtered ('\n'); | |
622 | } | |
b8db102d MS |
623 | if (printed == NULL) |
624 | { | |
625 | if (requested > 0) | |
2277426b | 626 | printf_filtered (_("No checkpoint number %d.\n"), requested); |
b8db102d | 627 | else |
2277426b | 628 | printf_filtered (_("No checkpoints.\n")); |
b8db102d | 629 | } |
ac264b3b MS |
630 | } |
631 | ||
2277426b PA |
632 | /* The PID of the process we're checkpointing. */ |
633 | static int checkpointing_pid = 0; | |
ac264b3b | 634 | |
2277426b PA |
635 | int |
636 | linux_fork_checkpointing_p (int pid) | |
ac264b3b | 637 | { |
2277426b | 638 | return (checkpointing_pid == pid); |
ac264b3b MS |
639 | } |
640 | ||
92f6badc KP |
641 | /* Callback for iterate over threads. Used to check whether |
642 | the current inferior is multi-threaded. Returns true as soon | |
643 | as it sees the second thread of the current inferior. */ | |
644 | ||
645 | static int | |
646 | inf_has_multiple_thread_cb (struct thread_info *tp, void *data) | |
647 | { | |
648 | int *count_p = (int *) data; | |
649 | ||
650 | if (current_inferior ()->pid == ptid_get_pid (tp->ptid)) | |
651 | (*count_p)++; | |
652 | ||
653 | /* Stop the iteration if multiple threads have been detected. */ | |
654 | return *count_p > 1; | |
655 | } | |
656 | ||
657 | /* Return true if the current inferior is multi-threaded. */ | |
658 | ||
659 | static int | |
660 | inf_has_multiple_threads (void) | |
661 | { | |
662 | int count = 0; | |
663 | ||
664 | iterate_over_threads (inf_has_multiple_thread_cb, &count); | |
665 | return (count > 1); | |
666 | } | |
667 | ||
ac264b3b | 668 | static void |
0b39b52e | 669 | checkpoint_command (const char *args, int from_tty) |
ac264b3b | 670 | { |
3e3b026f UW |
671 | struct objfile *fork_objf; |
672 | struct gdbarch *gdbarch; | |
ac264b3b MS |
673 | struct target_waitstatus last_target_waitstatus; |
674 | ptid_t last_target_ptid; | |
675 | struct value *fork_fn = NULL, *ret; | |
676 | struct fork_info *fp; | |
677 | pid_t retpid; | |
74960c60 | 678 | |
92f6badc KP |
679 | if (!target_has_execution) |
680 | error (_("The program is not being run.")); | |
681 | ||
682 | /* Ensure that the inferior is not multithreaded. */ | |
683 | update_thread_list (); | |
684 | if (inf_has_multiple_threads ()) | |
685 | error (_("checkpoint: can't checkpoint multiple threads.")); | |
686 | ||
ac264b3b MS |
687 | /* Make the inferior fork, record its (and gdb's) state. */ |
688 | ||
3b7344d5 | 689 | if (lookup_minimal_symbol ("fork", NULL, NULL).minsym != NULL) |
3e3b026f | 690 | fork_fn = find_function_in_inferior ("fork", &fork_objf); |
ac264b3b | 691 | if (!fork_fn) |
3b7344d5 | 692 | if (lookup_minimal_symbol ("_fork", NULL, NULL).minsym != NULL) |
3e3b026f | 693 | fork_fn = find_function_in_inferior ("fork", &fork_objf); |
ac264b3b MS |
694 | if (!fork_fn) |
695 | error (_("checkpoint: can't find fork function in inferior.")); | |
696 | ||
3e3b026f UW |
697 | gdbarch = get_objfile_arch (fork_objf); |
698 | ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); | |
2277426b PA |
699 | |
700 | /* Tell linux-nat.c that we're checkpointing this inferior. */ | |
b7b633e9 TT |
701 | { |
702 | scoped_restore save_pid | |
703 | = make_scoped_restore (&checkpointing_pid, ptid_get_pid (inferior_ptid)); | |
704 | ||
7022349d | 705 | ret = call_function_by_hand (fork_fn, NULL, 0, &ret); |
b7b633e9 | 706 | } |
2277426b | 707 | |
ac264b3b MS |
708 | if (!ret) /* Probably can't happen. */ |
709 | error (_("checkpoint: call_function_by_hand returned null.")); | |
710 | ||
711 | retpid = value_as_long (ret); | |
712 | get_last_target_status (&last_target_ptid, &last_target_waitstatus); | |
6f9d33d8 PP |
713 | |
714 | fp = find_fork_pid (retpid); | |
715 | ||
ac264b3b MS |
716 | if (from_tty) |
717 | { | |
718 | int parent_pid; | |
719 | ||
6f9d33d8 PP |
720 | printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"), |
721 | fp != NULL ? fp->num : -1, (long) retpid); | |
ac264b3b MS |
722 | if (info_verbose) |
723 | { | |
724 | parent_pid = ptid_get_lwp (last_target_ptid); | |
725 | if (parent_pid == 0) | |
726 | parent_pid = ptid_get_pid (last_target_ptid); | |
3cb5bea9 | 727 | printf_filtered (_(" gdb says parent = %ld.\n"), |
ac264b3b MS |
728 | (long) parent_pid); |
729 | } | |
730 | } | |
731 | ||
ac264b3b MS |
732 | if (!fp) |
733 | error (_("Failed to find new fork")); | |
734 | fork_save_infrun_state (fp, 1); | |
7a298875 | 735 | fp->parent_ptid = last_target_ptid; |
ac264b3b MS |
736 | } |
737 | ||
738 | static void | |
739 | linux_fork_context (struct fork_info *newfp, int from_tty) | |
740 | { | |
741 | /* Now we attempt to switch processes. */ | |
0d14fc63 | 742 | struct fork_info *oldfp; |
ac264b3b | 743 | |
0d14fc63 | 744 | gdb_assert (newfp != NULL); |
ac264b3b | 745 | |
0d14fc63 PA |
746 | oldfp = find_fork_ptid (inferior_ptid); |
747 | gdb_assert (oldfp != NULL); | |
ac264b3b MS |
748 | |
749 | fork_save_infrun_state (oldfp, 1); | |
74960c60 | 750 | remove_breakpoints (); |
ac264b3b | 751 | fork_load_infrun_state (newfp); |
74960c60 | 752 | insert_breakpoints (); |
ac264b3b | 753 | |
3cb5bea9 | 754 | printf_filtered (_("Switching to %s\n"), |
ac264b3b MS |
755 | target_pid_to_str (inferior_ptid)); |
756 | ||
08d72866 | 757 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); |
ac264b3b MS |
758 | } |
759 | ||
2277426b | 760 | /* Switch inferior process (checkpoint) context, by checkpoint id. */ |
ac264b3b | 761 | static void |
0b39b52e | 762 | restart_command (const char *args, int from_tty) |
ac264b3b MS |
763 | { |
764 | struct fork_info *fp; | |
765 | ||
766 | if (!args || !*args) | |
767 | error (_("Requires argument (checkpoint id to restart)")); | |
768 | ||
769 | if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) | |
770 | error (_("Not found: checkpoint id %s"), args); | |
771 | ||
772 | linux_fork_context (fp, from_tty); | |
773 | } | |
774 | ||
775 | void | |
776 | _initialize_linux_fork (void) | |
777 | { | |
778 | init_fork_list (); | |
779 | ||
ac264b3b MS |
780 | /* Checkpoint command: create a fork of the inferior process |
781 | and set it aside for later debugging. */ | |
782 | ||
783 | add_com ("checkpoint", class_obscure, checkpoint_command, _("\ | |
784 | Fork a duplicate process (experimental).")); | |
785 | ||
2277426b PA |
786 | /* Restart command: restore the context of a specified checkpoint |
787 | process. */ | |
ac264b3b MS |
788 | |
789 | add_com ("restart", class_obscure, restart_command, _("\ | |
790 | restart <n>: restore program context from a checkpoint.\n\ | |
791 | Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'.")); | |
792 | ||
b8db102d | 793 | /* Delete checkpoint command: kill the process and remove it from |
3cb5bea9 | 794 | the fork list. */ |
ac264b3b | 795 | |
3cb5bea9 | 796 | add_cmd ("checkpoint", class_obscure, delete_checkpoint_command, _("\ |
2277426b | 797 | Delete a checkpoint (experimental)."), |
b8db102d | 798 | &deletelist); |
ac264b3b | 799 | |
3cb5bea9 | 800 | /* Detach checkpoint command: release the process to run independently, |
ac264b3b MS |
801 | and remove it from the fork list. */ |
802 | ||
3cb5bea9 | 803 | add_cmd ("checkpoint", class_obscure, detach_checkpoint_command, _("\ |
2277426b | 804 | Detach from a checkpoint (experimental)."), |
f73adfeb | 805 | &detachlist); |
ac264b3b | 806 | |
3cb5bea9 | 807 | /* Info checkpoints command: list all forks/checkpoints |
ac264b3b MS |
808 | currently under gdb's control. */ |
809 | ||
3cb5bea9 | 810 | add_info ("checkpoints", info_checkpoints_command, |
2277426b | 811 | _("IDs of currently known checkpoints.")); |
ac264b3b | 812 | } |