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?
GCC is open source - you can fix it. Of course, you'd probably have to revise LD to handle the information correctly, and you'd definitely have to fix GDB to use the embedded source. You'd be using a non-standard format for the debugging information, so you'd probably have to modify the other tools that manipulate object files.
So, the possibility is there. But it is easier to do the same as everyone else in the world does and keep your source around until you've finished debugging it. Normally, you can keep a single GDB session running while you rebuild the executable multiple times, if need so be. And, typically, it is easiest to debug the current version of the code rather than yesterday's version. If you do need to debug yesterday's version, you need yesterday's code available (you do have a good VCS in place, don't you?) so that you can see what was actually wrong with yesterday's code rather than today's modified version of the code.
I'll give you credit for asking the question - it takes some lateral thinking to come up with the idea. Well done! But in practice your suggestion is decidedly non-trivial to implement.
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.