From patchwork Thu Jul 13 14:05:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72654 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 89E3C385773F for ; Thu, 13 Jul 2023 14:06:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 89E3C385773F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689257218; bh=RZYupIS6bCDfKnKo+8vb5qM1YMs1DmdCsbRHUj89RTc=; 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=f9DROeYvtPhb65TenjZQTHpvfqBvs6r3aEqHbYFrBVr5sBoH8ldf8DMU7WD4Sf7ny 6JZLT1jMdPHN3A+GetutUrenITMNSLs0PCbe/tdE8a8cH9CAk6AfUB6E3WNDH25CV+ hBTfLwJsPV05840BUHymwEGMXRhoT2PtS8p7Kbug= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by sourceware.org (Postfix) with ESMTPS id 977333858288 for ; Thu, 13 Jul 2023 14:05:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 977333858288 Received: by mail-io1-xd33.google.com with SMTP id ca18e2360f4ac-7837329a00aso24423139f.2 for ; Thu, 13 Jul 2023 07:05:35 -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=RZYupIS6bCDfKnKo+8vb5qM1YMs1DmdCsbRHUj89RTc=; b=ZQ5hvCgZivJbEDVmg0x9iY6OVVvOHcl0jY6TPrlswcID5ttKlLhMobdpxQcxTdrE95 MtMcu2lGUxFXXjYwU0LhAISrbsDEkY+bVbaDl0H2FXqF47hfCYPchdtVnp/wil0phw1f DgnJH2br8V1S9sAOvx4tkhm1K+WX0HzbkrkP4P5ZF0vW0lyMkjOMVZ8PB7FzfYEmErq9 ZOTGqpJ5j4wszMPMuB+SbF21Sp4LQNkYo1JgQTiTDvVt5KvaQ7LqfDpsMSzbc4Lg/B9J tc36I2Rm388G9p3eWAYaT8ccSt8Kc9i/ZzKzxsPNq0dcem+Ys67KZjY/aXvfvDLsSGJ9 xjvA== X-Gm-Message-State: ABy/qLafGh464Fh41u4CVIpT+0jMVc/L7j/5HjtpPXEsrQ0o8Y8BNOQB H8PaFMJgM4GhQ5/fjBCIHloygmX5A5rS6UklqhrPng== X-Google-Smtp-Source: APBJJlFDLIzgH1PN/icAbfWGUfMbvELk4qrqd2xC9vi306UeaUPAhdcprPZzO1Bs9tSnoihujYpc0A== X-Received: by 2002:a05:6602:2208:b0:786:267e:bd4 with SMTP id n8-20020a056602220800b00786267e0bd4mr1605254ion.10.1689257134402; Thu, 13 Jul 2023 07:05:34 -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:31 -0600 Subject: [PATCH v3 6/6] Use correct inferior in Inferior.read_memory et al MIME-Version: 1.0 Message-Id: <20230713-py-inf-fixes-30615-v3-6-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.6 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" A user noticed that Inferior.read_memory and a few other Python APIs will always use the currently selected inferior, not the one passed to the call. This patch fixes the bug by arranging to switch to the inferior. I found this same issue in several APIs, so this fixes them all. I also added a few missing calls to INFPY_REQUIRE_VALID to these methods. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30615 --- gdb/python/py-inferior.c | 43 ++++++++++++++++++++++++++------ gdb/testsuite/gdb.python/py-inferior.exp | 26 +++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index af8bd8855a3..f6000b944da 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -30,6 +30,7 @@ #include "gdbsupport/gdb_signals.h" #include "py-event.h" #include "py-stopevent.h" +#include "progspace-and-thread.h" #include using thread_map_t @@ -528,11 +529,14 @@ gdbpy_inferiors (PyObject *unused, PyObject *unused2) static PyObject * infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) { + inferior_object *inf = (inferior_object *) self; CORE_ADDR addr, length; gdb::unique_xmalloc_ptr buffer; PyObject *addr_obj, *length_obj; static const char *keywords[] = { "address", "length", NULL }; + INFPY_REQUIRE_VALID (inf); + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords, &addr_obj, &length_obj)) return NULL; @@ -543,6 +547,11 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) try { + /* Use this scoped-restore because we want to be able to read + memory from an unwinder. */ + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); + buffer.reset ((gdb_byte *) xmalloc (length)); read_memory (addr, buffer.get (), length); @@ -565,6 +574,7 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) static PyObject * infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) { + inferior_object *inf = (inferior_object *) self; struct gdb_exception except; Py_ssize_t buf_len; const gdb_byte *buffer; @@ -573,6 +583,8 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) static const char *keywords[] = { "address", "buffer", "length", NULL }; Py_buffer pybuf; + INFPY_REQUIRE_VALID (inf); + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords, &addr_obj, &pybuf, &length_obj)) return NULL; @@ -591,6 +603,13 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) try { + /* It's probably not too important to avoid invalidating the + frame cache when writing memory, but this scoped-restore is + still used here, just to keep the code similar to other code + in this file. */ + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); + write_memory_with_notification (addr, buffer, length); } catch (gdb_exception &ex) @@ -604,7 +623,7 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) } /* Implementation of - gdb.search_memory (address, length, pattern). ADDRESS is the + Inferior.search_memory (address, length, pattern). ADDRESS is the address to start the search. LENGTH specifies the scope of the search from ADDRESS. PATTERN is the pattern to search for (and must be a Python object supporting the buffer protocol). @@ -614,6 +633,7 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) static PyObject * infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) { + inferior_object *inf = (inferior_object *) self; struct gdb_exception except; CORE_ADDR start_addr, length; static const char *keywords[] = { "address", "length", "pattern", NULL }; @@ -624,6 +644,8 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) int found = 0; Py_buffer pybuf; + INFPY_REQUIRE_VALID (inf); + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords, &start_addr_obj, &length_obj, &pybuf)) @@ -656,6 +678,13 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) try { + /* It's probably not too important to avoid invalidating the + frame cache when searching memory, but this scoped-restore is + still used here, just to keep the code similar to other code + in this file. */ + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); + found = target_search_memory (start_addr, length, buffer, pattern_size, &found_addr); @@ -910,12 +939,12 @@ infpy_get_main_name (PyObject *self, void *closure) try { /* This is unfortunate but the implementation of main_name can - reach into memory. */ - scoped_restore_current_inferior restore_inferior; - set_current_inferior (inf->inferior); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (inf->inferior->pspace); + reach into memory. It's probably not too important to avoid + invalidating the frame cache here, but this scoped-restore is + still used, just to keep the code similar to other code in + this file. */ + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); name = main_name (); } diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 8762b6992ca..eb501838117 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -91,6 +91,7 @@ gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ "" \ "read str contents" +gdb_test "python print(astr\[0\])" .* gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0 gdb_py_test_silent_cmd "python astr\[1\] = a" "change str" 0 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ @@ -98,6 +99,10 @@ gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ gdb_test "print str" " = \"hallo, testsuite\"" \ "ensure str was changed in the inferior" +# Add a new inferior here, so we can test that operations work on the +# correct inferior. +set num [add_inferior] + # Test memory search. set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*} @@ -115,6 +120,9 @@ with_test_prefix "string" { gdb_test_no_output "py start_addr = search_buf.address" gdb_test_no_output "py length = search_buf.type.sizeof" + # Switch to the new inferior before testing. + gdb_test "inferior $num" ".*" "switch to inferior $num" + gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \ "${one_pattern_found}" "find string pattern" @@ -128,6 +136,24 @@ with_test_prefix "string" { "${one_pattern_found}" "pattern found at end of range" } +# While still in the new inferior, test reading and writing memory +# again. +gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ + "" \ + "read str while other inferior selected" +gdb_test "python print(astr\[1\])" "b'a'" \ + "print a character from the string" +gdb_py_test_silent_cmd "python astr\[1\] = b'X'" "change str again" 0 +gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ + "write str while other inferior selected" 1 + +gdb_test "inferior 1" ".*" "switch back to inferior 1" + +gdb_test "print str" " = \"hXllo, testsuite\"" \ + "ensure str was changed while other inferior selected" + +gdb_test_no_output "remove-inferiors $num" "remove-inferiors $num" + # Import struct to pack the following patterns. gdb_test_no_output "py from struct import *"