From owner-freebsd-hackers@FreeBSD.ORG Thu Apr 29 22:23:05 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id A9A7D1065670 for ; Thu, 29 Apr 2010 22:23:05 +0000 (UTC) (envelope-from rb@gid.co.uk) Received: from mx0.gid.co.uk (mx0.gid.co.uk [194.32.164.250]) by mx1.freebsd.org (Postfix) with ESMTP id 30F8B8FC12 for ; Thu, 29 Apr 2010 22:23:04 +0000 (UTC) Received: from gidgate.gid.co.uk (80-46-130-69.static.dsl.as9105.com [80.46.130.69]) by mx0.gid.co.uk (8.14.2/8.14.2) with ESMTP id o3TMIXkP090722; Thu, 29 Apr 2010 23:18:33 +0100 (BST) (envelope-from rb@gid.co.uk) Received: from rbpbp.gid.co.uk ([194.32.164.6]) by gidgate.gid.co.uk (8.13.8/8.13.8) with ESMTP id o3TMIS4D069913; Thu, 29 Apr 2010 23:18:28 +0100 (BST) (envelope-from rb@gid.co.uk) Mime-Version: 1.0 (Apple Message framework v1078) Content-Type: text/plain; charset=us-ascii From: Bob Bishop In-Reply-To: Date: Thu, 29 Apr 2010 23:18:27 +0100 Content-Transfer-Encoding: quoted-printable Message-Id: <9EF83DA6-B2D3-456E-B0AF-0B4F5F458A1F@gid.co.uk> References: To: Garrett Cooper X-Mailer: Apple Mail (2.1078) Cc: freebsd-hackers@freebsd.org, Gunnar Hinriksson Subject: Re: Ptrace segfault X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Apr 2010 22:23:05 -0000 Hi, On 29 Apr 2010, at 22:37, Garrett Cooper wrote: > On Thu, Apr 29, 2010 at 12:06 PM, Gunnar Hinriksson = wrote: >> Hello >>=20 >> Im having a little problem using ptrace on my system. >> If I use ptrace to attach to another process the child process >> segfaults once I detach. >> For example using this simple program. >>=20 >> #include >> #include >> #include >> #include >> #include >>=20 >> int main(int argc, char *argv[]) >> { >> int pid =3D atoi(argv[1]); >> ptrace(PT_ATTACH, pid, 0, 0); >> wait(NULL); >> ptrace(PT_DETACH, pid, 0, 0); >> return 0; >> } >>=20 >> Am I using ptrace incorrectly or is there perhaps a bug in ptrace = that >> causes the child to always segfault ? >=20 > Nope -- it's a bug in your code. =46rom ptrace(2): >=20 > PT_CONTINUE The traced process continues execution. The addr = argument > is an address specifying the place where execution = is to be > resumed (a new value for the program counter), or > (caddr_t)1 to indicate that execution is to pick up = where > it left off. The data argument provides a signal = number to > be delivered to the traced process as it resumes = execution, > or 0 if no signal is to be sent. >=20 > [...] >=20 > PT_DETACH This request is like PT_CONTINUE, except that it = does not = ^^^^^^^^^^^ > allow specifying an alternate place to continue = execution, = ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > and after it succeeds, the traced process is no = longer > traced and continues execution normally. >=20 > Note very carefully the fact that PT_DETACH is like PT_CONTINUE, > and that PT_CONTINUE says that addr references the memory where the > execution is going to be resumed. Looks to me like a bug in ptrace(PT_DETACH,...) which to agree with = ptrace(2) ought either to (a) fail (EINVAL?) if addr !=3D (caddr_t)1, or (b) ignore addr entirely; it's not clear which. OP inferred (b) which is reasonable. > HTH, > -Garrett > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to = "freebsd-hackers-unsubscribe@freebsd.org" >=20 >=20 -- Bob Bishop +44 (0)118 940 1243 rb@gid.co.uk fax +44 (0)118 940 1295 mobile +44 (0)783 626 4518