Sunday, August 12, 2012

Easily rebuild Debian package(s) with debug symbols

You can easily rebuild many (usually only C- and C++-source-based) Debian  (Ubuntu, Mint, ...) packages so that they include the debug symbols. Let me give a quick example for dpkg. (Just replace dpkg with your package name)

For this you need first the source code of the package:
mkdir /tmp/dpkg
cd /tmp/dpkg
apt-get source dpkg
cd dpkg*

Then you want to update the version number of the package so that you can distinguish your build from the default build:
sudo apt-get install devscripts
dch --local ~debug
In the editor just add a description like "Debug build of dpkg for local development" after the star and save the file. Please note that the version number has been automatically changed. 
(If you just want to increment the version number use --increment instead of --local ~debug)

Now you need to rebuild the package. I prefer to use pdebuild (uses pbuilder) to not clutter my system with dozens of development packages. pdebuild will automatically install the development dependencies and build the package inside a chroot environment.
sudo apt-get install pbuilder
sudo bash -c "export DEB_BUILD_OPTIONS='debug nostrip noopt' ; pdebuild --use-pdebuild-internal --buildresult .."
This will rebuild the package with debug symbols and without optimizations and place the .deb file(s) in the parent directory.

Now all you need to do is to install the packages with the debug symbols. I prefer to use gdebi because it also installs the package dependencies for the case that the original packages aren't installed yet.
cd ..
sudo apt-get install gdebi-core
find . -type f -name \*.deb | xargs -n 1 --no-run-if-empty sudo gdebi --non-interactive
Please note that this installs all the created .deb files in the folder but you might want to install only some .deb files! Use this command with care...

And this is it. You can now start to debug with gdb and there will be debug symbols.


There are two more noteworthy items:
First of all with ~debug in the package version your build has a slightly lower version number than the default packages. This means a normal sudo apt-get update && sudo apt-get dist-upgrade will restore the default packages. If you don't want this you might want to put these packages on hold.
Second is that you can discard of the created pbuilder environment with a simple sudo pbuilder clean run.