Ubuntu provides debug symbol as dbgsym package. This article will describe installing dbgsym and debugging package. This article will debug bash with debug symbol.
Table of Contents
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/.build-id /usr/lib/debug/.build-id/16 /usr/lib/debug/.build-id/16/ef52e314889583b73f26a98eed260667fb1ed8.debug /usr/lib/debug/.build-id/60 /usr/lib/debug/.build-id/60/88017e1b386d955ce384f28b0bc642d5f6a38d.debug /usr/share /usr/share/doc /usr/share/doc/bash-dbgsym
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-5.0 is a source tree.
$ mkdir ~/bash $ cd ~/bash $ apt source bash $ ls bash-5.0 bash_5.0-3ubuntu1.1.debian.tar.xz bash_5.0-3ubuntu1.1.dsc bash_5.0.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-5.0/ <snip> (gdb) b main Breakpoint 1 at 0x2de30: file .././shell.c, line 364. (gdb) r Starting program: /usr/bin/bash Breakpoint 1, main (argc=1, argv=0x7fffffffe3b8, env=0x7fffffffe3c8) at .././shell.c:364 364 { (gdb) l 359 int 360 main (argc, argv, env) 361 int argc; 362 char **argv, **env; 363 #endif /* !NO_MAIN_ENV_ARG */ 364 { 365 register int i; 366 int code, old_errexit_flag; 367 #if defined (RESTRICTED_SHELL) 368 int saverst;
GDB command "la src" is useful for tracing source code.
(gdb) la src