1、安装strace工具

[root@localhost bin]# yum -y install strace
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package strace.x86_64 0:4.8-11.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================================
 Package                                    Arch                                       Version                                        Repository                                Size
=====================================================================================================================================================================================
Installing:
 strace                                     x86_64                                     4.8-11.el6                                     base                                     262 k

Transaction Summary
=====================================================================================================================================================================================
Install       1 Package(s)

Total download size: 262 k
Installed size: 946 k
Downloading Packages:
strace-4.8-11.el6.x86_64.rpm                                                                                                                                  | 262 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : strace-4.8-11.el6.x86_64                                                                                                                                          1/1 
  Verifying  : strace-4.8-11.el6.x86_64                                                                                                                                          1/1 

Installed:
  strace.x86_64 0:4.8-11.el6                                                                                                                                                         

Complete!



2、常用用法

strace -p pid 查看进程当前调用栈,查死循环或者卡顿时极为有用
strace -eopen /usr/local/kk-mail/service/dovecot/sbin/dovecot  查看进程当前打开了哪些文件

[root@localhost bin]# strace -eopen /usr/local/kk-mail/service/dovecot/sbin/dovecot
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/tls/x86_64/libdovecot.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/tls/libdovecot.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/x86_64/libdovecot.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/libdovecot.so.0", O_RDONLY) = 3
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib64/libc.so.6", O_RDONLY)      = 3
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/libdl.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/libdl.so.2", O_RDONLY)     = 3
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/librt.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/librt.so.1", O_RDONLY)     = 3
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/libpthread.so.0", O_RDONLY) = 3
open("/dev/urandom", O_RDONLY)          = 3
open("/dev/null", O_WRONLY)             = 4
open("/etc/nsswitch.conf", O_RDONLY)    = 7
open("/usr/local/kk-mail/service/dovecot/lib/dovecot/libnss_files.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 7
open("/lib64/libnss_files.so.2", O_RDONLY) = 7
open("/etc/passwd", O_RDONLY|O_CLOEXEC) = 7
open("/etc/passwd", O_RDONLY|O_CLOEXEC) = 7
open("/etc/passwd", O_RDONLY|O_CLOEXEC) = 7
Warning: fd limit (ulimit -n) is lower than required under max. load (1024 < 65535), because of default_client_limit
open("/etc/localtime", O_RDONLY)        = 8
open("/usr/local/kk-mail/data/dovecot/run/dovecot/master.pid", O_RDONLY) = 8
Fatal: Dovecot is already running with PID 11731 (read from /usr/local/kk-mail/data/dovecot/run/dovecot/master.pid)
+++ exited with 89 +++