From patchwork Mon May 12 23:34:15 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Don Breazeal X-Patchwork-Id: 878 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx20.g.dreamhost.com (mx2.sub5.homie.mail.dreamhost.com [208.113.200.128]) by wilcox.dreamhost.com (Postfix) with ESMTP id 772D13600C0 for ; Mon, 12 May 2014 16:34:32 -0700 (PDT) Received: by homiemail-mx20.g.dreamhost.com (Postfix, from userid 14314964) id 2BDF0419D1D7C; Mon, 12 May 2014 16:34:32 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx20.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx20.g.dreamhost.com (Postfix) with ESMTPS id 0C878419EC100 for ; Mon, 12 May 2014 16:34:32 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id :mime-version:content-type; q=dns; s=default; b=TtxD8uDA/PECymoK nO3bbI+HgS4B5PtLohd3eZabtfXuFAZZdECfwwQD4lBcmTU/JTx4e1hN8sXTyQ8W QNm7/UG89JAgzYQPshWuUqQ0BMV+/c92D+rLDKUxODBjwaFBY4R4uVBX7pwgLuLi 7Y8LRNUALqoH6UiY4poVmfpdfXY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id :mime-version:content-type; s=default; bh=HlgyWFkhfRMzDDEtW96gbw ezjh4=; b=crSKpGSP0tmRjUBOMoNvRzSo+k2oDehkg8tMzhG8lQd7As7ZNJXPOM zEbYzOTnsyTrheYpHpHY/jnBx0JXJsMcINnNQCGkP3Toe/SEhJrOSGJHq1dXDhPk nFaHVqcKwgsetoUlRq+gA6Phryl9QXgXjaLQIqZtRx4zj1pJR6zS4= Received: (qmail 29284 invoked by alias); 12 May 2014 23:34:29 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 29272 invoked by uid 89); 12 May 2014 23:34:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=2.0 required=5.0 tests=UNSUBSCRIBE_BODY autolearn=no version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 12 May 2014 23:34:28 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1Wjzjl-0004kN-82 from donb@codesourcery.com for gdb-patches@sourceware.org; Mon, 12 May 2014 16:34:25 -0700 Received: from SVR-ORW-FEM-02.mgc.mentorg.com ([147.34.96.206]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Mon, 12 May 2014 16:34:25 -0700 Received: from build2-lucid-cs (147.34.91.1) by svr-orw-fem-02.mgc.mentorg.com (147.34.96.168) with Microsoft SMTP Server id 14.2.247.3; Mon, 12 May 2014 16:34:24 -0700 Received: by build2-lucid-cs (Postfix, from userid 1905) id 5E83E3A00CC; Mon, 12 May 2014 16:34:24 -0700 (PDT) From: To: CC: Don Breazeal Subject: [PATCH] Fix for gdb/PR 14808, vfork/exec inferior problem Date: Mon, 12 May 2014 16:34:15 -0700 Message-ID: <1399937655-29577-1-git-send-email-donb@codesourcery.com> MIME-Version: 1.0 X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in From: Don Breazeal Hi, This patch is a fix for gdb/PR14808: "vfork, follow-fork child, detach-on-fork on, child execs, parent changes executable too (but should not)". The problem was that after a vfork the parent and child inferiors shared their pspace members (program space), reflecting the reality of the processes' shared address space. When the child exec'd, and the parent was detached, the pspace continued to be shared. The executable file name is stored in the pspace, so when the child's exec file name was updated after the exec, the parent's was also updated. The solution was to create a separate program space for the parent, so that its exec file name remains intact. This patch also includes a test for this scenario. thanks --Don gdb/ 2014-05-12 Don Breazeal * infrun.c (handle_vfork_child_exec_or_exit): For the case of a vfork where we follow the child and detach the parent, and the child execs, create a new pspace and aspace for the parent inferior. gdb/testsuite 2014-05-12 Don Breazeal * gdb.base/foll-vfork.exp (vfork_relations_in_info_inferiors): Test that after a vfork and child exec, the parent's exec file name has not been changed. --- gdb/infrun.c | 42 ++++++++++++++++++++++----------- gdb/testsuite/gdb.base/foll-vfork.exp | 11 ++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/gdb/infrun.c b/gdb/infrun.c index ab39b6e..82a67d5 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -649,6 +649,7 @@ handle_vfork_child_exec_or_exit (int exec) struct cleanup *old_chain; struct program_space *pspace; struct address_space *aspace; + struct inferior *parent_inf; /* follow-fork child, detach-on-fork on. */ @@ -665,27 +666,39 @@ handle_vfork_child_exec_or_exit (int exec) else old_chain = save_current_space_and_thread (); - /* We're letting loose of the parent. */ + /* Make the parent the current inferior for target_detach. */ tp = any_live_thread_of_process (inf->vfork_parent->pid); switch_to_thread (tp->ptid); - /* We're about to detach from the parent, which implicitly - removes breakpoints from its address space. There's a - catch here: we want to reuse the spaces for the child, - but, parent/child are still sharing the pspace at this - point, although the exec in reality makes the kernel give - the child a fresh set of new pages. The problem here is - that the breakpoints module being unaware of this, would - likely chose the child process to write to the parent - address space. Swapping the child temporarily away from - the spaces has the desired effect. Yes, this is "sort - of" a hack. */ - + /* The child inferior INF may be dead, so avoid giving the + breakpoints module the option to write through to it + by swapping the child temporarily away from the spaces + (cloning a program space resets breakpoints). */ pspace = inf->pspace; aspace = inf->aspace; inf->aspace = NULL; inf->pspace = NULL; + if (exec) + { + /* The parent and child inferiors have been sharing + program and address space structures from the point + where the parent called vfork. Now that the child has + called exec and we are detaching from the parent, the + parent inferior needs to have its own pspace and aspace + so that changes in the child don't affect it. We have + to give the new spaces to the parent since we saved the + child's spaces as the current spaces above. Even though + we are detaching the parent, we want to keep the + corresponding entry in the inferiors list intact. */ + parent_inf = current_inferior (); + parent_inf->aspace = new_address_space (); + parent_inf->pspace = add_program_space (parent_inf->aspace); + parent_inf->removable = inf->removable; + set_current_program_space (parent_inf->pspace); + clone_program_space (parent_inf->pspace, pspace); + } + if (debug_infrun || info_verbose) { target_terminal_ours (); @@ -702,9 +715,10 @@ handle_vfork_child_exec_or_exit (int exec) inf->vfork_parent->pid); } + /* Detach the parent. */ target_detach (NULL, 0); - /* Put it back. */ + /* Put the child spaces back. */ inf->pspace = pspace; inf->aspace = aspace; diff --git a/gdb/testsuite/gdb.base/foll-vfork.exp b/gdb/testsuite/gdb.base/foll-vfork.exp index fe3663c..e9b0110 100644 --- a/gdb/testsuite/gdb.base/foll-vfork.exp +++ b/gdb/testsuite/gdb.base/foll-vfork.exp @@ -442,6 +442,17 @@ proc vfork_relations_in_info_inferiors { variant } { pass $test } } + + # Make sure the exec file name of the vfork parent is not + # changed when the child's is changed. + if { $variant == "exec" } { + set test "exec file name change" + gdb_test_multiple "info inferiors" $test { + -re " 2 .*vforked-prog.* 1 .*foll-vfork.*$gdb_prompt " { + pass $test + } + } + } }} proc do_vfork_and_follow_parent_tests {} {