Tuesday, August 21, 2007

zsh DTrace Provider

Since there’s an effort underway to instrument various shells with dtrace probes, I thought I’d work on my shell of choice, zsh. Also, Brendan twisted my arm.

We’re trying to keep some uniformity between the shells, so I’m implementing the probes that Alan Hargreaves has documented.

So far, I’ve mostly finished the following probes:

builtin-entry
builtin-return
function-entry
function-return
script-done
script-start
And in simple use they look like this:
$ cat $tests/test_func_simple.zsh
#!./zsh

func()
{
echo "Hello from function"
return 1
}

func

$ dtrace -n 'zsh$target:::' -c $tests/test_func_simple.zsh
dtrace: description 'zsh$target:::' matched 8 probes
Hello from function
CPU ID FUNCTION:NAME
0 51903 zsh_main:script-start
0 51898 runshfunc:function-entry
0 51896 execbuiltin:builtin-entry
0 51897 execbuiltin:builtin-return
0 51896 execbuiltin:builtin-entry
0 51897 execbuiltin:builtin-return
0 51899 runshfunc:function-return
0 51902 zexit:script-done
dtrace: pid 16530 exited with status 1

And for something more elaborate:

$ cat $tests/basic_args.d

#pragma D option quiet

zsh$target:::builtin-entry, zsh$target:::function-entry
{
printf("%15s: %8s line: %d\n", probename, copyinstr(arg1), arg2);
}

zsh$target:::builtin-return, zsh$target:::function-return
{
printf("%15s: %8s ret: %d\n", probename, copyinstr(arg1), arg2);
}

zsh$target:::script-start
{
printf("Script %s starts\n", copyinstr(arg0));
}

zsh$target:::script-done
{
printf("Script %s done, return: %d\n", copyinstr(arg0), arg1);
}

$ dtrace -s $tests/basic_args.d -c $tests/test_func_simple.zsh
Hello from function
Script ../../tests/test_func_simple.zsh starts
function-entry: func line: 0
builtin-entry: echo line: 0
builtin-return: echo ret: 0
builtin-entry: return line: 0
builtin-return: return ret: 1
function-return: func ret: 1
Script ../../tests/test_func_simple.zsh done, return: 1


Note that the line numbers are all zero at the moment since I haven’t provided them yet.
There’s a few more probes to come and some cleanup to do, but I’m hoping to have a patch available soon.

1 comment:

Anonymous said...

Great work, Boyd!

I can't wait to start using this provider.

- Brendan