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.

Raven
  • 2,951
  • 2
  • 26
  • 42
  • 1
    have you tried this approach? https://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? – Rob Jul 13 '21 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? – Raven Jul 13 '21 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. https://sourceware.org/gdb/onlinedocs/gdb/Writing-a-Pretty_002dPrinter.html – Marek R Jul 13 '21 at 14:20
  • Here is possible [duplicate](https://stackoverflow.com/q/12574253/1387438) (I'm not voting to close as dupe since I'm not fully convinced and with my privilege I would close topic immediately). – Marek R Jul 13 '21 at 14:26
  • Hm I would like to avoid having to rewrite a pretty-printer for all my classes when I already have done that in code. But maybe one could write a pretty-printer that checks whether `operator<<` is defined and then somehow use that. Idk if that's possible but I will have a look. Thanks – Raven Jul 13 '21 at 14:35
  • @Raven ROFL.. well I guess I did. I had another one I looked up, but must have copy pasted the wrong link. – Rob Jul 13 '21 at 14:46
  • @Raven note that link I've provided used regular expression to select type. So I'm sure it is possible to provide pretty printer for multiple types at once. – Marek R Jul 13 '21 at 14:47
  • @MarekR Yes it does seem so. The question then only is if it is possible to test for and call the respective operator overload for the type at hand. But since GDB seems to generally be capable of this kind of stuff, I assume the Python interface allows for that as well... – Raven Jul 13 '21 at 14:50
  • 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()); – Rob Jul 13 '21 at 15:01
  • The question for core dump is [c - gdb evaluate function in process core - Stack Overflow](https://stackoverflow.com/questions/8892597/gdb-evaluate-function-in-process-core) ; otherwise the specific `operator<<` option works too. – user202729 Mar 10 '22 at 01:05

1 Answers1

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 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... – Raven Jul 14 '21 at 06:29
  • 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. – Raven Jul 14 '21 at 06:33