195

我知道我可以使用 -g 选项生成调试符号。但是,该符号嵌入在目标文件中。gcc 可以在结果可执行文件/库之外生成调试符号吗?就像 windows VC++ 编译器的 .pdb 文件一样。

4

5 回答 5

203

您需要使用objcopy分隔调试信息

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 文件,瞧,我有符号调试信息。

这是 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}"
于 2009-05-15T02:13:02.817 回答
137

使用调试信息编译:

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

通过 debuglink 模式调试:

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

于 2013-08-28T11:57:56.387 回答
11

注意:使用高优化级别(-O3、-O4)编译的程序不能为优化变量、内联函数和展开循环生成许多调试符号,无论符号是嵌入(-g)还是提取(objcopy)到一个'.debug' 文件。

替代方法是

  1. 将版本控制(VCS、git、svn)数据嵌入程序中,用于编译器优化的可执行文件(-O3、-O4)。
  2. 构建可执行文件的第二个非优化版本。

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

您的构建系统可以创建一个加载了编译日期、提交和其他 VCS 详细信息的 .c 文件。这是一个“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

程序编译后,您可以使用以下命令找到代码的原始“提交”: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

剩下的就是检查原始代码,重新编译而不进行优化,然后开始调试。

于 2013-12-27T20:25:14.767 回答
9

查看strip命令的“--only-keep-debug”选项。

从链接:

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

于 2009-05-15T02:17:18.967 回答
9

到目前为止没有答案提到eu-strip --strip-debug -f <out.debug> <input>

  • 这是由elfutils包提供的。
  • 结果将是该<input>文件已被剥离了调试符号,这些符号现在都在<out.debug>.
于 2019-11-02T23:36:57.537 回答