Watching system calls with strace
Video 030 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 4:06, with English and Hindi audio.
A program is slow, or it fails with a vague message. Which system calls did it make, and what did the kernel answer? One tool shows exactly that. It is called strace, and it watches a program's system calls and prints each one as it happens. You do not need to change the program. This article explains how strace sees the calls, how to read one line of its output, and how to trace a command on Ubuntu 24.04.
What you will learn
- strace prints each system call a program makes, with its arguments and its result.
- Read each line as the name, then the arguments, then the result after the equals sign.
- A result of minus one, with an error name such as ENOENT, means the call failed.
- Tracing slows the program, so use it briefly, and handle its output with care.
How strace sees each system call
The kernel offers a facility called ptrace. It lets one process control another. strace uses it to pause the program at each system call. At every call, strace reads the name, the arguments and the result, and prints one line for it.
Note: That pausing has a cost. Tracing can slow a program down by a lot. Use it briefly, and never leave it running on a busy production service.
How to read one line of strace output
- The name: the kind of request, such as openat, which opens a file, read, write or connect.
- The arguments: what the call asks for, such as a file name or a size.
- The result: after the equals sign. It is a number on success, or minus one with an error name, such as ENOENT, when the call fails.
ENOENT means there is no such file or directory. It is the clue to look for when a program fails at start with a vague message.
How to trace a command on Ubuntu 24.04
On Ubuntu 24.04, first install strace with apt, if it is not already present. Then trace one simple command: cat, which reads the hostname file. The option after strace limits the output to the open calls.
sudo apt install stracestrace -e trace=openat cat /etc/hostnameSummary mode is useful too. With the -c option, strace counts each kind of call, and prints the totals when the program ends.
strace -c ls /etcTo watch a program that is already running, give strace its process ID with the -p option. You may need root rights for that.
strace -p 4321Note: 4321 is only an example process ID. Replace it with the ID of the process you want to watch.
A case where the trace found the fault in minutes
Consider a warehouse app that fails at start with only one vague line: configuration error. Nothing more is written to the log. Run it under strace, and the trace shows the app opening a settings file. That open call returns ENOENT, so the file is not there.
The path in the app's settings was wrong. The team corrected it, and the app started. strace is best for a program that hangs, because a stuck program often sits inside one call, such as a network read that never returns.
Note: Trace output can reveal the data a program reads or writes. Store trace files safely, and delete them when the job is done.
Trace a command with strace
You need Ubuntu 24.04 with sudo rights and a terminal. Run the four commands in this order. The last one needs a running process, so use the ID of one you own.
Install strace with apt, if the tool is not already on the machine.
sudo apt install straceWhat you should see: apt installs the package, and the prompt returns when it is done.
Trace only the open calls made by cat while it reads the hostname file.
strace -e trace=openat cat /etc/hostnameWhat you should see: One line per call, each with its name, its arguments, and its result after an equals sign.
Count each kind of call that ls makes on the /etc folder, and print a summary at the end.
strace -c ls /etcWhat you should see: A summary table of the calls, with a count for each kind.
Attach strace to a process that is already running. Replace 4321 with the process ID of a process you own, and use root rights if the system asks for them.
strace -p 4321What you should see: Lines for each system call the process makes, until you stop strace.
Terms
- strace
- A tool that prints each system call a program makes, with its arguments and its result.
- ptrace
- A kernel facility that lets one process control another, which strace uses to pause a program at each call.
- ENOENT
- An error result that means there is no such file or directory.
- Process ID
- The number that Linux gives each running program, used to refer to it.
Check yourself
How do you read one line of strace output?
Read the name of the call first, then its arguments, then the result after the equals sign.
What does minus one with ENOENT mean in a trace?
The call failed because the file or directory it asked for does not exist.
Why should you use strace only briefly on a busy service?
Tracing pauses the program at every call, so it slows the program and can make a busy service time out.
Previous: What is a system call?. Next: Interrupts: how devices get the CPU's attention.