Skip to content

Instantly share code, notes, and snippets.

@dhedlund
Created March 19, 2014 03:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhedlund/9635035 to your computer and use it in GitHub Desktop.
Save dhedlund/9635035 to your computer and use it in GitHub Desktop.
Dynamic Tracing with DTrace and SystemTap

class: center, middle

Dynamic Tracing with DTrace and SystemTap


DTrace

  • Originally developed by Sun Microsystems, now Oracle

  • Works with most UNIXes. Comes pre-installed on Solaris and Mac OS X

  • CDDL license, not compatible with the GPL

DTrace has been described as a tool that "allows you to ask arbitrary questions about what the system is doing, and get answers."


SystemTap

  • Only works with Linux

  • GPL license

  • Current project members include Red Hat, IBM, Hitachi, and Oracle.


Both...

  • Provide a similar feature set

  • Are loaded into the kernel as modules to compiled in

  • Must be run as root (or with special groups)

  • Use Kprobes to implant probes and register event handlers(?)


Kprobes

  • A probe is an automated breakpoint that is implanted dynamically in executing (kernel-space) modules without the need to modify their underlying source.

  • They are particularly advocated in production environments where the use of interactive debuggers is undesirable.

  • Probe event handlers run as extensions to the system breakpoint interrupt handler and are expected to have little or no dependence on system facilities.

  • Because of this design point, probes are able to be implanted in the most hostile environments without adversely skewing system performance.

https://sourceware.org/systemtap/kprobes/


Hello World

DTrace

# hello-world.d
BEGIN {
  trace("hello world\n");
  exit(0);
}

$ dtrace -s hello-world.d

SystemTap

# hello-world.stp
probe begin {
  print("hello world\n")
  exit()
}

$ stap hello-world.stp


Hello World

DTrace

$ dtrace -n 'BEGIN { trace("hello world\n"); exit(0) }'

SystemTap

$ stap -e 'probe begin { print("hello world\n"); exit() }'


They're Pretty Similar...

Probes

DTrace SystemTap
BEGIN begin
END end
syscall:::entry syscall.\*
syscall:::return syscall.\*.return
syscall::read:entry syscall.read
profile:::tick-10s timer.s(10)

They're Pretty Similar...

Built-in Variables

DTrace SystemTap
execname execname()
uid uid()
pid pid()
timestamp gettimeofday_ns()
arg0..N (custom variable: see `stap -L PROBE`)
$target target()

They're Pretty Similar...

Functions

DTrace SystemTap
stack() print_backtrace()
quantize() @hist_log()
lquantize() @hist_linear()
exit(status) exit()

Listing Syscall Probes

DTrace

$ dtrace -ln syscall:::entry

SystemTap

$ stap -l 'syscall.*'

syscall.accept
syscall.access
syscall.acct
syscall.add_key
syscall.adjtimex
syscall.alarm

class: center, middle

Comparative Examples


Return size of read() syscall

DTrace

$ dtrace -n 'syscall::read:return { @bytes = quantize(arg1); }'

SystemTap

$ stap -e 'global bytes; probe syscall.read.return { bytes <<< $return; } probe end { print(@hist_log(bytes)); }'


Count syscalls by process

DTrace

$ dtrace -n 'syscall:::entry { @[execname] = count(); }'

SystemTap

$ stap -e 'global ops; probe syscall.* { ops[execname()] <<< 1; }'


New procs w/ name and args

DTrace

$ dtrace -n 'proc:::exec-success { trace(curpsinfo->pr_psargs); }'

SystemTap

$ stap -e 'probe process.begin { printf("%s\n", cmdline_str()); }'


More Examples

SystemTap

Feature Comparison


SystemTap

Safety

User-space Tracing

The current iteration of SystemTap allows for a multitude of options when probing kernel-space events for a wide range of kernels. However, SystemTap's ability to probe user-space events is dependent on kernel support (the Utrace mechanism) that is unavailable in many kernels. Thus, only some kernel versions support user-space probing. At present, the developmental efforts of the SystemTap community are geared towards improving SystemTap's user-space probing capabilities.

https://sourceware.org/systemtap/wiki/utrace/arch/HowTo

But utrace is dead? http://stackoverflow.com/questions/12134041/is-utrace-project-dead


Other Tracing Tools


Resources


DTrace Issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment