Eliminate make_cleanup_ui_file_delete / make ui_file a class hierarchy
[deliverable/binutils-gdb.git] / gdb / python / py-arch.c
1 /* Python interface to architecture
2
3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "gdbarch.h"
22 #include "arch-utils.h"
23 #include "disasm.h"
24 #include "python-internal.h"
25 #include "py-ref.h"
26
27 typedef struct arch_object_type_object {
28 PyObject_HEAD
29 struct gdbarch *gdbarch;
30 } arch_object;
31
32 static struct gdbarch_data *arch_object_data = NULL;
33
34 /* Require a valid Architecture. */
35 #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \
36 do { \
37 arch = arch_object_to_gdbarch (arch_obj); \
38 if (arch == NULL) \
39 { \
40 PyErr_SetString (PyExc_RuntimeError, \
41 _("Architecture is invalid.")); \
42 return NULL; \
43 } \
44 } while (0)
45
46 extern PyTypeObject arch_object_type
47 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
48
49 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
50 post init registration mechanism (gdbarch_data_register_post_init). */
51
52 static void *
53 arch_object_data_init (struct gdbarch *gdbarch)
54 {
55 arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
56
57 if (arch_obj == NULL)
58 return NULL;
59
60 arch_obj->gdbarch = gdbarch;
61
62 return (void *) arch_obj;
63 }
64
65 /* Returns the struct gdbarch value corresponding to the given Python
66 architecture object OBJ. */
67
68 struct gdbarch *
69 arch_object_to_gdbarch (PyObject *obj)
70 {
71 arch_object *py_arch = (arch_object *) obj;
72
73 return py_arch->gdbarch;
74 }
75
76 /* Returns the Python architecture object corresponding to GDBARCH.
77 Returns a new reference to the arch_object associated as data with
78 GDBARCH. */
79
80 PyObject *
81 gdbarch_to_arch_object (struct gdbarch *gdbarch)
82 {
83 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
84
85 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
86 in arch_object_data_init. */
87 Py_XINCREF (new_ref);
88
89 return new_ref;
90 }
91
92 /* Implementation of gdb.Architecture.name (self) -> String.
93 Returns the name of the architecture as a string value. */
94
95 static PyObject *
96 archpy_name (PyObject *self, PyObject *args)
97 {
98 struct gdbarch *gdbarch = NULL;
99 const char *name;
100 PyObject *py_name;
101
102 ARCHPY_REQUIRE_VALID (self, gdbarch);
103
104 name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
105 py_name = PyString_FromString (name);
106
107 return py_name;
108 }
109
110 /* Implementation of
111 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
112 Returns a list of instructions in a memory address range. Each instruction
113 in the list is a Python dict object.
114 */
115
116 static PyObject *
117 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
118 {
119 static char *keywords[] = { "start_pc", "end_pc", "count", NULL };
120 CORE_ADDR start, end = 0;
121 CORE_ADDR pc;
122 gdb_py_ulongest start_temp;
123 long count = 0, i;
124 PyObject *end_obj = NULL, *count_obj = NULL;
125 struct gdbarch *gdbarch = NULL;
126
127 ARCHPY_REQUIRE_VALID (self, gdbarch);
128
129 if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
130 &start_temp, &end_obj, &count_obj))
131 return NULL;
132
133 start = start_temp;
134 if (end_obj)
135 {
136 /* Make a long logic check first. In Python 3.x, internally,
137 all integers are represented as longs. In Python 2.x, there
138 is still a differentiation internally between a PyInt and a
139 PyLong. Explicitly do this long check conversion first. In
140 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
141 to be done first to ensure we do not lose information in the
142 conversion process. */
143 if (PyLong_Check (end_obj))
144 end = PyLong_AsUnsignedLongLong (end_obj);
145 #if PY_MAJOR_VERSION == 2
146 else if (PyInt_Check (end_obj))
147 /* If the end_pc value is specified without a trailing 'L', end_obj will
148 be an integer and not a long integer. */
149 end = PyInt_AsLong (end_obj);
150 #endif
151 else
152 {
153 PyErr_SetString (PyExc_TypeError,
154 _("Argument 'end_pc' should be a (long) integer."));
155
156 return NULL;
157 }
158
159 if (end < start)
160 {
161 PyErr_SetString (PyExc_ValueError,
162 _("Argument 'end_pc' should be greater than or "
163 "equal to the argument 'start_pc'."));
164
165 return NULL;
166 }
167 }
168 if (count_obj)
169 {
170 count = PyInt_AsLong (count_obj);
171 if (PyErr_Occurred () || count < 0)
172 {
173 PyErr_SetString (PyExc_TypeError,
174 _("Argument 'count' should be an non-negative "
175 "integer."));
176
177 return NULL;
178 }
179 }
180
181 gdbpy_ref result_list (PyList_New (0));
182 if (result_list == NULL)
183 return NULL;
184
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);)
194 {
195 int insn_len = 0;
196 gdbpy_ref insn_dict (PyDict_New ());
197
198 if (insn_dict == NULL)
199 return NULL;
200 if (PyList_Append (result_list.get (), insn_dict.get ()))
201 return NULL; /* PyList_Append Sets the exception. */
202
203 string_file stb;
204
205 TRY
206 {
207 insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
208 }
209 CATCH (except, RETURN_MASK_ALL)
210 {
211 gdbpy_convert_exception (except);
212 return NULL;
213 }
214 END_CATCH
215
216 if (PyDict_SetItemString (insn_dict.get (), "addr",
217 gdb_py_long_from_ulongest (pc))
218 || PyDict_SetItemString (insn_dict.get (), "asm",
219 PyString_FromString (!stb.empty ()
220 ? stb.c_str ()
221 : "<unknown>"))
222 || PyDict_SetItemString (insn_dict.get (), "length",
223 PyInt_FromLong (insn_len)))
224 return NULL;
225
226 pc += insn_len;
227 i++;
228 }
229
230 return result_list.release ();
231 }
232
233 /* Initializes the Architecture class in the gdb module. */
234
235 int
236 gdbpy_initialize_arch (void)
237 {
238 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
239 arch_object_type.tp_new = PyType_GenericNew;
240 if (PyType_Ready (&arch_object_type) < 0)
241 return -1;
242
243 return gdb_pymodule_addobject (gdb_module, "Architecture",
244 (PyObject *) &arch_object_type);
245 }
246
247 static PyMethodDef arch_object_methods [] = {
248 { "name", archpy_name, METH_NOARGS,
249 "name () -> String.\n\
250 Return the name of the architecture as a string value." },
251 { "disassemble", (PyCFunction) archpy_disassemble,
252 METH_VARARGS | METH_KEYWORDS,
253 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
254 Return a list of at most COUNT disassembled instructions from START_PC to\n\
255 END_PC." },
256 {NULL} /* Sentinel */
257 };
258
259 PyTypeObject arch_object_type = {
260 PyVarObject_HEAD_INIT (NULL, 0)
261 "gdb.Architecture", /* tp_name */
262 sizeof (arch_object), /* tp_basicsize */
263 0, /* tp_itemsize */
264 0, /* tp_dealloc */
265 0, /* tp_print */
266 0, /* tp_getattr */
267 0, /* tp_setattr */
268 0, /* tp_compare */
269 0, /* tp_repr */
270 0, /* tp_as_number */
271 0, /* tp_as_sequence */
272 0, /* tp_as_mapping */
273 0, /* tp_hash */
274 0, /* tp_call */
275 0, /* tp_str */
276 0, /* tp_getattro */
277 0, /* tp_setattro */
278 0, /* tp_as_buffer */
279 Py_TPFLAGS_DEFAULT, /* tp_flags */
280 "GDB architecture object", /* tp_doc */
281 0, /* tp_traverse */
282 0, /* tp_clear */
283 0, /* tp_richcompare */
284 0, /* tp_weaklistoffset */
285 0, /* tp_iter */
286 0, /* tp_iternext */
287 arch_object_methods, /* tp_methods */
288 0, /* tp_members */
289 0, /* tp_getset */
290 0, /* tp_base */
291 0, /* tp_dict */
292 0, /* tp_descr_get */
293 0, /* tp_descr_set */
294 0, /* tp_dictoffset */
295 0, /* tp_init */
296 0, /* tp_alloc */
297 };
This page took 0.039476 seconds and 4 git commands to generate.