0
点赞
收藏
分享

微信扫一扫

strace 使用说明

雷亚荣 05-14 12:00 阅读 16

strace 说明

strace 是 Linux 系统调用跟踪工具,用于监控进程与内核的交互

strace 安装

yum -y install strace

strace 基础用法

常用选项(General)

参数

说明

-e EXPR

指定跟踪的 系统调用/信号/事件(见下方 Filtering

-o FILE

将跟踪结果输出到文件(默认输出到 stderr)。

-p PID

附加到正在运行的进程(需 PID)。

-f

跟踪子进程(如 fork、exec 产生的子进程)。

-ff

跟踪子进程,并将输出保存到 独立文件(文件名格式:FILE.PID)。

-c

统计系统调用耗时和次数(摘要模式)。

进程启动选项(Startup)

参数

说明

-E VAR=VAL

设置环境变量(如 -E PATH=/bin),用于启动新进程时覆盖环境变量。

-u USERNAME

以指定用户身份运行命令(处理 setuid/setgid)。

-D

以守护进程模式运行(分离跟踪进程与目标进程)。

过滤选项(Filtering)

参数

说明

-e trace=SYSCALL

仅跟踪特定系统调用(支持通配符或正则表达式)。

示例:

-e trace=open,read:仅跟踪 open 和 read

-e trace=file:跟踪文件相关系统调用组。

-e trace=!SYSCALL

排除特定系统调用(反向过滤)。

示例:-e trace=!write:排除所有 write 调用。

-e signal=SET

仅跟踪指定信号

示例:-e signal=SIGSEGV,SIGKILL:仅跟踪段错误和强制终止信号。

-z

仅显示成功的系统调用(返回值非负)。

-Z

仅显示失败的系统调用(返回错误码)。

输出格式选项(Output Format)

参数

说明

-s STRSIZE

限制打印字符串的最大长度(默认 32 字符)。

示例:-s 100 显示完整长路径。

-t / -tt / -ttt

时间戳格式

-t:绝对时间(秒);-tt:微秒精度;-ttt:UNIX 时间戳。

-T

显示系统调用耗时(微秒)。

-v

详细模式:显示完整的结构体参数(如 stat 的详细信息)。

-y / -yy

显示文件描述符路径

-y:显示路径;-yy:显示更多信息(如 socket 类型)。

-xx

以十六进制显示所有字符串

统计与摘要(Statistics)

参数

说明

-c

生成摘要报告:统计各系统调用的次数、耗时和错误。

示例:strace -c ls

-C

生成摘要并保留原始输出。

-S SORTBY

按指定字段排序摘要(如 -S calls 按调用次数排序)。

高级功能(Tampering)

参数

说明

-e inject=...

篡改系统调用行为(模拟错误或延迟)。

示例:

-e inject=open:error=ENOENT:使 open 返回 "文件不存在" 错误。

-e inject=read:delay_exit=1sread 调用延迟 1 秒返回。

常用场景示例

跟踪进程的系统调用

strace -p 1234                 # 跟踪 PID=1234 的进程
strace -f -p 1234              # 跟踪 PID=1234 及其子进程
strace -e trace=open,read ls   # 仅跟踪 `ls` 命令的 open 和 read 调用

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\177\0\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000#\0\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\\\2\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\"\0\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\21\0\0\0\0\0\0"..., 832) = 832
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@|\0\0\0\0\0\0"..., 832) = 832
read(3, "nodev\tsysfs\nnodev\trootfs\nnodev\tr"..., 1024) = 332
read(3, "", 1024)                       = 0
+++ exited with 0 +++

生成耗时统计

strace -c -p 1234              # 统计 PID=1234 的系统调用耗时
strace -c ls                   # 统计 `ls` 命令的系统调用摘要

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 33.13    0.001648          51        32           mmap
 11.16    0.000555          46        12           close
 10.64    0.000529          52        10           openat
  8.64    0.000430          39        11           mprotect
  8.50    0.000423          42        10           fstat
  6.76    0.000336          42         8           read
  4.22    0.000210          35         6         3 prctl
  3.00    0.000149         149         1           execve
  2.11    0.000105          35         3           brk
  1.55    0.000077          38         2           getdents64
  1.51    0.000075          37         2         1 access
  1.45    0.000072          36         2           ioctl
  1.35    0.000067          67         1           arch_prctl
  1.17    0.000058          29         2         2 statfs
  0.94    0.000047          23         2           rt_sigaction
  0.92    0.000046          46         1           munmap
  0.84    0.000042          42         1           set_robust_list
  0.72    0.000036          36         1           prlimit64
  0.70    0.000035          35         1           rt_sigprocmask
  0.68    0.000034          34         1           set_tid_address
------ ----------- ----------- --------- --------- ----------------
100.00    0.004974          45       109         6 total

% time  该系统调用占总时间的百分比(越高表示对性能影响越大)。
seconds 该系统调用的总耗时(秒)。
usecs/call  每次调用的平均耗时(微秒)。
calls 该系统调用的总次数。
errors  该系统调用返回错误码的次数(需重点关注)。
syscall 系统调用名称。

调试文件访问

strace -e trace=file -y ls      # 跟踪文件相关调用并显示路径
strace -P /etc/passwd cat /etc/passwd  # 跟踪对 /etc/passwd 的访问

openat(AT_FDCWD, "/etc/passwd", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1364, ...}) = 0
fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
read(3, "root:x:0:0:root:/root:/bin/bash\n"..., 131072) = 1364
root:x:0:0:root:/root:/bin/bash
....
wgs:x:1000:1000::/home/wgs:/bin/bash
read(3, "", 131072)                     = 0
close(3)                                = 0
+++ exited with 0 +++

分析网络连接

strace -e trace=network -yy curl www.baidu.com  # 跟踪网络相关调用并显示 socket 详情

# 创建套接字
socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3<UDPv6:[44848]>
socketpair(AF_UNIX, SOCK_STREAM, 0, [3<UNIX:[42876->42877]>, 4<UNIX:[42877->42876]>]) = 0
socketpair(AF_UNIX, SOCK_STREAM, 0, [5<UNIX:[44851->44852]>, 6<UNIX:[44852->44851]>]) = 0
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 5<TCP:[45543]>
# 设置 TCP 选项
setsockopt(5<TCP:[45543]>, SOL_TCP, TCP_NODELAY, [1], 4) = 0
setsockopt(5<TCP:[45543]>, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
setsockopt(5<TCP:[45543]>, SOL_TCP, TCP_KEEPIDLE, [60], 4) = 0
setsockopt(5<TCP:[45543]>, SOL_TCP, TCP_KEEPINTVL, [60], 4) = 0
# 非阻塞连接与状态确认
connect(5<TCP:[45543]>, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("110.242.69.21")}, 16) = -1 EINPROGRESS (Operation now in progress)
getsockopt(5<TCP:[192.168.174.144:52818->110.242.69.21:80]>, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
# 获取连接信息
getpeername(5<TCP:[192.168.174.144:52818->110.242.69.21:80]>, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("110.242.69.21")}, [128->16]) = 0
getsockname(5<TCP:[192.168.174.144:52818->110.242.69.21:80]>, {sa_family=AF_INET, sin_port=htons(52818), sin_addr=inet_addr("192.168.174.144")}, [128->16]) = 0
# 发送 HTTP 请求
sendto(5<TCP:[192.168.174.144:52818->110.242.69.21:80]>, "GET / HTTP/1.1\r\nHost: www.baidu."..., 77, MSG_NOSIGNAL, NULL, 0) = 77
# 接收 HTTP 响应
recvfrom(5<TCP:[192.168.174.144:52818->110.242.69.21:80]>, "HTTP/1.1 200 OK\r\nAccept-Ranges: "..., 102400, 0, NULL, NULL) = 2781
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
+++ exited with 0 +++

模拟故障注入

strace -e inject=write:error=EPIPE ./my_program  # 使所有 write 调用返回管道错误



举报

相关推荐

0 条评论