2.9 Displaying System Call Errors

The following D program, errno.d, displays the value of errno and the file name if an error occurs when using open() to open a file:

Example 2.21 errno.d: Display errno and the file name for failed open() calls

#!/usr/sbin/dtrace -qs

/* errno.d -- Display errno and the file name for failed open() calls */

syscall::open:entry
{
  self->filename = copyinstr(arg0);
}

syscall::open:return
/arg0 < 0/
{
  printf("errno = %-2d   file = %s\n", errno, self->filename);
}

If an error occurs in the open() system call, the return probe sets the arg0 argument to -1 and the value of the built-in errno variable indicates the nature of the error. We use a predicate to test the value of arg0. Alternatively, we could test whether the value of errno is greater than zero.

After saving this script to a file, and making the file executable, you can run it to display information about any failures of the open() system call that occur on the system:

# ./errno.d
errno = 2    file = /var/ld/ld.config
errno = 4    file = /images/UnorderedList16.gif
^C