Michal Pryc
2007-May-29 10:49 UTC
[dtrace-discuss] Clause-local Variables and two different probes - is this variable shared?
Hello, Recently I was trying some D-Trace script, and I am a little bit confused. In the Solaris Dynamic Tracing Guide it is written " Because the clauses are always executed in program order, and because clause-local variables are persistent across different clauses_ enabling the same probe_[...]" So the example that is shown in the guide (written below) should share "this->foo" clause-local variable within the tick-1sec probe, because this is one probe - that is correct. But, if I am adding at the end of the script any other probe like: proc:::exec-success{ printf("exec %d; foo is %d\n",me++ % 3, this->foo++);} than it seems that "this->foo" is shared across different probes, and this is confusing for me, seems it shouldn''t be shared. I know that declaration is at the beginning of the script and the name of the variable is the same, but even in that case, this variable should not be shared across different probes. int me; this int foo; tick-1sec { this->foo=(me %3 ==0) ? 10 : this->foo; printf("Clause 1 is number %d; foo is %d\n",me++ % 3, this->foo++); } tick-1sec { this->foo=(me %3 ==0) ? 20 : this->foo; printf("Clause 2 is number %d; foo is %d\n",me++ % 3, this->foo++); } tick-1sec { this->foo=(me %3 ==0) ? 30 : this->foo; printf("Clause 3 is number %d; foo is %d\n",me++ % 3, this->foo++); } -- best Michal Pryc
Michal Pryc
2007-May-29 11:48 UTC
[dtrace-discuss] Clause-local Variables and two different probes - is this variable shared?
Michal Pryc wrote:> Hello, > Recently I was trying some D-Trace script, and I am a little bit > confused. > In the Solaris Dynamic Tracing Guide it is written > " Because the clauses are always executed in program order, and > because clause-local variables are persistent across different > clauses_ enabling the same probe_[...]" > > So the example that is shown in the guide (written below) should share > "this->foo" clause-local variable within the tick-1sec probe, because > this is one probe - that is correct. > > But, if I am adding at the end of the script any other probe like: > proc:::exec-success{ printf("exec %d; foo is %d\n",me++ % 3, > this->foo++);} > than it seems that "this->foo" is shared across different probes, and > this is confusing for me, seems it shouldn''t be shared. I know that > declaration is at the beginning of the script and the name of the > variable is the same, but even in that case, this variable should not > be shared across different probes. >Ok, I think I''ve got the point, please correct me if I am wrong. The confusion was, because the value of the clause-local variable is not defined in the first clause executed for a specified probe, so in the other probe at the beginning it should be specified. I''ve tried that and it seems to be fine. -- best Michal Pryc
Chip Bennett
2007-May-29 12:11 UTC
[dtrace-discuss] Clause-local Variables and two different probes - is this variable shared?
That depends on what you mean by the "other" probe. If you mean "proc:::exec-success", no. this->foo in the tick-1sec clauses and this->foo in proc:::exec-success clause are different variables. The problem has to do with clause-local variables not being automatically initialized. You are correct that you have a problem with the clauses for tick-1sec, in that you don''t initialize this->foo. It''s first value is going to be whatever happens to be laying around in that memory location. The same thing happens in proc:::exec-success. Its copy of this->foo, if not initialized, will also contain whatever happens to be laying around in memory, which will often (but not always) be the value left over from tick-1sec. So even though you often see the same value for a clause-local variable in the clauses for two different probes, you can''t rely on it. You can only rely on the value being maintained for all of the clauses for one probe and only one firing of that probe. Chip Michal Pryc wrote:> Michal Pryc wrote: >> Hello, >> Recently I was trying some D-Trace script, and I am a little bit >> confused. >> In the Solaris Dynamic Tracing Guide it is written >> " Because the clauses are always executed in program order, and >> because clause-local variables are persistent across different >> clauses_ enabling the same probe_[...]" >> >> So the example that is shown in the guide (written below) should >> share "this->foo" clause-local variable within the tick-1sec probe, >> because this is one probe - that is correct. >> >> But, if I am adding at the end of the script any other probe like: >> proc:::exec-success{ printf("exec %d; foo is %d\n",me++ % 3, >> this->foo++);} >> than it seems that "this->foo" is shared across different probes, and >> this is confusing for me, seems it shouldn''t be shared. I know that >> declaration is at the beginning of the script and the name of the >> variable is the same, but even in that case, this variable should not >> be shared across different probes. >> > Ok, I think I''ve got the point, please correct me if I am wrong. > > The confusion was, because the value of the clause-local variable is > not defined in the first clause executed for a specified probe, so in > the other probe at the beginning it should be specified. > > I''ve tried that and it seems to be fine. >