From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id BE0A43858280; Tue, 5 Jul 2022 17:35:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE0A43858280 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Add gdb.Objfile.is_file attribute X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 3acd9a692ddaf8f24d6d34cb5ccb7c26d057e9b3 X-Git-Newrev: 99298c958c5393402cd2bc2885c195838a9dd363 Message-Id: <20220705173531.BE0A43858280@sourceware.org> Date: Tue, 5 Jul 2022 17:35:31 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jul 2022 17:35:31 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D99298c958c53= 93402cd2bc2885c195838a9dd363 commit 99298c958c5393402cd2bc2885c195838a9dd363 Author: Tom Tromey Date: Mon Jun 20 12:32:52 2022 -0600 Add gdb.Objfile.is_file attribute =20 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. Diff: --- 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 c4f4a02a7c0..1178a37017e 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -118,6 +118,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'. =20 + ** 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 =20 ** 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 =20 +@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.Objfi= le} 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; } =20 +/* Get the 'is_file' attribute. */ + +static PyObject * +objfpy_get_is_file (PyObject *o, void *ignore) +{ + objfile_object *self =3D (objfile_object *) o; + + if (self->objfile !=3D nullptr) + return PyBool_FromLong ((self->objfile->flags & OBJF_NOT_FILENAME) =3D= =3D 0); + Py_RETURN_NONE; +} + /* If SELF is a separate debug-info file, return the "backlink" field. Otherwise return None. */ =20 @@ -762,6 +774,8 @@ static gdb_PyGetSetDef objfile_getset[] =3D "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 } }; =20 diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base= /jit-reader.exp index e0b3ba42115..69bf721d90a 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 . =20 +# Optionally test a Python API here as well. +load_lib gdb-python.exp + standard_testfile jit-reader-host.c =20 if { (![istarget x86_64-*-*] && ![istarget i?86-*-*]) || ![is_lp64_target]= } { @@ -265,6 +268,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" + } } =20 jit_reader_test