Translation: Lctt https://linux.cn/article-3935-1.html Translator: Guodongxiaren
During debugging, Strace can help you trace the system calls that a program executes. This is extremely handy when you want to know how the program interacts with the operating system, for example, you want to know what system calls are being 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.
In the simplest form, strace can be followed by any command. It will list dozens of 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 system call tracking situation.
- [Email protected] -linoxide ~ $ strace ls
Stracing ls command
This is the first few lines of the strace command output. The other outputs are truncated.
Strace Write system call (LS)
The output section above shows the write system call, which outputs the list of current directories to standard output.
The following picture shows the contents of the directory listed using the LS command (no strace is used).
- [Email protected] -linoxide ~ $ ls
ls command output
Option 1 Find 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
- [Email protected] -linoxide ~ $ strace php 2>&1 | grep php. INI
Strace config file read by program
Option 2 tracks the specified system call
The-e option of the Strace command is only used to show specific system calls (for example, open,write, etc.)
Let's trace the ' open ' system call to the Cat command.
- [Email protected] -linoxide ~ $ strace -e open cat dead. Letter
Stracing Specific system call (open here)
Option 3 Trace Process
Strace can be used not only on commands, but also on running processes by using the-p option.
- [Email protected] -linoxide ~ $ sudo strace -p 1846
Strace a process
Option 4 Statistical Summary of Strace
It includes a summary of system calls, execution time, errors, and so on. Use the-C option to display in a neat way:
- [Email protected] -linoxide ~ $ strace -c ls
Strace Summary Display
Option 5 Saving output results
You can save the output of the Strace command to a file by using the-o option.
- [Email protected] -linoxide ~ $ sudo strace -o process_strace -p 3229
Strace a process
The above command is run with Sudo to prevent a situation where the user ID does not match the owner ID of the process being viewed.
Option 6 shows timestamps
With the-t option, you can add a timestamp before the output of each row.
- [Email protected] -linoxide ~ $ strace -t ls
Timestamp before each output line
Option 7 Finer Timestamp
The-TT option can display a microsecond-level timestamp.
- [Email protected] -linoxide ~ $ strace -tt ls
Time-microseconds
-TTT can also show microsecond timestamps to the above, but instead of printing the current time, it shows the number of seconds that have elapsed since the epoch (January 1, 1970, 00:00:00 UTC).
- [Email protected] -linoxide ~ $ strace -ttt ls
Seconds since epoch
Option 8 Relative time
The-r option shows the relative timestamp between system calls.
- [Email protected] -linoxide ~ $ strace -r ls
Relative Timestamp
via:http://linoxide.com/linux-command/linux-strace-command-examples/
Raghu Translator: Guodongxiaren proofreading: Wxy
This article by LCTT original translation, Linux China honors launch
Source: https://linux.cn/article-3935-1.html
Common options for using the Linux strace command to track/debug programs