1 1.2 christos #!/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.2 christos # $Id: statsnoop,v 1.2 2015/10/07 02:22:29 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.2 christos syscall::stat:entry, 195 1.2 christos syscall::lstat:entry, 196 1.2 christos syscall::fstat: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.2 christos syscall::stat:entry, 208 1.2 christos syscall::lstat: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.2 christos syscall::stat:return 215 1.2 christos syscall::lstat:return 216 1.1 christos /self->ok/ 217 1.1 christos { 218 1.1 christos self->path = copyinstr(self->pathp); 219 1.1 christos } 220 1.1 christos 221 1.2 christos syscall::fstat:return 222 1.1 christos /self->ok/ 223 1.1 christos { 224 1.2 christos self->path = strjoin("fd=", lltostr(arg0)); 225 1.2 christos /* curthread->l_proc->p_fd->fd_dt->dt_ff[arg0]; */ 226 1.1 christos } 227 1.1 christos 228 1.2 christos syscall::stat:return, 229 1.2 christos syscall::lstat:return, 230 1.2 christos syscall::fstat:return 231 1.1 christos /self->ok && (! OPT_failonly || (int)arg0 < 0) && 232 1.1 christos ((OPT_file == 0) || (OPT_file == 1 && PATHNAME == copyinstr(self->pathp)))/ 233 1.1 christos { 234 1.1 christos /* print optional fields */ 235 1.1 christos OPT_time ? printf("%-14d ", timestamp/1000) : 1; 236 1.1 christos OPT_timestr ? printf("%-20Y ", walltimestamp) : 1; 237 1.1 christos OPT_zone ? printf("%-10s ", zonename) : 1; 238 1.1 christos 239 1.1 christos /* print dump fields */ 240 1.1 christos OPT_dump ? printf("%s %d %d %d %d %s %d %d %s %s %S", zonename, 241 1.1 christos timestamp/1000, uid, pid, ppid, execname, (int)arg0, errno, 242 1.1 christos cwd, self->path, curpsinfo->pr_psargs) : 243 1.1 christos printf("%5d %6d ", uid, pid); 244 1.1 christos 245 1.1 christos /* print main fields */ 246 1.1 christos OPT_args == 0 ? printf("%-12.12s ", execname) : 1; 247 1.1 christos OPT_dump == 0 ? printf("%3d ", (int)arg0) : 1; 248 1.1 christos OPT_err ? printf("%3d ", errno) : 1; 249 1.1 christos OPT_cwd ? printf("%-20s ", cwd) : 1; 250 1.1 christos OPT_type ? printf("%-8s ", probefunc) : 1; 251 1.1 christos OPT_dump == 0 ? printf("%-20s ", self->path) : 1; 252 1.2 christos /* OPT_args == 1 ? printf("%S", curpsinfo->pr_psargs) : 1; */ 253 1.1 christos printf("\n"); 254 1.1 christos 255 1.1 christos /* cleanup */ 256 1.1 christos self->path = 0; 257 1.1 christos self->ok = 0; 258 1.1 christos } 259 1.1 christos 260 1.1 christos /* 261 1.1 christos * Cleanup 262 1.1 christos */ 263 1.2 christos syscall::stat:return, 264 1.2 christos syscall::lstat:return 265 1.2 christos /* 266 1.2 christos syscall::fstat:return 267 1.2 christos */ 268 1.1 christos /self->ok/ 269 1.1 christos { 270 1.1 christos self->path = 0; 271 1.1 christos self->ok = 0; 272 1.1 christos } 273 1.1 christos ' 274