Performance

根據文件句柄號獲取文件名

  • October 13, 2015

我正在strace -p 1234 -o strace.out一個系統上執行,該系統懷疑該程序1234執行不良。我看到這些reads需要將近一分鐘才能完成:

read(11, "\0\335\0\0\6\0\0\0\0\0\20\27m\3708\341\246\247\365\374\334\0274-X#\21!xs\10\25"..., 2064) = 221

我希望找出文件名與文件句柄相關聯11

更新

$ sudo strace -yp 3549 -o strace.out.2
strace: invalid option -- y
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

當您執行 Linux 時,您可以找到與您的 uid 擁有的任何程序(或所有,如果是 root)的任何文件描述符相關聯的文件名ls

例如:

ls -lF /proc/1234/fd/11

來自精美手冊:

  -y          Print paths associated with  file  descriptor  argu-
              ments

引用自:https://unix.stackexchange.com/questions/235928