代码之家  ›  专栏  ›  技术社区  ›  kcwu

如何在构建目标之外生成gcc调试符号?

  •  156
  • kcwu  · 技术社区  · 17 年前

    我知道我可以使用-g选项生成调试符号。但是,符号嵌入到目标文件中。GCC是否可以在结果可执行文件/库之外生成调试符号?与Windows VC++编译器的.pdb文件类似。

    4 回复  |  直到 7 年前
        1
  •  159
  •   chmurli lothar    14 年前

    你需要使用 物件拷贝 separate the debug information :

    objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
    strip --strip-debug --strip-unneeded "${tostripfile}"
    objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"
    

    我使用下面的bash脚本将调试信息分隔成.debug目录中扩展名为.debug的文件。通过这种方式,我可以对一个tar文件中的库和可执行文件以及另一个tar文件中的.debug目录进行tar。如果以后我想添加调试信息,我只需提取调试tar文件,voila就有了符号化的调试信息。

    这是bash脚本:

    #!/bin/bash
    
    scriptdir=`dirname ${0}`
    scriptdir=`(cd ${scriptdir}; pwd)`
    scriptname=`basename ${0}`
    
    set -e
    
    function errorexit()
    {
      errorcode=${1}
      shift
      echo $@
      exit ${errorcode}
    }
    
    function usage()
    {
      echo "USAGE ${scriptname} <tostrip>"
    }
    
    tostripdir=`dirname "$1"`
    tostripfile=`basename "$1"`
    
    
    if [ -z ${tostripfile} ] ; then
      usage
      errorexit 0 "tostrip must be specified"
    fi
    
    cd "${tostripdir}"
    
    debugdir=.debug
    debugfile="${tostripfile}.debug"
    
    if [ ! -d "${debugdir}" ] ; then
      echo "creating dir ${tostripdir}/${debugdir}"
      mkdir -p "${debugdir}"
    fi
    echo "stripping ${tostripfile}, putting debug info into ${debugfile}"
    objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
    strip --strip-debug --strip-unneeded "${tostripfile}"
    objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"
    chmod -x "${debugdir}/${debugfile}"
    
        2
  •  96
  •   Ciro Santilli OurBigBook.com    10 年前

    使用调试信息编译:

    gcc -g -o main main.c
    

    分离调试信息:

    objcopy --only-keep-debug main main.debug
    

    cp main main.debug
    strip --only-keep-debug main.debug
    

    从源文件中删除调试信息:

    objcopy --strip-debug main
    

    strip --strip-debug --strip-unneeded main
    

    通过调试模式调试:

    objcopy --add-gnu-debuglink main.debug main
    gdb main
    

    也可以单独使用exec文件和符号文件:

    gdb -s main.debug -e main
    

    gdb
    (gdb) exec-file main
    (gdb) symbol-file main.debug
    

    详情:

    (gdb) help exec-file
    (gdb) help symbol-file
    

    裁判:
    https://sourceware.org/gdb/onlinedocs/gdb/Files.html#Files https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html

        3
  •  7
  •   Lance Richardson    17 年前

    签出的“--only keep debug”选项 strip 命令。

    从链接:

    其目的是将此选项与--add gnu debuglink结合使用,以创建由两部分组成的可执行文件。一个是剥离的二进制文件,它在RAM和分发中占用的空间较小;另一个是调试信息文件,仅在需要调试功能时才需要。

        4
  •  7
  •   jww avp    7 年前

    注意:使用高优化级别(-o3,-o4)编译的程序无法为优化变量、线性函数和展开循环生成许多调试符号,无论符号是嵌入的(-g)还是提取的(objcopy)到“.debug”文件中。

    替代方法是

    1. 将版本控制(VCS、Git、SVN)数据嵌入到程序中,以获得编译器优化的可执行文件(-O3、-O4)。
    2. 生成可执行文件的第二个非优化版本。

    第一个选项提供了一种方法,可以在以后通过完全调试和符号重新生成生产代码。能够在不进行优化的情况下重新构建原始的生产代码对于调试是一个巨大的帮助。(注意:这假设测试是用程序的优化版本完成的)。

    您的构建系统可以创建一个.c文件,其中包含编译日期、提交和其他VCS详细信息。下面是一个“make+git”示例:

    program: program.o version.o 
    
    program.o: program.cpp program.h 
    
    build_version.o: build_version.c    
    
    build_version.c: 
        @echo "const char *build1=\"VCS: Commit: $(shell git log -1 --pretty=%H)\";" > "$@"
        @echo "const char *build2=\"VCS: Date: $(shell git log -1 --pretty=%cd)\";" >> "$@"
        @echo "const char *build3=\"VCS: Author: $(shell git log -1 --pretty="%an %ae")\";" >> "$@"
        @echo "const char *build4=\"VCS: Branch: $(shell git symbolic-ref HEAD)\";" >> "$@"
        # TODO: Add compiler options and other build details
    
    .TEMPORARY: build_version.c
    

    编译程序后,您可以使用以下命令找到代码的原始“commit”: strings -a my_program | grep VCS

    VCS: PROGRAM_NAME=my_program
    VCS: Commit=190aa9cace3b12e2b58b692f068d4f5cf22b0145
    VCS: BRANCH=refs/heads/PRJ123_feature_desc
    VCS: AUTHOR=Joe Developer  joe.developer@somewhere.com
    VCS: COMMIT_DATE=2013-12-19
    

    剩下的就是签出原始代码,在不进行优化的情况下重新编译,然后开始调试。