I explain a lot of why D scripts don't allow flow control constructs in 
footnote 1 of this article: http://www.laurustech.com/tech_paper.asp?ID=102.  
There is also some discussion of how to get the effects of flow control without 
real flow control.

However, I'll summarize here.  The reason for no looping is because probe 
clauses run as kernel threads with interrupts turned off.  If a loop became 
long, or even worse, infinite, your system would be hosed.

The reason for no if/else construct (and another reason for no loops) is 
because each probe clause must produce a fixed length trace record.  If a probe 
clause generates a 300 byte trace record the first time it is executed, it must 
always produce a 300 byte trace record.  Flow control statements could contain 
trace or printf actions that could be repeated a variable number of times and 
break the fixed length rule.  But as someone else pointed out, you can break up 
the clause into multiple clauses with different predicates.  (You could even do 
a kind of loop this way, as long as you know the maximum number of passes.  If 
the max was 100, you would have 100 clauses, and the predicates would control 
how many would actually execute on a particular pass.)

So the next thing you might ask is how come conditional operators (? :) are 
allowed?   Answer: You can't have a trace/printf actions inside an expression, 
so there's no harm.

Also, since strings can vary in length, you might ask how does that jive with a 
fixed length record?   Answer: The storage used by a string is actually fixed 
to the strings max length, which by default is 256.  So when you trace a 
string, it uses 256 byte in the trace record, no matter how long the current 
length.

So, here are the ways that I know to "simulate" flow control:

1) Break the probe clause into multiple clauses with different predicates.

2) Use the -C flag on the dtrace command line to invoke the C preprocessor, and 
use cpp conditional statements in your D script.

3) Use the conditional operator (logical-exp ? true-value : false-value)

4) Drive your D script using a shell script, calling the D script a 
multiple/variable number of times, and/or using shell variables in your D 
script.

Chip

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:dtrace-discuss-
> [EMAIL PROTECTED] On Behalf Of ??
> Sent: Wednesday, June 04, 2008 7:31 AM
> To: dtrace-discuss@opensolaris.org
> Subject: [dtrace-discuss] can i use if/else/for/while in dtrace script?
> 
> i am a newbie to solaris...
> 
> can i use if/else/for/while in dtrace script?
> 
> i see these are key words in dtrace, but when using it dtrace show
> syntax error...
> 
> i find through the doc and many examples , can't find same right
> syntax...
> 
> dtrace support this or not ?
> 
> thanks....
> 
> 
> --
> This message posted from opensolaris.org
> _______________________________________________
> dtrace-discuss mailing list
> dtrace-discuss@opensolaris.org

_______________________________________________
dtrace-discuss mailing list
dtrace-discuss@opensolaris.org

Reply via email to