From patchwork Thu Jul 13 14:05:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72652 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E20E13865C2D for ; Thu, 13 Jul 2023 14:06:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E20E13865C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689257189; bh=3TJyL9VXGTdhfrjnFX8NGe3dhiRDXcCizlSU3zWpqVA=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=J6LG6D9wMh01Mrczzqt80jqqDTCWjtxE9AO5xIGGM/VIgPY4nfVmbg2/1k0eY9dpQ Q91UkG5CnFkiEEgByQ3ze8hf41e2XgkKWgqCkz1v4VA0tGij7FENvP5VEzRCDa0BTV HsBLV36S6BG1XnMDCTxHgmrTFIomEdbjVHFCP1Es= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id DEA4B3858404 for ; Thu, 13 Jul 2023 14:05:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DEA4B3858404 Received: by mail-io1-xd2e.google.com with SMTP id ca18e2360f4ac-7835e2286c4so24560439f.1 for ; Thu, 13 Jul 2023 07:05:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689257134; x=1691849134; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3TJyL9VXGTdhfrjnFX8NGe3dhiRDXcCizlSU3zWpqVA=; b=Ap18MUJ51oJc6Ve6ikTLEmOE0UVPnCeRNE84xgSpKmhV6yyWRYod8KcYn7m095AgJy q6rHX7fxZ7TyE6huO6HjYhYazmQ5pu7/EOiWcWDTcU0oI+wB+pOAcJj7l1qMzMyVfg6h 4MAIuj8Zsq08b3zyCDT7jm+7GdqG5qZR09iP9Ipzz+g+kAe/9FDWki4cxxRqHV8kQrow 6VbDKCEZWi6/EiZl7ouBfqWNyfhH+vKmUy1H6VcrfAUqbk/UjSWJmNkGsEPzf3eUq5M4 zrAicA3Ng6qh7RcHRjcPUzrpUNEg5l9BvwbivW5t2bsPX1ZfMFwPwyFiGe+i24OA2K24 SISw== X-Gm-Message-State: ABy/qLbnxU/Ndsb3tuQj7sFc5UpfF61isdCo+XKbKKibJrZyN7miIkTW diqYREsMSqi/za0D0alQmlqbtV6awRhPikRfStAhYQ== X-Google-Smtp-Source: APBJJlH4xuqPMGXNW8IfAEZPRffqwlGERuWaUiI9NNhOlZ/o1jwAI/SYhn4ozbLkCWCNnLs+tfLxLQ== X-Received: by 2002:a6b:d919:0:b0:787:167e:f796 with SMTP id r25-20020a6bd919000000b00787167ef796mr1734920ioc.15.1689257133728; Thu, 13 Jul 2023 07:05:33 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id n13-20020a02a90d000000b0042b7b76b29fsm1841336jam.174.2023.07.13.07.05.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 07:05:33 -0700 (PDT) Date: Thu, 13 Jul 2023 08:05:30 -0600 Subject: [PATCH v3 5/6] Introduce scoped_restore_current_inferior_for_memory MIME-Version: 1.0 Message-Id: <20230713-py-inf-fixes-30615-v3-5-26a024f30553@adacore.com> References: <20230713-py-inf-fixes-30615-v3-0-26a024f30553@adacore.com> In-Reply-To: <20230713-py-inf-fixes-30615-v3-0-26a024f30553@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This introduces a new class, scoped_restore_current_inferior_for_memory, and arranges to use it in a few places. This class is intended to handle setting up and restoring the various globals that are needed to read or write memory -- but without invalidating the frame cache. I wasn't able to test the change to aix-thread.c. --- gdb/aix-thread.c | 18 ++++-------------- gdb/inferior.h | 29 +++++++++++++++++++++++++++++ gdb/proc-service.c | 10 ++-------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c index fbe80d656c2..74cc67c942b 100644 --- a/gdb/aix-thread.c +++ b/gdb/aix-thread.c @@ -615,13 +615,8 @@ pdc_read_data (pthdb_user_t user_current_pid, void *buf, /* This is needed to eliminate the dependency of current thread which is null so that thread reads the correct target memory. */ { - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); - inferior_ptid = ptid_t (user_current_pid); - scoped_restore_current_inferior restore_inferior; - set_current_inferior (inf); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (inf->pspace); + scoped_restore_current_inferior_for_memory save_inferior + (inf, ptid_t (user_current_pid)); status = target_read_memory (addr, (gdb_byte *) buf, len); } ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE; @@ -648,13 +643,8 @@ pdc_write_data (pthdb_user_t user_current_pid, void *buf, user_current_pid, (long) buf, hex_string (addr), len); { - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); - inferior_ptid = ptid_t (user_current_pid); - scoped_restore_current_inferior restore_inferior; - set_current_inferior (inf); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (inf->pspace); + scoped_restore_current_inferior_for_memory save_inferior + (inf, ptid_t (user_current_pid)); status = target_write_memory (addr, (gdb_byte *) buf, len); } diff --git a/gdb/inferior.h b/gdb/inferior.h index caa8e4d494a..be76c456c8c 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -761,6 +761,35 @@ class scoped_restore_current_inferior inferior *m_saved_inf; }; +/* When reading memory from an inferior, the global inferior_ptid must + also be set. This class arranges to save and restore the necessary + state for reading or writing memory, but without invalidating the + frame cache. */ + +class scoped_restore_current_inferior_for_memory +{ +public: + + /* Save the current globals and switch to the given inferior and the + inferior's program space. PTID must name a thread in INF, it is + used as the new inferior_ptid. */ + scoped_restore_current_inferior_for_memory (inferior *inf, ptid_t ptid) + : m_save_ptid (&inferior_ptid) + { + set_current_inferior (inf); + set_current_program_space (inf->pspace); + inferior_ptid = ptid; + } + + DISABLE_COPY_AND_ASSIGN (scoped_restore_current_inferior_for_memory); + +private: + + scoped_restore_current_inferior m_save_inferior; + scoped_restore_current_program_space m_save_progspace; + scoped_restore_tmpl m_save_ptid; +}; + /* Traverse all inferiors. */ diff --git a/gdb/proc-service.c b/gdb/proc-service.c index 509836ec1a8..366e0510070 100644 --- a/gdb/proc-service.c +++ b/gdb/proc-service.c @@ -72,14 +72,8 @@ static ps_err_e ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr, gdb_byte *buf, size_t len, int write) { - scoped_restore_current_inferior restore_inferior; - set_current_inferior (ph->thread->inf); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (ph->thread->inf->pspace); - - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); - inferior_ptid = ph->thread->ptid; + scoped_restore_current_inferior_for_memory save_inferior (ph->thread->inf, + ph->thread->ptid); CORE_ADDR core_addr = ps_addr_to_core_addr (addr);