Honour SIGILL and SIGSEGV in cancel breakpoint

Message ID 871tr2ybg0.fsf@codesourcery.com
State New, archived
Headers

Commit Message

Yao Qi Sept. 23, 2014, 8:42 a.m. UTC
  Pedro Alves <palves@redhat.com> writes:

>> count_events_callback and select_event_lwp_callback in GDBServer need to
>> honour SIGILL and SIGSEGV too.  I write a patch to call
>> lp_status_is_sigtrap_like_event in them, but regression test result
>> isn't changed, which is a surprise to me.  I thought some fails should
>> be fixed.  I'll look into it deeply.
>
> Maybe you're getting lucky with scheduling.
> pthreads.exp and schedlock.exp I think are the most sensitive to this.

I run them ten times, the results aren't changed.

>
> See:
>  https://www.sourceware.org/ml/gdb-patches/2001-06/msg00250.html

Randomly selecting event lwp was added in the url above you gave, to
prevent the starvation of threads.  However, in my configuration
(arm-linux with SIGILL), event lwp selection does nothing, but no test
fails are caused.  GDBserver processes events like this:

 1. When GDBServer gets a breakpoint event from waitpid (-1, ),
 2. GDBserver will stop_all_lwps, in which wait_for_sigstop will drain
 all pending reports from kernel.
 3. GDBserver selects one lwp and cancels the breakpoint on the rest.  If
 event lwp selection does nothing, it is the lwp GDBserver gets in step 1.
 4. GDBserver steps over the breakpoint, and resumes all the threads.
 Go back to step 1, wait until any threads hit breakpoint,

As we can see, if waitpid (-1, ) (in step #1) returns event lwp randomly,
we don't have to randomly select event lwp again in step #3.  IMO, it is
naturally random that one thread hits the breakpoint first in a
multi-thread program.  That is the reason why no test fails are caused
without event lwp selection in my experiments.  IOW, on the platform
that waitpid (-1, ) returns event lwp randomly, we don't need such lwp
random selection at all.  However, if waitpid kernel implementation
always iterate over a list children in the fixed order, it is possible
that event of the lwp in the front of the list is reported and the rest
lwps may be starved.  In this case, we still have to reply on random
selection inside GDB/GDBserver to avoid starvation.

The patch below is updated to call lp_status_maybe_breakpoint in both
breakpoint cancellation and event lwp selection.
  

Comments

Pedro Alves Sept. 23, 2014, 9:58 a.m. UTC | #1
On 09/23/2014 09:42 AM, Yao Qi wrote:
> Pedro Alves <palves@redhat.com> writes:
> 
>>> count_events_callback and select_event_lwp_callback in GDBServer need to
>>> honour SIGILL and SIGSEGV too.  I write a patch to call
>>> lp_status_is_sigtrap_like_event in them, but regression test result
>>> isn't changed, which is a surprise to me.  I thought some fails should
>>> be fixed.  I'll look into it deeply.
>>
>> Maybe you're getting lucky with scheduling.
>> pthreads.exp and schedlock.exp I think are the most sensitive to this.
> 
> I run them ten times, the results aren't changed.
> 
>>
>> See:
>>  https://www.sourceware.org/ml/gdb-patches/2001-06/msg00250.html
> 
> Randomly selecting event lwp was added in the url above you gave, to
> prevent the starvation of threads.  However, in my configuration
> (arm-linux with SIGILL), event lwp selection does nothing, but no test
> fails are caused.  GDBserver processes events like this:
> 
>  1. When GDBServer gets a breakpoint event from waitpid (-1, ),
>  2. GDBserver will stop_all_lwps, in which wait_for_sigstop will drain
>  all pending reports from kernel.
>  3. GDBserver selects one lwp and cancels the breakpoint on the rest.  If
>  event lwp selection does nothing, it is the lwp GDBserver gets in step 1.
>  4. GDBserver steps over the breakpoint, and resumes all the threads.
>  Go back to step 1, wait until any threads hit breakpoint,
> 
> As we can see, if waitpid (-1, ) (in step #1) returns event lwp randomly,
> we don't have to randomly select event lwp again in step #3.  IMO, it is
> naturally random that one thread hits the breakpoint first in a
> multi-thread program.

Depends on scheduling.  When the program is resumed, the thread that had
last hit the breakpoint may manage to be scheduled before other threads
manage to be scheduled and hit a breakpoint themselves.

> That is the reason why no test fails are caused
> without event lwp selection in my experiments.  IOW, on the platform
> that waitpid (-1, ) returns event lwp randomly, 
> we don't need such lwp
> random selection at all.  However, if waitpid kernel implementation
> always iterate over a list children in the fixed order, it is possible
> that event of the lwp in the front of the list is reported and the rest
> lwps may be starved.  In this case, we still have to reply on random
> selection inside GDB/GDBserver to avoid starvation.

I'm looking at kernel/exit.c on the Linux kernel's sources I have handy
(14186fea0cb06bc43181ce239efe0df6f1af260a), specifically at
do_wait() / do_wait_thread() / ptrace_do_wait() and it seems to me
that waitpid always walks the task list in the same order:

	set_current_state(TASK_INTERRUPTIBLE);
	read_lock(&tasklist_lock);
	tsk = current;
	do {
		retval = do_wait_thread(wo, tsk);
		if (retval)
			goto end;

		retval = ptrace_do_wait(wo, tsk);
		if (retval)
			goto end;

		if (wo->wo_flags & __WNOTHREAD)
			break;
	} while_each_thread(current, tsk);
	read_unlock(&tasklist_lock);

So seems like it's still like Michael said back then: "If more than one
LWP is currently stopped at a breakpoint, the highest-numbered one
will be returned.", and it's likely you're being lucky with scheduling.
E.g., multi-core vs single-core, or the scheduling algorithms in the
kernel improved and are masking the issue.  Or, simply the tests
don't really exercise the starvation issue properly.

Anyway,

> The patch below is updated to call lp_status_maybe_breakpoint in both
> breakpoint cancellation and event lwp selection.

This patch is OK.

Thanks,
Pedro Alves
  
Yao Qi Sept. 23, 2014, 12:51 p.m. UTC | #2
Pedro Alves <palves@redhat.com> writes:

> This patch is OK.

Thanks for the review.  Patch is pushed in.
  

Patch

diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 705edde..2f860e9 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -1739,6 +1739,20 @@  cancel_breakpoint (struct lwp_info *lwp)
   return 0;
 }
 
+/* Return true if the event in LP may be caused by breakpoint.  */
+
+static int
+lp_status_maybe_breakpoint (struct lwp_info *lp)
+{
+  return (lp->status_pending_p
+	  && WIFSTOPPED (lp->status_pending)
+	  && (WSTOPSIG (lp->status_pending) == SIGTRAP
+	      /* SIGILL and SIGSEGV are also treated as traps in case a
+		 breakpoint is inserted at the current PC.  */
+	      || WSTOPSIG (lp->status_pending) == SIGILL
+	      || WSTOPSIG (lp->status_pending) == SIGSEGV));
+}
+
 /* Do low-level handling of the event, and check if we should go on
    and pass it to caller code.  Return the affected lwp if we are, or
    NULL otherwise.  */
@@ -1936,7 +1950,7 @@  linux_low_filter_event (ptid_t filter_ptid, int lwpid, int wstat)
 		 the core before this one is handled.  All-stop always
 		 cancels breakpoint hits in all threads.  */
 	      if (non_stop
-		  && WSTOPSIG (wstat) == SIGTRAP
+		  && lp_status_maybe_breakpoint (child)
 		  && cancel_breakpoint (child))
 		{
 		  /* Throw away the SIGTRAP.  */
@@ -2197,9 +2211,7 @@  count_events_callback (struct inferior_list_entry *entry, void *data)
      should be reported to GDB.  */
   if (thread->last_status.kind == TARGET_WAITKIND_IGNORE
       && thread->last_resume_kind != resume_stop
-      && lp->status_pending_p
-      && WIFSTOPPED (lp->status_pending)
-      && WSTOPSIG (lp->status_pending) == SIGTRAP
+      && lp_status_maybe_breakpoint (lp)
       && !breakpoint_inserted_here (lp->stop_pc))
     (*count)++;
 
@@ -2237,9 +2249,7 @@  select_event_lwp_callback (struct inferior_list_entry *entry, void *data)
   /* Select only resumed LWPs that have a SIGTRAP event pending. */
   if (thread->last_resume_kind != resume_stop
       && thread->last_status.kind == TARGET_WAITKIND_IGNORE
-      && lp->status_pending_p
-      && WIFSTOPPED (lp->status_pending)
-      && WSTOPSIG (lp->status_pending) == SIGTRAP
+      && lp_status_maybe_breakpoint (lp)
       && !breakpoint_inserted_here (lp->stop_pc))
     if ((*selector)-- == 0)
       return 1;
@@ -2271,9 +2281,7 @@  cancel_breakpoints_callback (struct inferior_list_entry *entry, void *data)
 
   if (thread->last_resume_kind != resume_stop
       && thread->last_status.kind == TARGET_WAITKIND_IGNORE
-      && lp->status_pending_p
-      && WIFSTOPPED (lp->status_pending)
-      && WSTOPSIG (lp->status_pending) == SIGTRAP
+      && lp_status_maybe_breakpoint (lp)
       && !lp->stepping
       && !lp->stopped_by_watchpoint
       && cancel_breakpoint (lp))