findの-execでコマンドを起動 #2
id:ha-tan:20050418の続き。
gdbで追ってみた。以下のところでfork & execしている。
src/usr.bin/find/function.c 1.40
... 410 int 411 f_exec(plan, entry) 412 PLAN *plan; 413 FTSENT *entry; 414 { ... 427 /* don't mix output of command with find output */ 428 fflush(stdout); 429 fflush(stderr); 430 431 switch (pid = vfork()) { 432 case -1: 433 err(1, "vfork"); 434 /* NOTREACHED */ 435 case 0: 436 if (fchdir(dotfd)) { 437 warn("chdir"); 438 _exit(1); 439 } 440 execvp(plan->e_argv[0], plan->e_argv); 441 warn("%s", plan->e_argv[0]); 442 _exit(1); 443 } ...
id:ha-tan:20050418と同様の状況で、gdbを動かしてplan->e_argvの値を見てみる。
まず、findをデバッグオプション付きでコンパイルする。
$ cd /usr/src/usr.bin/find $ DBG=-ggdb make
set args dir -exec /usr/bin/grep hoge '{}' \; b function.c:431 commands silent p *(plan->p_un.ex._e_argv)@4 cont end run
実行する。
$ gdb -q -x run /usr/src/usr.bin/find/find Breakpoint 1 at 0x804a3a0: file function.c, line 431. $1 = {0xbfbfd681 "/usr/bin/grep", 0xbfbfd68f "hoge", 0x8052800 "dir", 0x0} $2 = {0xbfbfd681 "/usr/bin/grep", 0xbfbfd68f "hoge", 0x8052800 "dir/a.txt", 0x0} $3 = {0xbfbfd681 "/usr/bin/grep", 0xbfbfd68f "hoge", 0x8052800 "dir/b.txt", 0x0} $4 = {0xbfbfd681 "/usr/bin/grep", 0xbfbfd68f "hoge", 0x8052800 "dir/c.txt", 0x0}
やはり、4回呼ばれていることがわかる。