1 /* Python interface to architecture
3 Copyright (C) 2013-2022 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"
28 struct gdbarch *gdbarch;
31 static struct gdbarch_data *arch_object_data = NULL;
33 /* Require a valid Architecture. */
34 #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \
36 arch = arch_object_to_gdbarch (arch_obj); \
39 PyErr_SetString (PyExc_RuntimeError, \
40 _("Architecture is invalid.")); \
45 extern PyTypeObject arch_object_type
46 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
48 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
49 post init registration mechanism (gdbarch_data_register_post_init). */
52 arch_object_data_init (struct gdbarch *gdbarch)
54 arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
59 arch_obj->gdbarch = gdbarch;
61 return (void *) arch_obj;
64 /* Returns the struct gdbarch value corresponding to the given Python
65 architecture object OBJ, which must be a gdb.Architecture object. */
68 arch_object_to_gdbarch (PyObject *obj)
70 gdb_assert (gdbpy_is_architecture (obj));
72 arch_object *py_arch = (arch_object *) obj;
73 return py_arch->gdbarch;
76 /* See python-internal.h. */
79 gdbpy_is_architecture (PyObject *obj)
81 return PyObject_TypeCheck (obj, &arch_object_type);
84 /* Returns the Python architecture object corresponding to GDBARCH.
85 Returns a new reference to the arch_object associated as data with
89 gdbarch_to_arch_object (struct gdbarch *gdbarch)
91 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
93 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
94 in arch_object_data_init. */
100 /* Implementation of gdb.Architecture.name (self) -> String.
101 Returns the name of the architecture as a string value. */
104 archpy_name (PyObject *self, PyObject *args)
106 struct gdbarch *gdbarch = NULL;
109 ARCHPY_REQUIRE_VALID (self, gdbarch);
111 name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
112 return PyString_FromString (name);
116 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
117 Returns a list of instructions in a memory address range. Each instruction
118 in the list is a Python dict object.
122 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
124 static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
125 CORE_ADDR start, end = 0;
127 gdb_py_ulongest start_temp;
129 PyObject *end_obj = NULL, *count_obj = NULL;
130 struct gdbarch *gdbarch = NULL;
132 ARCHPY_REQUIRE_VALID (self, gdbarch);
134 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO",
135 keywords, &start_temp, &end_obj,
142 /* Make a long logic check first. In Python 3.x, internally,
143 all integers are represented as longs. In Python 2.x, there
144 is still a differentiation internally between a PyInt and a
145 PyLong. Explicitly do this long check conversion first. In
146 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
147 to be done first to ensure we do not lose information in the
148 conversion process. */
149 if (PyLong_Check (end_obj))
150 end = PyLong_AsUnsignedLongLong (end_obj);
153 PyErr_SetString (PyExc_TypeError,
154 _("Argument 'end_pc' should be a (long) integer."));
161 PyErr_SetString (PyExc_ValueError,
162 _("Argument 'end_pc' should be greater than or "
163 "equal to the argument 'start_pc'."));
170 count = PyInt_AsLong (count_obj);
171 if (PyErr_Occurred () || count < 0)
173 PyErr_SetString (PyExc_TypeError,
174 _("Argument 'count' should be an non-negative "
181 gdbpy_ref<> result_list (PyList_New (0));
182 if (result_list == NULL)
185 for (pc = start, i = 0;
186 /* All args are specified. */
187 (end_obj && count_obj && pc <= end && i < count)
188 /* end_pc is specified, but no count. */
189 || (end_obj && count_obj == NULL && pc <= end)
190 /* end_pc is not specified, but a count is. */
191 || (end_obj == NULL && count_obj && i < count)
192 /* Both end_pc and count are not specified. */
193 || (end_obj == NULL && count_obj == NULL && pc == start);)
196 gdbpy_ref<> insn_dict (PyDict_New ());
198 if (insn_dict == NULL)
200 if (PyList_Append (result_list.get (), insn_dict.get ()))
201 return NULL; /* PyList_Append Sets the exception. */
207 insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
209 catch (const gdb_exception &except)
211 gdbpy_convert_exception (except);
215 gdbpy_ref<> pc_obj = gdb_py_object_from_ulongest (pc);
216 if (pc_obj == nullptr)
219 gdbpy_ref<> asm_obj (PyString_FromString (!stb.empty ()
222 if (asm_obj == nullptr)
225 gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len);
226 if (len_obj == nullptr)
229 if (PyDict_SetItemString (insn_dict.get (), "addr", pc_obj.get ())
230 || PyDict_SetItemString (insn_dict.get (), "asm", asm_obj.get ())
231 || PyDict_SetItemString (insn_dict.get (), "length", len_obj.get ()))
238 return result_list.release ();
241 /* Implementation of gdb.Architecture.registers (self, reggroup) -> Iterator.
242 Returns an iterator over register descriptors for registers in GROUP
243 within the architecture SELF. */
246 archpy_registers (PyObject *self, PyObject *args, PyObject *kw)
248 static const char *keywords[] = { "reggroup", NULL };
249 struct gdbarch *gdbarch = NULL;
250 const char *group_name = NULL;
252 /* Parse method arguments. */
253 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s", keywords,
257 /* Extract the gdbarch from the self object. */
258 ARCHPY_REQUIRE_VALID (self, gdbarch);
260 return gdbpy_new_register_descriptor_iterator (gdbarch, group_name);
263 /* Implementation of gdb.Architecture.register_groups (self) -> Iterator.
264 Returns an iterator that will give up all valid register groups in the
265 architecture SELF. */
268 archpy_register_groups (PyObject *self, PyObject *args)
270 struct gdbarch *gdbarch = NULL;
272 /* Extract the gdbarch from the self object. */
273 ARCHPY_REQUIRE_VALID (self, gdbarch);
274 return gdbpy_new_reggroup_iterator (gdbarch);
277 /* Implementation of gdb.integer_type. */
279 archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw)
281 static const char *keywords[] = { "size", "signed", NULL };
283 PyObject *is_signed_obj = nullptr;
285 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "i|O", keywords,
286 &size, &is_signed_obj))
289 /* Assume signed by default. */
290 bool is_signed = (is_signed_obj == nullptr
291 || PyObject_IsTrue (is_signed_obj));
293 struct gdbarch *gdbarch;
294 ARCHPY_REQUIRE_VALID (self, gdbarch);
296 const struct builtin_type *builtins = builtin_type (gdbarch);
297 struct type *type = nullptr;
301 type = builtins->builtin_int0;
304 type = is_signed ? builtins->builtin_int8 : builtins->builtin_uint8;
307 type = is_signed ? builtins->builtin_int16 : builtins->builtin_uint16;
310 type = is_signed ? builtins->builtin_int24 : builtins->builtin_uint24;
313 type = is_signed ? builtins->builtin_int32 : builtins->builtin_uint32;
316 type = is_signed ? builtins->builtin_int64 : builtins->builtin_uint64;
319 type = is_signed ? builtins->builtin_int128 : builtins->builtin_uint128;
323 PyErr_SetString (PyExc_ValueError,
324 _("no integer type of that size is available"));
328 return type_to_type_object (type);
331 /* Implementation of gdb.architecture_names(). Return a list of all the
332 BFD architecture names that GDB understands. */
335 gdbpy_all_architecture_names (PyObject *self, PyObject *args)
337 gdbpy_ref<> list (PyList_New (0));
341 std::vector<const char *> name_list = gdbarch_printable_names ();
342 for (const char *name : name_list)
344 gdbpy_ref <> py_name (PyString_FromString (name));
345 if (py_name == nullptr)
347 if (PyList_Append (list.get (), py_name.get ()) < 0)
351 return list.release ();
354 void _initialize_py_arch ();
356 _initialize_py_arch ()
358 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
361 /* Initializes the Architecture class in the gdb module. */
364 gdbpy_initialize_arch (void)
366 arch_object_type.tp_new = PyType_GenericNew;
367 if (PyType_Ready (&arch_object_type) < 0)
370 return gdb_pymodule_addobject (gdb_module, "Architecture",
371 (PyObject *) &arch_object_type);
374 static PyMethodDef arch_object_methods [] = {
375 { "name", archpy_name, METH_NOARGS,
376 "name () -> String.\n\
377 Return the name of the architecture as a string value." },
378 { "disassemble", (PyCFunction) archpy_disassemble,
379 METH_VARARGS | METH_KEYWORDS,
380 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
381 Return a list of at most COUNT disassembled instructions from START_PC to\n\
383 { "integer_type", (PyCFunction) archpy_integer_type,
384 METH_VARARGS | METH_KEYWORDS,
385 "integer_type (size [, signed]) -> type\n\
386 Return an integer Type corresponding to the given bitsize and signed-ness.\n\
387 If not specified, the type defaults to signed." },
388 { "registers", (PyCFunction) archpy_registers,
389 METH_VARARGS | METH_KEYWORDS,
390 "registers ([ group-name ]) -> Iterator.\n\
391 Return an iterator of register descriptors for the registers in register\n\
392 group GROUP-NAME." },
393 { "register_groups", archpy_register_groups,
395 "register_groups () -> Iterator.\n\
396 Return an iterator over all of the register groups in this architecture." },
397 {NULL} /* Sentinel */
400 PyTypeObject arch_object_type = {
401 PyVarObject_HEAD_INIT (NULL, 0)
402 "gdb.Architecture", /* tp_name */
403 sizeof (arch_object), /* tp_basicsize */
411 0, /* tp_as_number */
412 0, /* tp_as_sequence */
413 0, /* tp_as_mapping */
419 0, /* tp_as_buffer */
420 Py_TPFLAGS_DEFAULT, /* tp_flags */
421 "GDB architecture object", /* tp_doc */
424 0, /* tp_richcompare */
425 0, /* tp_weaklistoffset */
428 arch_object_methods, /* tp_methods */
433 0, /* tp_descr_get */
434 0, /* tp_descr_set */
435 0, /* tp_dictoffset */