public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Eli Zaretskii <eliz@gnu.org>
Subject: [PATCH v2 2/3] Add Progspace.objfile_for_address
Date: Fri, 21 Jul 2023 09:55:32 -0600	[thread overview]
Message-ID: <20230721-dap-modules-v2-2-1cf831842cc8@adacore.com> (raw)
In-Reply-To: <20230721-dap-modules-v2-0-1cf831842cc8@adacore.com>

This adds a new objfile_for_address method to gdb.Progspace.  This
makes it easy to find the objfile for a given address.

There's a related PR; and while this change would have been sufficient
for my original need, it's not clear to me whether I should close the
bug.  Nevertheless I think it makes sense to at least mention it here.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19288
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
 gdb/NEWS                                  |  3 +++
 gdb/doc/python.texi                       |  5 +++++
 gdb/progspace.c                           | 16 ++++++++++++++++
 gdb/progspace.h                           |  4 ++++
 gdb/python/py-progspace.c                 | 27 +++++++++++++++++++++++++++
 gdb/testsuite/gdb.python/py-progspace.exp |  7 +++++++
 6 files changed, 62 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 4c91a380e6c..a44f90e7c1c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -244,6 +244,9 @@ info main
 
   ** gdb.Value now has the 'assign' method.
 
+  ** gdb.Progspace now has the new method "objfile_for_address".  This
+     returns the gdb.Objfile, if any, that covers a given address.
+
 *** Changes in GDB 13
 
 * MI version 1 is deprecated, and will be removed in GDB 14.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 9a342f34bf0..d2868fe9f46 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -5068,6 +5068,11 @@ Return the name of the shared library holding the given @var{address}
 as a string, or @code{None}.
 @end defun
 
+@defun Progspace.objfile_for_address (address)
+Return the @code{gdb.Objfile} holding the given address, or
+@code{None} if no objfile covers it.
+@end defun
+
 One may add arbitrary attributes to @code{gdb.Progspace} objects
 in the usual Python way.
 This is useful if, for example, one needs to do some extra record keeping
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 32bdfebcf7c..5cf8334ee67 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -180,6 +180,22 @@ program_space::remove_objfile (struct objfile *objfile)
 
 /* See progspace.h.  */
 
+struct objfile *
+program_space::objfile_for_address (CORE_ADDR address)
+{
+  for (auto iter : objfiles ())
+    {
+      /* Don't check separate debug objfiles.  */
+      if (iter->separate_debug_objfile_backlink != nullptr)
+	continue;
+      if (is_addr_in_objfile (address, iter))
+	return iter;
+    }
+  return nullptr;
+}
+
+/* See progspace.h.  */
+
 void
 program_space::exec_close ()
 {
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 85215f0e2f1..ee12d89c173 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -249,6 +249,10 @@ struct program_space
   /* Free all the objfiles associated with this program space.  */
   void free_all_objfiles ();
 
+  /* Return the objfile containing ADDRESS, or nullptr if the address
+     is outside all objfiles in this progspace.  */
+  struct objfile *objfile_for_address (CORE_ADDR address);
+
   /* Return a range adapter for iterating over all the solibs in this
      program space.  Use it like:
 
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index a231d240342..b98ac8dde61 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -392,6 +392,30 @@ pspy_solib_name (PyObject *o, PyObject *args)
   return host_string_to_python_string (soname).release ();
 }
 
+/* Implement objfile_for_address.  */
+
+static PyObject *
+pspy_objfile_for_address (PyObject *o, PyObject *args)
+{
+  CORE_ADDR addr;
+  PyObject *addr_obj;
+
+  pspace_object *self = (pspace_object *) o;
+
+  PSPY_REQUIRE_VALID (self);
+
+  if (!PyArg_ParseTuple (args, "O", &addr_obj))
+    return nullptr;
+  if (get_addr_from_python (addr_obj, &addr) < 0)
+    return nullptr;
+
+  struct objfile *objf = self->pspace->objfile_for_address (addr);
+  if (objf == nullptr)
+    Py_RETURN_NONE;
+
+  return objfile_to_objfile_object (objf).release ();
+}
+
 /* Return the innermost lexical block containing the specified pc value,
    or 0 if there is none.  */
 static PyObject *
@@ -569,6 +593,9 @@ static PyMethodDef progspace_object_methods[] =
   { "solib_name", pspy_solib_name, METH_VARARGS,
     "solib_name (Long) -> String.\n\
 Return the name of the shared library holding a given address, or None." },
+  { "objfile_for_address", pspy_objfile_for_address, METH_VARARGS,
+    "objfile_for_address (int) -> gdb.Objfile\n\
+Return the objfile containing the given address, or None." },
   { "block_for_pc", pspy_block_for_pc, METH_VARARGS,
     "Return the block containing the given pc value, or None." },
   { "find_pc_line", pspy_find_pc_line, METH_VARARGS,
diff --git a/gdb/testsuite/gdb.python/py-progspace.exp b/gdb/testsuite/gdb.python/py-progspace.exp
index 638b27927c6..f0dc208ae4b 100644
--- a/gdb/testsuite/gdb.python/py-progspace.exp
+++ b/gdb/testsuite/gdb.python/py-progspace.exp
@@ -68,6 +68,13 @@ if ![is_address_zero_readable] {
     gdb_test "python print (gdb.current_progspace ().block_for_pc (0))" "None"
 }
 
+gdb_test "python print(gdb.current_progspace().objfile_for_address(${pc_val}).username)" \
+    ".*py-progspace" \
+    "objfile for pc"
+gdb_test "python print(gdb.current_progspace().objfile_for_address(0))" \
+    "None" \
+    "no objfile for 0"
+
 # With a single inferior, progspace.objfiles () and gdb.objfiles () should
 # be identical.
 gdb_test "python print (progspace.objfiles () == gdb.objfiles ())" "True"

-- 
2.40.1


  parent reply	other threads:[~2023-07-21 15:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-21 15:55 [PATCH v2 0/3] Implement the DAP "modules" request Tom Tromey
2023-07-21 15:55 ` [PATCH v2 1/3] Remove unused imports Tom Tromey
2023-07-21 15:55 ` Tom Tromey [this message]
2023-07-21 16:03   ` [PATCH v2 2/3] Add Progspace.objfile_for_address Eli Zaretskii
2023-07-21 15:55 ` [PATCH v2 3/3] Implement DAP modules request Tom Tromey

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=20230721-dap-modules-v2-2-1cf831842cc8@adacore.com \
    --to=tromey@adacore.com \
    --cc=eliz@gnu.org \
    --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).