@@ -6551,7 +6551,7 @@ that objfile. @xref{Objfiles In Python}.
A @code{gdb.Compunit} object has the following attributes:
@defvar Compunit.objfile
-The compunits' backing object file. @xref{Objfiles In Python}.
+The compunit's backing object file. @xref{Objfiles In Python}.
This attribute is not writable.
@end defvar
@@ -6570,6 +6570,22 @@ The list of symbol tables 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
@@ -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"
@@ -217,6 +218,100 @@ set_compunit (compunit_object *obj, struct compunit_symtab *compunit)
obj->next = nullptr;
}
+/* 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 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 compunits' global block does not overlap
+ with existing compunit. */
+
+ for (struct objfile *of : objf->pspace ()->objfiles_safe ())
+ {
+ for (compunit_symtab *cu : of->compunits ())
+ {
+ global_block *gb = cu->blockvector ()->global_block ();
+ if (std::max (start, gb->start ()) < std::min(end, gb->end ()))
+ {
+ PyErr_Format (PyExc_ValueError,
+ _("The start-end range overlaps 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. */
gdbpy_ref<>
@@ -313,7 +408,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 */
};
@@ -824,6 +824,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. */
@@ -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);
@@ -54,9 +54,49 @@ 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 compunit4 passing overlapping global block range"
+
+
gdb_unload "unload 1"
gdb_test "python print (objfile.is_valid())" "False" \
"Get objfile validity after unload"
gdb_test "python print (objfile.compunits())" "RuntimeError.*: Objfile no longer exists.*" \
-"Get objfile compunits after unload"
\ No newline at end of file
+"Get objfile compunits after unload"
+gdb_py_test_silent_cmd "python cu6 = gdb.Compunit(\"dynamic6\", objfile)" \
+ "Create compunit4 passing invalid objfile" 1
\ No newline at end of file