13.2.2 Adding Probes to Application Code

After you have defined your probes in a .d file, you then need to augment your source code to indicate the locations that should trigger your probes. Consider the following example C application source code:

void main_look(void)
{
  ...
  query = wait_for_new_query();
  process_query(query);
  ...
}

To add probes to an application, use the -h option to the dtrace command, which generates a header file based on the probe definitions. For example, the following command generates the header file myserv.h, which contains macro definitions corresponding to the probe definitions in myserv.d:

# dtrace -h -s myserv.d

This method is recommended, as the coding is easier to implement and understand. The method is also compatible with both C and C++. In addition, because the generated macros depend on the types that you define in the provider definition, the compiler can perform type checking on them.

For example, you can add a probe site by using the MYSERV_QUERY_RECEIVE macro that dtrace -h defines in myserv.h:

#include "myserv.h"
...
void main_look(void)
{
  ...
  query = wait_for_new_query();
  MYSERV_QUERY_RECEIVE(query->clientname, query->msg);
  process_query(query);
  ...
}

In the previous example, the name of the macro encodes both the provider name and the probe name.