• 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. 02-01 Android学习进度报告一

    前两天,刚刚安装好有关Android开发有关的软件并配好了环境,有一些体会想要发表. 首先我了解到有一款专门用于Android开发的软件,叫做Android Studio ,是一个IDE集成软件 于是 ...

  2. 30 整数中1出现的次数(从1到n整数中1出现的次数)这题很难要多看*

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  3. 关于c++ 感想

    前言 在学校开展了c++的课程,但是不得不说相当乏味. 原因很简单: 1.感觉c++很高级,自己就这智商怎么学的会哦,自己给了自己门槛. 2.c++很难快速的做出一个能够展现的项目,缺乏成就感. 3. ...

  4. 如何解决 Django 前后端分离开发的跨域问题

    一.同源策略 1.先来说说什么是源 • 源(origin)就是协议.域名和端口号. 以上url中的源就是:http://www.company.com:80 若地址里面的协议.域名和端口号均相同则属于 ...

  5. 实验吧——Recursive

    环境:win10,kali虚拟机 工具:ida 虚拟机打开看看,发现是ELF文件,运行一下,额没有什么发现. Ida打开看看,发现是在文件内部运行python解释器,百度搜索了一个基本上可以找到Py_ ...

  6. ETC系列产品非接触式读卡器方案:SI522

    随着科技的不断发展,出行上高速这是非常寻常的事.但是在很多节假日高峰时期,在高速路口塞车缴费给很多车主造成很大的烦心.为了解决这一系列的问题,科技发明了ETC这种便捷式缴费技术,让车主们顺畅通过高速路 ...

  7. jmeter抓取cnode网站token值

    前置条件:已经登录 1.线程组下面先添加HTTP信息头管理器 1.1 jmeter向服务器发送http请求时,需要验证 cookie的等设置信息给到服务器去识别,因此,在发送请求前,我们一般会把相关需 ...

  8. 数学软件实训1-MATLAB程序设计及应用初步

    数学软件实训任务一 一 题目:MATLAB程序设计及应用初步 二 目的:掌握MATLAB程序设计的基本方法,会利用MATLAB程序设计思想编程处理一些简单问题. 三 要求: 1 掌握控制流的基本语法结 ...

  9. notifix测试

    成功 失败 警告 提示

  10. LeetCode455 分发饼干(简单贪心—Java优先队列简单应用)

    题目: 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有 ...