[RFC,v2,12/21] gdb/python: add unlink () method to gdb.Objfile object

Message ID 20241121124714.419946-13-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
  This commit adds method allowing one remove any objfile. This is meant
to be used to remove objfiles for dynamic code when this dynamic code
is discarded. However gdb.Objfile.unlink() makes no attempt to ensure
this - to make it consistent with other Python API to create and modify
objfiles related structures (compunits, symbol tables and so on).

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---
 gdb/doc/python.texi                     |  6 ++++++
 gdb/python/py-objfile.c                 | 18 ++++++++++++++++++
 gdb/testsuite/gdb.python/py-objfile.exp | 12 ++++++++++++
 3 files changed, 36 insertions(+)
  

Comments

Eli Zaretskii Nov. 21, 2024, 1:39 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:05 +0000
> 
> This commit adds method allowing one remove any objfile. This is meant
> to be used to remove objfiles for dynamic code when this dynamic code
> is discarded. However gdb.Objfile.unlink() makes no attempt to ensure
> this - to make it consistent with other Python API to create and modify
> objfiles related structures (compunits, symbol tables and so on).
> 
> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
> ---
>  gdb/doc/python.texi                     |  6 ++++++
>  gdb/python/py-objfile.c                 | 18 ++++++++++++++++++
>  gdb/testsuite/gdb.python/py-objfile.exp | 12 ++++++++++++
>  3 files changed, 36 insertions(+)

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 c32d84fb9ed..f48803d08b4 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -5710,6 +5710,12 @@  Return a sequence of all the compunits associated with this objfile.
 @xref{Compunits In Python}.
 @end defun
 
+@defun Objfile.unlink ()
+Remove this objfile.  This should be used only on objfiles created by
+Python (see @code{Objfile.__init__} above) but @code{Objfile.unlink} does
+not make any checks.
+@end defun
+
 @node Frames In Python
 @subsubsection Accessing inferior stack frames from Python
 
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index cc225ca135d..f8b29ae1f47 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -608,6 +608,20 @@  objfpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
   Py_RETURN_NONE;
 }
 
+/* Implementation of gdb.Objfile.unlink ().  */
+
+static PyObject *
+objfpy_unlink (PyObject *self, PyObject *args)
+{
+  objfile_object *obj = (objfile_object *) self;
+
+  OBJFPY_REQUIRE_VALID (obj);
+
+  obj->objfile->unlink();
+
+  Py_RETURN_NONE;
+}
+
 /* Implement repr() for gdb.Objfile.  */
 
 static PyObject *
@@ -871,6 +885,10 @@  Look up a static-linkage global symbol in this objfile and return it." },
     "compunits () -> List.\n\
 Return a sequence of compunits associated to this objfile." },
 
+  { "unlink", objfpy_unlink, METH_NOARGS,
+    "unlink ().\n\
+Remove this objfile." },
+
   { NULL }
 };
 
diff --git a/gdb/testsuite/gdb.python/py-objfile.exp b/gdb/testsuite/gdb.python/py-objfile.exp
index 250ae4763b7..7a88cac663d 100644
--- a/gdb/testsuite/gdb.python/py-objfile.exp
+++ b/gdb/testsuite/gdb.python/py-objfile.exp
@@ -209,3 +209,15 @@  gdb_test "python print( gdb.Objfile(\"Test objfile 4\", gdb))" \
 gdb_test "python print( gdb.Objfile(\"Test objfile 5\", gdb.selected_inferior(), gdb.selected_inferior()))" \
 	"TypeError.*:.*" \
 	"create objfile with valid inferior but invalid arch"
+
+gdb_test "python print(objfile.unlink())" \
+	"None" \
+	"remove (dynamic) objfile"
+
+gdb_test "python print(objfile in gdb.objfiles())" \
+	"False" \
+	"removed (dynamic) objfile no longer in gdb.objfiles()"
+
+gdb_test "python print(objfile.is_valid())" \
+	"False" \
+	"removes (dynamic) objfile is no longer valid"