• strace是什么

  strace是一个可用于诊断、调试和教学的Linux用户空间跟踪器。我们用它来监控用户空间进程和内核的交互,比如系统调用、信号传递、进程状态变更等。

  • 使用方式
  1. strace 使用帮助
usage: strace [-CdffhiqrtttTvVwxxy] [-I n] [-e expr]...
[-a column] [-o file] [-s strsize] [-P path]...
-p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]
or: strace -c[dfw] [-I n] [-e expr]... [-O overhead] [-S sortby]
-p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]
Output format:
-a column alignment COLUMN for printing syscall results (default 40)
-i print instruction pointer at time of syscall
-o file send trace output to FILE instead of stderr
-q suppress messages about attaching, detaching, etc.
-r print relative timestamp
-s strsize limit length of print strings to STRSIZE chars (default 32)
-t print absolute timestamp
-tt print absolute timestamp with usecs
-T print time spent in each syscall
-x print non-ascii strings in hex
-xx print all strings in hex
-y print paths associated with file descriptor arguments
-yy print protocol specific information associated with socket file descriptors Statistics:
-c count time, calls, and errors for each syscall and report summary
-C like -c but also print regular output
-O overhead set overhead for tracing syscalls to OVERHEAD usecs
-S sortby sort syscall counts by: time, calls, name, nothing (default time)
-w summarise syscall latency (default is system time) Filtering:
-e expr a qualifying expression: option=[!]all or option=[!]val1[,val2]...
options: trace, abbrev, verbose, raw, signal, read, write
-P path trace accesses to path Tracing:
-b execve detach on execve syscall
-D run tracer process as a detached grandchild, not as parent
-f follow forks
-ff follow forks with output into separate files
-I interruptible
1: no signals are blocked
2: fatal signals are blocked while decoding syscall (default)
3: fatal signals are always blocked (default if '-o FILE PROG')
4: fatal signals and SIGTSTP (^Z) are always blocked
(useful to make 'strace -o FILE PROG' not stop on ^Z) Startup:
-E var remove var from the environment for command
-E var=val put var=val in the environment for command
-p pid trace process with process id PID, may be repeated
-u username run command as username handling setuid and/or setgid

常用使用方式

strace -tt  -e trace=process -o log.txt -f  command args

常见trace:
-e trace=file 跟踪和文件访问相关的调用(参数中有文件名)
-e trace=process 和进程管理相关的调用,比如fork/exec/exit_group
-e trace=network 和网络通信相关的调用,比如socket/sendto/connect
-e trace=signal 信号发送和处理相关,比如kill/sigaction
-e trace=desc 和文件描述符相关,比如write/read/select/epoll等
-e trace=ipc 进程见同学相关,比如shmget等
  • 以下是本人在使用的应用场景
  1. 研究system调用执行一个命令ls -lh时的工作过程
#include<stdio.h>
#include<stdlib.h> int main()
{
system("ls -lh");
return ;
}

  编译源码gcc systemcall.c -o systemcall,使用strace -tt -o log.txt -e trace=process -f ./systemcall追踪结果如下:

33816 04:34:23.873655 execve("./systemcall", ["./systemcall"], [/* 25 vars */]) = 0
33816 04:34:23.875018 arch_prctl(ARCH_SET_FS, 0x7f81656b2740) = 0
33816 04:34:23.875312 clone(child_stack=0, flags=CLONE_PARENT_SETTID|SIGCHLD, parent_tidptr=0x7fff68104620) = 33817
33816 04:34:23.875399 wait4(33817, <unfinished ...>
33817 04:34:23.875492 execve("/bin/sh", ["sh", "-c", "ls -lh"], [/* 25 vars */]) = 0
33817 04:34:23.876447 arch_prctl(ARCH_SET_FS, 0x7fd146dc9740) = 0
33817 04:34:23.879322 execve("/usr/bin/ls", ["ls", "-lh"], [/* 24 vars */]) = 0
33817 04:34:23.881106 arch_prctl(ARCH_SET_FS, 0x7f199c28a840) = 0
33817 04:34:23.887491 exit_group(0) = ?
33817 04:34:23.887627 +++ exited with 0 +++
33816 04:34:23.887654 <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 33817
33816 04:34:23.887734 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=33817, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
33816 04:34:23.887775 exit_group(0) = ?
33816 04:34:23.887839 +++ exited with 0 +++

可以看到system执行了clone生成了一个子进程33817,然后execve执行了/bin/sh -c "ls -lh"命令,接着sh程序解析-c参数后,调用execve执行了ls -lh命令

  2. 研究system调用执行两个命令ls -lh;ls时的工作过程

#include<stdio.h>
#include<stdlib.h>
int main()
{
system("ls -lh;ls");
return ;
}

  编译源码gcc systemcall2.c -o systemcall2,使用strace -tt -o log.txt -e trace=process -f ./systemcall2追踪结果如下:

35076 04:43:55.567202 execve("./systemcall2", ["./systemcall2"], [/* 25 vars */]) = 0
35076 04:43:55.568049 arch_prctl(ARCH_SET_FS, 0x7f8e13d49740) = 0
35076 04:43:55.568376 clone(child_stack=0, flags=CLONE_PARENT_SETTID|SIGCHLD, parent_tidptr=0x7ffe05e7bd30) = 35077
35076 04:43:55.568472 wait4(35077, <unfinished ...>
35077 04:43:55.568578 execve("/bin/sh", ["sh", "-c", "ls -lh;ls"], [/* 25 vars */]) = 0
35077 04:43:55.569651 arch_prctl(ARCH_SET_FS, 0x7f3ef8974740) = 0
35077 04:43:55.572759 clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f3ef8974a10) = 35078
35077 04:43:55.573011 wait4(-1, <unfinished ...>
35078 04:43:55.573253 execve("/usr/bin/ls", ["ls", "-lh"], [/* 24 vars */]) = 0
35078 04:43:55.575190 arch_prctl(ARCH_SET_FS, 0x7f07f5e0d840) = 0
35078 04:43:55.581789 exit_group(0) = ?
35078 04:43:55.581945 +++ exited with 0 +++
35077 04:43:55.581972 <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 35078
35077 04:43:55.582030 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=35078, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
35077 04:43:55.582060 wait4(-1, 0x7fff86e41950, WNOHANG, NULL) = -1 ECHILD (No child processes)
35077 04:43:55.582435 clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f3ef8974a10) = 35079
35077 04:43:55.582768 wait4(-1, <unfinished ...>
35079 04:43:55.583048 execve("/usr/bin/ls", ["ls"], [/* 24 vars */]) = 0
35079 04:43:55.585341 arch_prctl(ARCH_SET_FS, 0x7fd65912d840) = 0
35079 04:43:55.587148 exit_group(0) = ?
35079 04:43:55.587264 +++ exited with 0 +++
35077 04:43:55.587291 <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 35079
35077 04:43:55.587383 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=35079, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
35077 04:43:55.587413 wait4(-1, 0x7fff86e41990, WNOHANG, NULL) = -1 ECHILD (No child processes)
35077 04:43:55.587508 exit_group(0) = ?
35077 04:43:55.587626 +++ exited with 0 +++
35076 04:43:55.587649 <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 35077
35076 04:43:55.587721 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=35077, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
35076 04:43:55.587757 exit_group(0) = ?
35076 04:43:55.587826 +++ exited with 0 +++

  从追踪结果来看,system在执行时会产生一个子进程35077执行/bin/sh -c "ls -lh;ls"命令,然后由35077产生了子进程35078执行ls -lh命令,35078执行退出后,35077又产生了子进程35079执行ls命令。

  从上面追踪可知,system执行后首先创建一个进程执行/bin/sh命令,然后sh 命令解析其参数,如果只有一条命令时,则直接调用execve执行命令,否则由sh进程创建子进程调用execve执行相关命令。

linux命令之strace简单使用的更多相关文章

  1. 重学Python - Day 06 - python基础 -> linux命令行学习 -- 简单基础命令学习

    学习资源 虚拟机工具:VMWare 12 linux :Ubuntu 14 或者CentOS 6 PS:ubuntu用远程连接工具的设置方法 step 1: 输入sudo apt-get instal ...

  2. 小白的linux笔记7:批量运行复杂的linux命令组合——BASH简单使用法

    linux的BASH就相当于windows下的BAT文件,可以批处理命令.比如写好一个python脚本后,需要在运行时候加参数,但这个参数又不想每次输入,就可以用BASH的方式写好整条命令,然后直接运 ...

  3. linux命令 screen的简单使用

    在远程命令行下某些长时间的操作,一旦网络出现故障,后果可能会很严重,在这种情况下可以使用screen命令来解决.screen可以创建一个session,在不小心断开以后还可以继续恢复session保存 ...

  4. Linux命令之---which简单介绍

    命令简介 which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果.也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的 ...

  5. linux 命令之 objdump 简单使用

    objdump 介绍 objdump命令是用查看目标文件或者可执行的目标文件的构成的gcc工具 objdump 选项介绍 --archive-headers -a 显示档案库的成员信息,类似ls -l ...

  6. Linux命令行及Vim简单学习记录

    Linux命令行 1.打开命令行 Ctrl+Alt+t 2.目录 显示当前目录的文件列表 ls 跳转至当前目录中的x文件夹 cd x 返回根目录 cd 3.文件 新建文件1.cpp touch ./1 ...

  7. Linux监控命令之==>strace

    一.命令介绍 strace 常用来跟踪进程执行时的系统调用和所接收的信号.在Linux 世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式 ...

  8. linux中几个简单的系统命令(还有一些其他杂项命令)

    linux中几个简单的系统命令,其他命令接触到了在补充. 1.ps命令:(process status),提供对进程的一次性查看.以及执行ps命令时那个时刻的进程信息 格式:ps[参数] -e 此参数 ...

  9. Linux02 /Linux命令简单使用

    Linux02 /Linux命令简单使用 目录 Linux02 /Linux命令简单使用 1. 远程连接Linux 2. 目录相关操作 3. 简单命令 1. 远程连接Linux 远程连接工具 Xshe ...

随机推荐

  1. PHP的错误知识

    一.绪 PHP程序的错误发生一般归属于: 语法错误(会阻止脚本的执行) 运行时错误(不会阻止脚本的执行,但会组织脚本做希望它做的任何事情) 逻辑错误(不会阻止脚本执行,也不会显示错误消息) 二.开启错 ...

  2. LoRaWAN协议(一)------架构解析

    摘自:http://www.cnblogs.com/answerinthewind/p/6200497.html LoRaWAN协议(一)-----架构解析 (1)LoRaWAN分层 LoRaWAN总 ...

  3. 洛谷 P1886 滑动窗口 /【模板】单调队列

    纯板子题,入队时保证单调性,即单调栈,出队保证题目条件,本题即窗口长度k,在入队出队时都可以维护信息 ; int buf[maxm], maxq[maxm], minq[maxm], ans1[max ...

  4. docker for windows 容器内网通过独立IP直接访问的方法

    Docker官方推荐我们通过端口映射的方式把Docker容器的服务提供给宿主机或者局域网其他容器使用.一般过程是: 1.Docker进程通过监听宿主机的某个端口,将该端口的数据包发送给Docker容器 ...

  5. 如何使用Nexus搭建Maven私服

    如何使用Nexus搭建Maven私服 听语音 | 浏览:47 | 更新:2016-09-29 10:22 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅最快的到家服务,最优质的电脑清灰! ...

  6. C# 篇基础知识5——委托和事件

    事件处理程序是基于“委托”机制运行的. 1.委托 (1)委托的定义和使用 有时需要将一个函数作为另一个函数的参数,这时就要用到委托(Delegate)机制.例如设计一个马戏表演函数: //定义委托 d ...

  7. javaweb框架--自定义标签与freemaker结合

    http://blog.csdn.net/myfmyfmyfmyf/article/details/8960299 很有用但是不不知道怎么说,写个例子,总之方便多了,并且容易管理,重复利用强 1.自定 ...

  8. 蓝牙 BLE 协议学习: 3种蓝牙架构实现方案(蓝牙协议栈方案)

    导言 不同的蓝牙架构可以用在不同的场景中.从而协议帧的架构方案也会不同. 转载自:<三种蓝牙架构实现方案(蓝牙协议栈方案)> 蓝牙架构实现方案有哪几种?我们一般把整个蓝牙实现方案叫做蓝牙协 ...

  9. node批量修改文件名称

    let fs = require('fs');//引用文件系统模块 let PATH = `./app_zijietiaodong/`;//当前文件夹 let ext = { readFileList ...

  10. uni-app小程序滑动事件

    <view class="relative" @touchmove="handletouchmove" @touchstart="handlet ...