vanz
vanz

Reputation: 337

Printing C++ class objects with GDB

Is there some "default function" to print an object like a string on the GDB when we are debugging C++ applications? Something like: toString();

Or my class have to implement something like that?

Upvotes: 9

Views: 14224

Answers (4)

Define operator<< and call it from GDB

C++ equivalent of Java's toString? was mentioned in the comments, and operator<< is the most common way of defining a to string method on a class.

This is likely the sanest approach because the resulting string method will be part of the codebase itself, and so:

  • it is less to stop compiling (one would hope!)
  • it is readily available without any GDB setup
  • it can be called from C++ itself when needed

Unfortunately I haven't found a completely sane way of calling operator<< from GDB, what a mess: calling operator<< in gdb

This has worked on my hello world test:

(gdb) call (void)operator<<(std::cerr, my_class)
MyClass: i = 0(gdb)

There is no newline at the end, but I can live with that.

main.cpp

#include <iostream>

struct MyClass {
    int i;
    MyClass() { i = 0; }
};

std::ostream& operator<<(std::ostream &oss, const MyClass &my_class) {
    return oss << "MyClass: i = " << my_class.i;
}

int main() {
    MyClass my_class;
    std::cout << my_class << std::endl;
}

Compiled with:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp

Tested in GDB 8.1.0, Ubuntu 18.04.

Upvotes: 2

ks1322
ks1322

Reputation: 35716

You can call any member functions from Standard Library or your own data type during debug session. This is sometimes the easiest way to output object state in gdb. For std::string you could call it's c_str() member which returns const char*:

(gdb) p str.c_str()
$1 = "Hello, World!"

Though this will work only for debugging live process, but not for core dump debugging.

Upvotes: 2

user405725
user405725

Reputation:

You could always have printed std::string (or anything else for that matter) using print command. However, struggling with C++ template container internals might not be pleasant. In the recent versions of toolchains (GDB + Python + Pretty Printers that are usually installed together as part of the development packages on most user-friendly Linux distros), those are automatically recognized and printed (pretty!). For example:

$ cat test.cpp 
#include <string>
#include <iostream>

int main()
{
    std::string s = "Hello, World!";
    std::cout << s << std::endl;
}

$ g++ -Wall -ggdb -o test ./test.cpp 
$ gdb ./test 

(gdb) break main
Breakpoint 1 at 0x400ae5: file ./test.cpp, line 6.
(gdb) run
Starting program: /tmp/test 

Breakpoint 1, main () at ./test.cpp:6
6       std::string s = "Hello, World!";
Missing separate debuginfos, use: debuginfo-install glibc-2.16-28.fc18.x86_64 libgcc-4.7.2-8.fc18.x86_64 libstdc++-4.7.2-8.fc18.x86_64
(gdb) next
7       std::cout << s << std::endl;
(gdb) p s
$1 = "Hello, World!"
(gdb) 

As @111111 pointed out, check out http://sourceware.org/gdb/wiki/STLSupport for instructions on how to get this installed yourself.

Upvotes: 3

Matt Kline
Matt Kline

Reputation: 10487

gdb has a built-in print command that you can call in gdb on any variable or expression to see its value. You should look at the gdb documentation for details. You can find the full manual here and a decent intro guide can be found here

Upvotes: 1

Related Questions