During debugging, Strace can help you track down the system calls that a program performs. This is extremely handy when you want to know how the program interacts with the operating system, such as you want to know what system calls were executed and in what order.
This simple and powerful tool is available on almost all Linux operating systems and can be used to debug a large number of programs.
Command Usage
Let's see how the strace command tracks the execution of a program.
The simplest form of strace can be followed by any command. It will list many of the system calls. At first, we don't understand all the output, but if you're looking for something special, you should be able to find it from the output.
Let's take a look at the simple command LS's system call tracking situation.
Raghu@raghu-linoxide ~ $ strace ls
Stracing ls command
This is the first few lines of the strace command output. The other output was truncated.
Strace Write system call (LS)
The output section above shows the write system call, which outputs the list of current directories to the standard output.
The following picture shows the contents of the directory listed using the LS command (no strace is used).
Raghu@raghu-linoxide ~ $ ls
ls command output option 1 Looking for the configuration file read by the program
One of the uses of Strace (in addition to debugging some problems) is that you can find a configuration file that is read by a program. For example
Raghu@raghu-linoxide ~ $ strace php 2>&1 | grep php.ini
Strace config file read by program option 2 tracks specified system calls
The-e option for the Strace command is only used to show specific system calls (for example, open,write, etc.)
Let's keep track of the ' Open ' system Call of the Cat command.
Raghu@raghu-linoxide ~ $ strace-e Open Cat Dead.letter
Stracing Specific system call (open here) Option 3 track process
Strace can be used not only on commands, but also on running processes by using the-p option.
Raghu@raghu-linoxide ~ $ sudo strace-p 1846
Statistical Summary of the Strace a process Option 4 Strace
It includes a summary of system calls, execution times, errors, and so on. Use the-C option to display in a neat way:
Raghu@raghu-linoxide ~ $ strace-c ls
Strace Summary display option 5 save output
You can save the output of the Strace command to a file by using the-o option.
Raghu@raghu-linoxide ~ $ sudo strace-o process_strace-p 3229
Strace a process
The above command was run in sudo to prevent the user ID from matching the owner ID of the process being viewed. Option 6 Displays the time stamp
Using the-t option, you can add a timestamp before the output of each row.
Raghu@raghu-linoxide ~ $ strace-t ls
Timestamp before each output line option 7 Finer time stamp
The-TT option shows a microsecond-level timestamp.
Raghu@raghu-linoxide ~ $ strace-tt ls
Time-microseconds
-TTT can also display the microsecond timestamp as shown above, but it does not print the current time, but rather displays the number of seconds that have elapsed since epoch (January 1, 1970 00:00:00 UTC).
Raghu@raghu-linoxide ~ $ strace-ttt ls
Seconds since epoch option 8 relative time
The-r option shows the relative timestamp between system calls.
Raghu@raghu-linoxide ~ $ strace-r ls