【发布时间】:2010-10-26 09:18:05
【问题描述】:
我知道我可以使用 -g 选项生成调试符号。但是,该符号嵌入在目标文件中。 gcc 可以在结果可执行文件/库之外生成调试符号吗?就像windows VC++编译器的.pdb文件一样。
【问题讨论】:
我知道我可以使用 -g 选项生成调试符号。但是,该符号嵌入在目标文件中。 gcc 可以在结果可执行文件/库之外生成调试符号吗?就像windows VC++编译器的.pdb文件一样。
【问题讨论】:
你需要使用objcopy到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 文件,瞧,我有符号调试信息。
这是 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}"
【讨论】:
--build-id linker option有什么问题吗?
使用调试信息编译:
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
【讨论】:
objcopy --add-gnu-debuglink main main.debug 嵌入创建的调试文件的名称和校验和。在这种情况下,gdb 将尝试在几个依赖于发行版的位置找到调试代码本身,不再需要 -s 选项。
注意:使用高优化级别 (-O3, -O4) 编译的程序不能为优化变量、内联函数和展开循环生成许多调试符号,无论符号是嵌入 (-g) 还是提取 (objcopy ) 到一个“.debug”文件中。
替代方法是
第一个选项提供了一种在以后通过完整的调试和符号重建生产代码的方法。能够在没有优化的情况下重新构建原始生产代码对调试有很大帮助。 (注意:这假设测试是使用程序的优化版本完成的)。
您的构建系统可以创建一个加载了编译日期、提交和其他 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
剩下的就是检查原始代码,重新编译而不进行优化,然后开始调试。
【讨论】:
-O4 甚至不存在。
查看strip 命令的“--only-keep-debug”选项。
来自链接:
目的是这个选项将与 --add-gnu-debuglink 结合使用来创建一个两部分的可执行文件。一个是剥离的二进制文件,它将在 RAM 和分发中占用更少的空间,第二个是调试信息文件,仅在需要调试能力时才需要。
【讨论】:
目前没有答案提到eu-strip --strip-debug -f <out.debug> <input>。
elfutils 包提供的。 <input> 文件中的调试符号已被删除,这些调试符号现在都在<out.debug> 中。【讨论】: