Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate gcc debug symbol outside the build target?

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.

like image 467
kcwu Avatar asked May 15 '09 02:05

kcwu


People also ask

How do I enable built in debugging in GCC?

When writing C/C++ code, in order to debug the binary executable the debug option must be enabled on the compiler/linker. In the case of GCC, the option is -g.

What compiler flag is used to generate a debug build?

The -g flag tells the compiler to generate debugging information. It has no impact on whether or not a core file will be generated.


1 Answers

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}" 
like image 132
lothar Avatar answered Sep 28 '22 01:09

lothar