1 /* Python interface to architecture
3 Copyright (C) 2013-2019 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"
24 #include "python-internal.h"
27 typedef struct arch_object_type_object {
29 struct gdbarch *gdbarch;
32 static struct gdbarch_data *arch_object_data = NULL;
34 /* Require a valid Architecture. */
35 #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \
37 arch = arch_object_to_gdbarch (arch_obj); \
40 PyErr_SetString (PyExc_RuntimeError, \
41 _("Architecture is invalid.")); \
46 extern PyTypeObject arch_object_type
47 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
49 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
50 post init registration mechanism (gdbarch_data_register_post_init). */
53 arch_object_data_init (struct gdbarch *gdbarch)
55 arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
60 arch_obj->gdbarch = gdbarch;
62 return (void *) arch_obj;
65 /* Returns the struct gdbarch value corresponding to the given Python
66 architecture object OBJ. */
69 arch_object_to_gdbarch (PyObject *obj)
71 arch_object *py_arch = (arch_object *) obj;
73 return py_arch->gdbarch;
76 /* Returns the Python architecture object corresponding to GDBARCH.
77 Returns a new reference to the arch_object associated as data with
81 gdbarch_to_arch_object (struct gdbarch *gdbarch)
83 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
85 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
86 in arch_object_data_init. */
92 /* Implementation of gdb.Architecture.name (self) -> String.
93 Returns the name of the architecture as a string value. */
96 archpy_name (PyObject *self, PyObject *args)
98 struct gdbarch *gdbarch = NULL;
101 ARCHPY_REQUIRE_VALID (self, gdbarch);
103 name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
104 return PyString_FromString (name);
108 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
109 Returns a list of instructions in a memory address range. Each instruction
110 in the list is a Python dict object.
114 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
116 static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
117 CORE_ADDR start, end = 0;
119 gdb_py_ulongest start_temp;
121 PyObject *end_obj = NULL, *count_obj = NULL;
122 struct gdbarch *gdbarch = NULL;
124 ARCHPY_REQUIRE_VALID (self, gdbarch);
126 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO",
127 keywords, &start_temp, &end_obj,
134 /* Make a long logic check first. In Python 3.x, internally,
135 all integers are represented as longs. In Python 2.x, there
136 is still a differentiation internally between a PyInt and a
137 PyLong. Explicitly do this long check conversion first. In
138 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
139 to be done first to ensure we do not lose information in the
140 conversion process. */
141 if (PyLong_Check (end_obj))
142 end = PyLong_AsUnsignedLongLong (end_obj);
143 #if PY_MAJOR_VERSION == 2
144 else if (PyInt_Check (end_obj))
145 /* If the end_pc value is specified without a trailing 'L', end_obj will
146 be an integer and not a long integer. */
147 end = PyInt_AsLong (end_obj);
151 PyErr_SetString (PyExc_TypeError,
152 _("Argument 'end_pc' should be a (long) integer."));
159 PyErr_SetString (PyExc_ValueError,
160 _("Argument 'end_pc' should be greater than or "
161 "equal to the argument 'start_pc'."));
168 count = PyInt_AsLong (count_obj);
169 if (PyErr_Occurred () || count < 0)
171 PyErr_SetString (PyExc_TypeError,
172 _("Argument 'count' should be an non-negative "
179 gdbpy_ref<> result_list (PyList_New (0));
180 if (result_list == NULL)
183 for (pc = start, i = 0;
184 /* All args are specified. */
185 (end_obj && count_obj && pc <= end && i < count)
186 /* end_pc is specified, but no count. */
187 || (end_obj && count_obj == NULL && pc <= end)
188 /* end_pc is not specified, but a count is. */
189 || (end_obj == NULL && count_obj && i < count)
190 /* Both end_pc and count are not specified. */
191 || (end_obj == NULL && count_obj == NULL && pc == start);)
194 gdbpy_ref<> insn_dict (PyDict_New ());
196 if (insn_dict == NULL)
198 if (PyList_Append (result_list.get (), insn_dict.get ()))
199 return NULL; /* PyList_Append Sets the exception. */
205 insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
207 CATCH (except, RETURN_MASK_ALL)
209 gdbpy_convert_exception (except);
214 if (PyDict_SetItemString (insn_dict.get (), "addr",
215 gdb_py_long_from_ulongest (pc))
216 || PyDict_SetItemString (insn_dict.get (), "asm",
217 PyString_FromString (!stb.empty ()
220 || PyDict_SetItemString (insn_dict.get (), "length",
221 PyInt_FromLong (insn_len)))
228 return result_list.release ();
231 /* Initializes the Architecture class in the gdb module. */
234 gdbpy_initialize_arch (void)
236 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
237 arch_object_type.tp_new = PyType_GenericNew;
238 if (PyType_Ready (&arch_object_type) < 0)
241 return gdb_pymodule_addobject (gdb_module, "Architecture",
242 (PyObject *) &arch_object_type);
245 static PyMethodDef arch_object_methods [] = {
246 { "name", archpy_name, METH_NOARGS,
247 "name () -> String.\n\
248 Return the name of the architecture as a string value." },
249 { "disassemble", (PyCFunction) archpy_disassemble,
250 METH_VARARGS | METH_KEYWORDS,
251 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
252 Return a list of at most COUNT disassembled instructions from START_PC to\n\
254 {NULL} /* Sentinel */
257 PyTypeObject arch_object_type = {
258 PyVarObject_HEAD_INIT (NULL, 0)
259 "gdb.Architecture", /* tp_name */
260 sizeof (arch_object), /* tp_basicsize */
268 0, /* tp_as_number */
269 0, /* tp_as_sequence */
270 0, /* tp_as_mapping */
276 0, /* tp_as_buffer */
277 Py_TPFLAGS_DEFAULT, /* tp_flags */
278 "GDB architecture object", /* tp_doc */
281 0, /* tp_richcompare */
282 0, /* tp_weaklistoffset */
285 arch_object_methods, /* tp_methods */
290 0, /* tp_descr_get */
291 0, /* tp_descr_set */
292 0, /* tp_dictoffset */