From 28bf096c62d7da6b349605f3940f4c586a850f78 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Mon, 29 Jun 2015 16:07:57 +0100 Subject: PR threads/18127 - threads spawned by infcall end up stuck in "running" state Refs: https://sourceware.org/ml/gdb/2015-03/msg00024.html https://sourceware.org/ml/gdb/2015-06/msg00005.html On GNU/Linux, if an infcall spawns a thread, that thread ends up with stuck running state. This happens because: - when linux-nat.c detects a new thread, it marks them as running, and does not report anything to the core. - we skip finish_thread_state when the thread that is running the infcall stops. As result, that new thread ends up with stuck "running" state, even though it really is stopped. On Windows, _all_ threads end up stuck in running state, not just the one that was spawned. That happens because when a new thread is detected, unlike linux-nat.c, windows-nat.c reports TARGET_WAITKIND_SPURIOUS to infrun. It's the fact that that event does not cause a user-visible stop that triggers the problem. When the target is re-resumed, we call set_running with a wildcard ptid, which marks all thread as running. That set_running is not suppressed because the (leader) thread being resumed does not have in_infcall set. Later, when the infcall finally finishes successfully, nothing marks all threads back to stopped. We can trigger the same problem on all targets by having a thread other than the one that is running the infcall report a breakpoint hit to infrun, and then have that breakpoint not cause a stop. That's what the included test does. The fix is to stop GDB from suppressing the set_running calls while doing an infcall, and then set the threads back to stopped when the call finishes, iff they were originally stopped before the infcall started. (Note the MI *running/*stopped event suppression isn't affected.) Tested on x86_64 GNU/Linux. gdb/ChangeLog: 2015-06-29 Pedro Alves PR threads/18127 * infcall.c (run_inferior_call): On infcall success, if the thread was marked stopped before, reset it back to stopped. * infrun.c (resume): Don't suppress the set_running calls when doing an infcall. (normal_stop): Only discard the finish_thread_state cleanup if the infcall succeeded. gdb/testsuite/ChangeLog: 2015-06-29 Pedro Alves PR threads/18127 * gdb.threads/hand-call-new-thread.c: New file. * gdb.threads/hand-call-new-thread.c: New file. --- gdb/infcall.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'gdb/infcall.c') diff --git a/gdb/infcall.c b/gdb/infcall.c index f79afea..e3bd72a 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -387,6 +387,7 @@ run_inferior_call (struct thread_info *call_thread, CORE_ADDR real_pc) int saved_in_infcall = call_thread->control.in_infcall; ptid_t call_thread_ptid = call_thread->ptid; int saved_sync_execution = sync_execution; + int was_running = call_thread->state == THREAD_RUNNING; /* Infcalls run synchronously, in the foreground. */ if (target_can_async_p ()) @@ -433,6 +434,26 @@ run_inferior_call (struct thread_info *call_thread, CORE_ADDR real_pc) CALL_THREAD as it could be invalid if its thread has exited. */ call_thread = find_thread_ptid (call_thread_ptid); + /* If the infcall does NOT succeed, normal_stop will have already + finished the thread states. However, on success, normal_stop + defers here, so that we can set back the thread states to what + they were before the call. Note that we must also finish the + state of new threads that might have spawned while the call was + running. The main cases to handle are: + + - "(gdb) print foo ()", or any other command that evaluates an + expression at the prompt. (The thread was marked stopped before.) + + - "(gdb) break foo if return_false()" or similar cases where we + do an infcall while handling an event (while the thread is still + marked running). In this example, whether the condition + evaluates true and thus we'll present a user-visible stop is + decided elsewhere. */ + if (!was_running + && ptid_equal (call_thread_ptid, inferior_ptid) + && stop_stack_dummy == STOP_STACK_DUMMY) + finish_thread_state (user_visible_resume_ptid (0)); + enable_watchpoints_after_interactive_call_stop (); /* Call breakpoint_auto_delete on the current contents of the bpstat -- cgit v1.1