6.1 printf Action

The printf action combines the ability to trace data, as if by the trace function, but with the ability to output the data and other text in a specific format that you describe. The printf function directs DTrace to trace the data associated with each argument after the first argument and then 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 a % 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's printf() function, DTrace's printf function is a built-in function that is recognized by the D compiler. The D compiler provides several useful services for the DTrace printf function that are not found in printf(), including the following:

  • 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 is aware of 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.

To implement these features, you must specify the format string in the DTrace printf function as a string constant in your D program. Format strings cannot be dynamic variables of type string.