Home | History | Annotate | Line # | Download | only in dist
      1  1.1  christos #!/bin/sh
      2  1.1  christos #
      3  1.1  christos # opensnoop - snoop file opens as they occur.
      4  1.1  christos #             Written using DTrace (Solaris 10 3/05).
      5  1.1  christos #
      6  1.1  christos # $Id: opensnoop,v 1.1.1.1 2015/09/30 22:01:06 christos Exp $
      7  1.1  christos #
      8  1.1  christos # USAGE:	opensnoop [-a|-A|-ceghsvxZ] [-f pathname] [-n name] [-p PID]
      9  1.1  christos #
     10  1.1  christos #		opensnoop	# default output
     11  1.1  christos #
     12  1.1  christos #		-a		# print most data
     13  1.1  christos #		-A		# dump all data, space delimited
     14  1.1  christos #		-c		# print cwd of process
     15  1.1  christos #		-e		# print errno value
     16  1.1  christos #		-g		# print command arguments
     17  1.1  christos #		-s		# print start time, us
     18  1.1  christos #		-v		# print start time, string
     19  1.1  christos #		-x		# only print failed opens
     20  1.1  christos #		-Z		# print zonename
     21  1.1  christos #		-f pathname	# file pathname to snoop
     22  1.1  christos #		-n name		# command name to snoop
     23  1.1  christos #		-p PID		# process ID to snoop
     24  1.1  christos #	eg,
     25  1.1  christos #		opensnoop -v			# human readable timestamps
     26  1.1  christos #		opensnoop -e			# see error codes
     27  1.1  christos #		opensnoop -f /etc/passwd	# snoop this file only
     28  1.1  christos # 	
     29  1.1  christos # FIELDS:
     30  1.1  christos #		ZONE		Zone name
     31  1.1  christos #		UID		User ID
     32  1.1  christos #		PID		Process ID
     33  1.1  christos #		PPID		Parent Process ID
     34  1.1  christos #		FD		file descriptor (-1 for error)
     35  1.1  christos #		ERR		errno value (see /usr/include/sys/errno.h)
     36  1.1  christos #		CWD		print current working directory of process
     37  1.1  christos #		PATH		pathname for file open
     38  1.1  christos #		COMM		command name for the process
     39  1.1  christos #		ARGS		argument listing for the process
     40  1.1  christos #		TIME		timestamp for the open event, us
     41  1.1  christos #		STRTIME		timestamp for the open event, string
     42  1.1  christos #
     43  1.1  christos # SEE ALSO: truss, BSM auditing.
     44  1.1  christos #
     45  1.1  christos # COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
     46  1.1  christos #
     47  1.1  christos # CDDL HEADER START
     48  1.1  christos #
     49  1.1  christos #  The contents of this file are subject to the terms of the
     50  1.1  christos #  Common Development and Distribution License, Version 1.0 only
     51  1.1  christos #  (the "License").  You may not use this file except in compliance
     52  1.1  christos #  with the License.
     53  1.1  christos #
     54  1.1  christos #  You can obtain a copy of the license at Docs/cddl1.txt
     55  1.1  christos #  or http://www.opensolaris.org/os/licensing.
     56  1.1  christos #  See the License for the specific language governing permissions
     57  1.1  christos #  and limitations under the License.
     58  1.1  christos #
     59  1.1  christos # CDDL HEADER END
     60  1.1  christos #
     61  1.1  christos # Author: Brendan Gregg  [Sydney, Australia]
     62  1.1  christos #
     63  1.1  christos # 09-May-2004	Brendan Gregg	Created this.
     64  1.1  christos # 21-Jan-2005	   "	  "	Wrapped in sh to provide options.
     65  1.1  christos # 08-May-2005	   "      "	Rewritten for performance.
     66  1.1  christos # 14-May-2005	   "      "	Added errno.
     67  1.1  christos # 28-Jun-2005	   "      "	Added cwd, zonename.
     68  1.1  christos # 17-Sep-2005	   "      "	Increased switchrate, fixed page fault bug.
     69  1.1  christos # 16-Jan-2006	   "	  "	Added -n, -p.
     70  1.1  christos # 16-Jan-2006	   "	  "	Last update.
     71  1.1  christos # 
     72  1.1  christos 
     73  1.1  christos 
     74  1.1  christos ##############################
     75  1.1  christos # --- Process Arguments ---
     76  1.1  christos #
     77  1.1  christos 
     78  1.1  christos ### Default variables
     79  1.1  christos opt_dump=0; opt_file=0; opt_time=0; opt_timestr=0; opt_args=0
     80  1.1  christos opt_zone=0; opt_cwd=0; opt_failonly=0; opt_err=0; filter=0; pathname=.
     81  1.1  christos opt_name=0; opt_pid=0; pname=.; pid=0
     82  1.1  christos 
     83  1.1  christos ### Process options
     84  1.1  christos while getopts aAcef:ghn:p:svxZ name
     85  1.1  christos do
     86  1.1  christos 	case $name in
     87  1.1  christos 	a)	opt_time=1; opt_timestr=1; opt_args=1; opt_err=1 ;;
     88  1.1  christos 	A)	opt_dump=1 ;;
     89  1.1  christos 	c)	opt_cwd=1 ;;
     90  1.1  christos 	e)	opt_err=1 ;;
     91  1.1  christos 	g)	opt_args=1 ;;
     92  1.1  christos 	f)	opt_file=1; pathname=$OPTARG ;;
     93  1.1  christos 	n)	opt_name=1; pname=$OPTARG ;;
     94  1.1  christos 	p)	opt_pid=1; pid=$OPTARG ;;
     95  1.1  christos 	s)	opt_time=1 ;;
     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: opensnoop [-a|-A|-ceghsvxZ] [-f pathname]
    101  1.1  christos 		                 [-n name] [-p PID]
    102  1.1  christos 		       opensnoop                # 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 		                -s              # print start time, us
    109  1.1  christos 		                -v              # print start time, string
    110  1.1  christos 		                -x              # only print failed opens
    111  1.1  christos 		                -Z              # print zonename
    112  1.1  christos 		                -f pathname	# pathname name to snoop
    113  1.1  christos 		                -n name		# process name to snoop
    114  1.1  christos 		                -p PID		# process ID to snoop
    115  1.1  christos 		  eg,
    116  1.1  christos 		       opensnoop -v             # human readable timestamps
    117  1.1  christos 		       opensnoop -e             # see error codes
    118  1.1  christos 		       opensnoop -f /etc/motd   # snoop this file only
    119  1.1  christos 		END
    120  1.1  christos 		exit 1
    121  1.1  christos 	esac
    122  1.1  christos done
    123  1.1  christos 
    124  1.1  christos ### Option logic
    125  1.1  christos if [ $opt_dump -eq 1 ]; then
    126  1.1  christos 	opt_zone=0; opt_cwd=0; opt_time=0; opt_timestr=0; opt_args=2 
    127  1.1  christos fi
    128  1.1  christos if [ $opt_name -eq 1 -o $opt_pid -eq 1 ]; then
    129  1.1  christos 	filter=1
    130  1.1  christos fi
    131  1.1  christos 
    132  1.1  christos 
    133  1.1  christos #################################
    134  1.1  christos # --- Main Program, DTrace ---
    135  1.1  christos #
    136  1.1  christos /usr/sbin/dtrace -n '
    137  1.1  christos  /*
    138  1.1  christos   * Command line arguments
    139  1.1  christos   */
    140  1.1  christos  inline int OPT_dump 	 = '$opt_dump';
    141  1.1  christos  inline int OPT_file 	 = '$opt_file';
    142  1.1  christos  inline int OPT_args 	 = '$opt_args';
    143  1.1  christos  inline int OPT_cwd	 = '$opt_cwd';
    144  1.1  christos  inline int OPT_err	 = '$opt_err';
    145  1.1  christos  inline int OPT_zone 	 = '$opt_zone';
    146  1.1  christos  inline int OPT_time 	 = '$opt_time';
    147  1.1  christos  inline int OPT_timestr	 = '$opt_timestr';
    148  1.1  christos  inline int OPT_failonly = '$opt_failonly';
    149  1.1  christos  inline int OPT_pid	 = '$opt_pid';
    150  1.1  christos  inline int OPT_name	 = '$opt_name';
    151  1.1  christos  inline int FILTER 	 = '$filter';
    152  1.1  christos  inline int PID		 = '$pid';
    153  1.1  christos  inline string PATHNAME	 = "'$pathname'";
    154  1.1  christos  inline string NAME	 = "'$pname'";
    155  1.1  christos  
    156  1.1  christos  #pragma D option quiet
    157  1.1  christos  #pragma D option switchrate=10hz
    158  1.1  christos  
    159  1.1  christos  /*
    160  1.1  christos   * Print header
    161  1.1  christos   */
    162  1.1  christos  dtrace:::BEGIN 
    163  1.1  christos  {
    164  1.1  christos 	/* 
    165  1.1  christos 	 * ternary operators are used to improve performance. 
    166  1.1  christos 	 * OPT_args is unusual in that it can have one of three values.
    167  1.1  christos 	 */
    168  1.1  christos  
    169  1.1  christos 	/* print optional headers */
    170  1.1  christos  	OPT_time ? printf("%-14s ", "TIME") : 1;
    171  1.1  christos  	OPT_timestr ? printf("%-20s ", "STRTIME") : 1;
    172  1.1  christos  	OPT_zone ? printf("%-10s ", "ZONE") : 1;
    173  1.1  christos 
    174  1.1  christos 	/* print dump headers */
    175  1.1  christos 	OPT_dump ? printf("%s %s %s %s %s %s %s %s %s %s %s", "ZONE",
    176  1.1  christos 	    "TIME", "UID", "PID", "PPID", "COMM", "FD", "ERR", "CWD", 
    177  1.1  christos 	    "PATH", "ARGS") : printf("%5s %6s ","UID","PID");
    178  1.1  christos 	
    179  1.1  christos 	/* print main headers */
    180  1.1  christos 	OPT_args == 0 ? printf("%-12s ", "COMM") : 1;
    181  1.1  christos 	OPT_dump == 0 ? printf("%3s ", "FD") : 1;
    182  1.1  christos 	OPT_err ? printf("%3s ", "ERR") : 1;
    183  1.1  christos 	OPT_cwd ? printf("%-20s ", "CWD") : 1;
    184  1.1  christos 	OPT_dump == 0 ? printf("%-20s ", "PATH") : 1;
    185  1.1  christos 	OPT_args == 1 ? printf("%s", "ARGS") : 1;
    186  1.1  christos 	printf("\n");
    187  1.1  christos  }
    188  1.1  christos 
    189  1.1  christos  /*
    190  1.1  christos   * Print open event
    191  1.1  christos   */
    192  1.1  christos  syscall::open:entry
    193  1.1  christos  {
    194  1.1  christos 	/* save pathname */
    195  1.1  christos 	self->pathp = arg0;
    196  1.1  christos 
    197  1.1  christos 	/* default is to trace unless filtering */
    198  1.1  christos 	self->ok = FILTER ? 0 : 1;
    199  1.1  christos 
    200  1.1  christos 	/* check each filter */
    201  1.1  christos 	(OPT_name == 1 && NAME == execname) ? self->ok = 1 : 1;
    202  1.1  christos 	(OPT_pid == 1 && PID == pid) ? self->ok = 1 : 1;
    203  1.1  christos 	/* OPT_file is checked on return to ensure pathp is mapped */
    204  1.1  christos  }
    205  1.1  christos 
    206  1.1  christos  syscall::open:return
    207  1.1  christos  /self->ok && (! OPT_failonly || (int)arg0 < 0) && 
    208  1.1  christos  ((OPT_file == 0) || (OPT_file == 1 && PATHNAME == copyinstr(self->pathp)))/
    209  1.1  christos  {
    210  1.1  christos 	/* print optional fields */
    211  1.1  christos  	OPT_time ? printf("%-14d ", timestamp/1000) : 1;
    212  1.1  christos  	OPT_timestr ? printf("%-20Y ", walltimestamp) : 1;
    213  1.1  christos  	OPT_zone ? printf("%-10s ", zonename) : 1;
    214  1.1  christos 
    215  1.1  christos 	/* print dump fields */
    216  1.1  christos 	OPT_dump ? printf("%s %d %d %d %d %s %d %d %s %s %S", zonename,
    217  1.1  christos 	    timestamp/1000, uid, pid, ppid, execname, (int)arg0, errno,
    218  1.1  christos 	    cwd, copyinstr(self->pathp), curpsinfo->pr_psargs) :
    219  1.1  christos 	    printf("%5d %6d ", uid, pid);
    220  1.1  christos 
    221  1.1  christos 	/* print main fields */
    222  1.1  christos 	OPT_args == 0 ? printf("%-12s ", execname) : 1;
    223  1.1  christos 	OPT_dump == 0 ? printf("%3d ", (int)arg0) : 1;
    224  1.1  christos 	OPT_err ? printf("%3d ", errno) : 1;
    225  1.1  christos 	OPT_cwd ? printf("%-20s ", cwd) : 1;
    226  1.1  christos 	OPT_dump == 0 ? printf("%-20s ", copyinstr(self->pathp)) : 1;
    227  1.1  christos 	OPT_args == 1 ? printf("%S", curpsinfo->pr_psargs) : 1;
    228  1.1  christos 	printf("\n");
    229  1.1  christos 
    230  1.1  christos 	/* cleanup */
    231  1.1  christos 	self->pathp = 0;
    232  1.1  christos 	self->ok = 0;
    233  1.1  christos  }
    234  1.1  christos 
    235  1.1  christos  /* 
    236  1.1  christos   * Cleanup 
    237  1.1  christos   */
    238  1.1  christos  syscall::open:return
    239  1.1  christos  /self->ok/
    240  1.1  christos  {
    241  1.1  christos 	self->pathp = 0;
    242  1.1  christos 	self->ok = 0;
    243  1.1  christos  }
    244  1.1  christos '
    245