Am Samstag, 7. Mai 2022, 03:23:46 MESZ hat Paul Smith <paul@xxxxxxxxxxxxxxxxx> Folgendes geschrieben: > Are there any docs or other information about how to use the GDB > pretty-printers for C++ STL that come with GCC? > > I have them installed and they work for displaying data, but they don't > work for accessing data. > > Just as one very simple example, is there a way to dereference a > unique_ptr? I see xmethods defined but nothing I've tried works. > Suppose I have: > > (gdb) p $mp->mgr > $6 = std::unique_ptr<class Mgr> = { > get() = 0x7f519a24e000 > } > > (so you can see the pretty-printers are installed). Now, how do I > extract out this object pointer so I can see what's in it? These don't > work: > > (gdb) p *$mp->mgr > One of the arguments you tried to pass to operator* could not be > converted to what the function wants. > > (gdb) p $mp->mgr->initialized > One of the arguments you tried to pass to operator-> could not be > converted to what the function wants. > > It used to work in GCC 10.2 / GDB 10.1 to access the pointer directly > if you knew, or deciphered, the internal structor of unique_ptr: > > (gdb) p $mp->mgr._M_t._M_t._M_head_impl > > However, with GCC 11.3 / GDB 12.1 this no longer works: I get this > error: > > Request for member '_M_head_impl' is ambiguous in type > 'std::tuple<Mgr*, std::default_delete<Mgr> >'. > Candidates are: > 'std::default_delete<Mgr> std::_Head_base<1ul, > std::default_delete<Mgr>, true>::_M_head_impl' (std::tuple<Mgr*, > std::default_delete<Mgr> > -> std::_Tuple_impl<0ul, Mgr*, > std::default_delete<Mgr> > -> std::_Tuple_impl<1ul, > std::default_delete<Mgr> > -> std::_Head_base<1ul, > std::default_delete<Mgr>, true>) > '<unnamed type> std::_Head_base<0ul, Mgr*, false>::_M_head_impl' > (std::tuple<Mgr*, std::default_delete<Mgr> > -> std::_Tuple_impl<0ul, > Mgr*, std::default_delete<Mgr> > -> std::_Head_base<0ul, Mgr*, false>) > > I have found no way to resolve this ambiguity to GDB's satisfaction. > > The only thing I've found that works is just to access the pointer > value directly by cutting and pasting it with a cast: > > (gdb) p *((Mgr*)0x7f519a24e000) > $8 = { > ... > initialized = true > } > > (gdb) p ((Mgr*)0x7f519a24e000)->initialized > $9 = true > > Is that really what we have to do? I'm assuming you installed the pretty printers with a call to register_libstdcxx_printers() somewhere. For access to C++ STL objects there is another set of gdb helpers, called xmethods, which you need to install with a call to register_libstdcxx_xmethods(). > Secondly, is there some interface that is defined by the libstdcxx.v6 > Python macros for GDB that people who are doing their own python > scripting for their own C++ programs can take advantage of to avoid too > much groveling through the depths of the C++ STL implementation? Depends on what you want to do with it, but you can get access to the pretty printers in gdb with a call to gdb.default_visualizer() [1]. [1] https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing-API.html#index-gdb_002edefault_005fvisualizer Hannes