Tip of the Trade: strace

Tip of the Trade: strace

May 16, 2006

When a Linux or Unix program fails to run and does not offer anything helpful in the way of error messages, the resourceful admin typically resorts to Google searches and haunting user forums to find answers. This often entails a fair bit of hit-or-miss diagnosis, which is the most time-consuming and frustrating part.

What if you could look under the hood and pinpoint the failure before wandering all over cyberspace? You can, with strace, the "system call tracer."

Traditionally strace is a programmer's debugging tool, but ordinary end users can benefit from it as well. It shows each system call, arguments, and return values. The simplest way to run it is like this:

$ strace [program_name]

For example, when Tuxracer is run from the games menu (hey, hardworking admins need relaxation, too) nothing happens, with no indication of why nothing happens.

However, with strace:

$ strace tuxracer
execve("/usr/games/tuxracer", ["tuxracer"], [/* 32 vars */]) = 0
uname({sys="Linux", node="windbag", ...}) = 0
brk(0) = 0x80eb000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
[snip voluminous output]
write(1, "tuxracer isn't developed any fur"..., 69tuxracer isn't developed any further. Please run 'ppracer' instead.
) = 69
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
write(1, "For further Information please r"..., 74For further Information please read /usr/share/doc/tuxracer/README.Debian
) = 74

Even nonprogrammers will understand much of the output, like "No such file or directory," and "tuxracer isn't developed any further. Please run 'ppracer' instead." Those are messages that apparently should be displayed somewhere, but weren't. Just for kicks, from our terminal:

$ tuxracer
tuxracer isn't developed any further. Please run 'ppracer' instead.
For further Information please read /usr/share/doc/tuxracer/README.Debian

In an ideal world, since Tuxracer is a graphical program not customarily run from the command line, error messages would be sent where they could be read. But this world is far from ideal, so here we are.

strace has a number of useful options. Because of the volume of output, consider sending it to a file:

$ strace -o tuxracer.txt tuxracer

Use the -p option to monitor a running process. Hit Ctrl+C to stop it.

strace output isn't all that mysterious, just over-abundant. With a little patience you can usually figure out where an application went wrong.