strace笔记

strace 4.6版本用法输出

某嵌入式设备的strace版本及用法输出

strace -- version 4.6
usage: strace [-CdDffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]
              [-p pid] ... [-s strsize] [-u username] [-E var=val] ...
              [command [arg ...]]
   or: strace -c [-D] [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...
              [command [arg ...]]
-c -- count time, calls, and errors for each syscall and report summary
-C -- like -c but also print regular output while processes are running
-f -- follow forks, -ff -- with output into separate files
-F -- attempt to follow vforks, -h -- print help message
-i -- print instruction pointer at time of syscall
-q -- suppress messages about attaching, detaching, etc.
-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs
-T -- print time spent in each syscall, -V -- print version
-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args
-x -- print non-ascii strings in hex, -xx -- print all strings in hex
-a column -- alignment COLUMN for printing syscall results (default 40)
-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...
   options: trace, abbrev, verbose, raw, signal, read, or write
-o file -- send trace output to FILE instead of stderr
-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs
-p pid -- trace process with process id PID, may be repeated
-D -- run tracer process as a detached grandchild, not as parent
-s strsize -- limit length of print strings to STRSIZE chars (default 32)
-S sortby -- sort syscall counts by: time, calls, name, nothing (default time)

-u username -- run command as username handling setuid and/or setgid
-E var=val -- put var=val in the environment for command
-E var -- remove var from the environment for command

零碎的strace文档记录

strace尝试把参数以原结构显示,如果系统调用失败,可能显示不出来。解释了为什么参数有的是一个地址。结构是用大括号注释的,但是简单的指针和数组是用方括号和逗号分隔元素来打印的,bit-sets也使用方括号

unfinished,strace的一个系统调用未完成,另一个系统调用开始了,上一个完成以后会显示resumed

中断一个可以重启的系统调用,内核指定

字符指针被取消引用并打印为C字符串。字符串中的非打印字符通常由普通的C转义码表示。只打印字符串的第一个strsize(默认为32)字节;较长的字符串在右引号后附加一个省略号。

strace参数详解

argsdesc
-c统计每个系统调用的的时间,调用次数和错误次数,并报告总结
-C和-c类似,在进程运行时定时输出
-f跟踪fork
-ff输出到不同的文件
-F尝试跟踪vforks,-f是vforks不跟踪
-h输出帮助信息
-i输出系统调用时的指令指针
-q禁止输出attach和detach的信息
-r输出相对时间戳
-t输出绝对时间戳
-tt输出微秒
-T输出每个系统调用的时间开销
-V输出版本信息
-v输出未缩写的argv,stat,termio等,默认不输出,取消缩写
-x非ascii字符串hex输出
-xx所有字符串hex输出
-a设置返回值输出位置,默认40
-e指定表达式
-o输出到文件
-Oset overhead for tracing syscalls to OVERHEAD usecs
-p指定跟踪的进程id
-D将跟踪的进程作为分离的子进程,而不是父进程
-s设置打印的字符串长度,默认32
-S排序系统调用计数,通过时间,调用名,名字等,默认时间
-u以指定用户名的UID和GID运行命令
-E设置运行命令时的系统变量
--------
后面是5.5版本命令后面是5.5版本命令
-bexecve
-Drun tracer process as a grandchild, not as a parent
-DDrun tracer process in a separate process group
-DDDrun tracer process in a separate session
-k获取栈信息在每个系统调用之间
-qq屏蔽进程退出状态
-yprint paths associated with file descriptor arguments
-yyprint protocol specific information associated with socket file descriptors

-e选项详解

后面会有一个简单的输出例子
选项:trace, abbrev, verbose, raw, signal, read, write
5.5版本新添选项:fault, inject, status, kvm

过滤选项

signal, status

-e signal=SET
只跟踪指定的信号
-e status=SET
只跟踪指定返回值的调用
statuses: successful, failed, unfinished, unavailable, detached

trace

-e trace=[syscall name]
只跟踪指定的系统调用

输出选项

abbrev, verbose, raw

-e abbrev=SET verbose=SET raw=SET
输出展示格式:
abbrev:缩写输出
verbose:取消引用输出
raw:直接打印未编码的参数

read, write

-e read=SET write=SET
read,write,dump数据从文件描述符中读写的

kvm

-e kvm=vcpu, --kvm=vcpu
                 print exit reason of kvm vcpu

篡改选项

inject, fault

-e inject=SET[:error=ERRNO|:retval=VALUE][:signal=SIG][:syscall=SYSCALL]
            [:delay_enter=DELAY][:delay_exit=DELAY][:when=WHEN],
--inject=SET[:error=ERRNO|:retval=VALUE][:signal=SIG][:syscall=SYSCALL]
           [:delay_enter=DELAY][:delay_exit=DELAY][:when=WHEN]
                 perform syscall tampering for the syscalls in SET
     delay:      milliseconds or NUMBER{s|ms|us|ns}
     when:       FIRST, FIRST+, or FIRST+STEP
-e fault=SET[:error=ERRNO][:when=WHEN], --fault=SET[:error=ERRNO][:when=WHEN]
                 synonym for -e inject with default ERRNO set to ENOSYS.

常用参数

跟踪网络调用

strace -v -f -r -xx -s 202800 -e  trace=recv -e verbose=recv -p [pid]

-v取消缩写;
-f跟踪fork;
-r输出相对时间戳;
-T输出每个调用的时间开销;
-xx输出hex编码;
-s指定字符串大小限制;
-e指定只看recv系统调用,并且取消引用输出
效果

recv(5, "A", 102400, 0) = 1

如果想看地址,不取消引用,可以用:

strace -v -f -r -xx -s 202800 -e  trace=recv -e raw=recv -p [pid]

效果:

recv(0x5, 0xbe8d64fc, 0x19000, 0)       = 0x27d8

问题和思考

  1. 遇到输出选项是verbose,但是recv系统调用参数显示成地址,没有取消引用?
  • 可能是返回值位置的原因??
  • 在树莓派测试,也是arm,strace5.5,recv可以正常取消引用超过10000长度的数据。
  • 编译一个用recv的程序传上去试一试。
  • 全部保持引用,看看那个地址有没有被别的调用用到: strace -f -r -xx -s 202800 -e raw=all -p [pid]

一些还不错的参考资料

strace命令介绍与用法

strace源码分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏打呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值