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# Find the MacOSX SDK path 73# 74$sdk_path = `xcrun --sdk macosx --show-sdk-path` or die "ERROR: trying to find the MacOSX SDK path: $?\n"; 75chomp($sdk_path); 76 77# 78# Load errno descriptions 79# 80open(ERRNO, "$sdk_path/usr/include/sys/errno.h") || die "ERROR1: reading errno.h: $!\n"; 81while (chomp($line = <ERRNO>)) { 82 next unless $line =~ /^#define/; 83 ($errno,$desc) = $line =~ /^#define\s+\S+\s+(\d+)\s+\/\*(.*)\*\//; 84 $Errno{$errno} = $desc; 85} 86close ERRNO; 87 88# 89# Declare DTrace script 90# 91 if ($COUNT) { # aggregate style 92$dtrace = <<END; 93/usr/sbin/dtrace -n ' 94 #pragma D option quiet 95 syscall:::return 96 /errno != 0 && pid != \$pid $FILTER/ 97 { 98 \@Errs[execname, probefunc, errno] = count(); 99 } 100 dtrace:::END { 101 printa("%s %s %d %\@d\\n", \@Errs); 102 }' 103END 104 } else { # snoop style 105$dtrace = <<END; 106/usr/sbin/dtrace -n ' 107 #pragma D option quiet 108 #pragma D option switchrate=5hz 109 syscall:::return 110 /errno != 0 && pid != \$pid $FILTER/ 111 { 112 printf("%s %s %d\\n", execname, probefunc, errno); 113 }' 114END 115 } 116 117# 118# Cleanup on signals 119# 120$SIG{INT} = \&Cleanup_Signal; # Ctrl-C 121$SIG{QUIT} = \&Cleanup_Signal; # Ctrl-\ 122$SIG{TERM} = \&Cleanup_Signal; # TERM 123 124# 125# Run DTrace, process output 126# 127 128if ($COUNT) { 129 print STDERR "Tracing... Hit Ctrl-C to end.\n"; 130 $header = 1; 131} else { 132 printf("%16s %16s %4s %s\n","EXEC","SYSCALL","ERR","DESC"); 133} 134 135### Open DTrace 136open(DTRACE,"$dtrace |") || die "ERROR2: Can't start dtrace (perms?): $!\n"; 137 138### Process DTrace output 139while (chomp($line = <DTRACE>)) { 140 141 ### Print count header 142 if ($COUNT && $header) { 143 printf("\n%16s %16s %4s %6s %s\n", 144 "EXEC","SYSCALL","ERR","COUNT","DESC"); 145 $header = 0; 146 } 147 148 ### Split data 149 ($execname,$syscall,$errno,$counts) = split(' ',$line); 150 next if $errno eq ""; 151 152 ### Fetch errno description 153 $desc = $Errno{$errno}; 154 155 ### Print output line 156 if ($COUNT) { 157 printf("%16s %16s %4d %6d %s\n", 158 $execname,$syscall,$errno,$counts,$desc); 159 } else { 160 printf("%16s %16s %4d %s\n",$execname,$syscall,$errno,$desc); 161 } 162} 163close(DTRACE); 164 165# 166# Triggered by signals 167# 168sub Cleanup_Signal { 169} 170 171# 172# Usage message 173# 174sub Usage { 175 print STDERR "USAGE: errinfo [-ch] [-p PID] [-n name]\n"; 176 print STDERR <<ENDUSAGE; 177 eg, 178 errinfo # default output - snoop event style 179 -c # counts - aggregate style 180 -p 871 # examine PID 871 only 181 -n ssh # examine processes with the name "ssh" only 182 -cn ssh # examine "ssh" using counts 183ENDUSAGE 184 exit(1); 185} 186