From owner-freebsd-hackers@FreeBSD.ORG Thu Apr 29 21:37:39 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 E465E106564A for ; Thu, 29 Apr 2010 21:37:39 +0000 (UTC) (envelope-from yanefbsd@gmail.com) Received: from mail-pw0-f54.google.com (mail-pw0-f54.google.com [209.85.160.54]) by mx1.freebsd.org (Postfix) with ESMTP id B87498FC19 for ; Thu, 29 Apr 2010 21:37:39 +0000 (UTC) Received: by pwi9 with SMTP id 9so11606429pwi.13 for ; Thu, 29 Apr 2010 14:37:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=S7NDWkTUGpSZE0mvo0YfD51nh6/CxcqI0glnam59J/U=; b=dcIK9rw/toqDgL4TbOOVAkNwh2HD2WCAu8nbnPEo6JKqTgMfA+DFkRhMpPQa9LxjS1 4GDBSpvM2rl4imzSGVSeGaEbbpIveVkzSW3aAITiLfmgfzJJzWNOFWRLzRx8QClJvoQv m0N3pSIkRfb42c0jXEDBzGCdN72Rj+0akPCPk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=bJZTP5WjViaXLwc+00fZSk47+zu3TFzblyOW6xL64ZxztiPMVhK3FUrhdj+P6OaPcc XEz3DkfRGRNPJJEnHSqoT57KJXCRGZ2qSCyB7RvNXijwqtpO5SCh4+YonG1UsnvdW0uG nhkaHfIUA3bsln6iig1m5HqkUIM08m25XGpng= MIME-Version: 1.0 Received: by 10.142.1.29 with SMTP id 29mr5864730wfa.337.1272577056855; Thu, 29 Apr 2010 14:37:36 -0700 (PDT) Received: by 10.142.69.2 with HTTP; Thu, 29 Apr 2010 14:37:36 -0700 (PDT) In-Reply-To: References: Date: Thu, 29 Apr 2010 14:37:36 -0700 Message-ID: From: Garrett Cooper To: Gunnar Hinriksson Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org 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 21:37:40 -0000 On Thu, Apr 29, 2010 at 12:06 PM, Gunnar Hinriksson wro= te: > Hello > > 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. > > #include > #include > #include > #include > #include > > int main(int argc, char *argv[]) > { > =A0 =A0 =A0 =A0int pid =3D atoi(argv[1]); > =A0 =A0 =A0 =A0ptrace(PT_ATTACH, pid, 0, 0); > =A0 =A0 =A0 =A0wait(NULL); > =A0 =A0 =A0 =A0ptrace(PT_DETACH, pid, 0, 0); > =A0 =A0 =A0 =A0return 0; > } > > Am I using ptrace incorrectly or is there perhaps a bug in ptrace that > causes the child to always segfault ? Nope -- it's a bug in your code. From ptrace(2): PT_CONTINUE The traced process continues execution. The addr argume= nt 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 wher= e it left off. The data argument provides a signal number= to be delivered to the traced process as it resumes executi= on, or 0 if no signal is to be sent. [...] PT_DETACH This request is like PT_CONTINUE, except that it does no= t allow specifying an alternate place to continue executio= n, and after it succeeds, the traced process is no longer traced and continues execution normally. 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. HTH, -Garrett