public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: Andrew Burgess <aburgess@redhat.com>
Cc: Tom Tromey <tromey@adacore.com>,  gdb-patches@sourceware.org
Subject: Re: [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al
Date: Mon, 17 Jul 2023 11:12:09 -0600	[thread overview]
Message-ID: <87lefemphy.fsf@tromey.com> (raw)
In-Reply-To: <878rbhi9nh.fsf@redhat.com> (Andrew Burgess's message of "Sat, 15 Jul 2023 14:31:14 +0100")

>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

Andrew> Turns out, 'start' doesn't do anything special.  You call
Andrew> scoped_restore_current_inferior_for_memory in order to setup
Andrew> inferior_ptid, but at the point 'start' calls 'main_name', inferior_ptid
Andrew> is null_ptid, so any memory accesses will fail.

Andrew> I suspect the reason this is OK is that the memory read is actually
Andrew> hitting the objfile, I did try 'set trust-readonly-sections off', but
Andrew> everything still appears to work, I'm not sure why.

I think it's because, before the inferior is started, memory accesses
reach exec_target::xfer_partial, which calls section_table_xfer_memory_partial.

Andrew> Or we could use 'scoped_restore_current_inferior' or
Andrew> 'scoped_restore_current_inferior_for_memory' depending on whether
Andrew> inferior::pid is 0 or not.
...
Andrew> I think for things like 'read_memory' it is fine if we just add a check
Andrew> and raise a Python error if the inferior is not yet active, but as I
Andrew> argue above, I don't think this is good enough for 'main_name'.

I think it's valid to read or search memory before the inferior started.
It's even valid to write memory when 'set write on'.

So, let me know what you think of the appended, which tries to handle
the pid==0 case in all these spots.

Tom

diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index f6000b944da..3b5d8378c3a 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -520,6 +520,19 @@ gdbpy_inferiors (PyObject *unused, PyObject *unused2)
   return PyList_AsTuple (list.get ());
 }
 
+/* A helper for scoped_restore_current_inferior_for_memory that
+   handles the situation where the desired inferior has not yet run,
+   so has a PID of 0.  This takes an inferior and returns the ptid of
+   a thread to use when accessing memory.  */
+
+static ptid_t
+ptid_for_memory (inferior *inf)
+{
+  if (inf->pid == 0)
+    return {};
+  return any_thread_of_inferior (inf)->ptid;
+}
+
 /* Membuf and memory manipulation.  */
 
 /* Implementation of Inferior.read_memory (address, length).
@@ -550,7 +563,7 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
       /* 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);
+	(inf->inferior, ptid_for_memory (inf->inferior));
 
       buffer.reset ((gdb_byte *) xmalloc (length));
 
@@ -608,7 +621,7 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
 	 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);
+	(inf->inferior, ptid_for_memory (inf->inferior));
 
       write_memory_with_notification (addr, buffer, length);
     }
@@ -683,7 +696,7 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
 	 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);
+	(inf->inferior, ptid_for_memory (inf->inferior));
 
       found = target_search_memory (start_addr, length,
 				    buffer, pattern_size,
@@ -944,7 +957,7 @@ infpy_get_main_name (PyObject *self, void *closure)
 	 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);
+	(inf->inferior, ptid_for_memory (inf->inferior));
 
       name = main_name ();
     }

  reply	other threads:[~2023-07-17 17:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-14 17:06 [PATCH v4 0/6] Fix some Python Inferior methods Tom Tromey
2023-07-14 17:06 ` [PATCH v4 1/6] Minor cleanups in py-inferior.exp Tom Tromey
2023-07-14 17:06 ` [PATCH v4 2/6] Refactor py-inferior.exp Tom Tromey
2023-07-14 17:06 ` [PATCH v4 3/6] Rename Python variable in py-inferior.exp Tom Tromey
2023-07-14 17:06 ` [PATCH v4 4/6] Remove obsolete comment from gdbthread.h Tom Tromey
2023-07-14 17:06 ` [PATCH v4 5/6] Introduce scoped_restore_current_inferior_for_memory Tom Tromey
2023-07-14 17:06 ` [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al Tom Tromey
2023-07-15 13:31   ` Andrew Burgess
2023-07-17 17:12     ` Tom Tromey [this message]
2023-07-17 18:15       ` Pedro Alves
2023-07-17 18:44         ` Tom Tromey
2023-07-18 12:20           ` [PATCH] Fix gdb.Inferior.read_memory without execution (PR dap/30644) (was: Re: [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al) Pedro Alves
2023-07-18 17:25             ` [PATCH] Fix gdb.Inferior.read_memory without execution (PR dap/30644) Tom Tromey
2023-07-19 13:14               ` Pedro Alves
2023-07-18  8:48         ` [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al Aktemur, Tankut Baris
2023-07-18 12:26           ` Pedro Alves
2023-07-18 13:19             ` Aktemur, Tankut Baris
2023-07-14 18:09 ` [PATCH v4 0/6] Fix some Python Inferior methods Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lefemphy.fsf@tromey.com \
    --to=tromey@adacore.com \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).