From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1805) id 3A2113858C83; Tue, 18 Oct 2022 13:25:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A2113858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666099513; bh=SJeB3G2lwiieg2L9+wmFYdKlsf3gl+qm9ZcfUn8F9V8=; h=From:To:Subject:Date:From; b=RawNtsBnb22rLVODmifMs4oI9q2hlaV0FNpyNmgdN7p5YnOF1vMvXjWejrIW/sjvx M7WDXkc98+56lwD6omHS7q7CTJMrBv4qpP0mQ1az4DihDsJ+JukptX6ePvW5vP8eGW cwb7TWVL/qdgEWFnoMIkYH7USNFPNR6Y6CxemG/o= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Markus Metzger To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb, python: use gdbarch_iterate_over_objfiles_in_search_order X-Act-Checkin: binutils-gdb X-Git-Author: Markus Metzger X-Git-Refname: refs/heads/master X-Git-Oldrev: fb4f3f38e98599690946cc24b09ae6883a36edb0 X-Git-Newrev: 85933f7c91b668e42ae5ff27f432917328828468 Message-Id: <20221018132513.3A2113858C83@sourceware.org> Date: Tue, 18 Oct 2022 13:25:13 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D85933f7c91b6= 68e42ae5ff27f432917328828468 commit 85933f7c91b668e42ae5ff27f432917328828468 Author: Markus Metzger Date: Mon Apr 11 16:44:36 2022 +0200 gdb, python: use gdbarch_iterate_over_objfiles_in_search_order =20 The implementation of gdb.lookup_objfile() iterates over all objfiles a= nd compares their name or build id to the user-provided search string. =20 This will cause problems when supporting linker namespaces as the first objfile in any namespace will be found. Instead, use gdbarch_iterate_over_objfiles_in_search_order to only consider the namespace of gdb.current_objfile() for the search, which defaults to the initial namespace when gdb.current_objfile() is None. Diff: --- gdb/python/py-objfile.c | 116 +++++++++++++++++++++++---------------------= ---- gdb/python/python.c | 7 +-- gdb/python/python.h | 6 +++ 3 files changed, 64 insertions(+), 65 deletions(-) diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index 757f9aac4fc..c278925531b 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -24,6 +24,7 @@ #include "language.h" #include "build-id.h" #include "symtab.h" +#include "python.h" =20 struct objfile_object { @@ -584,57 +585,6 @@ objfpy_build_id_matches (const struct bfd_build_id *bu= ild_id, return 1; } =20 -/* Subroutine of gdbpy_lookup_objfile to simplify it. - Look up an objfile by its file name. */ - -static struct objfile * -objfpy_lookup_objfile_by_name (const char *name) -{ - for (objfile *objfile : current_program_space->objfiles ()) - { - const char *filename; - - if ((objfile->flags & OBJF_NOT_FILENAME) !=3D 0) - continue; - /* Don't return separate debug files. */ - if (objfile->separate_debug_objfile_backlink !=3D NULL) - continue; - - filename =3D objfile_filename (objfile); - if (filename !=3D NULL && compare_filenames_for_search (filename, na= me)) - return objfile; - if (compare_filenames_for_search (objfile->original_name, name)) - return objfile; - } - - return NULL; -} - -/* Subroutine of gdbpy_lookup_objfile to simplify it. - Look up an objfile by its build id. */ - -static struct objfile * -objfpy_lookup_objfile_by_build_id (const char *build_id) -{ - for (objfile *objfile : current_program_space->objfiles ()) - { - const struct bfd_build_id *obfd_build_id; - - if (objfile->obfd =3D=3D NULL) - continue; - /* Don't return separate debug files. */ - if (objfile->separate_debug_objfile_backlink !=3D NULL) - continue; - obfd_build_id =3D build_id_bfd_get (objfile->obfd.get ()); - if (obfd_build_id =3D=3D NULL) - continue; - if (objfpy_build_id_matches (obfd_build_id, build_id)) - return objfile; - } - - return NULL; -} - /* Implementation of gdb.lookup_objfile. */ =20 PyObject * @@ -644,7 +594,6 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, P= yObject *kw) const char *name; PyObject *by_build_id_obj =3D NULL; int by_build_id; - struct objfile *objfile; =20 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords, &name, &PyBool_Type, &by_build_id_obj)) @@ -660,17 +609,64 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args,= PyObject *kw) by_build_id =3D cmp; } =20 - if (by_build_id) + if (by_build_id && !objfpy_build_id_ok (name)) { - if (!objfpy_build_id_ok (name)) - { - PyErr_SetString (PyExc_TypeError, _("Not a valid build id.")); - return NULL; - } - objfile =3D objfpy_lookup_objfile_by_build_id (name); + PyErr_SetString (PyExc_TypeError, _("Not a valid build id.")); + return NULL; } + + struct objfile *objfile =3D nullptr; + if (by_build_id) + gdbarch_iterate_over_objfiles_in_search_order + (target_gdbarch (), + [&objfile, name] (struct objfile *obj) + { + /* Don't return separate debug files. */ + if (obj->separate_debug_objfile_backlink !=3D nullptr) + return 0; + + bfd *obfd =3D obj->obfd.get (); + if (obfd =3D=3D nullptr) + return 0; + + const bfd_build_id *obfd_build_id =3D build_id_bfd_get (obfd); + if (obfd_build_id =3D=3D nullptr) + return 0; + + if (!objfpy_build_id_matches (obfd_build_id, name)) + return 0; + + objfile =3D obj; + return 1; + }, gdbpy_current_objfile); else - objfile =3D objfpy_lookup_objfile_by_name (name); + gdbarch_iterate_over_objfiles_in_search_order + (target_gdbarch (), + [&objfile, name] (struct objfile *obj) + { + /* Don't return separate debug files. */ + if (obj->separate_debug_objfile_backlink !=3D nullptr) + return 0; + + if ((obj->flags & OBJF_NOT_FILENAME) !=3D 0) + return 0; + + const char *filename =3D objfile_filename (obj); + if (filename !=3D NULL + && compare_filenames_for_search (filename, name)) + { + objfile =3D obj; + return 1; + } + + if (compare_filenames_for_search (obj->original_name, name)) + { + objfile =3D obj; + return 1; + } + + return 0; + }, gdbpy_current_objfile); =20 if (objfile !=3D NULL) return objfile_to_objfile_object (objfile).release (); diff --git a/gdb/python/python.c b/gdb/python/python.c index 516e2c983cf..29f2010ee8e 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1583,11 +1583,8 @@ gdbpy_current_language (PyObject *unused1, PyObject = *unused2) =20 =0C =20 -/* The "current" objfile. This is set when gdb detects that a new - objfile has been loaded. It is only set for the duration of a call to - gdbpy_source_objfile_script and gdbpy_execute_objfile_script; it is NULL - at other times. */ -static struct objfile *gdbpy_current_objfile; +/* See python.h. */ +struct objfile *gdbpy_current_objfile; =20 /* Set the current objfile to OBJFILE and then read FILE named FILENAME as Python code. This does not throw any errors. If an exception diff --git a/gdb/python/python.h b/gdb/python/python.h index 4b150c36a9e..2be83ba2da6 100644 --- a/gdb/python/python.h +++ b/gdb/python/python.h @@ -28,4 +28,10 @@ extern const struct extension_language_defn extension_la= nguage_python; /* Command element for the 'python' command. */ extern cmd_list_element *python_cmd_element; =20 +/* The "current" objfile. This is set when gdb detects that a new + objfile has been loaded. It is only set for the duration of a call to + gdbpy_source_objfile_script and gdbpy_execute_objfile_script; it is NULL + at other times. */ +extern struct objfile *gdbpy_current_objfile; + #endif /* PYTHON_PYTHON_H */