Find Which Files and Configs a Program Opens With strace
Use strace 6.8 on Linux to list every file a program opens, find its real config path from the ENOENT search order, and spot unexpected reads and writes.

Documentation says a tool reads ~/.toolrc. The tool behaves as if it reads something else. Distribution packages move configuration, wrappers set environment variables, and old settings files linger in a home directory and override new ones. When the question is "what does this program actually read?", strace answers it from the system call level, with no source code. This post shows how to list every file a program opens with strace 6.8 on Linux, how to read the search order from failed opens, how to tell reads from writes, and how to cope with a large trace. It assumes you have used strace before, as in the introduction to tracing system calls.
Trace the open calls
On current Linux systems with glibc 2.39, open() is implemented with the openat system call, so that is the name to filter on. The %file set covers it and its relatives:
strace -f -e trace=openat -o /tmp/open.trace ./mytool --verboseWriting to a file keeps the program's own output separate. A short program that reads one configuration file produces a trace like this:
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/home/dev/.config/mytool/config.toml", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/dev/.mytoolrc", O_RDONLY) = 3
openat(AT_FDCWD, "/home/dev/.local/share/mytool/cache.db", O_RDWR|O_CREAT, 0644) = 4
+++ exited with 0 +++The first two lines are the dynamic loader finding libc, which every dynamically linked program does. The remaining three are the tool's own behavior, and they say a lot. It looked first for a TOML file under .config, did not find it, fell back to .mytoolrc, which exists, and opened a cache database for reading and writing, creating it if absent.
Read the search order
The failed lines are the most informative part. A program that supports several config locations tries them in a fixed order, and each failed openat with ENOENT is a place that was checked and found empty. The first success is the file in effect. If the program stops at the first hit, the lines after it were never tried, and the order of the failures shows the precedence.
Filter to the failures when the trace is long:
strace -f -e trace=openat -e status=failed ./mytoolopenat(AT_FDCWD, "/home/dev/.config/mytool/config.toml", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/mytool/override.conf", O_RDONLY) = -1 ENOENT (No such file or directory)Do not treat every failure as a problem. A program may probe for optional files by design, and a missing optional file is the normal case.
Other errors are more interesting than ENOENT:
| Error | What it tells you |
|---|---|
EACCES | the file exists, but this user cannot read it, a permissions or ownership problem |
ENOTDIR | a component of the path is a file, not a directory |
ELOOP | too many symbolic links, often a link loop |
EMFILE | the process is out of file descriptors |
Tell reads from writes
The flags argument says how a file is opened. O_RDONLY is a read, O_WRONLY or O_RDWR a write, and O_CREAT means the call may create the file. To find everything a program writes, search the trace for the writing flags:
grep -E 'O_(WRONLY|RDWR)|O_CREAT' /tmp/open.traceopenat(AT_FDCWD, "/home/dev/.local/share/mytool/cache.db", O_RDWR|O_CREAT, 0644) = 4This is the quick way to answer "does this tool write anywhere I do not expect?", for instance before running an unfamiliar script on a production host. A file opened O_WRONLY|O_CREAT|O_TRUNC is being replaced, which is worth knowing before you run the program twice.
Follow what was read afterwards
An openat only opens a file. To see what the program read from it, include read and use -y, which decorates descriptor numbers with their paths:
strace -y -s 120 -e trace=openat,read ./mytoolopenat(AT_FDCWD, "/home/dev/.mytoolrc", O_RDONLY) = 3</home/dev/.mytoolrc>
read(3</home/dev/.mytoolrc>, "port = 8080\nlog = /var/log/mytool.log\n", 4096) = 38
read(3</home/dev/.mytoolrc>, "", 4096) = 0The read shows the file content as the program saw it, truncated at the -s limit. That is how you catch the case where the file on disk differs from the one that was read, for example because a different home directory was in effect when the process ran as a service. Seeing the actual port = 8080 line settles an argument faster than reading both copies of the file.
Include the other ways a program finds files
Not every file access goes through openat. To get a complete picture use the full %file set, which also includes stat-family calls, access, readlink and execve:
strace -f -e trace=%file -o /tmp/files.trace ./mytoolPrograms often check a file with access or newfstatat before opening it, or look up a path with readlink to resolve a symbolic link. Those lines show which path a program resolved to, which is useful when the file you edited was a link to somewhere else.
A worked case: the setting that would not change
A developer edits /etc/mytool/mytool.conf, restarts the service and sees no effect. Reading the documentation again will not help, but the trace will. Start the service binary under strace the way the unit file does, with the same user and environment:
sudo -u mytool strace -f -e trace=openat -e status=failed -o /tmp/svc.trace /usr/local/bin/mytool --serve
grep -c ENOENT /tmp/svc.trace2The two failures are the paths the service checks before it finds a file, and the line after them shows the first success, which was /var/lib/mytool/mytool.conf, a copy created by an earlier version and still read first. The edit to /etc never mattered because a higher-priority file shadowed it. The fix is to remove or update the file the service really reads, and the trace is the evidence that tells you which one that is.
This is also the right method for confirming a fix. After changing the file, run the same command again and check that the successful open now names the path you intended. A change that does not alter the openat lines has not changed what the program reads.
Handle a large trace
A real program can open hundreds of files. Three habits keep that readable:
Remove the loader noise with
grep -v -E 'ld.so|[.]so'. Anything under/lib,/usr/libor ending in.sois a shared library.Sort the unique paths:
grep -o '"[^"]*"' /tmp/open.trace | sort -ulists every distinct path the program touched.Use
-ffor a program that launches helpers, so files opened by child processes are included, as covered in the post on following child processes.
What strace will not show
Two limits are worth knowing. A program that reads a file through a memory mapping makes an openat and an mmap but no read, so a missing read does not mean the content was ignored. And a program that uses a library function such as getenv to find a path shows the environment lookup nowhere in a system call trace, because the environment is already in memory. If the path was built from an environment variable, the openat argument still shows the final path, which is the answer you wanted, though not why that path was chosen.


