Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch complex objects in gdb?

Is there any easy way to write scripts or codes of some sort to use them to watch complex objects inside GDB? One thing I can think of is to have special functions in the code for that purpose and directly call them via call debugViewer(object). Is there any better way for doing this?

How would you do that differently using a visual debugger such as the one in Qt Creator?

like image 353
mmirzadeh Avatar asked May 20 '26 15:05

mmirzadeh


1 Answers

I use the DDD graphical interface for GDB, which is specifically designed to display what you might call "complex objects". However, having said that, I find it's often better to code a dbg() method in anything other than trivial classes/structs, with the benefit being that the dbg() method can not only print the object's contents to stdout, it can also do custom object integrity checking, including verifying that owned/connected objects are in expected states, etc, and it can hide information that it knows is valid but isn't normally helpful for debugging, unless you pass a "verbose" flag to it (or have a separate function for verbose). To make it easier to call the method from the GDB command line I have written nonmember methods that take a pointer to the intended object's dbg() method as you've suggested in your post.

Also with regard to integrity checking and not actually related to your question, I've found that pretty much anytime someone tries to duplicate functionality similar to std::string, std::list, or std::map or whatever (usually for "performance reasons"), they invariably get something wrong, usually something that doesn't show up except on hairy edge cases that are difficult to test for. Three times now I've uncovered multiple hairy edge case bugs in such implementations by writing a data structure integrity tester friend-class of the data structure class(es), whose job is to simply crawl the entire data structure (list or tree or whatever) all the way to the ends, looking for stale/corrupt pointers (i.e. in a list, any whose 'next->prev' does not equal 'this' or 'prev->next' does not equal 'this', etc). One of the data structures was an intrusive combo list-graph (a list with an embedded DAG), which was a lot of fun to troubleshoot.... After several splice/transfer actions between separate lists, there are lots of opportunities to screw up the links & get the DAG in one list referencing nodes in the other list. It's amazing that the structure was in internal use for nearly a year before I wrote the integrity checker and found the hairy edge case bugs.

Sorry, I guess that wasn't what your question was about but it was fun to spout off about it anyway.

like image 125
phonetagger Avatar answered May 23 '26 04:05

phonetagger