Search code examples
c++debugginggdbntl

How to use Gdb debugger to find value of ZZ object from Number Theory Library?


I use gdb debugger to print values of a variable which is of type ZZ. This data type is defined in Number Theory Library or NTL. When I use "print x" to find out my variable's value I obtain something like this:

print x 
$1 = {rep=0xab2cc54}. 

I guess this is the address of my ZZ object. How can I print its value ? I should mention that I don't know the internal representation of this class.

Can I use NTL with a compiler like Eclipse to debug easier my application ?


Solution

  • … my ZZ object. How can I print its value ?

    This is a bit ugly, but works:

    (gdb) call NTL::operator<<(std::ostream&, NTL::ZZ const&)(cerr, x)
    42$1 = (std::ostream &) @0x620020: <incomplete type>
    

    (in this example, the variable x has the value 42).
    If you don't want the garbage after the value, you can cast to void:

    (gdb) call (void)NTL::operator<<(std::ostream&, NTL::ZZ const&)(cerr, x)
    42(gdb) 
    

    (but note that then there's no newline after the value).

    If you're not using namespace std, you may have to write

    (gdb) call NTL::operator<<(std::ostream&, NTL::ZZ const&)('std::cerr', x)
    

    Sometimes cerr may not be in scope:

    (gdb) call NTL::operator<<(std::ostream&, NTL::ZZ const&)(cerr, x)        
    No symbol "cerr" in current context.
    

    - then you can try with cout, but it gets even uglier, because the buffered output has to be flushed:

    (gdb) call NTL::operator<<(std::ostream&, NTL::ZZ const&)(cout, x)
    (gdb) call 'std::basic_ostream<char, std::char_traits<char>>& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char>>&, char const*)'(&cout, "\n")
    42