1 /* Python interface to architecture
3 Copyright (C) 2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
23 #include "python-internal.h"
25 typedef struct arch_object_type_object {
27 struct gdbarch *gdbarch;
30 static struct gdbarch_data *arch_object_data = NULL;
31 static PyTypeObject arch_object_type;
33 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
34 post init registration mechanism (gdbarch_data_register_post_init). */
37 arch_object_data_init (struct gdbarch *gdbarch)
39 arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
44 arch_obj->gdbarch = gdbarch;
46 return (void *) arch_obj;
49 /* Returns the struct gdbarch value corresponding to the given Python
50 architecture object OBJ. */
53 arch_object_to_gdbarch (PyObject *obj)
55 arch_object *py_arch = (arch_object *) obj;
57 return py_arch->gdbarch;
60 /* Returns the Python architecture object corresponding to GDBARCH.
61 Returns a new reference to the arch_object associated as data with
65 gdbarch_to_arch_object (struct gdbarch *gdbarch)
67 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
69 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
70 in arch_object_data_init. */
76 /* Implementation of gdb.Architecture.name (self) -> String.
77 Returns the name of the architecture as a string value. */
80 archpy_name (PyObject *self, PyObject *args)
82 struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
83 const char *name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
84 PyObject *py_name = PyString_FromString (name);
89 /* Initializes the Architecture class in the gdb module. */
92 gdbpy_initialize_arch (void)
94 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
95 arch_object_type.tp_new = PyType_GenericNew;
96 if (PyType_Ready (&arch_object_type) < 0)
99 Py_INCREF (&arch_object_type);
100 PyModule_AddObject (gdb_module, "Architecture",
101 (PyObject *) &arch_object_type);
104 static PyMethodDef arch_object_methods [] = {
105 { "name", archpy_name, METH_NOARGS,
106 "name () -> String.\n\
107 Return the name of the architecture as a string value." },
108 {NULL} /* Sentinel */
111 static PyTypeObject arch_object_type = {
112 PyVarObject_HEAD_INIT (NULL, 0)
113 "gdb.Architecture", /* tp_name */
114 sizeof (arch_object), /* tp_basicsize */
122 0, /* tp_as_number */
123 0, /* tp_as_sequence */
124 0, /* tp_as_mapping */
130 0, /* tp_as_buffer */
131 Py_TPFLAGS_DEFAULT, /* tp_flags */
132 "GDB architecture object", /* tp_doc */
135 0, /* tp_richcompare */
136 0, /* tp_weaklistoffset */
139 arch_object_methods, /* tp_methods */
144 0, /* tp_descr_get */
145 0, /* tp_descr_set */
146 0, /* tp_dictoffset */