Some get_last_target_status tweaks
[deliverable/binutils-gdb.git] / gdb / linux-fork.c
CommitLineData
ac264b3b
MS
1/* GNU/Linux native-dependent code for debugging multiple forks.
2
b811d2c2 3 Copyright (C) 2005-2020 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"
2978b111 35#include <dirent.h>
ac264b3b
MS
36#include <ctype.h>
37
06974e6c 38#include <list>
ac264b3b 39
ac264b3b
MS
40/* Fork list data structure: */
41struct fork_info
42{
06974e6c
PA
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
06974e6c
PA
80 /* Set of open file descriptors' offsets. */
81 off_t *filepos = nullptr;
82
83 int maxfd = 0;
ac264b3b
MS
84};
85
06974e6c
PA
86static std::list<fork_info> fork_list;
87static int highest_fork_num;
88
ac264b3b
MS
89/* Fork list methods: */
90
3cb5bea9 91int
ac264b3b
MS
92forks_exist_p (void)
93{
06974e6c 94 return !fork_list.empty ();
ac264b3b
MS
95}
96
2f341b6e
PA
97/* Return the last fork in the list. */
98
99static struct fork_info *
100find_last_fork (void)
101{
06974e6c 102 if (fork_list.empty ())
2f341b6e
PA
103 return NULL;
104
06974e6c 105 return &fork_list.back ();
2f341b6e
PA
106}
107
06974e6c 108/* Return true iff there's one fork in the list. */
ac264b3b 109
06974e6c
PA
110static bool
111one_fork_p ()
ac264b3b 112{
06974e6c
PA
113 return (!fork_list.empty ()
114 && &fork_list.front () == &fork_list.back ());
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)
fc5b8736 223 get_current_regcache ()->restore (fp->savedregs);
ac264b3b 224
791b663b
DJ
225 registers_changed ();
226 reinit_frame_cache ();
227
f2ffa92b
PA
228 inferior_thread ()->suspend.stop_pc
229 = regcache_read_pc (get_current_regcache ());
ac264b3b
MS
230 nullify_last_target_wait_ptid ();
231
232 /* Now restore the file positions of open file descriptors. */
233 if (fp->filepos)
234 {
235 for (i = 0; i <= fp->maxfd; i++)
236 if (fp->filepos[i] != (off_t) -1)
237 call_lseek (i, fp->filepos[i], SEEK_SET);
238 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
239 this is native-only. If it ever has to be cross, we'll have
240 to rethink this. */
241 }
242}
243
b7e60d85 244/* Save infrun state for the fork FP. */
ac264b3b 245
2277426b 246static void
b7e60d85 247fork_save_infrun_state (struct fork_info *fp)
ac264b3b 248{
d8d2a3ee 249 char path[PATH_MAX];
ac264b3b
MS
250 struct dirent *de;
251 DIR *d;
252
253 if (fp->savedregs)
c0e383c6 254 delete fp->savedregs;
ac264b3b 255
daf6667d
YQ
256 fp->savedregs = new readonly_detached_regcache (*get_current_regcache ());
257 fp->pc = regcache_read_pc (get_current_regcache ());
ac264b3b 258
b7e60d85
PA
259 /* Now save the 'state' (file position) of all open file descriptors.
260 Unfortunately fork does not take care of that for us... */
261 snprintf (path, PATH_MAX, "/proc/%ld/fd", (long) fp->ptid.pid ());
262 if ((d = opendir (path)) != NULL)
ac264b3b 263 {
b7e60d85
PA
264 long tmp;
265
266 fp->maxfd = 0;
267 while ((de = readdir (d)) != NULL)
ac264b3b 268 {
b7e60d85
PA
269 /* Count open file descriptors (actually find highest
270 numbered). */
271 tmp = strtol (&de->d_name[0], NULL, 10);
272 if (fp->maxfd < tmp)
273 fp->maxfd = tmp;
ac264b3b 274 }
b7e60d85
PA
275 /* Allocate array of file positions. */
276 fp->filepos = XRESIZEVEC (off_t, fp->filepos, fp->maxfd + 1);
277
278 /* Initialize to -1 (invalid). */
279 for (tmp = 0; tmp <= fp->maxfd; tmp++)
280 fp->filepos[tmp] = -1;
281
282 /* Now find actual file positions. */
283 rewinddir (d);
284 while ((de = readdir (d)) != NULL)
285 if (isdigit (de->d_name[0]))
286 {
287 tmp = strtol (&de->d_name[0], NULL, 10);
288 fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR);
289 }
290 closedir (d);
ac264b3b
MS
291 }
292}
293
294/* Kill 'em all, let God sort 'em out... */
295
3cb5bea9 296void
ac264b3b
MS
297linux_fork_killall (void)
298{
299 /* Walk list and kill every pid. No need to treat the
300 current inferior_ptid as special (we do not return a
301 status for it) -- however any process may be a child
302 or a parent, so may get a SIGCHLD from a previously
303 killed child. Wait them all out. */
ac264b3b 304
06974e6c 305 for (fork_info &fi : fork_list)
56aac7e8 306 {
06974e6c
PA
307 pid_t pid = fi.ptid.pid ();
308 int status;
309 pid_t ret;
56aac7e8 310 do {
4c28f408
PA
311 /* Use SIGKILL instead of PTRACE_KILL because the former works even
312 if the thread is running, while the later doesn't. */
313 kill (pid, SIGKILL);
56aac7e8
MS
314 ret = waitpid (pid, &status, 0);
315 /* We might get a SIGCHLD instead of an exit status. This is
316 aggravated by the first kill above - a child has just
317 died. MVS comment cut-and-pasted from linux-nat. */
318 } while (ret == pid && WIFSTOPPED (status));
319 }
06974e6c
PA
320
321 /* Clear list, prepare to start fresh. */
322 fork_list.clear ();
ac264b3b
MS
323}
324
325/* The current inferior_ptid has exited, but there are other viable
326 forks to debug. Delete the exiting one and context-switch to the
327 first available. */
328
3cb5bea9 329void
ac264b3b
MS
330linux_fork_mourn_inferior (void)
331{
2f341b6e
PA
332 struct fork_info *last;
333 int status;
334
ac264b3b
MS
335 /* Wait just one more time to collect the inferior's exit status.
336 Do not check whether this succeeds though, since we may be
337 dealing with a process that we attached to. Such a process will
338 only report its exit status to its original parent. */
e99b03dc 339 waitpid (inferior_ptid.pid (), &status, 0);
ac264b3b
MS
340
341 /* OK, presumably inferior_ptid is the one who has exited.
342 We need to delete that one from the fork_list, and switch
343 to the next available fork. */
344 delete_fork (inferior_ptid);
791b663b
DJ
345
346 /* There should still be a fork - if there's only one left,
347 delete_fork won't remove it, because we haven't updated
348 inferior_ptid yet. */
06974e6c 349 gdb_assert (!fork_list.empty ());
791b663b 350
2f341b6e
PA
351 last = find_last_fork ();
352 fork_load_infrun_state (last);
791b663b 353 printf_filtered (_("[Switching to %s]\n"),
a068643d 354 target_pid_to_str (inferior_ptid).c_str ());
791b663b
DJ
355
356 /* If there's only one fork, switch back to non-fork mode. */
06974e6c 357 if (one_fork_p ())
791b663b 358 delete_fork (inferior_ptid);
ac264b3b
MS
359}
360
7a7d3353
PA
361/* The current inferior_ptid is being detached, but there are other
362 viable forks to debug. Detach and delete it and context-switch to
363 the first available. */
364
3cb5bea9 365void
6bd6f3b6 366linux_fork_detach (int from_tty)
7a7d3353
PA
367{
368 /* OK, inferior_ptid is the one we are detaching from. We need to
369 delete it from the fork_list, and switch to the next available
370 fork. */
371
e99b03dc 372 if (ptrace (PTRACE_DETACH, inferior_ptid.pid (), 0, 0))
a068643d
TT
373 error (_("Unable to detach %s"),
374 target_pid_to_str (inferior_ptid).c_str ());
7a7d3353
PA
375
376 delete_fork (inferior_ptid);
7a7d3353
PA
377
378 /* There should still be a fork - if there's only one left,
379 delete_fork won't remove it, because we haven't updated
380 inferior_ptid yet. */
06974e6c 381 gdb_assert (!fork_list.empty ());
7a7d3353 382
06974e6c 383 fork_load_infrun_state (&fork_list.front ());
7a7d3353
PA
384
385 if (from_tty)
386 printf_filtered (_("[Switching to %s]\n"),
a068643d 387 target_pid_to_str (inferior_ptid).c_str ());
7a7d3353
PA
388
389 /* If there's only one fork, switch back to non-fork mode. */
06974e6c 390 if (one_fork_p ())
7a7d3353
PA
391 delete_fork (inferior_ptid);
392}
393
a07c8880
AB
394/* Temporarily switch to the infrun state stored on the fork_info
395 identified by a given ptid_t. When this object goes out of scope,
396 restore the currently selected infrun state. */
7a298875 397
a07c8880
AB
398class scoped_switch_fork_info
399{
400public:
401 /* Switch to the infrun state held on the fork_info identified by
402 PPTID. If PPTID is the current inferior then no switch is done. */
1ef8573c 403 explicit scoped_switch_fork_info (ptid_t pptid)
a07c8880
AB
404 : m_oldfp (nullptr)
405 {
406 if (pptid != inferior_ptid)
407 {
408 struct fork_info *newfp = nullptr;
409
410 /* Switch to pptid. */
411 m_oldfp = find_fork_ptid (inferior_ptid);
412 gdb_assert (m_oldfp != nullptr);
413 newfp = find_fork_ptid (pptid);
414 gdb_assert (newfp != nullptr);
b7e60d85 415 fork_save_infrun_state (m_oldfp);
a07c8880
AB
416 remove_breakpoints ();
417 fork_load_infrun_state (newfp);
418 insert_breakpoints ();
419 }
420 }
421
422 /* Restore the previously selected infrun state. If the constructor
423 didn't need to switch states, then nothing is done here either. */
424 ~scoped_switch_fork_info ()
425 {
426 if (m_oldfp != nullptr)
427 {
428 /* Switch back to inferior_ptid. */
a70b8144 429 try
1ef8573c
AB
430 {
431 remove_breakpoints ();
432 fork_load_infrun_state (m_oldfp);
433 insert_breakpoints ();
434 }
230d2906 435 catch (const gdb_exception &ex)
1ef8573c
AB
436 {
437 warning (_("Couldn't restore checkpoint state in %s: %s"),
3d6e9d23
TT
438 target_pid_to_str (m_oldfp->ptid).c_str (),
439 ex.what ());
1ef8573c 440 }
a07c8880
AB
441 }
442 }
443
444 DISABLE_COPY_AND_ASSIGN (scoped_switch_fork_info);
445
446private:
447 /* The fork_info for the previously selected infrun state, or nullptr if
448 we were already in the desired state, and nothing needs to be
449 restored. */
450 struct fork_info *m_oldfp;
451};
7a298875
HZ
452
453static int
454inferior_call_waitpid (ptid_t pptid, int pid)
455{
456 struct objfile *waitpid_objf;
457 struct value *waitpid_fn = NULL;
7a298875
HZ
458 int ret = -1;
459
a07c8880 460 scoped_switch_fork_info switch_fork_info (pptid);
e17c9e56 461
7a298875 462 /* Get the waitpid_fn. */
3b7344d5 463 if (lookup_minimal_symbol ("waitpid", NULL, NULL).minsym != NULL)
7a298875 464 waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf);
3b7344d5
TT
465 if (!waitpid_fn
466 && lookup_minimal_symbol ("_waitpid", NULL, NULL).minsym != NULL)
7a298875 467 waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf);
a07c8880
AB
468 if (waitpid_fn != nullptr)
469 {
470 struct gdbarch *gdbarch = get_current_arch ();
471 struct value *argv[3], *retv;
7a298875 472
a07c8880
AB
473 /* Get the argv. */
474 argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid);
475 argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0);
476 argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
7a298875 477
a07c8880 478 retv = call_function_by_hand (waitpid_fn, NULL, argv);
7a298875 479
a07c8880
AB
480 if (value_as_long (retv) >= 0)
481 ret = 0;
482 }
7a298875 483
7a298875
HZ
484 return ret;
485}
486
ac264b3b
MS
487/* Fork list <-> user interface. */
488
489static void
5b64bf74 490delete_checkpoint_command (const char *args, int from_tty)
ac264b3b 491{
e17c9e56 492 ptid_t ptid, pptid;
7a298875 493 struct fork_info *fi;
ac264b3b
MS
494
495 if (!args || !*args)
2277426b 496 error (_("Requires argument (checkpoint id to delete)"));
ac264b3b
MS
497
498 ptid = fork_id_to_ptid (parse_and_eval_long (args));
d7e15655 499 if (ptid == minus_one_ptid)
2277426b 500 error (_("No such checkpoint id, %s"), args);
ac264b3b 501
d7e15655 502 if (ptid == inferior_ptid)
3cb5bea9
PA
503 error (_("\
504Please switch to another checkpoint before deleting the current one"));
ac264b3b 505
e99b03dc 506 if (ptrace (PTRACE_KILL, ptid.pid (), 0, 0))
a068643d 507 error (_("Unable to kill pid %s"), target_pid_to_str (ptid).c_str ());
ac264b3b 508
7a298875
HZ
509 fi = find_fork_ptid (ptid);
510 gdb_assert (fi);
e17c9e56 511 pptid = fi->parent_ptid;
7a298875 512
ac264b3b 513 if (from_tty)
a068643d 514 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid).c_str ());
ac264b3b
MS
515
516 delete_fork (ptid);
7a298875
HZ
517
518 /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint
519 list, waitpid the ptid.
00431a78 520 If fi->parent_ptid is a part of lwp and it is stopped, waitpid the
7a298875 521 ptid. */
00431a78
PA
522 thread_info *parent = find_thread_ptid (pptid);
523 if ((parent == NULL && find_fork_ptid (pptid))
524 || (parent != NULL && parent->state == THREAD_STOPPED))
7a298875 525 {
e99b03dc 526 if (inferior_call_waitpid (pptid, ptid.pid ()))
a068643d
TT
527 warning (_("Unable to wait pid %s"),
528 target_pid_to_str (ptid).c_str ());
7a298875 529 }
ac264b3b
MS
530}
531
532static void
5b64bf74 533detach_checkpoint_command (const char *args, int from_tty)
ac264b3b
MS
534{
535 ptid_t ptid;
536
537 if (!args || !*args)
2277426b 538 error (_("Requires argument (checkpoint id to detach)"));
ac264b3b
MS
539
540 ptid = fork_id_to_ptid (parse_and_eval_long (args));
d7e15655 541 if (ptid == minus_one_ptid)
2277426b 542 error (_("No such checkpoint id, %s"), args);
ac264b3b 543
d7e15655 544 if (ptid == inferior_ptid)
2277426b
PA
545 error (_("\
546Please switch to another checkpoint before detaching the current one"));
ac264b3b 547
e99b03dc 548 if (ptrace (PTRACE_DETACH, ptid.pid (), 0, 0))
a068643d 549 error (_("Unable to detach %s"), target_pid_to_str (ptid).c_str ());
ac264b3b
MS
550
551 if (from_tty)
a068643d 552 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid).c_str ());
ac264b3b
MS
553
554 delete_fork (ptid);
555}
556
3cb5bea9 557/* Print information about currently known checkpoints. */
ac264b3b
MS
558
559static void
1d12d88f 560info_checkpoints_command (const char *arg, int from_tty)
ac264b3b 561{
5af949e3 562 struct gdbarch *gdbarch = get_current_arch ();
b8db102d 563 int requested = -1;
06974e6c 564 const fork_info *printed = NULL;
b8db102d
MS
565
566 if (arg && *arg)
567 requested = (int) parse_and_eval_long (arg);
ac264b3b 568
06974e6c 569 for (const fork_info &fi : fork_list)
ac264b3b 570 {
06974e6c 571 if (requested > 0 && fi.num != requested)
b8db102d
MS
572 continue;
573
06974e6c
PA
574 printed = &fi;
575 if (fi.ptid == inferior_ptid)
daf6667d 576 printf_filtered ("* ");
ac264b3b 577 else
daf6667d
YQ
578 printf_filtered (" ");
579
06974e6c 580 ULONGEST pc = fi.pc;
a068643d 581 printf_filtered ("%d %s", fi.num, target_pid_to_str (fi.ptid).c_str ());
06974e6c 582 if (fi.num == 0)
ac264b3b
MS
583 printf_filtered (_(" (main process)"));
584 printf_filtered (_(" at "));
5af949e3 585 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
ac264b3b 586
06974e6c 587 symtab_and_line sal = find_pc_line (pc, 0);
ac264b3b 588 if (sal.symtab)
05cba821
JK
589 printf_filtered (_(", file %s"),
590 symtab_to_filename_for_display (sal.symtab));
ac264b3b
MS
591 if (sal.line)
592 printf_filtered (_(", line %d"), sal.line);
593 if (!sal.symtab && !sal.line)
594 {
7cbd4a93 595 struct bound_minimal_symbol msym;
ac264b3b
MS
596
597 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 598 if (msym.minsym)
c9d95fa3 599 printf_filtered (", <%s>", msym.minsym->linkage_name ());
ac264b3b
MS
600 }
601
602 putchar_filtered ('\n');
603 }
b8db102d
MS
604 if (printed == NULL)
605 {
606 if (requested > 0)
2277426b 607 printf_filtered (_("No checkpoint number %d.\n"), requested);
b8db102d 608 else
2277426b 609 printf_filtered (_("No checkpoints.\n"));
b8db102d 610 }
ac264b3b
MS
611}
612
2277426b
PA
613/* The PID of the process we're checkpointing. */
614static int checkpointing_pid = 0;
ac264b3b 615
2277426b
PA
616int
617linux_fork_checkpointing_p (int pid)
ac264b3b 618{
2277426b 619 return (checkpointing_pid == pid);
ac264b3b
MS
620}
621
92f6badc
KP
622/* Return true if the current inferior is multi-threaded. */
623
e52c971f
PA
624static bool
625inf_has_multiple_threads ()
92f6badc
KP
626{
627 int count = 0;
628
e52c971f
PA
629 /* Return true as soon as we see the second thread of the current
630 inferior. */
631 for (thread_info *tp ATTRIBUTE_UNUSED : current_inferior ()->threads ())
632 if (++count > 1)
633 return true;
634
635 return false;
92f6badc
KP
636}
637
ac264b3b 638static void
0b39b52e 639checkpoint_command (const char *args, int from_tty)
ac264b3b 640{
3e3b026f
UW
641 struct objfile *fork_objf;
642 struct gdbarch *gdbarch;
ac264b3b
MS
643 struct target_waitstatus last_target_waitstatus;
644 ptid_t last_target_ptid;
645 struct value *fork_fn = NULL, *ret;
646 struct fork_info *fp;
647 pid_t retpid;
74960c60 648
92f6badc
KP
649 if (!target_has_execution)
650 error (_("The program is not being run."));
651
652 /* Ensure that the inferior is not multithreaded. */
653 update_thread_list ();
654 if (inf_has_multiple_threads ())
655 error (_("checkpoint: can't checkpoint multiple threads."));
656
ac264b3b
MS
657 /* Make the inferior fork, record its (and gdb's) state. */
658
3b7344d5 659 if (lookup_minimal_symbol ("fork", NULL, NULL).minsym != NULL)
3e3b026f 660 fork_fn = find_function_in_inferior ("fork", &fork_objf);
ac264b3b 661 if (!fork_fn)
3b7344d5 662 if (lookup_minimal_symbol ("_fork", NULL, NULL).minsym != NULL)
3e3b026f 663 fork_fn = find_function_in_inferior ("fork", &fork_objf);
ac264b3b
MS
664 if (!fork_fn)
665 error (_("checkpoint: can't find fork function in inferior."));
666
3e3b026f
UW
667 gdbarch = get_objfile_arch (fork_objf);
668 ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
2277426b
PA
669
670 /* Tell linux-nat.c that we're checkpointing this inferior. */
b7b633e9
TT
671 {
672 scoped_restore save_pid
e99b03dc 673 = make_scoped_restore (&checkpointing_pid, inferior_ptid.pid ());
b7b633e9 674
e71585ff 675 ret = call_function_by_hand (fork_fn, NULL, {});
b7b633e9 676 }
2277426b 677
ac264b3b
MS
678 if (!ret) /* Probably can't happen. */
679 error (_("checkpoint: call_function_by_hand returned null."));
680
681 retpid = value_as_long (ret);
682 get_last_target_status (&last_target_ptid, &last_target_waitstatus);
6f9d33d8
PP
683
684 fp = find_fork_pid (retpid);
685
ac264b3b
MS
686 if (from_tty)
687 {
688 int parent_pid;
689
6f9d33d8
PP
690 printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"),
691 fp != NULL ? fp->num : -1, (long) retpid);
ac264b3b
MS
692 if (info_verbose)
693 {
e38504b3 694 parent_pid = last_target_ptid.lwp ();
ac264b3b 695 if (parent_pid == 0)
e99b03dc 696 parent_pid = last_target_ptid.pid ();
3cb5bea9 697 printf_filtered (_(" gdb says parent = %ld.\n"),
ac264b3b
MS
698 (long) parent_pid);
699 }
700 }
701
ac264b3b
MS
702 if (!fp)
703 error (_("Failed to find new fork"));
72f31aea 704
06974e6c 705 if (one_fork_p ())
72f31aea
PA
706 {
707 /* Special case -- if this is the first fork in the list (the
708 list was hitherto empty), then add inferior_ptid first, as a
709 special zeroeth fork id. */
06974e6c 710 fork_list.emplace_front (inferior_ptid.pid ());
72f31aea
PA
711 }
712
b7e60d85 713 fork_save_infrun_state (fp);
7a298875 714 fp->parent_ptid = last_target_ptid;
ac264b3b
MS
715}
716
717static void
718linux_fork_context (struct fork_info *newfp, int from_tty)
719{
720 /* Now we attempt to switch processes. */
0d14fc63 721 struct fork_info *oldfp;
ac264b3b 722
0d14fc63 723 gdb_assert (newfp != NULL);
ac264b3b 724
0d14fc63
PA
725 oldfp = find_fork_ptid (inferior_ptid);
726 gdb_assert (oldfp != NULL);
ac264b3b 727
b7e60d85 728 fork_save_infrun_state (oldfp);
74960c60 729 remove_breakpoints ();
ac264b3b 730 fork_load_infrun_state (newfp);
74960c60 731 insert_breakpoints ();
ac264b3b 732
3cb5bea9 733 printf_filtered (_("Switching to %s\n"),
a068643d 734 target_pid_to_str (inferior_ptid).c_str ());
ac264b3b 735
08d72866 736 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
ac264b3b
MS
737}
738
2277426b 739/* Switch inferior process (checkpoint) context, by checkpoint id. */
ac264b3b 740static void
0b39b52e 741restart_command (const char *args, int from_tty)
ac264b3b
MS
742{
743 struct fork_info *fp;
744
745 if (!args || !*args)
746 error (_("Requires argument (checkpoint id to restart)"));
747
748 if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL)
749 error (_("Not found: checkpoint id %s"), args);
750
751 linux_fork_context (fp, from_tty);
752}
753
754void
755_initialize_linux_fork (void)
756{
ac264b3b
MS
757 /* Checkpoint command: create a fork of the inferior process
758 and set it aside for later debugging. */
759
760 add_com ("checkpoint", class_obscure, checkpoint_command, _("\
761Fork a duplicate process (experimental)."));
762
2277426b
PA
763 /* Restart command: restore the context of a specified checkpoint
764 process. */
ac264b3b
MS
765
766 add_com ("restart", class_obscure, restart_command, _("\
590042fc
PW
767Restore program context from a checkpoint.\n\
768Usage: restart N\n\
c8a15b78 769Argument N is checkpoint ID, as displayed by 'info checkpoints'."));
ac264b3b 770
b8db102d 771 /* Delete checkpoint command: kill the process and remove it from
3cb5bea9 772 the fork list. */
ac264b3b 773
3cb5bea9 774 add_cmd ("checkpoint", class_obscure, delete_checkpoint_command, _("\
2277426b 775Delete a checkpoint (experimental)."),
b8db102d 776 &deletelist);
ac264b3b 777
3cb5bea9 778 /* Detach checkpoint command: release the process to run independently,
ac264b3b
MS
779 and remove it from the fork list. */
780
3cb5bea9 781 add_cmd ("checkpoint", class_obscure, detach_checkpoint_command, _("\
2277426b 782Detach from a checkpoint (experimental)."),
f73adfeb 783 &detachlist);
ac264b3b 784
3cb5bea9 785 /* Info checkpoints command: list all forks/checkpoints
ac264b3b
MS
786 currently under gdb's control. */
787
3cb5bea9 788 add_info ("checkpoints", info_checkpoints_command,
2277426b 789 _("IDs of currently known checkpoints."));
ac264b3b 790}
This page took 1.325063 seconds and 4 git commands to generate.