strace
child process
process tracking
Linux
debugging

How to track child process using strace?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Understanding how processes interact with the operating system is crucial for debugging and performance tuning. The Linux utility strace is an invaluable tool for tracing system calls and signals in a process. A common requirement is to observe not just a single process but also its child processes. This article will guide you through how to track a child process using strace.

What is strace?

strace is a diagnostic, debugging, and instructional utility for Linux. It is used to monitor the system calls used by a program and the signals received by that program. It can print a trace of one or more processes as they execute, providing insights into how the program interacts with the operation system and its resources.

Tracking Child Processes

By default, strace monitors a single process. However, tracing a child process involves some additional configurations. The -f option is crucial for tracking child processes as it tells strace to trace forked child processes too.

Key Options

The table below summarizes the key options for strace when tracking child processes:

OptionDescription
-fTrace child processes as they are created via fork calls.
-o <filename>Redirect all output to the specified file for later analysis.
-p <pid>Attach to the process with the specified PID.
-ffWith -o, interprets each process's trace into separate files with the PID appended.

Example Commands

Here are some sample commands that utilize strace to track child processes:

Tracing a Command with Child Processes:

bash
strace -f -o trace_output.txt ls

In this command, -f traces child processes and -o trace_output.txt writes the output to a file named trace_output.txt.

Attaching to an Existing Process:

bash
strace -f -p 12345

Assuming 12345 is the PID of a running process, this will start tracing it along with any child processes.

Separate Files for Each Process:

bash
strace -ff -o trace_output ls

Here, -ff ensures that each process's trace gets written to a separate file, named according to its PID, like trace_output.12345.

Technical Explainers

System Calls

When a process requests a service from the kernel, such as reading from a file or sending data over a network, it makes a system call. strace logs every system call invoked by the process.

Signals

A signal is a limited form of inter-process communication used in Unix, Linux, and other POSIX-compliant operating systems. strace can intercept and block these signals.

Forking Process

When a process creates a child process using fork(), the child is a duplicate of the parent. If strace follows a process that calls fork(), with -f, it will continue to trace the child process.

Analyzing strace Output

The output from strace can be verbose. Here are some tips to analyze it:

  1. Identify System Calls: Understand which system calls are frequent and important.
  2. Look for Errors: Check for system calls that return errors, indicated by negative return values.
  3. Use Output Files: Redirecting output to a file allows for better searching, filtering, and archiving of the trace data.
  4. Separate by Process ID: If you used -ff, each file will pertain to a specific PID, allowing you to focus on the interaction of particular processes.

Advanced Topics

Filtering System Calls

strace allows filtering specific system calls using the -e option to reduce output volume. For instance, to trace only open and read calls:

bash
strace -f -e trace=open,read ls

Timing and Statistics

To include timestamps in the trace, the -t option can be used:

bash
strace -f -t ls

To summarize system call usage, including number and time spent:

bash
strace -f -c ls

Conclusion

The ability to trace child processes is one of the powerful features of strace, as it enables comprehensive monitoring of the behavior of complex applications composed of multiple processes. By leveraging options such as -f and -ff, you can capture the execution pattern of both parent and child processes, providing insights necessary for debugging and performance tuning. With practice and experience, strace becomes an invaluable part of your system administration toolkit.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.