在运行时从应用程序读取调试信息
除了调试之外,我还有一些关于调试符号以及可以用它们做什么的问题。我最感兴趣的是有关 GCC 的答案,但我也很高兴知道它在其他编译器(包括 MSVC)下的样子。
首先:
- 调试符号的常见格式/类型有哪些?
- 它们与编译器和平台有何关系? GCC 和 MinGW 平台之间的格式总是相同吗?
- 我可以在运行时检查构建是否有它们以及它们的格式是什么?
还有一些更实际的问题... 如何:
- 检查当前文件和行号?
- 获取正在执行的(限定)函数名称?
- 获取完整的当前堆栈跟踪?
让我强调一下,我正在谈论运行时检查。所有这些都可以由 GDB 读取和漂亮地打印,但我不知道有多少信息来自调试符号本身以及 GDB 还可以访问的源代码有多少信息。
也许有一个库能够解析调试符号并产生此类信息?
调试符号是否标准化得足够好,以至于我可以期望此类解决方案具有一定程度的可移植性?
I have some questions regarding debugging symbols and what can be done with them, besides, well, debugging. I'm mostly interested in answers regarding GCC, but I'd also be happy to know how it looks like under other compilers, including MSVC.
First of all:
- What are the common formats/types of debugging symbols?
- How do they relate to compilers and platforms? Is it always the same format on GCC and MinGW among platforms?
- Can I check in runtime whether the build has them and what format are they in?
And some more practical questions... How can I:
- Check the current file and line number?
- Obtain the (qualified) function name being executed?
- Obtain a full current stack trace?
Let me emphasize that I'm talking about run-time checks. All of those can be read and pretty-printed by GDB, but I don't know how much info comes from the debugging symbols themselves and how much from the source code which GDB also has access to.
Maybe there's a library which is able to parse the debugging symbols and yield such information?
Are the debugging symbols standardised well enough that I can expect some degree of portability for such solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DWARF 和 STABS(嵌入在可执行文件中的特殊部分)、程序数据库(PDB;外部文件,由 MSVC 使用)。
GCC在Linux(ELF)和Windows(PE)上都使用DWARF/STABS(我认为这是GCC编译时选项),不知道其他的。 MSVC 始终使用 PDB。
您可以解析可执行映像并查看是否有包含调试信息的部分(请参阅 STABS 文档 和 DWARF 规范)。 PDB 文件通过可执行文件或通过符号服务器分发(因此,如果您不想上网,请检查是否有 X.exe/X.dll 的 X.pdb)。
关于如何读取和使用这些符号——我不知道 DWARF/STABS(GNU binutils 周围可能有一些东西可以定位和提取这些符号),但对于 PDB,你最好的选择是使用 dbghelp — 其用法是 文档非常齐全,网上有很多示例。还有可用于查询 PDB 文件的 DIA SDK。
DWARF 有一个正式的规范,而且非常复杂。 PDB AFAIK 没有记录,但 dbghelp/DIA 有,并且是推荐的方法。
DWARF and STABS (those are embedded inside executable, in special sections), Program Database (PDB; external file, used by MSVC).
GCC uses DWARF/STABS (I think it's a GCC compile-time option) both on Linux (ELF) and Windows (PE), don't know about others. MSVC always uses PDB.
You can parse the executable image and see if there are sections with debugging info (see STABS documentation and DWARF specs). PDB files are distributed either with executables or via symbol servers (so if you don't want to go online, check if there is X.pdb for X.exe/X.dll).
About how to read and use those symbols — I don't know about DWARF/STABS (there's probably something around GNU binutils that can locate and extract those), but for PDB your best bet is to use dbghelp — its usage is pretty well documented and there are a lot of examples available on the net. There's also DIA SDK that can be used to query PDB files.
DWARF has a formal specification, and it's complicated as hell. PDB AFAIK is not documented, but dbghelp/DIA are, and are the recommended way.