Ubuntu 16.04: Debug package with debug symbol

Ubuntu provides debug symbol as dbgsym package. This article will describe installing dbgsym and debugging package. This article will debug bash with debug symbol.

1 Add repository of dbgsym

dbgsym package is published in ddebs.ubuntu.com. Add ddebs.ubuntu.com to repository list.

$ D=`lsb_release -cs`
$ sudo su -c "
cat <<EOF > /etc/apt/sources.list.d/ddebs.list
deb http://ddebs.ubuntu.com ${D} main restricted universe multiverse
deb http://ddebs.ubuntu.com ${D}-security main restricted universe multiverse
deb http://ddebs.ubuntu.com ${D}-updates main restricted universe multiverse
#deb http://ddebs.ubuntu.com ${D}-proposed main restricted universe multiverse
EOF
"

Import GPG key of ddebs.ubuntu.com.

$ wget -O - http://ddebs.ubuntu.com/dbgsym-release-key.asc | sudo apt-key add -

Update repository database.

$ sudo apt update -y

2 Install dbgsym package

The package which has a suffix of dbgsym provides debug symbol.

<package>-dbgsym

In case of bash is as below.

$ sudo apt install -y bash-dbgsym
$ dpkg -L bash-dbgsym
/.
/usr
/usr/lib
/usr/lib/debug
/usr/lib/debug/usr
/usr/lib/debug/usr/bin
/usr/lib/debug/usr/bin/clear_console
/usr/lib/debug/bin
/usr/lib/debug/bin/bash

GDB will search /usr/lib/debug automatically even if you do not specify path of debug symbol.

3 Download package source code

Ubuntu 16.04 disables deb-src by default. You need to enable deb-src for downloading package source code.

$ sudo su -c "grep '^deb ' /etc/apt/sources.list | \
sed 's/^deb/deb-src/g' > /etc/apt/sources.list.d/src.list"

Update repository database.

$ sudo apt update -y

Download package source code.

$ mkdir <package>
$ cd <package>
$ apt source <package>

In case of bash is as below.

$ mkdir ~/bash
$ cd ~/bash
$ apt source bash
$ ls
bash-4.3                            bash_4.3-14ubuntu1.1.dsc
bash_4.3-14ubuntu1.1.debian.tar.xz  bash_4.3.orig.tar.gz

4 Install GDB

Install gdb64 for 64bit system or gdb for 32bit system.

$ sudo apt install -y gdb64

5 Debug package

Run GDB with command and path of source code.

$ gdb64 <command> --directory /path/to/source
<snip>
(gdb)

In case of bash is as below.

$ gdb64 bash --directory ~/bash/bash-4.3/
<snip>
(gdb) b main
Breakpoint 1 at 0x41eed0: file .././shell.c, line 361.
(gdb) r
Starting program: /bin/bash
warning: the debug information found in "/lib64/ld-2.23.so" does not
match "/lib64/ld-linux-x86-64.so.2" (CRC mismatch).


Breakpoint 1, main (argc=1, argv=0x7fffffffe1d8, env=0x7fffffffe1e8) at .././shell.c:361
warning: Source file is more recent than executable.
361     {
(gdb) l
356     int
357     main (argc, argv, env)
358          int argc;
359          char **argv, **env;
360     #endif /* !NO_MAIN_ENV_ARG */
361     {
362       register int i;
363       int code, old_errexit_flag;
364     #if defined (RESTRICTED_SHELL)
365       int saverst;

GDB command "la src" is useful for tracing source code.

(gdb) la src

0001_gdb-la-src.png