1 /* Python interface to architecture
3 Copyright (C) 2013-2017 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
;
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
;
101 ARCHPY_REQUIRE_VALID (self
, gdbarch
);
103 name
= (gdbarch_bfd_arch_info (gdbarch
))->printable_name
;
104 py_name
= PyString_FromString (name
);
110 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
111 Returns a list of instructions in a memory address range. Each instruction
112 in the list is a Python dict object.
116 archpy_disassemble (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
118 static char *keywords
[] = { "start_pc", "end_pc", "count", NULL
};
119 CORE_ADDR start
, end
= 0;
121 gdb_py_ulongest start_temp
;
123 PyObject
*result_list
, *end_obj
= NULL
, *count_obj
= NULL
;
124 struct gdbarch
*gdbarch
= NULL
;
126 ARCHPY_REQUIRE_VALID (self
, gdbarch
);
128 if (!PyArg_ParseTupleAndKeywords (args
, kw
, GDB_PY_LLU_ARG
"|OO", keywords
,
129 &start_temp
, &end_obj
, &count_obj
))
135 /* Make a long logic check first. In Python 3.x, internally,
136 all integers are represented as longs. In Python 2.x, there
137 is still a differentiation internally between a PyInt and a
138 PyLong. Explicitly do this long check conversion first. In
139 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
140 to be done first to ensure we do not lose information in the
141 conversion process. */
142 if (PyLong_Check (end_obj
))
143 end
= PyLong_AsUnsignedLongLong (end_obj
);
144 #if PY_MAJOR_VERSION == 2
145 else if (PyInt_Check (end_obj
))
146 /* If the end_pc value is specified without a trailing 'L', end_obj will
147 be an integer and not a long integer. */
148 end
= PyInt_AsLong (end_obj
);
153 Py_XDECREF (count_obj
);
154 PyErr_SetString (PyExc_TypeError
,
155 _("Argument 'end_pc' should be a (long) integer."));
163 Py_XDECREF (count_obj
);
164 PyErr_SetString (PyExc_ValueError
,
165 _("Argument 'end_pc' should be greater than or "
166 "equal to the argument 'start_pc'."));
173 count
= PyInt_AsLong (count_obj
);
174 if (PyErr_Occurred () || count
< 0)
176 Py_DECREF (count_obj
);
177 Py_XDECREF (end_obj
);
178 PyErr_SetString (PyExc_TypeError
,
179 _("Argument 'count' should be an non-negative "
186 result_list
= PyList_New (0);
187 if (result_list
== NULL
)
190 for (pc
= start
, i
= 0;
191 /* All args are specified. */
192 (end_obj
&& count_obj
&& pc
<= end
&& i
< count
)
193 /* end_pc is specified, but no count. */
194 || (end_obj
&& count_obj
== NULL
&& pc
<= end
)
195 /* end_pc is not specified, but a count is. */
196 || (end_obj
== NULL
&& count_obj
&& i
< count
)
197 /* Both end_pc and count are not specified. */
198 || (end_obj
== NULL
&& count_obj
== NULL
&& pc
== start
);)
201 struct ui_file
*memfile
= mem_fileopen ();
202 PyObject
*insn_dict
= PyDict_New ();
204 if (insn_dict
== NULL
)
206 Py_DECREF (result_list
);
207 ui_file_delete (memfile
);
211 if (PyList_Append (result_list
, insn_dict
))
213 Py_DECREF (result_list
);
214 Py_DECREF (insn_dict
);
215 ui_file_delete (memfile
);
217 return NULL
; /* PyList_Append Sets the exception. */
222 insn_len
= gdb_print_insn (gdbarch
, pc
, memfile
, NULL
);
224 CATCH (except
, RETURN_MASK_ALL
)
226 Py_DECREF (result_list
);
227 ui_file_delete (memfile
);
229 gdbpy_convert_exception (except
);
234 std::string as
= ui_file_as_string (memfile
);
236 if (PyDict_SetItemString (insn_dict
, "addr",
237 gdb_py_long_from_ulongest (pc
))
238 || PyDict_SetItemString (insn_dict
, "asm",
239 PyString_FromString (!as
.empty ()
242 || PyDict_SetItemString (insn_dict
, "length",
243 PyInt_FromLong (insn_len
)))
245 Py_DECREF (result_list
);
247 ui_file_delete (memfile
);
254 ui_file_delete (memfile
);
260 /* Initializes the Architecture class in the gdb module. */
263 gdbpy_initialize_arch (void)
265 arch_object_data
= gdbarch_data_register_post_init (arch_object_data_init
);
266 arch_object_type
.tp_new
= PyType_GenericNew
;
267 if (PyType_Ready (&arch_object_type
) < 0)
270 return gdb_pymodule_addobject (gdb_module
, "Architecture",
271 (PyObject
*) &arch_object_type
);
274 static PyMethodDef arch_object_methods
[] = {
275 { "name", archpy_name
, METH_NOARGS
,
276 "name () -> String.\n\
277 Return the name of the architecture as a string value." },
278 { "disassemble", (PyCFunction
) archpy_disassemble
,
279 METH_VARARGS
| METH_KEYWORDS
,
280 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
281 Return a list of at most COUNT disassembled instructions from START_PC to\n\
283 {NULL
} /* Sentinel */
286 PyTypeObject arch_object_type
= {
287 PyVarObject_HEAD_INIT (NULL
, 0)
288 "gdb.Architecture", /* tp_name */
289 sizeof (arch_object
), /* tp_basicsize */
297 0, /* tp_as_number */
298 0, /* tp_as_sequence */
299 0, /* tp_as_mapping */
305 0, /* tp_as_buffer */
306 Py_TPFLAGS_DEFAULT
, /* tp_flags */
307 "GDB architecture object", /* tp_doc */
310 0, /* tp_richcompare */
311 0, /* tp_weaklistoffset */
314 arch_object_methods
, /* tp_methods */
319 0, /* tp_descr_get */
320 0, /* tp_descr_set */
321 0, /* tp_dictoffset */