Home | History | Annotate | Line # | Download | only in Bin
statsnoop revision 1.1
      1  1.1  christos #!/usr/bin/sh
      2  1.1  christos #
      3  1.1  christos # statsnoop - snoop file stats as they occur.
      4  1.1  christos #             Written using DTrace (Solaris 10 3/05).
      5  1.1  christos #
      6  1.1  christos # $Id: statsnoop,v 1.1 2015/09/30 22:01:07 christos Exp $
      7  1.1  christos #
      8  1.1  christos # USAGE:	statsnoop [-a|-A|-ceghlsvxZ] [-f pathname] [-t syscall]
      9  1.1  christos #		          [-n name] [-p PID]
     10  1.1  christos #
     11  1.1  christos #		statsnoop	# default output
     12  1.1  christos #
     13  1.1  christos #		-a		# print most data
     14  1.1  christos #		-A		# dump all data, space delimited
     15  1.1  christos #		-c		# print cwd of process
     16  1.1  christos #		-e		# print errno value
     17  1.1  christos #		-g		# print command arguments
     18  1.1  christos #		-l		# print syscall type
     19  1.1  christos #		-s		# print start time, us
     20  1.1  christos #		-v		# print start time, string
     21  1.1  christos #		-x		# only print failed stats
     22  1.1  christos #		-Z		# print zonename
     23  1.1  christos #		-f pathname	# file pathname to snoop
     24  1.1  christos #		-n name		# command name to snoop
     25  1.1  christos #		-p PID		# process ID to snoop
     26  1.1  christos #		-t syscall	# stat syscall to trace
     27  1.1  christos #	eg,
     28  1.1  christos #		statsnoop -v			# human readable timestamps
     29  1.1  christos #		statsnoop -S			# syscall type
     30  1.1  christos #		statsnoop -e			# see error codes
     31  1.1  christos #		statsnoop -f /etc/passwd	# snoop this file only
     32  1.1  christos # 	
     33  1.1  christos # FIELDS:
     34  1.1  christos #		ZONE		Zone name
     35  1.1  christos #		UID		User ID
     36  1.1  christos #		PID		Process ID
     37  1.1  christos #		PPID		Parent Process ID
     38  1.1  christos #		FD		file descriptor (-1 for error)
     39  1.1  christos #		ERR		errno value (see /usr/include/sys/errno.h)
     40  1.1  christos #		TYPE		syscall type
     41  1.1  christos #		CWD		current working directory of process
     42  1.1  christos #		PATH		pathname for file stat
     43  1.1  christos #		COMM		command name for the process
     44  1.1  christos #		ARGS		argument listing for the process
     45  1.1  christos #		TIME		timestamp for the stat event, us
     46  1.1  christos #		STRTIME		timestamp for the stat event, string
     47  1.1  christos #
     48  1.1  christos # SEE ALSO: truss, BSM auditing.
     49  1.1  christos #
     50  1.1  christos # COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
     51  1.1  christos #
     52  1.1  christos # CDDL HEADER START
     53  1.1  christos #
     54  1.1  christos #  The contents of this file are subject to the terms of the
     55  1.1  christos #  Common Development and Distribution License, Version 1.0 only
     56  1.1  christos #  (the "License").  You may not use this file except in compliance
     57  1.1  christos #  with the License.
     58  1.1  christos #
     59  1.1  christos #  You can obtain a copy of the license at Docs/cddl1.txt
     60  1.1  christos #  or http://www.opensolaris.org/os/licensing.
     61  1.1  christos #  See the License for the specific language governing permissions
     62  1.1  christos #  and limitations under the License.
     63  1.1  christos #
     64  1.1  christos # CDDL HEADER END
     65  1.1  christos #
     66  1.1  christos # Author: Brendan Gregg  [Sydney, Australia]
     67  1.1  christos #
     68  1.1  christos # 09-Sep-2007	Brendan Gregg	Created this.
     69  1.1  christos # 
     70  1.1  christos 
     71  1.1  christos 
     72  1.1  christos ##############################
     73  1.1  christos # --- Process Arguments ---
     74  1.1  christos #
     75  1.1  christos 
     76  1.1  christos ### Default variables
     77  1.1  christos opt_dump=0; opt_file=0; opt_time=0; opt_timestr=0; opt_args=0
     78  1.1  christos opt_zone=0; opt_cwd=0; opt_failonly=0; opt_err=0; filter=0; pathname=.
     79  1.1  christos opt_name=0; opt_pid=0; opt_type=0; opt_trace=0; pname=.; pid=0; trace=.
     80  1.1  christos 
     81  1.1  christos ### Process options
     82  1.1  christos while getopts aAcef:ghln:p:st:vxZ name
     83  1.1  christos do
     84  1.1  christos 	case $name in
     85  1.1  christos 	a)	opt_time=1; opt_timestr=1; opt_args=1; opt_err=1 ;;
     86  1.1  christos 	A)	opt_dump=1 ;;
     87  1.1  christos 	c)	opt_cwd=1 ;;
     88  1.1  christos 	e)	opt_err=1 ;;
     89  1.1  christos 	g)	opt_args=1 ;;
     90  1.1  christos 	f)	opt_file=1; pathname=$OPTARG ;;
     91  1.1  christos 	l)	opt_type=1 ;;
     92  1.1  christos 	n)	opt_name=1; pname=$OPTARG ;;
     93  1.1  christos 	p)	opt_pid=1; pid=$OPTARG ;;
     94  1.1  christos 	s)	opt_time=1 ;;
     95  1.1  christos 	t)	opt_trace=1; trace=$OPTARG ;;
     96  1.1  christos 	v)	opt_timestr=1 ;;
     97  1.1  christos 	x)	opt_failonly=1 ;;
     98  1.1  christos 	Z)	opt_zone=1 ;;
     99  1.1  christos 	h|?)	cat <<-END >&2
    100  1.1  christos 		USAGE: statsnoop [-a|-A|-ceghlsvxZ] [-f pathname] [-t syscall]
    101  1.1  christos 		                 [-n execname] [-p PID]
    102  1.1  christos 		       statsnoop                # default output
    103  1.1  christos 		                -a              # print most data
    104  1.1  christos 		                -A              # dump all data, space delimited
    105  1.1  christos 		                -c              # print cwd of process
    106  1.1  christos 		                -e              # print errno value
    107  1.1  christos 		                -g              # print command arguments
    108  1.1  christos 		                -l              # print syscall type
    109  1.1  christos 		                -s              # print start time, us
    110  1.1  christos 		                -v              # print start time, string
    111  1.1  christos 		                -x              # only print failed stats
    112  1.1  christos 		                -Z              # print zonename
    113  1.1  christos 		                -f pathname	# pathname name to snoop
    114  1.1  christos 		                -n name		# process name to snoop
    115  1.1  christos 		                -p PID		# process ID to snoop
    116  1.1  christos 		                -t syscall	# stat syscall to trace
    117  1.1  christos 		  eg,
    118  1.1  christos 		       statsnoop -v             # human readable timestamps
    119  1.1  christos 		       statsnoop -e             # see error codes
    120  1.1  christos 		       statsnoop -f /etc/motd   # snoop this file only
    121  1.1  christos 		END
    122  1.1  christos 		exit 1
    123  1.1  christos 	esac
    124  1.1  christos done
    125  1.1  christos 
    126  1.1  christos ### Option logic
    127  1.1  christos if [ $opt_dump -eq 1 ]; then
    128  1.1  christos 	opt_zone=0; opt_cwd=0; opt_time=0; opt_timestr=0; opt_type=0
    129  1.1  christos 	opt_args=2 
    130  1.1  christos fi
    131  1.1  christos if [ $opt_name -eq 1 -o $opt_pid -eq 1 -o $opt_trace -eq 1 ]; then
    132  1.1  christos 	filter=1
    133  1.1  christos fi
    134  1.1  christos 
    135  1.1  christos 
    136  1.1  christos #################################
    137  1.1  christos # --- Main Program, DTrace ---
    138  1.1  christos #
    139  1.1  christos /usr/sbin/dtrace -n '
    140  1.1  christos  /*
    141  1.1  christos   * Command line arguments
    142  1.1  christos   */
    143  1.1  christos  inline int OPT_dump 	 = '$opt_dump';
    144  1.1  christos  inline int OPT_file 	 = '$opt_file';
    145  1.1  christos  inline int OPT_args 	 = '$opt_args';
    146  1.1  christos  inline int OPT_cwd	 = '$opt_cwd';
    147  1.1  christos  inline int OPT_err	 = '$opt_err';
    148  1.1  christos  inline int OPT_zone 	 = '$opt_zone';
    149  1.1  christos  inline int OPT_time 	 = '$opt_time';
    150  1.1  christos  inline int OPT_timestr	 = '$opt_timestr';
    151  1.1  christos  inline int OPT_type	 = '$opt_type';
    152  1.1  christos  inline int OPT_failonly = '$opt_failonly';
    153  1.1  christos  inline int OPT_pid	 = '$opt_pid';
    154  1.1  christos  inline int OPT_name	 = '$opt_name';
    155  1.1  christos  inline int OPT_trace	 = '$opt_trace';
    156  1.1  christos  inline int FILTER 	 = '$filter';
    157  1.1  christos  inline int PID		 = '$pid';
    158  1.1  christos  inline string PATHNAME	 = "'$pathname'";
    159  1.1  christos  inline string NAME	 = "'$pname'";
    160  1.1  christos  inline string TRACE	 = "'$trace'";
    161  1.1  christos 
    162  1.1  christos  #pragma D option quiet
    163  1.1  christos  #pragma D option switchrate=10hz
    164  1.1  christos 
    165  1.1  christos  /*
    166  1.1  christos   * Print header
    167  1.1  christos   */
    168  1.1  christos  dtrace:::BEGIN 
    169  1.1  christos  {
    170  1.1  christos 	/* print optional headers */
    171  1.1  christos  	OPT_time ? printf("%-14s ", "TIME") : 1;
    172  1.1  christos  	OPT_timestr ? printf("%-20s ", "STRTIME") : 1;
    173  1.1  christos  	OPT_zone ? printf("%-10s ", "ZONE") : 1;
    174  1.1  christos 
    175  1.1  christos 	/* print dump headers */
    176  1.1  christos 	OPT_dump ? printf("%s %s %s %s %s %s %s %s %s %s %s", "ZONE",
    177  1.1  christos 	    "TIME", "UID", "PID", "PPID", "COMM", "FD", "ERR", "CWD", 
    178  1.1  christos 	    "PATH", "ARGS") : printf("%5s %6s ","UID","PID");
    179  1.1  christos 	
    180  1.1  christos 	/* print main headers */
    181  1.1  christos 	OPT_args == 0 ? printf("%-12s ", "COMM") : 1;
    182  1.1  christos 	OPT_dump == 0 ? printf("%3s ", "FD") : 1;
    183  1.1  christos 	OPT_err ? printf("%3s ", "ERR") : 1;
    184  1.1  christos 	OPT_cwd ? printf("%-20s ", "CWD") : 1;
    185  1.1  christos 	OPT_type ? printf("%-8s ", "TYPE") : 1;
    186  1.1  christos 	OPT_dump == 0 ? printf("%-20s ", "PATH") : 1;
    187  1.1  christos 	OPT_args == 1 ? printf("%s", "ARGS") : 1;
    188  1.1  christos 	printf("\n");
    189  1.1  christos  }
    190  1.1  christos 
    191  1.1  christos  /*
    192  1.1  christos   * Print stat event
    193  1.1  christos   */
    194  1.1  christos  syscall::stat:entry, syscall::stat64:entry, syscall::xstat:entry,
    195  1.1  christos  syscall::lstat:entry, syscall::lstat64:entry, syscall::lxstat:entry,
    196  1.1  christos  syscall::fstat:entry, syscall::fstat64:entry, syscall::fxstat:entry
    197  1.1  christos  {
    198  1.1  christos 	/* default is to trace unless filtering */
    199  1.1  christos 	self->ok = FILTER ? 0 : 1;
    200  1.1  christos 
    201  1.1  christos 	/* check each filter */
    202  1.1  christos 	(OPT_name == 1 && NAME == execname) ? self->ok = 1 : 1;
    203  1.1  christos 	(OPT_pid == 1 && PID == pid) ? self->ok = 1 : 1;
    204  1.1  christos 	(OPT_trace == 1 && TRACE == probefunc) ? self->ok = 1 : 1;
    205  1.1  christos  }
    206  1.1  christos 
    207  1.1  christos  syscall::stat:entry, syscall::stat64:entry,
    208  1.1  christos  syscall::lstat:entry, syscall::lstat64:entry, syscall::lxstat:entry
    209  1.1  christos  /self->ok/
    210  1.1  christos  {
    211  1.1  christos 	self->pathp = arg0;
    212  1.1  christos  }
    213  1.1  christos 
    214  1.1  christos  syscall::xstat:entry
    215  1.1  christos  /self->ok/
    216  1.1  christos  {
    217  1.1  christos 	self->pathp = arg1;
    218  1.1  christos  }
    219  1.1  christos 
    220  1.1  christos  syscall::stat:return, syscall::stat64:return, syscall::xstat:return,
    221  1.1  christos  syscall::lstat:return, syscall::lstat64:return, syscall::lxstat:return
    222  1.1  christos  /self->ok/
    223  1.1  christos  {
    224  1.1  christos 	self->path = copyinstr(self->pathp);
    225  1.1  christos 	self->pathp = 0;
    226  1.1  christos  }
    227  1.1  christos 
    228  1.1  christos  syscall::fstat:return, syscall::fstat64:entry, syscall::fxstat:entry
    229  1.1  christos  /self->ok/
    230  1.1  christos  {
    231  1.1  christos 	self->filep = curthread->t_procp->p_user.u_finfo.fi_list[arg0].uf_file;
    232  1.1  christos  }
    233  1.1  christos 
    234  1.1  christos  syscall::fstat:return, syscall::fstat64:return, syscall::fxstat:return
    235  1.1  christos  /self->ok/
    236  1.1  christos  {
    237  1.1  christos         this->vnodep = self->filep != 0 ? self->filep->f_vnode : 0;
    238  1.1  christos         self->path = this->vnodep ? (this->vnodep->v_path != 0 ?
    239  1.1  christos             cleanpath(this->vnodep->v_path) : "<unknown>") : "<unknown>";
    240  1.1  christos 	self->filep = 0;
    241  1.1  christos  }
    242  1.1  christos 
    243  1.1  christos  syscall::stat:return, syscall::stat64:return, syscall::xstat:return,
    244  1.1  christos  syscall::lstat:return, syscall::lstat64:return, syscall::lxstat:return,
    245  1.1  christos  syscall::fstat:return, syscall::fstat64:return, syscall::fxstat:return
    246  1.1  christos  /self->ok && (! OPT_failonly || (int)arg0 < 0) && 
    247  1.1  christos      ((OPT_file == 0) || (OPT_file == 1 && PATHNAME == copyinstr(self->pathp)))/
    248  1.1  christos  {
    249  1.1  christos 	/* print optional fields */
    250  1.1  christos  	OPT_time ? printf("%-14d ", timestamp/1000) : 1;
    251  1.1  christos  	OPT_timestr ? printf("%-20Y ", walltimestamp) : 1;
    252  1.1  christos  	OPT_zone ? printf("%-10s ", zonename) : 1;
    253  1.1  christos 
    254  1.1  christos 	/* print dump fields */
    255  1.1  christos 	OPT_dump ? printf("%s %d %d %d %d %s %d %d %s %s %S", zonename,
    256  1.1  christos 	    timestamp/1000, uid, pid, ppid, execname, (int)arg0, errno,
    257  1.1  christos 	    cwd, self->path, curpsinfo->pr_psargs) :
    258  1.1  christos 	    printf("%5d %6d ", uid, pid);
    259  1.1  christos 
    260  1.1  christos 	/* print main fields */
    261  1.1  christos 	OPT_args == 0 ? printf("%-12.12s ", execname) : 1;
    262  1.1  christos 	OPT_dump == 0 ? printf("%3d ", (int)arg0) : 1;
    263  1.1  christos 	OPT_err ? printf("%3d ", errno) : 1;
    264  1.1  christos 	OPT_cwd ? printf("%-20s ", cwd) : 1;
    265  1.1  christos 	OPT_type ? printf("%-8s ", probefunc) : 1;
    266  1.1  christos 	OPT_dump == 0 ? printf("%-20s ", self->path) : 1;
    267  1.1  christos 	OPT_args == 1 ? printf("%S", curpsinfo->pr_psargs) : 1;
    268  1.1  christos 	printf("\n");
    269  1.1  christos 
    270  1.1  christos 	/* cleanup */
    271  1.1  christos 	self->path = 0;
    272  1.1  christos 	self->ok = 0;
    273  1.1  christos  }
    274  1.1  christos 
    275  1.1  christos  /* 
    276  1.1  christos   * Cleanup 
    277  1.1  christos   */
    278  1.1  christos  syscall::stat:return, syscall::stat64:return, syscall::xstat:return,
    279  1.1  christos  syscall::lstat:return, syscall::lstat64:return, syscall::lxstat:return,
    280  1.1  christos  syscall::fstat:return, syscall::fstat64:return, syscall::fxstat:return
    281  1.1  christos  /self->ok/
    282  1.1  christos  {
    283  1.1  christos 	self->path = 0;
    284  1.1  christos 	self->ok = 0;
    285  1.1  christos  }
    286  1.1  christos '
    287