3.3.3 Example: Creating a .h File From a dprime.d File

The next step is to create a .h file from the dprime.d file, as shown here:

# dtrace -h -s dprime.d

The dprime.h file that is created contains a reference to each of the probe points that are defined in the dprime.d file.

Next, in the application source file, primain.c, we add a reference to the #include "dprime.h" file and add the appropriate probe macros at the proper locations.

In the resulting primain.c file, the probe macros (shown in bold font for example purposes only) are easy to recognize, as they appear in uppercase letters:

#include <stdio.h>
#include "primelib.h"
#include "dprime.h"

/*
 * Nominal C program churning to provide a code base we might want to
 * instrument with D
*/

// Search for a divisor -- thereby proving composite value of the input.
int main()  {
  int targVal, divisor, factorA=0, factorB=0;

  printf( "Enter a positive target integer to test for prime status: " );
  scanf( "%d", &targVal );

  // Check that the user input is valid
  if( targVal < 2 ) {
    printf( "Invalid input value -- exiting now\n" );
    return -2;
  }
  if (PRIMEGET_QUERY_USERENTRY_ENABLED())
    PRIMEGET_QUERY_USERENTRY(targVal);

  // Search for a divisor using method and function A
  int lastCheck;
  lastCheck = findMaxCheck( targVal );
  printf( "%d highest value to check as divisor\n", lastCheck );
  if (PRIMEGET_QUERY_MAXCHECKVAL_ENABLED())
    PRIMEGET_QUERY_MAXCHECKVAL(lastCheck, targVal);

  factorA = seekFactorA( targVal, lastCheck );
  if (PRIMEGET_QUERY_FACTORRETURNA_ENABLED())
    PRIMEGET_QUERY_FACTORRETURNA(factorA, targVal);

  // Search for a divisor using method and function B
  factorB = seekFactorB( targVal );
 if (PRIMEGET_QUERY_FACTORRETURNB_ENABLED())
    PRIMEGET_QUERY_FACTORRETURNB(factorB, targVal);

  // Warn if the methods give different results
  if (factorA != factorB)
    printf( "%d does not equal %d! How can this be?\n", factorA, factorB );

  // Print results
  if( !factorA )
    printf( "%d is a prime number\n", targVal );
  else
    printf( "%d is not prime because there is a factor %d\n",
	    targVal, factorA );
  if (PRIMEGET_QUERY_FINAL_ENABLED())
    PRIMEGET_QUERY_FINAL();

  return 0;
}
Note

Any *_ENABLED() probe will translate into a truth value if the associated probe is enabled (some consumer is using it), and a false value if the associated probe is not enabled.

Before continuing, ensure that the probes are enabled and appear as the macros listed in the dprime.h file. See Testing if a Probe Is Enabled in the Oracle® Linux: DTrace Guide.

Note

Make sure to include any desired values in the macros, if they exist, so that the probe can also identify those values.

Next, you will need to modify the makefile file. For step-by-step instructions, See Building Applications With Probes in the Oracle® Linux: DTrace Guide.