public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Markus Metzger <markus.t.metzger@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v5 07/15] gdb, python: use gdbarch_iterate_over_objfiles_in_search_order
Date: Thu,  2 Jun 2022 15:25:06 +0200	[thread overview]
Message-ID: <20220602132514.957983-8-markus.t.metzger@intel.com> (raw)
In-Reply-To: <20220602132514.957983-1-markus.t.metzger@intel.com>

The implementation of gdb.lookup_objfile() iterates over all objfiles and
compares their name or build id to the user-provided search string.

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.
---
 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 3e3270e7cd3..b278cec99d8 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"
 
 struct objfile_object
 {
@@ -559,57 +560,6 @@ objfpy_build_id_matches (const struct bfd_build_id *build_id,
   return 1;
 }
 
-/* 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) != 0)
-	continue;
-      /* Don't return separate debug files.  */
-      if (objfile->separate_debug_objfile_backlink != NULL)
-	continue;
-
-      filename = objfile_filename (objfile);
-      if (filename != NULL && compare_filenames_for_search (filename, name))
-	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 == NULL)
-	continue;
-      /* Don't return separate debug files.  */
-      if (objfile->separate_debug_objfile_backlink != NULL)
-	continue;
-      obfd_build_id = build_id_bfd_get (objfile->obfd);
-      if (obfd_build_id == NULL)
-	continue;
-      if (objfpy_build_id_matches (obfd_build_id, build_id))
-	return objfile;
-    }
-
-  return NULL;
-}
-
 /* Implementation of gdb.lookup_objfile.  */
 
 PyObject *
@@ -619,7 +569,6 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
   const char *name;
   PyObject *by_build_id_obj = NULL;
   int by_build_id;
-  struct objfile *objfile;
 
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
 					&name, &PyBool_Type, &by_build_id_obj))
@@ -635,17 +584,64 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
       by_build_id = cmp;
     }
 
-  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 = objfpy_lookup_objfile_by_build_id (name);
+      PyErr_SetString (PyExc_TypeError, _("Not a valid build id."));
+      return NULL;
     }
+
+  struct objfile *objfile = 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 != nullptr)
+	     return 0;
+
+	   bfd *obfd = obj->obfd;
+	   if (obfd == nullptr)
+	     return 0;
+
+	   const bfd_build_id *obfd_build_id = build_id_bfd_get (obfd);
+	   if (obfd_build_id == nullptr)
+	     return 0;
+
+	   if (!objfpy_build_id_matches (obfd_build_id, name))
+	     return 0;
+
+	   objfile = obj;
+	   return 1;
+	 }, gdbpy_current_objfile);
   else
-    objfile = 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 != nullptr)
+	     return 0;
+
+	   if ((obj->flags & OBJF_NOT_FILENAME) != 0)
+	     return 0;
+
+	   const char *filename = objfile_filename (obj);
+	   if (filename != NULL
+	       && compare_filenames_for_search (filename, name))
+	     {
+	       objfile = obj;
+	       return 1;
+	     }
+
+	   if (compare_filenames_for_search (obj->original_name, name))
+	     {
+	       objfile = obj;
+	       return 1;
+	     }
+
+	   return 0;
+	 }, gdbpy_current_objfile);
 
   if (objfile != NULL)
     return objfile_to_objfile_object (objfile).release ();
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9bef2252e88..ba6fdc455b7 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1581,11 +1581,8 @@ gdbpy_current_language (PyObject *unused1, PyObject *unused2)
 
 \f
 
-/* 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;
 
 /* 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_language_python;
 /* Command element for the 'python' command.  */
 extern cmd_list_element *python_cmd_element;
 
+/* 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 */
-- 
2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


  parent reply	other threads:[~2022-06-02 14:29 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-02 13:24 [PATCH v5 00/15] basic linker namespace support Markus Metzger
2022-06-02 13:25 ` [PATCH v5 01/15] gdb, testsuite: extend gdb_test_multiple checks Markus Metzger
2022-06-13  1:28   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 02/15] gdb, solib-svr4: remove locate_base() Markus Metzger
2022-06-02 23:04   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 03/15] gdb, gdbserver: support dlmopen() Markus Metzger
2022-06-19  4:02   ` Kevin Buettner
2022-06-27 12:55     ` Metzger, Markus T
2022-06-30 22:35       ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 04/15] gdbserver: move main_lm handling into caller Markus Metzger
2022-06-19  4:22   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 05/15] gdb, gdbserver: extend RSP to support namespaces Markus Metzger
2022-06-02 16:09   ` Eli Zaretskii
2022-06-19  4:32   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 06/15] gdb, compile: unlink objfile stored in module Markus Metzger
2022-06-23 17:20   ` Kevin Buettner
2022-06-02 13:25 ` Markus Metzger [this message]
2022-06-24 17:18   ` [PATCH v5 07/15] gdb, python: use gdbarch_iterate_over_objfiles_in_search_order Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 08/15] gdb, ada: collect standard exceptions in all objfiles Markus Metzger
2022-06-24 17:26   ` Kevin Buettner
2022-07-18 16:49     ` Tom Tromey
2022-07-18  5:35   ` Metzger, Markus T
2022-09-14  8:19     ` Metzger, Markus T
2022-09-14  8:37       ` Joel Brobecker
2022-09-14  8:45         ` Metzger, Markus T
2022-06-02 13:25 ` [PATCH v5 09/15] gdb, ada: update ada_lookup_simple_minsym Markus Metzger
2022-06-24 23:42   ` Kevin Buettner
2022-07-18 17:02   ` Tom Tromey
2022-07-19  7:14     ` Metzger, Markus T
2022-09-14  8:19       ` Metzger, Markus T
2022-09-21 16:11         ` Tom Tromey
2022-06-02 13:25 ` [PATCH v5 10/15] gdb, ada: update ada_add_all_symbols Markus Metzger
2022-06-24 23:53   ` Kevin Buettner
2022-07-18  5:36   ` Metzger, Markus T
2022-07-18 16:56   ` Tom Tromey
2022-07-19  7:13     ` Metzger, Markus T
2022-07-19 12:23       ` Tom Tromey
2022-07-19 13:49         ` Metzger, Markus T
2022-06-02 13:25 ` [PATCH v5 11/15] gdb, cp: update add_symbol_overload_list_qualified Markus Metzger
2022-06-24 23:59   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 12/15] gdb, hppa: remove unused hppa_lookup_stub_minimal_symbol Markus Metzger
2022-06-25  0:01   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 13/15] gdb, symtab: inline find_quick_global_symbol_language Markus Metzger
2022-06-25  0:16   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 14/15] gdb: update gnu ifunc resolve Markus Metzger
2022-06-25  0:34   ` Kevin Buettner
2022-06-02 13:25 ` [PATCH v5 15/15] gdb, solib-svr4: support namespaces in DSO iteration Markus Metzger
2022-06-25  0:42   ` Kevin Buettner
2022-07-15 10:30 ` [PATCH v5 00/15] basic linker namespace support Metzger, Markus T
2022-07-16  0:04   ` Kevin Buettner
2022-07-18  5:33     ` Metzger, Markus T
2022-10-05 11:16       ` Metzger, Markus T

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220602132514.957983-8-markus.t.metzger@intel.com \
    --to=markus.t.metzger@intel.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).