GDB (xrefs)
Loading...
Searching...
No Matches
py-arch.c
Go to the documentation of this file.
1/* Python interface to architecture
2
3 Copyright (C) 2013-2023 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
27 PyObject_HEAD
29};
30
31static const registry<gdbarch>::key<PyObject, gdb::noop_deleter<PyObject>>
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
46extern PyTypeObject arch_object_type
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 PyObject *
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 (PyObject *) arch_obj;
63}
64
65/* Returns the struct gdbarch value corresponding to the given Python
66 architecture object OBJ, which must be a gdb.Architecture object. */
67
68struct gdbarch *
70{
71 gdb_assert (gdbpy_is_architecture (obj));
72
73 arch_object *py_arch = (arch_object *) obj;
74 return py_arch->gdbarch;
75}
76
77/* See python-internal.h. */
78
79bool
81{
82 return PyObject_TypeCheck (obj, &arch_object_type);
83}
84
85/* Returns the Python architecture object corresponding to GDBARCH.
86 Returns a new reference to the arch_object associated as data with
87 GDBARCH. */
88
91{
93 if (new_ref == nullptr)
94 {
96 arch_object_data.set (gdbarch, new_ref);
97 }
98
99 /* new_ref could be NULL if creation failed. */
100 Py_XINCREF (new_ref);
101
102 return new_ref;
103}
104
105/* Implementation of gdb.Architecture.name (self) -> String.
106 Returns the name of the architecture as a string value. */
107
108static PyObject *
110{
111 struct gdbarch *gdbarch = NULL;
112 const char *name;
113
115
116 name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
117 return PyUnicode_FromString (name);
118}
119
120/* Implementation of
121 gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
122 Returns a list of instructions in a memory address range. Each instruction
123 in the list is a Python dict object.
124*/
125
126static PyObject *
128{
129 static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
130 CORE_ADDR start = 0, end = 0;
131 CORE_ADDR pc;
132 long count = 0, i;
133 PyObject *start_obj = nullptr, *end_obj = nullptr, *count_obj = nullptr;
134 struct gdbarch *gdbarch = NULL;
135
137
138 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O|OO",
139 keywords, &start_obj, &end_obj,
140 &count_obj))
141 return NULL;
142
143 if (get_addr_from_python (start_obj, &start) < 0)
144 return nullptr;
145
146 if (end_obj != nullptr)
147 {
148 if (get_addr_from_python (end_obj, &end) < 0)
149 return nullptr;
150
151 if (end < start)
152 {
153 PyErr_SetString (PyExc_ValueError,
154 _("Argument 'end_pc' should be greater than or "
155 "equal to the argument 'start_pc'."));
156
157 return NULL;
158 }
159 }
160 if (count_obj)
161 {
162 count = PyLong_AsLong (count_obj);
163 if (PyErr_Occurred () || count < 0)
164 {
165 PyErr_SetString (PyExc_TypeError,
166 _("Argument 'count' should be an non-negative "
167 "integer."));
168
169 return NULL;
170 }
171 }
172
173 gdbpy_ref<> result_list (PyList_New (0));
174 if (result_list == NULL)
175 return NULL;
176
177 for (pc = start, i = 0;
178 /* All args are specified. */
179 (end_obj && count_obj && pc <= end && i < count)
180 /* end_pc is specified, but no count. */
181 || (end_obj && count_obj == NULL && pc <= end)
182 /* end_pc is not specified, but a count is. */
183 || (end_obj == NULL && count_obj && i < count)
184 /* Both end_pc and count are not specified. */
185 || (end_obj == NULL && count_obj == NULL && pc == start);)
186 {
187 int insn_len = 0;
188 gdbpy_ref<> insn_dict (PyDict_New ());
189
190 if (insn_dict == NULL)
191 return NULL;
192 if (PyList_Append (result_list.get (), insn_dict.get ()))
193 return NULL; /* PyList_Append Sets the exception. */
194
195 string_file stb;
196
197 try
198 {
199 insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
200 }
201 catch (const gdb_exception &except)
202 {
204 return NULL;
205 }
206
208 if (pc_obj == nullptr)
209 return nullptr;
210
211 gdbpy_ref<> asm_obj
212 (PyUnicode_FromString (!stb.empty () ? stb.c_str () : "<unknown>"));
213 if (asm_obj == nullptr)
214 return nullptr;
215
216 gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len);
217 if (len_obj == nullptr)
218 return nullptr;
219
220 if (PyDict_SetItemString (insn_dict.get (), "addr", pc_obj.get ())
221 || PyDict_SetItemString (insn_dict.get (), "asm", asm_obj.get ())
222 || PyDict_SetItemString (insn_dict.get (), "length", len_obj.get ()))
223 return NULL;
224
225 pc += insn_len;
226 i++;
227 }
228
229 return result_list.release ();
230}
231
232/* Implementation of gdb.Architecture.registers (self, reggroup) -> Iterator.
233 Returns an iterator over register descriptors for registers in GROUP
234 within the architecture SELF. */
235
236static PyObject *
238{
239 static const char *keywords[] = { "reggroup", NULL };
240 struct gdbarch *gdbarch = NULL;
241 const char *group_name = NULL;
242
243 /* Parse method arguments. */
244 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s", keywords,
245 &group_name))
246 return NULL;
247
248 /* Extract the gdbarch from the self object. */
250
252}
253
254/* Implementation of gdb.Architecture.register_groups (self) -> Iterator.
255 Returns an iterator that will give up all valid register groups in the
256 architecture SELF. */
257
258static PyObject *
260{
261 struct gdbarch *gdbarch = NULL;
262
263 /* Extract the gdbarch from the self object. */
266}
267
268/* Implementation of gdb.integer_type. */
269static PyObject *
271{
272 static const char *keywords[] = { "size", "signed", NULL };
273 int size;
274 PyObject *is_signed_obj = nullptr;
275
276 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "i|O", keywords,
277 &size, &is_signed_obj))
278 return nullptr;
279
280 /* Assume signed by default. */
281 bool is_signed = (is_signed_obj == nullptr
282 || PyObject_IsTrue (is_signed_obj));
283
284 struct gdbarch *gdbarch;
286
287 const struct builtin_type *builtins = builtin_type (gdbarch);
288 struct type *type = nullptr;
289 switch (size)
290 {
291 case 0:
292 type = builtins->builtin_int0;
293 break;
294 case 8:
295 type = is_signed ? builtins->builtin_int8 : builtins->builtin_uint8;
296 break;
297 case 16:
298 type = is_signed ? builtins->builtin_int16 : builtins->builtin_uint16;
299 break;
300 case 24:
301 type = is_signed ? builtins->builtin_int24 : builtins->builtin_uint24;
302 break;
303 case 32:
304 type = is_signed ? builtins->builtin_int32 : builtins->builtin_uint32;
305 break;
306 case 64:
307 type = is_signed ? builtins->builtin_int64 : builtins->builtin_uint64;
308 break;
309 case 128:
310 type = is_signed ? builtins->builtin_int128 : builtins->builtin_uint128;
311 break;
312
313 default:
314 PyErr_SetString (PyExc_ValueError,
315 _("no integer type of that size is available"));
316 return nullptr;
317 }
318
319 return type_to_type_object (type);
320}
321
322/* __repr__ implementation for gdb.Architecture. */
323
324static PyObject *
326{
327 const auto gdbarch = arch_object_to_gdbarch (self);
328 if (gdbarch == nullptr)
329 return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
330
331 auto arch_info = gdbarch_bfd_arch_info (gdbarch);
332 return PyUnicode_FromFormat ("<%s arch_name=%s printable_name=%s>",
333 Py_TYPE (self)->tp_name, arch_info->arch_name,
334 arch_info->printable_name);
335}
336
337/* Implementation of gdb.architecture_names(). Return a list of all the
338 BFD architecture names that GDB understands. */
339
340PyObject *
342{
343 gdbpy_ref<> list (PyList_New (0));
344 if (list == nullptr)
345 return nullptr;
346
347 std::vector<const char *> name_list = gdbarch_printable_names ();
348 for (const char *name : name_list)
349 {
350 gdbpy_ref <> py_name (PyUnicode_FromString (name));
351 if (py_name == nullptr)
352 return nullptr;
353 if (PyList_Append (list.get (), py_name.get ()) < 0)
354 return nullptr;
355 }
356
357 return list.release ();
358}
359
360/* Initializes the Architecture class in the gdb module. */
361
364{
365 arch_object_type.tp_new = PyType_GenericNew;
366 if (PyType_Ready (&arch_object_type) < 0)
367 return -1;
368
369 return gdb_pymodule_addobject (gdb_module, "Architecture",
371}
372
374
375
376
377static PyMethodDef arch_object_methods [] = {
378 { "name", archpy_name, METH_NOARGS,
379 "name () -> String.\n\
380Return the name of the architecture as a string value." },
381 { "disassemble", (PyCFunction) archpy_disassemble,
382 METH_VARARGS | METH_KEYWORDS,
383 "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
384Return a list of at most COUNT disassembled instructions from START_PC to\n\
385END_PC." },
386 { "integer_type", (PyCFunction) archpy_integer_type,
387 METH_VARARGS | METH_KEYWORDS,
388 "integer_type (size [, signed]) -> type\n\
389Return an integer Type corresponding to the given bitsize and signed-ness.\n\
390If not specified, the type defaults to signed." },
391 { "registers", (PyCFunction) archpy_registers,
392 METH_VARARGS | METH_KEYWORDS,
393 "registers ([ group-name ]) -> Iterator.\n\
394Return an iterator of register descriptors for the registers in register\n\
395group GROUP-NAME." },
396 { "register_groups", archpy_register_groups,
397 METH_NOARGS,
398 "register_groups () -> Iterator.\n\
399Return an iterator over all of the register groups in this architecture." },
400 {NULL} /* Sentinel */
401};
402
403PyTypeObject arch_object_type = {
404 PyVarObject_HEAD_INIT (NULL, 0)
405 "gdb.Architecture", /* tp_name */
406 sizeof (arch_object), /* tp_basicsize */
407 0, /* tp_itemsize */
408 0, /* tp_dealloc */
409 0, /* tp_print */
410 0, /* tp_getattr */
411 0, /* tp_setattr */
412 0, /* tp_compare */
413 archpy_repr, /* tp_repr */
414 0, /* tp_as_number */
415 0, /* tp_as_sequence */
416 0, /* tp_as_mapping */
417 0, /* tp_hash */
418 0, /* tp_call */
419 0, /* tp_str */
420 0, /* tp_getattro */
421 0, /* tp_setattro */
422 0, /* tp_as_buffer */
423 Py_TPFLAGS_DEFAULT, /* tp_flags */
424 "GDB architecture object", /* tp_doc */
425 0, /* tp_traverse */
426 0, /* tp_clear */
427 0, /* tp_richcompare */
428 0, /* tp_weaklistoffset */
429 0, /* tp_iter */
430 0, /* tp_iternext */
431 arch_object_methods, /* tp_methods */
432 0, /* tp_members */
433 0, /* tp_getset */
434 0, /* tp_base */
435 0, /* tp_dict */
436 0, /* tp_descr_get */
437 0, /* tp_descr_set */
438 0, /* tp_dictoffset */
439 0, /* tp_init */
440 0, /* tp_alloc */
441};
const char *const name
std::vector< const char * > gdbarch_printable_names()
void set(unsigned key, void *datum)
Definition registry.h:204
void * get(unsigned key)
Definition registry.h:211
const char * c_str() const
Definition ui-file.h:222
bool empty() const
Definition ui-file.h:224
int gdb_print_insn(struct gdbarch *gdbarch, CORE_ADDR memaddr, struct ui_file *stream, int *branch_delay_insns)
Definition disasm.c:1217
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
Definition gdbarch.c:1387
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
size_t size
Definition go32-nat.c:239
PyObject * gdbpy_all_architecture_names(PyObject *self, PyObject *args)
Definition py-arch.c:341
static PyObject * arch_object_data_init(struct gdbarch *gdbarch)
Definition py-arch.c:53
static PyObject * archpy_register_groups(PyObject *self, PyObject *args)
Definition py-arch.c:259
static PyMethodDef arch_object_methods[]
Definition py-arch.c:377
bool gdbpy_is_architecture(PyObject *obj)
Definition py-arch.c:80
struct gdbarch * arch_object_to_gdbarch(PyObject *obj)
Definition py-arch.c:69
static PyObject * archpy_name(PyObject *self, PyObject *args)
Definition py-arch.c:109
static PyObject * archpy_disassemble(PyObject *self, PyObject *args, PyObject *kw)
Definition py-arch.c:127
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_arch(void)
Definition py-arch.c:363
static PyObject * archpy_registers(PyObject *self, PyObject *args, PyObject *kw)
Definition py-arch.c:237
static const registry< gdbarch >::key< PyObject, gdb::noop_deleter< PyObject > > arch_object_data
Definition py-arch.c:32
PyObject * gdbarch_to_arch_object(struct gdbarch *gdbarch)
Definition py-arch.c:90
static PyObject * archpy_repr(PyObject *self)
Definition py-arch.c:325
static PyObject * archpy_integer_type(PyObject *self, PyObject *args, PyObject *kw)
Definition py-arch.c:270
#define ARCHPY_REQUIRE_VALID(arch_obj, arch)
Definition py-arch.c:35
PyTypeObject arch_object_type
Definition py-arch.c:403
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
Definition py-ref.h:42
PyObject * gdbpy_new_register_descriptor_iterator(struct gdbarch *gdbarch, const char *group_name)
PyObject * gdbpy_new_reggroup_iterator(struct gdbarch *gdbarch)
PyObject * type_to_type_object(struct type *type)
Definition py-type.c:1460
gdbpy_ref gdb_py_object_from_longest(LONGEST l)
Definition py-utils.c:282
int get_addr_from_python(PyObject *obj, CORE_ADDR *addr)
Definition py-utils.c:239
void gdbpy_convert_exception(const struct gdb_exception &exception)
Definition py-utils.c:217
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition py-utils.c:334
gdbpy_ref gdb_py_object_from_ulongest(ULONGEST l)
Definition py-utils.c:293
PyObject * gdb_module
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#define GDBPY_INITIALIZE_FILE(INIT,...)
static int gdb_PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, const char **keywords,...)
PyObject_HEAD struct gdbarch * gdbarch
Definition py-arch.c:28
struct type * builtin_int0
Definition gdbtypes.h:2112
struct type * builtin_uint16
Definition gdbtypes.h:2116
struct type * builtin_int24
Definition gdbtypes.h:2117
struct type * builtin_int8
Definition gdbtypes.h:2113
struct type * builtin_uint128
Definition gdbtypes.h:2124
struct type * builtin_uint32
Definition gdbtypes.h:2120
struct type * builtin_uint64
Definition gdbtypes.h:2122
struct type * builtin_int64
Definition gdbtypes.h:2121
struct type * builtin_uint24
Definition gdbtypes.h:2118
struct type * builtin_int32
Definition gdbtypes.h:2119
struct type * builtin_uint8
Definition gdbtypes.h:2114
struct type * builtin_int128
Definition gdbtypes.h:2123
struct type * builtin_int16
Definition gdbtypes.h:2115
int PyObject
Definition varobj.c:41