1

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?

5
  • I would instead host debuginfod server, is that an option? Commented Apr 4 at 9:14
  • I'm not aware of any compiler option to embed the source in the debug information, nor am I aware of any mechanism in GDB that can pull the source code from the debug information. You could, potentially, do something manually, after a build you could tar up the source code, then use objcopy to embed it into the executable within a custom section. On the GDB side, a Python extension could catch maybe the executable_changed event, and then use objcopy to extract the section, unpack it, and then add the source tree to the directory source search path in GDB. Commented Apr 4 at 9:51
  • @KamilCuk No, think no IP connectivity and only console access. Commented Apr 4 at 9:53
  • No IP? How are you getting the [updated] target executable onto the system? Burning a micro-SD card on the host? Kermit-like file transfer over UART? tar? rsync? It would help to know the target environment (e.g. bare metal, linux, FreeRTOS, etc.). And, target system (e.g. RPi et. al., nvidia Jetson, etc.). Creating a debuginfo file and sending that along seems like the most compact. I'm guessing you're using linux. For situations like this, I've copied files using rsync, so it can happen quickly as, likely, the only copies are executable and 1-2 source files. Is debug console UART? Commented Apr 4 at 20:11
  • @CraigEstey It's a third party board with an UART as the service interface. It is going to space, maybe today, and surrounded by ground support equipment until that happens. I'm asking this because debuggers already have information about symbols and struct layouts, so it is conceptually not a big leap to ask for source code as well. After all, debuggers read ELF sections for breakfast. Why is there so much hoop-jumping with remote debugging and debuginfod and whatnot when it could be as simple as adding -fadd-source-for-debugging? Commented Apr 6 at 9:26

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I guess one could play fun linker tricks to embed the sources in the binary as data, and have a utility or even a built-in feature for unpacking them to the local filesystem. Or maybe that custom GDB plugin could access the embedded data directly. But all the tooling for such a thing would be a significant project of its own.
@JohnBollinger definitely, yeah. One "simple" way I could think about doing it is to just embed the whole source as a compressed blob (i.e. literally a zip or tar+gz archive) in the binary and then unpack it via a plugin when debugging... but copying the source code over seems like much less work.
According to this answer clang can do this with -gdwarf-5 -gembed-source.
@ssbssa Oh wow, I must update my knowledge on DWARF5. On second thought IDK if compilers are able to use that still.
It looks like embedding source within the debug information will be part of DWARF 6 when that is released: dwarfstd.org/issues/180201.1.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.