GDB python: why do xmethod workers always get sent pointers?
Paul Smith via Gdb <[email protected]>
| Newsgroups | gmane.comp.gdb.devel |
|---|---|
| Organization | GNU's Not UNIX! |
| Message-ID | <[email protected]> |
When examining xmethods I've discovered that the __call__() method for
the worker always is sent a pointer even if the value we want to work
with is not a pointer.
I've tested this in both GDB 10.2 and GDB 12.1.
Suppose I have a class Bar and I implement a worker for a getval()
method:
class BarWorker_getval(gdb.xmethod.XMethodWorker):
def get_arg_types(self):
return None
def get_result_type(self, obj):
return gdb.lookup_type('int')
def __call__(self, obj):
gdb.write("TYPE: %s\n" % (str(obj.type)))
return obj['val']
Now I declare a variable in my program:
Bar bar;
Now in GDB I use the xmethod to call the getval() method:
(gdb) ptype bar
type = class Bar {
public:
int getval(void);
int val;
}
(gdb) p bar.getval()
TYPE: Bar *
$1 = 0
Why is the type "Bar*" when I used a variable of type "Bar"?
This matters because it seems to prevent me from using xmethods with
convenience variables:
(gdb) set $x = bar
(gdb) ptype $x
type = class Bar {
public:
int getval(void);
int val;
}
(gdb) p $x.getval()
Attempt to take address of value not located in memory.
If the xmethod infrastructure didn't "attempt to take address of value"
which of course we can't for convenience variables, then this would
work (I assume).
In this particular case I could work around it by setting the
convenience variable to the address of the value:
(gdb) set $xp = &bar
(gdb) p $xp->getval()
type: Bar *
$1 = 0
(weirdly the type here is still "Bar*")
But this doesn't work so well with other situations: the one I've run
into in particular is std::unique_ptr<>, which provides an xmethod for
operator-> so that you can retrieve values from it. I can't find any
way to put a std::unique_ptr into a convenience variable then use it:
(gdb) p uptr->val
$1 = 0
(gdb) set $xx = uptr
(gdb) p $xx->val
Attempt to take address of value not located in memory.
(gdb) set $xp = &uptr
(gdb) p $xp->val
There is no member or method named val.