如何在构建目标之外生成gcc调试符号? [英] How to generate gcc debug symbol outside the build target?

查看:149
本文介绍了如何在构建目标之外生成gcc调试符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

你需要使用 objcopy href =http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/4/html/Debugging_with_gdb/separate-debug-files.html =noreferrer>分离调试信息:

  objcopy --only-keep-debug$ {tostripfile}$ {debugdir} / $ {debugfile}
strip --strip-debug --strip-unneeded$ {tostripfile}
objcopy --add-gnu-debuglink =$ {debugdir} / $ {debugfile}$ {tostripfile}

我使用下面的bash脚本将调试信息与.debug扩展名中的调试目录。这样,我可以在一个tar文件中的库和可执行文件,另一个tar文件中的.debug目录。如果我想稍后添加调试信息,我只需要提取调试tar文件,并且我有符号调试信息。



这是bash脚本:

 #!/ bin / bash 

scriptdir =`dirname $ {0}`
scriptdir = (cd $ {scriptdir}; pwd)`
scriptname =`basename $ {0}`

set -e

函数errorexit()
{
errorcode = $ {1}
shift
echo $ @
exit $ {errorcode}
}

函数用法()
{
echoUSAGE $ {scriptname}< tostrip>


tostripdir =`dirname$ 1`
tostripfile =`basename$ 1`


如果[-z $ {tostripfile}];然后
使用
errorexit 0tostrip必须指定
fi

cd$ {tostripdir}

debugdir = .debug
debugfile =$ {tostripfile} .debug

如果[! -d$ {debugdir}];然后
echocreate dir $ {tostripdir} / $ {debugdir}
mkdir -p$ {debugdir}
fi
echo剥离$ {tostripfile},放调试信息到$ {debugfile}
objcopy --only-keep-debug$ {tostripfile}$ {debugdir} / $ {debugfile}
strip --strip-debug --strip -$$ {$}} $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / $ {debugfile}


I know I can generate debug symbol using -g option. However the symbol is embeded in the target file. Could gcc generate debug symbol outside the result executable/library? Like .pdb file of windows VC++ compiler did.

解决方案

You need to use objcopy to separate the debug information:

objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
strip --strip-debug --strip-unneeded "${tostripfile}"
objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"

I use the bash script below to separate the debug information into files with a .debug extension in a .debug directory. This way I can tar the libraries and executables in one tar file and the .debug directories in another. If I want to add the debug info later on I simply extract the debug tar file and voila I have symbolic debug information.

This is the bash script:

#!/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调试符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆