9.4 Target Process ID

Use the $target macro variable to create scripts that can be applied to a user process of interest that you select using the -p option or create using the -c option on the dtrace command line. The D programs specified on the command line or using the -s option are compiled after processes are created or grabbed, and the $target variable expands to the integer process ID of the first such process. For example, you could use the following D script to determine the distribution of system calls executed by a particular subject process:

syscall:::entry
/pid == $target/
{
  @[probefunc] = count();
}

To determine the number of system calls executed by the date command, save the script in the file syscall.d and execute the following command:

# dtrace -s syscall.d -c date
dtrace: script 'syscall.d' matched 296 probes
Tue Oct 16 15:12:07 BST 2012

  access                                                            1
  arch_prctl                                                        1
  clock_gettime                                                     1
  exit_group                                                        1
  getrlimit                                                         1
  lseek                                                             1
  rt_sigprocmask                                                    1
  set_robust_list                                                   1
  set_tid_address                                                   1
  write                                                             1
  futex                                                             2
  rt_sigaction                                                      2
  brk                                                               3
  munmap                                                            3
  read                                                              5
  open                                                              6
  mprotect                                                          7
  close                                                             8
  newfstat                                                          8
  mmap                                                             16