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