linux命令之strace简单使用
- strace是什么
strace是一个可用于诊断、调试和教学的Linux用户空间跟踪器。我们用它来监控用户空间进程和内核的交互,比如系统调用、信号传递、进程状态变更等。
- 使用方式
- 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等
- 以下是本人在使用的应用场景
- 研究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简单使用的更多相关文章
- 重学Python - Day 06 - python基础 -> linux命令行学习 -- 简单基础命令学习
学习资源 虚拟机工具:VMWare 12 linux :Ubuntu 14 或者CentOS 6 PS:ubuntu用远程连接工具的设置方法 step 1: 输入sudo apt-get instal ...
- 小白的linux笔记7:批量运行复杂的linux命令组合——BASH简单使用法
linux的BASH就相当于windows下的BAT文件,可以批处理命令.比如写好一个python脚本后,需要在运行时候加参数,但这个参数又不想每次输入,就可以用BASH的方式写好整条命令,然后直接运 ...
- linux命令 screen的简单使用
在远程命令行下某些长时间的操作,一旦网络出现故障,后果可能会很严重,在这种情况下可以使用screen命令来解决.screen可以创建一个session,在不小心断开以后还可以继续恢复session保存 ...
- Linux命令之---which简单介绍
命令简介 which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果.也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的 ...
- linux 命令之 objdump 简单使用
objdump 介绍 objdump命令是用查看目标文件或者可执行的目标文件的构成的gcc工具 objdump 选项介绍 --archive-headers -a 显示档案库的成员信息,类似ls -l ...
- Linux命令行及Vim简单学习记录
Linux命令行 1.打开命令行 Ctrl+Alt+t 2.目录 显示当前目录的文件列表 ls 跳转至当前目录中的x文件夹 cd x 返回根目录 cd 3.文件 新建文件1.cpp touch ./1 ...
- Linux监控命令之==>strace
一.命令介绍 strace 常用来跟踪进程执行时的系统调用和所接收的信号.在Linux 世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式 ...
- linux中几个简单的系统命令(还有一些其他杂项命令)
linux中几个简单的系统命令,其他命令接触到了在补充. 1.ps命令:(process status),提供对进程的一次性查看.以及执行ps命令时那个时刻的进程信息 格式:ps[参数] -e 此参数 ...
- Linux02 /Linux命令简单使用
Linux02 /Linux命令简单使用 目录 Linux02 /Linux命令简单使用 1. 远程连接Linux 2. 目录相关操作 3. 简单命令 1. 远程连接Linux 远程连接工具 Xshe ...
随机推荐
- 随机序列[SHOI2016](找规律+线段树)
传送门 这道题的题意就是给你n个数让你在每个数之间插入+.-.*三种运算符中的一种,然后算出一个答案,再把答案加起来. 这题肯定是不能暴力的(题目都告诉你了由3n-1种结果).我们先从小的情况枚举找一 ...
- 2020 NUC 19级第一次训练赛
感染(low) Description n户人家住在一条直线上,从左往右依次编号为1,2,...,n.起初,有m户人家感染了COVID-19,而接下来的每天感染的人家都会感染他家左右两家的人,问t天后 ...
- jdbc学习一半的代码
用java连接MySQL的准备工作 1.下载MySQL(了解MySQL的基本语法) 2.下载java的和MySQL的连接 3.在程序中加入2中下载的jar包 写java程序连接数据库的基本步骤: 1. ...
- 「SCOI2005」栅栏
传送门 Luogu 解题思路 我们有很显然的这样一条贪心思路: 首先满足长度短的木板,因为如果可以满足长的也肯定可以满足短的,而且可能满足更多. 那么我们就会有这样的思路:枚举一条木板由哪条木板切割而 ...
- php 增删改查范例(1)
主页index.php(含多条件查询): <?php$db = new Mysqli("localhost","root","root" ...
- Redis使用守护进程启动sentinel并指定其日志目录
正常redis-server可以通过配置文件来指定守护进程启动以及指定日志路径,但sentinel就不一样了.正常启动redis的sentinel时,进程会直接在前台跑,一退出sentinel进程就关 ...
- 小程序父子组件onLoad和Created之间的问题
今天开发日历插件时,遇到了以下问题: 因为需要在父组件的onLoad里加载接口从而得到每一天的房间数据,然后将数据存进小程序缓存. 接着在子组件里 获取小程序缓存来得到父组件传来的房间数据,在子组件里 ...
- Unity Scene视图下 输出物体坐标等信息
using UnityEditor; using UnityEngine; [CustomEditor(typeof(GameObject))] public class MyEditor : Edi ...
- HihoCoder第一周与POJ3974:最长回文字串
这个题目是hihoCoder第一周的题目,自己打算从第一周开始做起,不知道能追上多少,更不知道这一篇写完,下一篇会是什么时候... 题意很简单. 输入: abababa aaaabaa acacdas ...
- 关于ESP8266和ESP8285的对比
ESP8285=ESP8266+1M Flash. 与ESP8266相比,其能耐高温达125摄氏度!且原有ESP8266源码程序可以原封不动移植使用.ESP-M1/M2 模块核心处理器采用高性价比芯片 ...