1235368Sgnn#!/usr/bin/perl 2235368Sgnn# 3235368Sgnn# errinfo - report on syscall failures and print errno error messages. 4235368Sgnn# Written using Perl and DTrace (Solaris 10 03/05) 5235368Sgnn# 6235368Sgnn# When system calls fail, an errno variable is set to convay a meaningful 7235368Sgnn# message to the end user - so long as the program does something with it 8235368Sgnn# (eg, "ls" printing "No such file or directory"). This program fetches 9235368Sgnn# and prints details for all syscall failures along with their message, 10235368Sgnn# whether the failing program is already printing this info or not. 11235368Sgnn# 12235368Sgnn# $Id: errinfo 3 2007-08-01 10:50:08Z brendan $ 13235368Sgnn# 14235368Sgnn# USAGE: errinfo [-ch] [-p PID] [-n name] 15235368Sgnn# 16235368Sgnn# -c # counts - aggregate style 17235368Sgnn# -p PID # examine this PID only 18235368Sgnn# -n name # examine processes with this name only 19235368Sgnn# eg, 20235368Sgnn# errinfo # default output - snoop event style 21235368Sgnn# errinfo -n ssh # examine "ssh" processes only 22235368Sgnn# errinfo -cn ssh # examine "ssh" using counts 23235368Sgnn# 24235368Sgnn# FIELDS: 25235368Sgnn# EXEC Program name (truncated) 26235368Sgnn# SYSCALL System call name 27235368Sgnn# ERR Value of errno 28235368Sgnn# DESC Description of errno message 29235368Sgnn# 30235368Sgnn# SEE ALSO: /usr/include/sys/errno.h 31235368Sgnn# 32235368Sgnn# COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg. 33235368Sgnn# 34235368Sgnn# CDDL HEADER START 35235368Sgnn# 36235368Sgnn# The contents of this file are subject to the terms of the 37235368Sgnn# Common Development and Distribution License, Version 1.0 only 38235368Sgnn# (the "License"). You may not use this file except in compliance 39235368Sgnn# with the License. 40235368Sgnn# 41235368Sgnn# You can obtain a copy of the license at Docs/cddl1.txt 42235368Sgnn# or http://www.opensolaris.org/os/licensing. 43235368Sgnn# See the License for the specific language governing permissions 44235368Sgnn# and limitations under the License. 45235368Sgnn# 46235368Sgnn# CDDL HEADER END 47235368Sgnn# 48235368Sgnn# Author: Brendan Gregg [Sydney, Australia] 49235368Sgnn# 50235368Sgnn# 18-Apr-2005 Brendan Gregg Created this. 51235368Sgnn# 20-Apr-2006 " " Last update. 52235368Sgnn# 53235368Sgnn 54235368Sgnnuse Getopt::Std; 55235368Sgnn 56235368Sgnn# 57235368Sgnn# Defaults 58235368Sgnn# 59235368Sgnn$FILTER = ""; 60235368Sgnn$COUNT = 0; 61235368Sgnn 62235368Sgnn# 63235368Sgnn# Command line arguments 64235368Sgnn# 65235368Sgnn&Usage() if $ARGV[0] eq "--help"; 66235368Sgnngetopts('ch:n:p:') || &Usage(); 67235368Sgnn&Usage() if $opt_h; 68235368Sgnn$COUNT = 1 if $opt_c; 69235368Sgnn$FILTER = "&& execname == \"$opt_n\"" if defined $opt_n; 70235368Sgnn$FILTER = "&& pid == $opt_p" if defined $opt_p; 71235368Sgnn 72235368Sgnn# 73235368Sgnn# Load errno descriptions 74235368Sgnn# 75235368Sgnnopen(ERRNO,"/usr/include/sys/errno.h") || die "ERROR1: reading errno.h: $!\n"; 76235368Sgnnwhile (chomp($line = <ERRNO>)) { 77235368Sgnn next unless $line =~ /^#define/; 78235368Sgnn ($errno,$desc) = $line =~ /^#define\s+\S+\s+(\d+)\s+\/\*(.*)\*\//; 79235368Sgnn $Errno{$errno} = $desc; 80235368Sgnn} 81235368Sgnnclose ERRNO; 82235368Sgnn 83235368Sgnn# 84235368Sgnn# Declare DTrace script 85235368Sgnn# 86235368Sgnn if ($COUNT) { # aggregate style 87235368Sgnn$dtrace = <<END; 88235368Sgnn/usr/sbin/dtrace -n ' 89235368Sgnn #pragma D option quiet 90235368Sgnn syscall:::return 91235368Sgnn /errno != 0 && pid != \$pid $FILTER/ 92235368Sgnn { 93235368Sgnn \@Errs[execname, probefunc, errno] = count(); 94235368Sgnn } 95235368Sgnn dtrace:::END { 96235368Sgnn printa("%s %s %d %\@d\\n", \@Errs); 97235368Sgnn }' 98235368SgnnEND 99235368Sgnn } else { # snoop style 100235368Sgnn$dtrace = <<END; 101235368Sgnn/usr/sbin/dtrace -n ' 102235368Sgnn #pragma D option quiet 103235368Sgnn #pragma D option switchrate=5hz 104235368Sgnn syscall:::return 105235368Sgnn /errno != 0 && pid != \$pid $FILTER/ 106235368Sgnn { 107235368Sgnn printf("%s %s %d\\n", execname, probefunc, errno); 108235368Sgnn }' 109235368SgnnEND 110235368Sgnn } 111235368Sgnn 112235368Sgnn# 113235368Sgnn# Cleanup on signals 114235368Sgnn# 115235368Sgnn$SIG{INT} = \&Cleanup_Signal; # Ctrl-C 116235368Sgnn$SIG{QUIT} = \&Cleanup_Signal; # Ctrl-\ 117235368Sgnn$SIG{TERM} = \&Cleanup_Signal; # TERM 118235368Sgnn 119235368Sgnn# 120235368Sgnn# Run DTrace, process output 121235368Sgnn# 122235368Sgnn 123235368Sgnnif ($COUNT) { 124235368Sgnn print STDERR "Tracing... Hit Ctrl-C to end.\n"; 125235368Sgnn $header = 1; 126235368Sgnn} else { 127235368Sgnn printf("%16s %16s %4s %s\n","EXEC","SYSCALL","ERR","DESC"); 128235368Sgnn} 129235368Sgnn 130235368Sgnn### Open DTrace 131235368Sgnnopen(DTRACE,"$dtrace |") || die "ERROR2: Can't start dtrace (perms?): $!\n"; 132235368Sgnn 133235368Sgnn### Process DTrace output 134235368Sgnnwhile (chomp($line = <DTRACE>)) { 135235368Sgnn 136235368Sgnn ### Print count header 137235368Sgnn if ($COUNT && $header) { 138235368Sgnn printf("\n%16s %16s %4s %6s %s\n", 139235368Sgnn "EXEC","SYSCALL","ERR","COUNT","DESC"); 140235368Sgnn $header = 0; 141235368Sgnn } 142235368Sgnn 143235368Sgnn ### Split data 144235368Sgnn ($execname,$syscall,$errno,$counts) = split(' ',$line); 145235368Sgnn next if $errno eq ""; 146235368Sgnn 147235368Sgnn ### Fetch errno description 148235368Sgnn $desc = $Errno{$errno}; 149235368Sgnn 150235368Sgnn ### Print output line 151235368Sgnn if ($COUNT) { 152235368Sgnn printf("%16s %16s %4d %6d %s\n", 153235368Sgnn $execname,$syscall,$errno,$counts,$desc); 154235368Sgnn } else { 155235368Sgnn printf("%16s %16s %4d %s\n",$execname,$syscall,$errno,$desc); 156235368Sgnn } 157235368Sgnn} 158235368Sgnnclose(DTRACE); 159235368Sgnn 160235368Sgnn# 161235368Sgnn# Triggered by signals 162235368Sgnn# 163235368Sgnnsub Cleanup_Signal { 164235368Sgnn} 165235368Sgnn 166235368Sgnn# 167235368Sgnn# Usage message 168235368Sgnn# 169235368Sgnnsub Usage { 170235368Sgnn print STDERR "USAGE: errinfo [-ch] [-p PID] [-n name]\n"; 171235368Sgnn print STDERR <<ENDUSAGE; 172235368Sgnn eg, 173235368Sgnn errinfo # default output - snoop event style 174235368Sgnn -c # counts - aggregate style 175235368Sgnn -p 871 # examine PID 871 only 176235368Sgnn -n ssh # examine processes with the name "ssh" only 177235368Sgnn -cn ssh # examine "ssh" using counts 178235368SgnnENDUSAGE 179235368Sgnn exit(1); 180235368Sgnn} 181