From patchwork Thu Jan 30 17:43:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 37623 Received: (qmail 85718 invoked by alias); 30 Jan 2020 17:43:16 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 85709 invoked by uid 89); 30 Jan 2020 17:43:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=HX-Languages-Length:5406 X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 30 Jan 2020 17:43:14 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id 8DA7B20387; Thu, 30 Jan 2020 12:43:12 -0500 (EST) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [IPv6:2620:52:3:1:5054:ff:fe06:16ca]) by mx1.osci.io (Postfix) with ESMTP id ED0EF2018B; Thu, 30 Jan 2020 12:43:10 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id DC7FF20AF7; Thu, 30 Jan 2020 12:43:10 -0500 (EST) X-Gerrit-PatchSet: 3 Date: Thu, 30 Jan 2020 12:43:10 -0500 From: "Tom de Vries (Code Review)" To: Pedro Alves , gdb-patches@sourceware.org Cc: Mihails Strasuns , Tankut Baris Aktemur Auto-Submitted: auto-generated X-Gerrit-MessageType: newpatchset Subject: [review v3] [gdb/threads] Fix hang in stop_all_threads after killing inferior X-Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1 X-Gerrit-Change-Number: 759 X-Gerrit-ChangeURL: X-Gerrit-Commit: 72327ba33578eb6f1adf90ac2dcc6b413f4a5766 In-Reply-To: References: Reply-To: tdevries@suse.de, tankut.baris.aktemur@intel.com, palves@redhat.com, mihails.strasuns@intel.com, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-79-g83ff7f88f1 Message-Id: <20200130174310.DC7FF20AF7@gnutoolchain-gerrit.osci.io> Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759 ...................................................................... [gdb/threads] Fix hang in stop_all_threads after killing inferior Consider a two-threaded testcase a.out, sleeping in both its threads: ... $ gdb -ex r --args a.out Reading symbols from a.out... Starting program: /data/gdb_versions/devel/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". [New Thread 0x7ffff77fe700 (LWP 31268)] ... Typing ^C causes stop_all_threads to be executed, and if an external SIGKILL (such as caused by killall -9 a.out) arrives at the start of stop_all_threads, gdb hangs in stop_all_threads after giving this warning: ... warning: unable to open /proc file '/proc/24938/status' ... Using "set debug infrun 1" we can see in more detail where we hang: ... infrun: stop_all_threads infrun: stop_all_threads, pass=0, iterations=0 infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, need stop infrun: target_wait (-1.0.0, status) = infrun: 10264.10268.0 [Thread 0x7ffff77fe700 (LWP 10268)], infrun: status->kind = signalled, signal = GDB_SIGNAL_KILL infrun: stop_all_threads status->kind = signalled, signal = GDB_SIGNAL_KILL \ Thread 0x7ffff77fe700 (LWP 10268) infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping warning: unable to open /proc file '/proc/10264/status' infrun: target_wait (-1.0.0, status) = infrun: -1.0.0 [process -1], infrun: status->kind = no-resumed infrun: infrun_async(0) infrun: stop_all_threads status->kind = no-resumed process -1 infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping infrun: stop_all_threads status->kind = no-resumed process -1 infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping infrun: stop_all_threads status->kind = no-resumed process -1 infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping ...... So, we're hanging in the 'while (1)' loop in stop_all_threads as follows: - thread t is tested, and both t->executing and t->stop_requested are found to be 1 (noted with 'executing, already stopping') - consequently need_wait is set 1 - consequently wait_one is executed - wait_one returns a TARGET_WAITKIND_NO_RESUMED event, which is handled by continuing at the start of the loop The loop actually starts with update_thread_list (), but that doesn't seem to change the state of the threads. Fix the hang by: - detecting the first sign of trouble: the TARGET_WAITKIND_SIGNALLED event with signal GDB_SIGNAL_KILL, - making that event pending again, - making sure the corresponding thread will not set need_wait again (by setting t->executing == 0) - making sure that the corresponding thread keeps t->resumed == 1 in the the all_non_exited_threads loop This results in the ^C being handled without showing the user that the test-case was killed: ... ^C Thread 1 received signal SIGINT, Interrupt. 0x00007ffff78c50f0 in nanosleep () from /lib64/libc.so.6 (gdb) ... But a subsequent continue does show that: ... (gdb) c Continuing. Program terminated with signal SIGKILL, Killed. The program no longer exists. (gdb) .... Build and reg-tested on x86_64-linux. gdb/ChangeLog: 2020-01-29 Tom de Vries PR threads/25478 * infrun.c (stop_all_threads): Detecting event TARGET_WAITKIND_SIGNALLED with signal GDB_SIGNAL_KILL, make event pending again, set t->executing to 0 and keep t->resumed set to 1. Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1 --- M gdb/infrun.c 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/gdb/infrun.c b/gdb/infrun.c index 22de42c..9587072 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -4749,7 +4749,12 @@ /* The thread may be not executing, but still be resumed with a pending status to process. */ - t->resumed = 0; + if (t->suspend.waitstatus.kind == TARGET_WAITKIND_SIGNALLED + && t->suspend.waitstatus.value.sig == GDB_SIGNAL_KILL + && t->suspend.waitstatus_pending_p) + ; + else + t->resumed = 0; } } @@ -4772,7 +4777,15 @@ target_pid_to_str (event.ptid).c_str ()); } - if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED + if (event.ws.kind == TARGET_WAITKIND_SIGNALLED + && event.ws.value.sig == GDB_SIGNAL_KILL) + { + thread_info *t = find_thread_ptid (event.target, event.ptid); + save_waitstatus (t, &event.ws); + t->resumed = 1; + t->executing = 0; + } + else if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED || event.ws.kind == TARGET_WAITKIND_THREAD_EXITED || event.ws.kind == TARGET_WAITKIND_EXITED || event.ws.kind == TARGET_WAITKIND_SIGNALLED)