public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior
@ 2020-01-29 14:11 Tom de Vries (Code Review)
  2020-01-29 15:33 ` Mihails Strasuns (Code Review)
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-29 14:11 UTC (permalink / raw)
  To: gdb-patches

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 causing 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
<repeat>
......

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
- 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, and breaking out
of the loop.

Build and reg-tested on x86_64-linux.

gdb/ChangeLog:

2020-01-29  Tom de Vries  <tdevries@suse.de>

	PR threads/25478
	* infrun.c (stop_all_threads): Return when detecting event
	TARGET_WAITKIND_SIGNALLED with signal GDB_SIGNAL_KILL.

Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
---
M gdb/infrun.c
1 file changed, 5 insertions(+), 1 deletion(-)



diff --git a/gdb/infrun.c b/gdb/infrun.c
index 22de42c..e34ddc8 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4772,7 +4772,10 @@
 				  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)
+	    goto done;
+	  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)
@@ -4872,6 +4875,7 @@
 	}
     }
 
+ done:
   if (debug_infrun)
     fprintf_unfiltered (gdb_stdlog, "infrun: stop_all_threads done\n");
 }

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 1
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-MessageType: newchange

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

* [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
@ 2020-01-29 15:33 ` Mihails Strasuns (Code Review)
  2020-01-29 16:13 ` Tom de Vries (Code Review)
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Mihails Strasuns (Code Review) @ 2020-01-29 15:33 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches; +Cc: Pedro Alves

Mihails Strasuns has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 1:

This seems very similar to https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/133 - I don't think there should be any logic specific to SIGKILL here. It seems like a general race condition for early termination while `stop_all_threads` is being executed.


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 1
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-Comment-Date: Wed, 29 Jan 2020 14:39:15 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
  2020-01-29 15:33 ` Mihails Strasuns (Code Review)
@ 2020-01-29 16:13 ` Tom de Vries (Code Review)
  2020-01-29 16:20 ` Tankut Baris Aktemur (Code Review)
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-29 16:13 UTC (permalink / raw)
  To: gdb-patches; +Cc: Mihails Strasuns, Pedro Alves

Tom de Vries has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 1:

> Patch Set 1:
> 
> This seems very similar to https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/133
> - I don't think there should be any logic specific to SIGKILL here. It seems like a
> general race condition for early termination while `stop_all_threads` is being executed.

Thanks for noticing, that's useful.

FWIW, I looked over the review comments there, and noticed the suggestion to "leave the TARGET_WAITKIND_EXITED/TARGET_WAITKIND_SIGNALLED event pending", which I tried using this additional patch:
...
diff --git a/gdb/infrun.c b/gdb/infrun.c
index e34ddc83b45..c1035c25d7f 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4774,7 +4774,11 @@ stop_all_threads (void)
 
          if (event.ws.kind == TARGET_WAITKIND_SIGNALLED
              && event.ws.value.sig == GDB_SIGNAL_KILL)
-           goto done;
+           {
+             thread_info *t = find_thread_ptid (event.target, event.ptid);
+             save_waitstatus (t, &event.ws);
+             goto done;
+           }
          else if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED
              || event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
              || event.ws.kind == TARGET_WAITKIND_EXITED
...

But I didn't notice any difference in behaviour.


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 1
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-Comment-Date: Wed, 29 Jan 2020 16:02:12 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
  2020-01-29 15:33 ` Mihails Strasuns (Code Review)
  2020-01-29 16:13 ` Tom de Vries (Code Review)
@ 2020-01-29 16:20 ` Tankut Baris Aktemur (Code Review)
  2020-01-30 14:55 ` Tom de Vries (Code Review)
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tankut Baris Aktemur (Code Review) @ 2020-01-29 16:20 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches; +Cc: Mihails Strasuns, Pedro Alves

Tankut Baris Aktemur has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 1:

> FWIW, I looked over the review comments there, and noticed the suggestion to "leave the TARGET_WAITKIND_EXITED/TARGET_WAITKIND_SIGNALLED event pending"

For the continuation of the discussion, please see
https://sourceware.org/ml/gdb-patches/2020-01/msg00212.html


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 1
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Wed, 29 Jan 2020 16:13:34 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (2 preceding siblings ...)
  2020-01-29 16:20 ` Tankut Baris Aktemur (Code Review)
@ 2020-01-30 14:55 ` Tom de Vries (Code Review)
  2020-01-30 16:30 ` [review v2] " Tom de Vries (Code Review)
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-30 14:55 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tankut Baris Aktemur, Mihails Strasuns, Pedro Alves

Tom de Vries has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 1:

> FWIW, I looked over the review comments there, and noticed the suggestion to "leave the TARGET_WAITKIND_EXITED/TARGET_WAITKIND_SIGNALLED event pending", which I tried using this additional patch:
> ...
> diff --git a/gdb/infrun.c b/gdb/infrun.c
> index e34ddc83b45..c1035c25d7f 100644
> --- a/gdb/infrun.c
> +++ b/gdb/infrun.c
> @@ -4774,7 +4774,11 @@ stop_all_threads (void)
>  
>           if (event.ws.kind == TARGET_WAITKIND_SIGNALLED
>               && event.ws.value.sig == GDB_SIGNAL_KILL)
> -           goto done;
> +           {
> +             thread_info *t = find_thread_ptid (event.target, event.ptid);
> +             save_waitstatus (t, &event.ws);
> +             goto done;
> +           }
>           else if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED
>               || event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
>               || event.ws.kind == TARGET_WAITKIND_EXITED
> ...
> 
> But I didn't notice any difference in behaviour.

I've realized from another use of save_waitstatus that I have to set t->resumed in order to have the saved wait status noticed.

With this additional patch, I did manage to observe a difference in behaviour:
...
@@ -4777,6 +4777,7 @@ stop_all_threads (void)
            {
              thread_info *t = find_thread_ptid (event.target, event.ptid);
              save_waitstatus (t, &event.ws);
+             t->resumed = 1;
              goto done;
            }
          else if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED
...

The difference is in what happens when I continue after the ^C is handled:
- without the two patches gdb hangs, until I press enter, after which I get the
  prompt
- with the two patches, gdb just presents the prompt


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 1
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Thu, 30 Jan 2020 14:52:10 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review v2] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (3 preceding siblings ...)
  2020-01-30 14:55 ` Tom de Vries (Code Review)
@ 2020-01-30 16:30 ` Tom de Vries (Code Review)
  2020-01-30 17:36 ` Tankut Baris Aktemur (Code Review)
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-30 16:30 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Mihails Strasuns, Tankut Baris Aktemur

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
<repeat>
......

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, and
- breaking out of the 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  <tdevries@suse.de>

	PR threads/25478
	* infrun.c (stop_all_threads): Detecting event
	TARGET_WAITKIND_SIGNALLED with signal GDB_SIGNAL_KILL, make event
	pending again and return.

Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
---
M gdb/infrun.c
1 file changed, 10 insertions(+), 1 deletion(-)



diff --git a/gdb/infrun.c b/gdb/infrun.c
index 22de42c..2fd1066 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4772,7 +4772,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;
+	      goto done;
+	    }
+	  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)
@@ -4872,6 +4880,7 @@
 	}
     }
 
+ done:
   if (debug_infrun)
     fprintf_unfiltered (gdb_stdlog, "infrun: stop_all_threads done\n");
 }

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 2
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-MessageType: newpatchset

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

* [review v2] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (4 preceding siblings ...)
  2020-01-30 16:30 ` [review v2] " Tom de Vries (Code Review)
@ 2020-01-30 17:36 ` Tankut Baris Aktemur (Code Review)
  2020-01-30 17:42 ` Tom de Vries (Code Review)
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tankut Baris Aktemur (Code Review) @ 2020-01-30 17:36 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches; +Cc: Mihails Strasuns, Pedro Alves

Tankut Baris Aktemur has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 2:

(1 comment)

| --- gdb/infrun.c
| +++ gdb/infrun.c
| @@ -4767,11 +4767,19 @@ stop_all_threads (void)
|  	  if (debug_infrun)
|  	    {
|  	      fprintf_unfiltered (gdb_stdlog,
|  				  "infrun: stop_all_threads %s %s\n",
|  				  target_waitstatus_to_string (&event.ws).c_str (),
|  				  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)

PS2, Line 4776:

Why only this case?  Wouldn't the same problem be seen if, say, a
different signal or an exited status was received?

| +	    {
| +	      thread_info *t = find_thread_ptid (event.target, event.ptid);
| +	      save_waitstatus (t, &event.ws);
| +	      t->resumed = 1;
| +	      goto done;
| +	    }
| +	  else if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED
|  	      || event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
|  	      || event.ws.kind == TARGET_WAITKIND_EXITED

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 2
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Thu, 30 Jan 2020 16:41:03 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review v2] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (5 preceding siblings ...)
  2020-01-30 17:36 ` Tankut Baris Aktemur (Code Review)
@ 2020-01-30 17:42 ` Tom de Vries (Code Review)
  2020-01-30 17:43 ` Tom de Vries (Code Review)
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-30 17:42 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tankut Baris Aktemur, Mihails Strasuns, Pedro Alves

Tom de Vries has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 2:

(1 comment)

| --- gdb/infrun.c
| +++ gdb/infrun.c
| @@ -4767,11 +4767,19 @@ stop_all_threads (void)
|  	  if (debug_infrun)
|  	    {
|  	      fprintf_unfiltered (gdb_stdlog,
|  				  "infrun: stop_all_threads %s %s\n",
|  				  target_waitstatus_to_string (&event.ws).c_str (),
|  				  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)

PS2, Line 4776:

For now I'm just trying to solve the PR in the absolute minimal way.

| +	    {
| +	      thread_info *t = find_thread_ptid (event.target, event.ptid);
| +	      save_waitstatus (t, &event.ws);
| +	      t->resumed = 1;
| +	      goto done;
| +	    }
| +	  else if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED
|  	      || event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
|  	      || event.ws.kind == TARGET_WAITKIND_EXITED

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 2
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Thu, 30 Jan 2020 17:35:54 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-MessageType: comment

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

* [review v2] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (6 preceding siblings ...)
  2020-01-30 17:42 ` Tom de Vries (Code Review)
@ 2020-01-30 17:43 ` Tom de Vries (Code Review)
  2020-01-30 17:52 ` [review v3] " Tom de Vries (Code Review)
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-30 17:43 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tankut Baris Aktemur, Mihails Strasuns, Pedro Alves

Tom de Vries has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 2:

(1 comment)

| --- gdb/infrun.c
| +++ gdb/infrun.c
| @@ -4773,10 +4773,18 @@ stop_all_threads (void)
|  	    }
|  
| -	  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;
| +	      goto done;

PS2, Line 4781:

I've realized that the 'goto done' is not a good idea, since
all_non_exited_threads can iterate over more than one inferior.

I'll upload a new version that removes the 'goto done.

| +	    }
| +	  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)
|  	    {
|  	      /* All resumed threads exited
|  		 or one thread/process exited/signalled.  */
|  	    }

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 2
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Thu, 30 Jan 2020 17:42:19 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review v3] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (7 preceding siblings ...)
  2020-01-30 17:43 ` Tom de Vries (Code Review)
@ 2020-01-30 17:52 ` Tom de Vries (Code Review)
  2020-02-03 14:30 ` Tankut Baris Aktemur (Code Review)
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-01-30 17:52 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Mihails Strasuns, Tankut Baris Aktemur

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
<repeat>
......

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  <tdevries@suse.de>

	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)

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 3
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-MessageType: newpatchset

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

* [review v3] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (8 preceding siblings ...)
  2020-01-30 17:52 ` [review v3] " Tom de Vries (Code Review)
@ 2020-02-03 14:30 ` Tankut Baris Aktemur (Code Review)
  2020-02-03 15:20 ` Tom de Vries (Code Review)
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Tankut Baris Aktemur (Code Review) @ 2020-02-03 14:30 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches; +Cc: Mihails Strasuns, Pedro Alves

Tankut Baris Aktemur has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 3:

I've tried this patch in two scenarios.  One is when multi-threading is
enabled (executable is multi.out), and the other is when the program is
single-threaded (executable is named single1.out and single2.out -- exactly
the same copies just to distinguish the processes).  The test program is
sleep.c from the PR.

I start the multi-threaded case as follows:

  $ gdb -ex "set prompt (master-gdb) " -ex "b stop_all_threads" -ex r -ex \
  "shell killall -9 multi.out" -ex c --args gdb -ex "set debug infrun 1" \
  -ex r  --args multi.out

When gdb stops, I try "info inferiors" and "info threads" commands:

  Thread 1 received signal SIGINT, Interrupt.
  0x00007ffff78a99d0 in nanosleep () from /lib/x86_64-linux-gnu/libc.so.6
  infrun: infrun_async(0)
  (gdb) i inferiors
    Num  Description       Connection           Executable
  * 1    process 12979     1 (native)           /path/to/multi.out
  (gdb) i threads
    Id   Target Id                         Frame
  * 1    Thread 0x7ffff7fe3740 (LWP 12979) 0x00007ffff78a99d0 in nanosleep () from /lib/x86_64-linux-gnu/libc.so.6
    2    Thread 0x7ffff77c4700 (LWP 12996) Couldn't get registers: No such process.
  (gdb) thread 2
  [Switching to thread 2 (Thread 0x7ffff77c4700 (LWP 12996))]
  Couldn't get registers: No such process.
  (gdb) i threads
  Couldn't get registers: No such process.
  (gdb) thread 1
  Couldn't get registers: No such process.

The second scenario is similar, except that instead of a multi-threaded
single process, we have two single-threaded processes.  Started as

  $ gdb -ex "set prompt (master-gdb) " -ex "b stop_all_threads" -ex r -ex c \
  -ex c -ex "shell killall -9 single1.out" -ex c --args gdb -ex "set debug infrun 1" \
  -ex start -ex "add-inferior -exec ./single2.out" -ex "inferior 2" -ex "start" \
  -ex "set schedule-multiple on" -ex c --args ./single1.out

The behavior is this:

  (gdb) info inferiors
    Num  Description       Connection           Executable
    1    process 2266      1 (native)           /path/to/single1.out
  * 2    process 2282      1 (native)           /path/to/single2.out
  (gdb) info threads
    Id   Target Id                  Frame
    1.1  process 2266               Couldn't get registers: No such process.
  (gdb) inferior 1
  [Switching to inferior 1 [process 2266] (/path/to/single1.out)]
  [Switching to thread 1.1 (process 2266)]
  Couldn't get registers: No such process.
  (gdb) info inferiors
    Num  Description       Connection           Executable
  Couldn't get registers: No such process.
  (gdb) info threads
  Couldn't get registers: No such process.
  (gdb)

So, I think at this point it boils down to the discussion covered in
https://sourceware.org/ml/gdb-patches/2020-01/msg00212.html
and
https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/133
GDB is not able to handle disappeared processes gracefully.  The 
maintainers may consider the fix for the hanging behavior (i.e.  the
infinite loop) OK and the weird post-behavior above as a separate problem
to be addressed later, but my understanding from Pedro's comment was that
the deeper problem of GDB not handling already-gone processes well shall be
addressed.

Thanks
-Baris


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 3
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Mon, 03 Feb 2020 14:30:20 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review v3] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (9 preceding siblings ...)
  2020-02-03 14:30 ` Tankut Baris Aktemur (Code Review)
@ 2020-02-03 15:20 ` Tom de Vries (Code Review)
  2020-02-05 19:53 ` [review v4] " Tom de Vries (Code Review)
  2020-02-05 19:53 ` Tom de Vries (Code Review)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-02-03 15:20 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tankut Baris Aktemur, Mihails Strasuns, Pedro Alves

Tom de Vries has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 3:

> Patch Set 3:
> GDB is not able to handle disappeared processes gracefully.

Ack.

> The 
> maintainers may consider the fix for the hanging behavior (i.e.  the
> infinite loop) OK and the weird post-behavior above as a separate problem

That's my understanding from Pedro's comment: the weird post-behaviour is a pre-existing issue, independent of the hang that this patch fixes.


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 3
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Mon, 03 Feb 2020 15:20:12 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

* [review v4] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (10 preceding siblings ...)
  2020-02-03 15:20 ` Tom de Vries (Code Review)
@ 2020-02-05 19:53 ` Tom de Vries (Code Review)
  2020-02-05 19:53 ` Tom de Vries (Code Review)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-02-05 19:53 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Mihails Strasuns, Tankut Baris Aktemur

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
<repeat>
......

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  <tdevries@suse.de>

	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.

gdb/testsuite/ChangeLog:

2020-02-05  Tom de Vries  <tdevries@suse.de>

	* gdb.threads/kill-in-stop-all-threads.c: New test.
	* gdb.threads/kill-in-stop-all-threads.exp: New file.

Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
---
M gdb/infrun.c
A gdb/testsuite/gdb.threads/kill-in-stop-all-threads.c
A gdb/testsuite/gdb.threads/kill-in-stop-all-threads.exp
3 files changed, 182 insertions(+), 2 deletions(-)



diff --git a/gdb/infrun.c b/gdb/infrun.c
index 3e846f8..a722da5 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 = false;
+		  if (t->suspend.waitstatus.kind == TARGET_WAITKIND_SIGNALLED
+		      && t->suspend.waitstatus.value.sig == GDB_SIGNAL_KILL
+		      && t->suspend.waitstatus_pending_p)
+		    ;
+		  else
+		    t->resumed = false;
 		}
 	    }
 
@@ -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 = true;
+	      t->executing = false;
+	    }
+	  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)
diff --git a/gdb/testsuite/gdb.threads/kill-in-stop-all-threads.c b/gdb/testsuite/gdb.threads/kill-in-stop-all-threads.c
new file mode 100644
index 0000000..cddbd4d
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/kill-in-stop-all-threads.c
@@ -0,0 +1,42 @@
+/* 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 <unistd.h>
+#include <signal.h>
+
+static void *
+fun (void *dummy)
+{
+  raise (SIGINT);
+  while (1)
+    sleep (1);
+
+  return NULL;
+}
+
+int
+main (void)
+{
+  pthread_t thread;
+  pthread_create (&thread, NULL, fun, NULL);
+
+  while (1)
+    sleep (1);
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.threads/kill-in-stop-all-threads.exp b/gdb/testsuite/gdb.threads/kill-in-stop-all-threads.exp
new file mode 100644
index 0000000..2df509f
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/kill-in-stop-all-threads.exp
@@ -0,0 +1,125 @@
+# 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 test-case starts gdb with a $binfile inferior, which sends a SIGINT to
+# itself.  Then when gdb handles the SIGINT and starts executing
+# stop_all_threads, the inferior is killed by a SIGKILL.
+#
+# In order to time the SIGKILL, the gdb itself is run using another gdb, which
+# allows us to set a breakpoint on stop_all_threads.  So, we have the following
+# hierarchy:
+# - master gdb
+# - inferior gdb
+# - $binfile
+#
+# This setup make this a non-standard test-case.
+#
+
+standard_testfile
+
+if {[build_executable "failed to build" $testfile $srcfile \
+	 {debug pthreads}] == -1} {
+    return -1
+}
+
+# Setup master gdb, with inferior gdb as executable.
+clean_restart $GDB
+
+# Set master gdb to have a different prompt, to make it easier to distinguish
+# between prompts of master gdb and inferior gdb.
+set saved_gdb_prompt $gdb_prompt
+set master_gdb_prompt "\\(master-gdb\\)"
+set gdb_prompt $master_gdb_prompt
+gdb_test_no_output "set prompt $master_gdb_prompt "
+
+# Set the arguments for the inferior gdb.
+gdb_test_no_output "set args $INTERNAL_GDBFLAGS $GDBFLAGS $binfile"
+
+# Run to main in master gdb.
+if ![runto_main] then {
+    fail "can't run to main"
+    return 0
+}
+
+# Set a breakpoint for the inferior gdb in master gdb.
+if {[gdb_breakpoint "stop_all_threads" no-message] != 1 } {
+    # If we cannot set this breakpoint, there no point in trying further.
+    # Bail out.
+    return -1
+}
+
+# Continue from main in master gdb to initial inferior gdb prompt.
+set gdb_prompt $saved_gdb_prompt
+gdb_test "continue" "Continuing\..*Reading symbols from.*" \
+    "continue from gdb main"
+
+# Start $binfile
+set gdb_prompt $master_gdb_prompt
+gdb_test "start" "Starting program.*Breakpoint 2, stop_all_threads \\(\\).*" \
+    "start inferior"
+
+# We run into stop_all_threads breakpoint in master gdb here, but it's too
+# early, continue to inferior gdb prompt.
+set gdb_prompt $saved_gdb_prompt
+gdb_continue_to_breakpoint "continue past stop_all_threads bp" ".*$srcfile:.*"
+
+# Get the pid of the $binfile process
+set pid -1
+gdb_test_multiple "info inferior 1" "get inferior pid" {
+    -re -wrap "process (\[0-9\]*).*" {
+	set pid $expect_out(1,string)
+	pass $gdb_test_name
+    }
+}
+if { $pid == -1 } {
+    return -1
+}
+
+# Continue $binfile.  The $binfile will then trigger SIGINT, which will
+# trigger stop_all_threads in inferior gdb, which will cause the breakpoint
+# to hit in master gdb.
+set gdb_prompt $master_gdb_prompt
+gdb_continue_to_breakpoint "continue to stop_all_threads bp" ".*infrun.c:.*"
+
+# Kill inferior in master gdb, while we're at the start of stop_all_threads in
+# inferior gdb.
+gdb_test_no_output "shell kill -9 $pid"
+
+# Continue from master gdb prompt to inferior gdb prompt.
+set gdb_prompt $saved_gdb_prompt
+gdb_test_multiple "continue" "continue to raise sigint" {
+    -re "warning: unable to open /proc file" {
+	# This warning is the first sign of trouble before we start hanging.
+	# Fail and bail out now, instead of waiting for a timeout.
+	fail $gdb_test_name
+	return -1
+    }
+    -re -wrap "Continuing\.\[\r\n\]+Thread \[0-9\]+ received signal SIGINT, Interrupt\..*" {
+	pass $gdb_test_name
+    }
+}
+
+# Check that the SIGKILL of $binfile is reported by inferior gdb.
+set killed_msg [multi_line "Program terminated with signal SIGKILL, Killed\." \
+		    "The program no longer exists\."]
+gdb_test "continue" "Continuing\.\[\r\n\]+$killed_msg" "continue to inferior exit"
+
+# Check that inferior gdb exits normally
+set gdb_prompt $master_gdb_prompt
+gdb_test "quit" "$inferior_exited_re normally\\\]"
+
+# Restore gdb prompt in master gdb.
+set gdb_prompt $saved_gdb_prompt
+gdb_test_no_output "set prompt $saved_gdb_prompt "

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 4
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-MessageType: newpatchset

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

* [review v4] [gdb/threads] Fix hang in stop_all_threads after killing inferior
  2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
                   ` (11 preceding siblings ...)
  2020-02-05 19:53 ` [review v4] " Tom de Vries (Code Review)
@ 2020-02-05 19:53 ` Tom de Vries (Code Review)
  12 siblings, 0 replies; 14+ messages in thread
From: Tom de Vries (Code Review) @ 2020-02-05 19:53 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tankut Baris Aktemur, Mihails Strasuns, Pedro Alves

Tom de Vries has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759
......................................................................


Patch Set 4:

Added test-case.


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ibe1f29251fe2ff1c1991f041babbe18373c113b1
Gerrit-Change-Number: 759
Gerrit-PatchSet: 4
Gerrit-Owner: Tom de Vries <tdevries@suse.de>
Gerrit-Reviewer: Pedro Alves <palves@redhat.com>
Gerrit-Reviewer: Tom de Vries <tdevries@suse.de>
Gerrit-CC: Mihails Strasuns <mihails.strasuns@intel.com>
Gerrit-CC: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
Gerrit-Comment-Date: Wed, 05 Feb 2020 19:53:46 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

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

end of thread, other threads:[~2020-02-05 19:53 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-29 14:11 [review] [gdb/threads] Fix hang in stop_all_threads after killing inferior Tom de Vries (Code Review)
2020-01-29 15:33 ` Mihails Strasuns (Code Review)
2020-01-29 16:13 ` Tom de Vries (Code Review)
2020-01-29 16:20 ` Tankut Baris Aktemur (Code Review)
2020-01-30 14:55 ` Tom de Vries (Code Review)
2020-01-30 16:30 ` [review v2] " Tom de Vries (Code Review)
2020-01-30 17:36 ` Tankut Baris Aktemur (Code Review)
2020-01-30 17:42 ` Tom de Vries (Code Review)
2020-01-30 17:43 ` Tom de Vries (Code Review)
2020-01-30 17:52 ` [review v3] " Tom de Vries (Code Review)
2020-02-03 14:30 ` Tankut Baris Aktemur (Code Review)
2020-02-03 15:20 ` Tom de Vries (Code Review)
2020-02-05 19:53 ` [review v4] " Tom de Vries (Code Review)
2020-02-05 19:53 ` Tom de Vries (Code Review)

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).