Interpreter Files
Similar to shell utilities such as awk
and perl
, dtrace
can be used to create executable interpreter files. An interpreter file begins with a line of the form:
#! pathname [arg]
In the preceding example, path name is the path of the interpreter and arg is a single optional argument. When an interpreter file is executed, the system invokes the specified interpreter. If arg
was specified in the interpreter file, it is passed as an argument to the interpreter. The path to the interpreter file itself and any additional arguments specified when it was executed are then appended to the interpreter argument list. Therefore, you will always need to create DTrace interpreter files with at least these arguments:
#!/usr/sbin/dtrace -s
When the interpreter file is executed, the argument to the -s
option will be the path name of the interpreter file itself. The dtrace
utility reads, compiles, and executes the interpreter file as if you had typed the following command in your shell:
# dtrace -s interpreter-file
The following example shows how to create and execute a dtrace
interpreter file. Type the following D source code and save it in a file named interp.d
:
#!/usr/sbin/dtrace -s BEGIN { trace("hello"); exit(0); }
Set the execute permissions for the interp.d
file and execute it as follows:
# chmod a+rx interp.d # ./interp.d dtrace: script './interp.d' matched 1 probe CPU ID FUNCTION:NAME 1 1 :BEGIN hello #
Remember that the #!
directive must comprise the first two characters of your file with no intervening or preceding whitespace. The D compiler knows to automatically ignore this line when it processes the interpreter file.
The dtrace
utility uses getopt
() to process command-line options, so you can combine multiple options in your single interpreter argument. For more information, see the getopt
(3C) man page. For example, to add the -q
option to the preceding example make the following change to the interpreter directive:
#!/usr/sbin/dtrace -qs
If you specify multiple option letters, the -s
option must always end the list of boolean options so that the next argument (the interpreter file name) is processed as the argument corresponding to the -s
option.
To specify more than one option that requires an argument in the interpreter file, use the #pragma D option
directive syntax. All of the dtrace
command-line options have #pragma
equivalents that you can use, as described in DTrace Options and Tunables.