diff options
author | Pedro Alves <palves@redhat.com> | 2020-01-10 20:05:41 +0000 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2020-01-10 20:05:41 +0000 |
commit | 873657b9e824943ae44c12966c29cbbcd21c986f (patch) | |
tree | 151b26a7c25ecb41375900ae9cdf52eeef701a4f /gdb/infrun.c | |
parent | 7f0ae84c80aae6fe8f7573343fe1ab455d18b5d8 (diff) | |
download | binutils-gdb-873657b9e824943ae44c12966c29cbbcd21c986f.tar.gz |
Preserve selected thread in all-stop w/ background execution
In non-stop mode, if you resume the program in the background (with
"continue&", for example), then gdb makes sure to not switch the
current thread behind your back. That means that you can be sure that
the commands you type apply to the thread you selected, even if some
other thread that was running in the background hits some event just
while you're typing.
In all-stop mode, however, if you resume the program in the
background, gdb let's the current thread switch behind your back.
This is bogus, of course. All-stop and non-stop background
resumptions should behave the same.
This patch fixes that, and adds a testcase that exposes the bad
behavior in current master.
The fork-running-state.exp changes are necessary because that
preexisting testcase was expecting the old behavior:
Before:
continue &
Continuing.
(gdb)
[Attaching after process 8199 fork to child process 8203]
[New inferior 2 (process 8203)]
info threads
Id Target Id Frame
1.1 process 8199 "fork-running-st" (running)
* 2.1 process 8203 "fork-running-st" (running)
(gdb)
After:
continue &
Continuing.
(gdb)
[Attaching after process 24660 fork to child process 24664]
[New inferior 2 (process 24664)]
info threads
Id Target Id Frame
* 1.1 process 24660 "fork-running-st" (running)
2.1 process 24664 "fork-running-st" (running)
(gdb)
Here we see that before this patch GDB switches current inferior to
the new inferior behind the user's back, as a side effect of handling
the fork.
The delete_exited_threads call in inferior_appeared is there to fix an
issue that Baris found in a previous version of this patch. The
fetch_inferior_event change increases the refcount of the current
thread, and in case the fetched inferior event denotes a thread exit,
the thread will not be deleted right away. A non-deleted but exited
thread stays in the inferior's thread list. This, in turn, causes the
"init_thread_list" call in inferior.c to be skipped. A consequence is
that the global thread ID counter is not restarted if the current
thread exits, and then the inferior is restarted:
(gdb) start
Temporary breakpoint 1 at 0x4004d6: file main.c, line 21.
Starting program: /tmp/main
Temporary breakpoint 1, main () at main.c:21
21 foo ();
(gdb) info threads -gid
Id GId Target Id Frame
* 1 1 process 16106 "main" main () at main.c:21
(gdb) c
Continuing.
[Inferior 1 (process 16106) exited normally]
(gdb) start
Temporary breakpoint 2 at 0x4004d6: file main.c, line 21.
Starting program: /tmp/main
Temporary breakpoint 2, main () at main.c:21
21 foo ();
(gdb) info threads -gid
Id GId Target Id Frame
* 1 2 process 16138 "main" main () at main.c:21
^^^
Notice that GId == 2 above. It should have been "1" instead.
The new tids-git-reset.exp testcase exercises the problem above.
gdb/ChangeLog:
2020-01-10 Pedro Alves <palves@redhat.com>
* gdbthread.h (scoped_restore_current_thread)
<dont_restore, restore, m_dont_restore>: Declare.
* thread.c (thread_alive): Add assertion. Return bool.
(switch_to_thread_if_alive): New.
(prune_threads): Switch inferior/thread.
(print_thread_info_1): Switch thread before calling target methods.
(scoped_restore_current_thread::restore): New, factored out from
...
(scoped_restore_current_thread::~scoped_restore_current_thread):
... this.
(scoped_restore_current_thread::scoped_restore_current_thread):
Add assertion.
(thread_apply_all_command, thread_select): Use
switch_to_thread_if_alive.
gdb/testsuite/ChangeLog:
2020-01-10 Pedro Alves <palves@redhat.com>
* gdb.base/fork-running-state.exp (do_test): Adjust expected
output.
* gdb.threads/async.c: New.
* gdb.threads/async.exp: New.
* gdb.multi/tids-gid-reset.c: New.
* gdb.multi/tids-gid-reset.exp: New.
Diffstat (limited to 'gdb/infrun.c')
-rw-r--r-- | gdb/infrun.c | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/gdb/infrun.c b/gdb/infrun.c index 60567aac402..d876634f19b 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -3039,6 +3039,11 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal) finish_state.release (); + /* If we've switched threads above, switch back to the previously + current thread. We don't want the user to see a different + selected thread. */ + switch_to_thread (cur_thr); + /* Tell the event loop to wait for it to stop. If the target supports asynchronous execution, it'll do this from within target_resume. */ @@ -3693,14 +3698,11 @@ fetch_inferior_event (void *client_data) set_current_traceframe (-1); } - gdb::optional<scoped_restore_current_thread> maybe_restore_thread; - - if (non_stop) - /* In non-stop mode, the user/frontend should not notice a thread - switch due to internal events. Make sure we reverse to the - user selected thread and frame after handling the event and - running any breakpoint commands. */ - maybe_restore_thread.emplace (); + /* The user/frontend should not notice a thread switch due to + internal events. Make sure we revert to the user selected + thread and frame after handling the event and running any + breakpoint commands. */ + scoped_restore_current_thread restore_thread; overlay_cache_invalid = 1; /* Flush target cache before starting to handle each event. Target @@ -3777,6 +3779,19 @@ fetch_inferior_event (void *client_data) inferior_event_handler (INF_EXEC_COMPLETE, NULL); cmd_done = 1; } + + /* If we got a TARGET_WAITKIND_NO_RESUMED event, then the + previously selected thread is gone. We have two + choices - switch to no thread selected, or restore the + previously selected thread (now exited). We chose the + later, just because that's what GDB used to do. After + this, "info threads" says "The current thread <Thread + ID 2> has terminated." instead of "No thread + selected.". */ + if (!non_stop + && cmd_done + && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED) + restore_thread.dont_restore (); } } |