cgccgdbcross-compiling

Add C Source Code to Debug Info (with GCC and for GDB)


I'm cross-compiling a C application for an ARM aarch64 target. I want to debug it on the target which has gdb 9.1 installed and I would like to avoid both remote debugging and having to copy the source code tree onto the target for gdb to find it.

The idea is to have my cross GCC 9.3.0 add all my source code to the debug info of the ELF executable so gdb can use it (no need for library source code). I've found gcc options -g3 -ggdb3 on https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Debugging-Options.html but that does not seem enough.

What magic can I use to integrate source code into debug info for gdb?


Solution

  • There is most likely no way around having access to the source code. Although DWARF5 defines vendor extensions that may be used to support source code embedding, they still are vendor extensions and therefore not a widely accepted standard. The DWARF debug information (which is what is generated by -g) used by GDB is at most paths to source files and lines of code. GDB will look on the filesystem for the source files based on the paths and line numbers stored in debug info.

    As @ssbssa points out in the comments, clang does make use of such vendor extensions and can embed source code with -gdwarf-5 -gembed-source. See: Embedding a program's source code into its binary using GCC for GDB's later use. It will embed relevant parts of source code in the .debug_line_str debug section. However LLDB and GDB don't seem to be able to automatically use it yet. So this solution is still incomplete.

    As a workaround, if you don't want to recursively copy the source tree remotely, you could use something like debuginfod and configure it, or code yourself a GDB plugin to fetch the source for you, but there isn't much more you can do.