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"
24 #include "python-internal.h"
26 typedef struct arch_object_type_object {
28 struct gdbarch *gdbarch;
31 static struct gdbarch_data *arch_object_data = NULL;
32 static PyTypeObject arch_object_type;
34 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
35 post init registration mechanism (gdbarch_data_register_post_init). */
38 arch_object_data_init (struct gdbarch *gdbarch)
40 arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
45 arch_obj->gdbarch = gdbarch;
47 return (void *) arch_obj;
50 /* Returns the struct gdbarch value corresponding to the given Python
51 architecture object OBJ. */
54 arch_object_to_gdbarch (PyObject *obj)
56 arch_object *py_arch = (arch_object *) obj;
58 return py_arch->gdbarch;
61 /* Returns the Python architecture object corresponding to GDBARCH.
62 Returns a new reference to the arch_object associated as data with
66 gdbarch_to_arch_object (struct gdbarch *gdbarch)
68 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
70 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
71 in arch_object_data_init. */
77 /* Implementation of gdb.Architecture.name (self) -> String.
78 Returns the name of the architecture as a string value. */
81 archpy_name (PyObject *self, PyObject *args)
83 struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
84 const char *name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
85 PyObject *py_name = PyString_FromString (name);
91 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
92 Returns a list of instructions in a memory address range. Each instruction
93 in the list is a Python dict object.
97 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
99 static char *keywords[] = { "start_pc", "end_pc", "count", NULL };
100 CORE_ADDR start, end = 0;
102 gdb_py_ulongest start_temp;
104 PyObject *result_list, *end_obj = NULL, *count_obj = NULL;
105 struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
107 if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
108 &start_temp, &end_obj, &count_obj))
114 if (PyObject_TypeCheck (end_obj, &PyInt_Type))
115 /* If the end_pc value is specified without a trailing 'L', end_obj will
116 be an integer and not a long integer. */
117 end = PyInt_AsLong (end_obj);
118 else if (PyObject_TypeCheck (end_obj, &PyLong_Type))
119 end = PyLong_AsUnsignedLongLong (end_obj);
123 Py_XDECREF (count_obj);
124 PyErr_SetString (PyExc_TypeError,
125 _("Argument 'end_pc' should be a (long) integer."));
133 Py_XDECREF (count_obj);
134 PyErr_SetString (PyExc_ValueError,
135 _("Argument 'end_pc' should be greater than or "
136 "equal to the argument 'start_pc'."));
143 count = PyInt_AsLong (count_obj);
144 if (PyErr_Occurred () || count < 0)
146 Py_DECREF (count_obj);
147 Py_XDECREF (end_obj);
148 PyErr_SetString (PyExc_TypeError,
149 _("Argument 'count' should be an non-negative "
156 result_list = PyList_New (0);
157 if (result_list == NULL)
160 for (pc = start, i = 0;
161 /* All args are specified. */
162 (end_obj && count_obj && pc <= end && i < count)
163 /* end_pc is specified, but no count. */
164 || (end_obj && count_obj == NULL && pc <= end)
165 /* end_pc is not specified, but a count is. */
166 || (end_obj == NULL && count_obj && i < count)
167 /* Both end_pc and count are not specified. */
168 || (end_obj == NULL && count_obj == NULL && pc == start);)
172 struct ui_file *memfile = mem_fileopen ();
173 PyObject *insn_dict = PyDict_New ();
174 volatile struct gdb_exception except;
176 if (insn_dict == NULL)
178 Py_DECREF (result_list);
179 ui_file_delete (memfile);
183 if (PyList_Append (result_list, insn_dict))
185 Py_DECREF (result_list);
186 Py_DECREF (insn_dict);
187 ui_file_delete (memfile);
189 return NULL; /* PyList_Append Sets the exception. */
192 TRY_CATCH (except, RETURN_MASK_ALL)
194 insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
196 if (except.reason < 0)
198 Py_DECREF (result_list);
199 ui_file_delete (memfile);
201 return gdbpy_convert_exception (except);
204 as = ui_file_xstrdup (memfile, NULL);
205 if (PyDict_SetItemString (insn_dict, "addr",
206 gdb_py_long_from_ulongest (pc))
207 || PyDict_SetItemString (insn_dict, "asm",
208 PyString_FromString (*as ? as : "<unknown>"))
209 || PyDict_SetItemString (insn_dict, "length",
210 PyInt_FromLong (insn_len)))
212 Py_DECREF (result_list);
214 ui_file_delete (memfile);
222 ui_file_delete (memfile);
229 /* Initializes the Architecture class in the gdb module. */
232 gdbpy_initialize_arch (void)
234 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
235 arch_object_type.tp_new = PyType_GenericNew;
236 if (PyType_Ready (&arch_object_type) < 0)
239 Py_INCREF (&arch_object_type);
240 PyModule_AddObject (gdb_module, "Architecture",
241 (PyObject *) &arch_object_type);
244 static PyMethodDef arch_object_methods [] = {
245 { "name", archpy_name, METH_NOARGS,
246 "name () -> String.\n\
247 Return the name of the architecture as a string value." },
248 { "disassemble", (PyCFunction) archpy_disassemble,
249 METH_VARARGS | METH_KEYWORDS,
250 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
251 Return a list of at most COUNT disassembled instructions from START_PC to\n\
253 {NULL} /* Sentinel */
256 static PyTypeObject arch_object_type = {
257 PyVarObject_HEAD_INIT (NULL, 0)
258 "gdb.Architecture", /* tp_name */
259 sizeof (arch_object), /* tp_basicsize */
267 0, /* tp_as_number */
268 0, /* tp_as_sequence */
269 0, /* tp_as_mapping */
275 0, /* tp_as_buffer */
276 Py_TPFLAGS_DEFAULT, /* tp_flags */
277 "GDB architecture object", /* tp_doc */
280 0, /* tp_richcompare */
281 0, /* tp_weaklistoffset */
284 arch_object_methods, /* tp_methods */
289 0, /* tp_descr_get */
290 0, /* tp_descr_set */
291 0, /* tp_dictoffset */