1#!/usr/bin/perl
2#
3# errinfo - report on syscall failures and print errno error messages.
4#	    Written using Perl and DTrace (Solaris 10 03/05)
5#
6# When system calls fail, an errno variable is set to convay a meaningful
7# message to the end user - so long as the program does something with it
8# (eg, "ls" printing "No such file or directory"). This program fetches
9# and prints details for all syscall failures along with their message,
10# whether the failing program is already printing this info or not.
11#
12# 20-Apr-2006, ver 0.83
13#
14# USAGE:	errinfo [-ch] [-p PID] [-n name]
15#
16#		-c		# counts - aggregate style
17#		-p PID		# examine this PID only
18#		-n name		# examine processes with this name only
19#	eg,
20#		errinfo			# default output - snoop event style
21#		errinfo -n ssh		# examine "ssh" processes only
22#		errinfo -cn ssh		# examine "ssh" using counts
23#
24# FIELDS:
25#		EXEC		Program name (truncated)
26#		SYSCALL		System call name
27#		ERR		Value of errno
28#		DESC		Description of errno message
29#
30# SEE ALSO:	/usr/include/sys/errno.h
31#
32# COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
33#
34# CDDL HEADER START
35#
36#  The contents of this file are subject to the terms of the
37#  Common Development and Distribution License, Version 1.0 only
38#  (the "License").  You may not use this file except in compliance
39#  with the License.
40#
41#  You can obtain a copy of the license at Docs/cddl1.txt
42#  or http://www.opensolaris.org/os/licensing.
43#  See the License for the specific language governing permissions
44#  and limitations under the License.
45#
46# CDDL HEADER END
47#
48# Author: Brendan Gregg  [Sydney, Australia]
49#
50# 18-Apr-2005   Brendan Gregg   Created this.
51#
52
53use Getopt::Std;
54
55#
56#  Defaults
57#
58$FILTER = "";
59$COUNT = 0;
60
61#
62#  Command line arguments
63#
64&Usage() if $ARGV[0] eq "--help";
65getopts('ch:n:p:') || &Usage();
66&Usage() if $opt_h;
67$COUNT = 1 if $opt_c;
68$FILTER = "&& (execname == strstr(execname,\"$opt_n\")) || (\"$opt_n\" == strstr(\"$opt_n\",execname))" if defined $opt_n;
69$FILTER = "&& pid == $opt_p" if defined $opt_p;
70
71#
72#  Load errno descriptions
73#
74open(ERRNO,"/usr/include/sys/errno.h") || die "ERROR1: reading errno.h: $!\n";
75while (chomp($line = <ERRNO>)) {
76	next unless $line =~ /^#define/;
77	($errno,$desc) = $line =~ /^#define\s+\S+\s+(\d+)\s+\/\*(.*)\*\//;
78	$Errno{$errno} = $desc;
79}
80close ERRNO;
81
82#
83#  Declare DTrace script
84#
85 if ($COUNT) {		# aggregate style
86$dtrace = <<END;
87/usr/sbin/dtrace -n '
88	#pragma D option quiet
89	syscall:::return 
90	/errno != 0 && pid != \$pid $FILTER/ 
91	{ 
92		\@Errs[execname, probefunc, errno] = count(); 
93	}
94	dtrace:::END {
95		printa("%s %s %d %\@d\\n", \@Errs);
96	}'
97END
98 } else {		# snoop style
99$dtrace = <<END; 
100/usr/sbin/dtrace -n '
101	#pragma D option quiet
102	#pragma D option switchrate=5hz
103	syscall:::return 
104	/errno != 0 && pid != \$pid $FILTER/ 
105	{ 
106		printf("%s %s %d\\n", execname, probefunc, errno); 
107	}'
108END
109 }
110
111#
112#  Cleanup on signals
113#
114$SIG{INT} = \&Cleanup_Signal;    # Ctrl-C
115$SIG{QUIT} = \&Cleanup_Signal;   # Ctrl-\
116$SIG{TERM} = \&Cleanup_Signal;   # TERM
117
118#
119#  Run DTrace, process output
120#
121
122if ($COUNT) {
123	print STDERR "Tracing... Hit Ctrl-C to end.\n";
124	$header = 1;
125} else {
126	printf("%16s %16s %4s  %s\n","EXEC","SYSCALL","ERR","DESC");
127}
128
129### Open DTrace
130open(DTRACE,"$dtrace |") || die "ERROR2: Can't start dtrace (perms?): $!\n";
131
132### Process DTrace output
133while (chomp($line = <DTRACE>)) {
134
135	### Print count header
136	if ($COUNT && $header) {
137		printf("\n%16s %16s %4s %6s  %s\n",
138		 "EXEC","SYSCALL","ERR","COUNT","DESC");
139		$header = 0;
140	}
141
142	### Split data
143	($execname,$syscall,$errno,$counts) = split(' ',$line);
144	next if $errno eq "";
145
146	### Fetch errno description
147	$desc = $Errno{$errno};
148
149	### Print output line
150	if ($COUNT) {
151		printf("%16s %16s %4d %6d %s\n",
152		 $execname,$syscall,$errno,$counts,$desc);
153	} else {
154		printf("%16s %16s %4d %s\n",$execname,$syscall,$errno,$desc);
155	}
156}
157close(DTRACE);
158
159#
160#  Triggered by signals
161#
162sub Cleanup_Signal {
163}
164
165#
166#  Usage message
167#
168sub Usage {
169        print STDERR "USAGE: errinfo [-ch] [-p PID] [-n name]\n";
170	print STDERR <<ENDUSAGE;
171     eg,
172       errinfo       # default output - snoop event style
173          -c         # counts - aggregate style
174          -p 871     # examine PID 871 only
175          -n ssh     # examine processes with the name "ssh" only
176          -cn ssh    # examine "ssh" using counts
177ENDUSAGE
178        exit(1);
179}
180