summaryrefslogtreecommitdiffstats
path: root/stap_tutorial-1.0/examples/cmd-eval
diff options
context:
space:
mode:
Diffstat (limited to 'stap_tutorial-1.0/examples/cmd-eval')
-rwxr-xr-xstap_tutorial-1.0/examples/cmd-eval/cmd-eval-futexes.stp44
-rwxr-xr-xstap_tutorial-1.0/examples/cmd-eval/cmd-eval-opens.stp73
-rwxr-xr-xstap_tutorial-1.0/examples/cmd-eval/cmd-eval-rw.stp104
-rwxr-xr-xstap_tutorial-1.0/examples/cmd-eval/cmd-eval-syscalls.stp33
4 files changed, 254 insertions, 0 deletions
diff --git a/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-futexes.stp b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-futexes.stp
new file mode 100755
index 0000000..8f2b414
--- /dev/null
+++ b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-futexes.stp
@@ -0,0 +1,44 @@
+#!/usr/bin/env stap
+
+global target_pids, target_execs
+global thread_thislock, thread_blocktime
+global FUTEX_WAIT = 0, FUTEX_WAKE = 1
+global lock_waits
+
+probe process.exec_complete {
+ if (success && (ppid() in target_pids || pid() == target()))
+ target_pids[pid()] = kernel_string($filename)
+}
+
+probe syscall.futex {
+ if (!(pid() in target_pids)) next
+
+ if (op != FUTEX_WAIT) next # ignore originators of WAKE events
+
+ t = tid()
+ thread_thislock[t] = $uaddr
+ thread_blocktime[t] = gettimeofday_us()
+}
+
+probe syscall.futex.return {
+ if (!(pid() in target_pids)) next
+
+ if (op != FUTEX_WAIT) next # ignore originators of WAKE events
+
+ t = tid()
+ ts = thread_blocktime[t]
+ if (ts) {
+ elapsed = gettimeofday_us() - ts
+ lock_waits[pid(), thread_thislock[t]] <<< elapsed
+ delete thread_blocktime[t]
+ delete thread_thislock[t]
+ }
+}
+
+probe end {
+ foreach ([pid, lock] in lock_waits)
+ printf ("%s[%d] lock %p contended %5d times, %9d avg us\n",
+ target_pids[pid], pid, lock,
+ @count(lock_waits[pid, lock]),
+ @avg(lock_waits[pid, lock]))
+}
diff --git a/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-opens.stp b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-opens.stp
new file mode 100755
index 0000000..a040059
--- /dev/null
+++ b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-opens.stp
@@ -0,0 +1,73 @@
+#!/usr/bin/env stap
+
+global target_pids, target_execs
+global opens_by_file_success, opens_by_exec_success, total_opens_success
+global opens_by_file_fail, opens_by_exec_fail, total_opens_fail
+global closes_by_exec, total_closes
+global open_failure_by_type
+
+probe process.exec_complete {
+ if (success && (ppid() in target_pids || pid() == target())) {
+ target_pids[pid()] = kernel_string($filename)
+ target_execs[kernel_string($filename)] ++
+ }
+}
+
+probe syscall.open.return {
+ if (!(pid() in target_pids)) next
+
+ if ($return >= 0) {
+ opens_by_exec_success[target_pids[pid()]] ++
+ opens_by_file_success[user_string($filename)] ++
+ total_opens_success ++
+ } else {
+ opens_by_exec_fail[target_pids[pid()]] ++
+ opens_by_file_fail[user_string($filename)] ++
+ open_failure_by_type[retstr] ++
+ total_opens_fail ++
+ }
+}
+
+probe syscall.close {
+ if (!(pid() in target_pids)) next
+
+ closes_by_exec[target_pids[pid()]] ++
+ total_closes ++
+}
+
+probe end {
+ prt_limit = 10
+
+ printf ("Total opens: %d (%d succeeded, %d failed)\n",
+ total_opens_success + total_opens_fail,
+ total_opens_success, total_opens_fail)
+ printf ("Total closes: %d\n\n", total_closes)
+
+ printf ("# Binary Instances:\n")
+ foreach(name in target_execs)
+ printf("%d\t%s\n", target_execs[name], name)
+
+ printf("\n# Successful Opens by Binary:\n")
+ foreach (name in opens_by_exec_success-)
+ printf("%d\t%s\n", opens_by_exec_success[name], name)
+
+ printf("\n# Failed Opens by Binary:\n")
+ foreach (name in opens_by_exec_fail-)
+ printf("%d\t%s\n", opens_by_exec_fail[name], name)
+
+ printf("\n# Successful Opens by File (top %d):\n", prt_limit)
+ foreach (name in opens_by_file_success- limit prt_limit)
+ printf("%d\t%s\n", opens_by_file_success[name], name)
+
+ printf("\n# Failed Opens by File: (top %d)\n", prt_limit)
+ foreach (name in opens_by_file_fail- limit prt_limit)
+ printf("%d\t%s\n", opens_by_file_fail[name], name)
+
+ printf("\n# Closes by Binary:\n")
+ foreach (name in closes_by_exec-)
+ printf("%d\t%s\n", closes_by_exec[name], name)
+
+ printf("\n# Open Failures by Type:\n")
+ foreach (type in open_failure_by_type-)
+ printf("%d\t%s\n", open_failure_by_type[type], type)
+}
diff --git a/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-rw.stp b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-rw.stp
new file mode 100755
index 0000000..f5107ff
--- /dev/null
+++ b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-rw.stp
@@ -0,0 +1,104 @@
+#!/usr/bin/env stap
+
+global target_pids, target_execs
+global reads_by_file, reads_by_exec
+global writes_by_file, writes_by_exec
+global files, execs
+
+%{
+#include <linux/file.h>
+%}
+
+function fd2name:string (fd:long)
+%{
+ char *start = NULL, buf[MAXSTRINGLEN];
+ struct file *file = NULL;
+ struct vfsmount *mnt = NULL;
+ struct dentry *dentry = NULL;
+
+ file = fget(THIS->fd);
+ if (file) {
+ mnt = (struct vfsmount *) kread(&(file->f_path.mnt));
+ dentry = (struct dentry *) kread(&(file->f_path.dentry));
+ if (mnt && dentry)
+ start = d_path(dentry, mnt, buf, MAXSTRINGLEN);
+ if (start > 0)
+ strlcpy(THIS->__retvalue, start, MAXSTRINGLEN);
+ fput(file);
+ }
+ CATCH_DEREF_FAULT();
+%}
+
+probe process.exec_complete {
+ if (success && (ppid() in target_pids || pid() == target())) {
+ target_pids[pid()] = kernel_string($filename)
+ target_execs[kernel_string($filename)] ++
+ }
+}
+
+probe syscall.read.return, syscall.readv.return {
+ if (!(pid() in target_pids)) next
+
+ if ($return >= 0) {
+ fn = fd2name($fd)
+ exec = execname()
+ reads_by_file[fn] <<< $return
+ reads_by_exec[exec] <<< $return
+ files[fn] ++
+ execs[exec] ++
+ }
+}
+
+probe syscall.write.return, syscall.writev.return {
+ if (!(pid() in target_pids)) next
+
+ if ($return >= 0) {
+ fn = fd2name($fd)
+ exec = execname()
+ writes_by_file[fn] <<< $return
+ writes_by_exec[exec] <<< $return
+ files[fn] ++
+ execs[exec] ++
+ }
+}
+
+probe end {
+ foreach (file in reads_by_file) {
+ total_read_cnt += @count(reads_by_file[file])
+ total_read_bytes += @sum(reads_by_file[file])
+ }
+
+ foreach (file in writes_by_file) {
+ total_write_cnt += @count(writes_by_file[file])
+ total_write_bytes += @sum(writes_by_file[file])
+ }
+
+ printf ("Total reads : %d cnt, %d KB\n",
+ total_read_cnt, total_read_bytes/1024)
+ printf ("Total writes: %d cnt, %d KB\n",
+ total_write_cnt, total_write_bytes/1024)
+
+ printf ("\n# Binary Instances:\n")
+ foreach (name in target_execs)
+ printf("%d\t%s\n", target_execs[name], name)
+
+ printf ("\nIO by Process:\n")
+ foreach (name in execs-) {
+ rd_cnt = @count(reads_by_exec[name])
+ rd_sum = (rd_cnt > 0 ? @sum(reads_by_exec[name])/1024 : 0)
+ wr_cnt = @count(writes_by_exec[name])
+ wr_sum = (wr_cnt > 0 ? @sum(writes_by_exec[name])/1024 : 0)
+ printf("%5d rds, %5d KB rd, %5d wrs, %5d KB wr: %s\n",
+ rd_cnt, rd_sum, wr_cnt, wr_sum, name)
+ }
+
+ printf ("\nIO by File:\n")
+ foreach (name in files-) {
+ rd_cnt = @count(reads_by_file[name])
+ rd_sum = (rd_cnt > 0 ? @sum(reads_by_file[name])/1024 : 0)
+ wr_cnt = @count(writes_by_file[name])
+ wr_sum = (wr_cnt > 0 ? @sum(writes_by_file[name])/1024 : 0)
+ printf("%5d rds, %5d KB rd, %5d wrs, %5d KB wr: %s\n",
+ rd_cnt, rd_sum, wr_cnt, wr_sum, name)
+ }
+}
diff --git a/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-syscalls.stp b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-syscalls.stp
new file mode 100755
index 0000000..17e7d95
--- /dev/null
+++ b/stap_tutorial-1.0/examples/cmd-eval/cmd-eval-syscalls.stp
@@ -0,0 +1,33 @@
+#!/usr/bin/env stap
+
+global target_pids, target_execs
+global syscalls_per_exec, syscalls_by_exec, syscalls
+
+probe process.exec_complete {
+ if (success && (ppid() in target_pids || pid() == target())) {
+ target_pids[pid()] = kernel_string($filename)
+ target_execs[kernel_string($filename)] ++
+ }
+}
+
+probe syscall.* {
+ if (!(pid() in target_pids)) next
+
+ syscalls_by_exec[name, target_pids[pid()]] ++
+ syscalls_per_exec[target_pids[pid()]] ++
+ syscalls[name] ++
+}
+
+probe end {
+ printf("\nIndividual System Calls by Binary:\n" )
+ foreach ([name, exec] in syscalls_by_exec-)
+ printf("%8d %-20s\t%s\n", syscalls_by_exec[name, exec], name, exec)
+
+ printf("\nTotal System Calls by Binary:\n" )
+ foreach (exec in syscalls_per_exec-)
+ printf("%8d %-20s\n", syscalls_per_exec[exec], exec)
+
+ printf("\nSystem Calls over all:\n")
+ foreach (name in syscalls-)
+ printf("%8d %-20s\n", syscalls[name], name)
+}