Linux strace

strace 示例

1.跟踪nginx, 看其启动时都访问了哪些文件
strace -tt -T -f -e trace=file -o /data/log/strace.log -s 1024 2>&1 ./nginx
2. 定位程序异常退出
strace -ttf -T -p 10893 -o tmp -e trace=process 2>&1
3.程序启动加载文件
strace -e open,acces ./sh 2>&1 | grep fileName
4. 查选程序耗时
strace -c -p 11084
5.链接服务器失败
strace -e poll,select,connect,recvfrom,sendto nc www.baidu.com 80

注:pstree -p pid 可以查看多线程程序的进程树。来跟踪具体子进程

使用cheat可以看到strace通常使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
xiaobaoqiu@xiaobaoqiu:~$ cheat strace
# 基本使用
strace <command>

# 将trace结构写到文件,使用-o参数
strace -o strace.out <other switches> <command>

# 只trace open()这个系统调用
strace -e trace=open <command>

# trace所有会打开文件的系统调用
strace -e trace=file <command>

# trace所有和进程管理相关的系统调用,再查看一个进程的fork,wait和exec等步骤的时候很有用
strace -e trace=process <command>

# 当前进程fork出来的子进程也trace
strace -f <command>

# 每个系统调用计数,包括:调用时间,调用次数,错误次数
strace -c <command>

# trace某一个进程(可以指定多个pid)
strace -p <pid>

通常而言,我们使用-c选项找出最耗时的系统调用,再使用-e选项跟踪这个操作

-c 选项使用完成后退出可看结果。

https://www.tecmint.com/cheat-command-line-cheat-sheet-for-linux-users/

原文地址:https://www.cnblogs.com/sanghai/p/7482754.html