Linux command
strace 命令
安全
权限或系统影响较大,执行前请核对目标。
常用示例
Attach
sudo strace -p 1234
Example
sudo strace -p 1234 -e read,write
Example
sudo strace -p 1234 -c
Example
sudo strace -p 1234 -T -s 32
Trace
strace ./program
Example
strace -e trace=file ./program
Example
strace -f -e trace=network -o trace.txt ./program
说明
strace intercepts and records system calls made by a process and the signals received by it. It is invaluable for debugging, diagnostics, and understanding system behavior without requiring source code access.
参数
- -p, --attach pid
- Attach to running process with given PID
- -f, --follow-forks
- Trace child processes created by fork/vfork/clone
- -e trace=set
- Trace only specified system calls (file, network, process, etc.)
- -e signal=set
- Trace only specified signals
- -o, --output file
- Write trace output to file
- -c, --summary-only
- Display summary statistics at exit
- -C, --summary
- Display summary along with normal output
- -t, -tt, -ttt
- Add timestamps (-t: HH:MM:SS, -tt: with microseconds, -ttt: epoch seconds)
- -T, --syscall-times
- Show time spent in each system call
- -s, --string-limit size
- Maximum string size to print (default 32)
- -v, --no-abbrev
- Display unabbreviated output
- -y, --decode-fds
- Print file paths associated with file descriptors
- -z, --successful-only
- Show only successful system calls
- -Z, --failed-only
- Show only failed system calls
- -P, --trace-path path
- Trace only syscalls accessing given path
- -k, --stack-traces
- Print stack trace for each system call
- -u, --user username
- Run command as specified user
FAQ
What is the strace command used for?
strace intercepts and records system calls made by a process and the signals received by it. It is invaluable for debugging, diagnostics, and understanding system behavior without requiring source code access.
How do I run a basic strace example?
Run `sudo strace -p 1234` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -p, --attach pid do in strace?
Attach to running process with given PID