Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>, Eli Zaretskii <eliz@gnu.org>
Subject: [RFC v5 12/18] gdb/python: allow instantiation of gdb.Block from Python
Date: Mon, 23 Jun 2025 17:10:07 +0100	[thread overview]
Message-ID: <20250623161013.650814-13-jan.vrany@labware.com> (raw)
In-Reply-To: <20250623161013.650814-1-jan.vrany@labware.com>

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

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
 gdb/doc/python.texi                   |   6 ++
 gdb/python/py-block.c                 | 111 +++++++++++++++++++++++++-
 gdb/testsuite/gdb.python/py-block.exp |  23 ++++++
 3 files changed, 138 insertions(+), 2 deletions(-)

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 65e243074ff..7f50615d151 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6174,6 +6174,12 @@ historical compatibility.
 
 A @code{gdb.Block} object has the following methods:
 
+@defun Block.__init__ (superblock, start, end)
+Create new block in @var{superblock} spanning from @var{start} to @var{end}.
+The new block's @var{start}--@var{end} range must be within superblock's
+range and must not overlap with any block already contained in superblock.
+@end defun
+
 @defun Block.is_valid ()
 Returns @code{True} if the @code{gdb.Block} object is valid,
 @code{False} if not.  A block object can become invalid if the block it
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 89315ebf866..43b15b6013e 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -344,6 +344,106 @@ blpy_dealloc (PyObject *obj)
   Py_TYPE (obj)->tp_free (obj);
 }
 
+/* Object initializer; creates new block.
+
+   Use: __init__(SUPERBLOCK, START, END).  */
+
+static int
+blpy_init (PyObject *zelf, PyObject *args, PyObject *kw)
+{
+  struct block_object *self = (struct block_object*) zelf;
+
+  if (self->block)
+    {
+      PyErr_Format (PyExc_RuntimeError,
+		    _("Block object already initialized."));
+      return -1;
+    }
+
+  static const char *keywords[] = { "superblock", "start", "end", NULL };
+  PyObject *superblock_obj;
+  uint64_t start;
+  uint64_t end;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OKK", keywords,
+					 &superblock_obj, &start, &end))
+    return -1;
+
+
+  auto superblock = block_object_to_block (superblock_obj);
+  if (superblock == nullptr)
+    {
+      PyErr_Format (PyExc_TypeError,
+		    _("The superblock argument is not valid gdb.Block "
+		      "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 start-end range is within superblocks' range.  */
+  if (! (superblock-> start() <= start && end <= superblock->end ()))
+    {
+      PyErr_Format (PyExc_ValueError,
+		    _("The start-end range must be within superblocks' "
+		      "range"));
+      return -1;
+    }
+
+  /* Check that start-end range does not overlap with any
+     "sibling" blocks' range.  */
+  auto cu = superblock->global_block ()->compunit ();
+
+  for (auto each : cu->blockvector ()->blocks ())
+    {
+      if (each->superblock () == superblock)
+	{
+	  /* each is a "sibling" block. */
+	  if (each->overlaps (start, end))
+	    {
+	      PyErr_Format (PyExc_ValueError,
+		    _("The start-end range overlaps with one of the "
+		      "sibling blocks"));
+	      return -1;
+	    }
+	}
+    }
+
+  auto obstack = &(cu->objfile ()->objfile_obstack);
+  auto blk = new (obstack) block ();
+
+  blk->set_superblock (superblock);
+  blk->set_multidict (mdict_create_linear (obstack, NULL));
+  blk->set_start ((CORE_ADDR) start);
+  blk->set_end ((CORE_ADDR) end);
+
+  cu->blockvector ()->add_block (blk);
+
+  self->block = blk;
+  self->objfile = cu->objfile ();
+
+  htab_t table = blpy_objfile_data_key.get (self->objfile);
+  if (table == nullptr)
+    {
+      table = htab_create_alloc (10, block_object_hash, block_object_eq,
+				 block_object_del, xcalloc, xfree);
+      blpy_objfile_data_key.set (self->objfile, table);
+    }
+  hashval_t hash = htab_hash_pointer (blk);
+  void **slot = htab_find_slot_with_hash (table, blk, hash, INSERT);
+  *slot = self;
+
+  return 0;
+}
+
 /* Create a new block object (gdb.Block) that encapsulates the struct
    block object from GDB.  */
 PyObject *
@@ -535,7 +635,6 @@ blpy_richcompare (PyObject *self, PyObject *other, int op)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_blocks (void)
 {
-  block_object_type.tp_new = PyType_GenericNew;
   if (gdbpy_type_ready (&block_object_type) < 0)
     return -1;
 
@@ -615,7 +714,15 @@ PyTypeObject block_object_type = {
   0,				  /* tp_iternext */
   block_object_methods,		  /* tp_methods */
   0,				  /* tp_members */
-  block_object_getset		  /* tp_getset */
+  block_object_getset,		  /* tp_getset */
+  0,				  /* tp_base */
+  0,				  /* tp_dict */
+  0,				  /* tp_descr_get */
+  0,				  /* tp_descr_set */
+  0,                              /* tp_dictoffset */
+  blpy_init,                      /* tp_init */
+  0,				  /* tp_alloc */
+  PyType_GenericNew		  /* tp_new */
 };
 
 static PyMethodDef block_iterator_object_methods[] = {
diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.python/py-block.exp
index 11c2de8fc6f..afa306be00f 100644
--- a/gdb/testsuite/gdb.python/py-block.exp
+++ b/gdb/testsuite/gdb.python/py-block.exp
@@ -115,6 +115,29 @@ gdb_test "python print (repr (block))" "<gdb.Block main \{.*\}>" \
          "Check Frame 2's block not None"
 gdb_test "python print (block.function)" "main" "main block"
 
+# Test creation of blocks. For that we create a new compunit to make sure
+# there's space for new blocks to fit in.
+gdb_py_test_silent_cmd "python cu = gdb.Compunit(\"dynamic\", gdb.current_progspace().objfiles()\[0\], 100, 200)" \
+		       "Create new compunit" 1
+gdb_test "python print ( gdb.Block(cu.static_block(), 100, 150))" \
+	 "<gdb.Block <anonymous> \{.*\}>" \
+	 "Create new block"
+gdb_test "python print ( gdb.Block(\"xxx\", 160, 170))" \
+	 "TypeError.*:.*" \
+	 "Try create new block with non-block superblock"
+gdb_test "python print ( gdb.Block(cu.static_block(), 170, 160))" \
+	 "ValueError.*:.*" \
+	 "Try create new block with start > end"
+gdb_test "python print ( gdb.Block(cu.static_block(), 70, 160))" \
+	 "ValueError.*:.*" \
+	 "Try create new block with outside superblock"
+gdb_test "python print ( gdb.Block(cu.static_block(), 140, 160))" \
+	 "ValueError.*:.*" \
+	 "Try create new block overlaping with sibling"
+gdb_test "python print ( gdb.Block(cu.static_block(), 160, 170))" \
+	 "<gdb.Block <anonymous> \{.*\}>" \
+	 "Create sibling block"
+
 # Test Block is_valid.  This must always be the last test in this
 # testcase as it unloads the object file.
 delete_breakpoints
-- 
2.47.2


  parent reply	other threads:[~2025-06-23 16:16 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23 16:09 [RFC v5 00/19] Add Python "JIT" API Jan Vrany
2025-06-23 16:09 ` [RFC v5 01/18] gdb: introduce expand_symtabs_maybe_overlapping Jan Vrany
2025-06-24 15:22   ` Tom Tromey
2025-06-26 15:05     ` Jan Vraný
2025-06-23 16:09 ` [RFC v5 02/18] gdb: introduce compunit_symtab::maybe_contains Jan Vrany
2025-06-23 16:09 ` [RFC v5 03/18] gdb: update is_addr_in_objfile to support "dynamic" objfiles Jan Vrany
2025-06-23 16:09 ` [RFC v5 04/18] gdb: introduce new function create_function_type Jan Vrany
2025-06-24 15:29   ` Tom Tromey
2025-06-26 11:12     ` Jan Vraný
2025-06-27 14:21       ` Tom Tromey
2025-06-27 14:30         ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 05/18] gdb/python: add function () method to gdb.Type object Jan Vrany
2025-06-24 16:11   ` Tom Tromey
2025-06-26 11:13     ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 06/18] gdb: use std::vector<> to hold on blocks in struct blockvector Jan Vrany
2025-06-23 16:10 ` [RFC v5 07/18] gdb/python: add gdb.Compunit Jan Vrany
2025-06-23 16:10 ` [RFC v5 08/18] gdb/python: allow instantiation of gdb.Objfile from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 09/18] gdb/python: add unlink () method to gdb.Objfile object Jan Vrany
2025-06-23 16:10 ` [RFC v5 10/18] gdb/python: allow instantiation of gdb.Compunit from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 11/18] gdb/python: allow instantiation of gdb.Symtab " Jan Vrany
2025-06-23 16:10 ` Jan Vrany [this message]
2025-06-23 16:10 ` [RFC v5 13/18] gdb/python: allow instantiation of gdb.Symbol " Jan Vrany
2025-06-23 16:10 ` [RFC v5 14/18] gdb/python: add add_symbol () method to gdb.Block Jan Vrany
2025-08-29 14:10   ` Andrew Burgess
2025-08-29 14:14     ` Andrew Burgess
2025-06-23 16:10 ` [RFC v5 15/18] gdb/python: add more attributes to gdb.LinetableEntry objects Jan Vrany
2025-08-29 14:00   ` Andrew Burgess
2025-09-02 11:03     ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 16/18] gdb/python: allow instantiation of gdb.LineTableEntry objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 17/18] gdb/python: allow instantiation of gdb.LineTable objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 18/18] gdb/python: add section in documentation on implementing JIT interface Jan Vrany

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=20250623161013.650814-13-jan.vrany@labware.com \
    --to=jan.vrany@labware.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