调试选项 -g 如何更改二进制可执行文件? [英] How Does The Debugging Option -g Change the Binary Executable?

查看:35
本文介绍了调试选项 -g 如何更改二进制可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写 C/C++ 代码时,为了调试二进制可执行文件,必须在编译器/链接器上启用调试选项.在 GCC 的情况下,选项是 -g.启用调试选项后,如何影响二进制可执行文件?文件中存储了哪些额外的数据以允许调试器正常工作?

When writing C/C++ code, in order to debug the binary executable the debug option must be enabled on the compiler/linker. In the case of GCC, the option is -g. When the debug option is enabled, how does the affect the binary executable? What additional data is stored in the file that allows the debugger function as it does?

推荐答案

-g 告诉编译器在可执行文件中存储符号表信息.其中包括:

-g tells the compiler to store symbol table information in the executable. Among other things, this includes:

  • 符号名称
  • 输入符号信息
  • 符号来自的文件和行号

调试器使用此信息为符号输出有意义的名称,并将指令与源代码中的特定行相关联.

Debuggers use this information to output meaningful names for symbols and to associate instructions with particular lines in the source.

对于某些编译器,提供 -g 将禁用某些优化.例如,icc 使用 -g 将默认优化级别设置为 -O0,除非您明确指定 -O[123].此外,即使您确实提供了 -O[123],阻止堆栈跟踪的优化仍将被禁用(例如,从堆栈帧中剥离帧指针.这对性能的影响很小).

For some compilers, supplying -g will disable certain optimizations. For example, icc sets the default optimization level to -O0 with -g unless you explicitly indicate -O[123]. Also, even if you do supply -O[123], optimizations that prevent stack tracing will still be disabled (e.g. stripping frame pointers from stack frames. This has only a minor effect on performance).

对于某些编译器,-g 将禁用可能混淆符号来自何处的优化(指令重新排序、循环展开、内联等).如果您想通过优化进行调试,您可以使用 -g3 和 gcc 来解决其中的一些问题.将包含有关可能已内联的宏、扩展和函数的额外调试信息.这可以允许调试器和性能工具将优化的代码映射到原始源代码,但这是最大的努力.一些优化确实破坏了代码.

With some compilers, -g will disable optimizations that can confuse where symbols came from (instruction reordering, loop unrolling, inlining etc). If you want to debug with optimization, you can use -g3 with gcc to get around some of this. Extra debug info will be included about macros, expansions, and functions that may have been inlined. This can allow debuggers and performance tools to map optimized code to the original source, but it's best effort. Some optimizations really mangle the code.

有关详细信息,请查看 DWARF,最初设计的调试格式使用 ELF(Linux 和其他操作系统的二进制格式).

For more info, take a look at DWARF, the debugging format originally designed to go along with ELF (the binary format for Linux and other OS's).

这篇关于调试选项 -g 如何更改二进制可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆