+/* Return the Python dictionary attribute containing frame filters for
+ this object file. */
+PyObject *
+objfpy_get_frame_filters (PyObject *o, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ Py_INCREF (self->frame_filters);
+ return self->frame_filters;
+}
+
+/* Set this object file's frame filters dictionary to FILTERS. */
+static int
+objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ if (! filters)
+ {
+ PyErr_SetString (PyExc_TypeError,
+ _("Cannot delete the frame filters attribute."));
+ return -1;
+ }
+
+ if (! PyDict_Check (filters))
+ {
+ PyErr_SetString (PyExc_TypeError,
+ _("The frame_filters attribute must be a dictionary."));
+ return -1;
+ }
+
+ /* Take care in case the LHS and RHS are related somehow. */
+ gdbpy_ref<> tmp (self->frame_filters);
+ Py_INCREF (filters);
+ self->frame_filters = filters;
+
+ return 0;
+}
+
+/* Return the frame unwinders attribute for this object file. */
+
+PyObject *
+objfpy_get_frame_unwinders (PyObject *o, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ Py_INCREF (self->frame_unwinders);
+ return self->frame_unwinders;
+}
+
+/* Set this object file's frame unwinders list to UNWINDERS. */
+
+static int
+objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ if (!unwinders)
+ {
+ PyErr_SetString (PyExc_TypeError,
+ _("Cannot delete the frame unwinders attribute."));
+ return -1;
+ }
+
+ if (!PyList_Check (unwinders))
+ {
+ PyErr_SetString (PyExc_TypeError,
+ _("The frame_unwinders attribute must be a list."));
+ return -1;
+ }
+
+ /* Take care in case the LHS and RHS are related somehow. */
+ gdbpy_ref<> tmp (self->frame_unwinders);
+ Py_INCREF (unwinders);
+ self->frame_unwinders = unwinders;
+
+ return 0;
+}
+
+/* Get the 'type_printers' attribute. */
+
+static PyObject *
+objfpy_get_type_printers (PyObject *o, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ Py_INCREF (self->type_printers);
+ return self->type_printers;
+}
+
+/* Get the 'xmethods' attribute. */
+
+PyObject *
+objfpy_get_xmethods (PyObject *o, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ Py_INCREF (self->xmethods);
+ return self->xmethods;
+}
+
+/* Set the 'type_printers' attribute. */
+
+static int
+objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
+{
+ objfile_object *self = (objfile_object *) o;
+
+ if (! value)
+ {
+ PyErr_SetString (PyExc_TypeError,
+ _("Cannot delete the type_printers attribute."));
+ return -1;
+ }
+
+ if (! PyList_Check (value))
+ {
+ PyErr_SetString (PyExc_TypeError,
+ _("The type_printers attribute must be a list."));
+ return -1;
+ }
+
+ /* Take care in case the LHS and RHS are related somehow. */
+ gdbpy_ref<> tmp (self->type_printers);
+ Py_INCREF (value);
+ self->type_printers = value;
+
+ return 0;
+}
+
+/* Implementation of gdb.Objfile.is_valid (self) -> Boolean.
+ Returns True if this object file still exists in GDB. */
+
+static PyObject *
+objfpy_is_valid (PyObject *self, PyObject *args)
+{
+ objfile_object *obj = (objfile_object *) self;
+
+ if (! obj->objfile)
+ Py_RETURN_FALSE;
+
+ Py_RETURN_TRUE;
+}
+
+/* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */
+
+static PyObject *
+objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
+{
+ static const char *keywords[] = { "file_name", NULL };
+ objfile_object *obj = (objfile_object *) self;
+ const char *file_name;
+
+ OBJFPY_REQUIRE_VALID (obj);
+
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &file_name))
+ return NULL;
+
+ TRY
+ {
+ gdb_bfd_ref_ptr abfd (symfile_bfd_open (file_name));
+
+ symbol_file_add_separate (abfd.get (), file_name, 0, obj->objfile);
+ }
+ CATCH (except, RETURN_MASK_ALL)
+ {
+ GDB_PY_HANDLE_EXCEPTION (except);
+ }
+ END_CATCH
+
+ Py_RETURN_NONE;
+}
+
+/* Implement repr() for gdb.Objfile. */
+
+static PyObject *
+objfpy_repr (PyObject *self_)
+{
+ objfile_object *self = (objfile_object *) self_;
+ objfile *obj = self->objfile;
+
+ if (obj == nullptr)
+ return PyString_FromString ("<gdb.Objfile (invalid)>");
+
+ return PyString_FromFormat ("<gdb.Objfile filename=%s>",
+ objfile_filename (obj));
+}
+
+/* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
+ Return non-zero if STRING is a potentially valid build id. */
+
+static int
+objfpy_build_id_ok (const char *string)
+{
+ size_t i, n = strlen (string);
+
+ if (n % 2 != 0)
+ return 0;
+ for (i = 0; i < n; ++i)
+ {
+ if (!isxdigit (string[i]))
+ return 0;
+ }
+ return 1;
+}
+
+/* Subroutine of gdbpy_lookup_objfile_by_build_id to simplify it.
+ Returns non-zero if BUILD_ID matches STRING.
+ It is assumed that objfpy_build_id_ok (string) returns TRUE. */
+
+static int
+objfpy_build_id_matches (const struct bfd_build_id *build_id,
+ const char *string)
+{
+ size_t i;
+
+ if (strlen (string) != 2 * build_id->size)
+ return 0;
+
+ for (i = 0; i < build_id->size; ++i)
+ {
+ char c1 = string[i * 2], c2 = string[i * 2 + 1];
+ int byte = (host_hex_value (c1) << 4) | host_hex_value (c2);
+
+ if (byte != build_id->data[i])
+ return 0;
+ }
+
+ 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 : all_objfiles (current_program_space))
+ {
+ 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 : all_objfiles (current_program_space))
+ {
+ 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 *
+gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
+{
+ static const char *keywords[] = { "name", "by_build_id", NULL };
+ 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))
+ return NULL;
+
+ by_build_id = 0;
+ if (by_build_id_obj != NULL)
+ {
+ int cmp = PyObject_IsTrue (by_build_id_obj);
+
+ if (cmp < 0)
+ return NULL;
+ by_build_id = cmp;
+ }
+
+ if (by_build_id)
+ {
+ 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);
+ }
+ else
+ objfile = objfpy_lookup_objfile_by_name (name);
+
+ if (objfile != NULL)
+ return objfile_to_objfile_object (objfile).release ();
+
+ PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
+ return NULL;
+}
+