* [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors
@ 2024-12-10 1:54 Kevin Buettner
2024-12-10 1:54 ` [PATCH v5 1/4] " Kevin Buettner
` (5 more replies)
0 siblings, 6 replies; 10+ messages in thread
From: Kevin Buettner @ 2024-12-10 1:54 UTC (permalink / raw)
To: gdb-patches; +Cc: pedro, Kevin Buettner
This series fixes some problems with the current checkpoint code. The
first patch makes the checkpoint code inferior aware, fixing a number
of bugs. The second and third patches are largely cosmetic - they
make changes to checkpoint related output. The fourth patch updates
the documentation.
The v2 series incorporated Pedro's suggestions regarding the
numbering of checkpoint ids. See the first patch for details. The
tests have been revised to account for these changes and new tests
have been added as well.
The v3 series split out a cosmetic change from the first patch. It
capitalized the output of a successful checkpoint command. This was
prompted by the Linaro regression tester, which, due to the
capitalization change, found two regressions in
gdb.base/kill-during-detach.exp. (I had made a mistake during my own
testing causing this to not be caught.)
The v4 series addressed Pedro's concerns from his review of the v3
series. It also adds a NEWS entry and updates the GDB manual with
regard to checkpoint identifiers.
This v5 series makes several changes to the output for the
"info checkpoints" command. As a consequence, both checkpoint related
tests as well as the documentation were adjusted for these changes.
Kevin Buettner (4):
Make linux checkpoints work with multiple inferiors
Capitalize output of successful checkpoint command
Print only process ptids from linux-fork.c
Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple
inferiors
gdb/NEWS | 2 +
gdb/doc/gdb.texinfo | 20 +-
gdb/linux-fork.c | 590 +++++++++----
gdb/linux-fork.h | 15 +-
gdb/linux-nat.c | 18 +-
gdb/testsuite/gdb.base/checkpoint.exp | 26 +-
gdb/testsuite/gdb.base/kill-during-detach.exp | 2 +-
gdb/testsuite/gdb.multi/checkpoint-multi.exp | 800 ++++++++++++++++++
8 files changed, 1277 insertions(+), 196 deletions(-)
create mode 100644 gdb/testsuite/gdb.multi/checkpoint-multi.exp
--
2.46.2
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v5 1/4] Make linux checkpoints work with multiple inferiors 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner @ 2024-12-10 1:54 ` Kevin Buettner 2025-01-30 17:45 ` Andrew Burgess 2024-12-10 1:54 ` [PATCH v5 2/4] Capitalize output of successful checkpoint command Kevin Buettner ` (4 subsequent siblings) 5 siblings, 1 reply; 10+ messages in thread From: Kevin Buettner @ 2024-12-10 1:54 UTC (permalink / raw) To: gdb-patches; +Cc: pedro, Kevin Buettner The current linux checkpoint code, most of which may be found in linux-fork.c, is quite broken when attempting to use more than one inferior. Running GDB will show internal errors when starting two inferiors, placing a checkpoint in one, then switching to the other and doing one of the following commands, "restart", "detach", "kill", or continue (to program exit). Test cases for two of those scenarios may be found in this bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31065 I've tested for each of the scenarios and many more in the new test case, gdb.multi/checkpoint-multi.exp. I started off with the goal of fixing just those problems, and was mostly successful with a much smaller patch, but doing "info checkpoints" with more than one inferior didn't work correctly due to some of the inferiors being in the wrong program space. That led me to making the linux-fork code fully inferior-aware. Prior to this commit, the list of forks was being maintained in a global named named 'fork_list'. I turned this into a per-inferior data structure. There was also global named 'highest_fork_num' which is also now part of the per-inferior struct. A registry key named 'checkpoint_inferior_data_key' along with function 'get_checkpoint_inferior_data' is used to access the per-inferior data. This new function, get_checkpoint_inferior_data, is only called by the new functions 'fork_list', 'reset_highest_fork_num', and increment_highest_fork_num, each of which is passed a pointer to the inferior. Most occurrences referring to the (previously) global 'fork_list' have been replaced by 'fork_list (inf)'. In some functions, where the 'fork_list' is referenced multiple times, a local named 'fork_list' is declared and initialized instead, like this: auto &fork_list = ::fork_list (inf); The constructor for 'struct fork_info' has gained an additional parameter. In addition to passing the pid of the new fork, we now also pass the fork identifier, fork_num, to the constructor. This integer is shown to the user in the "info checkpoints" command and is provided by the user, perhaps in conjunction with the inferior number, in commands which manipulate checkpoints, e.g. 'restart' and 'delete checkpoint'. When checkpoints are used in only one inferior, this commit will present information to the user and will accept checkpoint identifiers to commands in much the same way as the code did before this commit. Per Pedro Alves's recommendations, the "info checkpoints" command has been changed somewhat. "info checkpoints" used to display "(main process)" for the first process in the checkpoint list. This is no longer done because it does not provide useful information. It also used to display "<running>", when the process is running and no useful frame information may be displayed. This has been changed to "(running)" in order to be more consistent with the output of the "info threads" command. A new column has been added to the output for showing the active process in the output from "info checkpoints". This column will display 'y' for the active process and 'n' for the others. For the active inferior a '*' is also printed preceding the checkpoint identifier. Here's what things look(ed) like before and after for just one inferior: Before: (gdb) info checkpoints * 0 Thread 0x7ffff7cd3740 (LWP 84201) (main process) at 0x40114a, file hello.c, line 28 1 process 84205 at 0x401199, file hello.c, line 51 2 process 84206 at 0x4011a3, file hello.c, line 53 After: (gdb) info checkpoints Id Active Target Id Frame * 0 y process 551311 at 0x40114a, file hello.c, line 28 1 n process 551314 at 0x401199, file hello.c, line 51 2 n process 551315 at 0x4011a3, file hello.c, line 53 (The Thread versus process distinction is handled by another patch - the "After" example assumes that patch is applied too.) When there are multiple inferiors, the "info checkpoints" output looks like this: (gdb) info checkpoints Id Active Target Id Frame 1.0 y process 535276 at 0x401199, file hello.c, line 51 1.1 n process 535283 at 0x401199, file hello.c, line 51 1.2 n process 535288 at 0x401199, file hello.c, line 51 2.1 n process 535280 at 0x401258, file goodbye.c, line 62 2.2 y process 535284 at 0x401258, file goodbye.c, line 62 * 3.0 y process 535285 at 0x40115c, file hangout.c, line 31 3.2 n process 535287 at 0x40115c, file hangout.c, line 31 A new function named 'parse_checkpoint_id' has been added. As its name suggests, it's responsible for parsing a string representing a checkpoint identifier. These identifiers may be either a decimal number representing the checkpoint number in the current inferior or two decimal numbers separated by '.', in which case the first is the inferior number and the second is the checkpoint number in that inferior. It is called by delete_checkpoint_command, detach_checkpoint_command, info_checkpoints_command, and restart_command. Calls to 'parse_checkpoint_id' replace calls to 'parse_and_eval_long', plus error checking and error reporting code near the calls to 'parse_and_eval_long'. As such, error checking and reporting has been consolidated into a single function and the messages output are more uniform, though this has necessitated changes to the existing test case gdb.base/checkpoint.exp. The functions 'find_fork_ptid' and 'find_fork_pid' used to return a pointer to a fork_info struct. They now return a pair consisting of the pointer to a fork_info struct in addition to a pointer to the inferior containing that checkpoint. 'find_fork_id' returns a pointer to a fork_info struct just as it did before, but it's now gained a new parameter, 'inf', which is the inferior in which to look. info_checkpoints_command used to simply iterate over the list of forks (checkpoints), printing each one out. It now needs to iterate over all inferiors and, for those which have checkpoints, it needs to iterate over the list of checkpoints in that inferior. As noted earlier, the format of the output has been changed so that checkpoint identifiers incorporating an inferior number may be printed. linux_fork_context, called by restart_command, now contains code to switch inferiors when the fork being restarted is in an inferior which is different from the current one. The scoped_switch_fork_info class now also contains code for switching inferiors in both the constructor and destructor. gdb/linux-nat.c has a few changes. All but one of them are related to passing the inferior to one of the linux-fork functions. But one of the tests in linux_nat_target::detach has also changed in a non-obvious way. In attempting to determine whether to call linux_fork_detach(), that code used to do: if (pid == inferior_ptid.pid () && forks_exist_p ()) It's been simplified to: if (forks_exist_p (inf)) I had added the 'pid == inferior_ptid.pid ()' condition in late 2023 while working on a detach bug. It was kind of a hack to prevent calling linux_fork_detach() when in a different inferior. That's no longer needed since the call to forks_exist_p does this directly - i.e. it is now inferior-aware. Finally, the header file 'linux-fork.h' has been updated to reflect the fact that add_fork, linux_fork_killall, linux_fork_detach, and forks_exist_p all now require that a pointer to an inferior be passed to these functions. Additionally (as mentioned earlier), find_fork_pid now returns std::pair<fork_info *, inferior *> instead 'of fork_info *'. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31065 --- gdb/linux-fork.c | 557 +++++++++---- gdb/linux-fork.h | 15 +- gdb/linux-nat.c | 18 +- gdb/testsuite/gdb.base/checkpoint.exp | 24 +- gdb/testsuite/gdb.multi/checkpoint-multi.exp | 800 +++++++++++++++++++ 5 files changed, 1230 insertions(+), 184 deletions(-) create mode 100644 gdb/testsuite/gdb.multi/checkpoint-multi.exp diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c index 7d3505b5948..49c2cf1ed84 100644 --- a/gdb/linux-fork.c +++ b/gdb/linux-fork.c @@ -29,6 +29,7 @@ #include "linux-nat.h" #include "gdbthread.h" #include "source.h" +#include "progspace-and-thread.h" #include "nat/gdb_ptrace.h" #include "gdbsupport/gdb_wait.h" @@ -40,10 +41,11 @@ #include <list> /* Fork list data structure: */ + struct fork_info { - explicit fork_info (pid_t pid) - : ptid (pid, pid) + explicit fork_info (pid_t pid, int fork_num) + : ptid (pid, pid), num (fork_num) { } @@ -71,7 +73,7 @@ struct fork_info ptid_t parent_ptid = null_ptid; /* Convenient handle (GDB fork id). */ - int num = 0; + int num; /* Convenient for info fork, saves having to actually switch contexts. */ @@ -85,115 +87,247 @@ struct fork_info int maxfd = 0; }; -static std::list<fork_info> fork_list; -static int highest_fork_num; +/* Per-inferior checkpoint data. */ + +struct checkpoint_inferior_data +{ + /* List of forks (checkpoints) in particular inferior. Once a + checkpoint has been created, fork_list will contain at least two + items, the first in the list will be the original (or, if not + original, then the oldest) fork. */ + std::list<fork_info> fork_list; + + /* Most recently assigned fork number; when 0, no checkpoints have + been created yet. */ + int highest_fork_num = 0; +}; + +/* Per-inferior data key. */ + +static const registry<inferior>::key<checkpoint_inferior_data> + checkpoint_inferior_data_key; + +/* Fetch per-inferior checkpoint data. It always returns a valid pointer + to a checkpoint_inferior_info struct. */ + +static struct checkpoint_inferior_data * +get_checkpoint_inferior_data (struct inferior *inf) +{ + struct checkpoint_inferior_data *data; + + data = checkpoint_inferior_data_key.get (inf); + if (data == nullptr) + data = checkpoint_inferior_data_key.emplace (inf); + + return data; +} + +/* Return a reference to the per-inferior fork list. */ + +static std::list<fork_info> & +fork_list (inferior *inf) +{ + return get_checkpoint_inferior_data (inf)->fork_list; +} + +/* Increment the highest fork number for inferior INF, returning + the new value. */ + +static int +increment_highest_fork_num (inferior *inf) +{ + return ++get_checkpoint_inferior_data (inf)->highest_fork_num; +} + +/* Reset the highest fork number for inferior INF. */ + +static void +reset_highest_fork_num (inferior *inf) +{ + get_checkpoint_inferior_data (inf)->highest_fork_num = 0; +} /* Fork list methods: */ -int -forks_exist_p (void) +/* Predicate which returns true if checkpoint(s) exist in the inferior + INF, false otherwise. */ + +bool +forks_exist_p (inferior *inf) { - return !fork_list.empty (); + /* Avoid allocating checkpoint_inferior_data storage by checking + to see if such storage exists prior to calling fork_list. + If we just call fork_list alone, then that call will create + this storage, even for inferiors which don't need it. */ + return (checkpoint_inferior_data_key.get (inf) != nullptr + && !fork_list (inf).empty ()); } -/* Return the last fork in the list. */ +/* Return the last fork in the list for inferior INF. */ static struct fork_info * -find_last_fork (void) +find_last_fork (inferior *inf) { + auto &fork_list = ::fork_list (inf); + if (fork_list.empty ()) return NULL; return &fork_list.back (); } -/* Return true iff there's one fork in the list. */ +/* Return true iff there's one fork in the list for inferior INF. */ static bool -one_fork_p () +one_fork_p (inferior *inf) { - return fork_list.size () == 1; + return fork_list (inf).size () == 1; } /* Add a new fork to the internal fork list. */ void -add_fork (pid_t pid) +add_fork (pid_t pid, inferior *inf) { - fork_list.emplace_back (pid); - - if (one_fork_p ()) - highest_fork_num = 0; - - fork_info *fp = &fork_list.back (); - fp->num = ++highest_fork_num; + fork_list (inf).emplace_back (pid, increment_highest_fork_num (inf)); } +/* Delete a fork for PTID in inferior INF. When the last fork is + deleted, HIGHEST_FORK_NUM for the given inferior is reset to 0. + The fork list may also be made to be empty when only one fork + remains. */ + static void -delete_fork (ptid_t ptid) +delete_fork (ptid_t ptid, inferior *inf) { linux_target->low_forget_process (ptid.pid ()); + auto &fork_list = ::fork_list (inf); for (auto it = fork_list.begin (); it != fork_list.end (); ++it) if (it->ptid == ptid) { fork_list.erase (it); + if (fork_list.empty ()) + reset_highest_fork_num (inf); + /* Special case: if there is now only one process in the list, and if it is (hopefully!) the current inferior_ptid, then remove it, leaving the list empty -- we're now down to the default case of debugging a single process. */ - if (one_fork_p () && fork_list.front ().ptid == inferior_ptid) + if (one_fork_p (inf) && fork_list.front ().ptid == inferior_ptid) { /* Last fork -- delete from list and handle as solo process (should be a safe recursion). */ - delete_fork (inferior_ptid); + delete_fork (inferior_ptid, inf); } return; } } -/* Find a fork_info by matching PTID. */ -static struct fork_info * +/* Find a fork_info and inferior by matching PTID. */ + +static std::pair<fork_info *, inferior *> find_fork_ptid (ptid_t ptid) { - for (fork_info &fi : fork_list) - if (fi.ptid == ptid) - return &fi; + for (inferior *inf : all_inferiors (linux_target)) + { + for (fork_info &fi : fork_list (inf)) + if (fi.ptid == ptid) + return { &fi, inf }; + } - return NULL; + return { nullptr, nullptr }; } -/* Find a fork_info by matching ID. */ -static struct fork_info * -find_fork_id (int num) +/* Find a fork_info by matching NUM in inferior INF. */ + +static fork_info * +find_fork_id (inferior *inf, int num) { - for (fork_info &fi : fork_list) + for (fork_info &fi : fork_list (inf)) if (fi.num == num) return &fi; - return NULL; + return nullptr; } -/* Find a fork_info by matching pid. */ -extern struct fork_info * +/* Find a fork_info and inferior by matching pid. */ + +extern std::pair<fork_info *, inferior *> find_fork_pid (pid_t pid) { - for (fork_info &fi : fork_list) - if (pid == fi.ptid.pid ()) - return &fi; + for (inferior *inf : all_inferiors (linux_target)) + { + for (fork_info &fi : fork_list (inf)) + if (pid == fi.ptid.pid ()) + return { &fi, inf }; + } - return NULL; + return { nullptr, nullptr }; } -static ptid_t -fork_id_to_ptid (int num) +/* Parse a command argument representing a checkpoint id. This + can take one of two forms: + + Num + + -or- + + Inf.Num + + where Num is a non-negative decimal integer and Inf, if present, is + a positive decimal integer. + + Return a pair with a pointer to the fork_info struct and pointer + to the inferior. This function will throw an error if there's + a problem with the parsing or if either the inferior or checkpoint + id does not exist. */ + +static std::pair<fork_info *, inferior *> +parse_checkpoint_id (const char *ckptstr) { - struct fork_info *fork = find_fork_id (num); - if (fork) - return fork->ptid; + const char *number = ckptstr; + const char *p1; + struct inferior *inf; + + const char *dot = strchr (number, '.'); + + if (dot != nullptr) + { + /* Parse number to the left of the dot. */ + int inf_num; + + p1 = number; + inf_num = get_number_trailer (&p1, '.'); + if (inf_num <= 0) + error (_("Inferior number must be a positive integer")); + + inf = find_inferior_id (inf_num); + if (inf == NULL) + error (_("No inferior number '%d'"), inf_num); + + p1 = dot + 1; + } else - return ptid_t (-1); + { + inf = current_inferior (); + p1 = number; + } + + int fork_num = get_number_trailer (&p1, 0); + if (fork_num < 0) + error (_("Checkpoint number must be a non-negative integer")); + + if (!forks_exist_p (inf)) + error (_("Inferior %d has no checkpoints"), inf->num); + + fork_info *fork_ptr = find_fork_id (inf, fork_num); + if (fork_ptr == nullptr) + error (_("Invalid checkpoint number %d for inferior %d"), + fork_num, inf->num); + + return { fork_ptr, inf }; } /* Fork list <-> gdb interface. */ @@ -298,7 +432,7 @@ fork_save_infrun_state (struct fork_info *fp) /* Kill 'em all, let God sort 'em out... */ void -linux_fork_killall (void) +linux_fork_killall (inferior *inf) { /* Walk list and kill every pid. No need to treat the current inferior_ptid as special (we do not return a @@ -306,6 +440,7 @@ linux_fork_killall (void) or a parent, so may get a SIGCHLD from a previously killed child. Wait them all out. */ + auto &fork_list = ::fork_list (inf); for (fork_info &fi : fork_list) { pid_t pid = fi.ptid.pid (); @@ -324,6 +459,7 @@ linux_fork_killall (void) /* Clear list, prepare to start fresh. */ fork_list.clear (); + reset_highest_fork_num (inf); } /* The current inferior_ptid has exited, but there are other viable @@ -331,10 +467,11 @@ linux_fork_killall (void) first available. */ void -linux_fork_mourn_inferior (void) +linux_fork_mourn_inferior () { struct fork_info *last; int status; + inferior *inf = current_inferior (); /* Wait just one more time to collect the inferior's exit status. Do not check whether this succeeds though, since we may be @@ -343,23 +480,23 @@ linux_fork_mourn_inferior (void) gdb::waitpid (inferior_ptid.pid (), &status, 0); /* OK, presumably inferior_ptid is the one who has exited. - We need to delete that one from the fork_list, and switch + We need to delete that one from the fork list, and switch to the next available fork. */ - delete_fork (inferior_ptid); + delete_fork (inferior_ptid, inf); /* There should still be a fork - if there's only one left, delete_fork won't remove it, because we haven't updated inferior_ptid yet. */ - gdb_assert (!fork_list.empty ()); + gdb_assert (!fork_list (inf).empty ()); - last = find_last_fork (); + last = find_last_fork (inf); fork_load_infrun_state (last); gdb_printf (_("[Switching to %s]\n"), target_pid_to_str (inferior_ptid).c_str ()); /* If there's only one fork, switch back to non-fork mode. */ - if (one_fork_p ()) - delete_fork (inferior_ptid); + if (one_fork_p (inf)) + delete_fork (inferior_ptid, inf); } /* The current inferior_ptid is being detached, but there are other @@ -367,13 +504,13 @@ linux_fork_mourn_inferior (void) the first available. */ void -linux_fork_detach (int from_tty, lwp_info *lp) +linux_fork_detach (int from_tty, lwp_info *lp, inferior *inf) { gdb_assert (lp != nullptr); gdb_assert (lp->ptid == inferior_ptid); /* OK, inferior_ptid is the one we are detaching from. We need to - delete it from the fork_list, and switch to the next available + delete it from the fork list, and switch to the next available fork. But before doing the detach, do make sure that the lwp hasn't exited or been terminated first. */ @@ -386,11 +523,12 @@ linux_fork_detach (int from_tty, lwp_info *lp) target_pid_to_str (inferior_ptid).c_str ()); } - delete_fork (inferior_ptid); + delete_fork (inferior_ptid, inf); /* There should still be a fork - if there's only one left, delete_fork won't remove it, because we haven't updated inferior_ptid yet. */ + auto &fork_list = ::fork_list (inf); gdb_assert (!fork_list.empty ()); fork_load_infrun_state (&fork_list.front ()); @@ -400,8 +538,8 @@ linux_fork_detach (int from_tty, lwp_info *lp) target_pid_to_str (inferior_ptid).c_str ()); /* If there's only one fork, switch back to non-fork mode. */ - if (one_fork_p ()) - delete_fork (inferior_ptid); + if (one_fork_p (inf)) + delete_fork (inferior_ptid, inf); } /* Temporarily switch to the infrun state stored on the fork_info @@ -414,19 +552,26 @@ class scoped_switch_fork_info /* Switch to the infrun state held on the fork_info identified by PPTID. If PPTID is the current inferior then no switch is done. */ explicit scoped_switch_fork_info (ptid_t pptid) - : m_oldfp (nullptr) + : m_oldfp (nullptr), m_oldinf (nullptr) { if (pptid != inferior_ptid) { - struct fork_info *newfp = nullptr; - /* Switch to pptid. */ - m_oldfp = find_fork_ptid (inferior_ptid); + auto [oldfp, oldinf] = find_fork_ptid (inferior_ptid); + m_oldfp = oldfp; gdb_assert (m_oldfp != nullptr); - newfp = find_fork_ptid (pptid); + auto [newfp, newinf] = find_fork_ptid (pptid); gdb_assert (newfp != nullptr); fork_save_infrun_state (m_oldfp); remove_breakpoints (); + + if (oldinf != newinf) + { + thread_info *tp = any_thread_of_inferior (newinf); + switch_to_thread (tp); + m_oldinf = oldinf; + } + fork_load_infrun_state (newfp); insert_breakpoints (); } @@ -436,12 +581,17 @@ class scoped_switch_fork_info didn't need to switch states, then nothing is done here either. */ ~scoped_switch_fork_info () { - if (m_oldfp != nullptr) + if (m_oldinf != nullptr || m_oldfp != nullptr) { /* Switch back to inferior_ptid. */ try { remove_breakpoints (); + if (m_oldinf != nullptr) + { + thread_info *tp = any_thread_of_inferior (m_oldinf); + switch_to_thread (tp); + } fork_load_infrun_state (m_oldfp); insert_breakpoints (); } @@ -473,8 +623,16 @@ class scoped_switch_fork_info we were already in the desired state, and nothing needs to be restored. */ struct fork_info *m_oldfp; + + /* When switching to a different fork, this is the inferior for the + fork that we're switching from, and to which we'll switch back once + end-of-scope is reached. It may also be nullptr if no switching + is required. */ + inferior *m_oldinf; }; +/* Call waitpid() by making an inferior function call. */ + static int inferior_call_waitpid (ptid_t pptid, int pid) { @@ -517,30 +675,25 @@ static void delete_checkpoint_command (const char *args, int from_tty) { ptid_t ptid, pptid; - struct fork_info *fi; if (!args || !*args) error (_("Requires argument (checkpoint id to delete)")); - ptid = fork_id_to_ptid (parse_and_eval_long (args)); - if (ptid == minus_one_ptid) - error (_("No such checkpoint id, %s"), args); + auto [fi, inf] = parse_checkpoint_id (args); + ptid = fi->ptid; + gdb_assert (fi != nullptr); + pptid = fi->parent_ptid; - if (ptid == inferior_ptid) - error (_("\ -Please switch to another checkpoint before deleting the current one")); + if (ptid.pid () == inf->pid) + error (_("Cannot delete active checkpoint")); if (ptrace (PTRACE_KILL, ptid.pid (), 0, 0)) error (_("Unable to kill pid %s"), target_pid_to_str (ptid).c_str ()); - fi = find_fork_ptid (ptid); - gdb_assert (fi); - pptid = fi->parent_ptid; - if (from_tty) gdb_printf (_("Killed %s\n"), target_pid_to_str (ptid).c_str ()); - delete_fork (ptid); + delete_fork (ptid, inf); if (pptid == null_ptid) { @@ -558,7 +711,7 @@ Please switch to another checkpoint before deleting the current one")); If fi->parent_ptid is a part of lwp and it is stopped, waitpid the ptid. */ thread_info *parent = linux_target->find_thread (pptid); - if ((parent == NULL && find_fork_ptid (pptid)) + if ((parent == NULL && find_fork_ptid (pptid).first != nullptr) || (parent != NULL && parent->state == THREAD_STOPPED)) { if (inferior_call_waitpid (pptid, ptid.pid ())) @@ -575,9 +728,8 @@ detach_checkpoint_command (const char *args, int from_tty) if (!args || !*args) error (_("Requires argument (checkpoint id to detach)")); - ptid = fork_id_to_ptid (parse_and_eval_long (args)); - if (ptid == minus_one_ptid) - error (_("No such checkpoint id, %s"), args); + auto [fi, inf] = parse_checkpoint_id (args); + ptid = fi->ptid; if (ptid == inferior_ptid) error (_("\ @@ -589,7 +741,7 @@ Please switch to another checkpoint before detaching the current one")); if (from_tty) gdb_printf (_("Detached %s\n"), target_pid_to_str (ptid).c_str ()); - delete_fork (ptid); + delete_fork (ptid, current_inferior ()); } /* Print information about currently known checkpoints. */ @@ -598,70 +750,139 @@ static void info_checkpoints_command (const char *arg, int from_tty) { struct gdbarch *gdbarch = get_current_arch (); - int requested = -1; - bool printed = false; + struct inferior *cur_inf = current_inferior (); + inferior *req_inf = nullptr; + fork_info *req_fi = nullptr; + bool will_print_something = false; if (arg && *arg) - requested = (int) parse_and_eval_long (arg); + std::tie (req_fi, req_inf) = parse_checkpoint_id (arg); + + /* Figure out whether to print the inferior number in the + checkpoint list. */ + bool print_inf = (number_of_inferiors () > 1); - for (const fork_info &fi : fork_list) + /* Compute widths of some of the table components. */ + size_t inf_width = 0; + size_t num_width = 0; + size_t targid_width = 0; + for (inferior *inf : all_inferiors (linux_target)) { - if (requested > 0 && fi.num != requested) + if (req_inf != nullptr && req_inf != inf) continue; - printed = true; - bool is_current = fi.ptid == inferior_ptid; - if (is_current) - gdb_printf ("* "); - else - gdb_printf (" "); + scoped_restore_current_pspace_and_thread restore_pspace_thread; + switch_to_program_space_and_thread (inf->pspace); - gdb_printf ("%d %s", fi.num, target_pid_to_str (fi.ptid).c_str ()); - if (fi.num == 0) - gdb_printf (_(" (main process)")); - - if (is_current && inferior_thread ()->state == THREAD_RUNNING) + for (const fork_info &fi : fork_list (inf)) { - gdb_printf (_(" <running>\n")); - continue; - } - - gdb_printf (_(" at ")); - ULONGEST pc - = (is_current - ? regcache_read_pc (get_thread_regcache (inferior_thread ())) - : fi.pc); - gdb_puts (paddress (gdbarch, pc)); - - symtab_and_line sal = find_pc_line (pc, 0); - if (sal.symtab) - gdb_printf (_(", file %s"), - symtab_to_filename_for_display (sal.symtab)); - if (sal.line) - gdb_printf (_(", line %d"), sal.line); - if (!sal.symtab && !sal.line) - { - bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); - if (msym.minsym) - gdb_printf (", <%s>", msym.minsym->linkage_name ()); + if (req_fi != nullptr && req_fi != &fi) + continue; + + will_print_something = true; + + inf_width + = std::max (inf_width, + string_printf ("%d", inf->num).size ()); + num_width + = std::max (num_width, + string_printf ("%d", fi.num).size () + + (print_inf ? 1 : 0)); + targid_width + = std::max (targid_width, + target_pid_to_str (fi.ptid).size ()); } + } - gdb_putc ('\n'); + /* Return early if there are no checkpoints to print. */ + if (!will_print_something) + { + gdb_printf (_("No checkpoints.\n")); + return; } - if (!printed) + /* Ensure that column header width doesn't exceed that of the column data + for the Id field. */ + if (!print_inf && num_width < 2) + num_width = 2; + + /* Print column headers... */ + gdb_printf (" "); + gdb_printf ("%-*s", (print_inf ? (int) inf_width : 0) + + (int) num_width + 1, "Id"); + gdb_printf ("Active "); + gdb_printf ("%-*s", (int) targid_width + 1, "Target Id"); + gdb_printf ("Frame\n"); + + /* Print each checkpoint padded, as needed, with spaces so that everything + lines up. */ + for (inferior *inf : all_inferiors (linux_target)) { - if (requested > 0) - gdb_printf (_("No checkpoint number %d.\n"), requested); - else - gdb_printf (_("No checkpoints.\n")); + if (req_inf != nullptr && req_inf != inf) + continue; + + scoped_restore_current_pspace_and_thread restore_pspace_thread; + switch_to_program_space_and_thread (inf->pspace); + + for (const fork_info &fi : fork_list (inf)) + { + if (req_fi != nullptr && req_fi != &fi) + continue; + + thread_info *t = any_thread_of_inferior (inf); + bool is_current = fi.ptid.pid () == inf->pid; + if (is_current && cur_inf == inf) + gdb_printf ("* "); + else + gdb_printf (" "); + + if (print_inf) + gdb_printf ("%*d.%-*d", (int) inf_width, inf->num, + (int) num_width, fi.num); + else + gdb_printf ("%*d ", (int) num_width, fi.num); + + /* Print out 'y' or 'n' for whether the checkpoint is current. */ + gdb_printf ("%-7s", is_current ? "y" : "n"); + + /* Print target id. */ + gdb_printf ("%-*s", (int) targid_width, + target_pid_to_str (fi.ptid).c_str ()); + + if (t->state == THREAD_RUNNING && is_current) + gdb_printf (_(" (running)")); + else + { + gdb_printf (_(" at ")); + ULONGEST pc + = (is_current + ? regcache_read_pc (get_thread_regcache (t)) + : fi.pc); + gdb_puts (paddress (gdbarch, pc)); + + symtab_and_line sal = find_pc_line (pc, 0); + if (sal.symtab) + gdb_printf (_(", file %s"), + symtab_to_filename_for_display (sal.symtab)); + if (sal.line) + gdb_printf (_(", line %d"), sal.line); + if (!sal.symtab && !sal.line) + { + bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); + if (msym.minsym) + gdb_printf (", <%s>", msym.minsym->linkage_name ()); + } + } + + gdb_putc ('\n'); + } } } /* The PID of the process we're checkpointing. */ static int checkpointing_pid = 0; -int +bool linux_fork_checkpointing_p (int pid) { return (checkpointing_pid == pid); @@ -691,17 +912,16 @@ checkpoint_command (const char *args, int from_tty) struct target_waitstatus last_target_waitstatus; ptid_t last_target_ptid; struct value *fork_fn = NULL, *ret; - struct fork_info *fp; pid_t retpid; - if (!target_has_execution ()) + if (!target_has_execution ()) error (_("The program is not being run.")); /* Ensure that the inferior is not multithreaded. */ update_thread_list (); if (inf_has_multiple_threads ()) error (_("checkpoint: can't checkpoint multiple threads.")); - + /* Make the inferior fork, record its (and gdb's) state. */ if (lookup_minimal_symbol (current_program_space, "fork").minsym != nullptr) @@ -730,14 +950,21 @@ checkpoint_command (const char *args, int from_tty) retpid = value_as_long (ret); get_last_target_status (nullptr, &last_target_ptid, &last_target_waitstatus); - fp = find_fork_pid (retpid); + auto [fp, inf] = find_fork_pid (retpid); + + if (!fp) + error (_("Failed to find new fork")); if (from_tty) { int parent_pid; - gdb_printf (_("checkpoint %d: fork returned pid %ld.\n"), - fp != NULL ? fp->num : -1, (long) retpid); + gdb_printf (_("checkpoint %s: fork returned pid %ld.\n"), + ((number_of_inferiors () > 1) + ? string_printf ("%d.%d", inf->num, fp->num).c_str () + : string_printf ("%d", fp->num).c_str ()), + (long) retpid); + if (info_verbose) { parent_pid = last_target_ptid.lwp (); @@ -748,15 +975,12 @@ checkpoint_command (const char *args, int from_tty) } } - if (!fp) - error (_("Failed to find new fork")); - - if (one_fork_p ()) + if (one_fork_p (inf)) { /* Special case -- if this is the first fork in the list (the - list was hitherto empty), then add inferior_ptid first, as a - special zeroeth fork id. */ - fork_list.emplace_front (inferior_ptid.pid ()); + list was hitherto empty), then add inferior_ptid as a special + zeroeth fork id. */ + fork_list (inf).emplace_front (inferior_ptid.pid (), 0); } fork_save_infrun_state (fp); @@ -764,40 +988,57 @@ checkpoint_command (const char *args, int from_tty) } static void -linux_fork_context (struct fork_info *newfp, int from_tty) +linux_fork_context (struct fork_info *newfp, int from_tty, inferior *newinf) { - /* Now we attempt to switch processes. */ - struct fork_info *oldfp; + bool inferior_changed = false; + /* Now we attempt to switch processes. */ gdb_assert (newfp != NULL); - oldfp = find_fork_ptid (inferior_ptid); - gdb_assert (oldfp != NULL); + if (newinf != current_inferior ()) + { + thread_info *tp = any_thread_of_inferior (newinf); + switch_to_thread (tp); + inferior_changed = true; + } - fork_save_infrun_state (oldfp); - remove_breakpoints (); - fork_load_infrun_state (newfp); - insert_breakpoints (); + auto [oldfp, oldinf] = find_fork_ptid (inferior_ptid); + gdb_assert (oldfp != NULL); - gdb_printf (_("Switching to %s\n"), - target_pid_to_str (inferior_ptid).c_str ()); + if (oldfp != newfp) + { + fork_save_infrun_state (oldfp); + remove_breakpoints (); + fork_load_infrun_state (newfp); + insert_breakpoints (); + if (!inferior_changed) + gdb_printf (_("Switching to %s\n"), + target_pid_to_str (inferior_ptid).c_str ()); + } - print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); + notify_user_selected_context_changed + (inferior_changed ? (USER_SELECTED_INFERIOR | USER_SELECTED_FRAME) + : USER_SELECTED_FRAME); } /* Switch inferior process (checkpoint) context, by checkpoint id. */ + static void restart_command (const char *args, int from_tty) { - struct fork_info *fp; - if (!args || !*args) error (_("Requires argument (checkpoint id to restart)")); - if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) - error (_("Not found: checkpoint id %s"), args); + auto [fp, inf] = parse_checkpoint_id (args); + + /* Don't allow switching from a thread/fork that's running. */ + inferior *curinf = current_inferior (); + if (curinf->pid != 0 + && any_thread_of_inferior (curinf)->state == THREAD_RUNNING) + error (_("Cannot execute this command while " + "the selected thread is running.")); - linux_fork_context (fp, from_tty); + linux_fork_context (fp, from_tty, inf); } void _initialize_linux_fork (); diff --git a/gdb/linux-fork.h b/gdb/linux-fork.h index c553aaf0740..0ed47eda15f 100644 --- a/gdb/linux-fork.h +++ b/gdb/linux-fork.h @@ -22,12 +22,13 @@ struct fork_info; struct lwp_info; -extern void add_fork (pid_t); -extern struct fork_info *find_fork_pid (pid_t); -extern void linux_fork_killall (void); -extern void linux_fork_mourn_inferior (void); -extern void linux_fork_detach (int, lwp_info *); -extern int forks_exist_p (void); -extern int linux_fork_checkpointing_p (int); +class inferior; +extern void add_fork (pid_t, inferior *inf); +extern std::pair<fork_info *, inferior *> find_fork_pid (pid_t); +extern void linux_fork_killall (inferior *inf); +extern void linux_fork_mourn_inferior (); +extern void linux_fork_detach (int, lwp_info *, inferior *inf); +extern bool forks_exist_p (inferior *inf); +extern bool linux_fork_checkpointing_p (int); #endif /* LINUX_FORK_H */ diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 3f252370c7b..5b2cc8f2fb8 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -1558,13 +1558,13 @@ linux_nat_target::detach (inferior *inf, int from_tty) gdb_assert (num_lwps (pid) == 1 || (target_is_non_stop_p () && num_lwps (pid) == 0)); - if (pid == inferior_ptid.pid () && forks_exist_p ()) + if (forks_exist_p (inf)) { /* Multi-fork case. The current inferior_ptid is being detached from, but there are other viable forks to debug. Detach from the current fork, and context-switch to the first available. */ - linux_fork_detach (from_tty, find_lwp_pid (ptid_t (pid))); + linux_fork_detach (from_tty, find_lwp_pid (ptid_t (pid)), inf); } else { @@ -2082,8 +2082,12 @@ linux_handle_extended_wait (struct lwp_info *lp, int status) detach_breakpoints (ptid_t (new_pid, new_pid)); /* Retain child fork in ptrace (stopped) state. */ - if (!find_fork_pid (new_pid)) - add_fork (new_pid); + if (find_fork_pid (new_pid).first == nullptr) + { + struct inferior *inf = find_inferior_ptid (linux_target, + lp->ptid); + add_fork (new_pid, inf); + } /* Report as spurious, so that infrun doesn't want to follow this fork. We're actually doing an infcall in @@ -3729,8 +3733,8 @@ linux_nat_target::kill () parent will be sleeping if this is a vfork. */ iterate_over_lwps (pid_ptid, kill_unfollowed_child_callback); - if (forks_exist_p ()) - linux_fork_killall (); + if (forks_exist_p (current_inferior ())) + linux_fork_killall (current_inferior ()); else { /* Stop all threads before killing them, since ptrace requires @@ -3761,7 +3765,7 @@ linux_nat_target::mourn_inferior () close_proc_mem_file (pid); - if (! forks_exist_p ()) + if (! forks_exist_p (current_inferior ())) /* Normal case, no other forks available. */ inf_ptrace_target::mourn_inferior (); else diff --git a/gdb/testsuite/gdb.base/checkpoint.exp b/gdb/testsuite/gdb.base/checkpoint.exp index 31b5a13bb8f..4a8a9a8eb7f 100644 --- a/gdb/testsuite/gdb.base/checkpoint.exp +++ b/gdb/testsuite/gdb.base/checkpoint.exp @@ -274,17 +274,17 @@ gdb_test "kill" "" "kill all one" \ # and confirm that all are gone # -gdb_test "restart 0" "Not found.*" "no more checkpoint 0" -gdb_test "restart 1" "Not found.*" "no more checkpoint 1" -gdb_test "restart 2" "Not found.*" "no more checkpoint 2" -gdb_test "restart 3" "Not found.*" "no more checkpoint 3" -gdb_test "restart 4" "Not found.*" "no more checkpoint 4" -gdb_test "restart 5" "Not found.*" "no more checkpoint 5" -gdb_test "restart 6" "Not found.*" "no more checkpoint 6" -gdb_test "restart 7" "Not found.*" "no more checkpoint 7" -gdb_test "restart 8" "Not found.*" "no more checkpoint 8" -gdb_test "restart 9" "Not found.*" "no more checkpoint 9" -gdb_test "restart 10" "Not found.*" "no more checkpoint 10" +gdb_test "restart 0" "has no checkpoints" "no more checkpoint 0" +gdb_test "restart 1" "has no checkpoints" "no more checkpoint 1" +gdb_test "restart 2" "has no checkpoints" "no more checkpoint 2" +gdb_test "restart 3" "has no checkpoints" "no more checkpoint 3" +gdb_test "restart 4" "has no checkpoints" "no more checkpoint 4" +gdb_test "restart 5" "has no checkpoints" "no more checkpoint 5" +gdb_test "restart 6" "has no checkpoints" "no more checkpoint 6" +gdb_test "restart 7" "has no checkpoints" "no more checkpoint 7" +gdb_test "restart 8" "has no checkpoints" "no more checkpoint 8" +gdb_test "restart 9" "has no checkpoints" "no more checkpoint 9" +gdb_test "restart 10" "has no checkpoints" "no more checkpoint 10" # # Now let's try setting a large number of checkpoints (>600) @@ -331,7 +331,7 @@ gdb_assert { $nr_ok == 600 } "break1 with many checkpoints" set count 0 set msg "info checkpoints with at least 600 checkpoints" gdb_test_multiple "info checkpoints" $msg { - -re "\r\n $decimal process \[^\r\]*" { + -re "\r\n *$decimal n +process \[^\r\]*" { incr count exp_continue } diff --git a/gdb/testsuite/gdb.multi/checkpoint-multi.exp b/gdb/testsuite/gdb.multi/checkpoint-multi.exp new file mode 100644 index 00000000000..2bd654347b0 --- /dev/null +++ b/gdb/testsuite/gdb.multi/checkpoint-multi.exp @@ -0,0 +1,800 @@ +# Copyright 2009-2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# This file tests various scenarios involving multiple inferiors +# and the checkpoint command. + +# Checkpoint support works only on Linux. +require {istarget "*-*-linux*"} + +# Checkpoint support is implemented for the (Linux) native target. +require gdb_protocol_is_native + +set checkpoints_header_re " +Id +Active Target Id +Frame.*?" +set proc_re "(?:process $::decimal|Thread $::hex \\(LWP $::decimal\\))" +set ckpt_re "checkpoint" +set main_proc "\\(main process\\)" +set hello_c "hello\\.c" +set goodbye_c "goodbye\\.c" +set hangout_c "hangout\\.c" + +set testfile "checkpoint-multi" + +set exec1 "hello" +set srcfile1 ${exec1}.c +set binfile1 [standard_output_file ${exec1}] + +set exec2 "goodbye" +set srcfile2 ${exec2}.c +set binfile2 [standard_output_file ${exec2}] + +set exec3 "hangout" +set srcfile3 ${exec3}.c +set binfile3 [standard_output_file ${exec3}] + +if { [build_executable ${testfile}.exp ${exec1} "${srcfile1}" {debug}] == -1 } { + return -1 +} + +if { [build_executable ${testfile}.exp ${exec2} "${srcfile2}" {debug}] == -1 } { + return -1 +} + +if { [build_executable ${testfile}.exp ${exec3} "${srcfile3}" {debug}] == -1 } { + return -1 +} + +# Start two inferiors, place a checkpoint on inferior 2, but switch +# back to inferior 1. +proc start_2_inferiors_checkpoint_on_inf_2 {} { + clean_restart $::exec1 + + # Start inferior 1. + if {[gdb_start_cmd] < 0} { + fail "start first inferior" + } else { + gdb_test "" "main.*" "start first inferior" + } + + # Add a new inferior and exec into it. + gdb_test "add-inferior -exec $::binfile2" \ + "Added inferior 2.*" \ + "add inferior 2 with -exec $::exec2" + + # Check that we have multiple inferiors. + gdb_test "info inferiors" \ + "Executable.*$::exec1.*$::exec2.*" + + # Switch to inferior 2. + gdb_test "inferior 2" \ + "Switching to inferior 2.*$::exec2.*" + + # Start inferior 2: + if {[gdb_start_cmd] < 0} { + fail "start second inferior" + } else { + gdb_test "" "main.*" "start second inferior" + } + + # Set a checkpoint in inferior 2 + gdb_test "checkpoint" "$::ckpt_re 2\\.1: fork returned pid $::decimal.*" + + # Step one line in inferior 2. + gdb_test "step" "glob = 46;" + + # Switch back to inferior 1. + gdb_test "inferior 1" "Switching to inferior 1.*$::exec1.*" +} + +# Start two inferiors, place a checkpoint on inferior 2, but switch +# back to inferior 1. This is like the one above, except that it +# swaps the executables loaded into inferior 1 and inferior 2. This +# is important for being able to test "continue to exit". (Because... +# hello.c has an infinite loop, but goodbye.c doesn't. In order to +# test "continue to exit", we need to continue in an executable which +# will actually exit.) + +proc start_2_inferiors_checkpoint_on_inf_2_alt {} { + clean_restart $::exec2 + + # Start inferior 1. + if {[gdb_start_cmd] < 0} { + fail "start first inferior" + } else { + gdb_test "" "main.*" "start first inferior" + } + + # Add a new inferior and exec exec1 into it. + gdb_test "add-inferior -exec $::binfile1" \ + "Added inferior 2.*" \ + "add inferior 2 with -exec $::exec1" + + # Check that we have two inferiors. + gdb_test "info inferiors" \ + "Executable.*$::exec2.*$::exec1.*" + + # Switch to inferior 2. + gdb_test "inferior 2" \ + "Switching to inferior 2.*$::exec1.*" + + # Start inferior 2: + if {[gdb_start_cmd] < 0} { + fail "start second inferior" + } else { + gdb_test "" "main.*" "start second inferior" + } + + # Set a checkpoint in inferior 2 + gdb_test "checkpoint" "$::ckpt_re 2\\.1: fork returned pid $::decimal.*" + + # next one line in inferior 2. + gdb_test "next" "bar\\(\\).*" + + # Switch back to inferior 1. + gdb_test "inferior 1" "Switching to inferior 1.*$::exec2.*" +} + +with_test_prefix "check detach on non-checkpointed inferior" { + start_2_inferiors_checkpoint_on_inf_2 + gdb_test "detach" "Detaching from program.*$::exec1.*Inferior 1.*detached.*" +} + +with_test_prefix "check kill on non-checkpointed inferior" { + start_2_inferiors_checkpoint_on_inf_2 + gdb_test "kill" "" "kill non-checkpointed inferior" \ + "Kill the program being debugged.*y or n. $" "y" +} + +with_test_prefix "check restart 0 on non-checkpointed inferior" { + start_2_inferiors_checkpoint_on_inf_2 + gdb_test "restart 0" "Inferior 1 has no checkpoints" + gdb_test "restart 2.0" "Switching to inferior 2.*?goodbye.*?#0 +mailand .*?glob = 46;.*" +} + +with_test_prefix "check restart 1 on non-checkpointed inferior" { + start_2_inferiors_checkpoint_on_inf_2 + gdb_test "restart 1" "Inferior 1 has no checkpoints" + gdb_test "restart 2.1" "Switching to inferior 2.*?goodbye.*?#0 +main .*?mailand\\(\\);.*" +} + +with_test_prefix "check continue to exit on non-checkpointed inferior" { + start_2_inferiors_checkpoint_on_inf_2_alt + gdb_test "continue" "Inferior 1.*? exited normally.*" +} + +with_test_prefix "two inferiors with checkpoints" { + start_2_inferiors_checkpoint_on_inf_2 + with_test_prefix "one checkpoint" { + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " +2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " +2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + with_test_prefix "two checkpoints" { + gdb_test "checkpoint" "$ckpt_re 1\\.1: fork returned pid $::decimal.*" \ + "checkpoint in inferior 1" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + # Note: No switching is done here since checkpoint 0 is the active one. + gdb_test "restart 0" "main.*?$hello_c.*?alarm \\(240\\);" + + gdb_test "restart 2.0" \ + "\\\[Switching to inferior 2.*?mailand.*?glob = 46;.*" + gdb_test "next" "\}" + + with_test_prefix "restart 1" { + gdb_test "restart 1" "^Switching to $proc_re.*?#0 main \\(\\) at.*?$goodbye_c.*mailand\\(\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "info checkpoints twice in a row" { + # Doing "info_checkpoints" twice in a row might seem pointless, + # but during work on making the checkpoint code inferior aware, + # there was a point at which doing it twice in a row did not + # produce the same output. + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "restart 0" { + # Switch back to checkpoint 0; again, there should be no + # "Switching to inferior" message. + gdb_test "restart 0" \ + "^Switching to $proc_re.*?#0 mailand \\(\\) at.*?$goodbye_c.*\}" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + "\\* 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + # Try switching to invalid checkpoints: + with_test_prefix "invalid checkpoints" { + gdb_test "restart 3" "Invalid checkpoint number 3 for inferior 2" + gdb_test "restart 2" "Invalid checkpoint number 2 for inferior 2" + gdb_test "restart -1" "Checkpoint number must be a non-negative integer" + gdb_test "restart 2.3" "Invalid checkpoint number 3 for inferior 2" + gdb_test "restart 3.0" "No inferior number '3'" + gdb_test "restart 1.2" "Invalid checkpoint number 2 for inferior 1" + gdb_test "restart 1.3" "Invalid checkpoint number 3 for inferior 1" + gdb_test "restart 1.-1" "Checkpoint number must be a non-negative integer" + gdb_test "restart -1.0" "Inferior number must be a positive integer" + } + + with_test_prefix "restart 1.1" { + # Switch to checkpoint 1.1; this time, we should see a "Switching to + # inferior" message. + gdb_test "restart 1.1" \ + "\\\[Switching to inferior 1.*?main.*?$hello_c.*?alarm \\(240\\);" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + "\\* 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "restart 2.1" { + gdb_test "restart 2.1" \ + "Switching to inferior 2.*?#0 main \\(\\) at.*?$goodbye_c.*mailand\\(\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "second checkpoint in inferior 2" { + gdb_test "checkpoint" "$ckpt_re 2\\.2: fork returned pid $::decimal.*" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "third checkpoint in inferior 2" { + gdb_test "checkpoint" "$ckpt_re 2.3: fork returned pid $::decimal.*" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.3 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "continue to exit in checkpoint 2.1" { + gdb_test "continue" \ + "Inferior 2 \\(process $decimal\\) exited normally.*?Switching to $proc_re.*?" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.3 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "continue to exit in checkpoint 2.3" { + gdb_test "continue" \ + "Inferior 2 \\(process $decimal\\) exited normally.*?Switching to process $decimal.*?" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "continue to exit in checkpoint 2.2" { + gdb_test "continue" \ + "Inferior 2 \\(process $decimal\\) exited normally.*?Switching to process $decimal.*?" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?"] + } + + with_test_prefix "new checkpoints in inferior 2" { + gdb_test "checkpoint" "$ckpt_re 2.1: fork returned pid $::decimal.*" \ + "checkpoint 2.1" + + gdb_test "checkpoint" "$ckpt_re 2.2: fork returned pid $::decimal.*" \ + "checkpoint 2.2" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + "\\* 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "delete checkpoint 2.0" { + gdb_test "delete checkpoint 2.0" \ + "Cannot delete active checkpoint" \ + "failed attempt to delete active checkpoint 2.0" + + gdb_test "restart 2.1" \ + "^Switching to process.*?#0 mailand \\(\\) at.*?$goodbye_c.*\}" + + gdb_test "delete checkpoint 2.0" \ + "Killed process $::decimal" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "delete checkpoint 2.2" { + gdb_test "delete checkpoint 2.2" \ + "Killed process $::decimal" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?"] + } + + with_test_prefix "new checkpoint in inferior 2" { + gdb_test "checkpoint" "$ckpt_re 2.1: fork returned pid $::decimal.*" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + "\\* 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "switch to inferior 1" { + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + "\\* 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "kill inferior 1" { + gdb_test "kill" "\\\[Inferior 1 \\(process $::decimal\\) killed\\\]" \ + "kill inferior 1" \ + "Kill the program being debugged.*y or n. $" "y" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "start inferior 1 again" { + gdb_test "checkpoint" "The program is not being run\\." \ + "checkpoint in non-running inferior" + + gdb_test "start" "Starting program.*?hello.*?alarm \\(240\\);" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "checkpoint 1.1" { + gdb_test "checkpoint" "$ckpt_re 1.1: fork returned pid $::decimal.*" \ + "second checkpoint in inferior 1" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } +} + +with_test_prefix "three inferiors with checkpoints" { + start_2_inferiors_checkpoint_on_inf_2 + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + + with_test_prefix "add third inferior" { + # Add a third inferior and exec into it. + gdb_test "add-inferior -exec $::binfile3" \ + "Added inferior 3.*" \ + "add inferior 3 with -exec $::exec3" + + # Check that we have three inferiors. + gdb_test "info inferiors" \ + "Executable.*?\\* 1 .*?$::exec1.*? 2 .*?$::exec2.*? 3 .*?$::exec3.*?" \ + "check for three inferiors" + + # Switch to inferior 3. + gdb_test "inferior 3" \ + "Switching to inferior 3.*$::exec3.*" + + # Start inferior 2: + if {[gdb_start_cmd] < 0} { + fail "start third inferior" + } else { + gdb_test "" "main.*" "start third inferior" + } + + gdb_test "checkpoint" "$ckpt_re 3\\.1: fork returned pid $::decimal.*" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "make checkpoint in inferior 1" { + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" + + gdb_test "checkpoint" "$ckpt_re 1\\.1: fork returned pid $::decimal.*" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "restart 2.1" { + gdb_test "restart 2.1" \ + "Switching to inferior 2.*?#0 main \\(\\) at.*?$goodbye_c.*mailand\\(\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "next and make new checkpoint" { + gdb_test "next" "foo\\(glob\\);" + gdb_test "checkpoint" "$ckpt_re 2\\.2: fork returned pid $::decimal.*" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "switch to inferior 3 for upcoming kill" { + gdb_test "inferior 3" "Switching to inferior 3.*?alarm \\(30\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "kill inferior 3" { + gdb_test "kill" "\\\[Inferior 3 \\(process $::decimal\\) killed\\\]" \ + "kill inferior 3" \ + "Kill the program being debugged.*y or n. $" "y" + + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] + } + + with_test_prefix "delete checkpoint 2.0" { + gdb_test "delete checkpoint 0" \ + "Inferior 3 has no checkpoints" + gdb_test "delete checkpoint 2.0" \ + "Killed process $::decimal" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] + } + + with_test_prefix "restart 2.2" { + gdb_test "restart 2.2" \ + "Switching to inferior 2.*?#0 main \\(\\) at.*?$goodbye_c.*foo\\(glob\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] + } + + with_test_prefix "switch to non-running inferior 3" { + gdb_test "inferior 3" "\\\[Switching to inferior 3 \\\[<null>\\\] \\(.*?$::exec3\\)\\\]" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] + } + + with_test_prefix "restart inferior 3 and make new checkpoints" { + gdb_test "start" "Starting program.*?hangout.*?alarm \\(30\\);" + gdb_test "checkpoint" \ + "$ckpt_re 3\\.1: fork returned pid $::decimal.*" \ + "checkpoint 3.1" + gdb_test "checkpoint" \ + "$ckpt_re 3\\.2: fork returned pid $::decimal.*" \ + "checkpoint 3.2" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "delete checkpoint 3.1" { + gdb_test "delete checkpoint 1" \ + "Killed process $::decimal" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "attempt to delete active checkpoint in non-current inferior" { + # Switch to inferior 1, add another checkpoint - so that there + # are three of them in inferior 1 - then switch back to + # inferior 3 and delete active checkpoint in inferior 1. + # Then, switch to inferior 1 and attempt to add another + # checkpoint. During development, a "Cannot access memory at + # address ..." message was seen. This was a bug - there were + # several problems - but one of them was that the checkpoint in + # question was an "active" checkpoint. The fix was to + # disallow this case. + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" + gdb_test "checkpoint" "$ckpt_re 1\\.2: fork returned pid $::decimal.*" + gdb_test "inferior 3" "Switching to inferior 3.*?alarm \\(30\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + + # Check that deleting active checkpoints in other (non-current) + # inferiors is disallowed. + gdb_test "delete checkpoint 1.0" \ + "Cannot delete active checkpoint" + } + + with_test_prefix "delete non-active checkpoint in non-current inferior" { + # But deleting non-active checkpoints, even in other inferiors, + # should work. + gdb_test "delete checkpoint 1.1" \ + "Killed process $::decimal" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "switch to inferior 1" { + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "checkpoint 1.3" { + gdb_test "checkpoint" "$ckpt_re 1\\.3: fork returned pid $::decimal.*" \ + "third checkpoint in inferior 1" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.3 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] + } + + with_test_prefix "attempt to remove active but not current inferior" { + gdb_test "x/i \$pc" "=> $::hex <main.*" + gdb_test "remove-inferior 3" \ + "warning: Can not remove active inferior 3\." + } +} + +with_test_prefix "background execution" { + start_2_inferiors_checkpoint_on_inf_2 + with_test_prefix "one checkpoint" { + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 2.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + with_test_prefix "two checkpoints" { + gdb_test "checkpoint" "$ckpt_re 1\\.1: fork returned pid $::decimal.*" \ + "checkpoint in inferior 1" + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "background continue hello" { + gdb_test "continue &" "Continuing\." + gdb_test "info checkpoints" \ + [multi_line \ + "\\* 1\\.0 y +$proc_re \\(running\\)" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "fail to switch to inferior 2 w/ 1 in background" { + gdb_test "restart 2.1" "Cannot execute this command while the selected thread is running." + # Should be no change from earlier output. + gdb_test "info checkpoints" \ + [multi_line \ + "\\* 1\\.0 y +$proc_re \\(running\\)" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } + + with_test_prefix "switch to inferior 2" { + set msg "stop thread" + gdb_test_multiple "interrupt" $msg { + -re "$gdb_prompt " { + gdb_test_multiple "" $msg { + -re "Thread 1\\.1 \"hello\" received signal SIGINT, Interrupt\\." { + pass $gdb_test_name + } + } + } + } + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + "\\* 1\\.0 y +$proc_re +at $::hex,.*" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + gdb_test "restart 2.1" "Switching to inferior 2.*?goodbye.*?#0 +main .*?mailand\\(\\);.*" + } + + with_test_prefix "after restart 2.1" { + gdb_test "info checkpoints" \ + [multi_line \ + "$checkpoints_header_re" \ + " 1\\.0 y +$proc_re +at $::hex,.*" \ + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] + } +} -- 2.46.2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v5 1/4] Make linux checkpoints work with multiple inferiors 2024-12-10 1:54 ` [PATCH v5 1/4] " Kevin Buettner @ 2025-01-30 17:45 ` Andrew Burgess 2025-02-05 22:06 ` Kevin Buettner 0 siblings, 1 reply; 10+ messages in thread From: Andrew Burgess @ 2025-01-30 17:45 UTC (permalink / raw) To: Kevin Buettner, gdb-patches; +Cc: pedro, Kevin Buettner Hi Kevin, Thanks for working on this. I had some pretty minor thoughts about the layout of the 'info checkpoints' command, see my notes inline below. Additionally, there are a couple of places in this patch where you have spaces instead of tabs, and trailing whitespace. I have this in my ~/.gitconfig file: [alias] check=show --check --format="%C(auto)%h\\ %s\\ %C(blue)[%an\\ (%cr)]%C(reset)" Then you can do `git check` to check the last commit, or `git check SHA` to check some other commit. The output will highlight any whitespace issues with the patch. Kevin Buettner <kevinb@redhat.com> writes: > The current linux checkpoint code, most of which may be found in > linux-fork.c, is quite broken when attempting to use more than > one inferior. Running GDB will show internal errors when starting > two inferiors, placing a checkpoint in one, then switching to > the other and doing one of the following commands, "restart", > "detach", "kill", or continue (to program exit). Test cases > for two of those scenarios may be found in this bug: > > https://sourceware.org/bugzilla/show_bug.cgi?id=31065 > > I've tested for each of the scenarios and many more in the new > test case, gdb.multi/checkpoint-multi.exp. > > I started off with the goal of fixing just those problems, and was > mostly successful with a much smaller patch, but doing "info > checkpoints" with more than one inferior didn't work correctly due to > some of the inferiors being in the wrong program space. That led me > to making the linux-fork code fully inferior-aware. > > Prior to this commit, the list of forks was being maintained in a > global named named 'fork_list'. I turned this into a per-inferior > data structure. There was also global named 'highest_fork_num' which > is also now part of the per-inferior struct. A registry key named > 'checkpoint_inferior_data_key' along with function > 'get_checkpoint_inferior_data' is used to access the per-inferior > data. This new function, get_checkpoint_inferior_data, is only > called by the new functions 'fork_list', 'reset_highest_fork_num', > and increment_highest_fork_num, each of which is passed a pointer to > the inferior. Most occurrences referring to the (previously) global > 'fork_list' have been replaced by 'fork_list (inf)'. In some > functions, where the 'fork_list' is referenced multiple times, a local > named 'fork_list' is declared and initialized instead, like this: > > auto &fork_list = ::fork_list (inf); > > The constructor for 'struct fork_info' has gained an additional > parameter. In addition to passing the pid of the new fork, we now > also pass the fork identifier, fork_num, to the constructor. This > integer is shown to the user in the "info checkpoints" command and > is provided by the user, perhaps in conjunction with the inferior > number, in commands which manipulate checkpoints, e.g. 'restart' and > 'delete checkpoint'. > > When checkpoints are used in only one inferior, this commit will > present information to the user and will accept checkpoint identifiers > to commands in much the same way as the code did before this commit. > Per Pedro Alves's recommendations, the "info checkpoints" command has > been changed somewhat. "info checkpoints" used to display "(main > process)" for the first process in the checkpoint list. This is no > longer done because it does not provide useful information. It also > used to display "<running>", when the process is running and no useful > frame information may be displayed. This has been changed to > "(running)" in order to be more consistent with the output of the > "info threads" command. A new column has been added to the output for > showing the active process in the output from "info checkpoints". > This column will display 'y' for the active process and 'n' for the > others. For the active inferior a '*' is also printed preceding the > checkpoint identifier. Here's what things look(ed) like before and > after for just one inferior: > > Before: > > (gdb) info checkpoints > * 0 Thread 0x7ffff7cd3740 (LWP 84201) (main process) at 0x40114a, file hello.c, line 28 > 1 process 84205 at 0x401199, file hello.c, line 51 > 2 process 84206 at 0x4011a3, file hello.c, line 53 > > After: > > (gdb) info checkpoints > Id Active Target Id Frame > * 0 y process 551311 at 0x40114a, file hello.c, line 28 > 1 n process 551314 at 0x401199, file hello.c, line 51 > 2 n process 551315 at 0x4011a3, file hello.c, line 53 I'd like to suggest using GDB's structured table generation mechanism here. There's two great examples; 'print_inferior' in inferior.c (which does the 'info inferiors' command) and 'print_thread_info_1' in thread.c (which is the 'info threads' command). I'd also like to suggest, that for the 'Frame' column, you make use of print_stack_frame(). This is the function that 'info threads' uses to print its stack frame, I think having consistent output would be a win here. That all said, if you wanted to merge this now, and then we could possibly come back and tweak the output formatting and content afterwards, I'd be fine with that too. Either way: Approved-By: Andrew Burgess <aburgess@redhat.com> Thanks, Andrew > > (The Thread versus process distinction is handled by another > patch - the "After" example assumes that patch is applied too.) > > When there are multiple inferiors, the "info checkpoints" output looks > like this: > > (gdb) info checkpoints > Id Active Target Id Frame > 1.0 y process 535276 at 0x401199, file hello.c, line 51 > 1.1 n process 535283 at 0x401199, file hello.c, line 51 > 1.2 n process 535288 at 0x401199, file hello.c, line 51 > 2.1 n process 535280 at 0x401258, file goodbye.c, line 62 > 2.2 y process 535284 at 0x401258, file goodbye.c, line 62 > * 3.0 y process 535285 at 0x40115c, file hangout.c, line 31 > 3.2 n process 535287 at 0x40115c, file hangout.c, line 31 > > A new function named 'parse_checkpoint_id' has been added. As its > name suggests, it's responsible for parsing a string representing a > checkpoint identifier. These identifiers may be either a decimal > number representing the checkpoint number in the current inferior or > two decimal numbers separated by '.', in which case the first is the > inferior number and the second is the checkpoint number in that > inferior. It is called by delete_checkpoint_command, > detach_checkpoint_command, info_checkpoints_command, and > restart_command. Calls to 'parse_checkpoint_id' replace calls to > 'parse_and_eval_long', plus error checking and error reporting code > near the calls to 'parse_and_eval_long'. As such, error checking and > reporting has been consolidated into a single function and the > messages output are more uniform, though this has necessitated changes > to the existing test case gdb.base/checkpoint.exp. > > The functions 'find_fork_ptid' and 'find_fork_pid' used to return a > pointer to a fork_info struct. They now return a pair consisting of > the pointer to a fork_info struct in addition to a pointer to the > inferior containing that checkpoint. > > 'find_fork_id' returns a pointer to a fork_info struct just as it did > before, but it's now gained a new parameter, 'inf', which is the > inferior in which to look. > > info_checkpoints_command used to simply iterate over the list of > forks (checkpoints), printing each one out. It now needs to iterate > over all inferiors and, for those which have checkpoints, it needs > to iterate over the list of checkpoints in that inferior. As noted > earlier, the format of the output has been changed so that checkpoint > identifiers incorporating an inferior number may be printed. > > linux_fork_context, called by restart_command, now contains code to > switch inferiors when the fork being restarted is in an inferior which > is different from the current one. The scoped_switch_fork_info class > now also contains code for switching inferiors in both the constructor > and destructor. > > gdb/linux-nat.c has a few changes. All but one of them are related > to passing the inferior to one of the linux-fork functions. But > one of the tests in linux_nat_target::detach has also changed in > a non-obvious way. In attempting to determine whether to call > linux_fork_detach(), that code used to do: > > if (pid == inferior_ptid.pid () && forks_exist_p ()) > > It's been simplified to: > > if (forks_exist_p (inf)) > > I had added the 'pid == inferior_ptid.pid ()' condition in late 2023 > while working on a detach bug. It was kind of a hack to prevent > calling linux_fork_detach() when in a different inferior. That's no > longer needed since the call to forks_exist_p does this directly - > i.e. it is now inferior-aware. > > Finally, the header file 'linux-fork.h' has been updated to reflect > the fact that add_fork, linux_fork_killall, linux_fork_detach, and > forks_exist_p all now require that a pointer to an inferior be passed > to these functions. Additionally (as mentioned earlier), > find_fork_pid now returns std::pair<fork_info *, inferior *> instead > 'of fork_info *'. > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31065 > --- > gdb/linux-fork.c | 557 +++++++++---- > gdb/linux-fork.h | 15 +- > gdb/linux-nat.c | 18 +- > gdb/testsuite/gdb.base/checkpoint.exp | 24 +- > gdb/testsuite/gdb.multi/checkpoint-multi.exp | 800 +++++++++++++++++++ > 5 files changed, 1230 insertions(+), 184 deletions(-) > create mode 100644 gdb/testsuite/gdb.multi/checkpoint-multi.exp > > diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c > index 7d3505b5948..49c2cf1ed84 100644 > --- a/gdb/linux-fork.c > +++ b/gdb/linux-fork.c > @@ -29,6 +29,7 @@ > #include "linux-nat.h" > #include "gdbthread.h" > #include "source.h" > +#include "progspace-and-thread.h" > > #include "nat/gdb_ptrace.h" > #include "gdbsupport/gdb_wait.h" > @@ -40,10 +41,11 @@ > #include <list> > > /* Fork list data structure: */ > + > struct fork_info > { > - explicit fork_info (pid_t pid) > - : ptid (pid, pid) > + explicit fork_info (pid_t pid, int fork_num) > + : ptid (pid, pid), num (fork_num) > { > } > > @@ -71,7 +73,7 @@ struct fork_info > ptid_t parent_ptid = null_ptid; > > /* Convenient handle (GDB fork id). */ > - int num = 0; > + int num; > > /* Convenient for info fork, saves having to actually switch > contexts. */ > @@ -85,115 +87,247 @@ struct fork_info > int maxfd = 0; > }; > > -static std::list<fork_info> fork_list; > -static int highest_fork_num; > +/* Per-inferior checkpoint data. */ > + > +struct checkpoint_inferior_data > +{ > + /* List of forks (checkpoints) in particular inferior. Once a > + checkpoint has been created, fork_list will contain at least two > + items, the first in the list will be the original (or, if not > + original, then the oldest) fork. */ > + std::list<fork_info> fork_list; > + > + /* Most recently assigned fork number; when 0, no checkpoints have > + been created yet. */ > + int highest_fork_num = 0; > +}; > + > +/* Per-inferior data key. */ > + > +static const registry<inferior>::key<checkpoint_inferior_data> > + checkpoint_inferior_data_key; > + > +/* Fetch per-inferior checkpoint data. It always returns a valid pointer > + to a checkpoint_inferior_info struct. */ > + > +static struct checkpoint_inferior_data * > +get_checkpoint_inferior_data (struct inferior *inf) > +{ > + struct checkpoint_inferior_data *data; > + > + data = checkpoint_inferior_data_key.get (inf); > + if (data == nullptr) > + data = checkpoint_inferior_data_key.emplace (inf); > + > + return data; > +} > + > +/* Return a reference to the per-inferior fork list. */ > + > +static std::list<fork_info> & > +fork_list (inferior *inf) > +{ > + return get_checkpoint_inferior_data (inf)->fork_list; > +} > + > +/* Increment the highest fork number for inferior INF, returning > + the new value. */ > + > +static int > +increment_highest_fork_num (inferior *inf) > +{ > + return ++get_checkpoint_inferior_data (inf)->highest_fork_num; > +} > + > +/* Reset the highest fork number for inferior INF. */ > + > +static void > +reset_highest_fork_num (inferior *inf) > +{ > + get_checkpoint_inferior_data (inf)->highest_fork_num = 0; > +} > > /* Fork list methods: */ > > -int > -forks_exist_p (void) > +/* Predicate which returns true if checkpoint(s) exist in the inferior > + INF, false otherwise. */ > + > +bool > +forks_exist_p (inferior *inf) > { > - return !fork_list.empty (); > + /* Avoid allocating checkpoint_inferior_data storage by checking > + to see if such storage exists prior to calling fork_list. > + If we just call fork_list alone, then that call will create > + this storage, even for inferiors which don't need it. */ > + return (checkpoint_inferior_data_key.get (inf) != nullptr > + && !fork_list (inf).empty ()); > } > > -/* Return the last fork in the list. */ > +/* Return the last fork in the list for inferior INF. */ > > static struct fork_info * > -find_last_fork (void) > +find_last_fork (inferior *inf) > { > + auto &fork_list = ::fork_list (inf); > + > if (fork_list.empty ()) > return NULL; > > return &fork_list.back (); > } > > -/* Return true iff there's one fork in the list. */ > +/* Return true iff there's one fork in the list for inferior INF. */ > > static bool > -one_fork_p () > +one_fork_p (inferior *inf) > { > - return fork_list.size () == 1; > + return fork_list (inf).size () == 1; > } > > /* Add a new fork to the internal fork list. */ > > void > -add_fork (pid_t pid) > +add_fork (pid_t pid, inferior *inf) > { > - fork_list.emplace_back (pid); > - > - if (one_fork_p ()) > - highest_fork_num = 0; > - > - fork_info *fp = &fork_list.back (); > - fp->num = ++highest_fork_num; > + fork_list (inf).emplace_back (pid, increment_highest_fork_num (inf)); > } > > +/* Delete a fork for PTID in inferior INF. When the last fork is > + deleted, HIGHEST_FORK_NUM for the given inferior is reset to 0. > + The fork list may also be made to be empty when only one fork > + remains. */ > + > static void > -delete_fork (ptid_t ptid) > +delete_fork (ptid_t ptid, inferior *inf) > { > linux_target->low_forget_process (ptid.pid ()); > > + auto &fork_list = ::fork_list (inf); > for (auto it = fork_list.begin (); it != fork_list.end (); ++it) > if (it->ptid == ptid) > { > fork_list.erase (it); > > + if (fork_list.empty ()) > + reset_highest_fork_num (inf); > + > /* Special case: if there is now only one process in the list, > and if it is (hopefully!) the current inferior_ptid, then > remove it, leaving the list empty -- we're now down to the > default case of debugging a single process. */ > - if (one_fork_p () && fork_list.front ().ptid == inferior_ptid) > + if (one_fork_p (inf) && fork_list.front ().ptid == inferior_ptid) > { > /* Last fork -- delete from list and handle as solo > process (should be a safe recursion). */ > - delete_fork (inferior_ptid); > + delete_fork (inferior_ptid, inf); > } > return; > } > } > > -/* Find a fork_info by matching PTID. */ > -static struct fork_info * > +/* Find a fork_info and inferior by matching PTID. */ > + > +static std::pair<fork_info *, inferior *> > find_fork_ptid (ptid_t ptid) > { > - for (fork_info &fi : fork_list) > - if (fi.ptid == ptid) > - return &fi; > + for (inferior *inf : all_inferiors (linux_target)) > + { > + for (fork_info &fi : fork_list (inf)) > + if (fi.ptid == ptid) > + return { &fi, inf }; > + } > > - return NULL; > + return { nullptr, nullptr }; > } > > -/* Find a fork_info by matching ID. */ > -static struct fork_info * > -find_fork_id (int num) > +/* Find a fork_info by matching NUM in inferior INF. */ > + > +static fork_info * > +find_fork_id (inferior *inf, int num) > { > - for (fork_info &fi : fork_list) > + for (fork_info &fi : fork_list (inf)) > if (fi.num == num) > return &fi; > > - return NULL; > + return nullptr; > } > > -/* Find a fork_info by matching pid. */ > -extern struct fork_info * > +/* Find a fork_info and inferior by matching pid. */ > + > +extern std::pair<fork_info *, inferior *> > find_fork_pid (pid_t pid) > { > - for (fork_info &fi : fork_list) > - if (pid == fi.ptid.pid ()) > - return &fi; > + for (inferior *inf : all_inferiors (linux_target)) > + { > + for (fork_info &fi : fork_list (inf)) > + if (pid == fi.ptid.pid ()) > + return { &fi, inf }; > + } > > - return NULL; > + return { nullptr, nullptr }; > } > > -static ptid_t > -fork_id_to_ptid (int num) > +/* Parse a command argument representing a checkpoint id. This > + can take one of two forms: > + > + Num > + > + -or- > + > + Inf.Num > + > + where Num is a non-negative decimal integer and Inf, if present, is > + a positive decimal integer. > + > + Return a pair with a pointer to the fork_info struct and pointer > + to the inferior. This function will throw an error if there's > + a problem with the parsing or if either the inferior or checkpoint > + id does not exist. */ > + > +static std::pair<fork_info *, inferior *> > +parse_checkpoint_id (const char *ckptstr) > { > - struct fork_info *fork = find_fork_id (num); > - if (fork) > - return fork->ptid; > + const char *number = ckptstr; > + const char *p1; > + struct inferior *inf; > + > + const char *dot = strchr (number, '.'); > + > + if (dot != nullptr) > + { > + /* Parse number to the left of the dot. */ > + int inf_num; > + > + p1 = number; > + inf_num = get_number_trailer (&p1, '.'); > + if (inf_num <= 0) > + error (_("Inferior number must be a positive integer")); > + > + inf = find_inferior_id (inf_num); > + if (inf == NULL) > + error (_("No inferior number '%d'"), inf_num); > + > + p1 = dot + 1; > + } > else > - return ptid_t (-1); > + { > + inf = current_inferior (); > + p1 = number; > + } > + > + int fork_num = get_number_trailer (&p1, 0); > + if (fork_num < 0) > + error (_("Checkpoint number must be a non-negative integer")); > + > + if (!forks_exist_p (inf)) > + error (_("Inferior %d has no checkpoints"), inf->num); > + > + fork_info *fork_ptr = find_fork_id (inf, fork_num); > + if (fork_ptr == nullptr) > + error (_("Invalid checkpoint number %d for inferior %d"), > + fork_num, inf->num); > + > + return { fork_ptr, inf }; > } > > /* Fork list <-> gdb interface. */ > @@ -298,7 +432,7 @@ fork_save_infrun_state (struct fork_info *fp) > /* Kill 'em all, let God sort 'em out... */ > > void > -linux_fork_killall (void) > +linux_fork_killall (inferior *inf) > { > /* Walk list and kill every pid. No need to treat the > current inferior_ptid as special (we do not return a > @@ -306,6 +440,7 @@ linux_fork_killall (void) > or a parent, so may get a SIGCHLD from a previously > killed child. Wait them all out. */ > > + auto &fork_list = ::fork_list (inf); > for (fork_info &fi : fork_list) > { > pid_t pid = fi.ptid.pid (); > @@ -324,6 +459,7 @@ linux_fork_killall (void) > > /* Clear list, prepare to start fresh. */ > fork_list.clear (); > + reset_highest_fork_num (inf); > } > > /* The current inferior_ptid has exited, but there are other viable > @@ -331,10 +467,11 @@ linux_fork_killall (void) > first available. */ > > void > -linux_fork_mourn_inferior (void) > +linux_fork_mourn_inferior () > { > struct fork_info *last; > int status; > + inferior *inf = current_inferior (); > > /* Wait just one more time to collect the inferior's exit status. > Do not check whether this succeeds though, since we may be > @@ -343,23 +480,23 @@ linux_fork_mourn_inferior (void) > gdb::waitpid (inferior_ptid.pid (), &status, 0); > > /* OK, presumably inferior_ptid is the one who has exited. > - We need to delete that one from the fork_list, and switch > + We need to delete that one from the fork list, and switch > to the next available fork. */ > - delete_fork (inferior_ptid); > + delete_fork (inferior_ptid, inf); > > /* There should still be a fork - if there's only one left, > delete_fork won't remove it, because we haven't updated > inferior_ptid yet. */ > - gdb_assert (!fork_list.empty ()); > + gdb_assert (!fork_list (inf).empty ()); > > - last = find_last_fork (); > + last = find_last_fork (inf); > fork_load_infrun_state (last); > gdb_printf (_("[Switching to %s]\n"), > target_pid_to_str (inferior_ptid).c_str ()); > > /* If there's only one fork, switch back to non-fork mode. */ > - if (one_fork_p ()) > - delete_fork (inferior_ptid); > + if (one_fork_p (inf)) > + delete_fork (inferior_ptid, inf); > } > > /* The current inferior_ptid is being detached, but there are other > @@ -367,13 +504,13 @@ linux_fork_mourn_inferior (void) > the first available. */ > > void > -linux_fork_detach (int from_tty, lwp_info *lp) > +linux_fork_detach (int from_tty, lwp_info *lp, inferior *inf) > { > gdb_assert (lp != nullptr); > gdb_assert (lp->ptid == inferior_ptid); > > /* OK, inferior_ptid is the one we are detaching from. We need to > - delete it from the fork_list, and switch to the next available > + delete it from the fork list, and switch to the next available > fork. But before doing the detach, do make sure that the lwp > hasn't exited or been terminated first. */ > > @@ -386,11 +523,12 @@ linux_fork_detach (int from_tty, lwp_info *lp) > target_pid_to_str (inferior_ptid).c_str ()); > } > > - delete_fork (inferior_ptid); > + delete_fork (inferior_ptid, inf); > > /* There should still be a fork - if there's only one left, > delete_fork won't remove it, because we haven't updated > inferior_ptid yet. */ > + auto &fork_list = ::fork_list (inf); > gdb_assert (!fork_list.empty ()); > > fork_load_infrun_state (&fork_list.front ()); > @@ -400,8 +538,8 @@ linux_fork_detach (int from_tty, lwp_info *lp) > target_pid_to_str (inferior_ptid).c_str ()); > > /* If there's only one fork, switch back to non-fork mode. */ > - if (one_fork_p ()) > - delete_fork (inferior_ptid); > + if (one_fork_p (inf)) > + delete_fork (inferior_ptid, inf); > } > > /* Temporarily switch to the infrun state stored on the fork_info > @@ -414,19 +552,26 @@ class scoped_switch_fork_info > /* Switch to the infrun state held on the fork_info identified by > PPTID. If PPTID is the current inferior then no switch is done. */ > explicit scoped_switch_fork_info (ptid_t pptid) > - : m_oldfp (nullptr) > + : m_oldfp (nullptr), m_oldinf (nullptr) > { > if (pptid != inferior_ptid) > { > - struct fork_info *newfp = nullptr; > - > /* Switch to pptid. */ > - m_oldfp = find_fork_ptid (inferior_ptid); > + auto [oldfp, oldinf] = find_fork_ptid (inferior_ptid); > + m_oldfp = oldfp; > gdb_assert (m_oldfp != nullptr); > - newfp = find_fork_ptid (pptid); > + auto [newfp, newinf] = find_fork_ptid (pptid); > gdb_assert (newfp != nullptr); > fork_save_infrun_state (m_oldfp); > remove_breakpoints (); > + > + if (oldinf != newinf) > + { > + thread_info *tp = any_thread_of_inferior (newinf); > + switch_to_thread (tp); > + m_oldinf = oldinf; > + } > + > fork_load_infrun_state (newfp); > insert_breakpoints (); > } > @@ -436,12 +581,17 @@ class scoped_switch_fork_info > didn't need to switch states, then nothing is done here either. */ > ~scoped_switch_fork_info () > { > - if (m_oldfp != nullptr) > + if (m_oldinf != nullptr || m_oldfp != nullptr) > { > /* Switch back to inferior_ptid. */ > try > { > remove_breakpoints (); > + if (m_oldinf != nullptr) > + { > + thread_info *tp = any_thread_of_inferior (m_oldinf); > + switch_to_thread (tp); > + } > fork_load_infrun_state (m_oldfp); > insert_breakpoints (); > } > @@ -473,8 +623,16 @@ class scoped_switch_fork_info > we were already in the desired state, and nothing needs to be > restored. */ > struct fork_info *m_oldfp; > + > + /* When switching to a different fork, this is the inferior for the > + fork that we're switching from, and to which we'll switch back once > + end-of-scope is reached. It may also be nullptr if no switching > + is required. */ > + inferior *m_oldinf; > }; > > +/* Call waitpid() by making an inferior function call. */ > + > static int > inferior_call_waitpid (ptid_t pptid, int pid) > { > @@ -517,30 +675,25 @@ static void > delete_checkpoint_command (const char *args, int from_tty) > { > ptid_t ptid, pptid; > - struct fork_info *fi; > > if (!args || !*args) > error (_("Requires argument (checkpoint id to delete)")); > > - ptid = fork_id_to_ptid (parse_and_eval_long (args)); > - if (ptid == minus_one_ptid) > - error (_("No such checkpoint id, %s"), args); > + auto [fi, inf] = parse_checkpoint_id (args); > + ptid = fi->ptid; > + gdb_assert (fi != nullptr); > + pptid = fi->parent_ptid; > > - if (ptid == inferior_ptid) > - error (_("\ > -Please switch to another checkpoint before deleting the current one")); > + if (ptid.pid () == inf->pid) > + error (_("Cannot delete active checkpoint")); > > if (ptrace (PTRACE_KILL, ptid.pid (), 0, 0)) > error (_("Unable to kill pid %s"), target_pid_to_str (ptid).c_str ()); > > - fi = find_fork_ptid (ptid); > - gdb_assert (fi); > - pptid = fi->parent_ptid; > - > if (from_tty) > gdb_printf (_("Killed %s\n"), target_pid_to_str (ptid).c_str ()); > > - delete_fork (ptid); > + delete_fork (ptid, inf); > > if (pptid == null_ptid) > { > @@ -558,7 +711,7 @@ Please switch to another checkpoint before deleting the current one")); > If fi->parent_ptid is a part of lwp and it is stopped, waitpid the > ptid. */ > thread_info *parent = linux_target->find_thread (pptid); > - if ((parent == NULL && find_fork_ptid (pptid)) > + if ((parent == NULL && find_fork_ptid (pptid).first != nullptr) > || (parent != NULL && parent->state == THREAD_STOPPED)) > { > if (inferior_call_waitpid (pptid, ptid.pid ())) > @@ -575,9 +728,8 @@ detach_checkpoint_command (const char *args, int from_tty) > if (!args || !*args) > error (_("Requires argument (checkpoint id to detach)")); > > - ptid = fork_id_to_ptid (parse_and_eval_long (args)); > - if (ptid == minus_one_ptid) > - error (_("No such checkpoint id, %s"), args); > + auto [fi, inf] = parse_checkpoint_id (args); > + ptid = fi->ptid; > > if (ptid == inferior_ptid) > error (_("\ > @@ -589,7 +741,7 @@ Please switch to another checkpoint before detaching the current one")); > if (from_tty) > gdb_printf (_("Detached %s\n"), target_pid_to_str (ptid).c_str ()); > > - delete_fork (ptid); > + delete_fork (ptid, current_inferior ()); > } > > /* Print information about currently known checkpoints. */ > @@ -598,70 +750,139 @@ static void > info_checkpoints_command (const char *arg, int from_tty) > { > struct gdbarch *gdbarch = get_current_arch (); > - int requested = -1; > - bool printed = false; > + struct inferior *cur_inf = current_inferior (); > + inferior *req_inf = nullptr; > + fork_info *req_fi = nullptr; > + bool will_print_something = false; > > if (arg && *arg) > - requested = (int) parse_and_eval_long (arg); > + std::tie (req_fi, req_inf) = parse_checkpoint_id (arg); > + > + /* Figure out whether to print the inferior number in the > + checkpoint list. */ > + bool print_inf = (number_of_inferiors () > 1); > > - for (const fork_info &fi : fork_list) > + /* Compute widths of some of the table components. */ > + size_t inf_width = 0; > + size_t num_width = 0; > + size_t targid_width = 0; > + for (inferior *inf : all_inferiors (linux_target)) > { > - if (requested > 0 && fi.num != requested) > + if (req_inf != nullptr && req_inf != inf) > continue; > - printed = true; > > - bool is_current = fi.ptid == inferior_ptid; > - if (is_current) > - gdb_printf ("* "); > - else > - gdb_printf (" "); > + scoped_restore_current_pspace_and_thread restore_pspace_thread; > + switch_to_program_space_and_thread (inf->pspace); > > - gdb_printf ("%d %s", fi.num, target_pid_to_str (fi.ptid).c_str ()); > - if (fi.num == 0) > - gdb_printf (_(" (main process)")); > - > - if (is_current && inferior_thread ()->state == THREAD_RUNNING) > + for (const fork_info &fi : fork_list (inf)) > { > - gdb_printf (_(" <running>\n")); > - continue; > - } > - > - gdb_printf (_(" at ")); > - ULONGEST pc > - = (is_current > - ? regcache_read_pc (get_thread_regcache (inferior_thread ())) > - : fi.pc); > - gdb_puts (paddress (gdbarch, pc)); > - > - symtab_and_line sal = find_pc_line (pc, 0); > - if (sal.symtab) > - gdb_printf (_(", file %s"), > - symtab_to_filename_for_display (sal.symtab)); > - if (sal.line) > - gdb_printf (_(", line %d"), sal.line); > - if (!sal.symtab && !sal.line) > - { > - bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); > - if (msym.minsym) > - gdb_printf (", <%s>", msym.minsym->linkage_name ()); > + if (req_fi != nullptr && req_fi != &fi) > + continue; > + > + will_print_something = true; > + > + inf_width > + = std::max (inf_width, > + string_printf ("%d", inf->num).size ()); > + num_width > + = std::max (num_width, > + string_printf ("%d", fi.num).size () > + + (print_inf ? 1 : 0)); > + targid_width > + = std::max (targid_width, > + target_pid_to_str (fi.ptid).size ()); > } > + } > > - gdb_putc ('\n'); > + /* Return early if there are no checkpoints to print. */ > + if (!will_print_something) > + { > + gdb_printf (_("No checkpoints.\n")); > + return; > } > > - if (!printed) > + /* Ensure that column header width doesn't exceed that of the column data > + for the Id field. */ > + if (!print_inf && num_width < 2) > + num_width = 2; > + > + /* Print column headers... */ > + gdb_printf (" "); > + gdb_printf ("%-*s", (print_inf ? (int) inf_width : 0) > + + (int) num_width + 1, "Id"); > + gdb_printf ("Active "); > + gdb_printf ("%-*s", (int) targid_width + 1, "Target Id"); > + gdb_printf ("Frame\n"); > + > + /* Print each checkpoint padded, as needed, with spaces so that everything > + lines up. */ > + for (inferior *inf : all_inferiors (linux_target)) > { > - if (requested > 0) > - gdb_printf (_("No checkpoint number %d.\n"), requested); > - else > - gdb_printf (_("No checkpoints.\n")); > + if (req_inf != nullptr && req_inf != inf) > + continue; > + > + scoped_restore_current_pspace_and_thread restore_pspace_thread; > + switch_to_program_space_and_thread (inf->pspace); > + > + for (const fork_info &fi : fork_list (inf)) > + { > + if (req_fi != nullptr && req_fi != &fi) > + continue; > + > + thread_info *t = any_thread_of_inferior (inf); > + bool is_current = fi.ptid.pid () == inf->pid; > + if (is_current && cur_inf == inf) > + gdb_printf ("* "); > + else > + gdb_printf (" "); > + > + if (print_inf) > + gdb_printf ("%*d.%-*d", (int) inf_width, inf->num, > + (int) num_width, fi.num); > + else > + gdb_printf ("%*d ", (int) num_width, fi.num); > + > + /* Print out 'y' or 'n' for whether the checkpoint is current. */ > + gdb_printf ("%-7s", is_current ? "y" : "n"); > + > + /* Print target id. */ > + gdb_printf ("%-*s", (int) targid_width, > + target_pid_to_str (fi.ptid).c_str ()); > + > + if (t->state == THREAD_RUNNING && is_current) > + gdb_printf (_(" (running)")); > + else > + { > + gdb_printf (_(" at ")); > + ULONGEST pc > + = (is_current > + ? regcache_read_pc (get_thread_regcache (t)) > + : fi.pc); > + gdb_puts (paddress (gdbarch, pc)); > + > + symtab_and_line sal = find_pc_line (pc, 0); > + if (sal.symtab) > + gdb_printf (_(", file %s"), > + symtab_to_filename_for_display (sal.symtab)); > + if (sal.line) > + gdb_printf (_(", line %d"), sal.line); > + if (!sal.symtab && !sal.line) > + { > + bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); > + if (msym.minsym) > + gdb_printf (", <%s>", msym.minsym->linkage_name ()); > + } > + } > + > + gdb_putc ('\n'); > + } > } > } > > /* The PID of the process we're checkpointing. */ > static int checkpointing_pid = 0; > > -int > +bool > linux_fork_checkpointing_p (int pid) > { > return (checkpointing_pid == pid); > @@ -691,17 +912,16 @@ checkpoint_command (const char *args, int from_tty) > struct target_waitstatus last_target_waitstatus; > ptid_t last_target_ptid; > struct value *fork_fn = NULL, *ret; > - struct fork_info *fp; > pid_t retpid; > > - if (!target_has_execution ()) > + if (!target_has_execution ()) > error (_("The program is not being run.")); > > /* Ensure that the inferior is not multithreaded. */ > update_thread_list (); > if (inf_has_multiple_threads ()) > error (_("checkpoint: can't checkpoint multiple threads.")); > - > + > /* Make the inferior fork, record its (and gdb's) state. */ > > if (lookup_minimal_symbol (current_program_space, "fork").minsym != nullptr) > @@ -730,14 +950,21 @@ checkpoint_command (const char *args, int from_tty) > retpid = value_as_long (ret); > get_last_target_status (nullptr, &last_target_ptid, &last_target_waitstatus); > > - fp = find_fork_pid (retpid); > + auto [fp, inf] = find_fork_pid (retpid); > + > + if (!fp) > + error (_("Failed to find new fork")); > > if (from_tty) > { > int parent_pid; > > - gdb_printf (_("checkpoint %d: fork returned pid %ld.\n"), > - fp != NULL ? fp->num : -1, (long) retpid); > + gdb_printf (_("checkpoint %s: fork returned pid %ld.\n"), > + ((number_of_inferiors () > 1) > + ? string_printf ("%d.%d", inf->num, fp->num).c_str () > + : string_printf ("%d", fp->num).c_str ()), > + (long) retpid); > + > if (info_verbose) > { > parent_pid = last_target_ptid.lwp (); > @@ -748,15 +975,12 @@ checkpoint_command (const char *args, int from_tty) > } > } > > - if (!fp) > - error (_("Failed to find new fork")); > - > - if (one_fork_p ()) > + if (one_fork_p (inf)) > { > /* Special case -- if this is the first fork in the list (the > - list was hitherto empty), then add inferior_ptid first, as a > - special zeroeth fork id. */ > - fork_list.emplace_front (inferior_ptid.pid ()); > + list was hitherto empty), then add inferior_ptid as a special > + zeroeth fork id. */ > + fork_list (inf).emplace_front (inferior_ptid.pid (), 0); > } > > fork_save_infrun_state (fp); > @@ -764,40 +988,57 @@ checkpoint_command (const char *args, int from_tty) > } > > static void > -linux_fork_context (struct fork_info *newfp, int from_tty) > +linux_fork_context (struct fork_info *newfp, int from_tty, inferior *newinf) > { > - /* Now we attempt to switch processes. */ > - struct fork_info *oldfp; > + bool inferior_changed = false; > > + /* Now we attempt to switch processes. */ > gdb_assert (newfp != NULL); > > - oldfp = find_fork_ptid (inferior_ptid); > - gdb_assert (oldfp != NULL); > + if (newinf != current_inferior ()) > + { > + thread_info *tp = any_thread_of_inferior (newinf); > + switch_to_thread (tp); > + inferior_changed = true; > + } > > - fork_save_infrun_state (oldfp); > - remove_breakpoints (); > - fork_load_infrun_state (newfp); > - insert_breakpoints (); > + auto [oldfp, oldinf] = find_fork_ptid (inferior_ptid); > + gdb_assert (oldfp != NULL); > > - gdb_printf (_("Switching to %s\n"), > - target_pid_to_str (inferior_ptid).c_str ()); > + if (oldfp != newfp) > + { > + fork_save_infrun_state (oldfp); > + remove_breakpoints (); > + fork_load_infrun_state (newfp); > + insert_breakpoints (); > + if (!inferior_changed) > + gdb_printf (_("Switching to %s\n"), > + target_pid_to_str (inferior_ptid).c_str ()); > + } > > - print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); > + notify_user_selected_context_changed > + (inferior_changed ? (USER_SELECTED_INFERIOR | USER_SELECTED_FRAME) > + : USER_SELECTED_FRAME); > } > > /* Switch inferior process (checkpoint) context, by checkpoint id. */ > + > static void > restart_command (const char *args, int from_tty) > { > - struct fork_info *fp; > - > if (!args || !*args) > error (_("Requires argument (checkpoint id to restart)")); > > - if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) > - error (_("Not found: checkpoint id %s"), args); > + auto [fp, inf] = parse_checkpoint_id (args); > + > + /* Don't allow switching from a thread/fork that's running. */ > + inferior *curinf = current_inferior (); > + if (curinf->pid != 0 > + && any_thread_of_inferior (curinf)->state == THREAD_RUNNING) > + error (_("Cannot execute this command while " > + "the selected thread is running.")); > > - linux_fork_context (fp, from_tty); > + linux_fork_context (fp, from_tty, inf); > } > > void _initialize_linux_fork (); > diff --git a/gdb/linux-fork.h b/gdb/linux-fork.h > index c553aaf0740..0ed47eda15f 100644 > --- a/gdb/linux-fork.h > +++ b/gdb/linux-fork.h > @@ -22,12 +22,13 @@ > > struct fork_info; > struct lwp_info; > -extern void add_fork (pid_t); > -extern struct fork_info *find_fork_pid (pid_t); > -extern void linux_fork_killall (void); > -extern void linux_fork_mourn_inferior (void); > -extern void linux_fork_detach (int, lwp_info *); > -extern int forks_exist_p (void); > -extern int linux_fork_checkpointing_p (int); > +class inferior; > +extern void add_fork (pid_t, inferior *inf); > +extern std::pair<fork_info *, inferior *> find_fork_pid (pid_t); > +extern void linux_fork_killall (inferior *inf); > +extern void linux_fork_mourn_inferior (); > +extern void linux_fork_detach (int, lwp_info *, inferior *inf); > +extern bool forks_exist_p (inferior *inf); > +extern bool linux_fork_checkpointing_p (int); > > #endif /* LINUX_FORK_H */ > diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c > index 3f252370c7b..5b2cc8f2fb8 100644 > --- a/gdb/linux-nat.c > +++ b/gdb/linux-nat.c > @@ -1558,13 +1558,13 @@ linux_nat_target::detach (inferior *inf, int from_tty) > gdb_assert (num_lwps (pid) == 1 > || (target_is_non_stop_p () && num_lwps (pid) == 0)); > > - if (pid == inferior_ptid.pid () && forks_exist_p ()) > + if (forks_exist_p (inf)) > { > /* Multi-fork case. The current inferior_ptid is being detached > from, but there are other viable forks to debug. Detach from > the current fork, and context-switch to the first > available. */ > - linux_fork_detach (from_tty, find_lwp_pid (ptid_t (pid))); > + linux_fork_detach (from_tty, find_lwp_pid (ptid_t (pid)), inf); > } > else > { > @@ -2082,8 +2082,12 @@ linux_handle_extended_wait (struct lwp_info *lp, int status) > detach_breakpoints (ptid_t (new_pid, new_pid)); > > /* Retain child fork in ptrace (stopped) state. */ > - if (!find_fork_pid (new_pid)) > - add_fork (new_pid); > + if (find_fork_pid (new_pid).first == nullptr) > + { > + struct inferior *inf = find_inferior_ptid (linux_target, > + lp->ptid); > + add_fork (new_pid, inf); > + } > > /* Report as spurious, so that infrun doesn't want to follow > this fork. We're actually doing an infcall in > @@ -3729,8 +3733,8 @@ linux_nat_target::kill () > parent will be sleeping if this is a vfork. */ > iterate_over_lwps (pid_ptid, kill_unfollowed_child_callback); > > - if (forks_exist_p ()) > - linux_fork_killall (); > + if (forks_exist_p (current_inferior ())) > + linux_fork_killall (current_inferior ()); > else > { > /* Stop all threads before killing them, since ptrace requires > @@ -3761,7 +3765,7 @@ linux_nat_target::mourn_inferior () > > close_proc_mem_file (pid); > > - if (! forks_exist_p ()) > + if (! forks_exist_p (current_inferior ())) > /* Normal case, no other forks available. */ > inf_ptrace_target::mourn_inferior (); > else > diff --git a/gdb/testsuite/gdb.base/checkpoint.exp b/gdb/testsuite/gdb.base/checkpoint.exp > index 31b5a13bb8f..4a8a9a8eb7f 100644 > --- a/gdb/testsuite/gdb.base/checkpoint.exp > +++ b/gdb/testsuite/gdb.base/checkpoint.exp > @@ -274,17 +274,17 @@ gdb_test "kill" "" "kill all one" \ > # and confirm that all are gone > # > > -gdb_test "restart 0" "Not found.*" "no more checkpoint 0" > -gdb_test "restart 1" "Not found.*" "no more checkpoint 1" > -gdb_test "restart 2" "Not found.*" "no more checkpoint 2" > -gdb_test "restart 3" "Not found.*" "no more checkpoint 3" > -gdb_test "restart 4" "Not found.*" "no more checkpoint 4" > -gdb_test "restart 5" "Not found.*" "no more checkpoint 5" > -gdb_test "restart 6" "Not found.*" "no more checkpoint 6" > -gdb_test "restart 7" "Not found.*" "no more checkpoint 7" > -gdb_test "restart 8" "Not found.*" "no more checkpoint 8" > -gdb_test "restart 9" "Not found.*" "no more checkpoint 9" > -gdb_test "restart 10" "Not found.*" "no more checkpoint 10" > +gdb_test "restart 0" "has no checkpoints" "no more checkpoint 0" > +gdb_test "restart 1" "has no checkpoints" "no more checkpoint 1" > +gdb_test "restart 2" "has no checkpoints" "no more checkpoint 2" > +gdb_test "restart 3" "has no checkpoints" "no more checkpoint 3" > +gdb_test "restart 4" "has no checkpoints" "no more checkpoint 4" > +gdb_test "restart 5" "has no checkpoints" "no more checkpoint 5" > +gdb_test "restart 6" "has no checkpoints" "no more checkpoint 6" > +gdb_test "restart 7" "has no checkpoints" "no more checkpoint 7" > +gdb_test "restart 8" "has no checkpoints" "no more checkpoint 8" > +gdb_test "restart 9" "has no checkpoints" "no more checkpoint 9" > +gdb_test "restart 10" "has no checkpoints" "no more checkpoint 10" > > # > # Now let's try setting a large number of checkpoints (>600) > @@ -331,7 +331,7 @@ gdb_assert { $nr_ok == 600 } "break1 with many checkpoints" > set count 0 > set msg "info checkpoints with at least 600 checkpoints" > gdb_test_multiple "info checkpoints" $msg { > - -re "\r\n $decimal process \[^\r\]*" { > + -re "\r\n *$decimal n +process \[^\r\]*" { > incr count > exp_continue > } > diff --git a/gdb/testsuite/gdb.multi/checkpoint-multi.exp b/gdb/testsuite/gdb.multi/checkpoint-multi.exp > new file mode 100644 > index 00000000000..2bd654347b0 > --- /dev/null > +++ b/gdb/testsuite/gdb.multi/checkpoint-multi.exp > @@ -0,0 +1,800 @@ > +# Copyright 2009-2024 Free Software Foundation, Inc. > + > +# This program is free software; you can redistribute it and/or modify > +# it under the terms of the GNU General Public License as published by > +# the Free Software Foundation; either version 3 of the License, or > +# (at your option) any later version. > +# > +# This program is distributed in the hope that it will be useful, > +# but WITHOUT ANY WARRANTY; without even the implied warranty of > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +# GNU General Public License for more details. > +# > +# You should have received a copy of the GNU General Public License > +# along with this program. If not, see <http://www.gnu.org/licenses/>. > + > +# This file tests various scenarios involving multiple inferiors > +# and the checkpoint command. > + > +# Checkpoint support works only on Linux. > +require {istarget "*-*-linux*"} > + > +# Checkpoint support is implemented for the (Linux) native target. > +require gdb_protocol_is_native > + > +set checkpoints_header_re " +Id +Active Target Id +Frame.*?" > +set proc_re "(?:process $::decimal|Thread $::hex \\(LWP $::decimal\\))" > +set ckpt_re "checkpoint" > +set main_proc "\\(main process\\)" > +set hello_c "hello\\.c" > +set goodbye_c "goodbye\\.c" > +set hangout_c "hangout\\.c" > + > +set testfile "checkpoint-multi" > + > +set exec1 "hello" > +set srcfile1 ${exec1}.c > +set binfile1 [standard_output_file ${exec1}] > + > +set exec2 "goodbye" > +set srcfile2 ${exec2}.c > +set binfile2 [standard_output_file ${exec2}] > + > +set exec3 "hangout" > +set srcfile3 ${exec3}.c > +set binfile3 [standard_output_file ${exec3}] > + > +if { [build_executable ${testfile}.exp ${exec1} "${srcfile1}" {debug}] == -1 } { > + return -1 > +} > + > +if { [build_executable ${testfile}.exp ${exec2} "${srcfile2}" {debug}] == -1 } { > + return -1 > +} > + > +if { [build_executable ${testfile}.exp ${exec3} "${srcfile3}" {debug}] == -1 } { > + return -1 > +} > + > +# Start two inferiors, place a checkpoint on inferior 2, but switch > +# back to inferior 1. > +proc start_2_inferiors_checkpoint_on_inf_2 {} { > + clean_restart $::exec1 > + > + # Start inferior 1. > + if {[gdb_start_cmd] < 0} { > + fail "start first inferior" > + } else { > + gdb_test "" "main.*" "start first inferior" > + } > + > + # Add a new inferior and exec into it. > + gdb_test "add-inferior -exec $::binfile2" \ > + "Added inferior 2.*" \ > + "add inferior 2 with -exec $::exec2" > + > + # Check that we have multiple inferiors. > + gdb_test "info inferiors" \ > + "Executable.*$::exec1.*$::exec2.*" > + > + # Switch to inferior 2. > + gdb_test "inferior 2" \ > + "Switching to inferior 2.*$::exec2.*" > + > + # Start inferior 2: > + if {[gdb_start_cmd] < 0} { > + fail "start second inferior" > + } else { > + gdb_test "" "main.*" "start second inferior" > + } > + > + # Set a checkpoint in inferior 2 > + gdb_test "checkpoint" "$::ckpt_re 2\\.1: fork returned pid $::decimal.*" > + > + # Step one line in inferior 2. > + gdb_test "step" "glob = 46;" > + > + # Switch back to inferior 1. > + gdb_test "inferior 1" "Switching to inferior 1.*$::exec1.*" > +} > + > +# Start two inferiors, place a checkpoint on inferior 2, but switch > +# back to inferior 1. This is like the one above, except that it > +# swaps the executables loaded into inferior 1 and inferior 2. This > +# is important for being able to test "continue to exit". (Because... > +# hello.c has an infinite loop, but goodbye.c doesn't. In order to > +# test "continue to exit", we need to continue in an executable which > +# will actually exit.) > + > +proc start_2_inferiors_checkpoint_on_inf_2_alt {} { > + clean_restart $::exec2 > + > + # Start inferior 1. > + if {[gdb_start_cmd] < 0} { > + fail "start first inferior" > + } else { > + gdb_test "" "main.*" "start first inferior" > + } > + > + # Add a new inferior and exec exec1 into it. > + gdb_test "add-inferior -exec $::binfile1" \ > + "Added inferior 2.*" \ > + "add inferior 2 with -exec $::exec1" > + > + # Check that we have two inferiors. > + gdb_test "info inferiors" \ > + "Executable.*$::exec2.*$::exec1.*" > + > + # Switch to inferior 2. > + gdb_test "inferior 2" \ > + "Switching to inferior 2.*$::exec1.*" > + > + # Start inferior 2: > + if {[gdb_start_cmd] < 0} { > + fail "start second inferior" > + } else { > + gdb_test "" "main.*" "start second inferior" > + } > + > + # Set a checkpoint in inferior 2 > + gdb_test "checkpoint" "$::ckpt_re 2\\.1: fork returned pid $::decimal.*" > + > + # next one line in inferior 2. > + gdb_test "next" "bar\\(\\).*" > + > + # Switch back to inferior 1. > + gdb_test "inferior 1" "Switching to inferior 1.*$::exec2.*" > +} > + > +with_test_prefix "check detach on non-checkpointed inferior" { > + start_2_inferiors_checkpoint_on_inf_2 > + gdb_test "detach" "Detaching from program.*$::exec1.*Inferior 1.*detached.*" > +} > + > +with_test_prefix "check kill on non-checkpointed inferior" { > + start_2_inferiors_checkpoint_on_inf_2 > + gdb_test "kill" "" "kill non-checkpointed inferior" \ > + "Kill the program being debugged.*y or n. $" "y" > +} > + > +with_test_prefix "check restart 0 on non-checkpointed inferior" { > + start_2_inferiors_checkpoint_on_inf_2 > + gdb_test "restart 0" "Inferior 1 has no checkpoints" > + gdb_test "restart 2.0" "Switching to inferior 2.*?goodbye.*?#0 +mailand .*?glob = 46;.*" > +} > + > +with_test_prefix "check restart 1 on non-checkpointed inferior" { > + start_2_inferiors_checkpoint_on_inf_2 > + gdb_test "restart 1" "Inferior 1 has no checkpoints" > + gdb_test "restart 2.1" "Switching to inferior 2.*?goodbye.*?#0 +main .*?mailand\\(\\);.*" > +} > + > +with_test_prefix "check continue to exit on non-checkpointed inferior" { > + start_2_inferiors_checkpoint_on_inf_2_alt > + gdb_test "continue" "Inferior 1.*? exited normally.*" > +} > + > +with_test_prefix "two inferiors with checkpoints" { > + start_2_inferiors_checkpoint_on_inf_2 > + with_test_prefix "one checkpoint" { > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " +2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " +2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + with_test_prefix "two checkpoints" { > + gdb_test "checkpoint" "$ckpt_re 1\\.1: fork returned pid $::decimal.*" \ > + "checkpoint in inferior 1" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + # Note: No switching is done here since checkpoint 0 is the active one. > + gdb_test "restart 0" "main.*?$hello_c.*?alarm \\(240\\);" > + > + gdb_test "restart 2.0" \ > + "\\\[Switching to inferior 2.*?mailand.*?glob = 46;.*" > + gdb_test "next" "\}" > + > + with_test_prefix "restart 1" { > + gdb_test "restart 1" "^Switching to $proc_re.*?#0 main \\(\\) at.*?$goodbye_c.*mailand\\(\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "info checkpoints twice in a row" { > + # Doing "info_checkpoints" twice in a row might seem pointless, > + # but during work on making the checkpoint code inferior aware, > + # there was a point at which doing it twice in a row did not > + # produce the same output. > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "restart 0" { > + # Switch back to checkpoint 0; again, there should be no > + # "Switching to inferior" message. > + gdb_test "restart 0" \ > + "^Switching to $proc_re.*?#0 mailand \\(\\) at.*?$goodbye_c.*\}" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + "\\* 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + # Try switching to invalid checkpoints: > + with_test_prefix "invalid checkpoints" { > + gdb_test "restart 3" "Invalid checkpoint number 3 for inferior 2" > + gdb_test "restart 2" "Invalid checkpoint number 2 for inferior 2" > + gdb_test "restart -1" "Checkpoint number must be a non-negative integer" > + gdb_test "restart 2.3" "Invalid checkpoint number 3 for inferior 2" > + gdb_test "restart 3.0" "No inferior number '3'" > + gdb_test "restart 1.2" "Invalid checkpoint number 2 for inferior 1" > + gdb_test "restart 1.3" "Invalid checkpoint number 3 for inferior 1" > + gdb_test "restart 1.-1" "Checkpoint number must be a non-negative integer" > + gdb_test "restart -1.0" "Inferior number must be a positive integer" > + } > + > + with_test_prefix "restart 1.1" { > + # Switch to checkpoint 1.1; this time, we should see a "Switching to > + # inferior" message. > + gdb_test "restart 1.1" \ > + "\\\[Switching to inferior 1.*?main.*?$hello_c.*?alarm \\(240\\);" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + "\\* 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "restart 2.1" { > + gdb_test "restart 2.1" \ > + "Switching to inferior 2.*?#0 main \\(\\) at.*?$goodbye_c.*mailand\\(\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "second checkpoint in inferior 2" { > + gdb_test "checkpoint" "$ckpt_re 2\\.2: fork returned pid $::decimal.*" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "third checkpoint in inferior 2" { > + gdb_test "checkpoint" "$ckpt_re 2.3: fork returned pid $::decimal.*" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.3 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "continue to exit in checkpoint 2.1" { > + gdb_test "continue" \ > + "Inferior 2 \\(process $decimal\\) exited normally.*?Switching to $proc_re.*?" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.3 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "continue to exit in checkpoint 2.3" { > + gdb_test "continue" \ > + "Inferior 2 \\(process $decimal\\) exited normally.*?Switching to process $decimal.*?" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "continue to exit in checkpoint 2.2" { > + gdb_test "continue" \ > + "Inferior 2 \\(process $decimal\\) exited normally.*?Switching to process $decimal.*?" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?"] > + } > + > + with_test_prefix "new checkpoints in inferior 2" { > + gdb_test "checkpoint" "$ckpt_re 2.1: fork returned pid $::decimal.*" \ > + "checkpoint 2.1" > + > + gdb_test "checkpoint" "$ckpt_re 2.2: fork returned pid $::decimal.*" \ > + "checkpoint 2.2" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + "\\* 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "delete checkpoint 2.0" { > + gdb_test "delete checkpoint 2.0" \ > + "Cannot delete active checkpoint" \ > + "failed attempt to delete active checkpoint 2.0" > + > + gdb_test "restart 2.1" \ > + "^Switching to process.*?#0 mailand \\(\\) at.*?$goodbye_c.*\}" > + > + gdb_test "delete checkpoint 2.0" \ > + "Killed process $::decimal" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "delete checkpoint 2.2" { > + gdb_test "delete checkpoint 2.2" \ > + "Killed process $::decimal" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?"] > + } > + > + with_test_prefix "new checkpoint in inferior 2" { > + gdb_test "checkpoint" "$ckpt_re 2.1: fork returned pid $::decimal.*" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + "\\* 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "switch to inferior 1" { > + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + "\\* 1\\.1 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "kill inferior 1" { > + gdb_test "kill" "\\\[Inferior 1 \\(process $::decimal\\) killed\\\]" \ > + "kill inferior 1" \ > + "Kill the program being debugged.*y or n. $" "y" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "start inferior 1 again" { > + gdb_test "checkpoint" "The program is not being run\\." \ > + "checkpoint in non-running inferior" > + > + gdb_test "start" "Starting program.*?hello.*?alarm \\(240\\);" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "checkpoint 1.1" { > + gdb_test "checkpoint" "$ckpt_re 1.1: fork returned pid $::decimal.*" \ > + "second checkpoint in inferior 1" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > +} > + > +with_test_prefix "three inferiors with checkpoints" { > + start_2_inferiors_checkpoint_on_inf_2 > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + > + with_test_prefix "add third inferior" { > + # Add a third inferior and exec into it. > + gdb_test "add-inferior -exec $::binfile3" \ > + "Added inferior 3.*" \ > + "add inferior 3 with -exec $::exec3" > + > + # Check that we have three inferiors. > + gdb_test "info inferiors" \ > + "Executable.*?\\* 1 .*?$::exec1.*? 2 .*?$::exec2.*? 3 .*?$::exec3.*?" \ > + "check for three inferiors" > + > + # Switch to inferior 3. > + gdb_test "inferior 3" \ > + "Switching to inferior 3.*$::exec3.*" > + > + # Start inferior 2: > + if {[gdb_start_cmd] < 0} { > + fail "start third inferior" > + } else { > + gdb_test "" "main.*" "start third inferior" > + } > + > + gdb_test "checkpoint" "$ckpt_re 3\\.1: fork returned pid $::decimal.*" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "make checkpoint in inferior 1" { > + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" > + > + gdb_test "checkpoint" "$ckpt_re 1\\.1: fork returned pid $::decimal.*" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "restart 2.1" { > + gdb_test "restart 2.1" \ > + "Switching to inferior 2.*?#0 main \\(\\) at.*?$goodbye_c.*mailand\\(\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "next and make new checkpoint" { > + gdb_test "next" "foo\\(glob\\);" > + gdb_test "checkpoint" "$ckpt_re 2\\.2: fork returned pid $::decimal.*" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "switch to inferior 3 for upcoming kill" { > + gdb_test "inferior 3" "Switching to inferior 3.*?alarm \\(30\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "kill inferior 3" { > + gdb_test "kill" "\\\[Inferior 3 \\(process $::decimal\\) killed\\\]" \ > + "kill inferior 3" \ > + "Kill the program being debugged.*y or n. $" "y" > + > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] > + } > + > + with_test_prefix "delete checkpoint 2.0" { > + gdb_test "delete checkpoint 0" \ > + "Inferior 3 has no checkpoints" > + gdb_test "delete checkpoint 2.0" \ > + "Killed process $::decimal" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] > + } > + > + with_test_prefix "restart 2.2" { > + gdb_test "restart 2.2" \ > + "Switching to inferior 2.*?#0 main \\(\\) at.*?$goodbye_c.*foo\\(glob\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] > + } > + > + with_test_prefix "switch to non-running inferior 3" { > + gdb_test "inferior 3" "\\\[Switching to inferior 3 \\\[<null>\\\] \\(.*?$::exec3\\)\\\]" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" ] > + } > + > + with_test_prefix "restart inferior 3 and make new checkpoints" { > + gdb_test "start" "Starting program.*?hangout.*?alarm \\(30\\);" > + gdb_test "checkpoint" \ > + "$ckpt_re 3\\.1: fork returned pid $::decimal.*" \ > + "checkpoint 3.1" > + gdb_test "checkpoint" \ > + "$ckpt_re 3\\.2: fork returned pid $::decimal.*" \ > + "checkpoint 3.2" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.1 n +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "delete checkpoint 3.1" { > + gdb_test "delete checkpoint 1" \ > + "Killed process $::decimal" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "attempt to delete active checkpoint in non-current inferior" { > + # Switch to inferior 1, add another checkpoint - so that there > + # are three of them in inferior 1 - then switch back to > + # inferior 3 and delete active checkpoint in inferior 1. > + # Then, switch to inferior 1 and attempt to add another > + # checkpoint. During development, a "Cannot access memory at > + # address ..." message was seen. This was a bug - there were > + # several problems - but one of them was that the checkpoint in > + # question was an "active" checkpoint. The fix was to > + # disallow this case. > + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" > + gdb_test "checkpoint" "$ckpt_re 1\\.2: fork returned pid $::decimal.*" > + gdb_test "inferior 3" "Switching to inferior 3.*?alarm \\(30\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + > + # Check that deleting active checkpoints in other (non-current) > + # inferiors is disallowed. > + gdb_test "delete checkpoint 1.0" \ > + "Cannot delete active checkpoint" > + } > + > + with_test_prefix "delete non-active checkpoint in non-current inferior" { > + # But deleting non-active checkpoints, even in other inferiors, > + # should work. > + gdb_test "delete checkpoint 1.1" \ > + "Killed process $::decimal" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "switch to inferior 1" { > + gdb_test "inferior 1" "Switching to inferior 1.*?alarm \\(240\\);" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "checkpoint 1.3" { > + gdb_test "checkpoint" "$ckpt_re 1\\.3: fork returned pid $::decimal.*" \ > + "third checkpoint in inferior 1" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.2 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.3 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.2 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 3\\.0 y +$proc_re +at $::hex, file.*?$hangout_c.*?" \ > + " 3\\.2 n +$proc_re +at $::hex, file.*?$hangout_c.*?"] > + } > + > + with_test_prefix "attempt to remove active but not current inferior" { > + gdb_test "x/i \$pc" "=> $::hex <main.*" > + gdb_test "remove-inferior 3" \ > + "warning: Can not remove active inferior 3\." > + } > +} > + > +with_test_prefix "background execution" { > + start_2_inferiors_checkpoint_on_inf_2 > + with_test_prefix "one checkpoint" { > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 2.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + with_test_prefix "two checkpoints" { > + gdb_test "checkpoint" "$ckpt_re 1\\.1: fork returned pid $::decimal.*" \ > + "checkpoint in inferior 1" > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "background continue hello" { > + gdb_test "continue &" "Continuing\." > + gdb_test "info checkpoints" \ > + [multi_line \ > + "\\* 1\\.0 y +$proc_re \\(running\\)" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "fail to switch to inferior 2 w/ 1 in background" { > + gdb_test "restart 2.1" "Cannot execute this command while the selected thread is running." > + # Should be no change from earlier output. > + gdb_test "info checkpoints" \ > + [multi_line \ > + "\\* 1\\.0 y +$proc_re \\(running\\)" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > + > + with_test_prefix "switch to inferior 2" { > + set msg "stop thread" > + gdb_test_multiple "interrupt" $msg { > + -re "$gdb_prompt " { > + gdb_test_multiple "" $msg { > + -re "Thread 1\\.1 \"hello\" received signal SIGINT, Interrupt\\." { > + pass $gdb_test_name > + } > + } > + } > + } > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + "\\* 1\\.0 y +$proc_re +at $::hex,.*" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 y +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + " 2\\.1 n +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + gdb_test "restart 2.1" "Switching to inferior 2.*?goodbye.*?#0 +main .*?mailand\\(\\);.*" > + } > + > + with_test_prefix "after restart 2.1" { > + gdb_test "info checkpoints" \ > + [multi_line \ > + "$checkpoints_header_re" \ > + " 1\\.0 y +$proc_re +at $::hex,.*" \ > + " 1\\.1 n +$proc_re +at $::hex, file.*?$hello_c.*?" \ > + " 2\\.0 n +$proc_re +at $::hex, file.*?$goodbye_c.*?" \ > + "\\* 2\\.1 y +$proc_re +at $::hex, file.*?$goodbye_c.*?"] > + } > +} > -- > 2.46.2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v5 1/4] Make linux checkpoints work with multiple inferiors 2025-01-30 17:45 ` Andrew Burgess @ 2025-02-05 22:06 ` Kevin Buettner 0 siblings, 0 replies; 10+ messages in thread From: Kevin Buettner @ 2025-02-05 22:06 UTC (permalink / raw) To: Andrew Burgess; +Cc: gdb-patches, pedro On Thu, 30 Jan 2025 17:45:25 +0000 Andrew Burgess <aburgess@redhat.com> wrote: > Additionally, there are a couple of places in this patch where you have > spaces instead of tabs, and trailing whitespace. I have this in my > ~/.gitconfig file: > > [alias] > check=show --check --format="%C(auto)%h\\ %s\\ %C(blue)[%an\\ (%cr)]%C(reset)" > > Then you can do `git check` to check the last commit, or `git check SHA` > to check some other commit. The output will highlight any whitespace > issues with the patch. Thanks! I've added those lines to my own ~/.gitconfig file and will use "git check" going forward. [...] > > Before: > > > > (gdb) info checkpoints > > * 0 Thread 0x7ffff7cd3740 (LWP 84201) (main process) at 0x40114a, file hello.c, line 28 > > 1 process 84205 at 0x401199, file hello.c, line 51 > > 2 process 84206 at 0x4011a3, file hello.c, line 53 > > > > After: > > > > (gdb) info checkpoints > > Id Active Target Id Frame > > * 0 y process 551311 at 0x40114a, file hello.c, line 28 > > 1 n process 551314 at 0x401199, file hello.c, line 51 > > 2 n process 551315 at 0x4011a3, file hello.c, line 53 > > I'd like to suggest using GDB's structured table generation mechanism > here. There's two great examples; 'print_inferior' in inferior.c (which > does the 'info inferiors' command) and 'print_thread_info_1' in thread.c > (which is the 'info threads' command). > > I'd also like to suggest, that for the 'Frame' column, you make use of > print_stack_frame(). This is the function that 'info threads' uses to > print its stack frame, I think having consistent output would be a win > here. > > That all said, if you wanted to merge this now, and then we could > possibly come back and tweak the output formatting and content > afterwards, I'd be fine with that too. > > Either way: > > Approved-By: Andrew Burgess <aburgess@redhat.com> Thanks for the review! I've pushed this series. (Though I did first fix the whitespace problems, made sure that things still built and checked that the affected tests still ran as expected.) I'll work on a separate patch to use GDB's mechanism for structured table generation. (I'll also incorporate your print_stack_frame() suggestion.) Thanks for the pointers! Kevin ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v5 2/4] Capitalize output of successful checkpoint command 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 1/4] " Kevin Buettner @ 2024-12-10 1:54 ` Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 3/4] Print only process ptids from linux-fork.c Kevin Buettner ` (3 subsequent siblings) 5 siblings, 0 replies; 10+ messages in thread From: Kevin Buettner @ 2024-12-10 1:54 UTC (permalink / raw) To: gdb-patches; +Cc: pedro, Kevin Buettner This commit causes the output of a "checkpoint" command to be changed from: checkpoint N: fork returned pid DDDD to: Checkpoint N: fork returned pid DDDD This change was made to bring the output of the "checkpoint" command in line with that of other commands, e.g.: (gdb) break main Breakpoint 1 at ... (gdb) catch exec Catchpoint 2 (exec) (gdb) add-inferior [New inferior 2] Added inferior 2 The tests gdb.base/checkpoint.exp, gdb.base/kill-during-detach.exp, and gdb.multi/checkpoint-multi.exp have been updated to accept the new (capitalized) output from the "checkpoint" command. --- gdb/linux-fork.c | 2 +- gdb/testsuite/gdb.base/checkpoint.exp | 2 +- gdb/testsuite/gdb.base/kill-during-detach.exp | 2 +- gdb/testsuite/gdb.multi/checkpoint-multi.exp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c index 49c2cf1ed84..af7d4b60819 100644 --- a/gdb/linux-fork.c +++ b/gdb/linux-fork.c @@ -959,7 +959,7 @@ checkpoint_command (const char *args, int from_tty) { int parent_pid; - gdb_printf (_("checkpoint %s: fork returned pid %ld.\n"), + gdb_printf (_("Checkpoint %s: fork returned pid %ld.\n"), ((number_of_inferiors () > 1) ? string_printf ("%d.%d", inf->num, fp->num).c_str () : string_printf ("%d", fp->num).c_str ()), diff --git a/gdb/testsuite/gdb.base/checkpoint.exp b/gdb/testsuite/gdb.base/checkpoint.exp index 4a8a9a8eb7f..68bddd8244d 100644 --- a/gdb/testsuite/gdb.base/checkpoint.exp +++ b/gdb/testsuite/gdb.base/checkpoint.exp @@ -359,7 +359,7 @@ with_test_prefix "delete checkpoint 0" { clean_restart $binfile runto_main - gdb_test "checkpoint" "checkpoint 1: fork returned pid $decimal\\." + gdb_test "checkpoint" "Checkpoint 1: fork returned pid $decimal\\." gdb_test "restart 1" "Switching to .*" gdb_test "delete checkpoint 0" "Killed process $decimal" gdb_test "info checkpoints" [string_to_regexp "No checkpoints."] diff --git a/gdb/testsuite/gdb.base/kill-during-detach.exp b/gdb/testsuite/gdb.base/kill-during-detach.exp index 68292cc3c51..d9ab8ee5383 100644 --- a/gdb/testsuite/gdb.base/kill-during-detach.exp +++ b/gdb/testsuite/gdb.base/kill-during-detach.exp @@ -93,7 +93,7 @@ proc run_test { exit_p checkpoint_p } { # Set the checkpoint. gdb_test "checkpoint" \ - "checkpoint 1: fork returned pid $::decimal\\." + "Checkpoint 1: fork returned pid $::decimal\\." } # Must get the PID before we resume the inferior. diff --git a/gdb/testsuite/gdb.multi/checkpoint-multi.exp b/gdb/testsuite/gdb.multi/checkpoint-multi.exp index 2bd654347b0..4860608e704 100644 --- a/gdb/testsuite/gdb.multi/checkpoint-multi.exp +++ b/gdb/testsuite/gdb.multi/checkpoint-multi.exp @@ -24,7 +24,7 @@ require gdb_protocol_is_native set checkpoints_header_re " +Id +Active Target Id +Frame.*?" set proc_re "(?:process $::decimal|Thread $::hex \\(LWP $::decimal\\))" -set ckpt_re "checkpoint" +set ckpt_re "Checkpoint" set main_proc "\\(main process\\)" set hello_c "hello\\.c" set goodbye_c "goodbye\\.c" -- 2.46.2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v5 3/4] Print only process ptids from linux-fork.c 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 1/4] " Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 2/4] Capitalize output of successful checkpoint command Kevin Buettner @ 2024-12-10 1:54 ` Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 4/4] Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple inferiors Kevin Buettner ` (2 subsequent siblings) 5 siblings, 0 replies; 10+ messages in thread From: Kevin Buettner @ 2024-12-10 1:54 UTC (permalink / raw) To: gdb-patches; +Cc: pedro, Kevin Buettner This commit causes a "process ptid" to be passed to all calls of target_pid_to_str in linux-fork.c. A "process ptid" is one in which only the pid component is set to a non-zero value; both the lwp and tid components are zero. The reason for doing this is that pids associated with checkpoints can never be a thread due to the fact that checkpoints (which are implemented by forking a process) can only (reasonably) work with single-threaded processes. Without this commit, many of the "info checkpoints" commands in gdb.multi/checkpoint-multi.exp will incorrectly show some of the checkpoints as threads. E.g... Id Active Target Id Frame * 1.0 y Thread 0x7ffff7cb5740 (LWP 581704) at 0x401199, file hello.c, line 51 1.2 n process 581716 at 0x401199, file hello.c, line 51 1.3 n process 581717 at 0x401199, file hello.c, line 51 2.1 n process 581708 at 0x401258, file goodbye.c, line 62 2.2 y Thread 0x7ffff7cb5740 (LWP 581712) at 0x401258, file goodbye.c, line 62 3.0 y Thread 0x7ffff7cb5740 (LWP 581713) at 0x40115c, file hangout.c, line 31 3.2 n process 581715 at 0x40115c, file hangout.c, line 31 (gdb With this commit in place, the output looks like this instead: Id Active Target Id Frame * 1.0 y process 535276 at 0x401199, file hello.c, line 51 1.2 n process 535288 at 0x401199, file hello.c, line 51 1.3 n process 535289 at 0x401199, file hello.c, line 51 2.1 n process 535280 at 0x401258, file goodbye.c, line 62 2.2 y process 535284 at 0x401258, file goodbye.c, line 62 3.0 y process 535285 at 0x40115c, file hangout.c, line 31 3.2 n process 535287 at 0x40115c, file hangout.c, line 31 (For brevity, I've removed the directory elements in each of the paths above.) The testcase, gdb.multi/checkpoint-multi.exp, has been updated to reflect the fact that only "process" should now appear in output from "info checkpoints". --- gdb/linux-fork.c | 39 ++++++++++++++------ gdb/testsuite/gdb.multi/checkpoint-multi.exp | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c index af7d4b60819..f93e14bcac7 100644 --- a/gdb/linux-fork.c +++ b/gdb/linux-fork.c @@ -429,6 +429,17 @@ fork_save_infrun_state (struct fork_info *fp) } } +/* Given a ptid, return a "process ptid" in which only the pid member + is present. This is used in calls to target_pid_to_str() to ensure + that only process ptids are printed by this file. */ + +static inline ptid_t +proc_ptid (ptid_t ptid) +{ + ptid_t process_ptid (ptid.pid ()); + return process_ptid; +} + /* Kill 'em all, let God sort 'em out... */ void @@ -492,7 +503,7 @@ linux_fork_mourn_inferior () last = find_last_fork (inf); fork_load_infrun_state (last); gdb_printf (_("[Switching to %s]\n"), - target_pid_to_str (inferior_ptid).c_str ()); + target_pid_to_str (proc_ptid (inferior_ptid)).c_str ()); /* If there's only one fork, switch back to non-fork mode. */ if (one_fork_p (inf)) @@ -520,7 +531,7 @@ linux_fork_detach (int from_tty, lwp_info *lp, inferior *inf) { if (ptrace (PTRACE_DETACH, inferior_ptid.pid (), 0, 0)) error (_("Unable to detach %s"), - target_pid_to_str (inferior_ptid).c_str ()); + target_pid_to_str (proc_ptid (inferior_ptid)).c_str ()); } delete_fork (inferior_ptid, inf); @@ -535,7 +546,7 @@ linux_fork_detach (int from_tty, lwp_info *lp, inferior *inf) if (from_tty) gdb_printf (_("[Switching to %s]\n"), - target_pid_to_str (inferior_ptid).c_str ()); + target_pid_to_str (proc_ptid (inferior_ptid)).c_str ()); /* If there's only one fork, switch back to non-fork mode. */ if (one_fork_p (inf)) @@ -610,7 +621,7 @@ class scoped_switch_fork_info catch (const gdb_exception &ex) { warning (_("Couldn't restore checkpoint state in %s: %s"), - target_pid_to_str (m_oldfp->ptid).c_str (), + target_pid_to_str (proc_ptid (m_oldfp->ptid)).c_str (), ex.what ()); } } @@ -688,10 +699,12 @@ delete_checkpoint_command (const char *args, int from_tty) error (_("Cannot delete active checkpoint")); if (ptrace (PTRACE_KILL, ptid.pid (), 0, 0)) - error (_("Unable to kill pid %s"), target_pid_to_str (ptid).c_str ()); + error (_("Unable to kill pid %s"), + target_pid_to_str (proc_ptid (ptid)).c_str ()); if (from_tty) - gdb_printf (_("Killed %s\n"), target_pid_to_str (ptid).c_str ()); + gdb_printf (_("Killed %s\n"), + target_pid_to_str (proc_ptid (ptid)).c_str ()); delete_fork (ptid, inf); @@ -716,7 +729,7 @@ delete_checkpoint_command (const char *args, int from_tty) { if (inferior_call_waitpid (pptid, ptid.pid ())) warning (_("Unable to wait pid %s"), - target_pid_to_str (ptid).c_str ()); + target_pid_to_str (proc_ptid (ptid)).c_str ()); } } @@ -736,10 +749,12 @@ detach_checkpoint_command (const char *args, int from_tty) Please switch to another checkpoint before detaching the current one")); if (ptrace (PTRACE_DETACH, ptid.pid (), 0, 0)) - error (_("Unable to detach %s"), target_pid_to_str (ptid).c_str ()); + error (_("Unable to detach %s"), + target_pid_to_str (proc_ptid (ptid)).c_str ()); if (from_tty) - gdb_printf (_("Detached %s\n"), target_pid_to_str (ptid).c_str ()); + gdb_printf (_("Detached %s\n"), + target_pid_to_str (proc_ptid (ptid)).c_str ()); delete_fork (ptid, current_inferior ()); } @@ -790,7 +805,7 @@ info_checkpoints_command (const char *arg, int from_tty) + (print_inf ? 1 : 0)); targid_width = std::max (targid_width, - target_pid_to_str (fi.ptid).size ()); + target_pid_to_str (proc_ptid (fi.ptid)).size ()); } } @@ -847,7 +862,7 @@ info_checkpoints_command (const char *arg, int from_tty) /* Print target id. */ gdb_printf ("%-*s", (int) targid_width, - target_pid_to_str (fi.ptid).c_str ()); + target_pid_to_str (proc_ptid (fi.ptid)).c_str ()); if (t->state == THREAD_RUNNING && is_current) gdb_printf (_(" (running)")); @@ -1013,7 +1028,7 @@ linux_fork_context (struct fork_info *newfp, int from_tty, inferior *newinf) insert_breakpoints (); if (!inferior_changed) gdb_printf (_("Switching to %s\n"), - target_pid_to_str (inferior_ptid).c_str ()); + target_pid_to_str (proc_ptid (inferior_ptid)).c_str ()); } notify_user_selected_context_changed diff --git a/gdb/testsuite/gdb.multi/checkpoint-multi.exp b/gdb/testsuite/gdb.multi/checkpoint-multi.exp index 4860608e704..e401e5e9555 100644 --- a/gdb/testsuite/gdb.multi/checkpoint-multi.exp +++ b/gdb/testsuite/gdb.multi/checkpoint-multi.exp @@ -23,7 +23,7 @@ require {istarget "*-*-linux*"} require gdb_protocol_is_native set checkpoints_header_re " +Id +Active Target Id +Frame.*?" -set proc_re "(?:process $::decimal|Thread $::hex \\(LWP $::decimal\\))" +set proc_re "(?:process $::decimal)" set ckpt_re "Checkpoint" set main_proc "\\(main process\\)" set hello_c "hello\\.c" -- 2.46.2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v5 4/4] Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple inferiors 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner ` (2 preceding siblings ...) 2024-12-10 1:54 ` [PATCH v5 3/4] Print only process ptids from linux-fork.c Kevin Buettner @ 2024-12-10 1:54 ` Kevin Buettner 2024-12-10 13:04 ` Eli Zaretskii 2024-12-19 19:32 ` [PATCH v5 0/4] Make linux checkpoints work with " Tom Tromey 2025-01-30 17:46 ` Andrew Burgess 5 siblings, 1 reply; 10+ messages in thread From: Kevin Buettner @ 2024-12-10 1:54 UTC (permalink / raw) To: gdb-patches; +Cc: pedro, Kevin Buettner --- gdb/NEWS | 2 ++ gdb/doc/gdb.texinfo | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gdb/NEWS b/gdb/NEWS index 361d7726ba0..0c2d3637ce7 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -174,6 +174,8 @@ vFile:stat vFile:fstat but takes a filename rather than an open file descriptor. +* Linux checkpoint code has been updated to work with multiple inferiors. + *** Changes in GDB 15 * The MPX commands "show/set mpx bound" have been deprecated, as Intel diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 85ac3d9aab6..05a6acd95b2 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -4322,18 +4322,36 @@ listed: @table @code @item Checkpoint ID +@item Active state indicator @item Process ID @item Code Address @item Source line, or label @end table +Checkpoint IDs will be displayed as either a non-negative integer or +in the form @var{I}.@var{N}, where @var{I} is the inferior number, a +positive integer, as shown by the command @code{info inferiors}, and +@var{N}, a non-negative integer, is the checkpoint number for that +inferior. The single non-negative integer form is used when +there is only one inferior. The @var{I}.@var{N} form is used when +there are multiple inferiors. + +The active state indicator is a single letter, either @code{y} or +@code{n}, indicating yes or no. Only one checkpoint per inferior may +be active at once. The active checkpoint in the current inferior is +also shown by a @code{*} at the start of the line. Checkpoints whose +active state is @code{n} can be switched to using the @code{restart} +command or deleted using the @code{delete checkpoint} command. + @kindex restart @var{checkpoint-id} @item restart @var{checkpoint-id} Restore the program state that was saved as checkpoint number @var{checkpoint-id}. All program variables, registers, stack frames etc.@: will be returned to the values that they had when the checkpoint was saved. In essence, gdb will ``wind back the clock'' to the point -in time when the checkpoint was saved. +in time when the checkpoint was saved. The checkpoint number +@var{checkpoint-id} is specified in the same form as that output by the +@code{info checkpoints} command. Note that breakpoints, @value{GDBN} variables, command history etc. are not affected by restoring a checkpoint. In general, a checkpoint -- 2.46.2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v5 4/4] Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple inferiors 2024-12-10 1:54 ` [PATCH v5 4/4] Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple inferiors Kevin Buettner @ 2024-12-10 13:04 ` Eli Zaretskii 0 siblings, 0 replies; 10+ messages in thread From: Eli Zaretskii @ 2024-12-10 13:04 UTC (permalink / raw) To: Kevin Buettner; +Cc: gdb-patches, pedro > From: Kevin Buettner <kevinb@redhat.com> > Cc: pedro@palves.net, > Kevin Buettner <kevinb@redhat.com> > Date: Mon, 9 Dec 2024 18:54:07 -0700 > > --- > gdb/NEWS | 2 ++ > gdb/doc/gdb.texinfo | 20 +++++++++++++++++++- > 2 files changed, 21 insertions(+), 1 deletion(-) Thanks. > --- a/gdb/NEWS > +++ b/gdb/NEWS > @@ -174,6 +174,8 @@ vFile:stat > vFile:fstat but takes a filename rather than an open file > descriptor. > > +* Linux checkpoint code has been updated to work with multiple inferiors. > + This part is okay. > +Checkpoint IDs will be displayed as either a non-negative integer or > +in the form @var{I}.@var{N}, where @var{I} is the inferior number, a > +positive integer, as shown by the command @code{info inferiors}, and > +@var{N}, a non-negative integer, is the checkpoint number for that > +inferior. The single non-negative integer form is used when > +there is only one inferior. The @var{I}.@var{N} form is used when > +there are multiple inferiors. Please always use lower-case letters inside @var. In Info format, they are capitalized by makeinfo, but in other formats we get slanted typeface which is much more pretty than upper-case. > +The active state indicator is a single letter, either @code{y} or > +@code{n}, indicating yes or no. Only one checkpoint per inferior may > +be active at once. The active checkpoint in the current inferior is > +also shown by a @code{*} at the start of the line. Checkpoints whose > +active state is @code{n} can be switched to Please use @samp for the indicators, not @code. These are not words, so they will look strange without quotes in formats other than Info (where makeinfo quotes them). The gdb.texinfo part is okay with these nits fixed. Reviewed-By: Eli Zaretskii <eliz@gnu.org> ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner ` (3 preceding siblings ...) 2024-12-10 1:54 ` [PATCH v5 4/4] Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple inferiors Kevin Buettner @ 2024-12-19 19:32 ` Tom Tromey 2025-01-30 17:46 ` Andrew Burgess 5 siblings, 0 replies; 10+ messages in thread From: Tom Tromey @ 2024-12-19 19:32 UTC (permalink / raw) To: Kevin Buettner; +Cc: gdb-patches, pedro >>>>> "Kevin" == Kevin Buettner <kevinb@redhat.com> writes: Kevin> This v5 series makes several changes to the output for the Kevin> "info checkpoints" command. As a consequence, both checkpoint related Kevin> tests as well as the documentation were adjusted for these changes. I read through this series and it looks good to me. Thank you for doing this. Reviewed-By: Tom Tromey <tom@tromey.com> Tom ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner ` (4 preceding siblings ...) 2024-12-19 19:32 ` [PATCH v5 0/4] Make linux checkpoints work with " Tom Tromey @ 2025-01-30 17:46 ` Andrew Burgess 5 siblings, 0 replies; 10+ messages in thread From: Andrew Burgess @ 2025-01-30 17:46 UTC (permalink / raw) To: Kevin Buettner, gdb-patches; +Cc: pedro, Kevin Buettner Kevin Buettner <kevinb@redhat.com> writes: > This series fixes some problems with the current checkpoint code. The > first patch makes the checkpoint code inferior aware, fixing a number > of bugs. The second and third patches are largely cosmetic - they > make changes to checkpoint related output. The fourth patch updates > the documentation. I had some specific comments for patch #1. But I've looked through all the patches in this series, and they look good. Approved-By: Andrew Burgess <aburgess@redhat.com> Thanks, Andrew > > The v2 series incorporated Pedro's suggestions regarding the > numbering of checkpoint ids. See the first patch for details. The > tests have been revised to account for these changes and new tests > have been added as well. > > The v3 series split out a cosmetic change from the first patch. It > capitalized the output of a successful checkpoint command. This was > prompted by the Linaro regression tester, which, due to the > capitalization change, found two regressions in > gdb.base/kill-during-detach.exp. (I had made a mistake during my own > testing causing this to not be caught.) > > The v4 series addressed Pedro's concerns from his review of the v3 > series. It also adds a NEWS entry and updates the GDB manual with > regard to checkpoint identifiers. > > This v5 series makes several changes to the output for the > "info checkpoints" command. As a consequence, both checkpoint related > tests as well as the documentation were adjusted for these changes. > > Kevin Buettner (4): > Make linux checkpoints work with multiple inferiors > Capitalize output of successful checkpoint command > Print only process ptids from linux-fork.c > Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple > inferiors > > gdb/NEWS | 2 + > gdb/doc/gdb.texinfo | 20 +- > gdb/linux-fork.c | 590 +++++++++---- > gdb/linux-fork.h | 15 +- > gdb/linux-nat.c | 18 +- > gdb/testsuite/gdb.base/checkpoint.exp | 26 +- > gdb/testsuite/gdb.base/kill-during-detach.exp | 2 +- > gdb/testsuite/gdb.multi/checkpoint-multi.exp | 800 ++++++++++++++++++ > 8 files changed, 1277 insertions(+), 196 deletions(-) > create mode 100644 gdb/testsuite/gdb.multi/checkpoint-multi.exp > > -- > 2.46.2 ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2025-02-05 22:07 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2024-12-10 1:54 [PATCH v5 0/4] Make linux checkpoints work with multiple inferiors Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 1/4] " Kevin Buettner 2025-01-30 17:45 ` Andrew Burgess 2025-02-05 22:06 ` Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 2/4] Capitalize output of successful checkpoint command Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 3/4] Print only process ptids from linux-fork.c Kevin Buettner 2024-12-10 1:54 ` [PATCH v5 4/4] Linux checkpoints: Update NEWS and gdb.texinfo regarding multiple inferiors Kevin Buettner 2024-12-10 13:04 ` Eli Zaretskii 2024-12-19 19:32 ` [PATCH v5 0/4] Make linux checkpoints work with " Tom Tromey 2025-01-30 17:46 ` Andrew Burgess
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox