Translation(s): English - Português (Brasil) - Русский
This page will attempt to explain how to get a meaningful debugging backtrace to help understand a reproducible program crash. It is structured as an worked example.
Introduction
For this example, we will explain how to obtain debugging symbols for the hello package. We will suggest a couple of options, and also explain how to rebuild the package so that debugging information is retained in the binary.
For reference, a "#" at the beginning of a line means that what follows is a command that has to be executed as root (or via sudo). A "$" at the beginning means that what follows is a command that should be executed as your normal user. A "(gdb)" at the beginning means that you should be typing the rest of the command line at the gdb (GNU Debugger) prompt.
For example, you should start by installing gdb:
# apt install gdb
Installing debug symbols
ToDo: Add explanation of debug symbols: there is some text in AutomaticDebugPackages
Automatically downloading debugging symbols with debuginfod
The easiest way get debugging symbols is to install libdebuginfod-common which sets up Debuginfod.
# apt install libdebuginfod-common
(this bit should be on Debuginfod): You should not need to do any other configuration. If it does not work, try:
# dpkg-reconfigure -plow libdebuginfod-common # source /etc/profile.d/debuginfod.sh
Installing the debugging symbols from a debug package
If you do not want to use debuginfod, you can install an AutomaticDebugPackages.
This needs the debug archive listed in your SourcesList: (ToDo: convert to modern debian.sources format)
deb http://deb.debian.org/debian-debug/ $RELEASE-debug main # for security updates deb http://deb.debian.org/debian-debug/ $RELEASE-proposed-updates-debug main
So, depending on your release, it might look like this:
deb http://deb.debian.org/debian-debug/ buster-debug main # for security updates deb http://deb.debian.org/debian-debug/ buster-proposed-updates-debug main
For testing, unstable or experimental you can use:
deb http://deb.debian.org/debian-debug/ testing-debug main deb http://deb.debian.org/debian-debug/ unstable-debug main deb http://deb.debian.org/debian-debug/ experimental-debug main
Install the debian-goodies and run the find-dbgsym-packages to find which dbgsym packages to install:
$ find-dbgsym-packages /usr/bin/hello hello-dbgsym
If this does not find anything, see if the package you’re trying to debug includes a -dbgsym or -dbg package. So for the hello source package, we would look for a hello-dbgsym or hello-dbg package using any package manager:
$ apt search hello dbg p hello-dbgsym - Debug symbols for hello
If an appropriate package was found you should just be able to install it.
# apt install hello-dbgsym
Rebuilding the package you’re debugging
Only use this section if neither of the previous two sections worked
If all else fails we can rebuild the package without stripping out the debug information.
with sbuild
Using sbuild means we can rebuild in a temporary environment and not have to install anything else on our main system.
The following rebuilds hello in a clean chroot, creating a new ./hello_2.12.3_arch.deb:
# apt install sbuild # apt source hello $ DEB_BUILD_OPTIONS="nostrip noopt" sbuild ./hello_2.12.3.dsc --distribution unstable
You can install the new deb with apt install ./hello_2.12.3_amd64.deb and you can inspect its contents by unpacking into ./rebuilt: the hello binary will be unstripped and can more easily be debugged.
$ mkdir rebuilt $ dpkg-deb --extract hello_2.12.3-1_amd64.deb ./rebuilt $ file ./rebuilt/usr/bin/hello ./rebuilt/usr/bin/hello: ELF 64-bit LSB pie executable, [...,] for GNU/Linux 3.2.0, with debug_info, not stripped
rebuilding on your main system
If you really want to build on the main system, doing everything by hand, this section is for you.
First install some required tools:
# apt install build-essential fakeroot gdb # apt build-dep hello
If the build-dep line fails, add appropriate deb-src lines in apt sources and run apt update
Download and rebuild our package from source, keeping debugging symbols:
$ DEB_BUILD_OPTIONS="nostrip noopt" apt -b source hello
You can now install the newly built package. There may be multiple .deb packages generated, so make sure to install only the ones you want. In this example, the .deb generated was called hello_2.1.1-4_amd64.deb:
# apt install ./hello_2.1.1-4_amd64.deb
You can ensure the binaries installed from your .deb have debugging symbols with the 'file' command, or with gdb itself (see below).
$ file /usr/bin/hello # output should contain "not stripped"
Running gdb
Batch mode
For including a backtrace in a bug report, you can run this command and then try to reproduce your crash:
$ gdb -batch -n -ex 'set pagination off' -ex run -ex bt -ex 'bt full' -ex 'thread apply all bt full' --args hello
Interactively
Use this method if you need to interactively run gdb.
Now run your program as follows, replacing "[--args]" with any arguments you want to run the program with:
$ gdb hello ... gdb loads ... (gdb) set pagination 0 (gdb) run [--args] ... hello loads...
- Then try to reproduce your crash. If you’re lucky, a crash will occur and you’ll be dropped back to the gdb prompt.
- If you are not so lucky to get a crash but instead get a freeze, you can still get gdb prompt by pressing CTRL-C in the terminal running gdb.
At that point, you can run:
(gdb) bt
You’ll then get a lot of output, which you can then copy & paste to a bug followup e-mail or other bug reporting tool.
When you’re done with gdb, you can just run:
(gdb) quit
If the problem seems to be in a major library such as libc6, xlibs, or libgtk2.0-0, you’ll want to install the appropriate -dbg package (e.g. libc6-dbg in the case of libc6) and then run the problematic program again under gdb.
Often, you will see a backtrace where one or more of the top lines is in malloc() or g_malloc(). When this happens, chances are your backtrace isn’t very useful. The easiest way to find some useful information is to set the environment variable MALLOC_CHECK_ to a value of 2. You can do this while running gdb by doing this:
$ MALLOC_CHECK_=2 gdb hello
Advanced gdb commands
If the program you’re backtracing is multi-threaded, you might want to get a backtrace for all threads:
(gdb) thread apply all bt
Another thing which is quite helpful to report is what variables were set locally at each point in the stack:
(gdb) bt full
You might want to report the output of the combination of the preceding options:
(gdb) thread apply all bt full
And if this is too much irrelevant output, you might want to keep only a few calls, such as the top 10:
(gdb) thread apply all bt full 10
If you have a large backtrace, you can log gdb output to a file (the default is gdb.txt):
(gdb) set logging on
To check you have debugging symbols in your binary:
$ gdb (gdb) symbol-file /usr/bin/hello # you should see something like this: Reading symbols from /usr/bin/hello ... done Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1". (gdb) # NB you should _not_ see Reading symbols from /usr/bin/hello... (no debugging symbols found)...done
Debugging X Errors
If a GTK program has received an X error; i.e. you see a message of the form:
The program 'preview1' received an X Window System error.
then you can try running the program with --sync, and break on the gdk_x_error function in order to obtain a backtrace, thus:
(gdb) break gdk_x_error (gdb) run --sync
Core dump
For a crashed program, a core dump file may be used to evaluate post-mortem snapshot of the program's state at the time of the crash. You can check resource limitation including the maximum size of core files created by typing "ulimit" on the shell prompt. You may set the core file size to unlimited using "ulimit -c unlimited" and/or with some other configuration files (/etc/security/limits.conf, /usr/lib/sysctl.d/50-coredump.conf, and systemd service files).
The location of core dump file is specified by /proc/sys/kernel/core_pattern. If the systemd-coredump package is installed under systemd, it may look like:
|/lib/systemd/systemd-coredump ...
Then the lz4-compressed core dump file is generated in /var/lib/systemd/coredump upon crash. You can get information on this compressed core file with:
$ coredumpctl info -1
You can use this compressed core file with gdb by:
$ coredumpctl gdb -1
See more:
systemd-coredump(1), and coredumpctl(1)
gdb(1) and gcore(1)
core(5), coredump.conf(5), sysctl(8) and sysctl.d(5)
https://www.freedesktop.org/software/systemd/man/systemd-coredump.html
core-dump-handler virtual package is provided by corekeeper, minicoredumper, systemd-coredump
See also
CategoryDebugging CategoryPermalink: This page is referenced in https://www.debian.org/doc/manuals/developers-reference/best-pkging-practices.en.html#automatically-generated-debug-packages
