public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Simon Marchi <simon.marchi@polymtl.ca>, gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: Re: [PATCH 7/8] gdbserver: report correct status in thread stop race condition
Date: Thu, 31 Mar 2022 19:21:58 +0100	[thread overview]
Message-ID: <db7c85d5-59b6-ce0f-0563-1c56f5af7f4a@palves.net> (raw)
In-Reply-To: <20220117162742.524350-8-simon.marchi@polymtl.ca>

On 2022-01-17 16:27, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> The test introduced by the following patch would sometimes fail in this
> configuration:
> 
>     FAIL: gdb.threads/next-fork-other-thread.exp: fork_func=vfork: target-non-stop=on: non-stop=off: displaced-stepping=auto: i=14: next to for loop
> 
> The test has multiple threads constantly forking or vforking while the
> main thread keep doing "next"s.
> 
> (After writing the commit message, I realized this also fixes a similar
> failure in gdb.threads/forking-threads-plus-breakpoint.exp with the
> native-gdbserver and native-extended-gdbserver boards.)
> 
> As stop_all_threads is called, because the main thread finished its
> "next", it inevitably happens at some point that we ask the remote
> target to stop a thread and wait() reports that this thread stopped with
> a fork or vfork event, instead of the SIGSTOP we sent to try to stop it.
> 
> While running this test, I attached to GDBserver and stopped at
> linux-low.cc:3626.  We can see that the status pulled from the kernel
> for 2742805 is indeed a vfork event:
> 
>     (gdb) p/x w
>     $3 = 0x2057f
>     (gdb) p WIFSTOPPED(w)
>     $4 = true
>     (gdb) p WSTOPSIG(w)
>     $5 = 5
>     (gdb) p/x (w >> 8) & (PTRACE_EVENT_VFORK << 8)
>     $6 = 0x200
> 
> However, the statement at line 3626 overrides that:
> 
>     ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
> 
> OURSTATUS becomes "stopped by a SIGTRAP".  The information about the
> fork or vfork is lost.
> 
> It's then all downhill from there, stop_all_threads eventually asks for
> a thread list update.  That thread list includes the child of that
> forgotten fork or vfork, the remote target goes "oh cool, a new process,
> let's attach to it!", when in fact that vfork child's destiny was to be
> detached.
> 
> My reverse-engineered understanding of the code around there is that the
> if/else between lines 3562 and 3583 (in the original code) makes sure
> OURSTATUS is always initialized (not "ignore").  Either the details are
> already in event_child->waitstatus (in the case of fork/vfork, for
> example), in which case we just copy event_child->waitstatus to
> ourstatus.  Or, if the event is a plain "stopped by a signal" or a
> syscall event, OURSTATUS is set to "stopped", but without a signal
> number.  Lines 3601 to 3629 (in the original code) serve to fill in that
> last bit of information.
> 
> The problem is that when `w` holds the vfork status, the code wrongfully
> takes this branch, because WSTOPSIG(w) returns SIGTRAP:
> 
>   else if (current_thread->last_resume_kind == resume_stop
>        && WSTOPSIG (w) != SIGSTOP)
> 
> The intent of this branch is, for example, when we sent SIGSTOP to try
> to stop a thread, but wait() reports that it stopped with another signal
> (that it must have received from somewhere else simultaneously), say
> SIGWINCH.  In that case, we want to report the SIGWINCH.  But in our
> fork/vfork case, we don't want to take this branch, as the thread didn't
> really stop because it received a signal.  For the non "stopped by a
> signal" and non "syscall signal" cases, we would ideally skip over all
> that snippet that fills in the signal or syscall number.
> 
> The fix I propose is to move this snipppet of the else branch of the
> if/else above.  In addition to moving the code, the last two "else if"
> branches:
> 
>   else if (current_thread->last_resume_kind == resume_stop
> 	   && WSTOPSIG (w) != SIGSTOP)
>     {
>       /* A thread that has been requested to stop by GDB with vCont;t,
> 	 but, it stopped for other reasons.  */
>       ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
>     }
>   else if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
>     ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
> 
> are changed into a single else:
> 
>   else
>     ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
> 
> This is the default path we take if:
> 
>  - W is not a syscall status
>  - W does not represent a SIGSTOP that have sent to stop the thread and
>    therefore want to suppress it
> 
> Change-Id: If2dc1f0537a549c293f7fa3c53efd00e3e194e79

This is OK.  Thanks.

  reply	other threads:[~2022-03-31 18:22 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-17 16:27 [PATCH 0/8] Some fixes for handling vfork by multi-threaded programs Simon Marchi
2022-01-17 16:27 ` [PATCH 1/8] gdb/infrun: add reason parameter to stop_all_threads Simon Marchi
2022-03-31 15:05   ` Pedro Alves
2022-03-31 15:35     ` Simon Marchi
2022-01-17 16:27 ` [PATCH 2/8] gdb/linux-nat: remove check based on current_inferior in linux_handle_extended_wait Simon Marchi
2022-03-31 16:12   ` Pedro Alves
2022-03-31 17:06     ` Simon Marchi
2022-01-17 16:27 ` [PATCH 3/8] gdb: replace inferior::waiting_for_vfork_done with inferior::thread_waiting_for_vfork_done Simon Marchi
2022-03-31 18:17   ` Pedro Alves
2022-04-01 14:25     ` Simon Marchi
2022-01-17 16:27 ` [PATCH 4/8] gdb/infrun: add inferior parameters to stop_all_threads and restart_threads Simon Marchi
2022-03-31 18:17   ` Pedro Alves
2022-01-17 16:27 ` [PATCH 5/8] gdb/infrun: add logging statement to do_target_resume Simon Marchi
2022-03-31 18:18   ` Pedro Alves
2022-01-17 16:27 ` [PATCH 6/8] gdb: fix handling of vfork by multi-threaded program (follow-fork-mode=parent, detach-on-fork=on) Simon Marchi
2022-03-31 18:21   ` Pedro Alves
2022-04-01 17:28     ` Simon Marchi
2022-01-17 16:27 ` [PATCH 7/8] gdbserver: report correct status in thread stop race condition Simon Marchi
2022-03-31 18:21   ` Pedro Alves [this message]
2022-01-17 16:27 ` [PATCH 8/8] gdb: resume ongoing step after handling fork or vfork Simon Marchi
2022-03-31 18:22   ` Pedro Alves
2022-03-31 18:28   ` Pedro Alves
2022-04-01 18:42     ` Simon Marchi

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=db7c85d5-59b6-ce0f-0563-1c56f5af7f4a@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    --cc=simon.marchi@polymtl.ca \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).