]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: allow instantiation of gdb.LineTableEntry objects
authorJan Vrany <jan.vrany@labware.com>
Thu, 21 Nov 2024 12:31:21 +0000 (12:31 +0000)
committerJan Vrany <jan.vrany@labware.com>
Thu, 21 Nov 2024 13:52:21 +0000 (13:52 +0000)
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
gdb/doc/python.texi
gdb/python/py-linetable.c
gdb/testsuite/gdb.python/py-linetable.exp

index f1b80373f3bc751ee580f0edbaa36d4aa092674f..7d7441b66caee4d2bb968aea7f4235b3f4afd719 100644 (file)
@@ -6691,6 +6691,14 @@ True if pc (associated with this entry) marks the start of the epilogue.
 This attribute is not writable.
 @end defvar
 
+@code{LineTableEntry} objects have the following methods:
+
+@defun LineTableEntry.__init__ (line, pc@r{[}, is_stmt@r{][}, prologue_end@r{][}, epilogue_begin@r{]})
+Create new line table entry. Arguments correspond to @code{LineTableEntry}
+attributes described above.  Optional arguments @var{is_stmt},
+@var{prologue_end} and @var{epilogue_begin} default to @code{False}.
+@end defun
+
 As there can be multiple addresses for a single source line, you may
 receive multiple @code{LineTableEntry} objects with matching
 @code{line} attributes, but with different @code{pc} attributes.  The
index 8b6806a2484c85dae2955951320e783b2dc3dfb3..83a323891325bb5bcbf16e966f30369a17c49f60 100644 (file)
@@ -146,7 +146,7 @@ build_line_table_tuple_from_entries (
 
   for (i = 0; i < entries.size (); ++i)
     {
-      auto entry = entries[i];
+      const linetable_entry *entry = entries[i];
       gdbpy_ref<> obj (build_linetable_entry (
                        entry->line, entry->pc (objfile), entry->is_stmt,
                        entry->prologue_end, entry->epilogue_begin));
@@ -390,6 +390,41 @@ ltpy_entry_get_epilogue_begin (PyObject *self, void *closure)
     Py_RETURN_FALSE;
 }
 
+/* Object initializer; creates new linetable entry.
+
+   Use: __init__(LINE, PC, IS_STMT, PROLOGUE_END, EPILOGUE_BEGIN).  */
+
+static int
+ltpy_entry_init (PyObject *zelf, PyObject *args, PyObject *kw)
+{
+  linetable_entry_object *self = (linetable_entry_object *) zelf;
+
+   static const char *keywords[] = { "line", "pc", "is_stmt", "prologue_end",
+                                    "epilogue_begin", nullptr };
+   int line = 0;
+   CORE_ADDR pc = 0;
+   int is_stmt = 0;
+   int prologue_end = 0;
+   int epilogue_begin = 0;
+
+   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "iK|ppp",
+         keywords,
+         &line,
+         &pc,
+         &is_stmt,
+         &prologue_end,
+         &epilogue_begin))
+    return -1;
+
+   self->line = line;
+   self->pc = pc;
+   self->is_stmt = is_stmt == 1 ? true : false;
+   self->prologue_end = prologue_end == 1 ? true : false;
+   self->epilogue_begin = epilogue_begin == 1 ? true : false;
+
+   return 0;
+}
+
 /* LineTable iterator functions.  */
 
 /* Return a new line table iterator.  */
@@ -604,12 +639,12 @@ static gdb_PyGetSetDef linetable_entry_object_getset[] = {
     "The line number in the source file.", NULL },
   { "pc", ltpy_entry_get_pc, NULL,
     "The memory address for this line number.", NULL },
-  { "is_stmt", ltpy_entry_get_is_stmt, NULL,
-    "Whether this is a good location to place a breakpoint for associated LINE.", NULL },
-  { "prologue_end", ltpy_entry_get_prologue_end, NULL,
-    "Whether this is a good location to place a breakpoint after method prologue.", NULL },
-  { "epilogue_begin", ltpy_entry_get_epilogue_begin, NULL,
-    "True if this location marks the start of the epilogue.", NULL },
+  { "is_stmt", ltpy_entry_get_is_stmt, nullptr,
+    "Whether this is a good location to place a breakpoint for associated LINE.", nullptr },
+  { "prologue_end", ltpy_entry_get_prologue_end, nullptr,
+    "Whether this is a good location to place a breakpoint after method prologue.", nullptr },
+  { "epilogue_begin", ltpy_entry_get_epilogue_begin, nullptr,
+    "True if this location marks the start of the epilogue.", nullptr },
   { NULL }  /* Sentinel */
 };
 
@@ -650,6 +685,7 @@ PyTypeObject linetable_entry_object_type = {
   0,                             /* tp_descr_get */
   0,                             /* tp_descr_set */
   0,                             /* tp_dictoffset */
-  0,                             /* tp_init */
+  ltpy_entry_init,               /* tp_init */
   0,                             /* tp_alloc */
+  PyType_GenericNew,             /* tp_new */
 };
index 0f591e1ff9913c3c89f3c6096a2dfce6c4d0c6f6..e14a3bc1a0dd6621ba56ce2f633dccaa872630da 100644 (file)
@@ -81,3 +81,28 @@ gdb_test "python print(lt.has_line(44))" \
 gdb_test "python print(lt.has_line(10))" \
     "False.*" \
     "test has_pcs at line 10"
+
+# Test gdb.LineTableEntry.__init__ ()
+gdb_test "python print( gdb.LineTableEntry(10, 0xcafe0000).line)" \
+    "10" \
+    "test new LineTableEntry line"
+
+gdb_test "python print( gdb.LineTableEntry(10, 123456).pc)" \
+    "123456" \
+    "test new LineTableEntry pc"
+
+gdb_test "python print( gdb.LineTableEntry(10, 123456).is_stmt)" \
+    "False" \
+    "test new LineTableEntry is_stmt"
+gdb_test "python print( gdb.LineTableEntry(10, 123456).prologue_end)" \
+    "False" \
+    "test new LineTableEntry prologue_end"
+gdb_test "python print( gdb.LineTableEntry(10, 123456).epilogue_begin)" \
+    "False" \
+    "test new LineTableEntry epilogue_begin"
+gdb_test "python print( gdb.LineTableEntry('xx', 123456).pc)" \
+    "TypeError.*:.*" \
+    "test creating invalid gdb.LineTableEntry"
+gdb_test "python print( gdb.LineTableEntry(10, 123456, prologue_end = True).prologue_end)" \
+    "True" \
+    "test prologue_end keyword argument"