Add constructor and destructor to demangle_parse_info
[deliverable/binutils-gdb.git] / gdb / python / py-arch.c
CommitLineData
bea883fd
SCR
1/* Python interface to architecture
2
61baf725 3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
bea883fd
SCR
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"
9f44fbc0 23#include "disasm.h"
bea883fd 24#include "python-internal.h"
59e9e831 25#include "py-ref.h"
bea883fd
SCR
26
27typedef struct arch_object_type_object {
28 PyObject_HEAD
29 struct gdbarch *gdbarch;
30} arch_object;
31
32static struct gdbarch_data *arch_object_data = NULL;
62eec1a5 33
96d9056e
PM
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
e36122e9 46extern PyTypeObject arch_object_type
62eec1a5 47 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
bea883fd
SCR
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
52static void *
53arch_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
68struct gdbarch *
69arch_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
80PyObject *
81gdbarch_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
95static PyObject *
96archpy_name (PyObject *self, PyObject *args)
97{
96d9056e
PM
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);
bea883fd
SCR
106
107 return py_name;
108}
109
9f44fbc0
SCR
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
116static PyObject *
117archpy_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;
59e9e831 124 PyObject *end_obj = NULL, *count_obj = NULL;
96d9056e
PM
125 struct gdbarch *gdbarch = NULL;
126
127 ARCHPY_REQUIRE_VALID (self, gdbarch);
9f44fbc0
SCR
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 {
06ab7b19
PM
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. */
ddb08e9c
TT
143 if (PyLong_Check (end_obj))
144 end = PyLong_AsUnsignedLongLong (end_obj);
12c58cd4 145#if PY_MAJOR_VERSION == 2
ddb08e9c 146 else if (PyInt_Check (end_obj))
9f44fbc0
SCR
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);
12c58cd4 150#endif
9f44fbc0
SCR
151 else
152 {
9f44fbc0
SCR
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 {
9f44fbc0
SCR
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 {
9f44fbc0
SCR
173 PyErr_SetString (PyExc_TypeError,
174 _("Argument 'count' should be an non-negative "
175 "integer."));
176
177 return NULL;
178 }
179 }
180
59e9e831 181 gdbpy_ref result_list (PyList_New (0));
9f44fbc0
SCR
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;
9f44fbc0 196 struct ui_file *memfile = mem_fileopen ();
59e9e831 197 gdbpy_ref insn_dict (PyDict_New ());
9f44fbc0
SCR
198
199 if (insn_dict == NULL)
200 {
9f44fbc0
SCR
201 ui_file_delete (memfile);
202
203 return NULL;
204 }
59e9e831 205 if (PyList_Append (result_list.get (), insn_dict.get ()))
9f44fbc0 206 {
9f44fbc0
SCR
207 ui_file_delete (memfile);
208
209 return NULL; /* PyList_Append Sets the exception. */
210 }
211
492d29ea 212 TRY
9f44fbc0
SCR
213 {
214 insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
215 }
492d29ea 216 CATCH (except, RETURN_MASK_ALL)
9f44fbc0 217 {
9f44fbc0
SCR
218 ui_file_delete (memfile);
219
56cc411c
TT
220 gdbpy_convert_exception (except);
221 return NULL;
9f44fbc0 222 }
492d29ea 223 END_CATCH
9f44fbc0 224
c92aed16
PA
225 std::string as = ui_file_as_string (memfile);
226
59e9e831 227 if (PyDict_SetItemString (insn_dict.get (), "addr",
9f44fbc0 228 gdb_py_long_from_ulongest (pc))
59e9e831 229 || PyDict_SetItemString (insn_dict.get (), "asm",
c92aed16
PA
230 PyString_FromString (!as.empty ()
231 ? as.c_str ()
232 : "<unknown>"))
59e9e831 233 || PyDict_SetItemString (insn_dict.get (), "length",
9f44fbc0
SCR
234 PyInt_FromLong (insn_len)))
235 {
9f44fbc0 236 ui_file_delete (memfile);
9f44fbc0
SCR
237
238 return NULL;
239 }
240
241 pc += insn_len;
242 i++;
243 ui_file_delete (memfile);
9f44fbc0
SCR
244 }
245
59e9e831 246 return result_list.release ();
9f44fbc0
SCR
247}
248
bea883fd
SCR
249/* Initializes the Architecture class in the gdb module. */
250
999633ed 251int
bea883fd
SCR
252gdbpy_initialize_arch (void)
253{
254 arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
255 arch_object_type.tp_new = PyType_GenericNew;
256 if (PyType_Ready (&arch_object_type) < 0)
999633ed 257 return -1;
bea883fd 258
aa36459a
TT
259 return gdb_pymodule_addobject (gdb_module, "Architecture",
260 (PyObject *) &arch_object_type);
bea883fd
SCR
261}
262
263static PyMethodDef arch_object_methods [] = {
264 { "name", archpy_name, METH_NOARGS,
265 "name () -> String.\n\
266Return the name of the architecture as a string value." },
9f44fbc0
SCR
267 { "disassemble", (PyCFunction) archpy_disassemble,
268 METH_VARARGS | METH_KEYWORDS,
269 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
270Return a list of at most COUNT disassembled instructions from START_PC to\n\
271END_PC." },
bea883fd
SCR
272 {NULL} /* Sentinel */
273};
274
e36122e9 275PyTypeObject arch_object_type = {
bea883fd
SCR
276 PyVarObject_HEAD_INIT (NULL, 0)
277 "gdb.Architecture", /* tp_name */
278 sizeof (arch_object), /* tp_basicsize */
279 0, /* tp_itemsize */
280 0, /* tp_dealloc */
281 0, /* tp_print */
282 0, /* tp_getattr */
283 0, /* tp_setattr */
284 0, /* tp_compare */
285 0, /* tp_repr */
286 0, /* tp_as_number */
287 0, /* tp_as_sequence */
288 0, /* tp_as_mapping */
289 0, /* tp_hash */
290 0, /* tp_call */
291 0, /* tp_str */
292 0, /* tp_getattro */
293 0, /* tp_setattro */
294 0, /* tp_as_buffer */
295 Py_TPFLAGS_DEFAULT, /* tp_flags */
296 "GDB architecture object", /* tp_doc */
297 0, /* tp_traverse */
298 0, /* tp_clear */
299 0, /* tp_richcompare */
300 0, /* tp_weaklistoffset */
301 0, /* tp_iter */
302 0, /* tp_iternext */
303 arch_object_methods, /* tp_methods */
304 0, /* tp_members */
305 0, /* tp_getset */
306 0, /* tp_base */
307 0, /* tp_dict */
308 0, /* tp_descr_get */
309 0, /* tp_descr_set */
310 0, /* tp_dictoffset */
311 0, /* tp_init */
312 0, /* tp_alloc */
313};
This page took 0.423136 seconds and 4 git commands to generate.