Jon Flow
Jon Flow

Reputation: 535

DWARF DI with generated C code?

I can't seem to find any good documentation for the DWARF DI format to answer my question. I'm writing a language that compiles to C, is there any way that I can produce DWARF Debug Information into the C code or is it assembly only?

Is it impossible to add debug information to some (generated) C code other than using primitive stuff that isn't as good like #line and the #file directives?

Upvotes: 1

Views: 690

Answers (2)

Tom Tromey
Tom Tromey

Reputation: 22519

It can be done for some things, if you assume your compiler can emit assembly. For an example of this, see my favorite gdb test case.

Normally this isn't done, though. It can be quite hard to get right. Nowadays it's better to either write your compiler to be a direct front-end to GCC or LLVM; or to write gdb or lldb helper scripts to make debugging the generated C code simpler.

Upvotes: 2

Tom Goodfellow
Tom Goodfellow

Reputation: 914

I guess you'll compile the generated C with debug info (-g), so what you're asking is how to have additional DWARF records that describe your high level language. Conceivable approaches:

(1) edit the object file to augment the DWARF records for the C code. I don't know of an existing tool, perhaps doable with libdwarf or pyelftools.

(2) find a way to "smuggle" extra DWARF records through the compiler, e.g. somewhat as linker commands can be given through #pragmas. However I don't know of a compiler that supports this.

(3) LLVM has support for debug records in the IR representation . You could use clang to compile your generated C to IR (clang foo.c -S -emit-llvm -target arm -o foo.ll), then augment foo.ll, then generate the .o (llc foo.ll), ready for linking.

The third option seems likely to be the easiest.

Upvotes: 0

Related Questions