Programs ask the kernel for everything
A user program cannot touch the disk, the network card, or another process's memory directly. It has to ask the kernel. The request goes through a fixed set of entry points called system calls, and each one has a name and a number. openat opens a file, read and write move bytes, execve replaces the program image, and mmap maps memory into the address space. The C library wraps these calls in functions that look ordinary, so fopen eventually becomes a system call that you never see in the source.
When a command misbehaves, the system calls usually tell the story faster than the code. If a program reports that a file is missing, the trace shows which path it tried and what the kernel answered. If a program hangs, the trace shows the call it is blocked in. strace is the tool that prints that conversation.
What strace prints
strace starts a command, uses the ptrace interface to stop it at each system call entry and exit, and prints one line per call. A line contains the call name, its arguments in a readable form, and the return value after the equals sign. A failed call returns -1 and sets an error number, and strace prints both the number and its name, such as ENOENT, which means no such file or directory.
strace writes its report to standard error, not standard output. That matters when you pipe or redirect a command. The program's own output stays on standard output, and the trace lands on the terminal beside it, which is usually what you want. The option -o sends the trace to a file instead, which keeps the terminal clean.
strace -o trace.txt ls /workspaceThe listing appears on the terminal, and trace.txt holds the system calls. The first line of the trace is an execve call that started ls. The last line reports the exit status, such as +++ exited with 0 +++.
Filtering the noise
A simple program makes a few dozen system calls, and a shell pipeline can make hundreds. Most are not interesting for a given question. The -e option selects which calls to trace. The expression -e trace=openat keeps only file opens, and -e trace=file keeps every call that takes a path name. Other classes include network, process, and memory. The strace manual page describes the full set of classes.
strace -e trace=file ls /nopeThis run shows the path lookups that ls attempts. The failed lookup on /nope ends with ENOENT, and the program then prints its error message and exits with status 2. The trace explains the message: the kernel said the path does not exist, and the program reported that fact.
Summaries and counts
Reading every line is not always practical. The -c option counts calls instead of printing them. It prints a table with the time spent in each call, the number of calls, the number of errors, and the call name, with a total row at the bottom. This summary answers questions such as whether a program spends its time reading small chunks or waiting on the network.
strace -c -o summary.txt ls /workspaceSummary mode is also a fast way to compare two versions of a tool. If one version makes many more read calls, its buffering is probably smaller, and that often explains a slowdown.
Limits you should know
strace slows the traced program down, because every call stops the process twice. Timing numbers from a traced run are useful for relative comparisons, not for absolute speed. strace also sees only the calls a process makes. A program that asks a library for work, and the library asks the kernel, still shows only the kernel side, so the trace is accurate but can be harder to connect to the code.
Attaching to a running process with -p requires permission to trace that process. On many systems the kernel's Yama policy limits attaching to processes that are not your own children. The lab only traces commands it starts, which is the common case.
The lab has no man pages. Use strace --help for the option list, and read the strace and system call pages on man7.org for the details of each call.
Where this leads
The rest of this module uses the same idea. The /proc filesystem shows the state that the kernel keeps for each process, file descriptors show which files are open, and namespaces and cgroups show how the kernel isolates a process. Each of those sections can be checked with strace, which makes the kernel side of a command visible.
Reading one line carefully
A typical trace line for a file open looks like the sample below. It is output, not a command to type.
openat(AT_FDCWD, "/etc/passwd", O_RDONLY) = 3Read it from left to right. The name is the system call, and the arguments follow in parentheses. AT_FDCWD means a relative path is resolved from the current working directory. The quoted string is the path, and O_RDONLY is the flag set. The number after the equals sign is the return value, which here is the new file descriptor 3, because 0, 1, and 2 were already open. A return value of -1 followed by an error name means the call failed. Learning to read those four parts of a line is most of the skill, and the rest is knowing which calls matter for your problem.
When you only want to know whether a program read a file at all, search the trace for the path. The trace is plain text with one call on each line, so grep returns the call together with its result.
Questions strace answers well
Three questions come up again and again. Why did a command fail with a vague message? Look for the last call that returned an error, because the message usually reflects it. Which configuration file did a program read? Filter for openat and read the paths in order. Why is a program slow? Read the summary and see whether the time sits in a few calls, such as a long read or a blocking wait. Each question maps to one option, and remembering that mapping saves a lot of time.