calling operator in gdb
The only way I found was this:
call 'operator<<(std::ostream&, myclass&)'(mycout, c)
Since std::cout
wasn't visible to gdb for some reason, I had to resort to creating my own like this:
std::ostream mycout(std::cout.rdbuf());
You haven't stated any reasons for wanting to do this but won't print yourvariable
be easier?
If this is an absolute must you could have a Print
method in your class and call that from operator<<
and then call the Print
method on your object from gdb.
Do take note that stdout is probably buffered in gdb so you won't be seeing any output unless you redirect it somehow.
See this discussion from gdb's mailing archive regarding this issue.
Call an overloaded operator from GDB
The issue is not that the operator()
is being inlined, but the linker is just removing the function, since is not called anywhere in your program.
On my machine, if I change main
to:
int main()
{
Test t = Test(10);
t(42); // some call, result can be ignored
return 0;
}
then running the command gives:
(gdb) call (int)t.operator()((int)10)
$1 = 20
Also, when compiling the program, use -ggdb3
(for better debugging symbols) and -O0
(so that function calls don't get inlined, etc).
In GDB, what is the proper way to call C++ functions inside namespaces or classes in non-debug binaries?
Your C++ compiler kindly put ns::test
into the symbol table. All we need to do is prevent GDB's expression evaluator from trying to look up the non-existent symbol ns
. To do this, put the entire function name in single quotes.
(gdb) call (void)'ns::test'()
ns::test
GDB print variable in readable format (using operator)
it appears to me as if there must be a more convenient way of getting GDB to print variables in a readable format.
Yes: you implement a python pretty-printer for them. Documentation.
I would like to avoid having to rewrite a pretty-printer for all my classes when I already have done that in code.
The problem with call PrintMyClass()
solutions is that they require a running process. When you have a core
dump, you can't call any functions in your code, so you need something outside of your program to pretty-print the data.
Obviously not a concern if you never debug core
dumps, but sooner or later you'll likely have to do that, and then you'll need to duplicate the code anyway.
gdb Could not find operator[]
My understanding is that the compiler/linker includes only the member functions present in the source code and those are the ones I can use in gdb.
Your understanding is incorrect / incomplete.
std::vector
is a template class. Without explicit instantiation, the compiler is required to instantiate only the methods called (usually a subset of methods present in the source).
Is there a way to include every member function of std::vector so I can access them in gdb?
For a given type T
, you should be able to explicitly instantiate entire vector for that T
, by requesting it, e.g.:
template class std::vector<double>;
In gdb, I can call some class functions, but others cannot be resolved. Why?
gdb
is not a compiler, it will not do the (not-so-)nice user-defined type conversions for you. If you wish to call a function that wants a string
, you need to give it a string
, not a const char*
.
Unfortunately, gdb
cannot construct an std::string
for you on the command line, again because it is not a compiler and object creation is not a simple function call.
So you will have to add a little helper function to your program, that would take a const char*
and return an std::string&
. Note the reference here. It cannot return by value, because then gdb
will not be able to pass the result by const reference (it's not a compiler!) You can choose to return a reference to a static object, or to an object allocated on the heap. In the latter case it will leak memory, but this is not a big deal since the function is meant to be called only from the debugger anyway.
std::string& SSS (const char* s)
{
return *(new std::string(s));
}
Then in gdb
gdb> p (paramNode_.px)->Get(SSS("domain"))
should work.
Related Topics
Are Data Members Allocated in the Same Memory Space as Their Objects in C++
How to Implement Readlink to Find the Path
How to Easily Indent Output to Ofstream
Why Add Void to Method Parameter List
Should "Delete This" Be Called from Within a Member Method
C++ Debug Builds Broke in Snow Leopard Xcode
Redirect Both Cout and Stdout to a String in C++ for Unit Testing
How to Assign a Value to the Pointer 'This' in C++
How to Get at the Exception Information When Using Minidumpwritedump Out-Of-Process
Element Count of an Array in C++
C++ Get Description of an Exception Caught in Catch(...) Block
How Is Floating Point Conversion Actually Done in C++(Double to Float or Float to Double)