使用 GCC 将程序的源代码嵌入到其二进制文件中,以供 GDB 以后使用

发布于 2024-10-04 12:37:28 字数 209 浏览 2 评论 0原文

我厌倦了在完成调试之前必须保持源代码不变。每当我更改代码时,GDB 就会开始抱怨:

警告:源文件比可执行文件更新。

直到我重新编译它,这并不总是能很快完成。我认为如果能够将程序的源代码包含到其二进制文件中并让 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱武穆 2024-10-11 12:37:28

源代码嵌入是 DWARF v5 中的一项功能。奇怪的是 gcc 11.1 和 gdb 似乎完全缺少支持。不过 Clang 支持它。

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

不幸的是,lldb 似乎还不能使用嵌入的源代码。

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.

星星的軌跡 2024-10-11 12:37:28

GCC 是开源的 - 您可以修复它。当然,您可能必须修改 LD 才能正确处理信息,并且您肯定必须修复 GDB 才能使用嵌入式源代码。您将使用非标准格式来存储调试信息,因此您可能必须修改操作对象文件的其他工具。

所以,可能性是存在的。但是,像世界上其他人一样做同样的事情并保留源代码直到完成调试会更容易。通常,如果需要,您可以在多次重建可执行文件时保持单个 GDB 会话运行。而且,通常情况下,调试当前版本的代码比昨天的版本最容易。如果您确实需要调试昨天的版本,则需要昨天的代码可用(您确实有一个良好的 VCS,不是吗?),这样您就可以看到昨天的代码实际上有什么问题,而不是今天的代码修改版本。

我会赞扬你提出这个问题——需要一些横向思考才能想出这个想法。干得好!但实际上,你的建议实施起来绝对不简单。

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.

草莓味的萝莉 2024-10-11 12:37:28

编译代码后,您可以将源代码复制到其他位置。

然后,在 gdb 中您可以设置 gdb 查找源代码的目录:
设置目录/your/new/directory

现在,gdb 将使用该目录中找到的源代码,您可以更改原始源代码,而 gdb 不会注意到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文