Unless I am missing something I cannot find a way to do a condtional check and handle it when a dtrace script is written with macros $1, $2 being passed to it, etc... In other words I have a dtrace script that can have parameters passed to it. Lets say $1 and $2 for example. I am trying to verity that there is or is not a value for $1 or $2 and adjust accordingly. I am finding that if use macros as parameter and specially use $1, $2 etc., in the D script it will fail regardless. Unless I am missing something the only way to handle this would be a wrapper script around the D script to handle conditional checks on parameters, etc. example.d 5 5 I am trying to validate that there is a value for $1 and $2, if so continue. it not set a default value and continue. Am I off base or can this be done using dtrace or do I indeed have to have a wrapper script? Bob This message posted from opensolaris.org
Hey Bob,> Unless I am missing something I cannot find a way to do a condtional check and handle it when a dtrace script is written with macros $1, $2 being passed to it, etc... > > In other words I have a dtrace script that can have parameters passed to it. Lets say $1 and $2 for example. I am trying to verity that there is or is not a value for $1 or $2 and adjust accordingly. > > I am finding that if use macros as parameter and specially use $1, $2 etc., in the D script it will fail regardless. Unless I am missing something the only way to handle this would be a wrapper script around the D script to handle conditional checks on parameters, etc. > > example.d 5 5 > I am trying to validate that there is a value for $1 and $2, if so continue. it not set a default value and continue.You can do this by using the "defaultargs" option (via either "-x" or "#pragma D option"). When "defaultargs" is set, any non-present argument will have the value 0. You can use this together with the question mark operator to effect default options values, e.g.: ---8<--- defaultargs.d ---8<--- #pragma D option defaultargs #pragma D option quiet BEGIN { opt_foo = $1 ? $1 : 10; printf("value of opt_foo is %d\n", opt_foo); exit(0); } ---8<--- defaultargs.d ---8<--- # ./defaultargs.d value of opt_foo is 10 # ./defaultargs.d 123 value of opt_foo is 123 # No one is pretending that this is elegant, but it''s better than nothing...> Am I off base or can this be done using dtrace or do I indeed have to have a wrapper script?It depends on how sophisticated your options processing is. For really simple stuff, "defaultargs" may suffice. If it becomes more complicated, you will need the wrapper script. We gave serious thought to allowing argument processing in D, but it becomes an absolute mess as soon as you start wanting either graceful error handling and/or options arguments that are already taken by dtrace(1M). We ultimately decided that there would be so much weirdness that there wouldn''t be much added value -- and that a wrapper script was the right way to achieve sophisticated options processing. (By the way, when I say "we", I mean the DTrace team in conversation with Brendan Gregg, who was the source of the "defaultargs" RFE -- and of most of the demand for options processing in D.) - Bryan -------------------------------------------------------------------------- Bryan Cantrill, Solaris Kernel Development. http://blogs.sun.com/bmc
G''Day, On Tue, 29 Nov 2005, Bryan Cantrill wrote: [...]> opt_foo = $1 ? $1 : 10;I had to laugh the first time I wrote, interval = $1 ? $1 : 1; yeah, I should have commented that code, interval = $1 ? $1 : 1; /* $1 || 1 */ :-) [...]> > Am I off base or can this be done using dtrace or do I indeed have to have a wrapper script? > > It depends on how sophisticated your options processing is. For really > simple stuff, "defaultargs" may suffice. If it becomes more complicated, > you will need the wrapper script. We gave serious thought to allowing > argument processing in D, but it becomes an absolute mess as soon as you > start wanting either graceful error handling and/or options arguments that > are already taken by dtrace(1M). We ultimately decided that there would > be so much weirdness that there wouldn''t be much added value -- and that > a wrapper script was the right way to achieve sophisticated options > processing. (By the way, when I say "we", I mean the DTrace team in > conversation with Brendan Gregg, who was the source of the "defaultargs" > RFE -- and of most of the demand for options processing in D.)Wrapping scripts is really fine, it gives you a quick and well understood way to process arguments which are then handed to DTrace. In fact, if there are argument errors the shell can print a usage message immediatly and quit, without having to invoke DTrace (which can take a few seconds if argument processing was done in a BEGIN statement). Once you wrap something in shell/Perl it''s tempting to format the output, read input as a coprocess, etc. That''s when things can get nasty, you can end up with a mangled Perl/DTrace monster. I''ve tried to avoid this. (See http://www.brendangregg.com/DTrace/prustat for an example of a script monster, it''s scary - it even has a heartbeat (I''m not making that up!)). I aim to have an obvious block of shell code to process arguments followed by the DTrace script. See tools like "execsnoop" from the DTraceToolkit as an example. It''s written in a way that if D ever did do getopts in the future (not that it really needs to), it would be little work to convert the shell code to 100% DTrace. cheers, Brendan