Commit | Line | Data |
---|---|---|
bea883fd SCR |
1 | /* Python interface to architecture |
2 | ||
3666a048 | 3 | Copyright (C) 2013-2021 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 SCR |
24 | #include "python-internal.h" |
25 | ||
f99b5177 | 26 | struct arch_object { |
bea883fd SCR |
27 | PyObject_HEAD |
28 | struct gdbarch *gdbarch; | |
f99b5177 | 29 | }; |
bea883fd SCR |
30 | |
31 | static struct gdbarch_data *arch_object_data = NULL; | |
62eec1a5 | 32 | |
96d9056e PM |
33 | /* Require a valid Architecture. */ |
34 | #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \ | |
35 | do { \ | |
36 | arch = arch_object_to_gdbarch (arch_obj); \ | |
37 | if (arch == NULL) \ | |
38 | { \ | |
39 | PyErr_SetString (PyExc_RuntimeError, \ | |
40 | _("Architecture is invalid.")); \ | |
41 | return NULL; \ | |
42 | } \ | |
43 | } while (0) | |
44 | ||
e36122e9 | 45 | extern PyTypeObject arch_object_type |
62eec1a5 | 46 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object"); |
bea883fd SCR |
47 | |
48 | /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch | |
49 | post init registration mechanism (gdbarch_data_register_post_init). */ | |
50 | ||
51 | static void * | |
52 | arch_object_data_init (struct gdbarch *gdbarch) | |
53 | { | |
54 | arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type); | |
55 | ||
56 | if (arch_obj == NULL) | |
57 | return NULL; | |
58 | ||
59 | arch_obj->gdbarch = gdbarch; | |
60 | ||
61 | return (void *) arch_obj; | |
62 | } | |
63 | ||
64 | /* Returns the struct gdbarch value corresponding to the given Python | |
65 | architecture object OBJ. */ | |
66 | ||
67 | struct gdbarch * | |
68 | arch_object_to_gdbarch (PyObject *obj) | |
69 | { | |
70 | arch_object *py_arch = (arch_object *) obj; | |
71 | ||
72 | return py_arch->gdbarch; | |
73 | } | |
74 | ||
75 | /* Returns the Python architecture object corresponding to GDBARCH. | |
76 | Returns a new reference to the arch_object associated as data with | |
77 | GDBARCH. */ | |
78 | ||
79 | PyObject * | |
80 | gdbarch_to_arch_object (struct gdbarch *gdbarch) | |
81 | { | |
82 | PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data); | |
83 | ||
84 | /* new_ref could be NULL if registration of arch_object with GDBARCH failed | |
85 | in arch_object_data_init. */ | |
86 | Py_XINCREF (new_ref); | |
87 | ||
88 | return new_ref; | |
89 | } | |
90 | ||
91 | /* Implementation of gdb.Architecture.name (self) -> String. | |
92 | Returns the name of the architecture as a string value. */ | |
93 | ||
94 | static PyObject * | |
95 | archpy_name (PyObject *self, PyObject *args) | |
96 | { | |
96d9056e PM |
97 | struct gdbarch *gdbarch = NULL; |
98 | const char *name; | |
96d9056e PM |
99 | |
100 | ARCHPY_REQUIRE_VALID (self, gdbarch); | |
101 | ||
102 | name = (gdbarch_bfd_arch_info (gdbarch))->printable_name; | |
8833fbf0 | 103 | return PyString_FromString (name); |
bea883fd SCR |
104 | } |
105 | ||
9f44fbc0 SCR |
106 | /* Implementation of |
107 | gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List. | |
108 | Returns a list of instructions in a memory address range. Each instruction | |
109 | in the list is a Python dict object. | |
110 | */ | |
111 | ||
112 | static PyObject * | |
113 | archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) | |
114 | { | |
2adadf51 | 115 | static const char *keywords[] = { "start_pc", "end_pc", "count", NULL }; |
9f44fbc0 SCR |
116 | CORE_ADDR start, end = 0; |
117 | CORE_ADDR pc; | |
118 | gdb_py_ulongest start_temp; | |
119 | long count = 0, i; | |
59e9e831 | 120 | PyObject *end_obj = NULL, *count_obj = NULL; |
96d9056e PM |
121 | struct gdbarch *gdbarch = NULL; |
122 | ||
123 | ARCHPY_REQUIRE_VALID (self, gdbarch); | |
9f44fbc0 | 124 | |
2adadf51 PA |
125 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", |
126 | keywords, &start_temp, &end_obj, | |
127 | &count_obj)) | |
9f44fbc0 SCR |
128 | return NULL; |
129 | ||
130 | start = start_temp; | |
131 | if (end_obj) | |
132 | { | |
06ab7b19 PM |
133 | /* Make a long logic check first. In Python 3.x, internally, |
134 | all integers are represented as longs. In Python 2.x, there | |
135 | is still a differentiation internally between a PyInt and a | |
136 | PyLong. Explicitly do this long check conversion first. In | |
137 | GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has | |
138 | to be done first to ensure we do not lose information in the | |
139 | conversion process. */ | |
ddb08e9c | 140 | if (PyLong_Check (end_obj)) |
dda83cd7 | 141 | end = PyLong_AsUnsignedLongLong (end_obj); |
12c58cd4 | 142 | #if PY_MAJOR_VERSION == 2 |
ddb08e9c | 143 | else if (PyInt_Check (end_obj)) |
dda83cd7 SM |
144 | /* If the end_pc value is specified without a trailing 'L', end_obj will |
145 | be an integer and not a long integer. */ | |
146 | end = PyInt_AsLong (end_obj); | |
12c58cd4 | 147 | #endif |
9f44fbc0 | 148 | else |
dda83cd7 SM |
149 | { |
150 | PyErr_SetString (PyExc_TypeError, | |
151 | _("Argument 'end_pc' should be a (long) integer.")); | |
9f44fbc0 | 152 | |
dda83cd7 SM |
153 | return NULL; |
154 | } | |
9f44fbc0 SCR |
155 | |
156 | if (end < start) | |
dda83cd7 SM |
157 | { |
158 | PyErr_SetString (PyExc_ValueError, | |
159 | _("Argument 'end_pc' should be greater than or " | |
160 | "equal to the argument 'start_pc'.")); | |
9f44fbc0 | 161 | |
dda83cd7 SM |
162 | return NULL; |
163 | } | |
9f44fbc0 SCR |
164 | } |
165 | if (count_obj) | |
166 | { | |
167 | count = PyInt_AsLong (count_obj); | |
168 | if (PyErr_Occurred () || count < 0) | |
dda83cd7 SM |
169 | { |
170 | PyErr_SetString (PyExc_TypeError, | |
171 | _("Argument 'count' should be an non-negative " | |
172 | "integer.")); | |
9f44fbc0 | 173 | |
dda83cd7 SM |
174 | return NULL; |
175 | } | |
9f44fbc0 SCR |
176 | } |
177 | ||
7780f186 | 178 | gdbpy_ref<> result_list (PyList_New (0)); |
9f44fbc0 SCR |
179 | if (result_list == NULL) |
180 | return NULL; | |
181 | ||
182 | for (pc = start, i = 0; | |
183 | /* All args are specified. */ | |
184 | (end_obj && count_obj && pc <= end && i < count) | |
185 | /* end_pc is specified, but no count. */ | |
186 | || (end_obj && count_obj == NULL && pc <= end) | |
187 | /* end_pc is not specified, but a count is. */ | |
188 | || (end_obj == NULL && count_obj && i < count) | |
189 | /* Both end_pc and count are not specified. */ | |
190 | || (end_obj == NULL && count_obj == NULL && pc == start);) | |
191 | { | |
192 | int insn_len = 0; | |
7780f186 | 193 | gdbpy_ref<> insn_dict (PyDict_New ()); |
9f44fbc0 SCR |
194 | |
195 | if (insn_dict == NULL) | |
d7e74731 | 196 | return NULL; |
59e9e831 | 197 | if (PyList_Append (result_list.get (), insn_dict.get ())) |
d7e74731 | 198 | return NULL; /* PyList_Append Sets the exception. */ |
9f44fbc0 | 199 | |
d7e74731 | 200 | string_file stb; |
9f44fbc0 | 201 | |
a70b8144 | 202 | try |
dda83cd7 SM |
203 | { |
204 | insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL); | |
205 | } | |
230d2906 | 206 | catch (const gdb_exception &except) |
dda83cd7 | 207 | { |
56cc411c TT |
208 | gdbpy_convert_exception (except); |
209 | return NULL; | |
dda83cd7 | 210 | } |
9f44fbc0 | 211 | |
d1cab987 TT |
212 | gdbpy_ref<> pc_obj = gdb_py_object_from_ulongest (pc); |
213 | if (pc_obj == nullptr) | |
214 | return nullptr; | |
215 | ||
216 | gdbpy_ref<> asm_obj (PyString_FromString (!stb.empty () | |
217 | ? stb.c_str () | |
218 | : "<unknown>")); | |
219 | if (asm_obj == nullptr) | |
220 | return nullptr; | |
221 | ||
222 | gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len); | |
223 | if (len_obj == nullptr) | |
224 | return nullptr; | |
225 | ||
226 | if (PyDict_SetItemString (insn_dict.get (), "addr", pc_obj.get ()) | |
dda83cd7 SM |
227 | || PyDict_SetItemString (insn_dict.get (), "asm", asm_obj.get ()) |
228 | || PyDict_SetItemString (insn_dict.get (), "length", len_obj.get ())) | |
d7e74731 | 229 | return NULL; |
9f44fbc0 SCR |
230 | |
231 | pc += insn_len; | |
232 | i++; | |
9f44fbc0 SCR |
233 | } |
234 | ||
59e9e831 | 235 | return result_list.release (); |
9f44fbc0 SCR |
236 | } |
237 | ||
0f767f94 AB |
238 | /* Implementation of gdb.Architecture.registers (self, reggroup) -> Iterator. |
239 | Returns an iterator over register descriptors for registers in GROUP | |
240 | within the architecture SELF. */ | |
241 | ||
242 | static PyObject * | |
243 | archpy_registers (PyObject *self, PyObject *args, PyObject *kw) | |
244 | { | |
245 | static const char *keywords[] = { "reggroup", NULL }; | |
246 | struct gdbarch *gdbarch = NULL; | |
247 | const char *group_name = NULL; | |
248 | ||
249 | /* Parse method arguments. */ | |
250 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s", keywords, | |
251 | &group_name)) | |
252 | return NULL; | |
253 | ||
254 | /* Extract the gdbarch from the self object. */ | |
255 | ARCHPY_REQUIRE_VALID (self, gdbarch); | |
256 | ||
257 | return gdbpy_new_register_descriptor_iterator (gdbarch, group_name); | |
258 | } | |
259 | ||
64cb3757 AB |
260 | /* Implementation of gdb.Architecture.register_groups (self) -> Iterator. |
261 | Returns an iterator that will give up all valid register groups in the | |
262 | architecture SELF. */ | |
263 | ||
264 | static PyObject * | |
265 | archpy_register_groups (PyObject *self, PyObject *args) | |
266 | { | |
267 | struct gdbarch *gdbarch = NULL; | |
268 | ||
269 | /* Extract the gdbarch from the self object. */ | |
270 | ARCHPY_REQUIRE_VALID (self, gdbarch); | |
271 | return gdbpy_new_reggroup_iterator (gdbarch); | |
272 | } | |
273 | ||
bea883fd SCR |
274 | /* Initializes the Architecture class in the gdb module. */ |
275 | ||
999633ed | 276 | int |
bea883fd SCR |
277 | gdbpy_initialize_arch (void) |
278 | { | |
279 | arch_object_data = gdbarch_data_register_post_init (arch_object_data_init); | |
280 | arch_object_type.tp_new = PyType_GenericNew; | |
281 | if (PyType_Ready (&arch_object_type) < 0) | |
999633ed | 282 | return -1; |
bea883fd | 283 | |
aa36459a TT |
284 | return gdb_pymodule_addobject (gdb_module, "Architecture", |
285 | (PyObject *) &arch_object_type); | |
bea883fd SCR |
286 | } |
287 | ||
288 | static PyMethodDef arch_object_methods [] = { | |
289 | { "name", archpy_name, METH_NOARGS, | |
290 | "name () -> String.\n\ | |
291 | Return the name of the architecture as a string value." }, | |
9f44fbc0 SCR |
292 | { "disassemble", (PyCFunction) archpy_disassemble, |
293 | METH_VARARGS | METH_KEYWORDS, | |
294 | "disassemble (start_pc [, end_pc [, count]]) -> List.\n\ | |
295 | Return a list of at most COUNT disassembled instructions from START_PC to\n\ | |
296 | END_PC." }, | |
0f767f94 AB |
297 | { "registers", (PyCFunction) archpy_registers, |
298 | METH_VARARGS | METH_KEYWORDS, | |
299 | "registers ([ group-name ]) -> Iterator.\n\ | |
300 | Return an iterator of register descriptors for the registers in register\n\ | |
301 | group GROUP-NAME." }, | |
64cb3757 AB |
302 | { "register_groups", archpy_register_groups, |
303 | METH_NOARGS, | |
304 | "register_groups () -> Iterator.\n\ | |
305 | Return an iterator over all of the register groups in this architecture." }, | |
bea883fd SCR |
306 | {NULL} /* Sentinel */ |
307 | }; | |
308 | ||
e36122e9 | 309 | PyTypeObject arch_object_type = { |
bea883fd SCR |
310 | PyVarObject_HEAD_INIT (NULL, 0) |
311 | "gdb.Architecture", /* tp_name */ | |
312 | sizeof (arch_object), /* tp_basicsize */ | |
313 | 0, /* tp_itemsize */ | |
314 | 0, /* tp_dealloc */ | |
315 | 0, /* tp_print */ | |
316 | 0, /* tp_getattr */ | |
317 | 0, /* tp_setattr */ | |
318 | 0, /* tp_compare */ | |
319 | 0, /* tp_repr */ | |
320 | 0, /* tp_as_number */ | |
321 | 0, /* tp_as_sequence */ | |
322 | 0, /* tp_as_mapping */ | |
323 | 0, /* tp_hash */ | |
324 | 0, /* tp_call */ | |
325 | 0, /* tp_str */ | |
326 | 0, /* tp_getattro */ | |
327 | 0, /* tp_setattro */ | |
328 | 0, /* tp_as_buffer */ | |
329 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
330 | "GDB architecture object", /* tp_doc */ | |
331 | 0, /* tp_traverse */ | |
332 | 0, /* tp_clear */ | |
333 | 0, /* tp_richcompare */ | |
334 | 0, /* tp_weaklistoffset */ | |
335 | 0, /* tp_iter */ | |
336 | 0, /* tp_iternext */ | |
337 | arch_object_methods, /* tp_methods */ | |
338 | 0, /* tp_members */ | |
339 | 0, /* tp_getset */ | |
340 | 0, /* tp_base */ | |
341 | 0, /* tp_dict */ | |
342 | 0, /* tp_descr_get */ | |
343 | 0, /* tp_descr_set */ | |
344 | 0, /* tp_dictoffset */ | |
345 | 0, /* tp_init */ | |
346 | 0, /* tp_alloc */ | |
347 | }; |