kcw*_*cwu 167 debugging gcc pdb-files
我知道我可以使用-g选项生成调试符号.但是,符号将嵌入目标文件中.gcc可以在结果可执行文件/库之外生成调试符号吗?像Windows VC++编译器的.pdb文件一样.
lot*_*har 175
您需要使用objcopy来分隔调试信息:
objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
strip --strip-debug --strip-unneeded "${tostripfile}"
objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"
Run Code Online (Sandbox Code Playgroud)
我使用下面的bash脚本将调试信息分离为.debug目录中带有.debug扩展名的文件.这样我可以在一个tar文件和另一个tar文件中的.debug目录中tar和库和可执行文件.如果我想稍后添加调试信息,我只需提取调试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}"
Run Code Online (Sandbox Code Playgroud)
小智 104
编译调试信息:
gcc -g -o main main.c
Run Code Online (Sandbox Code Playgroud)
分离调试信息:
objcopy --only-keep-debug main main.debug
Run Code Online (Sandbox Code Playgroud)
要么
cp main main.debug
strip --only-keep-debug main.debug
Run Code Online (Sandbox Code Playgroud)
从原始文件中删除调试信息:
objcopy --strip-debug main
Run Code Online (Sandbox Code Playgroud)
要么
strip --strip-debug --strip-unneeded main
Run Code Online (Sandbox Code Playgroud)
通过debuglink模式调试:
objcopy --add-gnu-debuglink main.debug main
gdb main
Run Code Online (Sandbox Code Playgroud)
您还可以分别使用exec文件和符号文件:
gdb -s main.debug -e main
Run Code Online (Sandbox Code Playgroud)
要么
gdb
(gdb) exec-file main
(gdb) symbol-file main.debug
Run Code Online (Sandbox Code Playgroud)
详情如下:
(gdb) help exec-file
(gdb) help symbol-file
Run Code Online (Sandbox Code Playgroud)
参考:
https
://sourceware.org/gdb/onlinedocs/gdb/Files.html#Files https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
注意:使用高优化级别(-O3,-O4)编译的程序无法为优化变量,内联函数和展开循环生成许多调试符号,无论嵌入(-g)或提取(objcopy)的符号是什么'.debug'文件.
替代方法是
第一个选项提供了一种方法,可以在以后使用完整的调试和符号重建生产代码.能够在没有优化的情况下重新构建原始生产代码是调试的巨大帮助.(注意:这假设测试是使用程序的优化版本完成的).
您的构建系统可以创建一个.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
Run Code Online (Sandbox Code Playgroud)
编译程序后,您可以使用以下命令找到代码的原始"提交": 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
Run Code Online (Sandbox Code Playgroud)
剩下的就是签出原始代码,重新编译而不进行优化,然后开始调试.
到目前为止没有任何答案提到eu-strip --strip-debug -f <out.debug> <input>
。
elfutils
包提供的。<input>
文件已删除调试符号,这些符号现在都在<out.debug>
.