From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by sourceware.org (Postfix) with ESMTPS id B1190384B82D for ; Wed, 25 May 2022 13:42:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B1190384B82D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f41.google.com with SMTP id i20-20020a05600c355400b0039456976dcaso2553468wmq.1 for ; Wed, 25 May 2022 06:42:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=stZr/G/Q1LQvs6fHznk+pbN6pCCQYexlotj+Lh8DWHc=; b=R7Gnpw1yzqm96fqbxN3zhdh2owM1qWmOSdi81UmkTR8zCvHmuzjBh+olhZCt5XH0Il F2PKfrKqoE/NRL3hpS7HBmeLGjW8httVuvbP9evCU3PEZwMrJseQDDUEnEnT9S1A55K/ 2a126IFchVX1NomnwUtT6PZgSt4wn0aTO0FNQnpfg2XfDQXYtG8JNrIYzHFvtdYQuy/1 ooxASiVwh6vpQIaWs8MO2z+UeEy+rxsU1uiMqER0+9aWqOYh4oiqjgszB7GLN5NpTuiF RJgSkRvQiiBUroWvAoq7IALZjlWlg8EcHxl4TcRIWzv/MTDutIQfskMtl8YXuH5/H4XG 90CA== X-Gm-Message-State: AOAM532Cw5ht2sY5kL23F7vI8Icgiy2S4ih7+nXnW0zawU67NBp6/qpy k78umKM+bknKcbfm6FY09oY= X-Google-Smtp-Source: ABdhPJxwwIr5AN6O23WunboQQaFnKqiUK3Cmt6yz44OuUtZ5zjB+d/5FCV630WVDEkKDlmMPAANFEQ== X-Received: by 2002:a05:600c:1d9d:b0:397:3dc1:5249 with SMTP id p29-20020a05600c1d9d00b003973dc15249mr8466407wms.144.1653486120534; Wed, 25 May 2022 06:42:00 -0700 (PDT) Received: from ?IPV6:2001:8a0:f924:2600:209d:85e2:409e:8726? ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id v7-20020a056000144700b0020c5253d926sm2251589wrx.114.2022.05.25.06.41.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 May 2022 06:41:59 -0700 (PDT) Message-ID: Date: Wed, 25 May 2022 14:41:57 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH] linux_nat_target::xfer_partial: Fallback to ptrace Content-Language: en-US To: Keith Seitz , gdb-patches@sourceware.org References: <20220512181557.2093666-1-keiths@redhat.com> <23154482-133e-8bfe-6d14-17f7e79b716b@palves.net> <7cca96e8-bd52-0c6c-8a90-582c0abd80a9@redhat.com> From: Pedro Alves In-Reply-To: <7cca96e8-bd52-0c6c-8a90-582c0abd80a9@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, BODY_8BITS, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no 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: Wed, 25 May 2022 13:42:04 -0000 On 2022-05-24 19:56, Keith Seitz wrote: > On 5/20/22 11:51, Pedro Alves wrote: >> On 2022-05-12 19:15, Keith Seitz via Gdb-patches wrote: >>> Commit 05c06f318fd9a112529dfc313e6512b399a645e4 enabled GDB >>> to access memory while threads are running. It did this by accessing >>> /proc/PID/task/LWP/mem. >>> >>> Unfortunatley, this interface is not implemented for writing in older kernels >> >> Unfortunatley -> Unfortunately > > Fixed. > >> Oh man.  I thought such kernels were already older than the oldest version >> we support, but looks like not.  :-/  I don't suppose you could instead >> convince the kernel team to backport the patches that made /proc/pid/mem >> writable (https://lore.kernel.org/lkml/20110314151320.GG21770@outflux.net/T/).. :-P > > :-) > >> Both gdb and gdbserver are now relying on this to access memory of running threads. >> This never worked for gdb, but it did for gdbserver, by stopping all threads temporarily. >> I would really-really-really prefer not to add that code back for ancient >> kernels... > > I did not observe any issues with gdbserver. As to whether we need to support > kernels as old as RHEL6? I don't really know. I noticed problems when I was > running through some internal testing which still uses RHEL6. > > I figured (maybe incorrectly) that the fallthrough was otherwise harmless. > I'm fine if we'd prefer not to include this patch, though. I honestly > haven't a clue how widespread RHEL6-vintage kernels are in the wild. > >>> --- 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_EOF) >>> +    return xfer; >>> +      /* Fallthrough to ptrace.  */ >> >> Seems fine, but I'd like a comment here giving a hint that we'll be able to >> remove this once we stop supporting such old kernels.  Something like: >> >>        /* Fallthrough to ptrace.  /proc/pid/mem wasn't writable before Linux 2.6.39.  */ >> >> I got that number by finding commit 198214a7ee50, and looking at git tag --contains 198214a7ee50. > > I've updated that comment. > >> AFAICT, RHEL 6 is on 2.6.32. > > As far as I can tell, that is correct. > > I will wait before pushing this to give others the opportunity to chime in. I'd like to chime in again myself. :-P Lancelot's patch here: https://sourceware.org/pipermail/gdb-patches/2022-May/189388.html made me realize that checking for TARGET_XFER_EOF isn't what we want here. If /proc/pid/mem failed with TARGET_XFER_EOF, it means the program exited, or execed. If the latter, we want to propagate the error out, not fallback to ptrace, as that would result in read/writing memory out of the post-exec address space, while gdb thought it was reading/writing the pre-exec address space. I.e., the kernel failing with EOF is nice in that it avoids a race condition where we e.g., poke a breakpoint (or poke back the original instruction) in the post-exec binary at a completely wrong address/instruction. The other case for EOF is when the process exits, and falling back to ptrace in that case is just pointless. I guess the write to /proc/pid/mem fails with EIO for you, and there's nothing else we can use to detect the scenario. So we probably want to check TARGET_XFER_E_IO instead. And, maybe only do the fallback if writing.