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 10/18] gdb/python: allow instantiation of gdb.Compunit from Python
Date: Mon, 23 Jun 2025 17:10:05 +0100 [thread overview]
Message-ID: <20250623161013.650814-11-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.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 | 9 ++
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 | 64 ++++++++++++++
gdb/symtab.h | 6 ++
gdb/testsuite/gdb.python/py-compunit.exp | 47 +++++++++++
8 files changed, 256 insertions(+), 1 deletion(-)
diff --git a/gdb/block.h b/gdb/block.h
index 89ceeb4268b..3ea8878481e 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -24,6 +24,7 @@
#include "dictionary.h"
#include "gdbsupport/array-view.h"
#include "gdbsupport/next-iterator.h"
+#include "gdbsupport/range.h"
/* Opaque declarations. */
@@ -312,6 +313,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 eac12bec4d3..ffa78c0d859 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -6716,6 +6716,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 a2d7620f483..5346663af19 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 *
@@ -305,7 +405,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 18b78a7d235..7daa58516f1 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -568,6 +568,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 506e21eeac4..ba77c89573e 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -75,6 +75,7 @@
#include "gdbsupport/common-utils.h"
#include <optional>
#include "gdbsupport/unordered_set.h"
+#include "gdbsupport/range.h"
/* Forward declarations for local functions. */
@@ -537,6 +538,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 7f084cef691..e55b06048e3 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -1967,6 +1967,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
next prev parent reply other threads:[~2025-06-23 16:15 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 ` Jan Vrany [this message]
2025-06-23 16:10 ` [RFC v5 11/18] gdb/python: allow instantiation of gdb.Symtab from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 12/18] gdb/python: allow instantiation of gdb.Block " Jan Vrany
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-11-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