On Mon, 9 May 2022 at 10:32, Jonathan Wakely wrote: > > On Sun, 8 May 2022 at 20:44, Paul Smith wrote: > > > > On Sun, 2022-05-08 at 09:16 +0100, Jonathan Wakely wrote: > > > > gdb.set_convenience_variable('mgr', val['mgr']) This doesn't do what I thought it does, meaning that $mgr ends up defined to void. I think you can't set a gdb.Value as the value of a convenience variable. > > > > init = gdb.parse_and_eval('$mgr->initialized') > > > > > > > > This will use the xmethod to evaluate the expression. > > > > > > And then: > > > > > > if init: > > > return gdb.parse_and_eval('*$mgr') > > > > Unfortunately, this doesn't work :(. I can't do it from the GDB > > command line or python (I have tried both with the same results). > > Something about convenience variables doesn't play well with xmethods > > (or maybe this xmethod implementation specifically?) > > You're right, sorry. It doesn't work for me with a convenience variable. And even if I don't set the convenience variable to a gdb.Value, it still doesn't work with the xmethod: (gdb) set $mgr = x.mgr (gdb) whatis $mgr type = std::unique_ptr (gdb) p $mgr $2 = std::unique_ptr = {get() = 0x416eb0} (gdb) p $mgr->initialized Attempt to take address of value not located in memory. > > But since what you want is something that works in arbitrary Python > code, not just within GDB, doesn't pybind11 already do everything you > want? > https://github.com/pybind/pybind11 If you don't want to (or can't) use pybind11 to create Python bindings, and you don't want to use the GDB Python API, then you will have a ton of work to do. The GDB Python API is what provides all the tools for traversing C++ class hierarchies, examining template arguments, casting values to related types etc. The attached diff refactors the std::unique_ptr pretty printer to be defined in terms of a new types.UniquePtr class that exposes the std::unique_ptr API to the printer (and the start of a types.Tuple class for std::tuple, but I didn't make the TuplePrinter use that yet, and didn't change the xmethods yet). This was mostly just a bit of copy & paste, and renaming some functions. The hardest part for me was figuring out how Python module imports work, not refactoring the actual code. But it all still relies on the GDB API. Doing it in pure Python would be much more work. This patch can't be committed though, because it breaks the libstdc++ testsuite. The gdb-test.exp procs expect to be able to source a single Python script, which breaks if that script tries to import modules in the same package (because there's no package when GDB loads the Python file directly via "source blah/blah/printers.py"). That would need to be fixed to go ahead with changes like this.