Stathis Kamperis
2009-Sep-12 19:07 UTC
[dtrace-discuss] Pass argument to dtrace script for use as predicate
Greetings to everyone. I wrote a script to calculate how much time is spent on the system calls originating from a specific executable: #!/usr/sbin/dtrace -s /* * Generate table statistics on how much time is spent on every system call, * for a given program (its name is assigned to $1). */ #pragma D option quiet syscall:::entry /execname == $1/ { self->sstart = timestamp; } syscall:::return /execname == $1/ { @timestats[probefunc] = avg(timestamp - self->sstart); @cntstats[probefunc] = count(); self->sstart = 0; } tick-1sec { printf("%20s | %20s | %5s\n", "Function", "<ns> over 1-sec period", "Count"); printa("%20s %22 at d %6 at d\n", @timestats, @cntstats); printf("\n"); } Here''s the deal: 1. If I leave $1 without double quotes " and the execname I pass has a dot in it, it (accidentally?) works ok: $ pfexec ./temp.sh ... In another terminal: $ pfexec ./lala.d temp.sh Function | <ns> over 1-sec period | Count getgid 873 2 getuid 981 2 umask 1032 2 sysconfig 1077 9 getrlimit 1279 1 llseek 1290 4 gtime 1365 1 getpid 1437 25 2. If I leave $1 without double quotes " and the execname doesn''t have a dot in it, it fails with: $ pfexec ./lala.d temp dtrace: failed to compile script ./lala.d: line 11: failed to resolve temp: Unknown variable name 3. If I surround $1 with double quotes, it fails not matter what the supplied argument is: $ pfexec ./lala.d temp.sh dtrace: failed to compile script ./lala.d: line 31: extraneous argument ''temp.sh'' ($1 is not referenced) $ pfexec ./lala.d temp dtrace: failed to compile script ./lala.d: line 31: extraneous argument ''temp'' ($1 is not referenced) I guess that I''m wrong in all 3 cases, but just being lucky in 1). How could I pass an argument (possibly containing spaces) to dtrace script and use that as a predicate for execname ? Best regards, Stathis
Stathis Kamperis
2009-Sep-12 19:19 UTC
[dtrace-discuss] Pass argument to dtrace script for use as predicate
2009/9/12 Stathis Kamperis <ekamperi at gmail.com>:> 2. If I leave $1 without double quotes " and the execname doesn''t have > a dot in it, it fails with: > $ pfexec ./lala.d temp > dtrace: failed to compile script ./lala.d: line 11: failed to resolve > temp: Unknown variable nameOk, I got lucky with google this time: http://solaris.reys.net/passing-command-line-parameters-to-dtrace-scripts I have to write: $ pfexec ./lala.d ''"temp"'' for dtrace to treat it as string argument.> 3. If I surround $1 with double quotes, it fails not matter what the > supplied argument is: > $ pfexec ./lala.d temp.sh > dtrace: failed to compile script ./lala.d: line 31: extraneous > argument ''temp.sh'' ($1 is not referenced) > $ pfexec ./lala.d temp > dtrace: failed to compile script ./lala.d: line 31: extraneous > argument ''temp'' ($1 is not referencedI still can''t figure this out. Does "$1" inside a script make dtrace perceive it as a literal string ''$1'' ? Cheers, Stathis
Angelo Rajadurai
2009-Sep-12 20:02 UTC
[dtrace-discuss] Pass argument to dtrace script for use as predicate
For string arguments use $$1 -Angelo On Sep 12, 2009, at 3:07 PM, Stathis Kamperis wrote:> Greetings to everyone. > I wrote a script to calculate how much time is spent on the system > calls originating from a specific executable: > > #!/usr/sbin/dtrace -s > > /* > * Generate table statistics on how much time is spent on every > system call, > * for a given program (its name is assigned to $1). > */ > > #pragma D option quiet > > syscall:::entry > /execname == $1/ > { > self->sstart = timestamp; > } > > syscall:::return > /execname == $1/ > { > @timestats[probefunc] = avg(timestamp - self->sstart); > @cntstats[probefunc] = count(); > self->sstart = 0; > } > > tick-1sec > { > printf("%20s | %20s | %5s\n", "Function", "<ns> over 1-sec > period", > "Count"); > printa("%20s %22 at d %6 at d\n", @timestats, @cntstats); > printf("\n"); > } > > Here''s the deal: > > 1. If I leave $1 without double quotes " and the execname I pass has a > dot in it, it (accidentally?) works ok: > $ pfexec ./temp.sh > ... > > In another terminal: > $ pfexec ./lala.d temp.sh > > Function | <ns> over 1-sec period | Count > getgid 873 2 > getuid 981 2 > umask 1032 2 > sysconfig 1077 9 > getrlimit 1279 1 > llseek 1290 4 > gtime 1365 1 > getpid 1437 25 > > 2. If I leave $1 without double quotes " and the execname doesn''t have > a dot in it, it fails with: > $ pfexec ./lala.d temp > dtrace: failed to compile script ./lala.d: line 11: failed to resolve > temp: Unknown variable name > > 3. If I surround $1 with double quotes, it fails not matter what the > supplied argument is: > $ pfexec ./lala.d temp.sh > dtrace: failed to compile script ./lala.d: line 31: extraneous > argument ''temp.sh'' ($1 is not referenced) > $ pfexec ./lala.d temp > dtrace: failed to compile script ./lala.d: line 31: extraneous > argument ''temp'' ($1 is not referenced) > > > I guess that I''m wrong in all 3 cases, but just being lucky in 1). > How could I pass an argument (possibly containing spaces) to dtrace > script and use that as a predicate for execname ? > > Best regards, > Stathis > _______________________________________________ > dtrace-discuss mailing list > dtrace-discuss at opensolaris.org
Jim Mauro
2009-Sep-12 21:22 UTC
[dtrace-discuss] Pass argument to dtrace script for use as predicate
From http://wikis.sun.com/display/DTrace/Scripting; If you want your D macro arguments to be interpreted as string tokens even if they match the form of an integer or identifier, prefix the macro variable or argument name with two leading dollar signs (for example, $$1) to force the D compiler to interpret the argument value as if it were a string surrounded by double quotes. All the usual D string escape sequences (see Table 2?5) are expanded inside of any string macro arguments, regardless of whether they are referenced using the $arg or $$arg form of the macro. If the defaultargs option is set, unspecified arguments that are referenced with the $$arg form have the value of the empty string (""). If you''re passing a string, use $$1. /jim Stathis Kamperis wrote:> 2009/9/12 Stathis Kamperis <ekamperi at gmail.com>: > >> 2. If I leave $1 without double quotes " and the execname doesn''t have >> a dot in it, it fails with: >> $ pfexec ./lala.d temp >> dtrace: failed to compile script ./lala.d: line 11: failed to resolve >> temp: Unknown variable name >> > Ok, I got lucky with google this time: > http://solaris.reys.net/passing-command-line-parameters-to-dtrace-scripts > > I have to write: > $ pfexec ./lala.d ''"temp"'' > > for dtrace to treat it as string argument. > > >> 3. If I surround $1 with double quotes, it fails not matter what the >> supplied argument is: >> $ pfexec ./lala.d temp.sh >> dtrace: failed to compile script ./lala.d: line 31: extraneous >> argument ''temp.sh'' ($1 is not referenced) >> $ pfexec ./lala.d temp >> dtrace: failed to compile script ./lala.d: line 31: extraneous >> argument ''temp'' ($1 is not referenced >> > > I still can''t figure this out. Does "$1" inside a script make dtrace > perceive it as a literal string ''$1'' ? > > Cheers, > Stathis > _______________________________________________ > dtrace-discuss mailing list > dtrace-discuss at opensolaris.org >