Linux命令-strace(跟踪系统调用和信号)) Linux命令-strace跟踪系统调用和信号快速参考基本用法常用选项过滤系统调用输出格式与时间统计模式性能分析实用调试场景高级用法strace vs ltrace安全注意事项strace是 Linux 下最强大的诊断工具之一用于跟踪进程发出的系统调用syscall和接收的信号。它拦截并记录程序与内核的每一次交互是调试程序行为、分析性能瓶颈、排查权限问题以及逆向工程的神器。快速参考strace[选项]命令[参数...]strace-pPIDstrace使用ptrace()系统调用附加到目标进程。注意strace会显著降低被跟踪进程的性能不建议在生产环境中长时间使用。基本用法# 跟踪一个命令的执行stracels# 跟踪并保存输出到文件strace-otrace.logls# 跟踪已有进程需要权限strace-p12345# 跟踪已有进程多个 PIDstrace-p12345-p12346# 将输出追加到文件strace-otrace.log-Als常用选项选项说明-p PID附加到运行中的进程-f跟踪子进程fork/clone-ff每个子进程写入单独日志文件-e expr过滤特定系统调用-o FILE输出到文件而非 stderr-tt微秒级时间戳-T显示每个系统调用耗时-c统计模式汇总报告-s SIZE定义输出字符串的最大长度-v详细模式显示结构体全部字段-x以十六进制显示非 ASCII 字符串-y显示文件描述符关联的路径-k显示系统调用的内核栈-E VAR从环境变量中移除指定变量过滤系统调用# 仅跟踪 open 系统调用strace-etraceopenls# 跟踪多个系统调用strace-etraceopen,read,write,closels# 仅跟踪网络相关调用strace-etracenetworkcurlhttps://example.com# 跟踪文件操作strace-etracefilecat/etc/hosts# 跟踪进程管理相关strace-etraceprocessbash-cecho $$# 跟踪信号strace-etracesignalsleep5# 跟踪内存相关strace-etracememoryfree# 排除某些调用仅看关注的strace-etrace\!futex,clock_gettimels# 排除 futex 和时钟# 跟踪特定文件描述符strace-eread3curlhttps://example.com# 跟踪以特定路径为参数的系统调用strace-P/etc/passwdcat/etc/passwd# 跟踪网络调用的详细信息strace-etracenetwork-vcurl-shttps://example.com/dev/null输出格式与时间# 相对时间戳从跟踪开始strace-rls# 系统调用耗时strace-Tls# 微秒级绝对时间strace-ttls# 时钟时间Wall clockstrace-wls# 组合使用strace-ttT-odetailed.logls# 增加字符串输出长度默认 32strace-s256catlong_text_file.txt# 以十六进制显示strace-x-s64read_binary统计模式性能分析# 统计系统调用次数和耗时strace-cls# 输出示例# % time seconds usecs/call calls errors syscall# ------ ----------- ----------- --------- --------- ----------------# 99.50 0.000423 21 20 read# 0.50 0.000002 0 6 mmap# ...# 统计并排序按时间strace-c-Stimefind/tmp-name*.log# 统计并排序按调用次数strace-c-Scallsfind/tmp# 统计特定系统调用strace-c-etracefilefind/tmp-typef# 统计网络调用strace-c-etracenetworkwgethttps://example.com实用调试场景# 场景 1: 程序找不到配置文件strace-etraceopenat nginx-t21|grepnginx.conf# 查看程序尝试打开哪些配置文件路径# 场景 2: 权限问题排查strace-etracefile program21|grepEACCES# 查找 Permission Denied 的调用# 场景 3: 子进程跟踪strace-f-etraceprocessmake# 场景 4: 跟踪服务启动sudostrace-f-oservice_trace.log systemctl start myservice# 场景 5: 查找程序读取的配置文件strace-etraceopenat,statvim21|grep\.vimrc# 场景 6: 查看 DNS 解析过程strace-etracenetworkping-c1example.com21# 场景 7: 查看程序写入的日志文件strace-etracewrite myapp21|grep-E^write# 场景 8: 跟踪特定用户的进程sudostrace-uusername-p$(pgrep-uusername process_name)高级用法# 中断系统调用时显示详细信号信息strace-etraceread-esignalSIGINTcat# 读取字符数组非字符串的内容strace-etraceread-ereadfd,countcat/dev/urandom# 注入错误模拟测试错误处理# strace -e inject系统调用:error错误号strace-einjectopenat:errorENOENTls/tmp# 模拟 openat 返回 No such file 错误# 注入延迟strace-einjectread:delay_enter100000ls# 每次 read 调用前延迟 100ms# 跟踪多线程程序strace-f-omysql_trace.log mysqld# 同时显示调用者信息strace-kls/tmp# 只跟踪失败的调用strace-etraceopenat-Zls/nonexistent# -Z 只显示返回错误码的调用# 缩写输出减少噪音strace-etraceread,write-eabbrevallddif/dev/zeroof/dev/nullbs1count10strace vs ltrace特性straceltrace跟踪层级系统调用库函数典型输出open(/etc/hosts, O_RDONLY)fopen(/etc/hosts, r)性能开销中等到高高适用场景内核交互、I/O 分析库调用逻辑、malloc 分析静态链接完全可见不可见无动态链接# strace: 看到的是系统调用级别stracels21|head-5# execve(/usr/bin/ls, ...)# openat(AT_FDCWD, /etc/ld.so.cache, ...)# mmap(NULL, ...)# ltrace: 看到的是库函数级别ltracels21|head-5# __libc_start_main(...)# setlocale(...)# opendir(...)安全注意事项警告strace使用ptrace()需要在/etc/sysctl.conf中设置kernel.yama.ptrace_scope0才能跟踪非子进程被跟踪的程序可能会在输出中包含敏感信息密码、密钥等输出文件需妥善保管在生产环境使用strace会产生显著的性能影响应在非高峰时段短时间使用对多线程高并发进程如数据库strace可能严重影响响应延迟# 检查 ptrace 限制sysctlkernel.yama.ptrace_scope# 0: 无限制 1: 受限默认 2: 仅 root 可以CAP_SYS_PTRACE# 3: 完全禁用# 临时放宽限制sudosysctl-wkernel.yama.ptrace_scope0strace-p12345# 完成后恢复sudosysctl-wkernel.yama.ptrace_scope1# 使用 strace 诊断死锁strace-f-etracefutex-odeadlock.log ./hanging_app# 最小化性能开销的跟踪方式strace-etraceopen,openat,read,write,close\-ominimal.log-T\./performance_sensitive_appstrace是 Linux 问题诊断的X 光机——当你对问题一无所知时让strace告诉你程序在内核层面究竟做了什么。但请谨记它是一把重型诊断工具应用在生产环境时需要格外谨慎。