Background: I am programming with Qt5 and want to debug the core Qt5 libraries on Debian using GDB.
I have installed all the necessary -dev
and -dbg
packages. When I try to debug, GDB complains that the source code is not found.
How to install source code and use within GDB?
Best Answer
I am answering my own question. This was harder than I expected, and I want to gather all the information in one convenient place.
Install Binary, Development, and Debug Packages
This can be done using
apt
(oraptitude
).Example for Qt5:
qt5-default
qtbase5-dev
qtbase5-dbg
Find Source Package Name
You can find it using
aptitude
or the Debian package search tool: https://www.debian.org/distrib/packages#search_packagesExample: Searching for
qtbase5-dbg
will find: https://packages.debian.org/jessie/qtbase5-dbgThe top of each binary package page has a link to the source package.
Example:
qtbase-opensource-src
Install Source Package
If necessary, first configure your
apt
source package repositories by reading this answer: https://unix.stackexchange.com/a/121042/29414su -
cd /usr/src
apt-get source ${source_package_name}
apt-get source qtbase-opensource-src
/usr/src/qtbase-opensource-src-5.3.2+dfsg/
.dsc
).rm *.tar.* *.dsc
ln -s qtbase-opensource-src-5.3.2+dfsg qtbase-opensource-src
Configure GDB
(This section can be completed using your non-root / regular account.)
GDB needs configuration to know where source code can be found.
Each source package is a little different, so I will use Qt5 as an example.
cd /usr/src/qtbase-opensource-src/src
find $(pwd) -mindepth 1 -maxdepth 1 -type d
/usr/src/qtbase-opensource-src/src/corelib ...
find
output to create GDB commands:find $(pwd) -mindepth 1 -maxdepth 1 -type d | sort | xargs -l -i printf -- 'directory %s\n' "{}"
directory /usr/src/qtbase-opensource-src/src/3rdparty ...
~/.gdbinit
for convenience.Sample
~/.gdbinit
:Run GDB
This section assumes you already have a program compiled with debugging symbols linked to the library of interest, e.g., Qt5.
gdb ${program_name}
main()
automatically:start
b '${function_signature}'
Command
b(reakpoint)
with single quotes supports (very impressive!) tab completion.b 'QStyled<tab>
->
b 'QStyledItemDelegate
->
::paint(<tab>
->
b 'QStyledItemDelegate::paint(QPainter*, QStyleOptionViewItem const&, QModelIndex const&) const'
c
orcontinue