public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add gdb.Objfile.is_file attribute
@ 2022-06-20 18:39 Tom Tromey
  2022-06-20 18:47 ` Eli Zaretskii
  2022-06-21 11:25 ` Andrew Burgess
  0 siblings, 2 replies; 3+ messages in thread
From: Tom Tromey @ 2022-06-20 18:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Sometimes an objfile comes from memory and not from a file.  It can be
useful to be able to check this from Python, so this patch adds a new
"is_file" attribute.
---
 gdb/NEWS                              |  3 +++
 gdb/doc/python.texi                   |  7 +++++++
 gdb/python/py-objfile.c               | 14 ++++++++++++++
 gdb/testsuite/gdb.base/jit-reader.exp | 12 ++++++++++++
 4 files changed, 36 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 5576c355b7a..e1bb468e76c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -106,6 +106,9 @@ GNU/Linux/LoongArch (gdbserver)	loongarch*-*-linux*
        to wrap the result of a call to a Disassembler.  It has
        read-only attributes 'length' and 'string'.
 
+  ** gdb.Objfile now has an attribute named "is_file".  This is True
+     if the objfile comes from a file, and False otherwise.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on LoongArch GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 9e2e97b1e74..19ae33012c3 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4891,6 +4891,13 @@ The value is @code{None} if the objfile is no longer valid.
 See the @code{gdb.Objfile.is_valid} method, described below.
 @end defvar
 
+@defvar Objfile.is_file
+An objfile often comes from an ordinary file, but in some cases it may
+be constructed from the contents of memory.  This attribute is
+@code{True} for file-backed objfiles, and @code{False} for other
+kinds.
+@end defvar
+
 @defvar Objfile.owner
 For separate debug info objfiles this is the corresponding @code{gdb.Objfile}
 object that debug info is being provided for.
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 3e3270e7cd3..298f3f23632 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -101,6 +101,18 @@ objfpy_get_username (PyObject *self, void *closure)
   Py_RETURN_NONE;
 }
 
+/* Get the 'is_file' attribute.  */
+
+static PyObject *
+objfpy_get_is_file (PyObject *o, void *ignore)
+{
+  objfile_object *self = (objfile_object *) o;
+
+  if (self->objfile != nullptr)
+    return PyBool_FromLong ((self->objfile->flags & OBJF_NOT_FILENAME) == 0);
+  Py_RETURN_NONE;
+}
+
 /* If SELF is a separate debug-info file, return the "backlink" field.
    Otherwise return None.  */
 
@@ -762,6 +774,8 @@ static gdb_PyGetSetDef objfile_getset[] =
     "Type printers.", NULL },
   { "xmethods", objfpy_get_xmethods, NULL,
     "Debug methods.", NULL },
+  { "is_file", objfpy_get_is_file, nullptr,
+    "Whether this objfile came from a file.", nullptr },
   { NULL }
 };
 
diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base/jit-reader.exp
index d94360cd7d9..117e9fe0e34 100644
--- a/gdb/testsuite/gdb.base/jit-reader.exp
+++ b/gdb/testsuite/gdb.base/jit-reader.exp
@@ -13,6 +13,9 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+# Optionally test a Python API here as well.
+load_lib gdb-python.exp
+
 standard_testfile jit-reader-host.c
 
 if { (![istarget x86_64-*-*] && ![istarget i?86-*-*]) || ![is_lp64_target] } {
@@ -271,6 +274,15 @@ proc jit_reader_test {} {
 		 "#1 ${any} in main ${any}" \
 		]
     }
+
+    if {![skip_python_tests]} {
+	gdb_test "python print(any(\[not x.is_file for x in gdb.objfiles()\]))" \
+	    "True" \
+	    "at least one non-file objfile"
+	gdb_test "python print(any(\[x.is_file for x in gdb.objfiles()\]))" \
+	    "True" \
+	    "at least one file-based objfile"
+    }
 }
 
 jit_reader_test
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Add gdb.Objfile.is_file attribute
  2022-06-20 18:39 [PATCH] Add gdb.Objfile.is_file attribute Tom Tromey
@ 2022-06-20 18:47 ` Eli Zaretskii
  2022-06-21 11:25 ` Andrew Burgess
  1 sibling, 0 replies; 3+ messages in thread
From: Eli Zaretskii @ 2022-06-20 18:47 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> Date: Mon, 20 Jun 2022 12:39:40 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Tom Tromey <tromey@adacore.com>
> 
> Sometimes an objfile comes from memory and not from a file.  It can be
> useful to be able to check this from Python, so this patch adds a new
> "is_file" attribute.
> ---
>  gdb/NEWS                              |  3 +++
>  gdb/doc/python.texi                   |  7 +++++++
>  gdb/python/py-objfile.c               | 14 ++++++++++++++
>  gdb/testsuite/gdb.base/jit-reader.exp | 12 ++++++++++++
>  4 files changed, 36 insertions(+)

OK for the documentation parts, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Add gdb.Objfile.is_file attribute
  2022-06-20 18:39 [PATCH] Add gdb.Objfile.is_file attribute Tom Tromey
  2022-06-20 18:47 ` Eli Zaretskii
@ 2022-06-21 11:25 ` Andrew Burgess
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2022-06-21 11:25 UTC (permalink / raw)
  To: Tom Tromey via Gdb-patches, gdb-patches; +Cc: Tom Tromey

Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

> Sometimes an objfile comes from memory and not from a file.  It can be
> useful to be able to check this from Python, so this patch adds a new
> "is_file" attribute.

LGTM.

Thanks,
Andrew

> ---
>  gdb/NEWS                              |  3 +++
>  gdb/doc/python.texi                   |  7 +++++++
>  gdb/python/py-objfile.c               | 14 ++++++++++++++
>  gdb/testsuite/gdb.base/jit-reader.exp | 12 ++++++++++++
>  4 files changed, 36 insertions(+)
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 5576c355b7a..e1bb468e76c 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -106,6 +106,9 @@ GNU/Linux/LoongArch (gdbserver)	loongarch*-*-linux*
>         to wrap the result of a call to a Disassembler.  It has
>         read-only attributes 'length' and 'string'.
>  
> +  ** gdb.Objfile now has an attribute named "is_file".  This is True
> +     if the objfile comes from a file, and False otherwise.
> +
>  * New features in the GDB remote stub, GDBserver
>  
>    ** GDBserver is now supported on LoongArch GNU/Linux.
> diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
> index 9e2e97b1e74..19ae33012c3 100644
> --- a/gdb/doc/python.texi
> +++ b/gdb/doc/python.texi
> @@ -4891,6 +4891,13 @@ The value is @code{None} if the objfile is no longer valid.
>  See the @code{gdb.Objfile.is_valid} method, described below.
>  @end defvar
>  
> +@defvar Objfile.is_file
> +An objfile often comes from an ordinary file, but in some cases it may
> +be constructed from the contents of memory.  This attribute is
> +@code{True} for file-backed objfiles, and @code{False} for other
> +kinds.
> +@end defvar
> +
>  @defvar Objfile.owner
>  For separate debug info objfiles this is the corresponding @code{gdb.Objfile}
>  object that debug info is being provided for.
> diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
> index 3e3270e7cd3..298f3f23632 100644
> --- a/gdb/python/py-objfile.c
> +++ b/gdb/python/py-objfile.c
> @@ -101,6 +101,18 @@ objfpy_get_username (PyObject *self, void *closure)
>    Py_RETURN_NONE;
>  }
>  
> +/* Get the 'is_file' attribute.  */
> +
> +static PyObject *
> +objfpy_get_is_file (PyObject *o, void *ignore)
> +{
> +  objfile_object *self = (objfile_object *) o;
> +
> +  if (self->objfile != nullptr)
> +    return PyBool_FromLong ((self->objfile->flags & OBJF_NOT_FILENAME) == 0);
> +  Py_RETURN_NONE;
> +}
> +
>  /* If SELF is a separate debug-info file, return the "backlink" field.
>     Otherwise return None.  */
>  
> @@ -762,6 +774,8 @@ static gdb_PyGetSetDef objfile_getset[] =
>      "Type printers.", NULL },
>    { "xmethods", objfpy_get_xmethods, NULL,
>      "Debug methods.", NULL },
> +  { "is_file", objfpy_get_is_file, nullptr,
> +    "Whether this objfile came from a file.", nullptr },
>    { NULL }
>  };
>  
> diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base/jit-reader.exp
> index d94360cd7d9..117e9fe0e34 100644
> --- a/gdb/testsuite/gdb.base/jit-reader.exp
> +++ b/gdb/testsuite/gdb.base/jit-reader.exp
> @@ -13,6 +13,9 @@
>  # You should have received a copy of the GNU General Public License
>  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
>  
> +# Optionally test a Python API here as well.
> +load_lib gdb-python.exp
> +
>  standard_testfile jit-reader-host.c
>  
>  if { (![istarget x86_64-*-*] && ![istarget i?86-*-*]) || ![is_lp64_target] } {
> @@ -271,6 +274,15 @@ proc jit_reader_test {} {
>  		 "#1 ${any} in main ${any}" \
>  		]
>      }
> +
> +    if {![skip_python_tests]} {
> +	gdb_test "python print(any(\[not x.is_file for x in gdb.objfiles()\]))" \
> +	    "True" \
> +	    "at least one non-file objfile"
> +	gdb_test "python print(any(\[x.is_file for x in gdb.objfiles()\]))" \
> +	    "True" \
> +	    "at least one file-based objfile"
> +    }
>  }
>  
>  jit_reader_test
> -- 
> 2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-21 11:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-20 18:39 [PATCH] Add gdb.Objfile.is_file attribute Tom Tromey
2022-06-20 18:47 ` Eli Zaretskii
2022-06-21 11:25 ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).