Step-by-step debugging guide

From ArchWiki
Revision as of 23:28, 30 January 2012 by Trontonic (Talk | contribs) (Finally - report the bug)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

When an application fails

Run it from the commandline

If an application suddenly crashes, try running it from the commandline. If you're new to the commandline and use GNOME, press Alt+F2 and type "gnome-terminal", then type in the name of the application in lowercase letters. If you don't know the name of the executable, only the name of the package, the following command can be useful to find the name of the executable. Replace "packagename" with the name of the package:

for f in `pacman -Ql packagename | grep "/bin/" | cut -d" " -f2`; do file $f 2>/dev/null | grep -q executable && basename $f; done

Check if the application segfaults

If you see the word "segfault" or the phrase "segmentation fault", see if there is a file named "core" as well.

ls core

If there is, the application has segfaulted. The "core" file can, if the application is compiled in a debug-friendly way, be used to find out where things went wrong. Sometimes the core file ends up in one of the directories the application has visited instead of the current directory.

How to investigate a segfault

There are a couple of techniques that can be used to figure out what went wrong. Put your detective hat on. 🔎

Technique #1 - gdb

gdb is an ancient and well tested application for debugging applications. Try running the application (replace "appname" with the name of your executable) with gdb:

gdb appname
r
(wait for segfault)
bt full

Now post the output to one of the many pastebin sites on the web and include the URL if you file a bugreport.

Technique #2 - even better gdb output

If you're able to, recompile the application in question with the -g, -O0 and -fbuiltin flags, make sure "!strip" is in the options array in the PKGBUILD, install and try running it again with gdb, like above.

One way of enabling -g, -O0 and -fbuiltin is to put these two lines at the very beginning of the build() function in the relevant PKGBUILD:

export CFLAGS="$CFLAGS -O0 -fbuiltin -g"
export CXXFLAGS="$CXXFLAGS -O0 -fbuiltin -g"

Note that -g enables debug symbols and -O0 turns off optimizations. -O2 is the normal choice, -O3 is usually overkill and -O4 and above behaves exactly like -O3.

If you have a "core" file, it can be used to get a backtrace:

gdb appname core
bt full

How to investigate missing files or libraries

Technique #1 - strace

Strace is great for finding out, in detail, what an application is actually doing. If an application tries to open a file that just isn't there, it can be discovered by strace.

For finding which files a program named "appname" tries to open:

strace -o /dev/stdout appname | grep open

Again, save the output, post it to a pastebin site and keep the URL in handy.

Technique #2 - LD_DEBUG

Setting LD_DEBUG to "files" is another way to get an overview of which files an application are looking for. For an application named "appname":

LD_DEBUG=files appname > appname.log 2>&1

The output will end up in appname.log.

For more information about this:

man ld-linux

Technique #3 - no such file or directory

If you get "no such file or directory" when running an application, try the following command:

readelf -a /usr/bin/appname | grep interp

(replace /usr/bin/appname with the location of your executable)

Make sure the interpreter in question (like /lib/ld-linux-x86-64.so.2) actually exists. Install ld-lsb from AUR if you have to.

If it's not written in C or C++, but perhaps in Python

Use "file" on the executable to get more information (replace "appname" with your executable):

file /usr/bin/appname

If it says "ELF" it's a binary exectuable and is usually written in C or C++. If it says "Python script" you know you're dealing with an application written in Python.

If it's a shell script, open up the shell script in a text editor and see (usually at the bottom of the file) if you can find the name of the real application (ELF file). You can then temporarily put "gdb" right in the shellscript, before the name of the executable, for debugging purposes. See the sections about gdb further up.

For Python applications, the output will often say which file and line number the crash occured at. If you're proficient with Python, you can try to fix this and include the fix in the bug report.

Finally - report the bug

Please report a bug at https://bugs.archlinux.org and possibly also directly to the developers of the application in question, then include a link in the Arch Linux bug report. This helps us all.

However, if you think there's something wrong with the application itself, and not with how it is packaged, report the bug directly to upstream (which means the developers of the application).

Normally, software streams from developers, through packagers/maintainers and down to users. Upstream means the other way, so in this case directly to the developers of an application.

See also