上线踩坑引发的处理方式---lsof,strace

两个跟踪进程的linux命令

lsof -p pid
strace -p pid
快速跟踪进程出现问题的地方。
由于进程本身运行良好,但是内部一直等待第三方哪个应答,导致进程假死。
引用自:http://linux.chinaunix.net/techdoc/net/2007/08/24/966264.shtml
strace 命令
  用途:打印 STREAMS 跟踪消息。
  语法:strace [ mid sid level ] ...
 
 描述:没有参数的 strace 命令将所有的驱动程序和模块中的所有 STREAMS 事件跟踪消息写到它的标准输出中。 这些消息是从
STREAMS
日志驱动程序中获取的。如果提供参数,它们必须是在三元组中。每个三元组表明跟踪消息要从给定的模块或驱动程序、子标识(通常表明次要设备)以及优先级别
等于或小于给定级别的模块或驱动程序中接收。all 标记可由任何成员使用,以表明对该属性没有限制。
  参数:mid---指定 STREAMS 模块的标识号  sid---指定子标识号  level----指定跟踪优先级别
  输出格式:每个跟踪消息输出的格式是:
     跟踪序列号
     消息时间(格式为 hh:mm:ss)
     系统启动后,以机器滴答信号表示消息的时间
     跟踪优先级别
     有以下值之一:
  E
   消息也在错误日志中
  F
   表示一个致命错误
  N
   邮件已发送给系统管理员
       源的模块标识号
       源的子标识号
       跟踪消息的格式化文本
  在多处理器系统上, 由两部分组成:消息所有者发送处的处理器号码,格式化文本本身。
  一旦启动,strace 命令将继续执行,直到用户终止该命令。
  
  注:由于性能的考虑,所以一次只允许一个 strace 命令来打开 STREAMS 日志驱动程序。
 
 日志驱动程序有一个三元组的列表(该列表在命令调用中指定),并且程序会根据该列表比较每个潜在的跟踪消息,以决定是否要格式化和发送这个信息到
strace 进程中。因此,长的三元组列表会对 STREAMS 的总体性能的影响更大。 运行 strace
命令对于某些模块和驱动程序(生成要发送给 strace 进程的跟踪消息的模块和驱动程序)的定时的影响最大。 如果跟踪消息生成过快,以至
strace 进程无法处理,那么就会丢失一些消息。 最后的情况可以通过检查跟踪消息输出上的序列号来确定。
  
  示例
  要输出模块标识为 41 的模块或驱动程序中的所有跟踪消息,请输入:
  
  strace 41 all all
  要输出模块标识为 41,子标识为 0、1 或 2 的模块或驱动程序中的跟踪消息:
  
  strace 41 0 1 41 1 1 41 2 0
   子标识为 0 和 1 的模块或驱动程序中的消息必须具有小于或等于 1 的跟踪级别。子标识为 2 的模块或驱动程序中的消息必须具有跟踪级别 0。
  
  strace: option requires an argument -- e
  usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]
                [-p pid] ... [-s strsize] [-u username] [-E var=val] ...
                [command [arg ...]]
     or: strace -c [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...
                [command [arg ...]]
  -c -- count time, calls, and errors for each syscall and report summary
  -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
  -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 - 跟踪系统调用和信号
  
  usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] [-o file]
  [-p pid] [-s strsize] [-u username] [command [arg]]
  strace -c [-e expr] [-O overhead] [-S sortby] [command [arg]]
  
  -a column
   指定显示返回值的列位置,默认是40(从0开始计数),就是说"="出现在40列的位
   置。
  
  -c 产生类似下面的统计信息
  
   strace -c -p 14653 (Ctrl-C)
   % time seconds usecs/call calls errors syscall
   ------ ----------- ----------- --------- --------- ----------------
   53.99 0.012987 3247 4 2 wait4
   42.16 0.010140 2028 5 read
   1.78 0.000429 61 7 write
   0.76 0.000184 10 18 ioctl
   0.50 0.000121 2 52 rt_sigprocmask
   0.48 0.000115 58 2 fork
   0.18 0.000043 2 18 rt_sigaction
   0.06 0.000014 14 1 1 stat
   0.03 0.000008 4 2 sigreturn
   0.02 0.000006 2 3 time
   0.02 0.000006 3 2 1 setpgid
   ------ ----------- ----------- --------- --------- ----------------
   100.00 0.024053 114 4 total
  
   -d 输出一些strace自身的调试信息到标准输出
  
   strace -c -p 14653 -d (Ctrl-C)
   [wait(0x137f) = 14653]
   pid 14653 stopped, [SIGSTOP]
   [wait(0x57f) = 14653]
   pid 14653 stopped, [SIGTRAP]
   cleanup: looking at pid 14653
   % time seconds usecs/call calls errors syscall
   ------ ----------- ----------- --------- --------- ----------------
   ------ ----------- ----------- --------- --------- ----------------
   100.00 0.000000 0 total
  
   -e expr
   A qualifying expression which modifies which events to trace or how to trace
   them. The format of the expression is:
  
   [qualifier=][!]value1[,value2]...
  
   这里qualifier可以是trace、abbrev、verbose、raw、signal、read或者write。
   value是qualifier相关的符号或数值。缺省qualifier是trace。!表示取反。
   -eopen等价于-e trace=open,表示只跟踪open系统调用。-etrace=!open意思是
   跟踪除open系统调用之外的其他所有系统调用。此外value还可以取值all和none。
  
   某些shell用!表示重复历史指令,此时可能需要引号、转义符号(\)的帮助。
  
   -e trace=set
   只跟踪指定的系统调用列表。决定跟踪哪些系统调用时,-c选项很有用。
   trace=open,close,read,write意即只跟踪这四种系统调用,缺省是trace=all
  
   -e trace=file
   跟踪以指定文件名做参数的所有系统调用。
  
   -e trace=process
   Trace all system calls which involve process management. This is
   useful for watching the fork, wait, and exec steps of a process.
  
   -e trace=network
   跟踪所有和网络相关的系统调用
  
   -e trace=signal
   Trace all signal related system calls.
  
   -e trace=ipc
   Trace all IPC related system calls.
  
   -e abbrev=set
   Abbreviate the output from printing each member of large structures.
   缺省是abbrev=all,-v选项等价于abbrev=none
  
   -e verbose=set
   Dereference structures for the specified set of system calls.
   The default is verbose=all.
  
   -e raw=set
   Print raw, undecoded arguments for the specifed set of system calls.
   This option has the effect of causing all arguments to be printed in
   hexadecimal. This is mostly useful if you don"t trust the decoding or
   you need to know the actual numeric value of an argument.
  
   -e signal=set
   只跟踪指定的信号列表,缺省是signal=all。signal=!SIGIO (or signal=!io)
   导致 SIGIO 信号不被跟踪
  
   -e read=set
   Perform a full hexadecimal and ASCII dump of all the data read from
   file descriptors listed in the specified set. For example, to see all
   input activity on file descriptors 3 and 5 use -e read=3,5. Note that
   this is independent from the normal tracing of the read(2) system call
   which is controlled by the option -e trace=read.
  
   -e write=set
   Perform a full hexadecimal and ASCII dump of all the data written to
   file descriptors listed in the specified set. For example, to see all
   output activity on file descriptors 3 and 5 use -e write=3,5. Note
   that this is independent from the normal tracing of the write(2)
   system call which is controlled by the option -e trace=write.
  
   -f
   follow forks,跟随子进程?
  
   Trace child processes as they are created by currently traced
   processes as a result of the fork(2) system call. The new process
   is attached to as soon as its pid is known (through the return value
   of fork(2) in the parent process). This means that such children may
   run uncontrolled for a while (especially in the case of a vfork(2)),
   until the parent is scheduled again to complete its (v)fork(2)
   call. If the parent process decides to wait(2) for a child that is
   currently being traced, it is suspended until an appropriate child
   process either terminates or incurs a signal that would cause it to
   terminate (as determined from the child"s current signal disposition).
  
   意思应该是说跟踪某个进程时,如果发生fork()调用,则选择跟踪子进程
   可以参考gdb的set follow-fork-mode设置
  
   -F
   attempt to follow vforks
   (On SunOS 4.x, this is accomplished with some dynamic linking trickery.
   On Linux, it requires some kernel functionality not yet in the
   standard kernel.) Otherwise, vforks will not be followed even if -f
   has been given.
  
   类似-f选项
  
   -ff
   如果-o file选项有效指定,则跟踪过程中新产生的其他相关进程的信息分别写
   入file.pid,这里pid是各个进程号。
  
   -h
   显示帮助信息
  
   -i
   显示发生系统调用时的IP寄存器值
   strace -p 14653 -i
  
   -o filename
   指定保存strace输出信息的文件,默认使用标准错误输出stderr
  
   Use filename.pid if -ff is used. If the argument begins with `|" or
   with `!" then the rest of the argument is treated as a command and all
   output is piped to it. This is convenient for piping the debugging
   output to a program without affecting the redirections of executed
   programs.
  
   -O overhead
   Set the overhead for tracing system calls to overhead microseconds.
   This is useful for overriding the default heuristic for guessing how
   much time is spent in mere measuring when timing system calls using
   the -c option. The acuracy of the heuristic can be gauged by timing
   a given program run without tracing (using time(1)) and comparing
   the accumulated system call time to the total produced using -c.
  
   好象是用于确定哪些系统调用耗时多
  
   -p pid
  
   指定待跟踪的进程号,可以用Ctrl-C终止这种跟踪而被跟踪进程继续运行。可以
   指定多达32个-p参数同时进行跟踪。
  
   比如 strace -ff -o output -p 14653 -p 14117
  
   -q
   Suppress messages about attaching, detaching etc. This happens
   automatically when output is redirected to a file and the command is
   run directly instead of attaching.
  
   -r
   Print a relative timestamp upon entry to each system call. This
   records the time difference between the beginning of successive
   system calls.
  
   strace -p 14653 -i -r
  
   -s strsize
   指定字符串最大显示长度,默认32。但文件名总是显示完整。
   -S sortby
   Sort the output of the histogram printed by the -c option by the
   specified critereon. Legal values are time, calls, name, and nothing
   (default time).
  
   -t
   与-r选项类似,只不过-r采用相对时间戳,-t采用绝对时间戳(当前时钟)
  
   -tt
   与-t类似,绝对时间戳中包含微秒
  
   -ttt
   If given thrice, the time printed will include the microseconds and
   the leading portion will be printed as the number of seconds since
   the epoch.
  
   -T
   这个选项显示单个系统调用耗时
  
   -u username
   用指定用户的UID、GID以及辅助组身份运行待跟踪程序
  
   -v
   冗余显示模式
   Print unabbreviated versions of environment, stat, termios, etc. calls.
   These structures are very common in calls and so the default behavior
   displays a reasonable subset of structure members. Use this option to
   get all of the gory details.
  
   -V
   显示strace版本信息
  
   -x 以16进制字符串格式显示非ascii码,比如"\x08",默认采用8进制,比如"\10"
  
   -xx 以16进制字符串格式显示所有字节



引用自:http://blog.csdn.net/guoguo1980/article/details/2324454


lsof简介
lsof(list open files)是一个列出当前系统打开文件的工具。在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。所以如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符,无论这个文件的本质如何,该文件描述符为应用程序与基础操作系统之间的交互提供了通用接口。因为应用程序打开文件的描述符列表提供了大量关于这个应用程序本身的信息,因此通过lsof工具能够查看这个列表对系统监测以及排错将是很有帮助的。 
lsof使用
 
lsof输出信息含义
在终端下输入lsof即可显示系统打开的文件,因为 lsof 需要访问核心内存和各种文件,所以必须以 root 用户的身份运行它才能够充分地发挥其功能。 
COMMAND    PID      USER   FD      TYPE     DEVICE     SIZE       NODE      NAME
init       1         root  cwd      DIR       3,3       1024       2         /
init       1         root  rtd      DIR       3,3       1024       2         /
init       1         root  txt      REG       3,3       38432      1763452  /sbin/init
init       1         root  mem      REG       3,3       106114     1091620  /lib/libdl-2.6.so
init       1         root  mem      REG       3,3       7560696    1091614  /lib/libc-2.6.so
init       1         root  mem      REG       3,3       79460      1091669  /lib/libselinux.so.1
init       1         root  mem      REG       3,3       223280     1091668  /lib/libsepol.so.1
init       1         root  mem      REG       3,3       564136     1091607  /lib/ld-2.6.so
init       1         root  10u      FIFO      0,15                  1309     /dev/initctl
每行显示一个打开的文件,若不指定条件默认将显示所有进程打开的所有文件。lsof输出各列信息的意义如下: 
COMMAND:进程的名称
PID:进程标识符
USER:进程所有者
FD:文件描述符,应用程序通过文件描述符识别该文件。如cwd、txt等
TYPE:文件类型,如DIR、REG等
DEVICE:指定磁盘的名称
SIZE:文件的大小
NODE:索引节点(文件在磁盘上的标识)
NAME:打开文件的确切名称
其中FD 列中的文件描述符cwd 值表示应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改。
txt 类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /sbin/init 程序。其次数值表示应用
程序的文件描述符,这是打开该文件时返回的一个整数。如上的最后一行文件/dev/initctl,其文件描述符为 10。u 表示该
文件被打开并处于读取/写入模式,而不是只读 ® 或只写 (w) 模式。同时还有大写 的W 表示该应用程序具有对整个文件的写
锁。该文件描述符用于确保每次只能打开一个应用程序实例。初始打开每个应用程序时,都具有三个文件描述符,从 0 到 2,
分别表示标准输入、输出和错误流。所以大多数应用程序所打开的文件的 FD 都是从 3 开始。 
与 FD 列相比,Type 列则比较直观。文件和目录分别称为 REG 和 DIR。而CHR 和 BLK,分别表示字符和块设备;
或者 UNIX、FIFO 和 IPv4,分别表示 UNIX 域套接字、先进先出 (FIFO) 队列和网际协议 (IP) 套接字。 
lsof常用参数
lsof 常见的用法是查找应用程序打开的文件的名称和数目。可用于查找出某个特定应用程序将日志数据记录到何处,或者正在跟踪某个问题。
例如,linux限制了进程能够打开文件的数目。通常这个数值很大,所以不会产生问题,并且在需要时,应用程序可以请求更大的值(直到某
个上限)。如果你怀疑应用程序耗尽了文件描述符,那么可以使用 lsof 统计打开的文件数目,以进行验证。lsof语法格式是: 
lsof [options] filename
常用的参数列表: 
lsof  filename 显示打开指定文件的所有进程
lsof -a 表示两个参数都必须满足时才显示结果
lsof -c string   显示COMMAND列中包含指定字符的进程所有打开的文件
lsof -u username  显示所属user进程打开的文件
lsof -g gid 显示归属gid的进程情况
lsof +d /DIR/ 显示目录下被进程打开的文件
lsof +D /DIR/ 同上,但是会搜索目录下的所有目录,时间相对较长
lsof -d FD 显示指定文件描述符的进程
lsof -n 不将IP转换为hostname,缺省是不加上-n参数
lsof -i 用以显示符合条件的进程情况
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
            46 --> IPv4 or IPv6
            protocol --> TCP or UDP
            hostname --> Internet host name
            hostaddr --> IPv4地址
            service --> /etc/service中的 service name (可以不只一个)
            port --> 端口号 (可以不只一个)
例如: 查看22端口现在运行的情况 
# lsof -i :22
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
sshd    1409 root    3u  IPv6   5678       TCP *:ssh (LISTEN)
查看所属root用户进程所打开的文件类型为txt的文件: 
# lsof -a -u root -d txt
COMMAND    PID USER  FD      TYPE DEVICE    SIZE    NODE NAME
init       1    root txt       REG    3,3   38432 1763452 /sbin/init
mingetty  1632 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1633 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1634 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1635 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1636 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1637 root txt       REG    3,3   14366 1763337 /sbin/mingetty
kdm        1638 root txt       REG    3,3  132548 1428194 /usr/bin/kdm
X          1670 root txt       REG    3,3 1716396 1428336 /usr/bin/Xorg
kdm        1671 root txt       REG    3,3  132548 1428194 /usr/bin/kdm
startkde  2427 root txt       REG    3,3  645408 1544195 /bin/bash
... ...  
lsof使用实例
 
一、查找谁在使用文件系统
在卸载文件系统时,如果该文件系统中有任何打开的文件,操作通常将会失败。那么通过lsof可以找出那些进程在使用当前要卸载的文件系统,如下: 
# lsof  /GTES11/
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
bash    4208 root  cwd    DIR    3,1 4096    2 /GTES11/
vim     4230 root  cwd    DIR    3,1 4096    2 /GTES11/
在这个示例中,用户root正在其/GTES11目录中进行一些操作。一个 bash是实例正在运行,并且它当前的目录为/GTES11,另一个则显示的是vim正在编辑/GTES11下的文件。要成功地卸载/GTES11,应该在通知用户以确保情况正常之后,中止这些进程。 这个示例说明了应用程序的当前工作目录非常重要,因为它仍保持着文件资源,并且可以防止文件系统被卸载。这就是为什么大部分守护进程(后台进程)将它们的目录更改为根目录、或服务特定的目录(如 sendmail 示例中的 /var/spool/mqueue)的原因,以避免该守护进程阻止卸载不相关的文件系统。 
二、恢复删除的文件
当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹。管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志。有时可以通过lsof来恢复这些文件。 
当进程打开了某个文件时,只要该进程保持打开该文件,即使将其删除,它依然存在于磁盘中。这意味着,进程并不知道文件已经被删除,它仍然可以向打开该文件时提供给它的文件描述符进行读取和写入。除了该进程之外,这个文件是不可见的,因为已经删除了其相应的目录索引节点。 
在/proc 目录下,其中包含了反映内核和进程树的各种文件。/proc目录挂载的是在内存中所映射的一块区域,所以这些文件和目录并不存在于磁盘中,因此当我们对这些文件进行读取和写入时,实际上是在从内存中获取相关信息。大多数与 lsof 相关的信息都存储于以进程的 PID 命名的目录中,即 /proc/1234 中包含的是 PID 为 1234 的进程的信息。每个进程目录中存在着各种文件,它们可以使得应用程序简单地了解进程的内存空间、文件描述符列表、指向磁盘上的文件的符号链接和其他系统信息。lsof 程序使用该信息和其他关于内核内部状态的信息来产生其输出。所以lsof 可以显示进程的文件描述符和相关的文件名等信息。也就是我们通过访问进程的文件描述符可以找到该文件的相关信息。 
  
当系统中的某个文件被意外地删除了,只要这个时候系统中还有进程正在访问该文件,那么我们就可以通过lsof从/proc目录下恢复该文件的内容。 假如由于误操作将/var/log/messages文件删除掉了,那么这时要将/var/log/messages文件恢复的方法如下: 
首先使用lsof来查看当前是否有进程打开/var/logmessages文件,如下: 
# lsof |grep /var/log/messages
syslogd   1283      root    2w      REG        3,3  5381017    1773647 /var/log/messages (deleted)
从上面的信息可以看到 PID 1283(syslogd)打开文件的文件描述符为 2。同时还可以看到/var/log/messages已经标记被删除了。因此我们可以在 /proc/1283/fd/2 (fd下的每个以数字命名的文件表示进程对应的文件描述符)中查看相应的信息,如下: 
# head -n 10 /proc/1283/fd/2
Aug  4 13:50:15 holmes86 syslogd 1.4.1: restart.
Aug  4 13:50:15 holmes86 kernel: klogd 1.4.1, log source = /proc/kmsg started.
Aug  4 13:50:15 holmes86 kernel: Linux version 2.6.22.1-8 ([email protected]) (gcc version 4.2.0) #1 SMP Wed Jul 18 11:18:32 EDT 2007
Aug  4 13:50:15 holmes86 kernel: BIOS-provided physical RAM map:
Aug  4 13:50:15 holmes86 kernel:  BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
Aug  4 13:50:15 holmes86 kernel:  BIOS-e820: 000000000009f000 - 00000000000a0000 (reserved)
Aug  4 13:50:15 holmes86 kernel:  BIOS-e820: 0000000000100000 - 000000001f7d3800 (usable)
Aug  4 13:50:15 holmes86 kernel:  BIOS-e820: 000000001f7d3800 - 0000000020000000 (reserved)
Aug  4 13:50:15 holmes86 kernel:  BIOS-e820: 00000000e0000000 - 00000000f0007000 (reserved)
Aug  4 13:50:15 holmes86 kernel:  BIOS-e820: 00000000f0008000 - 00000000f000c000 (reserved)
从上面的信息可以看出,查看 /proc/8663/fd/15 就可以得到所要恢复的数据。如果可以通过文件描述符查看相应的数据,那么就可以使用 I/O 重定向将其复制到文件中,如: 
cat /proc/1283/fd/2 > /var/log/messages 
对于许多应用程序,尤其是日志文件和数据库,这种恢复删除文件的方法非常有用。 

你可能感兴趣的:(上线踩坑引发的处理方式---lsof,strace)