$_ CLI In Depth
Learning progress 0%
lesson syscalls namespaces cgroups

Tracing System Calls with strace

6 min read Module 17: Syscalls, Namespaces & cgroups Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

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.

BASH1 lines
strace -o trace.txt ls /workspace

The 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.

BASH1 lines
strace -e trace=file ls /nope

This 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.

BASH1 lines
strace -c -o summary.txt ls /workspace

Summary 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.

Note

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.

TEXT1 lines
openat(AT_FDCWD, "/etc/passwd", O_RDONLY) = 3

Read 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.

What cat /etc/hostname asks the kernel

Visualizer

Every program is a list of system calls. strace prints each call with its arguments and return value, so you can read what a command really does, in order.

1 / 6
strace of cat /etc/hostname (trimmed)
execve("/usr/bin/cat", ["cat", "/etc/hostname"], 0x7ffd5c2b8f30 /* 9 vars */) = 0

This trace is trimmed to the calls that matter. execve() replaces the forked child’s program with cat. The quoted strings are argv. The trailing = 0 is execve() reporting success; strace prints it once the new program is loaded.

strace -e trace=execve,openat,read,write,close,exit_group cat /etc/hostname 2>&1 | tail -n 9
Terminal lab · 3 steps · real Linux in your browser

Watch system calls with strace

Use strace to record execve calls, count system calls, and catch a failed open.

01
Record the execve call
Run ls under strace and keep only the execve system call. Write strace output to a file with -o so the listing and the trace do not mix on the terminal: ``bash strace -o execve.txt -e trace=execve ls /workspace > /dev/null `` Open execve.txt and find the line that starts with execve(. The last line should say the process exited with 0.
Show hint for step 1
strace writes its report to the file named after -o. The filter -e trace=execve keeps the report short.
02
Count the system calls
The -c option replaces the per-call trace with a summary table, one row for each system call and a total row at the bottom: ``bash strace -c -o summary.txt ls /workspace > /dev/null `` Open summary.txt. Find the row for openat and the row labelled total.
Show hint for step 2
Use -c, not -e. The header of the table names the columns, and the last row is labelled total.
03
Catch a failed path lookup
Trace file-related calls for a path that does not exist. Send the trace to errors.txt and discard the error text that ls prints to the terminal: ``bash strace -o errors.txt -e trace=file ls /nope > /dev/null 2>&1 `` Search errors.txt for ENOENT. That is the error number the kernel returns when a path does not exist.
Show hint for step 3
Each failed call ends with = -1 followed by an error name. Search for the name ENOENT.
Check yourself Question 1 of 4
Concept Check Writing the trace to a file recall

Which strace option sends the trace to a file instead of the terminal?

Recap & Key Takeaways

A system call is the request a program makes to the kernel for any action on files, memory, processes, or the network.
strace prints each system call with its arguments and return value, and writes the trace to standard error unless you use -o.
-e trace= filters the calls you see, and -c replaces the trace with a summary table of counts and time.
A failed call returns -1 and an error name such as ENOENT, which usually explains a failure faster than the program's message.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue