From owner-freebsd-current@FreeBSD.ORG Fri May 7 21:21:34 2010 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 90273106566C for ; Fri, 7 May 2010 21:21:34 +0000 (UTC) (envelope-from widawsky@gmail.com) Received: from mail-qy0-f181.google.com (mail-qy0-f181.google.com [209.85.221.181]) by mx1.freebsd.org (Postfix) with ESMTP id 4D8078FC08 for ; Fri, 7 May 2010 21:21:33 +0000 (UTC) Received: by qyk11 with SMTP id 11so2178577qyk.13 for ; Fri, 07 May 2010 14:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:content-type; bh=l4NjgXuC0iHCzImyMQD0d8VMxCXdDjSMEWY3w/CjqOM=; b=tHNtjguf/V0MxRSAWKZfVxXnhoq/QiiTim08kwP3CwyAVPoRqX3lNLWQRFhTQ02u47 gP1+2vpJlWB6oK9Y3otCTk/VYf0McDMRl+i3kTawx0/zy/QqOgitSzgMS6bZKrevg9lH pp+APRvbbYFcu2eSA3+zGzUOMn7B7EeCpAiIY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=uzY0QwHwKiyRyQdkv/VC4I4EgH24ls85OvrW9r+goIx9AauSAxRq8Hy2Lyjf1bT0ln 7lJUIjqeuJZ+sUTNXOMWAbFXtHiCFDM3Lya9WZn77UH5zAgEpqH5QIXzw22UqKmEipV+ O0arRq9v7djqVnG/MLoiE4lZmI4+GYGmANNGk= MIME-Version: 1.0 Received: by 10.224.107.144 with SMTP id b16mr307618qap.215.1273265535418; Fri, 07 May 2010 13:52:15 -0700 (PDT) Received: by 10.224.36.196 with HTTP; Fri, 7 May 2010 13:52:15 -0700 (PDT) Date: Fri, 7 May 2010 13:52:15 -0700 Message-ID: From: Ben Widawsky To: freebsd-current@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 Subject: PT_ATTACH resumes suspended process X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 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, 07 May 2010 21:21:34 -0000 If a debugger attaches to a suspended process, the process will be resumed, and backgrounded. This seems like the incorrect behavior to me based what I read in the man page. "The tracing process will see the newly-traced process stop and may then control it as if it had been traced all along." The behavior exhibited in FreeBSD is that the process is resumed, and we will not reach ptracestop() until the next debugger command comes in. The exact code in question I believe is a combination of kern_ptrace() and issignal(). When a PT_ATTACH comes in, ptrace code will unsuspend the process and set xsig=SIGSTOP of the thread picked to communicate with the debugger (which by the way should be the same as the thread chosen to deliver the SIGSTOP earlier, and I see no guarantee of this either but I may be missing something). The thread will resume in issignal, and may not have any signals pending, so issignal will return 0. The result here is every thread gets unsuspended until the debugger explicitly suspends. There is even a comment in kern_ptrace() for which I see no action: /* deliver or queue signal */ I've created a quick hack to enable debugging to work how I think it should. Essentially the change is as follows, there are a couple other bits as well; line 2524 kern_sig.c, in issignal(): if (traced && !sig) { /* * see if we were given a signal by sendsig in kern_ptrace() */ sig = td->td_xsig; } You can reproduce this with a simple app that spins forever doing something. In one shell, run the app and from another shell send a SIGSTOP and attach with gdb. I've only tried this on FreeBSD 8.0-RELEASE, but judging by the code it seems like it would still happen in HEAD. :::SHELL1::: [bwidawsk@bwfbsd ~/workspace/C/debugg]$ ./a.out 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [1]+ Stopped ./a.out [bwidawsk@bwfbsd ~/workspace/C/debugg]$ 21 22 :::SHELL2::: [bwidawsk@bwfbsd ~/workspace/C/debugg]$ kill -SIGSTOP 4134 [bwidawsk@bwfbsd ~/workspace/C/debugg]$ gdb a.out 4134 GNU gdb 6.1.1 [FreeBSD] Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "amd64-marcel-freebsd"... Attaching to program: /usr/home/bwidawsk/workspace/C/debugg/a.out, process 4134