printf() Function

The printf() function combines the ability to trace data, as if by the trace() function, with the ability to output the data and other text in a specific format that you describe. The printf() function tells DTrace to trace the data associated with each argument after the first argument, and then to format the results using the rules described by the first printf() argument, known as a format string. The format string is a regular string that contains any number of format conversions, each beginning with the % character, that describe how to format the corresponding argument. The first conversion in the format string corresponds to the second printf() argument, the second conversion to the third argument, and so on. All of the text between conversions is printed verbatim. The character following the % conversion character describes the format to use for the corresponding argument.

Unlike the C library printf(), DTrace printf() is a built-in function that is recognized by the D compiler. The D compiler provides several useful services for DTrace printf() that are not found in the C library printf():

  • The D compiler compares the arguments to the conversions in the format string. If an argument's type is incompatible with the format conversion, the D compiler provides an error message explaining the problem.

  • The D compiler does not require the use of size prefixes with printf() format conversions. The C printf() routine requires that you indicate the size of arguments by adding prefixes such as %ld for long or %lld for long long. The D compiler knows the size and type of your arguments, so these prefixes are not required in your D printf() statements.

  • DTrace provides additional format characters that are useful for debugging and observability. For example, the %a format conversion can be used to print a pointer as a symbol name and offset.

The format string in the DTrace printf() function must be specified as a string constant in your D program. Format strings may not be dynamic variables of type string.