From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 599263858D32 for ; Tue, 26 Jul 2022 17:24:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 599263858D32 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-52-ZYbeBg7kOs-CKIhyJTAvtA-1; Tue, 26 Jul 2022 13:24:37 -0400 X-MC-Unique: ZYbeBg7kOs-CKIhyJTAvtA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 846213800C35; Tue, 26 Jul 2022 17:24:37 +0000 (UTC) Received: from [10.2.16.251] (unknown [10.2.16.251]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3935F2026D64; Tue, 26 Jul 2022 17:24:37 +0000 (UTC) Message-ID: Date: Tue, 26 Jul 2022 10:24:36 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH] linux_nat_target::xfer_partial: Fallback to ptrace To: Pedro Alves , gdb-patches@sourceware.org References: <20220603151839.2632317-1-keiths@redhat.com> <97900ea5-350c-c30c-9a55-98355e8f9abc@palves.net> From: Keith Seitz In-Reply-To: <97900ea5-350c-c30c-9a55-98355e8f9abc@palves.net> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Jul 2022 17:24:42 -0000 On 7/21/22 13:07, Pedro Alves wrote: > > Appologies for the delay, there was something here that I wanted to think about, and then it > just fell through the cracks. I have quite a few cracks myself through which things fall! >> On 6/3/22 08:18, Keith Seitz via Gdb-patches wrote: >>> Pedro Alves wrote: >>> diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c >>> index 3b5400896bc..571c97137c2 100644 >>> --- a/gdb/linux-nat.c >>> +++ b/gdb/linux-nat.c >>> @@ -3706,8 +3706,12 @@ linux_nat_target::xfer_partial (enum target_object object, >>>         if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT)) >>>       offset &= ((ULONGEST) 1 << addr_bit) - 1; >>>   -      return linux_proc_xfer_memory_partial (readbuf, writebuf, >>> -                         offset, len, xfered_len); >>> +      enum target_xfer_status xfer >>> +    = linux_proc_xfer_memory_partial (readbuf, writebuf, >>> +                      offset, len, xfered_len); >>> +      if (xfer != TARGET_XFER_E_IO || readbuf != nullptr) >>> +    return xfer; >>> +      /* Fallthrough to ptrace.  /proc/pid/mem wasn't writable before Linux 2.6.39.  */ >>>       } >>>       return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf, >> > So I was wondering about mitigating this by only falling back to ptrace if writing to > /proc/pid/mem doesn't really work. Checking the kernel version itself seems a bit > fragile, so I thought we could make gdb probe once at started up whether writing > to itself via /proc/self/mem works. It turns out that actually works. With this, you'd > just add an extra proc_mem_file_is_writable() check in your patch before falling > back, or even, skip straight to ptrace if !proc_mem_file_is_writable(). > > WDYT? I think this is quite clever, and an obvious step up in reliability. Thanks for doing this for me. > From 56622b9cadff4b62a0b05861015ce06cf9d6e8f2 Mon Sep 17 00:00:00 2001 > From: Pedro Alves > Date: Thu, 21 Jul 2022 19:11:16 +0100 > Subject: [PATCH] gdb/linux-nat: Check whether /proc/pid/mem is writable > > Probe whether /proc/pid/mem is writable, by using it to write to a GDB > variable. [snip] I've taken your patch and updated mine (now trivial): diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 0a93ab5..95732a6 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -3710,8 +3712,10 @@ enum target_xfer_status if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT)) offset &= ((ULONGEST) 1 << addr_bit) - 1; - return linux_proc_xfer_memory_partial (readbuf, writebuf, - offset, len, xfered_len); + if (proc_mem_file_is_writable ()) + return linux_proc_xfer_memory_partial (readbuf, writebuf, + offset, len, xfered_len); + /* Fallthrough to ptrace */ } return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf, I've re-run this through all my testing, and all looks good. However, I suspect you already knew that. :-) If/when you push your patch, and there are no further concerns, I will push mine, with your approval. Thank you for your follow-up! Keith