From patchwork Tue Mar 14 15:44:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Arnez X-Patchwork-Id: 19575 Received: (qmail 17918 invoked by alias); 14 Mar 2017 15:44: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 17866 invoked by uid 89); 14 Mar 2017 15:44:27 -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=Hx-languages-length:6181, 4466 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; Tue, 14 Mar 2017 15:44:25 +0000 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v2EFd5XE137930 for ; Tue, 14 Mar 2017 11:44:24 -0400 Received: from e06smtp12.uk.ibm.com (e06smtp12.uk.ibm.com [195.75.94.108]) by mx0a-001b2d01.pphosted.com with ESMTP id 296h3t04da-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 14 Mar 2017 11:44:24 -0400 Received: from localhost by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 14 Mar 2017 15:44:22 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp12.uk.ibm.com (192.168.101.142) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 14 Mar 2017 15:44:20 -0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v2EFiKRZ10617138; Tue, 14 Mar 2017 15:44:20 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 49ABC4C04E; Tue, 14 Mar 2017 15:44:06 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 242BD4C044; Tue, 14 Mar 2017 15:44:06 +0000 (GMT) Received: from oc1027705133.ibm.com (unknown [9.152.212.162]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Tue, 14 Mar 2017 15:44:06 +0000 (GMT) From: Andreas Arnez To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH v2] inf-ptrace: Do not stop memory transfers after a single word Date: Tue, 14 Mar 2017 16:44:19 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 17031415-0008-0000-0000-00000400143A X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17031415-0009-0000-0000-00001CD11739 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-03-14_08:, , 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-1703140122 X-IsSubscribed: yes Version 1 is here: https://sourceware.org/ml/gdb-patches/2017-03/msg00043.html Changes from version 1: * "unsigned" -> "unsigned int" * Compare pointers with NULL explicitly. * Move the ptrace peek/poke loop to a separate routine for better readability. OK to apply? --- Andreas -- >8 -- Subject: [PATCH v2] inf-ptrace: Do not stop memory transfers after a single word When inf_ptrace_xfer_partial performs a memory transfer via ptrace with PT_READ_I, PT_WRITE_I (aka PTRACE_PEEKTEXT, PTRACE_POKETEXT), etc., then it currently transfers at most one word. This behavior yields degraded performance, particularly if the caller has significant preparation work for each invocation. And indeed it has for writing, in memory_xfer_partial in target.c, where all of the remaining data to be transferred is copied to a temporary buffer each time, for breakpoint shadow handling. Thus large writes have quadratic runtime and can take hours. Note: On GNU/Linux targets GDB usually does not use inf_ptrace_xfer_partial for large memory *reads* from the inferior, but attempts a single read from /proc//mem instead. However, this is not currently true for memory *writes*. So the problem occurs on GNU/Linux when transferring large amounts of data *into* the inferior. This patch fixes the performance issue by attempting to fulfill the whole transfer request in inf_ptrace_xfer_partial, using a loop around the ptrace call. gdb/ChangeLog: PR gdb/21220 * inf-ptrace.c (inf_ptrace_xfer_partial): In "case TARGET_OBJECT_MEMORY", extract the logic for ptrace peek/poke... (inf_ptrace_peek_poke): ...here. New function. Now also loop over ptrace peek/poke until end of buffer or error. --- gdb/inf-ptrace.c | 142 +++++++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 73 deletions(-) diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c index 21578742..ce6796b 100644 --- a/gdb/inf-ptrace.c +++ b/gdb/inf-ptrace.c @@ -446,6 +446,72 @@ inf_ptrace_wait (struct target_ops *ops, return pid_to_ptid (pid); } +/* Transfer data via ptrace into process PID's memory from WRITEBUF, or + from process PID's memory into READBUF. Start at target address ADDR + and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must + be non-null. Return the number of transferred bytes. */ + +static ULONGEST +inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf, + const gdb_byte *writebuf, + ULONGEST addr, ULONGEST len) +{ + ULONGEST n; + unsigned int chunk; + + /* We transfer aligned words. Thus align ADDR down to a word + boundary and determine how many bytes to skip at the + beginning. */ + unsigned int skip = addr & (sizeof (PTRACE_TYPE_RET) - 1); + addr -= skip; + + for (n = 0; + n < len; + n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0) + { + /* Restrict to a chunk that fits in the current word. */ + chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n); + + /* Use a union for type punning. */ + union + { + PTRACE_TYPE_RET word; + gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; + } buf; + + /* Read the word, also when doing a partial word write. */ + if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET)) + { + errno = 0; + buf.word = ptrace (PT_READ_I, pid, + (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0); + if (errno) + break; + if (readbuf != NULL) + memcpy (readbuf + n, buf.byte + skip, chunk); + } + if (writebuf != NULL) + { + memcpy (buf.byte + skip, writebuf + n, chunk); + errno = 0; + ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr, + buf.word); + if (errno) + { + /* Using the appropriate one (I or D) is necessary for + Gould NP1, at least. */ + errno = 0; + ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr, + buf.word); + if (errno) + break; + } + } + } + + return n; +} + /* Implement the to_xfer_partial target_ops method. */ static enum target_xfer_status @@ -491,79 +557,9 @@ inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object, return TARGET_XFER_EOF; } #endif - { - union - { - PTRACE_TYPE_RET word; - gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; - } buffer; - ULONGEST rounded_offset; - ULONGEST partial_len; - - /* Round the start offset down to the next long word - boundary. */ - rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET); - - /* Since ptrace will transfer a single word starting at that - rounded_offset the partial_len needs to be adjusted down to - that (remember this function only does a single transfer). - Should the required length be even less, adjust it down - again. */ - partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset; - if (partial_len > len) - partial_len = len; - - if (writebuf) - { - /* If OFFSET:PARTIAL_LEN is smaller than - ROUNDED_OFFSET:WORDSIZE then a read/modify write will - be needed. Read in the entire word. */ - if (rounded_offset < offset - || (offset + partial_len - < rounded_offset + sizeof (PTRACE_TYPE_RET))) - /* Need part of initial word -- fetch it. */ - buffer.word = ptrace (PT_READ_I, pid, - (PTRACE_TYPE_ARG3)(uintptr_t) - rounded_offset, 0); - - /* Copy data to be written over corresponding part of - buffer. */ - memcpy (buffer.byte + (offset - rounded_offset), - writebuf, partial_len); - - errno = 0; - ptrace (PT_WRITE_D, pid, - (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, - buffer.word); - if (errno) - { - /* Using the appropriate one (I or D) is necessary for - Gould NP1, at least. */ - errno = 0; - ptrace (PT_WRITE_I, pid, - (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, - buffer.word); - if (errno) - return TARGET_XFER_EOF; - } - } - - if (readbuf) - { - errno = 0; - buffer.word = ptrace (PT_READ_I, pid, - (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, - 0); - if (errno) - return TARGET_XFER_EOF; - /* Copy appropriate bytes out of the buffer. */ - memcpy (readbuf, buffer.byte + (offset - rounded_offset), - partial_len); - } - - *xfered_len = partial_len; - return TARGET_XFER_OK; - } + *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf, + offset, len); + return *xfered_len ? TARGET_XFER_OK : TARGET_XFER_EOF; case TARGET_OBJECT_UNWIND_TABLE: return TARGET_XFER_E_IO;