Linux strace command
What is strace
strace is a powerful diagnostic and debugging tool on Linux used to trace system calls and signals received during a program's execution. System calls are the interface between an application and the operating system kernel; with strace we can gain deep insight into a program's low-level behavior.
An Analogy: you can think of strace as a "translator" that turns the "conversation" (system calls) between a program and the operating system into human language we can understand.
Why you need strace
strace is especially useful in the following scenarios:
- Debug abnormal program behavior
- Analyze program performance bottlenecks
- Understand how a program interacts with the operating system
- Diagnose permission-related issues
- Troubleshoot file/network access issues
Basic Syntax
strace [选项] 命令 [命令参数]
Or attach to a running process:
strace -p PID
Commonly used options
| Option | Description |
|---|---|
-c |
Count the number and timing of system calls |
-f |
Trace child processes |
-e trace=系统调用 |
Trace only specific system calls |
-o 文件 |
Write the output to a file |
-p PID |
Attach to a running process |
-t |
Show timestamps |
-T |
Show the time spent in system calls |
-s 大小 |
Set the maximum display length of strings (default 32) |
-v |
Display more detailed information |
Usage examples
Example 1: Basic tracing
Trace the ls command's execution:
strace ls
The output shows ls all system calls during the command's execution, such as opening directories and reading file information.
Example 2: Count system calls
Statistics ls -l system call statistics:
strace -c ls -l
Example output:
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
45.21 0.000123 5 25 12 openat
32.35 0.000088 4 21 mmap
12.50 0.000034 3 11 read
...
Example 3: Trace specific system calls
Trace only file-related system calls:
strace -e trace=open,read,write ls
Example 4: Trace network connections
Trace the curl network-related system calls:
strace -e trace=network curl http://example.com
Example 5: Attach to a running process
First find the process ID:
ps aux | grep 进程名
Then attach to trace it:
strace -p 进程ID
Interpreting the Output
A typical line of strace output is as follows:
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
The meaning of each part:
openat: System call namesAT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC: call arguments= 3: return value (here, a file descriptor)
Advanced techniques
1. Filter output with grep
strace ls 2>&1 | grep open
2. Trace a process and all its child processes
strace -f 命令
3. Show the time spent in system calls
strace -T 命令
4. Save output to a file
strace -o trace.log 命令
Troubleshooting common problems
1. File-not-found errors
Look for ENOENT Errors:
open("/nonexistent/file", O_RDONLY) = -1 ENOENT (No such file or directory)
2. Permission issues
Find EPERM or EACCES Errors:
open("/root/file", O_RDONLY) = -1 EACCES (Permission denied)
3. Performance bottlenecks
Use -c option to tally the most time-consuming system calls.
Notes
- strace significantly slows down program execution and is not suitable for long-term use in production environments
- Some system calls may not be traceable due to security restrictions
- The output can be very verbose; it is recommended to combine it with
-eoption or redirect it to a file - A certain level of systems programming knowledge is needed to fully understand the output
With the powerful strace tool, you can gain a deep understanding of how Linux programs run and quickly locate various system-level problems.
Linux Command Reference