linux-fork.c 21 KB

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