Languages

From DTraceBook

Revision as of 07:48, 12 March 2012 by BrendanGregg (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

Errata

Links

  • pid provider links includes articles for the pid provider entry probe, entry arguments, return probe and arguments, instruction probes and overhead.
Personal tools