2015-01-03 3 views
1

나는 간단한 ST 스크립트를 가지고는 : 내가 stap test.stp -c 'cat test.stp'을 실행하면SystemTap의 스크립트 이상한 행동

global fds, counts 

probe syscall.open.return { 
     if ((pid() == target()) & ($return != -1)) { 
       printf("%s opened as %d\n", user_string($filename), $return) 
       fds[$return] = user_string($filename) 
     } 
} 

probe syscall.read.return, syscall.write.return { 
     if ((pid() == target()) & ($return > 0)) { 
       counts[fds[$fd]] += $return 
     } 
} 

probe end { 
     foreach (fname in counts+) { 
       count = counts[fname] 
       if (count > 1024) { 
         count = count/1024 
         bs = "Kb" 
       } else { 
         bs = "B" 
       } 
       printf("%s: %d %s\n", fname, count, bs) 
     } 
} 

내가 얻을 :이 거의 정확

global fds, counts 

probe syscall.open.return { 
    if ((pid() == target()) & ($return != -1)) { 
     printf("%s opened as %d\n", user_string($filename), $return) 
     fds[$return] = user_string($filename) 
    } 
} 

probe syscall.read.return, syscall.write.return { 
    if ((pid() == target()) & ($return > 0)) { 
     counts[fds[$fd]] += $return 
    } 
} 

probe end { 
    foreach (fname in counts+) { 
     count = counts[fname] 
     if (count > 1024) { 
      count = count/1024 
      bs = "Kb" 
     } else { 
      bs = "B" 
     } 
     printf("%s: %d %s\n", fname, count, bs) 
    } 
} 
/etc/ld.so.cache opened as 3 
/lib64/libc.so.6 opened as 3 
/usr/lib/locale/locale-archive opened as 3 
test.stp opened as 3 
test.stp: 541 B 
: 541 B 
/lib64/libc.so.6: 832 B 

.

/etc/ld.so.cache opened as 3 
/lib64/libtinfo.so.5 opened as 3 
/lib64/libdl.so.2 opened as 3 
/lib64/libc.so.6 opened as 3 
/dev/tty opened as 3 
/usr/lib/locale/locale-archive opened as 3 
/proc/meminfo opened as 3 
/usr/lib64/gconv/gconv-modules.cache opened as 3 
/lib64/libtinfo.so.5: 832 B 
/lib64/libdl.so.2: 832 B 
/lib64/libc.so.6: 832 B 
/proc/meminfo: 1024 B 

왜 내가 볼 수 없습니다 test.stp opened as 3초 경우 : 내가 stap test.stp -c 'cat test.stp > /dev/null'을 실행할 때하지만 난 뭔가 이상한거야?

나는 strace를 함께 몇 가지 테스트를 수행합니다

1) strace -e open -o trace cat test.stp :

open("/home/al/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/home/al/lib/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/home/al/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/home/al/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3 
open("test.stp", O_RDONLY)    = 3 
+++ exited with 0 +++ 

2) strace -e open -o trace cat test.stp > /dev/null :

open("/home/al/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/home/al/lib/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/home/al/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/home/al/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3 
open("test.stp", O_RDONLY)    = 3 
+++ exited with 0 +++ 

없음 특색있는가.

답변

1

cat test.stp > /dev/null은 쉘 (bash)을 실행해야하므로 출력 방향 재 지정을 >으로 지정해야하므로 bash 프로세스의 PID (고양이가 아님)는 target()으로 인식됩니다.

pid()이 (가) target() 인 자녀인지 확인할 수 있습니다. DTrace에는 progenyof()이라는 작업이 있습니다.이 작업은 현재 작업이 미리 정의 된 pid() 작업의 하위인지 여부를 결정합니다. SystemTap에는 아날로그가 없지만 다음과 같이 쉽게 재현 할 수 있습니다.

function progenyof(pid:long) { 
    parent = task_parent(task_current()); 
    task = pid2task(pid); 

    while(parent && task_pid(parent) > 0) { 
     if(task == parent) 
      return 1; 

     parent = task_parent(parent); 
    } 
}