0

GDB is a nice tool but in the way I have been using it so far, it turns pretty useless as soon as one is working with more complex data structures because simply using print on them just fills then entire screen with unreadable details about that class.

However I usually have a custom operator<< defined for my classes for the purpose of creating a (more or less) readable String representation of my class.

Therefore I would much prefer if GDB would always use that (if available) instead of its default print behavior.

I have found this question from 2010 which describes how one can call operator<< manually. Besides being a bit tedious to do, I have failed to get these to work for me (I always get No symbol "operator<<" in current context.).

However it appears to me as if there must be a more convenient way of getting GDB to print variables in a readable format. After all I can't be the first one who comes across this situation.

Therefore my question is: How do you get GDB to print variables in a readable format (preferably using operator<< as implemented in the respective type)? The linked question I found is from 2010 and so I am hoping that since then the situation has improved.

10
  • 1
    have you tried this approach? stackoverflow.com/questions/3832964/calling-operator-in-gdb I guess gdb can be augmented with customer python module (not sure if your version does).. or you could wraper only included in debug that would call the ostrem << operator for anything you give it? Commented Jul 13, 2021 at 13:50
  • 1
    @Rob If I am not mistaken you linked the same question as I did in my post... Are you referring to adding a wrapper function in c++ that uses operator<< for me and then use that inside GDB instead of trying to call the operator manually? Commented Jul 13, 2021 at 14:00
  • 1
    AFAIK you can customize how some types are represented by gdb (pretty print) by using a python script (gdb has own python interpreter). I never did that, but this should be a good starting point for you. sourceware.org/gdb/onlinedocs/gdb/… Commented Jul 13, 2021 at 14:20
  • Here is possible duplicate (I'm not voting to close as dupe since I'm not fully convinced and with my privilege I would close topic immediately). Commented Jul 13, 2021 at 14:26
  • 1
    @Raven actually the link works fine. this is the answer part: he 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()); Commented Jul 13, 2021 at 15:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't even know I could use gdb on core dumps xD In my case I really only want to display my variables in a readable format during regular debugging of my program. Imo it is really a shame that this is seemingly not really supported. But thanks anyways - Maybe I can write some Python code to automatically extract my operator<< implementations and generate a pretty-printer based on that...
To clarify: My biggest issue with having to duplicate the pretty-printers is that when working on non-finished code, the String representation will probably have to change again. And then I'll have to maintain two versions of pretty printers which is just a really annoying maintenance overhead when all I want is to be able to properly debug my program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.