[PATCHv7 2/2] gdb: Track the current frame for each thread

Andrew Burgess
2021-02-12T18:21:14Z

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 adds 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 is
unchanged.

However, with this option turned on GDB will 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 it seemed to make sense. However, it would be easy enough
to force the old behaviour if people would prefer. Alternatively I
could even investigate adding a switch to 'info threads' that allows
the user to select displaying either the selected frame, or the
innermost frame.

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
innermost 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. Again, I could potentially add a command flag
here to force the innermost frame.

gdb/ChangeLog:

* NEWS: Describe new feature.
* frame.c (cache_selected_frame_on_thread): New function.
(select_frame): Call new function.
* frame.h (get_current_frame): Update comment.
(get_selected_frame): Update comment.
* gdbthread.h (class thread_info) <selected_frame_id>: New
member variable.
<selected_frame_level>: Likewise.
(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 | 26 ++
gdb/NEWS | 11 +
gdb/doc/ChangeLog | 7 +
gdb/doc/gdb.texinfo | 23 +-
gdb/frame.c | 29 ++
gdb/frame.h | 46 ++-
gdb/gdbthread.h | 13 +-
gdb/testsuite/ChangeLog | 5 +
.../gdb.threads/restore-selected-frame.c | 85 +++++
.../gdb.threads/restore-selected-frame.exp | 336 ++++++++++++++++++
gdb/thread.c | 62 +++-
11 files changed, 611 insertions(+), 32 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 5e15ef1f88e..9322b9e50c9 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -56,6 +56,17 @@ show restore-selected-thread
available, then GDB falls back to selecting the first non-exited
thread.

+set restore-selected-frame [on|off]
+show restore-selected-frame
+
+ 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 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.
+
* Changed commands

break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 2a302c76d55..717890581c8 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3530,6 +3530,7 @@
@end smallexample

@table @code
+@anchor{info threads}
@kindex info threads
@item info threads @r{[}@var{thread-id-list}@r{]}

@@ -3557,7 +3558,9 @@
program itself.

@item
-the current stack frame summary for that thread
+the current stack frame summary for that thread, this is the innermost
+frame for the thread (@pxref{set restore-selected-frame} to display the
+threads selected frame instead).
@end enumerate

@noindent
@@ -3629,6 +3632,24 @@
@samp{Switching to} depends on your system's conventions for identifying
threads.

+When switching between threads, @value{GDBN} will select the innermost
+frame in the thread being switched too (@pxref{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 innermost 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 4578b1acab3..78e55dbc6f2 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -1870,12 +1870,41 @@ 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)
+ {
+ /* Only record the selected frame if the level is greater than 0. If
+ the user has the inner most frame selected then we should always
+ restore the inner most frame, even if the frame-id changes. This
+ matches the behaviour of the global SELECTED_FRAME_ID and
+ SELECTED_FRAME_LEVEL. */
+ if (frame_relative_level (fi) > 0)
+ save_selected_frame (&tp->selected_frame_id,
+ &tp->selected_frame_level);
+ else
+ {
+ tp->selected_frame_level = -1;
+ tp->selected_frame_id = null_frame_id;
+ }
+ }
+}
+
/* Select frame FI (or NULL - to invalidate the selected frame). */

void
select_frame (struct frame_info *fi)
{
selected_frame = fi;
+ cache_selected_frame_on_thread ();
selected_frame_level = frame_relative_level (fi);
if (selected_frame_level == 0)
{
diff --git a/gdb/frame.h b/gdb/frame.h
index 597a45967ed..9de53e493f3 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -289,19 +289,29 @@ enum frame_type
SENTINEL_FRAME
};

-/* For every stopped thread, GDB tracks two frames: current and
- selected. Current frame is the inner most frame of the selected
- thread. Selected frame is the one being examined by the GDB
- CLI (selected using `up', `down', ...). The frames are created
- on-demand (via get_prev_frame()) and then held in a frame cache. */
-/* FIXME: cagney/2002-11-28: Er, there is a lie here. If you do the
- sequence: `thread 1; up; thread 2; thread 1' you lose thread 1's
- selected frame. At present GDB only tracks the selected frame of
- the current thread. But be warned, that might change. */
-/* FIXME: cagney/2002-11-14: At any time, only one thread's selected
- and current frame can be active. Switching threads causes gdb to
- discard all that cached frame information. Ulgh! Instead, current
- and selected frame should be bound to a thread. */
+/* When stopped GDB tracks two frames, the current frame and the selected
+ frame. The current frame is the inner most frame of the currently
+ selected thread, and the selected frame is the one being examined by GDB
+ (selected using `up', `down', ...), again, in the currently selected
+ thread.
+
+ The frames are created on-demand (via get_prev_frame()) and then held in
+ a frame cache.
+
+ Switching threads causes GDB to discard all cached frame information.
+ Ideally the current and selected frames might be stored within the
+ thread object, this is not currently done as invalidating the frame
+ cache would require GDB to visit each thread to invalidate its cached
+ information.
+
+ Each thread does cache the frame-id of its selected frame. When
+ switching threads GDB can (optionally, see 'set restore-selected-frame')
+ restore the previous selected frame by looking for a frame with a
+ matching frame-id. The default behaviour is to not restore the selected
+ frame when switching threads, this means that doing 'thread 1; up;
+ thread 2; thread 1' would leave the user in thread 1, frame 0. When
+ setting restore-selected-frame on then the same commands will leave the
+ user in thread 1, frame 1. */

/* On demand, create the inner most frame using information found in
the inferior. If the inner most frame can't be created, throw an
@@ -325,12 +335,10 @@ extern void reinit_frame_cache (void);
/* Return the selected frame. Always returns non-NULL. If there
isn't an inferior sufficient for creating a frame, an error is
thrown. When MESSAGE is non-NULL, use it for the error message,
- otherwise use a generic error message. */
-/* FIXME: cagney/2002-11-28: At present, when there is no selected
- frame, this function always returns the current (inner most) frame.
- It should instead, when a thread has previously had its frame
- selected (but not resumed) and the frame cache invalidated, find
- and then return that thread's previously selected frame. */
+ otherwise use a generic error message.
+
+ If no frame has previously been selected then the inner most frame will
+ be returned. */
extern struct frame_info *get_selected_frame (const char *message = nullptr);

/* Select a specific frame. NULL implies re-select the inner most
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index eef37f79e6a..324866171ab 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -370,6 +370,12 @@ 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 selected_frame_id {};
+ int selected_frame_level = -1;
+
/* Private data used by the target vector implementation. */
std::unique_ptr<private_thread_info> priv;

@@ -575,8 +581,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 82107067217..8615e7b4a72 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -698,7 +698,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;

@@ -708,7 +708,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;
}
@@ -880,7 +880,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_level = -1;
+ }
}

void
@@ -1043,6 +1046,23 @@ thread_target_id_str (thread_info *tp)
return target_id;
}

+/* When this is true, GDB restores the thread's 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. */
@@ -1155,7 +1175,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
@@ -1337,7 +1358,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);

@@ -1347,6 +1368,10 @@ switch_to_thread (thread_info *thr)
switch_to_thread_no_regs (thr);

reinit_frame_cache ();
+
+ if (restore_previous_frame && thr->selected_frame_level > -1)
+ restore_selected_frame (thr->selected_frame_id,
+ thr->selected_frame_level);
}

/* See gdbsupport/common-gdbthread.h. */
@@ -1372,13 +1397,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.get ());
+ switch_to_thread (m_thread.get (), restore_selected_frame_per_thread);
else
switch_to_inferior_no_thread (m_inf.get ());

/* 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,8 @@ thread_apply_all_command (const char *cmd, int from_tty)
scoped_restore_current_thread restore_thread;

for (thread_info_ref &thr : thr_list_cpy)
- if (switch_to_thread_if_alive (thr.get ()))
+ if (switch_to_thread_if_alive (thr.get (),
+ restore_selected_frame_per_thread))
thr_try_catch_cmd (thr.get (), cmd, from_tty, flags);
}
}
@@ -1769,7 +1798,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;
@@ -1943,7 +1972,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 ();
@@ -2210,6 +2239,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.4