Ubuntu 18.10: 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.

$ U=http://ddebs.ubuntu.com
$ D=$(lsb_release -cs)
$ cat <<EOF | sudo tee /etc/apt/sources.list.d/ddebs.list
deb ${U} ${D} main restricted universe multiverse
#deb ${U} ${D}-security main restricted universe multiverse
deb ${U} ${D}-updates main restricted universe multiverse
deb ${U} ${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 disables deb-src by default. You need to enable deb-src for downloading package source code.

Enable deb-src.

$ grep '^deb ' /etc/apt/sources.list | \
  sed 's/^deb /deb-src /g' | \
  sudo tee /etc/apt/sources.list.d/deb-src.list
$ sudo apt update  -y

Install dpkg-dev package for dpkg-source command.

$ sudo apt install -y dpkg-dev

Download package source code.

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

In case of bash is as below. bash-4.4.18 is a source tree.

$ mkdir ~/bash
$ cd ~/bash
$ apt source bash
$ ls
bash-4.4.18                         bash_4.4.18-2ubuntu1.dsc
bash_4.4.18-2ubuntu1.debian.tar.xz  bash_4.4.18.orig.tar.xz

4 Install GDB

Install gdb.

$ sudo apt install -y gdb

5 Debug package

Run GDB with command and path of source code.

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

In case of bash is as below.

$ gdb bash --directory ~/bash/bash-4.4.18/
<snip>
(gdb) b main
Breakpoint 1 at 0x2fdb0: file .././shell.c, line 368.
(gdb) r
Starting program: /bin/bash
Breakpoint 1, main (argc=1, argv=0x7fffffffe418, env=0x7fffffffe428) at
.././shell.c:368
368     {
(gdb) l
363     int
364     main (argc, argv, env)
365          int argc;
366          char **argv, **env;
367     #endif /* !NO_MAIN_ENV_ARG */
368     {
369       register int i;
370       int code, old_errexit_flag;
371     #if defined (RESTRICTED_SHELL)
372       int saverst;

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

(gdb) la src

0001_gdb-la-src.png