[RFC,v2,19/21] gdb/python: allow instantiation of gdb.LineTableEntry objects

Message ID 20241121124714.419946-20-jan.vrany@labware.com
State New
Headers
Series Add Python "JIT" API |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Skipped upon request
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Skipped upon request

Commit Message

Jan Vraný Nov. 21, 2024, 12:47 p.m. UTC
  Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
 gdb/doc/python.texi                       |  8 ++++
 gdb/python/py-linetable.c                 | 52 +++++++++++++++++++----
 gdb/testsuite/gdb.python/py-linetable.exp | 25 +++++++++++
 3 files changed, 77 insertions(+), 8 deletions(-)
  

Comments

Eli Zaretskii Nov. 21, 2024, 1:27 p.m. UTC | #1
> From: Jan Vrany <jan.vrany@labware.com>
> CC: Jan Vrany <jan.vrany@labware.com>,
> 	Eli Zaretskii <eliz@gnu.org>
> Date: Thu, 21 Nov 2024 12:47:12 +0000
> 
> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
> ---
>  gdb/doc/python.texi                       |  8 ++++
>  gdb/python/py-linetable.c                 | 52 +++++++++++++++++++----
>  gdb/testsuite/gdb.python/py-linetable.exp | 25 +++++++++++
>  3 files changed, 77 insertions(+), 8 deletions(-)

OK for the documentation part, thanks.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  

Patch

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 9fe7e182bdb..277d26c6376 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -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
diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
index 8b6806a2484..83a32389132 100644
--- a/gdb/python/py-linetable.c
+++ b/gdb/python/py-linetable.c
@@ -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 */
 };
diff --git a/gdb/testsuite/gdb.python/py-linetable.exp b/gdb/testsuite/gdb.python/py-linetable.exp
index 0f591e1ff99..e14a3bc1a0d 100644
--- a/gdb/testsuite/gdb.python/py-linetable.exp
+++ b/gdb/testsuite/gdb.python/py-linetable.exp
@@ -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"