Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a program's source code into its binary using GCC for GDB's later use

Tags:

gcc

debugging

gdb

I'm getting tired of having to keep my source code untouched until I'm through with its debugging. Any time I change my code, GDB starts complaining about it:

warning: Source file is more recent than executable.

until I recompile it, which can not always be done quickly. I think it would be great if it was possible to include a program's source code into its binary and to make GDB use it instead of its up-to-date version.

Could anyone suggest a way how to do it? Has this been implemented at all?

like image 470
undercat Avatar asked Nov 27 '10 15:11

undercat


2 Answers

After you compile your code, you can copy of the source code to a different location.

Then, in gdb you can set the directory where gdb is looking for the source code: set directories /your/new/directory.

Now, gdb will work with the source code found in that directory and you can change the original source code without gdb noticing.

like image 138
user1729108 Avatar answered Sep 21 '22 01:09

user1729108


Source code embedding is a feature in DWARF v5. Oddly gcc 11.1 and gdb seem to be completely missing support. Clang supports it though.

clang-13 -gdwarf-5 -gembed-source hello.c -o hello

Unfortunately, lldb does not appear to be able to use the embedded source yet.

like image 21
Manouchehri Avatar answered Sep 20 '22 01:09

Manouchehri