From owner-freebsd-current@freebsd.org Fri Jul 15 17:58:42 2016 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D61FCB9AFE7 for ; Fri, 15 Jul 2016 17:58:42 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mail-pf0-x235.google.com (mail-pf0-x235.google.com [IPv6:2607:f8b0:400e:c00::235]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id A0C0617BE for ; Fri, 15 Jul 2016 17:58:42 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: by mail-pf0-x235.google.com with SMTP id t190so43231233pfb.3 for ; Fri, 15 Jul 2016 10:58:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=uLb/bhR3178LJgHHn09LBQRLF0aLwv+u45qom5B8Vs4=; b=W8tf5T9MqXzrjMQlf1zexnN7povZjbzrSLimVUZpdJ0jt8ly5AA/Xu2TE5j49bPaYL RXpQ7fwXKdJMkuFTiOiVuGeJzzYfgvUX7oABuwCzeWog4mt2BLm861n8meuoV3L/AEAa E0v6nJthwFVT2tyrRN23ivZ9bek8PIqRvI/EJPiWWQFNYS7lxPB8FsyA0IX2nkKLj/ZZ X6+QKK7GNwVMO2tGAq5rxtNWF8bPFalq78qpGbzjmrf1e3E7XUa4STtD+Y2lDHQumasS xaZwy72DYdqy0lvJrfexlvhgZSlQmfa3c8KZGilqsAYll7StdOJhtaWGsnucNKfdWFOf UU4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=uLb/bhR3178LJgHHn09LBQRLF0aLwv+u45qom5B8Vs4=; b=O0WnMv5iH6bxZ7jGZaYAdMZ4Z0fBCdXSHugzMYgCpswozfA3Q48ijp5LMz+EwSZ0ym zzWpWENZR/MZesx5qTK5SnMnrK026w6eyATeTcG80R40DFHqoUW3fwYe0Kf60CtGpaxZ 0n/kAwOeZFaGoOKnvUA5ok5Y2TehETtunoignznibKkbQky5sP9qpB3obH1fUcc9rYSg hE+UeryE3OdaK/jlD/TT+u9aCKCKK6vgA/hyzUbZeKJkTar06W2GvZv005EVNX5pUIxe 3bPmxRwnybYfu1O7Jti/ZD3EjKzcswWv6RqFjmVoy+56OlAUg6MuvBeMNEn7GHh3GoKK k4DA== X-Gm-Message-State: ALyK8tK3Yc2HozeFhRRbWmblH48NFIEOqaDfpzqQ7INiFHKBcDaqkAz+Ke0WmHrHbjcqUw== X-Received: by 10.98.32.81 with SMTP id g78mr23776112pfg.20.1468605522160; Fri, 15 Jul 2016 10:58:42 -0700 (PDT) Received: from wkstn-mjohnston.west.isilon.com (c-76-104-201-218.hsd1.wa.comcast.net. [76.104.201.218]) by smtp.gmail.com with ESMTPSA id s23sm4429443pfd.23.2016.07.15.10.58.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jul 2016 10:58:41 -0700 (PDT) Sender: Mark Johnston Date: Fri, 15 Jul 2016 11:01:59 -0700 From: Mark Johnston To: Konstantin Belousov Cc: freebsd-current@FreeBSD.org Subject: Re: ptrace attach in multi-threaded processes Message-ID: <20160715180159.GA4487@wkstn-mjohnston.west.isilon.com> References: <20160712182414.GC71220@wkstn-mjohnston.west.isilon.com> <20160713033036.GR38613@kib.kiev.ua> <20160713040210.GA89573@wkstn-mjohnston.west.isilon.com> <20160713045439.GT38613@kib.kiev.ua> <20160713164247.GA2066@wkstn-mjohnston.west.isilon.com> <20160713191947.GW38613@kib.kiev.ua> <20160713200139.GC2066@wkstn-mjohnston.west.isilon.com> <20160714052537.GZ38613@kib.kiev.ua> <20160714181605.GA17310@wkstn-mjohnston.west.isilon.com> <20160715072720.GB38613@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160715072720.GB38613@kib.kiev.ua> User-Agent: Mutt/1.6.1 (2016-04-27) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jul 2016 17:58:42 -0000 On Fri, Jul 15, 2016 at 10:27:20AM +0300, Konstantin Belousov wrote: > On Thu, Jul 14, 2016 at 11:16:05AM -0700, Mark Johnston wrote: > > Please see the program here: > > https://people.freebsd.org/~markj/ptrace_stop.c > > > > It cheats a bit: it uses SIGSTOP to stop the child before sending a > > SIGHUP to it. However, this is just for convenience; note that PT_ATTACH > > will result in a call to thread_unsuspend() on the child, so PT_ATTACH's > > SIGSTOP will be delivered to a running process. When ptrace attaches, > > the child stops and WSTOPSIG(status) == SIGHUP. When ptrace detaches, > > the child is left stopped. > No, it is not for convenience, it relies on another bug to get the effect, > see below. I see. I should have noted that the result can be reproduced without the first SIGSTOP, just not reliably. That is, I still occasionally get the following output when the kill(SIGSTOP) and subsequent waitpid() call are removed: stopping signal is 1 waiting on child... child is stopped after detach (sig 17) > > As I understand you intent, you prefer to get SIGSTOP from the first > waitpid(2) call after successful PT_ATTACH, am I right ? Hm, I don't care very much about that. I was just addressing your claim that the "debugger interface guarantees that SIGSTOP is noted." > At least for > single-threaded case, this can be achieved with a flag indicating that > we a doing first cursig(9) action after the attach, and preferring > SIGSTOP over any other queued signal. The new flag P2_PTRACE_FSTP > does just that. For mt case, I believe that some enchancements to > my proc_next_xthread() would fix that. This seems like a sound approach to me. It provides the guarantee I referenced above, and ensures that the SIGSTOP from PT_ATTACH is delivered before PT_DETACH. > > But when debugging the code, I found that it still does not work reliably > for your test. The reason is that issignal() consumes a queued stop signal > after the thread_suspend_switch(). It allows the attach to occur, but then > sigqueue_delete() calls ('take the signal!') eat the signal for attach. It > seems that we should consume stops before going to stop state. An open > question is how much this hurts when another (non-debugging) SIGSTOP is > queued while in stopped state. > > Please try this. Thanks, this seems to give the desired behaviour in the single-threaded case. I'll write a test case for the multi-threaded case next. Am I correct in thinking that r302179 could be reverted if your change is committed?