GDB pretty printers for LLVM ADTs

I’ve added a few LLVM GDB pretty printers a while back (& just added llvm::Optional earlier today) & thought people might be interested in how to use them, etc.

I use them by adding the following to my .gdbinit:

source /path/to/llvm/src/utils/gdb-scripts/prettyprinters.py

Also, I can suggest adding:

set print pretty on

there too, it helps a lot when printing complex nested data structures.

Once you have that, you can get pretty output like this:

(gdb) p Abbrev->AbbrDeclSets
$1 = std::map with 1 elements = {
[0] = {
Offset = 0,
FirstAbbrCode = 1,
Decls = std::vector of length 1, capacity 1 = {{
Code = 1,
Tag = llvm::dwarf::Tag::DW_TAG_compile_unit,
CodeByteSize = 1 ‘\001’,
HasChildren = false,
AttributeSpecs = llvm::SmallVector of length 8, capacity 8 = {{
Attr = llvm::dwarf::Attribute::DW_AT_low_pc,
Form = llvm::dwarf::Form::DW_FORM_addr,
ByteSize = llvm::Optional is not initialized
}, {
Attr = llvm::dwarf::Attribute::DW_AT_high_pc,
Form = llvm::dwarf::Form::DW_FORM_data8,
ByteSize = llvm::Optional is initialized = {
value = 8 ‘\b’
}

Here you can see the pretty printers for SmallVector and Optional, but there’s also support for StringRef, SmallString, and ArrayRef.

I’ve tried doing a DenseMap pretty printer, but haven’t quite figured it out yet (having trouble calling member functions to do some of teh more complicated parts of DenseMap iteration - may end up printing it with the tombstone/empty values present, but not sure how easy that’ll be to read, etc). Happy to take requests or talk people through adding more pretty printers anyone wants to add :slight_smile:

Enjoy!

  • Dave

Added some basic DenseMap support in r290011

Excellent. I think it would make sense to add small section somewhere in documentation (e.g. LLVM Programmer’s Manual) about it, so it won’t be lost somewhere in the mailing list.

Piotr

Good point - any ideas on where in the programmer’s manual would suit best? Looking around I sort of feel like it belongs in the “Picking the right data structure” section - but I fear someone merely examining existing data structures might not think to look there for help in their task (if they have no need to ‘pick’ a data structure for a new use, etc).

I guess adding section “Debugging” at the bottom of programmers manual would sounds like a good plan to me.

Piotr

Added something in r290186.

Say, Reid - do you know anything about the MSVC formatters that are also provided in LLVM? Perhaps you could add a brief bit of documentation about them, if they need an explicit wiring up (if they just come for free/automatically - might not be worth documenting, people will find it when they need it) or point this thread to someone who knows about them to provide such documentation.

  • Dave

The VC visualizers are automatcially installed.

Dandy :slight_smile:

I looked into ways to do this for the GDB visualizers - but was unable to come up with a totally automated solution, unfortunately.

Dandy :slight_smile:

I looked into ways to do this for the GDB visualizers - but was unable to come up with a totally automated solution, unfortunately.

I’ve wanted the same. Best I could think of was to embed them (or a reference to them) in the binary for the debugger to look at. No idea if it would be accepted though. Just a thought.

Also thanks for adding the docs. Would you (or would you mind if I) add a similar one for the lldb pretty printers?

Cheers,
Pete

Dandy :slight_smile:

I looked into ways to do this for the GDB visualizers - but was unable to come up with a totally automated solution, unfortunately.

I’ve wanted the same. Best I could think of was to embed them (or a reference to them) in the binary for the debugger to look at. No idea if it would be accepted though. Just a thought.

Right - GDB has something /like/ that supported ( https://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-extensions.html#Auto_002dloading-extensions ) - but none of the options seemed to quite fit. You could use debug_gdb_scripts, but that lists script file names - that need to exist in a directory gdb already knows about. And the other option works for object files, I guess - but that’s not exactly applicable to templates which have no object file home. That’s my understanding, at least.

If I could embed the formatter itself (even if it meant 20+ lines of python in each ADT header, included in object files and deduplicated by the linker) I’d totally advocate for that so the formatters would ‘Just Work’.

Also thanks for adding the docs. Would you (or would you mind if I) add a similar one for the lldb pretty printers?

Not at all - go ahead (probably pull my stuff down into a ‘gdb’ subheader and add an ‘lldb’ subheader too)