DTrace: $target macro variable

Macro variables are meant to make your life MUCH easier when scripting with DTrace. Names of such variables must begin with a $ sign, and DTrace will automatically replace all the macro variables with the appropriate values when parsing your script.

Full list of macro variables can be found here, I will only talk about $target variable this time.

$target specifies the PID of the process you associate with your DTrace script.

DTrace has two useful command line options which can be used not only when running a dtrace command, but also when running DTrace-scripts:

solaris# ./script.d -c xterm

This command asks DTrace to run a command which follows the -c option, and process the actual script.d only after this. Our macro variable, $target, will containt a PID of the started command we specified – xterm in this example.

solaris# ./script.d -p 654

This is a similar way of running a DTrace script, only we notify DTrace that we’d like to work with the process with PID of 654. This way of using DTrace is especially useful as you don’t have to start or restart an already running process. $target will contain the PID specified in the command line – 654 in this example.

$target may be used like this:

#!/usr/sbin/dtrace -s
#pragma D option quiet
syscall:::entry, syscall:::return
/pid == $target/
printf("%d/%d: %s:%sn", pid, tid, probefunc, probename);

If you execute this script, you’ll probably get an output like this:

solaris# ./script.d -c xterm
26525/1: munmap:entry
26525/1: munmap:return
26525/1: mmap:entry
26525/1: mmap:return
26525/1: setcontext:entry
26525/1: setcontext:return
26525/1: getrlimit:entry
26525/1: getrlimit:return
26525/1: getpid:entry
26525/1: getpid:return

Just a little bit later I’ll talk about aggregations – yet another one of the wonderful DTrace features, and then I’ll be able to give you more interesting and useful examples of DTrace scripting.


  1. […] DTrace for our investigation, we’d probably use the $target variable (I’ve already spoken about it in the past) or simply specify the process identifier (pid) in some predicates of our probes, thus […]

Speak Your Mind