About strace
(August 2004)
strace
is a useful tool, included in most Linux distributions, which shows all system calls when a program is run (system calls are basic operations like open a file, get a pid, write some text, sleep,...).
Sometimes you want to modify the configuration file of a program, but you do not know in which directory this configuration is located. You can use strace
to discover this information.
Suppose your program is bash
(shell), and you want to know which files are read for configuring the environment variables. You have to enter:
strace -o /tmp/sh.strace bash
(bash
is launched and the result of strace
is written to the /tmp/sh.strace
file)
Then hit ctrl-D to exit the bash which has been launched, and view the /tmp/sh.strace
file.
The interesting lines I get in my /tmp/sh.strace
file are:
open("/etc/bash.bashrc", O_RDONLY|O_LARGEFILE) = 3
open("/etc/profile.d/complete.bash", O_RDONLY|O_LARGEFILE) = 3
open("/home/fred/.bashrc", O_RDONLY|O_LARGEFILE) = 3
open("/etc/inputrc", O_RDONLY) = 3
(you will have a slightly different result on your computer)
We discover that these 4 files are opened by bash
. Some of them may be used for the setting of environment variables (we do not have more information at this point, but it is afterwards easy to test if the setting of a new environment variable in one of them is effective or not).
When a program fails, it may be helpful to use strace
to find out where the program has failed (at which point).
These are simple cases where one understands immediately why the program fails: