opensnoop revision 1
303N/A# opensnoop - snoop file opens as they occur. 303N/A# Written using DTrace (Solaris 10 3/05). 303N/A# $Id: opensnoop 3 2007-08-01 10:50:08Z brendan $ 303N/A# USAGE: opensnoop [-a|-A|-ceghsvxZ] [-f pathname] [-n name] [-p PID] 303N/A# opensnoop # default output 303N/A# -A # dump all data, space delimited 303N/A# -c # print cwd of process 303N/A# -e # print errno value 303N/A# -g # print command arguments 303N/A# -s # print start time, us 303N/A# -v # print start time, string 303N/A# -x # only print failed opens 5680N/A# -f pathname # file pathname to snoop 303N/A# -n name # command name to snoop 5680N/A# -p PID # process ID to snoop 5680N/A# opensnoop -v # human readable timestamps 5680N/A# opensnoop -e # see error codes 844N/A# PPID Parent Process ID 6630N/A# FD file descriptor (-1 for error) 1258N/A# CWD print current working directory of process 303N/A# PATH pathname for file open 6630N/A# COMM command name for the process 3083N/A# ARGS argument listing for the process 5680N/A# TIME timestamp for the open event, us 303N/A# STRTIME timestamp for the open event, string 303N/A# SEE ALSO: truss, BSM auditing. 303N/A# COPYRIGHT: Copyright (c) 2006 Brendan Gregg. 303N/A# The contents of this file are subject to the terms of the 303N/A# Common Development and Distribution License, Version 1.0 only 303N/A# (the "License"). You may not use this file except in compliance 303N/A# See the License for the specific language governing permissions 4115N/A# and limitations under the License. 4115N/A# Author: Brendan Gregg [Sydney, Australia] 4115N/A# 09-May-2004 Brendan Gregg Created this. 4115N/A# 21-Jan-2005 " " Wrapped in sh to provide options. 303N/A# 08-May-2005 " " Rewritten for performance. 303N/A# 14-May-2005 " " Added errno. 303N/A# 28-Jun-2005 " " Added cwd, zonename. 5680N/A# 17-Sep-2005 " " Increased switchrate, fixed page fault bug. 303N/A# 16-Jan-2006 " " Added -n, -p. 303N/A# 16-Jan-2006 " " Last update. 303N/A############################## 303N/A# --- Process Arguments --- USAGE: opensnoop [-a|-A|-ceghsvxZ] [-f pathname] opensnoop # default output -A # dump all data, space delimited -c # print cwd of process -g # print command arguments -s # print start time, us -v # print start time, string -x # only print failed opens -f pathname # pathname name to snoop -n name # process name to snoop -p PID # process ID to snoop opensnoop -v # human readable timestamps opensnoop -e # see error codes opensnoop -f /etc/motd # snoop this file only if [ $opt_dump -eq 1 ]; then opt_zone=0; opt_cwd=0; opt_time=0; opt_timestr=0; opt_args=2 if [ $opt_name -eq 1 -o $opt_pid -eq 1 ]; then ################################# # --- Main Program, DTrace --- inline int OPT_dump = '$opt_dump'; inline int OPT_file = '$opt_file'; inline int OPT_args = '$opt_args'; inline int OPT_cwd = '$opt_cwd'; inline int OPT_err = '$opt_err'; inline int OPT_zone = '$opt_zone'; inline int OPT_time = '$opt_time'; inline int OPT_timestr = '$opt_timestr'; inline int OPT_failonly = '$opt_failonly'; inline int OPT_pid = '$opt_pid'; inline int OPT_name = '$opt_name'; inline int FILTER = '$filter'; inline string PATHNAME = "'$pathname'"; inline string NAME = "'$pname'"; #pragma D option switchrate=10hz * ternary operators are used to improve performance. * OPT_args is unusual in that it can have one of three values. /* print optional headers */ OPT_time ? printf("%-14s ", "TIME") : 1; OPT_timestr ? printf("%-20s ", "STRTIME") : 1; OPT_zone ? printf("%-10s ", "ZONE") : 1; OPT_dump ? printf("%s %s %s %s %s %s %s %s %s %s %s", "ZONE", "TIME", "UID", "PID", "PPID", "COMM", "FD", "ERR", "CWD", "PATH", "ARGS") : printf("%5s %6s ","UID","PID"); OPT_args == 0 ? printf("%-12s ", "COMM") : 1; OPT_dump == 0 ? printf("%3s ", "FD") : 1; OPT_err ? printf("%3s ", "ERR") : 1; OPT_cwd ? printf("%-20s ", "CWD") : 1; OPT_dump == 0 ? printf("%-20s ", "PATH") : 1; OPT_args == 1 ? printf("%s", "ARGS") : 1; syscall::openat:entry, syscall::openat64:entry /* default is to trace unless filtering */ self->ok = FILTER ? 0 : 1; (OPT_name == 1 && NAME == execname) ? self->ok = 1 : 1; (OPT_pid == 1 && PID == pid) ? self->ok = 1 : 1; /* OPT_file is checked on return to ensure pathp is mapped */ syscall::openat:return, syscall::openat64:return /self->ok && (! OPT_failonly || (int)arg0 < 0) && ((OPT_file == 0) || (OPT_file == 1 && PATHNAME == copyinstr(self->pathp)))/ /* print optional fields */ OPT_time ? printf("%-14d ", timestamp/1000) : 1; OPT_timestr ? printf("%-20Y ", walltimestamp) : 1; OPT_zone ? printf("%-10s ", zonename) : 1; OPT_dump ? printf("%s %d %d %d %d %s %d %d %s %s %S", zonename, timestamp/1000, uid, pid, ppid, execname, (int)arg0, errno, cwd, copyinstr(self->pathp), curpsinfo->pr_psargs) : printf("%5d %6d ", uid, pid); OPT_args == 0 ? printf("%-12s ", execname) : 1; OPT_dump == 0 ? printf("%3d ", (int)arg0) : 1; OPT_err ? printf("%3d ", errno) : 1; OPT_cwd ? printf("%-20s ", cwd) : 1; OPT_dump == 0 ? printf("%-20s ", copyinstr(self->pathp)) : 1; OPT_args == 1 ? printf("%S", curpsinfo->pr_psargs) : 1; syscall::openat:return, syscall::openat64:return