[RFC v3 17/25] gdb/python: allow instantiation of gdb.Compunit from Python

Jan Vrany jan.vrany@labware.com
Wed Jan 29 12:43:39 GMT 2025


This commit adds code to allow user extension to instantiate
gdb.Compunit. This is a step towards a Python support for dynamically
generated code (JIT) in GDB.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
 gdb/block.h                              |   8 ++
 gdb/doc/python.texi                      |  16 ++++
 gdb/python/py-compunit.c                 | 102 ++++++++++++++++++++++-
 gdb/python/py-objfile.c                  |  12 +++
 gdb/python/python-internal.h             |   1 +
 gdb/symtab.c                             |  63 ++++++++++++++
 gdb/symtab.h                             |   6 ++
 gdb/testsuite/gdb.python/py-compunit.exp |  47 +++++++++++
 8 files changed, 254 insertions(+), 1 deletion(-)

diff --git a/gdb/block.h b/gdb/block.h
index c8fc0f8da39..40202e32ca7 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -312,6 +312,14 @@ struct block : public allocate_on_obstack<block>
 
   bool contains (const struct block *a, bool allow_nested = false) const;
 
+  /* Return true if this block's range overlap with [L, H) range.  Return
+     false otherwise.  */
+
+  bool overlaps (CORE_ADDR l, CORE_ADDR h) const
+  {
+    return ranges_overlap (l, h, start (), end ());
+  }
+
 private:
 
   /* Return the default entry-pc of this block.  The default is the address
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 46ac3f0eb53..47a934806d4 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6599,6 +6599,22 @@ The sequence of @code{gdb.Symtab} objects associated with this compunit.
 
 A @code{gdb.Compunit} object has the following methods:
 
+@defun Compunit.__init__ (filename, objfile, start, end @r{[}, capacity @r{]})
+Create a new compunit with given @var{filename} in given @var{objfile}
+(@pxref{Objfiles In Python}).  The newly created compunit has an empty global
+block and empty static block (@pxref{Blocks In Python}).
+
+The @var{start} and @var{end} arguments specifies the start and end address
+of compunit's global and static blocks.  It must not overlap with any existing
+compunit belonging to the same program space
+(@pxref{Progspaces In Python}).
+
+The optional @var{capacity} argument sets the initial capacity of the
+internal block vector.  More blocks than @var{capacity} can still be added
+to the compunit however.  If not specified, defaults to 8 blocks (including
+global and static blocks).
+@end defun
+
 @defun Compunit.is_valid ()
 Returns @code{True} if the @code{gdb.Compunit} object is valid,
 @code{False} if not.  A @code{gdb.Compunit} object can become invalid if
diff --git a/gdb/python/py-compunit.c b/gdb/python/py-compunit.c
index f0224ada09e..22bc4e5f0a6 100644
--- a/gdb/python/py-compunit.c
+++ b/gdb/python/py-compunit.c
@@ -17,6 +17,7 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <algorithm>
 #include "charset.h"
 #include "symtab.h"
 #include "source.h"
@@ -198,6 +199,105 @@ set_compunit (compunit_object *obj, struct compunit_symtab *compunit)
   Py_INCREF (obj);
 }
 
+/* Object initializer; creates a new compunit.
+
+   Use: __init__(FILENAME, OBJFILE, START, END [, CAPACITY]).  */
+
+static int
+cupy_init (PyObject *zelf, PyObject *args, PyObject *kw)
+{
+  struct compunit_object *self = (struct compunit_object*) zelf;
+
+  if (self->compunit)
+    {
+      PyErr_Format (PyExc_RuntimeError,
+		    _("Compunit object already initialized."));
+      return -1;
+    }
+
+  static const char *keywords[] = { "filename", "objfile", "start", "end",
+				    "capacity", nullptr };
+  const char *filename;
+  PyObject *objf_obj = nullptr;
+  uint64_t start = 0;
+  uint64_t end = 0;
+  uint64_t capacity = 8;
+
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "sOKK|K", keywords,
+					&filename, &objf_obj, &start, &end,
+					&capacity))
+    return -1;
+
+  auto objf = objfile_object_to_objfile (objf_obj);
+  if (! objf)
+    {
+      PyErr_Format (PyExc_TypeError,
+		    _("The objfile argument is not a valid gdb.Objfile "
+		      "object"));
+      return -1;
+    }
+
+  /* Check that start-end range is valid.  */
+  if (! (start <= end))
+    {
+      PyErr_Format (PyExc_ValueError,
+		    _("The start argument must be less or equal to the end "
+		      "argument"));
+      return -1;
+
+    }
+
+  /* Check that to-be created compunit does not overlap any other existing
+     existing compunit.  We have to make sure that all possibly overlapping
+     compunits are fully expanded before, though.  */
+
+  for (struct objfile *of : objf->pspace ()->objfiles_safe ())
+    {
+      if (of->has_unexpanded_symtabs ())
+	of->expand_symtabs_maybe_overlapping (start, end);
+
+      for (const compunit_symtab *cu : of->compunits ())
+	{
+	  if (cu->maybe_overlaps (start, end))
+	    {
+	      PyErr_Format (PyExc_ValueError,
+		    _("The start-end range may overlap with existing "
+		      "compunit"));
+	      return -1;
+	    }
+	}
+    }
+
+  blockvector *bv = allocate_blockvector (&objf->objfile_obstack,
+					  FIRST_LOCAL_BLOCK, capacity);
+  compunit_symtab *cu = allocate_compunit_symtab (objf, filename);
+  cu->set_dirname (nullptr);
+  cu->set_blockvector (bv);
+
+  /* Allocate global block. */
+  global_block *gb = new (&objf->objfile_obstack) global_block ();
+  gb->set_multidict (mdict_create_linear_expandable (language_minimal));
+  gb->set_start ((CORE_ADDR) start);
+  gb->set_end ((CORE_ADDR) end);
+  gb->set_compunit (cu);
+  bv->set_block (GLOBAL_BLOCK, gb);
+
+  /* Allocate static block.  */
+  struct block *sb = new (&objf->objfile_obstack) block ();
+  sb->set_multidict (mdict_create_linear_expandable (language_minimal));
+  sb->set_start ((CORE_ADDR) start);
+  sb->set_end ((CORE_ADDR) end);
+  sb->set_superblock (gb);
+  bv->set_block (STATIC_BLOCK, sb);
+
+  add_compunit_symtab_to_objfile (cu);
+
+  set_compunit(self, cu);
+
+  return 0;
+}
+
 /* Return a new reference to gdb.Compunit Python object representing
    COMPUNIT.  Return NULL and set the Python error on failure.  */
 PyObject *
@@ -306,7 +406,7 @@ PyTypeObject compunit_object_type = {
   0,				  /* tp_descr_get */
   0,				  /* tp_descr_set */
   0,				  /* tp_dictoffset */
-  0,				  /* tp_init */
+  cupy_init,                      /* tp_init */
   0,				  /* tp_alloc */
   PyType_GenericNew		  /* tp_new */
 };
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index dc43e2c9e7d..170a440a0dd 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -826,6 +826,18 @@ objfile_to_objfile_object (struct objfile *objfile)
   return gdbpy_ref<>::new_reference (result);
 }
 
+/* Returns the struct objfile value corresponding to the given Python
+   objfile object OBJ.  Returns NULL if OBJ is not an objfile object.  */
+
+struct objfile *
+objfile_object_to_objfile (PyObject *obj)
+{
+  if (! PyObject_TypeCheck (obj, &objfile_object_type))
+    return nullptr;
+
+  return ((objfile_object *)obj)->objfile;
+}
+
 /* This function remove any dynamic objfiles left over when the
    inferior exits.  */
 
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index c9405d923fc..6ebb5e90fb3 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -575,6 +575,7 @@ frame_info_ptr frame_object_to_frame_info (PyObject *frame_obj);
 struct gdbarch *arch_object_to_gdbarch (PyObject *obj);
 struct compunit_symtab *compunit_object_to_compunit (PyObject *obj);
 inferior *inferior_object_to_inferior(PyObject *obj);
+struct objfile *objfile_object_to_objfile(PyObject *obj);
 
 extern PyObject *gdbpy_execute_mi_command (PyObject *self, PyObject *args,
 					   PyObject *kw);
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 95094eea87e..01867acf476 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -555,6 +555,69 @@ compunit_symtab::maybe_contains (CORE_ADDR addr) const
 
 /* See symtab.h.  */
 
+bool
+compunit_symtab::maybe_overlaps (CORE_ADDR start, CORE_ADDR end) const
+{
+  if (blockvector ()->global_block ()->overlaps (start, end))
+    {
+      const addrmap_fixed *map = blockvector ()->map ();
+      if (map != nullptr)
+	{
+	  CORE_ADDR range_start = 0;
+	  auto fn = [&](CORE_ADDR addr, const void* b) -> int
+	  {
+	    if (range_start != 0)
+	      {
+		/* We're currently "inside" a range.  This transition means
+		   that either:
+
+		    (i)  The current range ends (b == nullptr). In that case
+			 check for overlap and if there's an overlap, return 1
+			 and finish.
+
+		   (ii)  Or the range continues into another block. In that
+			 case, just continue.  */
+
+		if (b == nullptr)
+		  {
+		    CORE_ADDR range_end = addr;
+		    bool overlaps = ranges_overlap (start, end,
+						    range_start, range_end);
+		    range_start = 0;
+		    return overlaps;
+		  }
+		else
+		  {
+		    return 0; /* continue iterating */
+		  }
+	      }
+	    else
+	      {
+		/* We're "outside" the range.  This transition means that
+		   either:
+
+		    (i)  This is a start of a new range (b != nullptr). In
+			 this case, just note the start address (which also
+			 indicates we're "inside" a range from now on).
+		   (ii)  This the very a beggining of address space. In tha
+			 case do nothing.  */
+		if (b != nullptr)
+		  {
+		    range_start = addr;
+		  }
+		return 0; /* continue iterating */
+	      }
+
+	  };
+	  return map->foreach (fn);
+	}
+      return true;
+    }
+  return false;
+}
+
+/* See symtab.h.  */
+
 void
 compunit_symtab::finalize ()
 {
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 9e06a105228..b183e6fe590 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -1968,6 +1968,12 @@ struct compunit_symtab
      definitely is not.  */
   bool maybe_contains (CORE_ADDR addr) const;
 
+  /* True, if given address range [START, END) may overlap with
+     addresses covered by this compunit.  Return false if given
+     range definitely does not overlap.  */
+
+  bool maybe_overlaps (CORE_ADDR start, CORE_ADDR end) const;
+
   /* Unordered chain of all compunit symtabs of this objfile.  */
   struct compunit_symtab *next;
 
diff --git a/gdb/testsuite/gdb.python/py-compunit.exp b/gdb/testsuite/gdb.python/py-compunit.exp
index 9e5c4dc6e03..9f8b4cbe453 100644
--- a/gdb/testsuite/gdb.python/py-compunit.exp
+++ b/gdb/testsuite/gdb.python/py-compunit.exp
@@ -71,6 +71,50 @@ gdb_test "python print (objfile.compunits()\[0\].symtabs)" \
     "Compunit symtabs return a sequence of gdb.Symtab"
 
 
+# Test creation of compunits
+gdb_py_test_silent_cmd "python cu = gdb.Compunit(\"compunit1\", objfile, 200, 300)" \
+    "Create compunit1" 1
+gdb_test "python print(cu)" \
+    "<gdb\.Compunit object at .*>" \
+    "Print created compunit1"
+gdb_test "python print(cu in objfile.compunits())" \
+    "True" \
+    "Test compunit1 is in objfile.compunits()"
+gdb_test "python print(cu.global_block().start)" \
+    "0" \
+    "Test compunit1.global_block().start is 0"
+gdb_test "python print(cu.global_block().end)" \
+    "0" \
+    "Test compunit1.global_block().end is 0"
+gdb_test "python print(cu.static_block().start)" \
+    "0" \
+    "Test compunit1.static_block().start is 0"
+gdb_test "python print(cu.static_block().end)" \
+    "0" \
+    "Test compunit1.static_block().end is 0"
+
+gdb_py_test_silent_cmd "python cu2 = gdb.Compunit(\"dynamic2\", objfile, 400, 500, 24)" \
+    "Create compunit2 with capacity fo 24 blocks" 1
+
+gdb_test "python cu3 = gdb.Compunit(\"dynamic3\", gdb, 600, 700)" \
+    "TypeError.*:.*" \
+    "Create compunit3 passing non-objfile"
+
+gdb_test "python cu4 = gdb.Compunit(\"dynamic4\", objfile, 900, 800)" \
+    "ValueError.*:.*" \
+    "Create compunit4 passing invalid global block range"
+
+gdb_test "python cu5 = gdb.Compunit(\"dynamic5\", objfile, 225, 325)" \
+    "ValueError.*:.*" \
+    "Create compunit5 passing overlapping global block range"
+
+gdb_py_test_silent_cmd "python cu6 = gdb.Compunit(\"dynamic6\", objfile, 0x00EFFF00, 0x00EFFF0A)" \
+    "Create compunit6 'inside a hole' of existing compunit spanning over multiple disjoint ranges" 1
+
+gdb_test "python cu7 = gdb.Compunit(\"dynamic7\", objfile, 0x00F00000, 0x00F0000F)" \
+    "ValueError.*:.*" \
+    "Create compunit7 overlapping one of the existing compunit ranges"
+
 gdb_unload "unload 1"
 
 gdb_test "python print (objfile.is_valid())" "False" \
@@ -81,3 +125,6 @@ gdb_py_test_silent_cmd "python compunit = None" \
 "Test compunit deallocation" 1
 gdb_test "python print (objfile.compunits())" "RuntimeError.*: Objfile no longer exists.*" \
 "Get objfile compunits after unload"
+gdb_test "python cu8 = gdb.Compunit(\"dynamic8\", objfile, 1000, 1100)" \
+    "TypeError.*: The objfile argument is not a valid gdb.Objfile object.*" \
+    "Create compunit8 passing invalid objfile"
-- 
2.47.2



More information about the Gdb-patches mailing list