gdb: add target_ops::supports_displaced_step
[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;
84d53fa9
SM
64
65 xfree (filepos);
06974e6c
PA
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{
fa47e446 113 return fork_list.size () == 1;
72f31aea
PA
114}
115
116/* Add a new fork to the internal fork list. */
117
118void
119add_fork (pid_t pid)
120{
06974e6c 121 fork_list.emplace_back (pid);
2f341b6e 122
06974e6c
PA
123 if (one_fork_p ())
124 highest_fork_num = 0;
2f341b6e 125
06974e6c 126 fork_info *fp = &fork_list.back ();
72f31aea 127 fp->num = ++highest_fork_num;
ac264b3b
MS
128}
129
ac264b3b
MS
130static void
131delete_fork (ptid_t ptid)
132{
e99b03dc 133 linux_target->low_forget_process (ptid.pid ());
26cb8b7c 134
06974e6c
PA
135 for (auto it = fork_list.begin (); it != fork_list.end (); ++it)
136 if (it->ptid == ptid)
137 {
138 fork_list.erase (it);
ac264b3b 139
06974e6c
PA
140 /* Special case: if there is now only one process in the list,
141 and if it is (hopefully!) the current inferior_ptid, then
142 remove it, leaving the list empty -- we're now down to the
143 default case of debugging a single process. */
144 if (one_fork_p () && fork_list.front ().ptid == inferior_ptid)
145 {
146 /* Last fork -- delete from list and handle as solo
147 process (should be a safe recursion). */
148 delete_fork (inferior_ptid);
149 }
150 return;
151 }
ac264b3b
MS
152}
153
154/* Find a fork_info by matching PTID. */
155static struct fork_info *
156find_fork_ptid (ptid_t ptid)
157{
06974e6c
PA
158 for (fork_info &fi : fork_list)
159 if (fi.ptid == ptid)
160 return &fi;
ac264b3b
MS
161
162 return NULL;
163}
164
165/* Find a fork_info by matching ID. */
166static struct fork_info *
167find_fork_id (int num)
168{
06974e6c
PA
169 for (fork_info &fi : fork_list)
170 if (fi.num == num)
171 return &fi;
ac264b3b
MS
172
173 return NULL;
174}
175
176/* Find a fork_info by matching pid. */
177extern struct fork_info *
178find_fork_pid (pid_t pid)
179{
06974e6c
PA
180 for (fork_info &fi : fork_list)
181 if (pid == fi.ptid.pid ())
182 return &fi;
ac264b3b
MS
183
184 return NULL;
185}
186
187static ptid_t
188fork_id_to_ptid (int num)
189{
190 struct fork_info *fork = find_fork_id (num);
191 if (fork)
192 return fork->ptid;
193 else
f2907e49 194 return ptid_t (-1);
ac264b3b
MS
195}
196
ac264b3b
MS
197/* Fork list <-> gdb interface. */
198
3cb5bea9 199/* Utility function for fork_load/fork_save.
ac264b3b
MS
200 Calls lseek in the (current) inferior process. */
201
202static off_t
203call_lseek (int fd, off_t offset, int whence)
204{
205 char exp[80];
206
7022349d 207 snprintf (&exp[0], sizeof (exp), "(long) lseek (%d, %ld, %d)",
ac264b3b
MS
208 fd, (long) offset, whence);
209 return (off_t) parse_and_eval_long (&exp[0]);
210}
211
212/* Load infrun state for the fork PTID. */
213
214static void
215fork_load_infrun_state (struct fork_info *fp)
216{
ac264b3b
MS
217 int i;
218
2277426b 219 linux_nat_switch_fork (fp->ptid);
f973ed9c 220
b7e60d85 221 if (fp->savedregs)
fc5b8736 222 get_current_regcache ()->restore (fp->savedregs);
ac264b3b 223
791b663b
DJ
224 registers_changed ();
225 reinit_frame_cache ();
226
f2ffa92b
PA
227 inferior_thread ()->suspend.stop_pc
228 = regcache_read_pc (get_current_regcache ());
ac264b3b
MS
229 nullify_last_target_wait_ptid ();
230
231 /* Now restore the file positions of open file descriptors. */
232 if (fp->filepos)
233 {
234 for (i = 0; i <= fp->maxfd; i++)
235 if (fp->filepos[i] != (off_t) -1)
236 call_lseek (i, fp->filepos[i], SEEK_SET);
237 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
238 this is native-only. If it ever has to be cross, we'll have
239 to rethink this. */
240 }
241}
242
b7e60d85 243/* Save infrun state for the fork FP. */
ac264b3b 244
2277426b 245static void
b7e60d85 246fork_save_infrun_state (struct fork_info *fp)
ac264b3b 247{
d8d2a3ee 248 char path[PATH_MAX];
ac264b3b
MS
249 struct dirent *de;
250 DIR *d;
251
252 if (fp->savedregs)
c0e383c6 253 delete fp->savedregs;
ac264b3b 254
daf6667d
YQ
255 fp->savedregs = new readonly_detached_regcache (*get_current_regcache ());
256 fp->pc = regcache_read_pc (get_current_regcache ());
ac264b3b 257
b7e60d85
PA
258 /* Now save the 'state' (file position) of all open file descriptors.
259 Unfortunately fork does not take care of that for us... */
260 snprintf (path, PATH_MAX, "/proc/%ld/fd", (long) fp->ptid.pid ());
261 if ((d = opendir (path)) != NULL)
ac264b3b 262 {
b7e60d85
PA
263 long tmp;
264
265 fp->maxfd = 0;
266 while ((de = readdir (d)) != NULL)
ac264b3b 267 {
b7e60d85
PA
268 /* Count open file descriptors (actually find highest
269 numbered). */
270 tmp = strtol (&de->d_name[0], NULL, 10);
271 if (fp->maxfd < tmp)
272 fp->maxfd = tmp;
ac264b3b 273 }
b7e60d85
PA
274 /* Allocate array of file positions. */
275 fp->filepos = XRESIZEVEC (off_t, fp->filepos, fp->maxfd + 1);
276
277 /* Initialize to -1 (invalid). */
278 for (tmp = 0; tmp <= fp->maxfd; tmp++)
279 fp->filepos[tmp] = -1;
280
281 /* Now find actual file positions. */
282 rewinddir (d);
283 while ((de = readdir (d)) != NULL)
284 if (isdigit (de->d_name[0]))
285 {
286 tmp = strtol (&de->d_name[0], NULL, 10);
287 fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR);
288 }
289 closedir (d);
ac264b3b
MS
290 }
291}
292
293/* Kill 'em all, let God sort 'em out... */
294
3cb5bea9 295void
ac264b3b
MS
296linux_fork_killall (void)
297{
298 /* Walk list and kill every pid. No need to treat the
299 current inferior_ptid as special (we do not return a
300 status for it) -- however any process may be a child
301 or a parent, so may get a SIGCHLD from a previously
302 killed child. Wait them all out. */
ac264b3b 303
06974e6c 304 for (fork_info &fi : fork_list)
56aac7e8 305 {
06974e6c
PA
306 pid_t pid = fi.ptid.pid ();
307 int status;
308 pid_t ret;
56aac7e8 309 do {
4c28f408
PA
310 /* Use SIGKILL instead of PTRACE_KILL because the former works even
311 if the thread is running, while the later doesn't. */
312 kill (pid, SIGKILL);
56aac7e8
MS
313 ret = waitpid (pid, &status, 0);
314 /* We might get a SIGCHLD instead of an exit status. This is
315 aggravated by the first kill above - a child has just
316 died. MVS comment cut-and-pasted from linux-nat. */
317 } while (ret == pid && WIFSTOPPED (status));
318 }
06974e6c
PA
319
320 /* Clear list, prepare to start fresh. */
321 fork_list.clear ();
ac264b3b
MS
322}
323
324/* The current inferior_ptid has exited, but there are other viable
325 forks to debug. Delete the exiting one and context-switch to the
326 first available. */
327
3cb5bea9 328void
ac264b3b
MS
329linux_fork_mourn_inferior (void)
330{
2f341b6e
PA
331 struct fork_info *last;
332 int status;
333
ac264b3b
MS
334 /* Wait just one more time to collect the inferior's exit status.
335 Do not check whether this succeeds though, since we may be
336 dealing with a process that we attached to. Such a process will
337 only report its exit status to its original parent. */
e99b03dc 338 waitpid (inferior_ptid.pid (), &status, 0);
ac264b3b
MS
339
340 /* OK, presumably inferior_ptid is the one who has exited.
341 We need to delete that one from the fork_list, and switch
342 to the next available fork. */
343 delete_fork (inferior_ptid);
791b663b
DJ
344
345 /* There should still be a fork - if there's only one left,
346 delete_fork won't remove it, because we haven't updated
347 inferior_ptid yet. */
06974e6c 348 gdb_assert (!fork_list.empty ());
791b663b 349
2f341b6e
PA
350 last = find_last_fork ();
351 fork_load_infrun_state (last);
791b663b 352 printf_filtered (_("[Switching to %s]\n"),
a068643d 353 target_pid_to_str (inferior_ptid).c_str ());
791b663b
DJ
354
355 /* If there's only one fork, switch back to non-fork mode. */
06974e6c 356 if (one_fork_p ())
791b663b 357 delete_fork (inferior_ptid);
ac264b3b
MS
358}
359
7a7d3353
PA
360/* The current inferior_ptid is being detached, but there are other
361 viable forks to debug. Detach and delete it and context-switch to
362 the first available. */
363
3cb5bea9 364void
6bd6f3b6 365linux_fork_detach (int from_tty)
7a7d3353
PA
366{
367 /* OK, inferior_ptid is the one we are detaching from. We need to
368 delete it from the fork_list, and switch to the next available
369 fork. */
370
e99b03dc 371 if (ptrace (PTRACE_DETACH, inferior_ptid.pid (), 0, 0))
a068643d
TT
372 error (_("Unable to detach %s"),
373 target_pid_to_str (inferior_ptid).c_str ());
7a7d3353
PA
374
375 delete_fork (inferior_ptid);
7a7d3353
PA
376
377 /* There should still be a fork - if there's only one left,
378 delete_fork won't remove it, because we haven't updated
379 inferior_ptid yet. */
06974e6c 380 gdb_assert (!fork_list.empty ());
7a7d3353 381
06974e6c 382 fork_load_infrun_state (&fork_list.front ());
7a7d3353
PA
383
384 if (from_tty)
385 printf_filtered (_("[Switching to %s]\n"),
a068643d 386 target_pid_to_str (inferior_ptid).c_str ());
7a7d3353
PA
387
388 /* If there's only one fork, switch back to non-fork mode. */
06974e6c 389 if (one_fork_p ())
7a7d3353
PA
390 delete_fork (inferior_ptid);
391}
392
a07c8880
AB
393/* Temporarily switch to the infrun state stored on the fork_info
394 identified by a given ptid_t. When this object goes out of scope,
395 restore the currently selected infrun state. */
7a298875 396
a07c8880
AB
397class scoped_switch_fork_info
398{
399public:
400 /* Switch to the infrun state held on the fork_info identified by
401 PPTID. If PPTID is the current inferior then no switch is done. */
1ef8573c 402 explicit scoped_switch_fork_info (ptid_t pptid)
a07c8880
AB
403 : m_oldfp (nullptr)
404 {
405 if (pptid != inferior_ptid)
406 {
407 struct fork_info *newfp = nullptr;
408
409 /* Switch to pptid. */
410 m_oldfp = find_fork_ptid (inferior_ptid);
411 gdb_assert (m_oldfp != nullptr);
412 newfp = find_fork_ptid (pptid);
413 gdb_assert (newfp != nullptr);
b7e60d85 414 fork_save_infrun_state (m_oldfp);
a07c8880
AB
415 remove_breakpoints ();
416 fork_load_infrun_state (newfp);
417 insert_breakpoints ();
418 }
419 }
420
421 /* Restore the previously selected infrun state. If the constructor
422 didn't need to switch states, then nothing is done here either. */
423 ~scoped_switch_fork_info ()
424 {
425 if (m_oldfp != nullptr)
426 {
427 /* Switch back to inferior_ptid. */
a70b8144 428 try
1ef8573c
AB
429 {
430 remove_breakpoints ();
431 fork_load_infrun_state (m_oldfp);
432 insert_breakpoints ();
433 }
230d2906 434 catch (const gdb_exception &ex)
1ef8573c
AB
435 {
436 warning (_("Couldn't restore checkpoint state in %s: %s"),
3d6e9d23
TT
437 target_pid_to_str (m_oldfp->ptid).c_str (),
438 ex.what ());
1ef8573c 439 }
a07c8880
AB
440 }
441 }
442
443 DISABLE_COPY_AND_ASSIGN (scoped_switch_fork_info);
444
445private:
446 /* The fork_info for the previously selected infrun state, or nullptr if
447 we were already in the desired state, and nothing needs to be
448 restored. */
449 struct fork_info *m_oldfp;
450};
7a298875
HZ
451
452static int
453inferior_call_waitpid (ptid_t pptid, int pid)
454{
455 struct objfile *waitpid_objf;
456 struct value *waitpid_fn = NULL;
7a298875
HZ
457 int ret = -1;
458
a07c8880 459 scoped_switch_fork_info switch_fork_info (pptid);
e17c9e56 460
7a298875 461 /* Get the waitpid_fn. */
3b7344d5 462 if (lookup_minimal_symbol ("waitpid", NULL, NULL).minsym != NULL)
7a298875 463 waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf);
3b7344d5
TT
464 if (!waitpid_fn
465 && lookup_minimal_symbol ("_waitpid", NULL, NULL).minsym != NULL)
7a298875 466 waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf);
a07c8880
AB
467 if (waitpid_fn != nullptr)
468 {
469 struct gdbarch *gdbarch = get_current_arch ();
470 struct value *argv[3], *retv;
7a298875 471
a07c8880
AB
472 /* Get the argv. */
473 argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid);
474 argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0);
475 argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
7a298875 476
a07c8880 477 retv = call_function_by_hand (waitpid_fn, NULL, argv);
7a298875 478
a07c8880
AB
479 if (value_as_long (retv) >= 0)
480 ret = 0;
481 }
7a298875 482
7a298875
HZ
483 return ret;
484}
485
ac264b3b
MS
486/* Fork list <-> user interface. */
487
488static void
5b64bf74 489delete_checkpoint_command (const char *args, int from_tty)
ac264b3b 490{
e17c9e56 491 ptid_t ptid, pptid;
7a298875 492 struct fork_info *fi;
ac264b3b
MS
493
494 if (!args || !*args)
2277426b 495 error (_("Requires argument (checkpoint id to delete)"));
ac264b3b
MS
496
497 ptid = fork_id_to_ptid (parse_and_eval_long (args));
d7e15655 498 if (ptid == minus_one_ptid)
2277426b 499 error (_("No such checkpoint id, %s"), args);
ac264b3b 500
d7e15655 501 if (ptid == inferior_ptid)
3cb5bea9
PA
502 error (_("\
503Please switch to another checkpoint before deleting the current one"));
ac264b3b 504
e99b03dc 505 if (ptrace (PTRACE_KILL, ptid.pid (), 0, 0))
a068643d 506 error (_("Unable to kill pid %s"), target_pid_to_str (ptid).c_str ());
ac264b3b 507
7a298875
HZ
508 fi = find_fork_ptid (ptid);
509 gdb_assert (fi);
e17c9e56 510 pptid = fi->parent_ptid;
7a298875 511
ac264b3b 512 if (from_tty)
a068643d 513 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid).c_str ());
ac264b3b
MS
514
515 delete_fork (ptid);
7a298875
HZ
516
517 /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint
518 list, waitpid the ptid.
00431a78 519 If fi->parent_ptid is a part of lwp and it is stopped, waitpid the
7a298875 520 ptid. */
5b6d1e4f 521 thread_info *parent = find_thread_ptid (linux_target, pptid);
00431a78
PA
522 if ((parent == NULL && find_fork_ptid (pptid))
523 || (parent != NULL && parent->state == THREAD_STOPPED))
7a298875 524 {
e99b03dc 525 if (inferior_call_waitpid (pptid, ptid.pid ()))
a068643d
TT
526 warning (_("Unable to wait pid %s"),
527 target_pid_to_str (ptid).c_str ());
7a298875 528 }
ac264b3b
MS
529}
530
531static void
5b64bf74 532detach_checkpoint_command (const char *args, int from_tty)
ac264b3b
MS
533{
534 ptid_t ptid;
535
536 if (!args || !*args)
2277426b 537 error (_("Requires argument (checkpoint id to detach)"));
ac264b3b
MS
538
539 ptid = fork_id_to_ptid (parse_and_eval_long (args));
d7e15655 540 if (ptid == minus_one_ptid)
2277426b 541 error (_("No such checkpoint id, %s"), args);
ac264b3b 542
d7e15655 543 if (ptid == inferior_ptid)
2277426b
PA
544 error (_("\
545Please switch to another checkpoint before detaching the current one"));
ac264b3b 546
e99b03dc 547 if (ptrace (PTRACE_DETACH, ptid.pid (), 0, 0))
a068643d 548 error (_("Unable to detach %s"), target_pid_to_str (ptid).c_str ());
ac264b3b
MS
549
550 if (from_tty)
a068643d 551 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid).c_str ());
ac264b3b
MS
552
553 delete_fork (ptid);
554}
555
3cb5bea9 556/* Print information about currently known checkpoints. */
ac264b3b
MS
557
558static void
1d12d88f 559info_checkpoints_command (const char *arg, int from_tty)
ac264b3b 560{
5af949e3 561 struct gdbarch *gdbarch = get_current_arch ();
b8db102d 562 int requested = -1;
06974e6c 563 const fork_info *printed = NULL;
b8db102d
MS
564
565 if (arg && *arg)
566 requested = (int) parse_and_eval_long (arg);
ac264b3b 567
06974e6c 568 for (const fork_info &fi : fork_list)
ac264b3b 569 {
06974e6c 570 if (requested > 0 && fi.num != requested)
b8db102d
MS
571 continue;
572
06974e6c
PA
573 printed = &fi;
574 if (fi.ptid == inferior_ptid)
daf6667d 575 printf_filtered ("* ");
ac264b3b 576 else
daf6667d
YQ
577 printf_filtered (" ");
578
06974e6c 579 ULONGEST pc = fi.pc;
a068643d 580 printf_filtered ("%d %s", fi.num, target_pid_to_str (fi.ptid).c_str ());
06974e6c 581 if (fi.num == 0)
ac264b3b
MS
582 printf_filtered (_(" (main process)"));
583 printf_filtered (_(" at "));
5af949e3 584 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
ac264b3b 585
06974e6c 586 symtab_and_line sal = find_pc_line (pc, 0);
ac264b3b 587 if (sal.symtab)
05cba821
JK
588 printf_filtered (_(", file %s"),
589 symtab_to_filename_for_display (sal.symtab));
ac264b3b
MS
590 if (sal.line)
591 printf_filtered (_(", line %d"), sal.line);
592 if (!sal.symtab && !sal.line)
593 {
7cbd4a93 594 struct bound_minimal_symbol msym;
ac264b3b
MS
595
596 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 597 if (msym.minsym)
c9d95fa3 598 printf_filtered (", <%s>", msym.minsym->linkage_name ());
ac264b3b
MS
599 }
600
601 putchar_filtered ('\n');
602 }
b8db102d
MS
603 if (printed == NULL)
604 {
605 if (requested > 0)
2277426b 606 printf_filtered (_("No checkpoint number %d.\n"), requested);
b8db102d 607 else
2277426b 608 printf_filtered (_("No checkpoints.\n"));
b8db102d 609 }
ac264b3b
MS
610}
611
2277426b
PA
612/* The PID of the process we're checkpointing. */
613static int checkpointing_pid = 0;
ac264b3b 614
2277426b
PA
615int
616linux_fork_checkpointing_p (int pid)
ac264b3b 617{
2277426b 618 return (checkpointing_pid == pid);
ac264b3b
MS
619}
620
92f6badc
KP
621/* Return true if the current inferior is multi-threaded. */
622
e52c971f
PA
623static bool
624inf_has_multiple_threads ()
92f6badc
KP
625{
626 int count = 0;
627
e52c971f
PA
628 /* Return true as soon as we see the second thread of the current
629 inferior. */
630 for (thread_info *tp ATTRIBUTE_UNUSED : current_inferior ()->threads ())
631 if (++count > 1)
632 return true;
633
634 return false;
92f6badc
KP
635}
636
ac264b3b 637static void
0b39b52e 638checkpoint_command (const char *args, int from_tty)
ac264b3b 639{
3e3b026f
UW
640 struct objfile *fork_objf;
641 struct gdbarch *gdbarch;
ac264b3b
MS
642 struct target_waitstatus last_target_waitstatus;
643 ptid_t last_target_ptid;
644 struct value *fork_fn = NULL, *ret;
645 struct fork_info *fp;
646 pid_t retpid;
74960c60 647
92f6badc
KP
648 if (!target_has_execution)
649 error (_("The program is not being run."));
650
651 /* Ensure that the inferior is not multithreaded. */
652 update_thread_list ();
653 if (inf_has_multiple_threads ())
654 error (_("checkpoint: can't checkpoint multiple threads."));
655
ac264b3b
MS
656 /* Make the inferior fork, record its (and gdb's) state. */
657
3b7344d5 658 if (lookup_minimal_symbol ("fork", NULL, NULL).minsym != NULL)
3e3b026f 659 fork_fn = find_function_in_inferior ("fork", &fork_objf);
ac264b3b 660 if (!fork_fn)
3b7344d5 661 if (lookup_minimal_symbol ("_fork", NULL, NULL).minsym != NULL)
3e3b026f 662 fork_fn = find_function_in_inferior ("fork", &fork_objf);
ac264b3b
MS
663 if (!fork_fn)
664 error (_("checkpoint: can't find fork function in inferior."));
665
08feed99 666 gdbarch = fork_objf->arch ();
3e3b026f 667 ret = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
2277426b
PA
668
669 /* Tell linux-nat.c that we're checkpointing this inferior. */
b7b633e9
TT
670 {
671 scoped_restore save_pid
e99b03dc 672 = make_scoped_restore (&checkpointing_pid, inferior_ptid.pid ());
b7b633e9 673
e71585ff 674 ret = call_function_by_hand (fork_fn, NULL, {});
b7b633e9 675 }
2277426b 676
ac264b3b
MS
677 if (!ret) /* Probably can't happen. */
678 error (_("checkpoint: call_function_by_hand returned null."));
679
680 retpid = value_as_long (ret);
5b6d1e4f 681 get_last_target_status (nullptr, &last_target_ptid, &last_target_waitstatus);
6f9d33d8
PP
682
683 fp = find_fork_pid (retpid);
684
ac264b3b
MS
685 if (from_tty)
686 {
687 int parent_pid;
688
6f9d33d8
PP
689 printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"),
690 fp != NULL ? fp->num : -1, (long) retpid);
ac264b3b
MS
691 if (info_verbose)
692 {
e38504b3 693 parent_pid = last_target_ptid.lwp ();
ac264b3b 694 if (parent_pid == 0)
e99b03dc 695 parent_pid = last_target_ptid.pid ();
3cb5bea9 696 printf_filtered (_(" gdb says parent = %ld.\n"),
ac264b3b
MS
697 (long) parent_pid);
698 }
699 }
700
ac264b3b
MS
701 if (!fp)
702 error (_("Failed to find new fork"));
72f31aea 703
06974e6c 704 if (one_fork_p ())
72f31aea
PA
705 {
706 /* Special case -- if this is the first fork in the list (the
707 list was hitherto empty), then add inferior_ptid first, as a
708 special zeroeth fork id. */
06974e6c 709 fork_list.emplace_front (inferior_ptid.pid ());
72f31aea
PA
710 }
711
b7e60d85 712 fork_save_infrun_state (fp);
7a298875 713 fp->parent_ptid = last_target_ptid;
ac264b3b
MS
714}
715
716static void
717linux_fork_context (struct fork_info *newfp, int from_tty)
718{
719 /* Now we attempt to switch processes. */
0d14fc63 720 struct fork_info *oldfp;
ac264b3b 721
0d14fc63 722 gdb_assert (newfp != NULL);
ac264b3b 723
0d14fc63
PA
724 oldfp = find_fork_ptid (inferior_ptid);
725 gdb_assert (oldfp != NULL);
ac264b3b 726
b7e60d85 727 fork_save_infrun_state (oldfp);
74960c60 728 remove_breakpoints ();
ac264b3b 729 fork_load_infrun_state (newfp);
74960c60 730 insert_breakpoints ();
ac264b3b 731
3cb5bea9 732 printf_filtered (_("Switching to %s\n"),
a068643d 733 target_pid_to_str (inferior_ptid).c_str ());
ac264b3b 734
08d72866 735 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
ac264b3b
MS
736}
737
2277426b 738/* Switch inferior process (checkpoint) context, by checkpoint id. */
ac264b3b 739static void
0b39b52e 740restart_command (const char *args, int from_tty)
ac264b3b
MS
741{
742 struct fork_info *fp;
743
744 if (!args || !*args)
745 error (_("Requires argument (checkpoint id to restart)"));
746
747 if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL)
748 error (_("Not found: checkpoint id %s"), args);
749
750 linux_fork_context (fp, from_tty);
751}
752
6c265988 753void _initialize_linux_fork ();
ac264b3b 754void
6c265988 755_initialize_linux_fork ()
ac264b3b 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.293477 seconds and 4 git commands to generate.