GDB (xrefs)
Loading...
Searching...
No Matches
py-registers.c
Go to the documentation of this file.
1/* Python interface to register, and register group information.
2
3 Copyright (C) 2020-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 "reggroups.h"
24#include "python-internal.h"
25#include "user-regs.h"
26#include <unordered_map>
27
28/* Per-gdbarch data type. */
29typedef std::vector<gdbpy_ref<>> gdbpy_register_type;
30
31/* Token to access per-gdbarch data related to register descriptors. */
34
35/* Structure for iterator over register descriptors. */
37 PyObject_HEAD
38
39 /* The register group that the user is iterating over. This will never
40 be NULL. */
41 const struct reggroup *reggroup;
42
43 /* The next register number to lookup. Starts at 0 and counts up. */
44 int regnum;
45
46 /* Pointer back to the architecture we're finding registers for. */
48};
49
51 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_iterator_object");
52
53/* A register descriptor. */
55 PyObject_HEAD
56
57 /* The register this is a descriptor for. */
58 int regnum;
59
60 /* The architecture this is a register for. */
62};
63
64extern PyTypeObject register_descriptor_object_type
65 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_object");
66
67/* Structure for iterator over register groups. */
69 PyObject_HEAD
70
71 /* The index into GROUPS for the next group to return. */
72 std::vector<const reggroup *>::size_type index;
73
74 /* Pointer back to the architecture we're finding registers for. */
76};
77
78extern PyTypeObject reggroup_iterator_object_type
79 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_iterator_object");
80
81/* A register group object. */
83 PyObject_HEAD
84
85 /* The register group being described. */
86 const struct reggroup *reggroup;
87};
88
89extern PyTypeObject reggroup_object_type
91
92/* Return a gdb.RegisterGroup object wrapping REGGROUP. The register
93 group objects are cached, and the same Python object will always be
94 returned for the same REGGROUP pointer. */
95
96static gdbpy_ref<>
98{
99 /* Map from GDB's internal reggroup objects to the Python representation.
100 GDB's reggroups are global, and are never deleted, so using a map like
101 this is safe. */
102 static std::unordered_map<const struct reggroup *,gdbpy_ref<>>
103 gdbpy_reggroup_object_map;
104
105 /* If there is not already a suitable Python object in the map then
106 create a new one, and add it to the map. */
107 if (gdbpy_reggroup_object_map[reggroup] == nullptr)
108 {
109 /* Create a new object and fill in its details. */
111 (PyObject_New (reggroup_object, &reggroup_object_type));
112 if (group == NULL)
113 return NULL;
114 group->reggroup = reggroup;
115 gdbpy_reggroup_object_map[reggroup]
116 = gdbpy_ref<> ((PyObject *) group.release ());
117 }
118
119 /* Fetch the Python object wrapping REGGROUP from the map, increasing
120 the reference count is handled by the gdbpy_ref class. */
121 return gdbpy_reggroup_object_map[reggroup];
122}
123
124/* Convert a gdb.RegisterGroup to a string, it just returns the name of
125 the register group. */
126
127static PyObject *
129{
130 reggroup_object *group = (reggroup_object *) self;
131 const reggroup *reggroup = group->reggroup;
132
133 return PyUnicode_FromString (reggroup->name ());
134}
135
136/* Implement gdb.RegisterGroup.name (self) -> String.
137 Return a string that is the name of this register group. */
138
139static PyObject *
140gdbpy_reggroup_name (PyObject *self, void *closure)
141{
142 return gdbpy_reggroup_to_string (self);
143}
144
145/* Return a gdb.RegisterDescriptor object for REGNUM from GDBARCH. For
146 each REGNUM (in GDBARCH) only one descriptor is ever created, which is
147 then cached on the GDBARCH. */
148
149static gdbpy_ref<>
151 int regnum)
152{
154 if (vecp == nullptr)
155 vecp = gdbpy_register_object_data.emplace (gdbarch);
156 gdbpy_register_type &vec = *vecp;
157
158 /* Ensure that we have enough entries in the vector. */
159 if (vec.size () <= regnum)
160 vec.resize ((regnum + 1), nullptr);
161
162 /* If we don't already have a descriptor for REGNUM in GDBARCH then
163 create one now. */
164 if (vec[regnum] == nullptr)
165 {
166 gdbpy_ref <register_descriptor_object> reg
167 (PyObject_New (register_descriptor_object,
169 if (reg == NULL)
170 return NULL;
171 reg->regnum = regnum;
172 reg->gdbarch = gdbarch;
173 vec[regnum] = gdbpy_ref<> ((PyObject *) reg.release ());
174 }
175
176 /* Grab the register descriptor from the vector, the reference count is
177 automatically incremented thanks to gdbpy_ref. */
178 return vec[regnum];
179}
180
181/* Convert the register descriptor to a string. */
182
183static PyObject *
185{
188 struct gdbarch *gdbarch = reg->gdbarch;
189 int regnum = reg->regnum;
190
191 const char *name = gdbarch_register_name (gdbarch, regnum);
192 return PyUnicode_FromString (name);
193}
194
195/* Implement gdb.RegisterDescriptor.name attribute get function. Return a
196 string that is the name of this register. Due to checking when register
197 descriptors are created the name will never by the empty string. */
198
199static PyObject *
201{
203}
204
205/* Return a reference to the gdb.RegisterGroupsIterator object. */
206
207static PyObject *
209{
210 Py_INCREF (self);
211 return self;
212}
213
214/* Return the next gdb.RegisterGroup object from the iterator. */
215
216static PyObject *
218{
220 = (reggroup_iterator_object *) self;
221
222 const std::vector<const reggroup *> &groups
223 = gdbarch_reggroups (iter_obj->gdbarch);
224 if (iter_obj->index >= groups.size ())
225 {
226 PyErr_SetString (PyExc_StopIteration, _("No more groups"));
227 return NULL;
228 }
229
230 const reggroup *group = groups[iter_obj->index];
231 iter_obj->index++;
232 return gdbpy_get_reggroup (group).release ();
233}
234
235/* Return a new gdb.RegisterGroupsIterator over all the register groups in
236 GDBARCH. */
237
238PyObject *
240{
241 gdb_assert (gdbarch != nullptr);
242
243 /* Create a new object and fill in its internal state. */
245 = PyObject_New (reggroup_iterator_object,
247 if (iter == NULL)
248 return NULL;
249 iter->index = 0;
250 iter->gdbarch = gdbarch;
251 return (PyObject *) iter;
252}
253
254/* Create and return a new gdb.RegisterDescriptorIterator object which
255 will iterate over all registers in GROUP_NAME for GDBARCH. If
256 GROUP_NAME is either NULL or the empty string then the ALL_REGGROUP is
257 used, otherwise lookup the register group matching GROUP_NAME and use
258 that.
259
260 This function can return NULL if GROUP_NAME isn't found. */
261
262PyObject *
264 const char *group_name)
265{
266 const reggroup *grp = NULL;
267
268 /* Lookup the requested register group, or find the default. */
269 if (group_name == NULL || *group_name == '\0')
270 grp = all_reggroup;
271 else
272 {
273 grp = reggroup_find (gdbarch, group_name);
274 if (grp == NULL)
275 {
276 PyErr_SetString (PyExc_ValueError,
277 _("Unknown register group name."));
278 return NULL;
279 }
280 }
281 /* Create a new iterator object initialised for this architecture and
282 fill in all of the details. */
286 if (iter == NULL)
287 return NULL;
288 iter->regnum = 0;
289 iter->gdbarch = gdbarch;
290 gdb_assert (grp != NULL);
291 iter->reggroup = grp;
292
293 return (PyObject *) iter;
294}
295
296/* Return a reference to the gdb.RegisterDescriptorIterator object. */
297
298static PyObject *
300{
301 Py_INCREF (self);
302 return self;
303}
304
305/* Return the next register name. */
306
307static PyObject *
309{
312 struct gdbarch *gdbarch = iter_obj->gdbarch;
313
314 do
315 {
316 if (iter_obj->regnum >= gdbarch_num_cooked_regs (gdbarch))
317 {
318 PyErr_SetString (PyExc_StopIteration, _("No more registers"));
319 return NULL;
320 }
321
322 const char *name = nullptr;
323 int regnum = iter_obj->regnum;
325 iter_obj->reggroup))
327 iter_obj->regnum++;
328
329 if (name != nullptr && *name != '\0')
330 return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
331 }
332 while (true);
333}
334
335/* Implement:
336
337 gdb.RegisterDescriptorIterator.find (self, name) -> gdb.RegisterDescriptor
338
339 Look up a descriptor for register with NAME. If no matching register is
340 found then return None. */
341
342static PyObject *
344{
345 static const char *keywords[] = { "name", NULL };
346 const char *register_name = NULL;
347
350 struct gdbarch *gdbarch = iter_obj->gdbarch;
351
352 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords,
354 return NULL;
355
356 if (register_name != NULL && *register_name != '\0')
357 {
359 strlen (register_name));
360 if (regnum >= 0)
361 return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
362 }
363
364 Py_RETURN_NONE;
365}
366
367/* See python-internal.h. */
368
369bool
371 int *reg_num)
372{
373 gdb_assert (pyo_reg_id != NULL);
374
375 /* The register could be a string, its name. */
376 if (gdbpy_is_string (pyo_reg_id))
377 {
378 gdb::unique_xmalloc_ptr<char> reg_name (gdbpy_obj_to_string (pyo_reg_id));
379
380 if (reg_name != NULL)
381 {
382 *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (),
383 strlen (reg_name.get ()));
384 if (*reg_num >= 0)
385 return true;
386 PyErr_SetString (PyExc_ValueError, "Bad register");
387 }
388 }
389 /* The register could be its internal GDB register number. */
390 else if (PyLong_Check (pyo_reg_id))
391 {
392 long value;
393 if (gdb_py_int_as_long (pyo_reg_id, &value) == 0)
394 {
395 /* Nothing -- error. */
396 }
397 else if ((int) value == value
399 {
400 *reg_num = (int) value;
401 return true;
402 }
403 else
404 PyErr_SetString (PyExc_ValueError, "Bad register");
405 }
406 /* The register could be a gdb.RegisterDescriptor object. */
407 else if (PyObject_IsInstance (pyo_reg_id,
409 {
411 = (register_descriptor_object *) pyo_reg_id;
412 if (reg->gdbarch == gdbarch)
413 {
414 *reg_num = reg->regnum;
415 return true;
416 }
417 else
418 PyErr_SetString (PyExc_ValueError,
419 _("Invalid Architecture in RegisterDescriptor"));
420 }
421 else
422 PyErr_SetString (PyExc_TypeError, _("Invalid type for register"));
423
424 gdb_assert (PyErr_Occurred ());
425 return false;
426}
427
428/* Initializes the new Python classes from this file in the gdb module. */
429
432{
433 register_descriptor_object_type.tp_new = PyType_GenericNew;
434 if (PyType_Ready (&register_descriptor_object_type) < 0)
435 return -1;
437 (gdb_module, "RegisterDescriptor",
439 return -1;
440
441 reggroup_iterator_object_type.tp_new = PyType_GenericNew;
442 if (PyType_Ready (&reggroup_iterator_object_type) < 0)
443 return -1;
445 (gdb_module, "RegisterGroupsIterator",
447 return -1;
448
449 reggroup_object_type.tp_new = PyType_GenericNew;
450 if (PyType_Ready (&reggroup_object_type) < 0)
451 return -1;
453 (gdb_module, "RegisterGroup",
455 return -1;
456
457 register_descriptor_iterator_object_type.tp_new = PyType_GenericNew;
458 if (PyType_Ready (&register_descriptor_iterator_object_type) < 0)
459 return -1;
461 (gdb_module, "RegisterDescriptorIterator",
463}
464
466
467
468
470 { "find", (PyCFunction) register_descriptor_iter_find,
471 METH_VARARGS | METH_KEYWORDS,
472 "registers (name) -> gdb.RegisterDescriptor.\n\
473Return a register descriptor for the register NAME, or None if no register\n\
474with that name exists in this iterator." },
475 {NULL} /* Sentinel */
476};
477
479 PyVarObject_HEAD_INIT (NULL, 0)
480 "gdb.RegisterDescriptorIterator", /*tp_name*/
481 sizeof (register_descriptor_iterator_object), /*tp_basicsize*/
482 0, /*tp_itemsize*/
483 0, /*tp_dealloc*/
484 0, /*tp_print*/
485 0, /*tp_getattr*/
486 0, /*tp_setattr*/
487 0, /*tp_compare*/
488 0, /*tp_repr*/
489 0, /*tp_as_number*/
490 0, /*tp_as_sequence*/
491 0, /*tp_as_mapping*/
492 0, /*tp_hash */
493 0, /*tp_call*/
494 0, /*tp_str*/
495 0, /*tp_getattro*/
496 0, /*tp_setattro*/
497 0, /*tp_as_buffer*/
498 Py_TPFLAGS_DEFAULT, /*tp_flags*/
499 "GDB architecture register descriptor iterator object", /*tp_doc */
500 0, /*tp_traverse */
501 0, /*tp_clear */
502 0, /*tp_richcompare */
503 0, /*tp_weaklistoffset */
507};
508
510 { "name", gdbpy_register_descriptor_name, NULL,
511 "The name of this register.", NULL },
512 { NULL } /* Sentinel */
513};
514
516 PyVarObject_HEAD_INIT (NULL, 0)
517 "gdb.RegisterDescriptor", /*tp_name*/
518 sizeof (register_descriptor_object), /*tp_basicsize*/
519 0, /*tp_itemsize*/
520 0, /*tp_dealloc*/
521 0, /*tp_print*/
522 0, /*tp_getattr*/
523 0, /*tp_setattr*/
524 0, /*tp_compare*/
525 0, /*tp_repr*/
526 0, /*tp_as_number*/
527 0, /*tp_as_sequence*/
528 0, /*tp_as_mapping*/
529 0, /*tp_hash */
530 0, /*tp_call*/
532 0, /*tp_getattro*/
533 0, /*tp_setattro*/
534 0, /*tp_as_buffer*/
535 Py_TPFLAGS_DEFAULT, /*tp_flags*/
536 "GDB architecture register descriptor object", /*tp_doc */
537 0, /*tp_traverse */
538 0, /*tp_clear */
539 0, /*tp_richcompare */
540 0, /*tp_weaklistoffset */
541 0, /*tp_iter */
542 0, /*tp_iternext */
543 0, /*tp_methods */
544 0, /*tp_members */
546};
547
549 PyVarObject_HEAD_INIT (NULL, 0)
550 "gdb.RegisterGroupsIterator", /*tp_name*/
551 sizeof (reggroup_iterator_object), /*tp_basicsize*/
552 0, /*tp_itemsize*/
553 0, /*tp_dealloc*/
554 0, /*tp_print*/
555 0, /*tp_getattr*/
556 0, /*tp_setattr*/
557 0, /*tp_compare*/
558 0, /*tp_repr*/
559 0, /*tp_as_number*/
560 0, /*tp_as_sequence*/
561 0, /*tp_as_mapping*/
562 0, /*tp_hash */
563 0, /*tp_call*/
564 0, /*tp_str*/
565 0, /*tp_getattro*/
566 0, /*tp_setattro*/
567 0, /*tp_as_buffer*/
568 Py_TPFLAGS_DEFAULT, /*tp_flags*/
569 "GDB register groups iterator object", /*tp_doc */
570 0, /*tp_traverse */
571 0, /*tp_clear */
572 0, /*tp_richcompare */
573 0, /*tp_weaklistoffset */
574 gdbpy_reggroup_iter, /*tp_iter */
575 gdbpy_reggroup_iter_next, /*tp_iternext */
576 0 /*tp_methods */
577};
578
580 { "name", gdbpy_reggroup_name, NULL,
581 "The name of this register group.", NULL },
582 { NULL } /* Sentinel */
583};
584
585PyTypeObject reggroup_object_type = {
586 PyVarObject_HEAD_INIT (NULL, 0)
587 "gdb.RegisterGroup", /*tp_name*/
588 sizeof (reggroup_object), /*tp_basicsize*/
589 0, /*tp_itemsize*/
590 0, /*tp_dealloc*/
591 0, /*tp_print*/
592 0, /*tp_getattr*/
593 0, /*tp_setattr*/
594 0, /*tp_compare*/
595 0, /*tp_repr*/
596 0, /*tp_as_number*/
597 0, /*tp_as_sequence*/
598 0, /*tp_as_mapping*/
599 0, /*tp_hash */
600 0, /*tp_call*/
601 gdbpy_reggroup_to_string, /*tp_str*/
602 0, /*tp_getattro*/
603 0, /*tp_setattro*/
604 0, /*tp_as_buffer*/
605 Py_TPFLAGS_DEFAULT, /*tp_flags*/
606 "GDB register group object", /*tp_doc */
607 0, /*tp_traverse */
608 0, /*tp_clear */
609 0, /*tp_richcompare */
610 0, /*tp_weaklistoffset */
611 0, /*tp_iter */
612 0, /*tp_iternext */
613 0, /*tp_methods */
614 0, /*tp_members */
615 gdbpy_reggroup_getset /*tp_getset */
616};
int regnum
const char *const name
void * get(unsigned key)
Definition registry.h:211
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition gdbarch.c:2173
int gdbarch_register_reggroup_p(struct gdbarch *gdbarch, int regnum, const struct reggroup *reggroup)
Definition gdbarch.c:3670
static int gdbarch_num_cooked_regs(gdbarch *arch)
Definition gdbarch.h:390
static const char * register_name(int reg, const char **regs, long num_regs)
int value
Definition py-param.c:79
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
Definition py-ref.h:42
static PyObject * gdbpy_reggroup_iter_next(PyObject *self)
PyObject * gdbpy_new_register_descriptor_iterator(struct gdbarch *gdbarch, const char *group_name)
static PyObject * gdbpy_reggroup_to_string(PyObject *self)
PyTypeObject register_descriptor_object_type
static const registry< gdbarch >::key< gdbpy_register_type > gdbpy_register_object_data
static gdbpy_ref gdbpy_get_reggroup(const reggroup *reggroup)
static PyMethodDef register_descriptor_iterator_object_methods[]
static gdb_PyGetSetDef gdbpy_reggroup_getset[]
static PyObject * gdbpy_reggroup_iter(PyObject *self)
PyTypeObject reggroup_object_type
static PyObject * gdbpy_register_descriptor_iter(PyObject *self)
static gdbpy_ref gdbpy_get_register_descriptor(struct gdbarch *gdbarch, int regnum)
static PyObject * gdbpy_register_descriptor_to_string(PyObject *self)
PyObject * gdbpy_new_reggroup_iterator(struct gdbarch *gdbarch)
std::vector< gdbpy_ref<> > gdbpy_register_type
PyTypeObject reggroup_iterator_object_type
static PyObject * register_descriptor_iter_find(PyObject *self, PyObject *args, PyObject *kw)
bool gdbpy_parse_register_id(struct gdbarch *gdbarch, PyObject *pyo_reg_id, int *reg_num)
static PyObject * gdbpy_register_descriptor_iter_next(PyObject *self)
static PyObject * gdbpy_register_descriptor_name(PyObject *self, void *closure)
static PyObject * gdbpy_reggroup_name(PyObject *self, void *closure)
PyTypeObject register_descriptor_iterator_object_type
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_registers()
static gdb_PyGetSetDef gdbpy_register_descriptor_getset[]
int gdb_py_int_as_long(PyObject *obj, long *result)
Definition py-utils.c:304
gdb::unique_xmalloc_ptr< char > gdbpy_obj_to_string(PyObject *obj)
Definition py-utils.c:173
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition py-utils.c:334
int gdbpy_is_string(PyObject *obj)
Definition py-utils.c:164
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,...)
const reggroup * reggroup_find(struct gdbarch *gdbarch, const char *name)
Definition reggroups.c:177
const reggroup *const all_reggroup
Definition reggroups.c:255
const std::vector< const reggroup * > & gdbarch_reggroups(struct gdbarch *gdbarch)
Definition reggroups.c:136
PyObject_HEAD std::vector< constreggroup * >::size_type index
struct gdbarch * gdbarch
PyObject_HEAD const struct reggroup * reggroup
reggroup(const char *name, enum reggroup_type type)
Definition reggroups.h:45
const char * name() const
Definition reggroups.h:51
PyObject_HEAD const struct reggroup * reggroup
struct gdbarch * gdbarch
PyObject_HEAD int regnum
Definition value.h:130
const char * user_reg_map_regnum_to_name(struct gdbarch *gdbarch, int regnum)
Definition user-regs.c:187
int user_reg_map_name_to_regnum(struct gdbarch *gdbarch, const char *name, int len)
Definition user-regs.c:132
int PyObject
Definition varobj.c:41