public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCHv2 0/3] Restore thread and frame patches
@ 2020-04-27 22:04 Andrew Burgess
  2020-04-27 22:04 ` [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior Andrew Burgess
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Andrew Burgess @ 2020-04-27 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Andrew Burgess

This series merges together and replaces two previously separate patches:

  https://sourceware.org/pipermail/gdb-patches/2020-February/166202.html
  https://sourceware.org/pipermail/gdb-patches/2020-April/167215.html

The first had no feedback, while the second had some positive
feedback, but also some negative feedback.

The concerns raised were that making this change broke the existing
GDB debug model, and added unneeded complexity.  I'm not sure what I
can do about the complexity, but to address the first point I've made
all of these changes optional in both the first and third patches of
this new series, and the default for both the new options is off.

In the first patch of this new series I updated the technique I use
for preserving the thread, previously I was storing inferior_ptid,
however, after seeing Pedro's recent series[1], I switched to storing
the thread_info pointer.

I tested this series on top of current master, and also on top of
Pedro's recent series[1].

All feedback welcome.

Thanks,
Andrew

[1] https://sourceware.org/pipermail/gdb-patches/2020-April/167578.html

---

Andrew Burgess (3):
  gdb: Restore previously selected thread when switching inferior
  gdb: Unify two copies of restore_selected_frame
  gdb: Track the current frame for each thread

 gdb/ChangeLog                                 |  52 +++
 gdb/NEWS                                      |  19 +
 gdb/doc/ChangeLog                             |  14 +
 gdb/doc/gdb.texinfo                           |  42 ++-
 gdb/frame.c                                   |  87 +++++
 gdb/frame.h                                   |  68 ++++
 gdb/gdbthread.h                               |  15 +-
 gdb/inferior.c                                |  58 ++-
 gdb/inferior.h                                |  10 +
 gdb/infrun.c                                  |  26 +-
 gdb/testsuite/ChangeLog                       |  10 +
 .../gdb.threads/restore-selected-frame.c      |  85 +++++
 .../gdb.threads/restore-selected-frame.exp    | 336 ++++++++++++++++++
 gdb/testsuite/gdb.threads/restore-thread.c    | 246 +++++++++++++
 gdb/testsuite/gdb.threads/restore-thread.exp  | 220 ++++++++++++
 gdb/thread.c                                  | 135 +++----
 16 files changed, 1315 insertions(+), 108 deletions(-)
 create mode 100644 gdb/testsuite/gdb.threads/restore-selected-frame.c
 create mode 100644 gdb/testsuite/gdb.threads/restore-selected-frame.exp
 create mode 100644 gdb/testsuite/gdb.threads/restore-thread.c
 create mode 100644 gdb/testsuite/gdb.threads/restore-thread.exp

-- 
2.25.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior
  2020-04-27 22:04 [PATCHv2 0/3] Restore thread and frame patches Andrew Burgess
@ 2020-04-27 22:04 ` Andrew Burgess
  2020-04-28 15:49   ` Aktemur, Tankut Baris
  2020-04-27 22:04 ` [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame Andrew Burgess
  2020-04-27 22:04 ` [PATCHv2 3/3] gdb: Track the current frame for each thread Andrew Burgess
  2 siblings, 1 reply; 7+ messages in thread
From: Andrew Burgess @ 2020-04-27 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Andrew Burgess

This commit adds a new option that allows the user to control how GDB
behaves when switching between multi-threaded inferiors.

Currently (and this remains the default after this commit) when
switching between inferiors GDB would select the first non-exited
thread from the inferior being switched to.

This commit adds the following new commands:

     set restore-selected-thread on|off
     show restore-selected-thread

This option is off by default in order to retain the existing
behaviour, but, when switched on GDB will remember which thread was
selected in each inferior.  As the user switches between inferiors GDB
will attempt to restore the previously selected thread.

If the previously selected thread is no longer available, for example,
if the thread has exited, then GDB will fall back on the old
behaviour.

I did consider, but eventually didn't implemented, adding a warning
when switching inferiors if the previously selected thread is no
longer available.  My reasoning here is that GDB should already have
informed the user that the thread has exited, and there is already a
message indicating which thread has been switched too, so adding an
extra warning felt like unneeded clutter.

In order to store the thread within the inferior I initially stored
the value of inferior_ptid, but we have recently been moving GDB away
from relying on inferior_ptid, so now I store a pointer to the
thread_info for the previously selected thread.

Unfortunately, to get the current thread_info I need to call
inferior_thread, however, this should only be called when the inferior
is alive and has a thread, so this doesn't work:

  $ gdb
  (gdb) add-inferior
  (gdb) inferior 2
  ./gdb/thread.c:95: internal-error: thread_info* inferior_thread(): Assertion `current_thread_ != nullptr' failed.

To avoid this I first check that inferior_ptid is not null_ptid, which
isn't ideal, I'd hoped to avoid adding any extra inferior_ptid
references, but right now I don't see a better solution.

There's a new test for this functionality.

gdb/ChangeLog:

	* inferior.c (inferior_command): Store current thread_info before
	switching inferiors.  Reselect the previous thread_info if
	possible after switching to the new inferior.
	(initialize_inferiors): Register restore-selected-thread option.
	* inferior.h (class inferior) <previous_thread_info>: New member
	variable.
	* NEWS: Mention new feature.

gdb/testsuite/ChangeLog:

	* gdb.threads/restore-thread.c: New file.
	* gdb.threads/restore-thread.exp: New file.

gdb/doc/ChangeLog:

	* gdb.texinfo (Inferiors Connections and Programs): Mention thread
	tracking within the inferior command.
	(Threads): Mention thread tracking in the general thread
	discussion.
---
 gdb/ChangeLog                                |  10 +
 gdb/NEWS                                     |   9 +
 gdb/doc/ChangeLog                            |   7 +
 gdb/doc/gdb.texinfo                          |  19 +-
 gdb/inferior.c                               |  58 ++++-
 gdb/inferior.h                               |  10 +
 gdb/testsuite/ChangeLog                      |   5 +
 gdb/testsuite/gdb.threads/restore-thread.c   | 246 +++++++++++++++++++
 gdb/testsuite/gdb.threads/restore-thread.exp | 220 +++++++++++++++++
 9 files changed, 582 insertions(+), 2 deletions(-)
 create mode 100644 gdb/testsuite/gdb.threads/restore-thread.c
 create mode 100644 gdb/testsuite/gdb.threads/restore-thread.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index 01e73c9e5ea..ad743508e23 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -59,6 +59,15 @@ tui new-layout NAME WINDOW WEIGHT [WINDOW WEIGHT]...
   Define a new TUI layout, specifying its name and the windows that
   will be displayed.
 
+set restore-selected-thread on|off
+show restore-selected-thread
+  This new option is off by default.  When turned on GDB will record
+  the currently selected thread in each inferior.  When switching
+  between inferiors GDB will attempt to restore the previously
+  selected thread in the inferior being switched too.  If the
+  previously selected thread is no longer available then GDB falls
+  back to selecting the first non-exited thread.
+
 * New targets
 
 GNU/Linux/RISC-V (gdbserver)	riscv*-*-linux*
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 239c078af33..3ee6e78c593 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3095,11 +3095,25 @@
 To switch focus between inferiors, use the @code{inferior} command:
 
 @table @code
+@anchor{inferior command}
 @kindex inferior @var{infno}
 @item inferior @var{infno}
 Make inferior number @var{infno} the current inferior.  The argument
 @var{infno} is the inferior number assigned by @value{GDBN}, as shown
 in the first field of the @samp{info inferiors} display.
+
+When switching between inferiors with multiple threads
+(@pxref{Threads}) @value{GDBN} will select the first non-exited thread
+in the inferior being switched to and make this the current thread.
+
+@kindex set restore-selected-thread
+@kindex show restore-selected-thread
+@item set restore-selected-thread @r{[}on|off@r{]}
+@item show restore-selected-thread
+When this option is on @value{GDBN} will record the currently selected
+thread in each inferior.  When switching between inferior @value{GDBN}
+will try to restore the previously selected thread in the inferior
+being switched to.  This option is off by default.
 @end table
 
 @vindex $_inferior@r{, convenience variable}
@@ -3481,7 +3495,10 @@
 
 If you're debugging multiple inferiors, @value{GDBN} displays thread
 IDs using the qualified @var{inferior-num}.@var{thread-num} format.
-Otherwise, only @var{thread-num} is shown.
+Otherwise, only @var{thread-num} is shown.  When switching between
+inferiors @value{GDBN} will select a suitable thread in the inferior
+being switched to, see @ref{inferior command,,the @code{inferior}
+command} for further details on how to control this behaviour.
 
 If you specify the @samp{-gid} option, @value{GDBN} displays a column
 indicating each thread's global thread ID:
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 2f4ced0788d..db69e5d34d9 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -613,6 +613,23 @@ switch_to_inferior_no_thread (inferior *inf)
   set_current_program_space (inf->pspace);
 }
 
+/* When this is true GDB restores the inferiors previously selected thread
+   each time the inferior is changed (where possible).  */
+
+static bool restore_selected_thread_per_inferior = false;
+
+/* Implement 'show restore-selected-thread'.  */
+
+static void
+show_restore_selected_thread_per_inferior (struct ui_file *file, int from_tty,
+					   struct cmd_list_element *c,
+					   const char *value)
+{
+  fprintf_filtered (file,
+		    _("Restoring the selected thread is currently %s.\n"),
+		    value);
+}
+
 static void
 inferior_command (const char *args, int from_tty)
 {
@@ -625,11 +642,38 @@ inferior_command (const char *args, int from_tty)
   if (inf == NULL)
     error (_("Inferior ID %d not known."), num);
 
+  /* We can only call INFERIOR_THREAD if the inferior is known to have an
+     active thread, which it wont if the inferior is currently exited.  So,
+     first check if we currently have a thread selected.  */
+  if (inferior_ptid != null_ptid)
+    {
+      /* Now take a strong reference to the current thread_info and store
+	 it within the inferior, this prevents the thread_info from being
+	 deleted until the inferior has released the reference.  */
+      thread_info *tp = inferior_thread ();
+      tp->incref ();
+      current_inferior ()->previous_thread_info.reset (tp);
+    }
+
   if (inf->pid != 0)
     {
       if (inf != current_inferior ())
 	{
-	  thread_info *tp = any_thread_of_inferior (inf);
+	  thread_info *tp = nullptr;
+
+	  if (restore_selected_thread_per_inferior
+	      && inf->previous_thread_info != nullptr)
+	    {
+	      /* Release the reference to the previous thread.  We don't
+		 switch back to this thread if it is already exited
+		 though.  */
+	      tp = inf->previous_thread_info.release ();
+	      tp->decref ();
+	      if (tp->state == THREAD_EXITED)
+		tp = nullptr;
+	    }
+	  if (tp == nullptr)
+	    tp = any_thread_of_inferior (inf);
 	  if (tp == NULL)
 	    error (_("Inferior has no threads."));
 
@@ -1007,5 +1051,17 @@ Show printing of inferior events (such as inferior start and exit)."), NULL,
          show_print_inferior_events,
          &setprintlist, &showprintlist);
 
+  add_setshow_boolean_cmd ("restore-selected-thread",
+			   no_class, &restore_selected_thread_per_inferior,
+                          _("\
+Set whether GDB restores the selected thread when switching inferiors."), _("\
+Show whether GDB restores the selected thread when switching inferiors."), _("\
+When this option is on GDB will record the currently selected thread for\n\
+each inferior, and restore the selected thread whenever GDB switches inferiors."),
+                          nullptr,
+                          show_restore_selected_thread_per_inferior,
+                          &setlist,
+                          &showlist);
+
   create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);
 }
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 1ac51369dff..2634ae0f6e7 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -536,6 +536,16 @@ class inferior : public refcounted_object
   /* Data related to displaced stepping.  */
   displaced_step_inferior_state displaced_step_state;
 
+  /* This field is updated when GDB switches away from this inferior to
+     some other inferior, a reference to a thread_info is stored in here,
+     the ref count for the thread_info should be non-zero to prevent the
+     thread_info being deleted.
+
+     When the user switches back to this inferior the thread_info is taken
+     out of this reference and used to (possibly) switch back to this
+     thread.  */
+  thread_info_ref previous_thread_info;
+
   /* Per inferior data-pointers required by other GDB modules.  */
   REGISTRY_FIELDS;
 
diff --git a/gdb/testsuite/gdb.threads/restore-thread.c b/gdb/testsuite/gdb.threads/restore-thread.c
new file mode 100644
index 00000000000..fe2c13ac429
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/restore-thread.c
@@ -0,0 +1,246 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 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/>.  */
+
+#include <pthread.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <errno.h>
+
+/* The number of threads to create.  */
+volatile int thread_count = 3;
+
+/* Is initialised with our pid, GDB will read this.  */
+pid_t global_pid;
+
+/* Holds one end of two different pipes.  Things written to READ will not
+   appear on WRITE.  */
+struct pipe_fds
+{
+  int read;
+  int write;
+};
+
+/* Information passed into each thread.  */
+struct thread_info
+{
+  /* Just a numeric id for the thread.  */
+  int id;
+
+  /* File handles with which the worker thread can communicate with the
+     master thread.  */
+  struct pipe_fds fds;
+};
+
+/* The control information held by the master thread, one of these for each
+   worker thread.  */
+struct thread_ctrl
+{
+  /* The actual pthread handle, used to join the threads.  */
+  pthread_t thread;
+
+  /* File handles with which the master thread can communicate with the
+     worker threads.  */
+  struct pipe_fds fds;
+
+  /* The information that is passed into the worker thread.  */
+  struct thread_info info;
+};
+
+/* Wait for a single byte of the read file handle in FDS.  */
+static void
+wait_on_byte (struct pipe_fds *fds)
+{
+  ssize_t rtn;
+  char c;
+
+  while ((rtn = read (fds->read, &c, 1)) != 1)
+    {
+      if (rtn != -1 || errno != EINTR)
+	abort ();
+    }
+}
+
+/* Send a single byte to the write file handle in FDS.  */
+static void
+send_byte (struct pipe_fds *fds)
+{
+  ssize_t rtn;
+  char c = 'x';
+  while ((rtn = write (fds->write, &c, 1)) != 1)
+    {
+      if (rtn != -1 || errno != EINTR)
+	abort ();
+    }
+}
+
+/* Create a function used to mark a breakpoint location.  */
+#define BREAKPOINT_FUNC(N)				\
+  static void						\
+  breakpt_ ## N ()					\
+  {							\
+    printf ("Hit breakpt_" #N "\n");			\
+  }
+
+BREAKPOINT_FUNC (0)	/* breakpt_0 */
+BREAKPOINT_FUNC (1)	/* breakpt_1 */
+BREAKPOINT_FUNC (2)	/* breakpt_2 */
+
+/* The worker thread entry point.  */
+static void *
+thread_worker (void *arg)
+{
+  struct thread_info *info = (struct thread_info *) arg;
+  int id = info->id;
+
+  printf ("Thread %d created.\n", id);
+  breakpt_0 ();
+
+  /* Let the main thread know that this thread is now running.  */
+  send_byte (&info->fds);
+
+  /* The thread with id #2 is special, it waits here for a nudge from the
+     main thread.  */
+  if (id == 2)
+    {
+      wait_on_byte (&info->fds);
+      breakpt_2 ();
+      send_byte (&info->fds);
+    }
+
+  /* Now wait for an incoming message indicating that the thread should
+     exit.  */
+  wait_on_byte (&info->fds);
+  printf ("In thread %d, exiting...\n", id);
+  return NULL;
+}
+
+/* Initialise CTRL for thread ID, this includes setting up all of the pipe
+   file handles.  */
+static void
+thread_ctrl_init (struct thread_ctrl *ctrl, int id)
+{
+  int fds[2];
+
+  ctrl->info.id = id;
+  if (pipe (fds))
+    abort ();
+  ctrl->info.fds.read = fds[0];
+  ctrl->fds.write = fds[1];
+
+  if (pipe (fds))
+    abort ();
+  ctrl->fds.read = fds[0];
+  ctrl->info.fds.write = fds[1];
+}
+
+/* Wait for a SIGUSR1 to arrive.  Assumes that SIGUSR1 is blocked on entry
+   to this function.  */
+static void
+wait_for_sigusr1 (void)
+{
+  int signo;
+  sigset_t set;
+
+  sigemptyset (&set);
+  sigaddset (&set, SIGUSR1);
+
+  /* Wait for a SIGUSR1.  */
+  if (sigwait (&set, &signo) != 0)
+    abort ();
+  if (signo != SIGUSR1)
+    abort ();
+}
+
+/* Main program.  */
+int
+main ()
+{
+  sigset_t set;
+  int i, max = thread_count;
+
+  /* Set an alarm in case the testsuite crashes, don't leave the test
+     running forever.  */
+  alarm (300);
+
+  struct thread_ctrl *info = malloc (sizeof (struct thread_ctrl) * max);
+  if (info == NULL)
+    abort ();
+
+  /* Put the pid somewhere easy for GDB to read, also print it.  */
+  global_pid = getpid ();
+  printf ("pid = %lld\n", ((long long) global_pid));
+
+  /* Block SIGUSR1, all threads will inherit this sigmask. */
+  sigemptyset (&set);
+  sigaddset (&set, SIGUSR1);
+  if (pthread_sigmask (SIG_BLOCK, &set, NULL))
+    abort ();
+
+  /* Create each thread.  */
+  for (i = 0; i < max; ++i)
+    {
+      struct thread_ctrl *thr = &info[i];
+      thread_ctrl_init (thr, i + 1);
+
+      if (pthread_create (&thr->thread, NULL, thread_worker, &thr->info) != 0)
+	abort ();
+
+      /* Wait for an indication that the thread has started, and is ready
+	 for action.  */
+      wait_on_byte (&thr->fds);
+    }
+
+  printf ("All threads created.\n");
+
+  /* Give thread thread #1 a little nudge.  */
+  if (max >= 2)
+    {
+      send_byte (&info[1].fds);
+      wait_on_byte (&info[1].fds);
+    }
+
+  breakpt_1 ();
+
+  /* For each thread in turn wait for a SIGUSR1 to arrive, signal the
+     thread so that it will exit (by sending it a byte down its pipe), then
+     join the newly exited thread.  */
+  for (i = 0; i < max; ++i)
+    {
+      struct thread_ctrl *thr = &info[i];
+
+      wait_for_sigusr1 ();
+
+      printf ("Telling thread %d to exit\n", thr->info.id);
+      send_byte (&thr->fds);
+
+      if (pthread_join (thr->thread, NULL) != 0)
+	abort ();
+
+      printf ("Thread %d exited\n", thr->info.id);
+    }
+
+  free (info);
+
+  /* Final wait before exiting.  */
+  wait_for_sigusr1 ();
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.threads/restore-thread.exp b/gdb/testsuite/gdb.threads/restore-thread.exp
new file mode 100644
index 00000000000..369472a2f97
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/restore-thread.exp
@@ -0,0 +1,220 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2020 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/>.
+
+# Test GDB's ability to restore the selected thread when switching
+# between inferiors, and check what happens when the selected thread
+# of one inferior exits while we have a different inferior selected.
+
+standard_testfile
+
+if [prepare_for_testing "failed to prepare" $binfile $srcfile \
+	{debug pthreads}] {
+    return -1
+}
+
+# Check that the current thread is THR in inferior INF.
+proc check_current_thread { inf thr {testname ""} } {
+    if {${testname} == ""} {
+	set testname "check_current_thread ${inf} ${thr}"
+    }
+
+    # As a final check, lets check the output for the 'thread'
+    # command.
+    gdb_test "thread" "Current thread is ${inf}.${thr} .*" \
+	"current thread is ${inf}.${thr}: $testname"
+}
+
+# Switch to inferior number INF, we expect that thread number THR
+# within the inferior will be selected.
+proc switch_to_inferior { inf thr {testname ""} } {
+    if {${testname} == ""} {
+	set testname "switch_to_inferior $inf $thr"
+    }
+
+    gdb_test "inferior $inf" \
+	"Switching to inferior ${inf} .*Switching to thread ${inf}.${thr} .*" \
+	"$testname: select inferior ${inf}"
+
+    check_current_thread $inf $thr "$testname: check current thread"
+}
+
+# Switch to thread number THR.  INF should be the number of the
+# currently selected inferior and is used when checking the currently
+# selected thread.
+proc switch_to_thread { inf thr {testname ""} } {
+    if {${testname} == ""} {
+	set testname "switch_to_thread $inf $thr"
+    }
+
+    gdb_test "thread ${thr}" \
+	"Switching to thread ${inf}.${thr} .*" \
+	"${testname}: select thread ${thr}"
+    check_current_thread $inf $thr \
+	"${testname}: check current thread"
+}
+
+# Continue the program in the background.
+proc continue_in_bg { testname } {
+    global gdb_prompt
+
+    gdb_test_multiple "continue&" $testname {
+	-re "Continuing\\.\r\n$gdb_prompt " {
+	    pass $gdb_test_name
+	}
+    }
+}
+
+# Send SIGUSR1 to PID, this will cause one of that processes threads
+# to exit (assuming the process is currently running).
+proc send_thread_exit_signal { pid } {
+    global decimal
+
+    remote_exec target "kill -USR1 ${pid}"
+    gdb_test_multiple "" "wait for thread to exit" {
+	-re "Thread $decimal exited.*exited\\\].*" {
+	}
+    }
+}
+
+# Start of test script.
+
+set pid_1 0
+set pid_2 0
+
+if ![runto_main] {
+    return -1
+}
+
+# Restoring the selected thread is off by default.  Switch it on now.
+gdb_test_no_output "set restore-selected-thread on"
+
+gdb_breakpoint "breakpt_0"
+gdb_breakpoint "breakpt_1"
+
+with_test_prefix "start inferior 1" {
+    gdb_continue_to_breakpoint "created thread 1.2" ".* breakpt_0 .*"
+    gdb_continue_to_breakpoint "created thread 1.3" ".* breakpt_0 .*"
+    gdb_continue_to_breakpoint "created thread 1.4" ".* breakpt_0 .*"
+    gdb_continue_to_breakpoint "all inferior 1 threads created" \
+	".* breakpt_1 .*"
+    gdb_test "info threads" ".*"
+    set pid_1 [get_valueof "/d" "global_pid" 0]
+}
+
+# Start another inferior.
+gdb_test "add-inferior" [multi_line \
+			     "\\\[New inferior 2\\\]" \
+			     "Added inferior 2 .*" ] \
+    "add empty inferior 2"
+gdb_test "inferior 2" "Switching to inferior 2.*" \
+    "switch to inferior 2"
+gdb_test "file ${binfile}" ".*" "load file in inferior 2"
+
+with_test_prefix "start inferior 2" {
+    gdb_breakpoint "breakpt_2"
+    gdb_run_cmd
+    gdb_test "" "hit Breakpoint .*" \
+	"runto breakpoint in main"
+    gdb_continue_to_breakpoint "created thread 2.2" ".* breakpt_0 .*"
+    gdb_continue_to_breakpoint "created thread 2.3" ".* breakpt_0 .*"
+    gdb_continue_to_breakpoint "created thread 2.4" ".* breakpt_0 .*"
+    gdb_continue_to_breakpoint "all inferior 2 threads created" \
+	".* breakpt_2 .*"
+    gdb_test "info threads" ".*"
+    set pid_2 [get_valueof "/d" "global_pid" 0]
+}
+
+gdb_assert {${pid_1} != 0} "read the pid for inferior 1"
+gdb_assert {${pid_2} != 0} "read the pid for inferior 2"
+
+check_current_thread 2 3 "check initial thread is 2.3"
+switch_to_inferior 1 1 "first switch to thread 1.1"
+switch_to_inferior 2 3
+switch_to_thread 2 2
+
+switch_to_inferior 1 1 "second switch to thread 1.1"
+switch_to_thread 1 3
+switch_to_inferior 2 2
+
+# Inferior 2 is special; it will have stopped at breakpt_2, in thread
+# 2.3.  To set this inferior up so that threads can exit we need to
+# continue to breakpt_1.
+gdb_continue_to_breakpoint "all inferior 2 threads created" \
+    ".* breakpt_1 .*"
+
+with_test_prefix "inferior 2 ready" {
+    check_current_thread 2 1
+
+    switch_to_inferior 1 3
+    switch_to_thread 1 2
+
+    continue_in_bg "continue inferior 1"
+    switch_to_inferior 2 1
+    switch_to_thread 2 2
+    continue_in_bg "continue inferior 2"
+}
+
+# Cause thread 1.2 to exit.
+send_thread_exit_signal ${pid_1}
+
+with_test_prefix "after 1.2 exited" {
+    # We should go back to 1.1 now as 1.2 has exited.
+    switch_to_inferior 1 1
+    switch_to_thread 1 4
+
+    # Cause thread 2.2 to exit.
+    send_thread_exit_signal ${pid_2}
+}
+
+with_test_prefix "after 2.2 exited" {
+    # We should go back to 2.1 now as 2.2 has exited.
+    switch_to_inferior 2 1
+
+    # Cause thread 1.3 to exit.
+    send_thread_exit_signal ${pid_1}
+}
+
+with_test_prefix "after 1.3 exited" {
+    # We should still switch back to 1.4 as only 1.3 exited.
+    switch_to_inferior 1 4
+
+    # Cause thread 2.3 to exit.
+    send_thread_exit_signal ${pid_2}
+}
+
+with_test_prefix "after 2.3 exited" {
+    # Switch back to 2.1, which should still be selected.
+    switch_to_inferior 2 1
+
+    # Cause thread 1.4 to exit.
+    send_thread_exit_signal ${pid_1}
+}
+
+with_test_prefix "after 1.4 exited" {
+    # We should now switch back to 1.1 as 1.4 exited, and 1.1 is the
+    # only thread left now.
+    switch_to_inferior 1 1
+
+    # Cause thread 2.4 to exit.
+    send_thread_exit_signal ${pid_2}
+}
+
+with_test_prefix "after 2.4 exited" {
+    # Switch back to 2.1, which should still be selected.
+    switch_to_inferior 2 1
+}
+
-- 
2.25.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame
  2020-04-27 22:04 [PATCHv2 0/3] Restore thread and frame patches Andrew Burgess
  2020-04-27 22:04 ` [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior Andrew Burgess
@ 2020-04-27 22:04 ` Andrew Burgess
  2020-04-28 15:43   ` Aktemur, Tankut Baris
  2020-04-27 22:04 ` [PATCHv2 3/3] gdb: Track the current frame for each thread Andrew Burgess
  2 siblings, 1 reply; 7+ messages in thread
From: Andrew Burgess @ 2020-04-27 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Andrew Burgess

GDB currently has two copies of restore_selected_frame, this commit
unifies these two, and moves the implementation into frame.c.

The only possible user visible change after this commit is if GDB is
unable to restore the currently selected stack after performing an
inferior call.

Previously, in any situation, if GDB failed to find a frame with a
matching frame id, then the following warning would be issued, and GDB
would be left with the innermost stack frame selected:

  Unable to restore previously selected frame.

After this commit, then GDB will give a slightly different warning:

  Couldn't restore frame #%d in current thread.  Bottom (innermost) frame selected:
  ....

Where '....' is a summary of the frame that was selected, and '%d' is
the frame level GDB was looking for.  Additionally, this warning is
not given if the previously selected frame was at level 0, in that
case GDB will just silently selected the innermost frame.

If this difference is a problem then it should be easy enough to have
restore_selected_frame take an 'always-warn' flag parameter, but I
haven't done that in this commit.

Beyond this there should be no other user visible changes with this
commit.

gdb/ChangeLog:

	* frame.c (restore_selected_frame): Moved from thread.c.
	* frame.h (restore_selected_frame): Declare.
	(struct frame_id_and_level): New struct.
	* gdbthread.h (scoped_restore_current_thread)
	<m_selected_frame_id>: Delete.  <m_selected_frame_level>: Delete.
	<m_selected_frame_info>: New member variable.
	* infrun.c (infcall_control_state) <selected_frame_id>: Delete.
	<selected_frame_info>: New member variable.
	(save_infcall_control_state): Update to use selected_frame_info.
	(restore_selected_frame): Delete.
	(restore_infcall_control_state): Update to use selected_frame_info.
	* thread.c (restore_selected_frame): Delete.
	(scoped_restore_current_thread::restore): Update for changes to
	member variables.
	(scoped_restore_current_thread::scoped_restore_current_thread):
	Likewise.
---
 gdb/ChangeLog   | 19 +++++++++++++
 gdb/frame.c     | 61 ++++++++++++++++++++++++++++++++++++++++
 gdb/frame.h     | 68 +++++++++++++++++++++++++++++++++++++++++++++
 gdb/gdbthread.h |  3 +-
 gdb/infrun.c    | 26 ++++-------------
 gdb/thread.c    | 74 ++-----------------------------------------------
 6 files changed, 158 insertions(+), 93 deletions(-)

diff --git a/gdb/frame.c b/gdb/frame.c
index ac1016b083f..7cbd03e1721 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -2910,6 +2910,67 @@ frame_prepare_for_sniffer (struct frame_info *frame,
   frame->unwind = unwind;
 }
 
+/* See frame.h.  */
+
+void
+restore_selected_frame (struct frame_id a_frame_id, int frame_level)
+{
+  struct frame_info *frame;
+  int count;
+
+  /* This means there was no selected frame.  */
+  if (frame_level == -1)
+    {
+      select_frame (NULL);
+      return;
+    }
+
+  gdb_assert (frame_level >= 0);
+
+  /* Restore by level first, check if the frame id is the same as
+     expected.  If that fails, try restoring by frame id.  If that
+     fails, nothing to do, just warn the user.  */
+
+  count = frame_level;
+  frame = find_relative_frame (get_current_frame (), &count);
+  if (count == 0
+      && frame != NULL
+      /* The frame ids must match - either both valid or both outer_frame_id.
+	 The latter case is not failsafe, but since it's highly unlikely
+	 the search by level finds the wrong frame, it's 99.9(9)% of
+	 the time (for all practical purposes) safe.  */
+      && frame_id_eq (get_frame_id (frame), a_frame_id))
+    {
+      /* Cool, all is fine.  */
+      select_frame (frame);
+      return;
+    }
+
+  frame = frame_find_by_id (a_frame_id);
+  if (frame != NULL)
+    {
+      /* Cool, refound it.  */
+      select_frame (frame);
+      return;
+    }
+
+  /* Nothing else to do, the frame layout really changed.  Select the
+     innermost stack frame.  */
+  select_frame (get_current_frame ());
+
+  /* Warn the user.  */
+  if (frame_level > 0 && !current_uiout->is_mi_like_p ())
+    {
+      warning (_("Couldn't restore frame #%d in "
+		 "current thread.  Bottom (innermost) frame selected:"),
+	       frame_level);
+      /* For MI, we should probably have a notification about
+	 current frame change.  But this error is not very
+	 likely, so don't bother for now.  */
+      print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+    }
+}
+
 static struct cmd_list_element *set_backtrace_cmdlist;
 static struct cmd_list_element *show_backtrace_cmdlist;
 
diff --git a/gdb/frame.h b/gdb/frame.h
index cfc15022ed5..e48e5801c4b 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -957,5 +957,73 @@ extern void set_frame_previous_pc_masked (struct frame_info *frame);
 
 extern bool get_frame_pc_masked (const struct frame_info *frame);
 
+/* Try to find a previously selected frame described by A_FRAME_ID and
+   select it.  If no matching frame can be found then the innermost frame
+   will be selected and a warning printed.  FRAME_LEVEL is the level at
+   which A_FRAME_ID was previously found, and can be -1 to indicate no
+   frame was previously selected, in which case the innermost frame will
+   be selected (without a warning).  */
+
+extern void restore_selected_frame (struct frame_id a_frame_id, int frame_level);
+
+/* When GDB needs to backup and then later restore the currently selected
+   frame this is done by storing the frame id, and then looking up a frame
+   with that stored frame id.
+
+   However, if the previously selected frame can't be restored then GDB
+   should give the user a warning in most cases.  If the previously
+   selected frame was level 0 then GDB will just reselect the innermost
+   frame silently without a warning.
+
+   And so, when we backup and restore the currently selected frame we need
+   to track both the frame id, and the frame level, so GDB knows if a
+   warning should be given or not.  */
+
+struct frame_id_and_level
+{
+  /* Setup this structure to track FI as the previously selected frame.  */
+  void reset (struct frame_info *fi)
+  {
+    try
+      {
+	m_id = get_frame_id (fi);
+	m_level = frame_relative_level (fi);
+      }
+    catch (const gdb_exception_error &ex)
+      {
+	m_id = null_frame_id;
+	m_level = -1;
+      }
+  }
+
+  /* Reset this structure to indicate there was no previously selected
+     frame.  */
+  void reset ()
+  {
+    m_level = -1;
+  }
+
+  /* The frame id of the previously selected frame.  This value is only
+     defined when LEVEL() is greater than -1.  */
+  struct frame_id id () const
+  {
+    return m_id;
+  }
+
+  /* The level of the previously selected frame, or -1 if no frame was
+     previously selected.  */
+  int level () const
+  {
+    return m_level;
+  }
+
+private:
+  /* The frame id.  */
+  struct frame_id m_id;
+
+  /* The level at which ID was found.  Set to -1 to indicate that this
+     structure is uninitialised.  */
+  int m_level = -1;
+};
 
 #endif /* !defined (FRAME_H)  */
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 6764c8fc498..fc09c3e2dd0 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -682,8 +682,7 @@ class scoped_restore_current_thread
      function in the Darwin API.  */
   class thread_info *m_thread;
   inferior *m_inf;
-  frame_id m_selected_frame_id;
-  int m_selected_frame_level;
+  struct frame_id_and_level m_selected_frame_info;
   bool m_was_stopped;
 };
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 0265dc84e43..f753fba6601 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -9156,8 +9156,9 @@ struct infcall_control_state
   enum stop_stack_kind stop_stack_dummy = STOP_NONE;
   int stopped_by_random_signal = 0;
 
-  /* ID if the selected frame when the inferior function call was made.  */
-  struct frame_id selected_frame_id {};
+  /* ID and level of the selected frame when the inferior function call was
+     made.  */
+  struct frame_id_and_level selected_frame_info;
 };
 
 /* Save all of the information associated with the inferior<==>gdb
@@ -9186,27 +9187,11 @@ save_infcall_control_state ()
   inf_status->stop_stack_dummy = stop_stack_dummy;
   inf_status->stopped_by_random_signal = stopped_by_random_signal;
 
-  inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
+  inf_status->selected_frame_info.reset (get_selected_frame (NULL));
 
   return inf_status;
 }
 
-static void
-restore_selected_frame (const frame_id &fid)
-{
-  frame_info *frame = frame_find_by_id (fid);
-
-  /* If inf_status->selected_frame_id is NULL, there was no previously
-     selected frame.  */
-  if (frame == NULL)
-    {
-      warning (_("Unable to restore previously selected frame."));
-      return;
-    }
-
-  select_frame (frame);
-}
-
 /* Restore inferior session state to INF_STATUS.  */
 
 void
@@ -9239,7 +9224,8 @@ restore_infcall_control_state (struct infcall_control_state *inf_status)
          error() trying to dereference it.  */
       try
 	{
-	  restore_selected_frame (inf_status->selected_frame_id);
+	  restore_selected_frame (inf_status->selected_frame_info.id (),
+				  inf_status->selected_frame_info.level ());
 	}
       catch (const gdb_exception_error &ex)
 	{
diff --git a/gdb/thread.c b/gdb/thread.c
index f5dde12a20e..fa10a51d89f 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1325,65 +1325,6 @@ switch_to_thread (process_stratum_target *proc_target, ptid_t ptid)
   switch_to_thread (thr);
 }
 
-static void
-restore_selected_frame (struct frame_id a_frame_id, int frame_level)
-{
-  struct frame_info *frame = NULL;
-  int count;
-
-  /* This means there was no selected frame.  */
-  if (frame_level == -1)
-    {
-      select_frame (NULL);
-      return;
-    }
-
-  gdb_assert (frame_level >= 0);
-
-  /* Restore by level first, check if the frame id is the same as
-     expected.  If that fails, try restoring by frame id.  If that
-     fails, nothing to do, just warn the user.  */
-
-  count = frame_level;
-  frame = find_relative_frame (get_current_frame (), &count);
-  if (count == 0
-      && frame != NULL
-      /* The frame ids must match - either both valid or both outer_frame_id.
-	 The latter case is not failsafe, but since it's highly unlikely
-	 the search by level finds the wrong frame, it's 99.9(9)% of
-	 the time (for all practical purposes) safe.  */
-      && frame_id_eq (get_frame_id (frame), a_frame_id))
-    {
-      /* Cool, all is fine.  */
-      select_frame (frame);
-      return;
-    }
-
-  frame = frame_find_by_id (a_frame_id);
-  if (frame != NULL)
-    {
-      /* Cool, refound it.  */
-      select_frame (frame);
-      return;
-    }
-
-  /* Nothing else to do, the frame layout really changed.  Select the
-     innermost stack frame.  */
-  select_frame (get_current_frame ());
-
-  /* Warn the user.  */
-  if (frame_level > 0 && !current_uiout->is_mi_like_p ())
-    {
-      warning (_("Couldn't restore frame #%d in "
-		 "current thread.  Bottom (innermost) frame selected:"),
-	       frame_level);
-      /* For MI, we should probably have a notification about
-	 current frame change.  But this error is not very
-	 likely, so don't bother for now.  */
-      print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
-    }
-}
-
 void
 scoped_restore_current_thread::restore ()
 {
@@ -1408,7 +1349,8 @@ scoped_restore_current_thread::restore ()
       && target_has_registers
       && target_has_stack
       && target_has_memory)
-    restore_selected_frame (m_selected_frame_id, m_selected_frame_level);
+    restore_selected_frame (m_selected_frame_info.id (),
+			    m_selected_frame_info.level ());
 }
 
 scoped_restore_current_thread::~scoped_restore_current_thread ()
@@ -1457,17 +1399,7 @@ scoped_restore_current_thread::scoped_restore_current_thread ()
       else
 	frame = NULL;
 
-      try
-	{
-	  m_selected_frame_id = get_frame_id (frame);
-	  m_selected_frame_level = frame_relative_level (frame);
-	}
-      catch (const gdb_exception_error &ex)
-	{
-	  m_selected_frame_id = null_frame_id;
-	  m_selected_frame_level = -1;
-	}
-
+      m_selected_frame_info.reset (frame);
       tp->incref ();
       m_thread = tp;
     }
-- 
2.25.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCHv2 3/3] gdb: Track the current frame for each thread
  2020-04-27 22:04 [PATCHv2 0/3] Restore thread and frame patches Andrew Burgess
  2020-04-27 22:04 ` [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior Andrew Burgess
  2020-04-27 22:04 ` [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame Andrew Burgess
@ 2020-04-27 22:04 ` Andrew Burgess
  2 siblings, 0 replies; 7+ messages in thread
From: Andrew Burgess @ 2020-04-27 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: palves, Andrew Burgess

Currently in GDB, each time a user switches between threads, the inner
most frame of the thread being switched to is selected.  In some
situations however, it might be helpful for a user to have GDB
remember which frame was selected in each thread, and restore this
frame as the user switches between threads.

This commit the following two commands:

  set restore-selected-frame on|off
  show restore-selected-frame

This new option is off by default, so the default behaviour of GDB in
unchanged.

However, with this option turned on GDB will now remember, and restore
the selected frame for each thread.

My initial motivation for this change was to have the thread restored
when switching threads with 'thread <num>', however, as I started to
work on this feature I realised that there were a couple of other
places where the sticky frame would naturally appear.  These are 'info
threads' and 'thread apply all'.

With 'info threads' the output contains a 'Frame' column.  Previously,
this was always the innermost frame, the info threads output was
created by switching to each thread in turn and collecting information
about the thread, this naturally placed us at the innermost frame.
Now, the 'Frame' column displays the _selected_ frame for each
thread.

I struggled to decide if this change was good or not.  In the end I
felt that having 'info threads' display the selected frame would feel
more natural, that's the frame you'll end up in if you switch to that
thread, so if seemed to make sense.  However, it would be easy enough
to force the old behaviour if people would prefer.

For 'thread apply all', again, we used to always apply to the
innermost frame.  Now it's possible for a user to adjust which frame
will be current when the 'thread apply all' runs - this feels like a
useful change to me.  It's easy enough to quickly restore the inner
most frame if required ('thread apply all -- frame 0') and having the
flexibility to tweak the selected frame in just some threads feels
like a nice advantage.

gdb/ChangeLog:

	* NEWS: Describe new feature.
	* frame.c (cache_selected_frame_on_thread): New function.
	(select_frame): Call new function.
	* gdbthread.h (class thread_info) <selected_frame_info>: New
	member variable.
	(switch_to_thread): Extra parameter.
	* thread.c (switch_to_thread_if_alive): Extra parameter, passed to
	switch_to_thread.
	(scoped_restore_current_thread::restore): Restore the frame either
	from the thread, or from the local object.
	(set_executing_thread): Reset the currently selected frame.
	(restore_selected_frame_per_thread): New file level static variable.
	(show_restore_selected_frame_per_thread): New function.
	(print_thread_info_1): Pass extra parameter to switch_to_thread.
	(switch_to_thread): Take extra parameter, restore the previous
	frame if appropriate.
	(thread_apply_all_command): Pass extra parameter to switch_to_thread.
	(thread_apply_command): Likewise.
	(thread_select): Pass extra parameter to switch_to_thread_if_alive.
	(_initialize_thread): Add new set/show variable.

gdb/doc/ChangeLog:

	* gdb.texinfo (Threads): Add anchor to 'info threads'.  Describe
	the Frame column of 'info threads' more.  Describe which frame is
	selected when switching threads, and document the new option for
	restoring the previously selected frame.

gdb/testsuite/ChangeLog:

	* gdb.threads/restore-selected-frame.c: New file.
	* gdb.threads/restore-selected-frame.exp: New file.
---
 gdb/ChangeLog                                 |  23 ++
 gdb/NEWS                                      |  10 +
 gdb/doc/ChangeLog                             |   7 +
 gdb/doc/gdb.texinfo                           |  23 +-
 gdb/frame.c                                   |  26 ++
 gdb/gdbthread.h                               |  12 +-
 gdb/testsuite/ChangeLog                       |   5 +
 .../gdb.threads/restore-selected-frame.c      |  85 +++++
 .../gdb.threads/restore-selected-frame.exp    | 336 ++++++++++++++++++
 gdb/thread.c                                  |  61 +++-
 10 files changed, 575 insertions(+), 13 deletions(-)
 create mode 100644 gdb/testsuite/gdb.threads/restore-selected-frame.c
 create mode 100644 gdb/testsuite/gdb.threads/restore-selected-frame.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index ad743508e23..58e94f9565d 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -68,6 +68,16 @@ show restore-selected-thread
   previously selected thread is no longer available then GDB falls
   back to selecting the first non-exited thread.
 
+set restore-selected-frame [on|off]
+show restore-selected-frame
+  This option is off by default.  When turned on GDB will record the
+  currently selected frame in each thread.  When switching between
+  threads GDB will attempt to restore the previously selected frame in
+  the thread being switched too.  Executing a thread will cause the
+  GDB to discard any previously selected frame (GDB will select the
+  inner most frame the next time the thread stops).  The 'info
+  threads' command will show the selected frame in its 'frame' field.
+
 * New targets
 
 GNU/Linux/RISC-V (gdbserver)	riscv*-*-linux*
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 3ee6e78c593..47eb6ff2bfc 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3446,6 +3446,7 @@
 @end smallexample
 
 @table @code
+@anchor{info threads}
 @kindex info threads
 @item info threads @r{[}@var{thread-id-list}@r{]}
 
@@ -3473,7 +3474,9 @@
 program itself.
 
 @item
-the current stack frame summary for that thread
+the current stack frame summary for that thread, this is the inner
+most frame for the thread, see @ref{set restore-selected-frame} to
+display the threads selected frame instead.
 @end enumerate
 
 @noindent
@@ -3545,6 +3548,24 @@
 @samp{Switching to} depends on your system's conventions for identifying
 threads.
 
+When switching between threads @value{GDBN} will select the inner most
+frame in the thread being switched too, see @ref{set
+restore-selected-frame} to change this behaviour.
+
+@anchor{set restore-selected-frame}
+@item set restore-selected-frame @r{[}on|off@r{]}
+@itemx show restore-selected-frame
+When @code{restore-selected-frame} is on, @value{GDBN} will restore
+the previously selected frame when switching to a different thread.
+Also the @code{info threads} command (@pxref{info threads}) will display the
+currently selected frame for each thread.
+
+If a thread has been running then when it stops the previously
+selected frame is discarded, and the inner most frame is again
+selected.
+
+This option is @code{off} by default.
+
 @anchor{thread apply all}
 @kindex thread apply
 @cindex apply command to several threads
diff --git a/gdb/frame.c b/gdb/frame.c
index 7cbd03e1721..454ff682ece 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -1704,12 +1704,38 @@ deprecated_safe_get_selected_frame (void)
   return get_selected_frame (NULL);
 }
 
+/* When RESTORE_SELECTED_FRAME_PER_THREAD is true, then update in the
+   current thread the information required to identify frame FI so the
+   frame can be selected again later if we switch threads.  */
+
+static void
+cache_selected_frame_on_thread ()
+{
+  struct frame_info *fi = selected_frame;
+  struct thread_info *tp
+    = find_thread_ptid (current_inferior (), inferior_ptid);
+  if (fi != nullptr && tp != nullptr)
+    {
+      /* We only record the selected frame if the level is greater than 0,
+	 this avoids having to calculate the frame id when selecting the
+	 innermost frame.  When the cached selected frame is cleared then
+	 we select the innermost frame anyway, so calculating the frame id
+	 for frame #0 adds no value.  */
+      if (frame_relative_level (fi) > 0)
+	tp->selected_frame_info.reset (fi);
+      else
+	tp->selected_frame_info.reset ();
+    }
+}
+
 /* Select frame FI (or NULL - to invalidate the current frame).  */
 
 void
 select_frame (struct frame_info *fi)
 {
   selected_frame = fi;
+  cache_selected_frame_on_thread ();
+
   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
      frame is being invalidated.  */
 
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index fc09c3e2dd0..8c65284135b 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -387,6 +387,11 @@ class thread_info : public refcounted_object
      bp_longjmp_call_dummy.  */
   struct frame_id initiating_frame = null_frame_id;
 
+  /* Information for the last frame successfully selected in this thread.
+     If the user configurable setting is on then GDB will try to reselect
+     this frame when switching threads.  */
+  struct frame_id_and_level selected_frame_info;
+
   /* Private data used by the target vector implementation.  */
   std::unique_ptr<private_thread_info> priv;
 
@@ -582,8 +587,11 @@ extern int thread_count (process_stratum_target *proc_target);
 /* Return true if we have any thread in any inferior.  */
 extern bool any_thread_p ();
 
-/* Switch context to thread THR.  Also sets the STOP_PC global.  */
-extern void switch_to_thread (struct thread_info *thr);
+/* Switch context to thread THR.  Also sets the STOP_PC global.  When
+   RESTORE_PREVIOUS_FRAME is true then, if this thread has a previously
+   selected frame cached, the previous frame is restored.  */
+extern void switch_to_thread (struct thread_info *thr,
+			      bool restore_previous_frame = false);
 
 /* Switch context to no thread selected.  */
 extern void switch_to_no_thread ();
diff --git a/gdb/testsuite/gdb.threads/restore-selected-frame.c b/gdb/testsuite/gdb.threads/restore-selected-frame.c
new file mode 100644
index 00000000000..c72b0b8b54b
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/restore-selected-frame.c
@@ -0,0 +1,85 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+volatile int loop_count = 10;
+volatile int thread_count = 3;
+
+static void
+thread_level_5 (int id, int count)
+{
+  printf ("Thread %d reached %s, #%d\n",
+	  id, __PRETTY_FUNCTION__, count);
+}
+
+static void
+thread_level_4 (int id, int count)
+{
+  thread_level_5 (id, count);
+}
+
+static void
+thread_level_3 (int id, int count)
+{
+  thread_level_4 (id, count);
+}
+
+static void
+thread_level_2 (int id, int count)
+{
+  thread_level_3 (id, count);
+}
+
+static void
+thread_level_1 (int id, int count)
+{
+  thread_level_2 (id, count);
+}
+
+static void *
+thread_worker (void *arg)
+{
+  int i, max, id;
+
+  id = *((int *) arg);
+  max = loop_count;
+  for (i = 0; i < max; ++i)
+    thread_level_1 (id, (i + 1));
+
+  return NULL;
+}
+
+struct thread_info
+{
+  pthread_t thread;
+  int id;
+};
+
+int
+main ()
+{
+  int i, max = thread_count;
+
+  struct thread_info *info = malloc (sizeof (struct thread_info) * max);
+  if (info == NULL)
+    abort ();
+
+  for (i = 0; i < max; ++i)
+    {
+      struct thread_info *thr = &info[i];
+      thr->id = i + 1;
+      if (pthread_create (&thr->thread, NULL, thread_worker, &thr->id) != 0)
+	abort ();
+    }
+
+  for (i = 0; i < max; ++i)
+    {
+      struct thread_info *thr = &info[i];
+      if (pthread_join (thr->thread, NULL) != 0)
+	abort ();
+    }
+
+  free (info);
+}
diff --git a/gdb/testsuite/gdb.threads/restore-selected-frame.exp b/gdb/testsuite/gdb.threads/restore-selected-frame.exp
new file mode 100644
index 00000000000..b40386fc58e
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/restore-selected-frame.exp
@@ -0,0 +1,336 @@
+# Copyright 2020 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 tests GDB's tracking of the currently selected frame on a
+# per-thread basis.
+#
+# We setup a couple of inferiors, each with mutliple theads, we then
+# switch between threads and modify the current frame.  We use 'info
+# threads' to check that GDB is correctly tracking the current frame.
+#
+# Toward the end of the test we check that when a thread executes the
+# currently selected frame is reset.
+#
+# Finally we disable tracking of the currently selected frame and
+# ensure GDB no longer restores the current frame.
+
+standard_testfile
+
+set options { debug pthreads }
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
+	 $options] == -1} {
+    return -1
+}
+
+# Run the 'info threads' command, and check that the frame part of
+# each threads output matches the corresponding pattern in FRAME_INFO,
+# with thread 1 using entry 0 from FRAME_INFO, thread 2 using entry 1,
+# and so on.
+proc test_info_threads { testname frame_info } {
+    global decimal hex gdb_prompt
+
+    set thread_count 0
+    gdb_test_multiple "info threads" ${testname} {
+	-re ".*  Id\\s+Target Id\\s+Frame\\s*\r\n" {
+	    # Discard the info threads header line as well as any
+	    # output before it in the expect buffer.
+	    exp_continue
+	}
+
+	-re "^\[* \]\\s+(($decimal)\.)?($decimal)\\s+Thread $hex \\(LWP $decimal\\) \"\[^\"\]+\"\\s+(\[^\r\n\]*)\r\n" {
+	    if {[info exists expect_out(2,string)]} {
+		set id "$expect_out(2,string).$expect_out(3,string)"
+		set index [expr [expr [expr $expect_out(2,string) - 1] * 4] \
+			       + [expr $expect_out(3,string) - 1]]
+	    } else {
+		set id $expect_out(3,string)
+		set index [expr $id - 1]
+	    }
+	    set frame $expect_out(4,string)
+	    set pattern [lindex $frame_info $index]
+	    gdb_assert {[regexp -- $pattern $frame]} \
+		"$testname: thread $id matches"
+	    incr thread_count
+	    exp_continue
+	}
+	-re "^$gdb_prompt " {
+	}
+    }
+    gdb_assert {$thread_count == [llength $frame_info]} \
+	"$testname: all threads seen"
+}
+
+# Run 'thread THREAD_NUM' and check that we switch thread.
+proc switch_thread { thread_num } {
+    gdb_test "thread ${thread_num}" \
+	"Switching to thread (2\.)?${thread_num} .*"
+}
+
+# Used during startup, continue the inferior and wait for all threads
+# to stop at the breakpoint.
+proc run_all_threads_to_breakpoint { } {
+    global gdb_prompt
+
+    set stopped_thread_count 0
+    gdb_test_multiple "continue" "wait for worker threads to stop" {
+	-re "Thread (2\.)?\[234\] \"\[^\"\]+\" hit Breakpoint" {
+	    incr stopped_thread_count
+	    if {$stopped_thread_count < 3} {
+		exp_continue
+	    }
+	}
+
+	-re "$gdb_prompt" {
+	    exp_continue
+	}
+    }
+
+    gdb_assert {$stopped_thread_count == 3} \
+	"all worker threads stopped"
+}
+
+# Switch to thread #1, and interrupt it.
+proc switch_to_and_stop_thread_1 {} {
+    global gdb_prompt
+    # There's a bit of a wart here in that after sending "interrupt"
+    # the output seems to appear out of order this is probably a
+    # consequence of being in non-stop mode, so this is what I'd like
+    # to see:
+    #
+    #   (gdb) interrupt
+    #   Thread 1 "...." stopped.
+    #   (gdb)
+    #
+    # But what we actually see is:
+    #
+    #   (gdb) interrupt
+    #   (gdb)
+    #   Thread 1 "...." stopped.
+    #
+    # What happens of course is that GDB processes the interrupt,
+    # sends a SIGSTOP to the inferior and then returns to the prompt,
+    # at this point we process the stop event from the inferior and
+    # print the stopped message.
+    #
+    # It would be nice if GDB could be smart enough to reprint the
+    # prompt after the stop message though.
+    #
+    # The first 'interrupt\n' here causes the interior to stop, while
+    # the following lone '\n' causes the prompt to be reprinted.  This
+    # allows us to match all the output up to the final prompt,
+    # ensuring we don't leave any stray output in expect's output
+    # buffer.
+    switch_thread 1
+    gdb_test_multiple "interrupt\\n" "interrupt thread 1" {
+	-re "^interrupt\\\\n\\r\\n$gdb_prompt " {
+	    pass $gdb_test_name
+	}
+    }
+    gdb_test_multiple "" "wait for thread 1 to stop" {
+	-re "Thread (2\.)?1 \"\[^\"\]+\" stopped\." {
+	    send_gdb "\n"
+	    gdb_test_multiple "" \
+		"wait for prompt after thread 1 stopped" {
+		-re ".*$gdb_prompt " {
+		    pass $gdb_test_name
+		}
+	    }
+	}
+    }
+}
+
+# Setup for this test.  Place GDB in non-stop mode, create an initial
+# breakpoint, run all of the threads to the breakpoint, then stop
+# thread 1 (which doesn't hit the breakpoint).
+proc setup_for_test {} {
+    gdb_test_no_output "set non-stop on"
+
+    if ![runto_main] {
+	fail "runto main"
+	return
+    }
+
+    gdb_test_no_output "set restore-selected-frame on"
+
+    gdb_breakpoint "thread_level_5"
+
+    with_test_prefix "setup inferior 1" {
+	# Now run the inferior, and wait for all of the expected threads
+	# to hit the thread_level_5 breakpoint.
+	run_all_threads_to_breakpoint
+
+	# The main thread will still be running at this point, waiting for
+	# the stopped threads to finish so it can join with them.  Lets go
+	# and interrupt it.
+	switch_to_and_stop_thread_1
+    }
+}
+
+setup_for_test
+
+# We can't rely on frames being within 'pthread_join' actually being
+# in a frame called pthread_join.  Different versions of pthreads
+# might call the function something different.  So, just have a
+# match all pattern.
+set pthread_join_pattern ".*"
+
+set frame_info [list "$hex in ${pthread_join_pattern}" \
+		    "thread_level_5" \
+		    "thread_level_5" \
+		    "thread_level_5" ]
+
+
+# We now have all threads stopped in known locations.  Lets check that
+# everyone is where we expect them to be.
+test_info_threads "info threads #1" $frame_info
+
+# First, lets move thread 1.  Then check that the info threads output
+# reflects this.
+gdb_test "up" ".*"
+set frame_info [lreplace $frame_info 0 0 "$hex in main"]
+test_info_threads "info threads #2" $frame_info
+
+# Now lets change the other threads, one at a time, checking the
+# output of info threads after each change.
+foreach spec [list [list 2 5 "$hex in thread_worker"] \
+		  [list 3 3 "$hex in thread_level_2"] \
+		  [list 4 1 "$hex in thread_level_4"] ] {
+    set thr [lindex $spec 0]
+    with_test_prefix "change frame for thread $thr" {
+	switch_thread $thr
+	gdb_test "frame [lindex $spec 1]" ".*"
+	set idx [expr $thr - 1]
+	set frame_info [lreplace $frame_info $idx $idx [lindex $spec 2]]
+	test_info_threads "info threads #3" $frame_info
+    }
+}
+
+# Start a new inferior, and runto main.
+gdb_test "add-inferior" "Added inferior 2 .*" \
+    "add empty inferior 2"
+gdb_test "inferior 2" "Switching to inferior 2 .*" \
+    "switch to inferior 2"
+gdb_test "file ${binfile}" ".*" "load file in inferior 2"
+
+with_test_prefix "start inferior 2" {
+    # Disable deleting of breakpoints.
+    proc delete_breakpoints {} {}
+    runto_main
+}
+
+with_test_prefix "setup inferior 2" {
+    run_all_threads_to_breakpoint
+    switch_to_and_stop_thread_1
+}
+
+set frame_info [concat $frame_info [list "$hex in ${pthread_join_pattern}" \
+					"thread_level_5" \
+					"thread_level_5" \
+					"thread_level_5" ]]
+test_info_threads "info threads #4" $frame_info
+
+# Now lets change the other threads, one at a time, checking the
+# output of info threads after each change.
+foreach spec [list [list 2 2 "$hex in thread_level_3"] \
+		  [list 3 2 "$hex in thread_level_3"] \
+		  [list 4 2 "$hex in thread_level_3"] ] {
+    set thr [lindex $spec 0]
+    with_test_prefix "change frame for thread $thr" {
+	switch_thread "2.$thr"
+	gdb_test "frame [lindex $spec 1]" ".*"
+	set idx [expr 4 + $thr - 1]
+	set frame_info [lreplace $frame_info $idx $idx [lindex $spec 2]]
+	test_info_threads "info threads #5" $frame_info
+    }
+}
+
+# Now step one of the threads.  The thread that is stepped should
+# discard its stored selected frame, but all other threads should
+# retain their selected frame.
+switch_thread "2.2"
+gdb_test "step" ".*" \
+    "step in thread 2.2"
+set frame_info [lreplace $frame_info 5 5 "thread_level_5"]
+test_info_threads "info threads #6" $frame_info
+
+# Same again for a thread in inferior #1.
+switch_thread "1.3"
+gdb_test "step" ".*" \
+    "step in thread 1.3"
+set frame_info [lreplace $frame_info 2 2 "thread_level_5"]
+test_info_threads "info threads #7" $frame_info
+
+# Now switch to another thread that already has a frame other than its
+# innermost selected.
+switch_thread "1.2"
+
+# Now disable restoring of the selected frame.
+gdb_test_no_output "set restore-selected-frame off"
+
+# And check to see which frame each thread has selected.  Our current
+# thread shouldn't change.
+set frame_info [list "$hex in ${pthread_join_pattern}" \
+		    "thread_worker" \
+		    "thread_level_5" \
+		    "thread_level_5" \
+		    "$hex in ${pthread_join_pattern}" \
+		    "thread_level_5" \
+		    "thread_level_5" \
+		    "thread_level_5"]
+test_info_threads "info threads #8" $frame_info
+
+# Now switch to some other thread, at this point GDB should forget the
+# selected frame for thread 1.2.
+switch_thread "1.4"
+set frame_info [lreplace $frame_info 1 1 "thread_level_5"]
+test_info_threads "info threads #9" $frame_info
+
+# A new test that will cover 'thread apply all'.  This test ensures
+# that any changes to the selected thread in 'thread apply all' are
+# sticky outside of the 'thread apply all'.
+with_test_prefix "thr apply all" {
+    clean_restart $binfile
+    setup_for_test
+
+    # Move all threads up a frame.
+    gdb_test "thread apply all -- up" ".*" \
+	"all threads up, first time"
+    set frame_info [list "$hex in main" \
+			"$hex in thread_level_4" \
+			"$hex in thread_level_4" \
+			"$hex in thread_level_4" ]
+    test_info_threads "info threads #10" $frame_info
+
+    # Move every thread back to frame 0.
+    gdb_test "thread apply all -- frame 0" ".*"
+    set frame_info [list "$hex in ${pthread_join_pattern}" \
+			"thread_level_5" \
+			"thread_level_5" \
+			"thread_level_5" ]
+    test_info_threads "info threads #11" $frame_info
+
+    # Disable restoring the current frame.
+    gdb_test_no_output "set restore-selected-frame off"
+
+    # Move all threads up a frame, no frame should change after this
+    # though.
+    gdb_test "thread apply all -- up" ".*" \
+	"all threads up, second time"
+    set frame_info [list "$hex in ${pthread_join_pattern}" \
+			"thread_level_5" \
+			"thread_level_5" \
+			"thread_level_5" ]
+    test_info_threads "info threads #12" $frame_info
+}
diff --git a/gdb/thread.c b/gdb/thread.c
index fa10a51d89f..60adbc47f48 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -665,7 +665,7 @@ thread_alive (thread_info *tp)
    switched, false otherwise.  */
 
 static bool
-switch_to_thread_if_alive (thread_info *thr)
+switch_to_thread_if_alive (thread_info *thr, bool restore_previous_frame)
 {
   scoped_restore_current_thread restore_thread;
 
@@ -675,7 +675,7 @@ switch_to_thread_if_alive (thread_info *thr)
 
   if (thread_alive (thr))
     {
-      switch_to_thread (thr);
+      switch_to_thread (thr, restore_previous_frame);
       restore_thread.dont_restore ();
       return true;
     }
@@ -847,7 +847,10 @@ set_executing_thread (thread_info *thr, bool executing)
 {
   thr->executing = executing;
   if (executing)
-    thr->suspend.stop_pc = ~(CORE_ADDR) 0;
+    {
+      thr->suspend.stop_pc = ~(CORE_ADDR) 0;
+      thr->selected_frame_info.reset ();
+    }
 }
 
 void
@@ -1010,6 +1013,23 @@ thread_target_id_str (thread_info *tp)
     return target_id;
 }
 
+/* When this is true GDB restore the threads previously selected frame
+   each time the current thread is changed (when possible).  */
+
+static bool restore_selected_frame_per_thread = false;
+
+/* Implement 'show restore-selected-frame'.  */
+
+static void
+show_restore_selected_frame_per_thread (struct ui_file *file, int from_tty,
+					struct cmd_list_element *c,
+					const char *value)
+{
+  fprintf_filtered (file,
+		    _("Restoring the selected frame is currently %s.\n"),
+		    value);
+}
+
 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
    whether REQUESTED_THREADS is a list of global or per-inferior
    thread ids.  */
@@ -1122,7 +1142,8 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
 	    uiout->field_signed ("id", tp->global_num);
 
 	  /* Switch to the thread (and inferior / target).  */
-	  switch_to_thread (tp);
+	  switch_to_thread (tp, (tp == current_thread
+				 || restore_selected_frame_per_thread));
 
 	  /* For the CLI, we stuff everything into the target-id field.
 	     This is a gross hack to make the output come out looking
@@ -1304,7 +1325,7 @@ switch_to_no_thread ()
 /* See gdbthread.h.  */
 
 void
-switch_to_thread (thread_info *thr)
+switch_to_thread (thread_info *thr, bool restore_previous_frame)
 {
   gdb_assert (thr != NULL);
 
@@ -1314,6 +1335,10 @@ switch_to_thread (thread_info *thr)
   switch_to_thread_no_regs (thr);
 
   reinit_frame_cache ();
+
+  if (restore_previous_frame && thr->selected_frame_info.level () > -1)
+    restore_selected_frame (thr->selected_frame_info.id (),
+			    thr->selected_frame_info.level ());
 }
 
 /* See gdbsupport/common-gdbthread.h.  */
@@ -1337,13 +1362,16 @@ scoped_restore_current_thread::restore ()
 	 in the mean time exited (or killed, detached, etc.), then don't revert
 	 back to it, but instead simply drop back to no thread selected.  */
       && m_inf->pid != 0)
-    switch_to_thread (m_thread);
+    switch_to_thread (m_thread, restore_selected_frame_per_thread);
   else
     switch_to_inferior_no_thread (m_inf);
 
   /* The running state of the originally selected thread may have
-     changed, so we have to recheck it here.  */
+     changed, so we have to recheck it here.  We only restore the frame
+     here if we didn't restore the threads selected frame when switching
+     thread above (see use of RESTORE_SELECTED_FRAME_PER_THREAD).  */
   if (inferior_ptid != null_ptid
+      && !restore_selected_frame_per_thread
       && m_was_stopped
       && m_thread->state == THREAD_STOPPED
       && target_has_registers
@@ -1612,7 +1640,7 @@ thread_apply_all_command (const char *cmd, int from_tty)
       scoped_restore_current_thread restore_thread;
 
       for (thread_info *thr : thr_list_cpy)
-	if (switch_to_thread_if_alive (thr))
+	if (switch_to_thread_if_alive (thr, restore_selected_frame_per_thread))
 	  thr_try_catch_cmd (thr, cmd, from_tty, flags);
     }
 }
@@ -1769,7 +1797,7 @@ thread_apply_command (const char *tidlist, int from_tty)
 	  continue;
 	}
 
-      if (!switch_to_thread_if_alive (tp))
+      if (!switch_to_thread_if_alive (tp, restore_selected_frame_per_thread))
 	{
 	  warning (_("Thread %s has terminated."), print_thread_id (tp));
 	  continue;
@@ -1937,7 +1965,7 @@ show_print_thread_events (struct ui_file *file, int from_tty,
 void
 thread_select (const char *tidstr, thread_info *tp)
 {
-  if (!switch_to_thread_if_alive (tp))
+  if (!switch_to_thread_if_alive (tp, restore_selected_frame_per_thread))
     error (_("Thread ID %s has terminated."), tidstr);
 
   annotate_thread_changed ();
@@ -2204,6 +2232,19 @@ Show printing of thread events (such as thread start and exit)."), NULL,
 			   show_print_thread_events,
 			   &setprintlist, &showprintlist);
 
+  add_setshow_boolean_cmd ("restore-selected-frame",
+			   class_stack, &restore_selected_frame_per_thread,
+			   _("\
+Set whether GDB restores the selected frame when switching threads."), _("\
+Show whether GDB restores the selected frame when switching threads."), _("\
+When this option is on GDB will record the currently selected frame for\n\
+each thread, and restore the selected frame whenever GDB switches thread.\n\
+Causing a thread to execute will invalidate the selected frame."),
+			   nullptr,
+			   show_restore_selected_frame_per_thread,
+			   &setlist,
+			   &showlist);
+
   create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
   create_internalvar_type_lazy ("_gthread", &gthread_funcs, NULL);
 }
-- 
2.25.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame
  2020-04-27 22:04 ` [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame Andrew Burgess
@ 2020-04-28 15:43   ` Aktemur, Tankut Baris
  2020-04-29  9:17     ` Andrew Burgess
  0 siblings, 1 reply; 7+ messages in thread
From: Aktemur, Tankut Baris @ 2020-04-28 15:43 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches; +Cc: palves

On Tuesday, April 28, 2020 12:04 AM, Andrew Burgess wrote:
> diff --git a/gdb/frame.c b/gdb/frame.c
> index ac1016b083f..7cbd03e1721 100644
> --- a/gdb/frame.c
> +++ b/gdb/frame.c
> @@ -2910,6 +2910,67 @@ frame_prepare_for_sniffer (struct frame_info *frame,
>    frame->unwind = unwind;
>  }
> 
> +/* See frame.h.  */
> +
> +void
> +restore_selected_frame (struct frame_id a_frame_id, int frame_level)
> +{
> +  struct frame_info *frame;
> +  int count;

Could the 'struct' keyword be omitted and the declarations be moved down to 
their first definition?  I think this is a general question regarding
the C to C++ transition and the style in new code.

Thanks.
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior
  2020-04-27 22:04 ` [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior Andrew Burgess
@ 2020-04-28 15:49   ` Aktemur, Tankut Baris
  0 siblings, 0 replies; 7+ messages in thread
From: Aktemur, Tankut Baris @ 2020-04-28 15:49 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches; +Cc: palves

On Tuesday, April 28, 2020 12:04 AM, Andrew Burgess wrote:
> diff --git a/gdb/testsuite/gdb.threads/restore-thread.c b/gdb/testsuite/gdb.threads/restore-
> thread.c
> new file mode 100644
> index 00000000000..fe2c13ac429
> --- /dev/null
> +++ b/gdb/testsuite/gdb.threads/restore-thread.c
> @@ -0,0 +1,246 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2020 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/>.  */
> +
> +#include <pthread.h>
> +#include <signal.h>
> +#include <sys/types.h>
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <pthread.h>
> +#include <errno.h>
> +
> +/* The number of threads to create.  */
> +volatile int thread_count = 3;
> +
> +/* Is initialised with our pid, GDB will read this.  */

Did you mean "If initialised"?

Thanks.
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame
  2020-04-28 15:43   ` Aktemur, Tankut Baris
@ 2020-04-29  9:17     ` Andrew Burgess
  0 siblings, 0 replies; 7+ messages in thread
From: Andrew Burgess @ 2020-04-29  9:17 UTC (permalink / raw)
  To: Aktemur, Tankut Baris; +Cc: gdb-patches, palves

* Aktemur, Tankut Baris <tankut.baris.aktemur@intel.com> [2020-04-28 15:43:32 +0000]:

> On Tuesday, April 28, 2020 12:04 AM, Andrew Burgess wrote:
> > diff --git a/gdb/frame.c b/gdb/frame.c
> > index ac1016b083f..7cbd03e1721 100644
> > --- a/gdb/frame.c
> > +++ b/gdb/frame.c
> > @@ -2910,6 +2910,67 @@ frame_prepare_for_sniffer (struct frame_info *frame,
> >    frame->unwind = unwind;
> >  }
> > 
> > +/* See frame.h.  */
> > +
> > +void
> > +restore_selected_frame (struct frame_id a_frame_id, int frame_level)
> > +{
> > +  struct frame_info *frame;
> > +  int count;
> 
> Could the 'struct' keyword be omitted and the declarations be moved down to 
> their first definition?  I think this is a general question regarding
> the C to C++ transition and the style in new code.

Thanks for the feedback.  I'll wait to see if there is any other
feedback, but I'll roll both these fixes in to the next revision.

Thanks,
Andrew


> 
> Thanks.
> -Baris
> 
> 
> Intel Deutschland GmbH
> Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
> Tel: +49 89 99 8853-0, www.intel.de
> Managing Directors: Christin Eisenschmid, Gary Kershaw
> Chairperson of the Supervisory Board: Nicole Lau
> Registered Office: Munich
> Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-04-29  9:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27 22:04 [PATCHv2 0/3] Restore thread and frame patches Andrew Burgess
2020-04-27 22:04 ` [PATCHv2 1/3] gdb: Restore previously selected thread when switching inferior Andrew Burgess
2020-04-28 15:49   ` Aktemur, Tankut Baris
2020-04-27 22:04 ` [PATCHv2 2/3] gdb: Unify two copies of restore_selected_frame Andrew Burgess
2020-04-28 15:43   ` Aktemur, Tankut Baris
2020-04-29  9:17     ` Andrew Burgess
2020-04-27 22:04 ` [PATCHv2 3/3] gdb: Track the current frame for each thread Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).