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