]>
Commit | Line | Data |
---|---|---|
bea883fd SCR |
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" | |
9f44fbc0 | 23 | #include "disasm.h" |
bea883fd SCR |
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; | |
62eec1a5 TT |
32 | |
33 | static PyTypeObject arch_object_type | |
34 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object"); | |
bea883fd SCR |
35 | |
36 | /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch | |
37 | post init registration mechanism (gdbarch_data_register_post_init). */ | |
38 | ||
39 | static void * | |
40 | arch_object_data_init (struct gdbarch *gdbarch) | |
41 | { | |
42 | arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type); | |
43 | ||
44 | if (arch_obj == NULL) | |
45 | return NULL; | |
46 | ||
47 | arch_obj->gdbarch = gdbarch; | |
48 | ||
49 | return (void *) arch_obj; | |
50 | } | |
51 | ||
52 | /* Returns the struct gdbarch value corresponding to the given Python | |
53 | architecture object OBJ. */ | |
54 | ||
55 | struct gdbarch * | |
56 | arch_object_to_gdbarch (PyObject *obj) | |
57 | { | |
58 | arch_object *py_arch = (arch_object *) obj; | |
59 | ||
60 | return py_arch->gdbarch; | |
61 | } | |
62 | ||
63 | /* Returns the Python architecture object corresponding to GDBARCH. | |
64 | Returns a new reference to the arch_object associated as data with | |
65 | GDBARCH. */ | |
66 | ||
67 | PyObject * | |
68 | gdbarch_to_arch_object (struct gdbarch *gdbarch) | |
69 | { | |
70 | PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data); | |
71 | ||
72 | /* new_ref could be NULL if registration of arch_object with GDBARCH failed | |
73 | in arch_object_data_init. */ | |
74 | Py_XINCREF (new_ref); | |
75 | ||
76 | return new_ref; | |
77 | } | |
78 | ||
79 | /* Implementation of gdb.Architecture.name (self) -> String. | |
80 | Returns the name of the architecture as a string value. */ | |
81 | ||
82 | static PyObject * | |
83 | archpy_name (PyObject *self, PyObject *args) | |
84 | { | |
85 | struct gdbarch *gdbarch = arch_object_to_gdbarch (self); | |
86 | const char *name = (gdbarch_bfd_arch_info (gdbarch))->printable_name; | |
87 | PyObject *py_name = PyString_FromString (name); | |
88 | ||
89 | return py_name; | |
90 | } | |
91 | ||
9f44fbc0 SCR |
92 | /* Implementation of |
93 | gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List. | |
94 | Returns a list of instructions in a memory address range. Each instruction | |
95 | in the list is a Python dict object. | |
96 | */ | |
97 | ||
98 | static PyObject * | |
99 | archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) | |
100 | { | |
101 | static char *keywords[] = { "start_pc", "end_pc", "count", NULL }; | |
102 | CORE_ADDR start, end = 0; | |
103 | CORE_ADDR pc; | |
104 | gdb_py_ulongest start_temp; | |
105 | long count = 0, i; | |
106 | PyObject *result_list, *end_obj = NULL, *count_obj = NULL; | |
107 | struct gdbarch *gdbarch = arch_object_to_gdbarch (self); | |
108 | ||
109 | if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords, | |
110 | &start_temp, &end_obj, &count_obj)) | |
111 | return NULL; | |
112 | ||
113 | start = start_temp; | |
114 | if (end_obj) | |
115 | { | |
ddb08e9c TT |
116 | if (PyLong_Check (end_obj)) |
117 | end = PyLong_AsUnsignedLongLong (end_obj); | |
118 | else if (PyInt_Check (end_obj)) | |
9f44fbc0 SCR |
119 | /* If the end_pc value is specified without a trailing 'L', end_obj will |
120 | be an integer and not a long integer. */ | |
121 | end = PyInt_AsLong (end_obj); | |
9f44fbc0 SCR |
122 | else |
123 | { | |
124 | Py_DECREF (end_obj); | |
125 | Py_XDECREF (count_obj); | |
126 | PyErr_SetString (PyExc_TypeError, | |
127 | _("Argument 'end_pc' should be a (long) integer.")); | |
128 | ||
129 | return NULL; | |
130 | } | |
131 | ||
132 | if (end < start) | |
133 | { | |
134 | Py_DECREF (end_obj); | |
135 | Py_XDECREF (count_obj); | |
136 | PyErr_SetString (PyExc_ValueError, | |
137 | _("Argument 'end_pc' should be greater than or " | |
138 | "equal to the argument 'start_pc'.")); | |
139 | ||
140 | return NULL; | |
141 | } | |
142 | } | |
143 | if (count_obj) | |
144 | { | |
145 | count = PyInt_AsLong (count_obj); | |
146 | if (PyErr_Occurred () || count < 0) | |
147 | { | |
148 | Py_DECREF (count_obj); | |
149 | Py_XDECREF (end_obj); | |
150 | PyErr_SetString (PyExc_TypeError, | |
151 | _("Argument 'count' should be an non-negative " | |
152 | "integer.")); | |
153 | ||
154 | return NULL; | |
155 | } | |
156 | } | |
157 | ||
158 | result_list = PyList_New (0); | |
159 | if (result_list == NULL) | |
160 | return NULL; | |
161 | ||
162 | for (pc = start, i = 0; | |
163 | /* All args are specified. */ | |
164 | (end_obj && count_obj && pc <= end && i < count) | |
165 | /* end_pc is specified, but no count. */ | |
166 | || (end_obj && count_obj == NULL && pc <= end) | |
167 | /* end_pc is not specified, but a count is. */ | |
168 | || (end_obj == NULL && count_obj && i < count) | |
169 | /* Both end_pc and count are not specified. */ | |
170 | || (end_obj == NULL && count_obj == NULL && pc == start);) | |
171 | { | |
172 | int insn_len = 0; | |
173 | char *as = NULL; | |
174 | struct ui_file *memfile = mem_fileopen (); | |
175 | PyObject *insn_dict = PyDict_New (); | |
176 | volatile struct gdb_exception except; | |
177 | ||
178 | if (insn_dict == NULL) | |
179 | { | |
180 | Py_DECREF (result_list); | |
181 | ui_file_delete (memfile); | |
182 | ||
183 | return NULL; | |
184 | } | |
185 | if (PyList_Append (result_list, insn_dict)) | |
186 | { | |
187 | Py_DECREF (result_list); | |
188 | Py_DECREF (insn_dict); | |
189 | ui_file_delete (memfile); | |
190 | ||
191 | return NULL; /* PyList_Append Sets the exception. */ | |
192 | } | |
193 | ||
194 | TRY_CATCH (except, RETURN_MASK_ALL) | |
195 | { | |
196 | insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL); | |
197 | } | |
198 | if (except.reason < 0) | |
199 | { | |
200 | Py_DECREF (result_list); | |
201 | ui_file_delete (memfile); | |
202 | ||
56cc411c TT |
203 | gdbpy_convert_exception (except); |
204 | return NULL; | |
9f44fbc0 SCR |
205 | } |
206 | ||
207 | as = ui_file_xstrdup (memfile, NULL); | |
208 | if (PyDict_SetItemString (insn_dict, "addr", | |
209 | gdb_py_long_from_ulongest (pc)) | |
210 | || PyDict_SetItemString (insn_dict, "asm", | |
211 | PyString_FromString (*as ? as : "<unknown>")) | |
212 | || PyDict_SetItemString (insn_dict, "length", | |
213 | PyInt_FromLong (insn_len))) | |
214 | { | |
215 | Py_DECREF (result_list); | |
216 | ||
217 | ui_file_delete (memfile); | |
218 | xfree (as); | |
219 | ||
220 | return NULL; | |
221 | } | |
222 | ||
223 | pc += insn_len; | |
224 | i++; | |
225 | ui_file_delete (memfile); | |
226 | xfree (as); | |
227 | } | |
228 | ||
229 | return result_list; | |
230 | } | |
231 | ||
bea883fd SCR |
232 | /* Initializes the Architecture class in the gdb module. */ |
233 | ||
234 | void | |
235 | gdbpy_initialize_arch (void) | |
236 | { | |
237 | arch_object_data = gdbarch_data_register_post_init (arch_object_data_init); | |
238 | arch_object_type.tp_new = PyType_GenericNew; | |
239 | if (PyType_Ready (&arch_object_type) < 0) | |
240 | return; | |
241 | ||
242 | Py_INCREF (&arch_object_type); | |
243 | 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." }, | |
9f44fbc0 SCR |
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." }, | |
bea883fd SCR |
256 | {NULL} /* Sentinel */ |
257 | }; | |
258 | ||
259 | static 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 | }; |