13

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?

3 Answers 3

7

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.

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

6 Comments

That is very interesting! Any idea if there is any other debugger or standalone tool that supports the source code embedding extension?
I've submitted an edit to your answer to include a link to the commit adding the feature. I think it would be helpful for understanding how it works and its apparent limitations.
None that I'm aware of sadly. I was looking because I was planning on embedding source code into ELFs in my RE pipeline. github.com/cesena/ghidra2dwarf/issues/14
Stack Overflow doesn't seem to let me review edits, as I don't have enough reputation here. Could you comment the link instead?
The answer was updated with a link to 2 code merge requests that implement the feature. I've also accepted your answer. I don't think this is a practical solution - yet - but I'm hopeful this would be supported by the mainstream debuggers soon enough, and it closely matches my original intention with the question. Meanwhile, people should refer to user1729108's answer that describes a viable workaround.
|
5

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.

4 Comments

Surely I don't have enough time/skill/patience/need to implement it as a new feature, however, I'm quite surprised it has not been implemented yet. While I agree that in most cases it should not be needed, it may be of some use sometimes. For example, I start a debugger with my old binary to find out what's wrong. I locate the source of the problem, but then, right after having written half of the code and saving it, the GDB session crashes due to a mere typo. Now I'll either have to fix the code without the aid of the debugger or roll back, rebuild the program, run GDB and undo the changes!
@vovick: there are two reasons at least. One is historical: the copious resources available now were not always available, and people felt (probably rightly) that it was better not to clutter the debuggable code with the actual source because it would require too much disk space and memory. Back in the old days, it was hard enough to squeeze the program into memory - let alone adding the extra debugging information, etc. The other is practical: people don't find it so hard to keep the relevant source available as to warrant putting the effort into the doing as you suggest.
This isn't correct anymore, as DWARFv5 standardized the source code embedding extension.
@Manouchehri — time passes; technology changes. I believe my answer was accurate when written. You can write up a new answer discussing DWARF-5 and the extensions. And, indeed, I see you have written such an answer, and it is now the accepted answer. The SO system is working as intended.
4

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.

Comments

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.