Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Yao Qi <qiyaoltc@gmail.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 02/18] Remote all-stop-on-top-of-non-stop
Date: Mon, 23 Nov 2015 15:40:00 -0000	[thread overview]
Message-ID: <56533372.3030805@redhat.com> (raw)
In-Reply-To: <86vb9x1u2h.fsf@gmail.com>

Hi Yao,

Thanks for the review, and sorry for the delay in getting
back to this...

On 10/23/2015 04:30 PM, Yao Qi wrote:
> Pedro Alves <palves@redhat.com> writes:
> 
>> @@ -2636,8 +2633,37 @@ attach_command_post_wait (char *args, int from_tty, int async_exec)
>>    target_post_attach (ptid_get_pid (inferior_ptid));
>>  
>>    post_create_inferior (&current_target, from_tty);
>> +}
>> +
>> +/* What to do after the first program stops after attaching.  */
>> +enum attach_post_wait_mode
>> +{
>> +  /* Do nothing.  Leaves threads as they are.  */
>> +  ATTACH_POST_WAIT_NOTHING,
>> +
>> +  /* Re-resume threads that are marked running.  */
>> +  ATTACH_POST_WAIT_RESUME,
>> +
>> +  /* Stop all threads.  */
>> +  ATTACH_POST_WAIT_STOP,
>> +};
>> +
>> +/* Called after we've attached to a process and we've seen it stop for
>> +   the first time.  If ASYNC_EXEC is true, re-resume threads that
>> +   should be running.  Else if ATTACH, */
>> +
> 
> Comments are not complete.

Whoops, at some point I was going to add an 'attach' parameter, then I
converted it to the new enum.  Fixed.

> 
>> +
>> +  /* Now go over all threads that are stopped, and print their current
>> +     frame.  If all-stop, then if there's a signalled thread, pick
>> +     that as current.  */
> 
> Is it the code you described in the last paragraph of the commit log?

Yes.

> 
>> +  ALL_NON_EXITED_THREADS (thread)
>> +    {
>> +      struct target_waitstatus *ws;
>> +
>> +      if (first == NULL)
>> +	first = thread;
>> +
>> +      if (!non_stop)
>> +	set_running (thread->ptid, 0);
>> +      else if (thread->state != THREAD_STOPPED)
>> +	continue;
>> +
>> +      ws = &thread->suspend.waitstatus;
>> +
>> +      if (selected == NULL
>> +	  && thread->suspend.waitstatus_pending_p)
>> +	selected = thread;
>> +
>> +      if (lowest == NULL || thread->num < lowest->num)
>> +	lowest = thread;
>> +
>> +      if (non_stop)
>> +	print_one_stopped_thread (thread);
>> +    }
>> +
>> +  /* In all-stop, we only print the status of one thread, and leave
>> +     others with their status pending.  */
>> +  if (!non_stop)
>> +    {
>> +      thread = selected;
>> +      if (thread == NULL)
>> +	thread = lowest;
>> +      if (thread == NULL)
>> +	thread = first;
> 
> Looks lowest can't be NULL, so first isn't used.

"lowest" here actually means "lowest stopped".  It can thus be NULL if
all threads are running.  I've renamed it to 'lowest_stopped' now
to make that clearer.

> 
>> +
>> +      print_one_stopped_thread (thread);
>> +    }
>> +
>> +  /* For "info program".  */
>> +  thread = inferior_thread ();
>> +  if (thread->state == THREAD_STOPPED)
>> +    set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
>>  }
>>  
>>  static void
>> @@ -3826,7 +3942,7 @@ remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
> 
>> @@ -12936,11 +13040,15 @@ remote_async (struct target_ops *ops, int enable)
>>  	 event loop to process them.  */
>>        if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
>>  	mark_async_event_handler (remote_async_inferior_event_token);
>> +      if (target_is_non_stop_p ())
>> +	mark_async_event_handler (rs->notif_state->get_pending_events_token);
> 
> I don't understand why do we need to mark the pending event token.
> Previously, we only need to do so when GDB sees a notification.

Thanks for pointing this out.  I should have added some comments here,
as it took me a while to re-understand it.  :-)

To explain why I added this, I need to start with the "else" branch ... 

> 
>>      }
>>    else
>>      {
>>        serial_async (rs->remote_desc, NULL, NULL);
>>        clear_async_event_handler (remote_async_inferior_event_token);
>> +      if (target_is_non_stop_p ())
>> +	clear_async_event_handler (rs->notif_state->get_pending_events_token);

... here.

Currently, it's possible that target_async(0) is called while the
pending event token is marked.  If we clear the pending event token when
disabling async, we need to mark it again when re-enabling async.  To simplify
(and avoid keeping track of whether the token was marked), I just always
marked the token when enabling async.  It just results in at most one
harmless spurious event-loop wake up.

That explains why we now need to mark the pending event token
while previously we didn't, but not why do we need to _clear_ the
pending event token while previously we didn't.  To understand
that, it helps to identify the target_async(0) call in question.  That's
called after hitting a breakpoint and reporting an event to the
user (in inf-loop.c).

    case INF_EXEC_COMPLETE:
      if (!non_stop)
	{
	  /* Unregister the inferior from the event loop.  This is done
	     so that when the inferior is not running we don't get
	     distracted by spurious inferior output.  */
	  if (target_has_execution && target_can_async_p ())
	    target_async (0);
	}

Thus if we don't clear the get_pending_events_token when disabling async,
target_wait ends up called even after target_async is disabled.  That
would normally be harmless, but annoying as it breaks testing
with "set debug infrun 1", because you end up with a spurious
TARGET_WAITKIND_IGNORE event after gdb has already printed the prompt,
which breaks the testsuite.

But in this case, it's actually worse.  It results in the event loop being
_continuously_ woken (#4 - #8):

 #1 - the event loop wakes up for get_pending_events_token, which results in
 #2 - remote_notif_stop_can_get_pending_events -> mark remote_async_inferior_event_token.
 #3 - event loop wakes up for remote_async_inferior_event_token
 #4 - fetch_inferior_event -> target_wait -> remote_wait -> TARGET_WAITKIND_IGNORE
 #5 - handle_inferior_event -> prepare_to_wait
 #6 - because async is off, prepare_to_wait calls mark_infrun_async_event_handler.
 #7 - event loop wakes up for infrun_async_inferior_event_token -> fetch_inferior_event.
 #8 - goto #4.

E.g., just running to main with logging enabled shows:

...
notif: discard queued event: 'Stop' in Thread 0
infrun: target_wait (-1.0.0, status) =
infrun:   -1.0.0 [Thread 0],
infrun:   status->kind = ignore
infrun: TARGET_WAITKIND_IGNORE
infrun: prepare_to_wait
notif: discard queued event: 'Stop' in Thread 0
infrun: target_wait (-1.0.0, status) =
infrun:   -1.0.0 [Thread 0],
infrun:   status->kind = ignore
infrun: TARGET_WAITKIND_IGNORE
infrun: prepare_to_wait
notif: discard queued event: 'Stop' in Thread 0
infrun: target_wait (-1.0.0, status) =
infrun:   -1.0.0 [Thread 0],
infrun:   status->kind = ignore
infrun: TARGET_WAITKIND_IGNORE
infrun: prepare_to_wait
notif: discard queued event: 'Stop' in Thread 0
infrun: target_wait (-1.0.0, status) =
infrun:   -1.0.0 [Thread 0],
infrun:   status->kind = ignore
infrun: TARGET_WAITKIND_IGNORE
infrun: prepare_to_wait
notif: discard queued event: 'Stop' in Thread 0
infrun: target_wait (-1.0.0, status) =
infrun:   -1.0.0 [Thread 0],
infrun:   status->kind = ignore
infrun: TARGET_WAITKIND_IGNORE
infrun: prepare_to_wait
notif: discard queued event: 'Stop' in Thread 0
infrun: target_wait (-1.0.0, status) =
infrun:   -1.0.0 [Thread 0],
infrun:   status->kind = ignore
infrun: TARGET_WAITKIND_IGNORE
infrun: prepare_to_wait
notif: discard queued event: 'Stop' in Thread 0
...

Forever and ever.  The testsuite actually regresses due to this, as some
tests enabling infrun logging (e.g., breakpoint-in-ro-region.exp).

The patch below (applies on top of the series) fixes this by making it so
that the pending event token is already clear when we get to the
target_async(0) call in question.  The testsuite does pass cleanly with
this patch and without the patch, but regresses if I apply only the
remote_async hunk (that is, if I revert the changes in the patch #2
under discussion).

The remote_notif_get_pending_events hunk below feels like a good
improvement to me, as it gets rid of unnecessary event loop wake ups.
However, I still think that we need the
clear_async_event_handler / mark_async_event_handler calls in
remote_async like I had them before.

That's because we have other target_async(0) calls that unlike the
inf-loop.c call happen while the target _is_ still running.  In particular,
I'm thinking of the one in top.c:gdb_readline_wrapper, when we're about to
show a secondary prompt and nest a secondary event loop that should not
get woken by target events.

So I'm thinking that I should leave the original patch as is, and
add some extra comments to remote_async.  WDYT?

From 2a73e1b66417edce50d97e566865f2273a988482 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Thu, 19 Nov 2015 18:40:13 +0000
Subject: [PATCH] clear pending events token before acking

---
 gdb/remote.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 9e69bfb..04057cc 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -6640,6 +6640,10 @@ remote_notif_get_pending_events (struct notif_client *nc)
 			    "notif: process: '%s' ack pending event\n",
 			    nc->name);
 
+      /* As we're acking the pending notification, we no longer need
+	 to wake the event loop to do it.  */
+      clear_async_event_handler (rs->notif_state->get_pending_events_token);
+
       /* acknowledge */
       nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
       rs->notif_state->pending_event[nc->id] = NULL;
@@ -13158,15 +13162,11 @@ remote_async (struct target_ops *ops, int enable)
 	 event loop to process them.  */
       if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
 	mark_async_event_handler (remote_async_inferior_event_token);
-      if (target_is_non_stop_p ())
-	mark_async_event_handler (rs->notif_state->get_pending_events_token);
     }
   else
     {
       serial_async (rs->remote_desc, NULL, NULL);
       clear_async_event_handler (remote_async_inferior_event_token);
-      if (target_is_non_stop_p ())
-	clear_async_event_handler (rs->notif_state->get_pending_events_token);
     }
 }
 
-- 
1.9.3


  reply	other threads:[~2015-11-23 15:40 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-14 15:28 [PATCH 00/18] Remote all-stop on top of non-stop Pedro Alves
2015-10-14 15:28 ` [PATCH 18/18] remote: enable "maint set target-non-stop" by default Pedro Alves
2015-10-14 15:28 ` [PATCH 01/18] Fix mi-nonstop.exp with extended-remote Pedro Alves
2015-10-14 15:28 ` [PATCH 02/18] Remote all-stop-on-top-of-non-stop Pedro Alves
2015-10-24 22:39   ` Yao Qi
2015-11-23 15:40     ` Pedro Alves [this message]
2015-11-23 18:39       ` Pedro Alves
2015-11-26 15:53         ` Yao Qi
2015-10-14 15:28 ` [PATCH 15/18] gdbserver:prepare_access_memory: pick another thread Pedro Alves
2015-10-14 15:28 ` [PATCH 03/18] attach + target always in non-stop mode: stop all threads Pedro Alves
2015-10-26 13:22   ` Yao Qi
2015-11-23 18:15     ` Pedro Alves
2015-11-23 18:42       ` Pedro Alves
2015-11-26 16:12       ` Yao Qi
2015-11-26 16:23         ` Pedro Alves
2015-11-27  9:33           ` Yao Qi
2015-10-14 15:28 ` [PATCH 13/18] infrun: Fix TARGET_WAITKIND_NO_RESUMED handling in non-stop mode Pedro Alves
2015-10-14 15:33 ` [PATCH 05/18] remote: stop reason and watchpoint data address per thread Pedro Alves
2015-10-14 15:33 ` [PATCH 10/18] Remote thread create/exit events Pedro Alves
2015-10-14 16:35   ` Eli Zaretskii
2015-10-26 16:50   ` Yao Qi
2015-11-23 15:41     ` Pedro Alves
2015-12-01 15:12   ` Ulrich Weigand
2015-12-01 16:06     ` Pedro Alves
2015-12-01 17:10       ` Ulrich Weigand
2015-10-14 15:36 ` [PATCH 06/18] New vCtrlC packet, non-stop mode equivalent of \003 Pedro Alves
2015-10-26 14:11   ` Yao Qi
2015-11-30 18:25     ` Pedro Alves
2015-10-14 15:36 ` [PATCH 04/18] gdbserver crash running gdb.threads/non-ldr-exc-1.exp Pedro Alves
2015-10-26 13:54   ` Yao Qi
2015-11-24 16:34     ` Pedro Alves
2015-11-26 16:23       ` Yao Qi
2015-11-30 14:53         ` Pedro Alves
2015-10-14 15:36 ` [PATCH 12/18] testsuite: Range stepping and non-stop mode Pedro Alves
2015-10-14 15:36 ` [PATCH 17/18] gdbserver: don't exit until GDB disconnects Pedro Alves
2015-10-14 15:36 ` [PATCH 11/18] gdbserver: fix killed-outside.exp Pedro Alves
2015-10-27 12:02   ` Yao Qi
2015-11-25 15:06     ` Pedro Alves
2015-11-26 16:51       ` Yao Qi
2015-11-26 17:56         ` Pedro Alves
2015-10-14 15:36 ` [PATCH 14/18] Implement TARGET_WAITKIND_NO_RESUMED in the remote protocol Pedro Alves
2015-10-14 16:36   ` Eli Zaretskii
2015-10-19 16:21   ` Yao Qi
2015-10-19 16:48     ` Pedro Alves
2015-10-14 15:37 ` [PATCH 09/18] Make dprintf-non-stop.exp cope with remote testing Pedro Alves
2015-10-14 15:37 ` [PATCH 07/18] gdbserver crash if gdb attaches too fast Pedro Alves
2015-10-14 15:37 ` [PATCH 16/18] gdbserver/linux: Always wake up event loop after resume Pedro Alves
2015-10-26 17:28   ` Yao Qi
2015-11-25 15:31     ` Pedro Alves
2015-10-14 15:38 ` [PATCH 08/18] gdbserver resume_stop handling bug Pedro Alves
2015-10-14 16:37   ` Eli Zaretskii
2015-11-25 15:12     ` Pedro Alves
2015-11-25 17:53       ` Eli Zaretskii
2015-10-15 10:46 ` [PATCH 00/18] Remote all-stop on top of non-stop Pedro Alves
2015-10-16 16:47 ` Yao Qi
2015-10-19 11:48   ` Yao Qi
2015-10-19 15:28     ` Pedro Alves
2015-10-19 15:47       ` Yao Qi
2015-10-27 13:11 ` Yao Qi
2015-11-30 19:59   ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56533372.3030805@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=qiyaoltc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox