30
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Linux上でアクセスされているファイルをtraceしてみる

Last updated at Posted at 2014-04-15

背景

PHP初心者がPHPのデバッグをしていて、どのファイルがどんなタイミングで参照されているか知りたかったため。

自分の環境

  • Vagrant上のCentOS 6

一度だけやる準備

straceを導入

$ sudo yum -y install strace

トレースのたびに毎回やる準備

まず httpd のプロセスIDを調べる

$ ps -ef | grep http[d]
root      2394     1  0 Apr14 ?        00:00:17 /usr/sbin/httpd
apache    3984  2394  1 20:34 ?        00:00:00 /usr/sbin/httpd
apache    3985  2394  0 20:34 ?        00:00:00 /usr/sbin/httpd
apache    3986  2394  0 20:34 ?        00:00:00 /usr/sbin/httpd
()

こいつが親プロセス(親PIDが1でUSERがroot)

root      2394     1  0 Apr14 ?        00:00:17 /usr/sbin/httpd

ターミナルでstraceを起動

$ sudo strace -f -p 2394 -e trace=file

別のターミナルで子プロセスを作り直す(説明は省略。 strace の -f オプションを与えている理由に通じている。)

$ sudo service httpd reload
Reloading httpd:

あとはブラウザからapacheに接続すると、ファイルシステムに関するイベントがターミナル1にバンバン流れる。

結果をファイルに書き出したいときは:

$ sudo strace -f -p 2394 -e trace=file -o /tmp/trace.log

詳しくは man strace する

こんなログが取れる

CakePHPが起動していくさま

(略)
4046  stat("/var/www/html/app/webroot/index.php", {st_mode=S_IFREG|0644, st_size=3170, ...}) = 0
4046  stat("/var/www/html/app/webroot/index.php", {st_mode=S_IFREG|0644, st_size=3170, ...}) = 0
4046  stat("/var/www/html/app/webroot/index.php", {st_mode=S_IFREG|0644, st_size=3170, ...}) = 0
4046  getcwd("/", 4095)                 = 2
4046  chdir("/var/www/html/app/webroot") = 0
4046  lstat("/var/www/html/app/webroot/index.php", {st_mode=S_IFREG|0644, st_size=3170, ...}) = 0
4046  lstat("/var/www/html/app/webroot", {st_mode=S_IFDIR|0755, st_size=374, ...}) = 0
4046  lstat("/var/www/html/app", {st_mode=S_IFDIR|0755, st_size=612, ...}) = 0
4046  lstat("/var/www/html", {st_mode=S_IFDIR|0755, st_size=782, ...}) = 0
4046  lstat("/var/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
4046  lstat("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
4046  open("/var/www/html/app/webroot/index.php", O_RDONLY) = 14
4046  lstat("/var/www/html/lib/Cake/bootstrap.php", {st_mode=S_IFREG|0644, st_size=14354, ...}) = 0
4046  lstat("/var/www/html/lib/Cake", {st_mode=S_IFDIR|0755, st_size=782, ...}) = 0
4046  lstat("/var/www/html/lib", {st_mode=S_IFDIR|0755, st_size=102, ...}) = 0
4046  open("/var/www/html/lib/Cake/bootstrap.php", O_RDONLY) = 14
4046  lstat("/var/www/html/lib/Cake/basics.php", {st_mode=S_IFREG|0644, st_size=23183, ...}) = 0
4046  open("/var/www/html/lib/Cake/basics.php", O_RDONLY) = 14
4046  lstat("/var/www/html/lib/Cake/Core/App.php", {st_mode=S_IFREG|0644, st_size=28002, ...}) = 0
4046  lstat("/var/www/html/lib/Cake/Core", {st_mode=S_IFDIR|0755, st_size=204, ...}) = 0
4046  open("/var/www/html/lib/Cake/Core/App.php", O_RDONLY) = 14
(略)

関連

Macの場合はfs_usageを使うのがよいと思う。

30
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?