* python/py-arch.c (archpy_disassemble): Use PyInt_Check and
[deliverable/binutils-gdb.git] / gdb / python / py-arch.c
1 /* Python interface to architecture
2
3 Copyright (C) 2013 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
26 typedef struct arch_object_type_object {
27 PyObject_HEAD
28 struct gdbarch *gdbarch;
29 } arch_object;
30
31 static struct gdbarch_data *arch_object_data = NULL;
32 static PyTypeObject arch_object_type;
33
34 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
35 post init registration mechanism (gdbarch_data_register_post_init). */
36
37 static void *
38 arch_object_data_init (struct gdbarch *gdbarch)
39 {
40 arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
41
42 if (arch_obj == NULL)
43 return NULL;
44
45 arch_obj->gdbarch = gdbarch;
46
47 return (void *) arch_obj;
48 }
49
50 /* Returns the struct gdbarch value corresponding to the given Python
51 architecture object OBJ. */
52
53 struct gdbarch *
54 arch_object_to_gdbarch (PyObject *obj)
55 {
56 arch_object *py_arch = (arch_object *) obj;
57
58 return py_arch->gdbarch;
59 }
60
61 /* Returns the Python architecture object corresponding to GDBARCH.
62 Returns a new reference to the arch_object associated as data with
63 GDBARCH. */
64
65 PyObject *
66 gdbarch_to_arch_object (struct gdbarch *gdbarch)
67 {
68 PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
69
70 /* new_ref could be NULL if registration of arch_object with GDBARCH failed
71 in arch_object_data_init. */
72 Py_XINCREF (new_ref);
73
74 return new_ref;
75 }
76
77 /* Implementation of gdb.Architecture.name (self) -> String.
78 Returns the name of the architecture as a string value. */
79
80 static PyObject *
81 archpy_name (PyObject *self, PyObject *args)
82 {
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);
86
87 return py_name;
88 }
89
90 /* Implementation of
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.
94 */
95
96 static PyObject *
97 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
98 {
99 static char *keywords[] = { "start_pc", "end_pc", "count", NULL };
100 CORE_ADDR start, end = 0;
101 CORE_ADDR pc;
102 gdb_py_ulongest start_temp;
103 long count = 0, i;
104 PyObject *result_list, *end_obj = NULL, *count_obj = NULL;
105 struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
106
107 if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
108 &start_temp, &end_obj, &count_obj))
109 return NULL;
110
111 start = start_temp;
112 if (end_obj)
113 {
114 if (PyLong_Check (end_obj))
115 end = PyLong_AsUnsignedLongLong (end_obj);
116 else if (PyInt_Check (end_obj))
117 /* If the end_pc value is specified without a trailing 'L', end_obj will
118 be an integer and not a long integer. */
119 end = PyInt_AsLong (end_obj);
120 else
121 {
122 Py_DECREF (end_obj);
123 Py_XDECREF (count_obj);
124 PyErr_SetString (PyExc_TypeError,
125 _("Argument 'end_pc' should be a (long) integer."));
126
127 return NULL;
128 }
129
130 if (end < start)
131 {
132 Py_DECREF (end_obj);
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'."));
137
138 return NULL;
139 }
140 }
141 if (count_obj)
142 {
143 count = PyInt_AsLong (count_obj);
144 if (PyErr_Occurred () || count < 0)
145 {
146 Py_DECREF (count_obj);
147 Py_XDECREF (end_obj);
148 PyErr_SetString (PyExc_TypeError,
149 _("Argument 'count' should be an non-negative "
150 "integer."));
151
152 return NULL;
153 }
154 }
155
156 result_list = PyList_New (0);
157 if (result_list == NULL)
158 return NULL;
159
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);)
169 {
170 int insn_len = 0;
171 char *as = NULL;
172 struct ui_file *memfile = mem_fileopen ();
173 PyObject *insn_dict = PyDict_New ();
174 volatile struct gdb_exception except;
175
176 if (insn_dict == NULL)
177 {
178 Py_DECREF (result_list);
179 ui_file_delete (memfile);
180
181 return NULL;
182 }
183 if (PyList_Append (result_list, insn_dict))
184 {
185 Py_DECREF (result_list);
186 Py_DECREF (insn_dict);
187 ui_file_delete (memfile);
188
189 return NULL; /* PyList_Append Sets the exception. */
190 }
191
192 TRY_CATCH (except, RETURN_MASK_ALL)
193 {
194 insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
195 }
196 if (except.reason < 0)
197 {
198 Py_DECREF (result_list);
199 ui_file_delete (memfile);
200
201 return gdbpy_convert_exception (except);
202 }
203
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)))
211 {
212 Py_DECREF (result_list);
213
214 ui_file_delete (memfile);
215 xfree (as);
216
217 return NULL;
218 }
219
220 pc += insn_len;
221 i++;
222 ui_file_delete (memfile);
223 xfree (as);
224 }
225
226 return result_list;
227 }
228
229 /* Initializes the Architecture class in the gdb module. */
230
231 void
232 gdbpy_initialize_arch (void)
233 {
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)
237 return;
238
239 Py_INCREF (&arch_object_type);
240 PyModule_AddObject (gdb_module, "Architecture",
241 (PyObject *) &arch_object_type);
242 }
243
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\
252 END_PC." },
253 {NULL} /* Sentinel */
254 };
255
256 static PyTypeObject arch_object_type = {
257 PyVarObject_HEAD_INIT (NULL, 0)
258 "gdb.Architecture", /* tp_name */
259 sizeof (arch_object), /* tp_basicsize */
260 0, /* tp_itemsize */
261 0, /* tp_dealloc */
262 0, /* tp_print */
263 0, /* tp_getattr */
264 0, /* tp_setattr */
265 0, /* tp_compare */
266 0, /* tp_repr */
267 0, /* tp_as_number */
268 0, /* tp_as_sequence */
269 0, /* tp_as_mapping */
270 0, /* tp_hash */
271 0, /* tp_call */
272 0, /* tp_str */
273 0, /* tp_getattro */
274 0, /* tp_setattro */
275 0, /* tp_as_buffer */
276 Py_TPFLAGS_DEFAULT, /* tp_flags */
277 "GDB architecture object", /* tp_doc */
278 0, /* tp_traverse */
279 0, /* tp_clear */
280 0, /* tp_richcompare */
281 0, /* tp_weaklistoffset */
282 0, /* tp_iter */
283 0, /* tp_iternext */
284 arch_object_methods, /* tp_methods */
285 0, /* tp_members */
286 0, /* tp_getset */
287 0, /* tp_base */
288 0, /* tp_dict */
289 0, /* tp_descr_get */
290 0, /* tp_descr_set */
291 0, /* tp_dictoffset */
292 0, /* tp_init */
293 0, /* tp_alloc */
294 };
This page took 0.036189 seconds and 5 git commands to generate.