7.4 Using a Speculation

To use a speculation, an identifier that is returned from speculation must be passed to the speculate function in a clause prior to any data-recording actions. All subsequent data-recording actions in a clause containing a speculate are speculatively traced. The D compiler generates a compile-time error if a call to speculate follows data-recording actions in a D probe clause. Therefore, clauses might contain speculative tracing or non-speculative tracing requests, but not both.

Aggregating actions, destructive actions, and the exit action may never be speculative. Any attempt to take one of these actions in a clause containing a speculate results in a compile-time error. Also, a speculate may not follow a speculate. Only one speculation is permitted per clause. A clause that contains only a speculate speculatively traces the default action, which is defined to trace only the enabled probe ID. See Chapter 4, Actions and Subroutines for a description of the default action.

Typically, you assign the result of speculation to a thread-local variable and then use that variable as a subsequent predicate to other probes, as well as an argument to speculate, as shown in the following example:

  self->spec = speculation();

  printf("this is speculative");