Languages
From DTraceBook
This chapter shows how DTrace can trace various commonly used programming languages, including Assembly, C, C++, Java, JavaScript, Perl, PHP, Python, Ruby, Shell and Tcl.
Contents |
Sample One-Liners
See the DTrace book for more one-liners.
C
Trace a specific function (eg, fopen()):
dtrace -n 'pid$target:libc:fopen:entry' -p PID
Trace function entry arguments (eg, fdopen()):
dtrace -n 'pid$target:libc:fdopen:entry { trace(arg0); }' -p PID
Trace function return value (eg, fclose()):
dtrace -n 'pid$target:libc:fclose:return { trace(arg1); }' -p PID
Count user stack traces for a function call (eg, fopen()):
dtrace -n 'pid$target:libc:fopen:entry { @[ustack()] = count(); }' -p PID
Count kernel stack traces for a function call (eg,arc_read()):
dtrace -n 'fbt::arc_read:entry { @[stack()] = count(); }'
Sample user stack trace at 101 Hertz, for processes named "example":
dtrace -n 'profile-101 /execname == "example"/ { @[ustack()] = count(); }'
Sample kernel function at 1001 Hertz:
dtrace -n 'profile-1001 /arg0/ { @[func(arg0)] = count(); }'
Java
Count Java events:
dtrace -Zn 'hotspot*::: { @[probename] = count(); }'
Profile Java stacks at 101 Hertz:
dtrace -Zn 'profile-101 /execname == "java"/ { @[jstack()] = count(); }'
JavaScript
Trace program execution showing filename and line number:
dtrace -n 'javascript*:::execute-start { printf("%s:%d", copyinstr(arg0), arg1); }'
Count function calls by function name:
dtrace -n 'javascript*:::function-entry { @[copyinstr(arg2)] = count(); }'
Perl
Trace subroutine calls:
dtrace -Zn 'perl*:::sub-entry { trace(copyinstr(arg0)); }'
Count subroutine calls:
dtrace -Zn 'perl*:::sub-entry { @[copyinstr(arg0)] = count(); }'
PHP
Trace program execution filename:
dtrace -Zn 'php*:::request-startup { trace(copyinstr(arg0)); }'
Count function calls by function name:
dtrace -Zn 'php*:::function-entry { @[copyinstr(arg0)] = count(); }'
Python
Trace function calls:
dtrace -Zn 'python*:::function-entry { trace(copyinstr(arg1)); }'
Profile Python stack traces at 123 Hertz:
dtrace -n 'profile-123 /pid == $target/ { @[jstack()] = count(); }' -p PID
Ruby
Trace method calls showing class and method:
dtrace -Zn 'ruby*:::function-entry { printf("%s::%s", copyinstr(arg0), copyinstr(arg1)); }'
Count method calls by method name:
dtrace -Zn 'ruby*:::function-entry { @[copyinstr(arg1)] = count(); }'
Bourne
Trace function calls showing function name:
dtrace -Zn 'sh*:::function-entry { trace(copyinstr(arg1)); }'
Tcl
Trace procedure calls showing procedure name:
dtrace -Zn 'tcl*:::proc-entry { trace(copyinstr(arg0)); }'
Scripts
- j_calls.d
- j_calltime.d
- j_flow.d
- j_thread.d
- js_calls.d
- js_calltime.d
- js_flowinfo.d
- php_calls.d
- php_flowinfo.d
- pl_calls.d
- pl_calltime.d
- pl_flowinfo.d
- pl_who.d
- py_calls.d
- py_calltime.d
- py_flowinfo.d
- py_who.d
- rb_calls.d
- rb_calltime.d
- rb_flowinfo.d
- rb_who.d
- sh_calls.d
- sh_flowinfo.d
- sh_who.d
- tcl_calls.d
- tcl_procflow.d
- tcl_who.d
Errata
Links
- pid provider links includes articles for the pid provider entry probe, entry arguments, return probe and arguments, instruction probes and overhead.