Skip to content

strace command in Linux

The strace command in Linux is a powerful utility used to trace system calls and signals made by a program. By monitoring interactions between processes and the kernel, strace helps diagnose and troubleshoot issues efficiently. This tool can be invaluable for understanding the behavior of programs, identifying performance bottlenecks, and debugging complex problems. By providing detailed insight into how programs interact with the operating system, strace is a key asset for system administrators and developers alike.

Terminal window
strace [options] command [args]
OptionDescription
-cPrint a summary of the counts of each system call and exit
-fTrace child processes as they are created by currently traced processes
-o filenameWrite the trace output to the specified filename
-p pidAttach to the process with the specified process ID and begin tracing
-s sizeSpecify the maximum string size to print (default is 32)
-tPrefix each line of the trace with the time of day
-TPrefix each line with the time spent in each system call
-e trace=setPerform a filtered trace based on the specified set of system calls
-yPrint paths associated with file descriptor arguments
-E var=valueSet the value of an environment variable for the traced process
-hDisplay help message and exit
ParameterDescription
commandThe command to be traced
argsOptional arguments to be passed to the command
Terminal window
strace -p <PID>

This command traces the system calls made by the process with the specified PID.

Terminal window
strace ls

Executes the “ls” command while tracing the system calls it makes.

Terminal window
strace -f command

Traces the specified command and any child processes it spawns.

Terminal window
strace -o output.txt command

Redirects the strace output of the command to the specified file.

Terminal window
strace -T command

Shows the time taken by each system call made by the specified command.

Terminal window
strace -e trace=file command

Traces only the system calls related to file operations made by the specified command.

Terminal window
strace -e trace=open,read command

Traces only the “open” and “read” system calls made by the specified command.

Terminal window
strace -e open command

Filters the strace output to display only the system call “open” made by the specified command.

To use the strace command in Linux, execute the following command:

Terminal window
strace ls

What is the purpose of the strace command in Linux?

Section titled “What is the purpose of the strace command in Linux?”

The strace command in Linux is used to trace and display the system calls and signals made by a process. It is helpful for troubleshooting and understanding the behavior of programs.

How can I trace a specific process with strace in bash?

Section titled “How can I trace a specific process with strace in bash?”

To trace a specific process with strace in bash, use the following command:

Terminal window
strace -p <PID>

How do I save the strace output to a file in Linux?

Section titled “How do I save the strace output to a file in Linux?”

To save the strace output to a file in Linux, run the command with output redirection, like this:

Terminal window
strace ls > output.txt

Can strace be used to trace multiple processes simultaneously in Linux?

Section titled “Can strace be used to trace multiple processes simultaneously in Linux?”

Yes, strace can be used to trace multiple processes simultaneously in Linux by specifying multiple process IDs (PIDs) separated by commas.

Terminal window
strace -p <PID1>,<PID2>

How can I show timestamps with strace output in Linux?

Section titled “How can I show timestamps with strace output in Linux?”

To show timestamps with strace output in Linux, use the -t option in the command:

Terminal window
strace -t ls

Is it possible to follow forks with strace in bash?

Section titled “Is it possible to follow forks with strace in bash?”

Yes, it is possible to follow forks with strace in bash using the -f option in the command. This option traces child processes as they are created.

Terminal window
strace -f ls

How can I display both system calls and signals with strace in Linux?

Section titled “How can I display both system calls and signals with strace in Linux?”

To display both system calls and signals with strace in Linux, use the -v option in the command:

Terminal window
strace -v ls
  • Debugging system calls and signals
  • Tracing and analyzing the behavior of programs
  • Troubleshooting and identifying issues with applications
  • Monitoring system calls and function calls
  • Analyzing the performance of programs
  • Studying the interactions between processes and the kernel
  • Understanding the flow of data between processes and the system