1N/A#!/usr/bin/perl
1N/A#
1N/A# errinfo - report on syscall failures and print errno error messages.
1N/A# Written using Perl and DTrace (Solaris 10 03/05)
1N/A#
1N/A# When system calls fail, an errno variable is set to convay a meaningful
1N/A# message to the end user - so long as the program does something with it
1N/A# (eg, "ls" printing "No such file or directory"). This program fetches
1N/A# and prints details for all syscall failures along with their message,
1N/A# whether the failing program is already printing this info or not.
1N/A#
1N/A# $Id: errinfo 3 2007-08-01 10:50:08Z brendan $
1N/A#
1N/A# USAGE: errinfo [-ch] [-p PID] [-n name]
1N/A#
1N/A# -c # counts - aggregate style
1N/A# -p PID # examine this PID only
1N/A# -n name # examine processes with this name only
1N/A# eg,
1N/A# errinfo # default output - snoop event style
1N/A# errinfo -n ssh # examine "ssh" processes only
1N/A# errinfo -cn ssh # examine "ssh" using counts
1N/A#
1N/A# FIELDS:
1N/A# EXEC Program name (truncated)
1N/A# SYSCALL System call name
1N/A# ERR Value of errno
1N/A# DESC Description of errno message
1N/A#
1N/A# SEE ALSO: /usr/include/sys/errno.h
1N/A#
1N/A# COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License, Version 1.0 only
1N/A# (the "License"). You may not use this file except in compliance
1N/A# with the License.
1N/A#
1N/A# You can obtain a copy of the license at Docs/cddl1.txt
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A# Author: Brendan Gregg [Sydney, Australia]
1N/A#
1N/A# 18-Apr-2005 Brendan Gregg Created this.
1N/A# 20-Apr-2006 " " Last update.
1N/A#
1N/A
1N/Ause Getopt::Std;
1N/A
1N/A#
1N/A# Defaults
1N/A#
1N/A$FILTER = "";
1N/A$COUNT = 0;
1N/A
1N/A#
1N/A# Command line arguments
1N/A#
1N/A&Usage() if $ARGV[0] eq "--help";
1N/Agetopts('ch:n:p:') || &Usage();
1N/A&Usage() if $opt_h;
1N/A$COUNT = 1 if $opt_c;
1N/A$FILTER = "&& execname == \"$opt_n\"" if defined $opt_n;
1N/A$FILTER = "&& pid == $opt_p" if defined $opt_p;
1N/A
1N/A#
1N/A# Load errno descriptions
1N/A#
1N/Aopen(ERRNO,"/usr/include/sys/errno.h") || die "ERROR1: reading errno.h: $!\n";
1N/Awhile (chomp($line = <ERRNO>)) {
1N/A next unless $line =~ /^#define/;
1N/A ($errno,$desc) = $line =~ /^#define\s+\S+\s+(\d+)\s+\/\*(.*)\*\//;
1N/A $Errno{$errno} = $desc;
1N/A}
1N/Aclose ERRNO;
1N/A
1N/A#
1N/A# Declare DTrace script
1N/A#
1N/A if ($COUNT) { # aggregate style
1N/A$dtrace = <<END;
1N/A/usr/sbin/dtrace -n '
1N/A #pragma D option quiet
1N/A syscall:::return
1N/A /errno != 0 && pid != \$pid $FILTER/
1N/A {
1N/A \@Errs[execname, probefunc, errno] = count();
1N/A }
1N/A dtrace:::END {
1N/A printa("%s %s %d %\@d\\n", \@Errs);
1N/A }'
1N/AEND
1N/A } else { # snoop style
1N/A$dtrace = <<END;
1N/A/usr/sbin/dtrace -n '
1N/A #pragma D option quiet
1N/A #pragma D option switchrate=5hz
1N/A syscall:::return
1N/A /errno != 0 && pid != \$pid $FILTER/
1N/A {
1N/A printf("%s %s %d\\n", execname, probefunc, errno);
1N/A }'
1N/AEND
1N/A }
1N/A
1N/A#
1N/A# Cleanup on signals
1N/A#
1N/A$SIG{INT} = \&Cleanup_Signal; # Ctrl-C
1N/A$SIG{QUIT} = \&Cleanup_Signal; # Ctrl-\
1N/A$SIG{TERM} = \&Cleanup_Signal; # TERM
1N/A
1N/A#
1N/A# Run DTrace, process output
1N/A#
1N/A
1N/Aif ($COUNT) {
1N/A print STDERR "Tracing... Hit Ctrl-C to end.\n";
1N/A $header = 1;
1N/A} else {
1N/A printf("%16s %16s %4s %s\n","EXEC","SYSCALL","ERR","DESC");
1N/A}
1N/A
1N/A### Open DTrace
1N/Aopen(DTRACE,"$dtrace |") || die "ERROR2: Can't start dtrace (perms?): $!\n";
1N/A
1N/A### Process DTrace output
1N/Awhile (chomp($line = <DTRACE>)) {
1N/A
1N/A ### Print count header
1N/A if ($COUNT && $header) {
1N/A printf("\n%16s %16s %4s %6s %s\n",
1N/A "EXEC","SYSCALL","ERR","COUNT","DESC");
1N/A $header = 0;
1N/A }
1N/A
1N/A ### Split data
1N/A ($execname,$syscall,$errno,$counts) = split(' ',$line);
1N/A next if $errno eq "";
1N/A
1N/A ### Fetch errno description
1N/A $desc = $Errno{$errno};
1N/A
1N/A ### Print output line
1N/A if ($COUNT) {
1N/A printf("%16s %16s %4d %6d %s\n",
1N/A $execname,$syscall,$errno,$counts,$desc);
1N/A } else {
1N/A printf("%16s %16s %4d %s\n",$execname,$syscall,$errno,$desc);
1N/A }
1N/A}
1N/Aclose(DTRACE);
1N/A
1N/A#
1N/A# Triggered by signals
1N/A#
1N/Asub Cleanup_Signal {
1N/A}
1N/A
1N/A#
1N/A# Usage message
1N/A#
1N/Asub Usage {
1N/A print STDERR "USAGE: errinfo [-ch] [-p PID] [-n name]\n";
1N/A print STDERR <<ENDUSAGE;
1N/A eg,
1N/A errinfo # default output - snoop event style
1N/A -c # counts - aggregate style
1N/A -p 871 # examine PID 871 only
1N/A -n ssh # examine processes with the name "ssh" only
1N/A -cn ssh # examine "ssh" using counts
1N/AENDUSAGE
1N/A exit(1);
1N/A}