debug information of executable

本文介绍如何通过GCC的-g选项为可执行文件添加调试信息,并解释了为什么在使用GDB进行调试时需要这些信息。同时介绍了DWARF调试文件格式及其优势。

When use gdb to debug, we often get following error for "list" command:

No symbol table is loaded. Use the "file" command

 

It is because, didn't use gcc -g option to put debug information in executable.

 

       -g  Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF).  GDB can work with this debugging infor-
           mation.

           On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debug-
           ging work better in GDB but will probably make other debuggers crash or refuse to read the program.  If you want to control for certain whether
           to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

           Unlike most other C compilers, GCC allows you to use -g with -O.  The shortcuts taken by optimized code may occasionally produce surprising
           results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may
           not be executed because they compute constant results or their values were already at hand; some statements may execute in different places
           because they were moved out of loops.

           Nevertheless it proves possible to debug optimized output.  This makes it reasonable to use the optimizer for programs that might have bugs.

           The following options are useful when GCC is generated with the capability for more than one debugging format.

       -ggdb
           Produce debugging information for use by GDB.  This means to use the most expressive format available (DWARF 2, stabs, or the native format if
           neither of those are supported), including GDB extensions if at all possible.

http://www.dwarfstd.org/

DWARF is a debugging file format used by many compilers and debuggers to support source level debugging. It addresses the requirements of a number of proceedural languages, such as C, C++, and Fortran, and is designed to be extensible to other languages. DWARF is architecture independent and applicable to any processor or operating system. It is widely used on Unix, Linux and other operating systems, as well as in stand-alone environments.

 

GCC manual in Chinese:

http://www.shanghai.ws/gnu/gcc_3.htm

 

sunhaoming@ubuntu:~/nvr_re/nvr_2025$ sstar931/toolchain/bin/arm-sigmastar-linux-uclibcgnueabihf-objdump torchlight/build_dir/target-arm-openwrt-linux-uclibcgnueabihf/nvr_work/nvr_work >nvr_work.disasm Usage: sstar931/toolchain/bin/arm-sigmastar-linux-uclibcgnueabihf-objdump <option(s)> <file(s)> Display information from object <file(s)>. At least one of the following switches must be given: -a, --archive-headers Display archive header information -f, --file-headers Display the contents of the overall file header -p, --private-headers Display object format specific file header contents -P, --private=OPT,OPT... Display object format specific contents -h, --[section-]headers Display the contents of the section headers -x, --all-headers Display the contents of all headers -d, --disassemble Display assembler contents of executable sections -D, --disassemble-all Display assembler contents of all sections --disassemble=<sym> Display assembler contents from <sym> -S, --source Intermix source code with disassembly -s, --full-contents Display the full contents of all sections requested -g, --debugging Display debug information in object file -e, --debugging-tags Display debug information using ctags style -G, --stabs Display (in raw form) any STABS info in the file -W[lLiaprmfFsoRtUuTgAckK] or --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames, =frames-interp,=str,=loc,=Ranges,=pubtypes, =gdb_index,=trace_info,=trace_abbrev,=trace_aranges, =addr,=cu_index,=links,=follow-links] Display DWARF info in the file -t, --syms Display the contents of the symbol table(s) -T, --dynamic-syms Display the contents of the dynamic symbol table -r, --reloc Display the relocation entries in the file -R, --dynamic-reloc Display the dynamic relocation entries in the file @<file> Read options from <file> -v, --version Display this program's version number -i, --info List object formats and architectures supported -H, --help Display this information
最新发布
09-10
### Release Mode vs Debug Mode Differences In programming, especially within environments like Visual C++ (VC), two primary build configurations exist: **Debug** and **Release** modes. These serve distinct purposes during development. #### Characteristics of Debug Mode The debug configuration is designed primarily for testing and troubleshooting code. This setup includes several features that facilitate easier debugging: - Detailed symbolic information embedded into executables or libraries. - Disabled optimizations which can obscure the relationship between source code and machine instructions[^1]. This allows developers to set breakpoints, inspect variables at runtime, step through lines of code one by one, etc., making it much simpler to identify issues early in the software lifecycle. #### Characteristics of Release Mode Conversely, release builds aim for optimal performance rather than ease-of-debugging capabilities. Key aspects include: - Full optimization enabled; this may involve reordering operations, removing redundant calculations, inline function expansion among others. - Minimal or no symbol table inclusion reduces executable size significantly compared with its counterpart. - Assertions are typically turned off unless explicitly specified otherwise. These changes result in faster execution times but make direct inspection more challenging due to lack of detailed metadata about program structure. ### Switching Between Modes To toggle between these settings in most Integrated Development Environments (IDEs): For projects using Microsoft's Visual Studio as an example: - Select `Build` from menu bar options. - Choose either `Configuration Manager`. - In Configuration dropdown list choose desired option (`Debug`, `Release`) accordingly before compiling project files again. Alternatively via command line tools provided alongside compilers such as MSBuild where appropriate flags control output type similarly. ```bash msbuild MyProject.sln /p:Configuration=Release ``` Or directly specifying target when invoking compiler commands manually if necessary. --related questions-- 1. What specific types of optimizations occur in a release build versus those present in debug? 2. How does enabling/disabling certain compile-time checks impact application stability across different versions? 3. Can you explain what happens internally when switching from debug to release compilation profiles? 4. Are there any best practices recommended while preparing applications specifically meant for production deployment?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值