The Linux strace command: tracking system calls

Introduction

In the world of system management and software development, understanding how programs interact with kernel is essential for diagnosing performance problems, debugging errors and optimizing resources. One of the most powerful and versatile tools for this purpose in Linux is strace. This command allows you to intercept and record the calls to the system that a process makes, providing a detailed view of your internal behavior without changing the source code.

What's strace?

strace is a command line utility that runs against another process and uses the kernel ptrace functionality to capture each call to the system (syscall) that the process invokes, as well as the signals it receives. Each event is displayed in the standard output or can be redirected to a file for further analysis. In addition to the name of the call, strace shows the arguments with which it was invoked and the return value, which makes it easier to identify the operation that is failing or consuming time.

How to install strace

In most Linux distributions, strace is already pre-installed or available in official repositories. On Debian-based or Ubuntu-based systems can be installed with the apt package manager running: sudo apt update & & sudo apt install trace. In Red Hat family distributions such as CentOS or Fedora, the command is: sudo dnf install strace or sudo yum install strace according to the version. In Arch Linux is installed with: sudo pacman -S strace. After installation, simply typing strace in the terminal will show the basic help.

Basic use

The simplest way to use stress is to prefix it to the command you want to draw. For example, to see all calls to the system of a program called my _ program runs: strace. / mi _ program. The output will appear in the terminal, showing each syscall in real time. If the process ends quickly, it may be useful to redirect the output to a file to review it calmly: strace -or registro.txt. / mi _ program. It is also possible to attach strace to a process already in operation using its process ID (PID) with the option -p: strace -p 1234.

Most useful options

  • -c: counts the time spent and the number of each type of call to the system, providing a statistical summary at the end.
  • -f: follow the child processes created by fork, vfork or clone, ensuring that no subprocess calls are lost.
  • -e expr: filters the calls to show; for example, -e trace = open, read, write will show only those three syscalls.
  • -s N: specifies the maximum chain size to be printed for arguments that are buffers, useful to avoid excessively long outputs.
  • -t: adds a time mark to each output line, facilitating the correlation of events.
  • -T: shows the time each call to the system took, ideal for detecting performance bottle necks.
  • -or file: redirect all trace to the indicated file instead of the terminal.

Practical examples

To observe only file openings and readings, you can use the filter option -e: strace -e trace = open, read. / mi _ program. This will reduce output to relevant syscalls and facilitate the detection of problems such as files that are not found or insufficient permits.

If you want to get a summary of time consumption per call type, the -c option is very useful: strace -c. / mi _ program. At the end of the run a table will be displayed with the number of calls, the total time spent and the average per call for each syscall.

If the process is already in operation and you want to inspect your behavior without reinitiating it, just get your PID and apply strace -p: strace -p 5678 -or trace.log. After a few seconds you can interrupt the trace with Ctrl + C and analyze the trace.log file to see what syscalls are occurring at that time.

To purify a program that fails silently, you can combine the -f option to follow child processes and -or to save the output: strace -f -or hijo.txt. / program _ that _ launches _ children. Then the file is reviewed for calls that return -1, which indicates an error, and the error value is examined to understand the cause.

Limitations and considerations

Although strace is extremely useful, it has some limitations that it is important to consider. Each call to the intercepted system adds a context overload due to the use of ptrace, which can significantly slow down the application under trace, especially in programs that make thousands of syscalls per second. In addition, certain processes with high privileges or performing setuid / setgid binaries may not be traceable without root permits, as the kernel restricts access to ptrace for security reasons. Finally, the presence of strace can alter the execution times and hide competition problems that only manifest in real load conditions, and it is recommended to use it in test or purification environments rather than in critical production.

Conclusion

In short, strace is an essential tool for any system manager or developer working in Linux. Its ability to reveal exactly how a program interacts with the kernel makes it a powerful ally to debug, optimize performance and understand complex software behavior. With a little practice and knowledge of your most common options, you can get precise diagnoses and speed up problem resolution without changing the source code or reinstalling packages.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish