strace
プロセスとシステム
書き方
strace [-f] -p <pid>プロセスがカーネルに投げるシステムコールを一つずつ表示し、どこで止まっているかを見せます。出力は stderr へ行くので `2>&1` で回し、ptrace の権限(通常は sudo)が必要で、Linux 専用です(macOS は dtruss)。
よく使うオプション
| オプション | 意味 |
|---|---|
| -p <pid> | Attach to a process that is already running. |
| -f | Follow child processes and threads too. |
| -e trace=openat,connect | Only the syscalls you care about. |
| -s 200 | Show 200 characters of each string instead of the default 32. |
| -o <file> | Write to a file, since output goes to stderr. |
| -c | A summary count per syscall instead of every line. |
| -tt | Timestamps with microseconds, to see where the time went. |
例
strace -f -e trace=openat ./app 2>&1 | grep configFind which config file it actually reads.
sudo strace -p 1234 -s 200See what a stuck process is waiting on.
sudo dtruss -p 1234The macOS counterpart; needs elevated privileges and often SIP changes.
何が動いていて何が資源を食っているかを見ます。止める前に何なのかを確かめるのが順番です。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. strace は何をしますか。
プロセスがカーネルに投げるシステムコールを一つずつ表示し、どこで止まっているかを見せます。出力は stderr へ行くので `2>&1` で回し、ptrace の権限(通常は sudo)が必要で、Linux 専用です(macOS は dtruss)。
Q. どう打ちますか。
strace [-f] -p <pid> — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは7個です。全部は man strace にあります。このコマンドはプロセスとシステムの仲間です。
関連するコマンド
man ページ: man strace