如何在构建目标之外生成gcc调试符号?
内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
- ·回答 (2)
- ·关注 (0)
- ·查看 (852)
我知道我可以使用-g选项生成调试符号。但是符号会嵌入在目标文件中。gcc能否在可执行文件/库之外生成调试符号?像Windows VC ++编译器的.pdb文件一样。
·提问于 2018-01-18
2 个回答
·回答于 2018-01-18
使用调试信息编译:
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
参考文献:
·回答于 2018-01-18
你需要使用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文件中,另一个放在.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}"
登录 后可回答问题
广告关闭
50+款云产品免费体验
提供包括云服务器,云数据库在内的50+款云计算产品。打造一站式的云产品试用服务,助力开发者和企业零门槛上云。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

![图片[1]就去ID网如何在构建目标之外生成gcc调试符号? – 问答 – 云+社区 – 腾讯云就去ID网97ID网](https://ask.qcloudimg.com/raw/yehe-fbd3d4418/95mayoszpj.jpg)
![[原创]让你的Android Studio能够对任意进程进行源码级native debug-Android安全-看雪论坛-安全社区|安全招聘|bbs.pediy.com就去ID网97ID网](https://97id.com/wp-content/uploads/2021/11/08a65e3e8bf430f45a22527554bf5567.png)

![[原创]Vmprotect3.5.1 壹之型 — 暗月·宵之宫-软件逆向-看雪论坛-安全社区|安全招聘|bbs.pediy.com就去ID网97ID网](https://97id.com/wp-content/uploads/2022/02/28065f7dad11fe88353c6b934b9dc7f8-300x41.jpg)
请登录后查看评论内容