1#!/usr/sbin/dtrace -s
2/*
3 * pathopens.d - full pathnames opened successfully count.
4 *               Written using DTrace (Solaris 10 3/05)
5 *
6 * This program prints a count of the number of times files have been
7 * successfully opened. This is somewhat special in that the full pathname
8 * is calculated, even if the file open referred to a relative pathname.
9 *
10 * 20-Apr-2006, ver 0.81
11 *
12 * USAGE:	fileopens.d
13 *
14 * FIELDS:
15 *		PATHNAME	full pathname
16 *		COUNT		number of successful opens
17 *
18 * Similar to a script from DExplorer.
19 *
20 * COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
21 *
22 * CDDL HEADER START
23 *
24 *  The contents of this file are subject to the terms of the
25 *  Common Development and Distribution License, Version 1.0 only
26 *  (the "License").  You may not use this file except in compliance
27 *  with the License.
28 *
29 *  You can obtain a copy of the license at Docs/cddl1.txt
30 *  or http://www.opensolaris.org/os/licensing.
31 *  See the License for the specific language governing permissions
32 *  and limitations under the License.
33 *
34 * CDDL HEADER END
35 *
36 * 28-Jun-2005	Brendan Gregg	Created this.
37 * 12-Jan-2006	   "	  "	Fixed known error.
38 */
39
40#pragma D option quiet
41
42dtrace:::BEGIN
43{
44	printf("Tracing... Hit Ctrl-C to end.\n");
45}
46
47syscall::open*:entry
48{
49	self->pathp = arg0;
50	self->ok = 1;
51}
52
53syscall::open*:return
54/self->ok && arg0 != -1/
55{
56	self->file = copyinstr(self->pathp);
57	self->char0 = copyin(self->pathp, 1);
58
59	/* fetch current working directory */
60	this->path = (curproc->p_fd->fd_cdir->v_name);
61
62	/*
63	 * Make the full pathname
64	 *
65	 * This routine takes the cwd and the filename, and generates a
66	 * full pathname. Sometimes the filename is absolute, so we must
67	 * ignore the cwd. This also checks if the cwd ends in an
68	 * unnecessary '/'.
69	 */
70	this->len = strlen(this->path);
71	self->join = *(char *)(this->path + this->len - 1) == '/' ?  "" : "/";
72	self->dir = strjoin(cwd, self->join);
73	self->dir = *(char *)self->char0 == '/' ? "" : self->dir;
74	self->full = strjoin(self->dir, self->file);
75
76	/* save to aggregation */
77	@num[self->full] = count();
78
79	/* cleanup */
80	self->join  = 0;
81	self->full  = 0;
82	self->dir   = 0;
83	self->file  = 0;
84	self->char0 = 0;
85}
86
87syscall::open*:return
88/self->ok/
89{
90	/* cleanup */
91	self->ok    = 0;
92	self->pathp = 0;
93}
94
95dtrace:::END
96{
97	printf("%6s %s\n", "COUNT", "PATHNAME");
98	printa("%@6d %s\n", @num);
99}
100