From patchwork Mon Mar 6 16:00:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Arnez X-Patchwork-Id: 19436 Received: (qmail 99230 invoked by alias); 6 Mar 2017 16:03:12 -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 99217 invoked by uid 89); 6 Mar 2017 16:03:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy= X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 06 Mar 2017 16:03:10 +0000 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v26FrciU108742 for ; Mon, 6 Mar 2017 11:03:10 -0500 Received: from e06smtp06.uk.ibm.com (e06smtp06.uk.ibm.com [195.75.94.102]) by mx0a-001b2d01.pphosted.com with ESMTP id 290u3j7ttp-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 06 Mar 2017 11:03:09 -0500 Received: from localhost by e06smtp06.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 6 Mar 2017 16:03:07 -0000 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp06.uk.ibm.com (192.168.101.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 6 Mar 2017 16:03:05 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id BE44D1B0804B for ; Mon, 6 Mar 2017 16:06:09 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v26G34mF61931716 for ; Mon, 6 Mar 2017 16:03:05 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0644842054 for ; Mon, 6 Mar 2017 16:02:58 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D639442041 for ; Mon, 6 Mar 2017 16:02:57 +0000 (GMT) Received: from oc1027705133.ibm.com (unknown [9.152.212.222]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Mon, 6 Mar 2017 16:02:57 +0000 (GMT) From: Andreas Arnez To: gdb-patches@sourceware.org Subject: [PATCH 3/3] linux-nat: Exploit /proc//mem for writing Date: Mon, 6 Mar 2017 17:00:20 +0100 In-Reply-To: <1488816060-20776-1-git-send-email-arnez@linux.vnet.ibm.com> References: <1488816060-20776-1-git-send-email-arnez@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17030616-0024-0000-0000-000002C3B2F6 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17030616-0025-0000-0000-000022A55ED2 Message-Id: <1488816060-20776-4-git-send-email-arnez@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-03-06_12:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703060134 X-IsSubscribed: yes So far linux_proc_xfer_partial refused to handle write requests. This is still based on the assumption that the Linux kernel does not support writes to /proc//mem. That used to be true, but has changed with Linux 2.6.39 released in May 2011. This patch lifts this restriction and now exploits /proc//mem for writing to inferior memory as well, if possible. gdb/ChangeLog: * linux-nat.c (linux_proc_xfer_partial): Handle write operations as well. --- gdb/linux-nat.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index c58ed83..73ef2d4 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -3978,10 +3978,9 @@ linux_child_pid_to_exec_file (struct target_ops *self, int pid) return linux_proc_pid_to_exec_file (pid); } -/* Implement the to_xfer_partial interface for memory reads using the /proc - filesystem. Because we can use a single read() call for /proc, this - can be much more efficient than banging away at PTRACE_PEEKTEXT, - but it doesn't support writes. */ +/* Implement the to_xfer_partial target method using /proc//mem. + Because we can use a single read/write call, this can be much more + efficient than banging away at PTRACE_PEEKTEXT. */ static enum target_xfer_status linux_proc_xfer_partial (struct target_ops *ops, enum target_object object, @@ -3993,7 +3992,7 @@ linux_proc_xfer_partial (struct target_ops *ops, enum target_object object, int fd; char filename[64]; - if (object != TARGET_OBJECT_MEMORY || !readbuf) + if (object != TARGET_OBJECT_MEMORY) return TARGET_XFER_EOF; /* Don't bother for one word. */ @@ -4004,26 +4003,27 @@ linux_proc_xfer_partial (struct target_ops *ops, enum target_object object, thread. That requires some juggling, but is even faster. */ xsnprintf (filename, sizeof filename, "/proc/%d/mem", ptid_get_pid (inferior_ptid)); - fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0); + fd = gdb_open_cloexec (filename, ((readbuf ? O_RDONLY : O_WRONLY) + | O_LARGEFILE), 0); if (fd == -1) return TARGET_XFER_EOF; - /* If pread64 is available, use it. It's faster if the kernel - supports it (only one syscall), and it's 64-bit safe even on - 32-bit platforms (for instance, SPARC debugging a SPARC64 - application). */ + /* Use pread64/pwrite64 if available, since they save a syscall and can + handle 64-bit offsets even on 32-bit platforms (for instance, SPARC + debugging a SPARC64 application). */ #ifdef HAVE_PREAD64 - if (pread64 (fd, readbuf, len, offset) != len) + ret = (readbuf ? pread64 (fd, readbuf, len, offset) + : pwrite64 (fd, writebuf, len, offset)); #else - if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len) + ret = lseek (fd, offset, SEEK_SET); + if (ret != -1) + ret = (readbuf ? read (fd, readbuf, len) + : write (fd, writebuf, len)); #endif - ret = 0; - else - ret = len; close (fd); - if (ret == 0) + if (ret == -1 || ret == 0) return TARGET_XFER_EOF; else {