我知道可以使用 -g 选项生成调试符号。但是这些符号被嵌入到目标文件中。是否有办法让gcc将调试符号生成在可执行文件/库之外,就像Windows VC++编译器生成的 .pdb 文件一样?
objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
strip --strip-debug --strip-unneeded "${tostripfile}"
objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"
#!/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}"
编译时包含调试信息:
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
您还可以单独使用执行文件和符号文件:
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选项。 - Lothar至今没有一个回答提到 eu-strip --strip-debug -f <out.debug> <input>
。
elfutils
软件包提供的。<input>
文件已被剥除了调试符号,这些符号现在全部在<out.debug>
中。注意:使用高度优化级别(-O3、-O4)编译的程序无法为经过优化的变量、内联函数和展开循环生成许多调试符号,无论这些符号是嵌入的(-g)还是被提取到“.debug”文件中(objcopy)。
替代方法包括:
第一种选项提供了在以后重新构建生产代码时获得完整调试和符号的手段。能够重新构建原始的未优化生产代码对于调试是极大的帮助。(注意:这假定已经使用了程序的优化版本进行了测试)。
您的构建系统可以创建一个包含编译日期、提交和其他 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
来查找代码的原始“commit”。
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
并不存在。 - Hi-Angel
--build-id
链接器选项会有什么问题吗? - jww