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. */
68 arch_object_to_gdbarch (PyObject *obj)
70 arch_object *py_arch = (arch_object *) obj;
72 return py_arch->gdbarch;
75 /* Returns the Python architecture object corresponding to GDBARCH.
76 Returns a new reference to the arch_object associated as data with
80 gdbarch_to_arch_object (struct gdbarch *gdbarch)
82 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
84 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
85 in arch_object_data_init. */
91 /* Implementation of gdb.Architecture.name (self) -> String.
92 Returns the name of the architecture as a string value. */
95 archpy_name (PyObject *self, PyObject *args)
97 struct gdbarch *gdbarch = NULL;
100 ARCHPY_REQUIRE_VALID (self, gdbarch);
102 name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
103 return PyString_FromString (name);
107 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
108 Returns a list of instructions in a memory address range. Each instruction
109 in the list is a Python dict object.
113 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
115 static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
116 CORE_ADDR start, end = 0;
118 gdb_py_ulongest start_temp;
120 PyObject *end_obj = NULL, *count_obj = NULL;
121 struct gdbarch *gdbarch = NULL;
123 ARCHPY_REQUIRE_VALID (self, gdbarch);
125 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO",
126 keywords, &start_temp, &end_obj,
133 /* Make a long logic check first. In Python 3.x, internally,
134 all integers are represented as longs. In Python 2.x, there
135 is still a differentiation internally between a PyInt and a
136 PyLong. Explicitly do this long check conversion first. In
137 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
138 to be done first to ensure we do not lose information in the
139 conversion process. */
140 if (PyLong_Check (end_obj))
141 end = PyLong_AsUnsignedLongLong (end_obj);
142 #if PY_MAJOR_VERSION == 2
143 else if (PyInt_Check (end_obj))
144 /* If the end_pc value is specified without a trailing 'L', end_obj will
145 be an integer and not a long integer. */
146 end = PyInt_AsLong (end_obj);
150 PyErr_SetString (PyExc_TypeError,
151 _("Argument 'end_pc' should be a (long) integer."));
158 PyErr_SetString (PyExc_ValueError,
159 _("Argument 'end_pc' should be greater than or "
160 "equal to the argument 'start_pc'."));
167 count = PyInt_AsLong (count_obj);
168 if (PyErr_Occurred () || count < 0)
170 PyErr_SetString (PyExc_TypeError,
171 _("Argument 'count' should be an non-negative "
178 gdbpy_ref<> result_list (PyList_New (0));
179 if (result_list == NULL)
182 for (pc = start, i = 0;
183 /* All args are specified. */
184 (end_obj && count_obj && pc <= end && i < count)
185 /* end_pc is specified, but no count. */
186 || (end_obj && count_obj == NULL && pc <= end)
187 /* end_pc is not specified, but a count is. */
188 || (end_obj == NULL && count_obj && i < count)
189 /* Both end_pc and count are not specified. */
190 || (end_obj == NULL && count_obj == NULL && pc == start);)
193 gdbpy_ref<> insn_dict (PyDict_New ());
195 if (insn_dict == NULL)
197 if (PyList_Append (result_list.get (), insn_dict.get ()))
198 return NULL; /* PyList_Append Sets the exception. */
204 insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
206 catch (const gdb_exception &except)
208 gdbpy_convert_exception (except);
212 gdbpy_ref<> pc_obj = gdb_py_object_from_ulongest (pc);
213 if (pc_obj == nullptr)
216 gdbpy_ref<> asm_obj (PyString_FromString (!stb.empty ()
219 if (asm_obj == nullptr)
222 gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len);
223 if (len_obj == nullptr)
226 if (PyDict_SetItemString (insn_dict.get (), "addr", pc_obj.get ())
227 || PyDict_SetItemString (insn_dict.get (), "asm", asm_obj.get ())
228 || PyDict_SetItemString (insn_dict.get (), "length", len_obj.get ()))
235 return result_list.release ();
238 /* Implementation of gdb.Architecture.registers (self, reggroup) -> Iterator.
239 Returns an iterator over register descriptors for registers in GROUP
240 within the architecture SELF. */
243 archpy_registers (PyObject *self, PyObject *args, PyObject *kw)
245 static const char *keywords[] = { "reggroup", NULL };
246 struct gdbarch *gdbarch = NULL;
247 const char *group_name = NULL;
249 /* Parse method arguments. */
250 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s", keywords,
254 /* Extract the gdbarch from the self object. */
255 ARCHPY_REQUIRE_VALID (self, gdbarch);
257 return gdbpy_new_register_descriptor_iterator (gdbarch, group_name);
260 /* Implementation of gdb.Architecture.register_groups (self) -> Iterator.
261 Returns an iterator that will give up all valid register groups in the
262 architecture SELF. */
265 archpy_register_groups (PyObject *self, PyObject *args)
267 struct gdbarch *gdbarch = NULL;
269 /* Extract the gdbarch from the self object. */
270 ARCHPY_REQUIRE_VALID (self, gdbarch);
271 return gdbpy_new_reggroup_iterator (gdbarch);
274 /* Implementation of gdb.integer_type. */
276 archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw)
278 static const char *keywords[] = { "size", "signed", NULL };
280 PyObject *is_signed_obj = nullptr;
282 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "i|O", keywords,
283 &size, &is_signed_obj))
286 /* Assume signed by default. */
287 bool is_signed = (is_signed_obj == nullptr
288 || PyObject_IsTrue (is_signed_obj));
290 struct gdbarch *gdbarch;
291 ARCHPY_REQUIRE_VALID (self, gdbarch);
293 const struct builtin_type *builtins = builtin_type (gdbarch);
294 struct type *type = nullptr;
298 type = builtins->builtin_int0;
301 type = is_signed ? builtins->builtin_int8 : builtins->builtin_uint8;
304 type = is_signed ? builtins->builtin_int16 : builtins->builtin_uint16;
307 type = is_signed ? builtins->builtin_int24 : builtins->builtin_uint24;
310 type = is_signed ? builtins->builtin_int32 : builtins->builtin_uint32;
313 type = is_signed ? builtins->builtin_int64 : builtins->builtin_uint64;
316 type = is_signed ? builtins->builtin_int128 : builtins->builtin_uint128;
320 PyErr_SetString (PyExc_ValueError,
321 _("no integer type of that size is available"));
325 return type_to_type_object (type);
328 /* Implementation of gdb.architecture_names(). Return a list of all the
329 BFD architecture names that GDB understands. */
332 gdbpy_all_architecture_names (PyObject *self, PyObject *args)
334 gdbpy_ref<> list (PyList_New (0));
338 std::vector<const char *> name_list = gdbarch_printable_names ();
339 for (const char *name : name_list)
341 gdbpy_ref <> py_name (PyString_FromString (name));
342 if (py_name == nullptr)
344 if (PyList_Append (list.get (), py_name.get ()) < 0)
348 return list.release ();
351 void _initialize_py_arch ();
353 _initialize_py_arch ()
355 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
358 /* Initializes the Architecture class in the gdb module. */
361 gdbpy_initialize_arch (void)
363 arch_object_type.tp_new = PyType_GenericNew;
364 if (PyType_Ready (&arch_object_type) < 0)
367 return gdb_pymodule_addobject (gdb_module, "Architecture",
368 (PyObject *) &arch_object_type);
371 static PyMethodDef arch_object_methods [] = {
372 { "name", archpy_name, METH_NOARGS,
373 "name () -> String.\n\
374 Return the name of the architecture as a string value." },
375 { "disassemble", (PyCFunction) archpy_disassemble,
376 METH_VARARGS | METH_KEYWORDS,
377 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
378 Return a list of at most COUNT disassembled instructions from START_PC to\n\
380 { "integer_type", (PyCFunction) archpy_integer_type,
381 METH_VARARGS | METH_KEYWORDS,
382 "integer_type (size [, signed]) -> type\n\
383 Return an integer Type corresponding to the given bitsize and signed-ness.\n\
384 If not specified, the type defaults to signed." },
385 { "registers", (PyCFunction) archpy_registers,
386 METH_VARARGS | METH_KEYWORDS,
387 "registers ([ group-name ]) -> Iterator.\n\
388 Return an iterator of register descriptors for the registers in register\n\
389 group GROUP-NAME." },
390 { "register_groups", archpy_register_groups,
392 "register_groups () -> Iterator.\n\
393 Return an iterator over all of the register groups in this architecture." },
394 {NULL} /* Sentinel */
397 PyTypeObject arch_object_type = {
398 PyVarObject_HEAD_INIT (NULL, 0)
399 "gdb.Architecture", /* tp_name */
400 sizeof (arch_object), /* tp_basicsize */
408 0, /* tp_as_number */
409 0, /* tp_as_sequence */
410 0, /* tp_as_mapping */
416 0, /* tp_as_buffer */
417 Py_TPFLAGS_DEFAULT, /* tp_flags */
418 "GDB architecture object", /* tp_doc */
421 0, /* tp_richcompare */
422 0, /* tp_weaklistoffset */
425 arch_object_methods, /* tp_methods */
430 0, /* tp_descr_get */
431 0, /* tp_descr_set */
432 0, /* tp_dictoffset */