2

I'm trying to printf some Solaris kernel level information with the type of uint64_t (e.g. timestamp) using a DTrace script. How I can print uint64_t safely and precisely in my DTrace code.

I know the proper way of printing uint64_t in C is:

#define __STDC_FORMAT_MACROS
#include <sys/inttypes.h> //sys: Kernel level

uint64_t timestamp;
printf("%"PRIu64"\n", timestamp);

What's an equivalent in DTrace D? (%d and `%llu are imprecise and dangerous).

NOTE Not to be confused with other programming languages named "D" (a C++-like programming language developed by Walter Bright).

Community
  • 1
  • 1

1 Answers1

1

According to DTrace Wiki: Output Formatting:

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.

Moreover, the %d and %i formats in D will handle either signed or unsigned ints, so you can use any of %d, %i, or %u to print an unsigned integer value in base 10, all with the same results.

The OP asked in a comment about the %Y format. This can be used for a uint64_t value that contains the number of nanoseconds since the Jan 1 1970 epoch, e.g. the built-in walltimestamp variable. (timestamp and vtimestamp are not counted from that epoch.)

The uint64_t argument is interpreted to be the number of nanoseconds since 00:00 Universal Coordinated Time, January 1, 1970, and is printed in the following cftime(3C) form: "%Y %a %b %e %T %Z."

Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
  • Does it mean that I can safely `printf` a `uint64_t` variable using `%d` or I should print it via `%Y`? –  Jul 14 '15 at 21:57
  • Thanks. I've edited my answer to suggest which integer formats to use, as well as `%Y`. – Mark Plotnick Jul 15 '15 at 15:02