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

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

问题描述

在编写C / C ++代码时,为了调试二进制可执行文件,必须在编译器/链接器上启用调试选项。在GCC的情况下,选项为-g。当启用调试选项时,如何影响二进制可执行文件?文件中存储什么额外的数据,允许调试器的功能呢?

解决方案

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




  • 符号名称

  • 键入符号信息

  • 符号来自的文件和行号



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



对于某些编译器,提供-g将禁用某些优化。例如,icc使用-g将默认优化级别设置为-O0,除非您明确指出-O [123]。此外,即使您提供-O [123],阻止堆栈跟踪的优化仍将被禁用(例如从框架中移除框架指针,对性能影响不大)。



对于某些编译器,-g将禁用可能混淆符号来源的指令(指令重新排序,循环展开,内联等)的优化。如果要通过优化进行调试,可以使用-g3与gcc来解决这些问题。将包含有关可能已内联的宏,扩展和函数的额外调试信息。这可以允许调试器和性能工具将优化的代码映射到原始源,但最好的努力。有些优化真的会扭曲代码。



有关更多信息,请查看 DWARF ,最初设计的调试格式与ELF(Linux和其他操作系统的二进制格式)一起。


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 tells the compiler to store symbol table information in the executable. Among other things, this includes:

  • symbol names
  • type info for symbols
  • files and line numbers where the symbols came from

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

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).

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.

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天全站免登陆