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

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

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.

Install the strace tool with apt
sudo apt install strace
Trace only the open calls of the cat command
strace -e trace=openat cat /etc/hostname

Summary mode is useful too. With the -c option, strace counts each kind of call, and prints the totals when the program ends.

Count each kind of system call made by ls
strace -c ls /etc

To watch a program that is already running, give strace its process ID with the -p option. You may need root rights for that.

Attach to a running process by its process ID
strace -p 4321

Note: 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.

  1. Install strace with apt, if the tool is not already on the machine.

    sudo apt install strace

    What you should see: apt installs the package, and the prompt returns when it is done.

  2. Trace only the open calls made by cat while it reads the hostname file.

    strace -e trace=openat cat /etc/hostname

    What you should see: One line per call, each with its name, its arguments, and its result after an equals sign.

  3. Count each kind of call that ls makes on the /etc folder, and print a summary at the end.

    strace -c ls /etc

    What you should see: A summary table of the calls, with a count for each kind.

  4. 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 4321

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