xref: /xnu-12377.81.4/bsd/dev/dtrace/scripts/io.d (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2007 Apple, Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * The contents of this file constitute Original Code as defined in and
7*043036a2SApple OSS Distributions  * are subject to the Apple Public Source License Version 1.1 (the
8*043036a2SApple OSS Distributions  * "License").  You may not use this file except in compliance with the
9*043036a2SApple OSS Distributions  * License.  Please obtain a copy of the License at
10*043036a2SApple OSS Distributions  * http://www.apple.com/publicsource and read it before using this file.
11*043036a2SApple OSS Distributions  *
12*043036a2SApple OSS Distributions  * This Original Code and all software distributed under the License are
13*043036a2SApple OSS Distributions  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17*043036a2SApple OSS Distributions  * License for the specific language governing rights and limitations
18*043036a2SApple OSS Distributions  * under the License.
19*043036a2SApple OSS Distributions  *
20*043036a2SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
21*043036a2SApple OSS Distributions  */
22*043036a2SApple OSS Distributions 
23*043036a2SApple OSS Distributions #pragma D depends_on library darwin.d
24*043036a2SApple OSS Distributions #pragma D depends_on module mach_kernel
25*043036a2SApple OSS Distributions #pragma D depends_on provider io
26*043036a2SApple OSS Distributions 
27*043036a2SApple OSS Distributions inline int B_WRITE = 0x0000;
28*043036a2SApple OSS Distributions #pragma D binding "1.0" B_WRITE
29*043036a2SApple OSS Distributions inline int B_READ = 0x0001;
30*043036a2SApple OSS Distributions #pragma D binding "1.0" B_READ
31*043036a2SApple OSS Distributions inline int B_ASYNC = 0x0002;
32*043036a2SApple OSS Distributions #pragma D binding "1.0" B_ASYNC
33*043036a2SApple OSS Distributions inline int B_NOCACHE = 0x0004;
34*043036a2SApple OSS Distributions #pragma D binding "1.0" B_NOCACHE
35*043036a2SApple OSS Distributions inline int B_DELWRI = 0x0008;
36*043036a2SApple OSS Distributions #pragma D binding "1.0" B_DELWRI
37*043036a2SApple OSS Distributions inline int B_LOCKED = 0x0010;
38*043036a2SApple OSS Distributions #pragma D binding "1.0" B_LOCKED
39*043036a2SApple OSS Distributions inline int B_PHYS = 0x0020;
40*043036a2SApple OSS Distributions #pragma D binding "1.0" B_PHYS
41*043036a2SApple OSS Distributions inline int B_CLUSTER = 0x0040;
42*043036a2SApple OSS Distributions #pragma D binding "1.0" B_CLUSTER
43*043036a2SApple OSS Distributions inline int B_PAGEIO = 0x0080;
44*043036a2SApple OSS Distributions #pragma D binding "1.0" B_PAGEIO
45*043036a2SApple OSS Distributions inline int B_META = 0x0100;
46*043036a2SApple OSS Distributions #pragma D binding "1.0" B_META
47*043036a2SApple OSS Distributions inline int B_RAW = 0x0200;
48*043036a2SApple OSS Distributions #pragma D binding "1.0" B_RAW
49*043036a2SApple OSS Distributions inline int B_FUA = 0x0400;
50*043036a2SApple OSS Distributions #pragma D binding "1.0" B_FUA
51*043036a2SApple OSS Distributions inline int B_PASSIVE = 0x0800;
52*043036a2SApple OSS Distributions #pragma D binding "1.0" B_PASSIVE
53*043036a2SApple OSS Distributions 
54*043036a2SApple OSS Distributions typedef struct bufinfo {
55*043036a2SApple OSS Distributions 	int b_flags;			/* buffer status */
56*043036a2SApple OSS Distributions 	size_t b_bcount;		/* number of bytes */
57*043036a2SApple OSS Distributions 	caddr_t b_addr;			/* buffer address */
58*043036a2SApple OSS Distributions 	uint64_t b_lblkno;		/* block # on device */
59*043036a2SApple OSS Distributions 	uint64_t b_blkno;		/* expanded block # on device */
60*043036a2SApple OSS Distributions 	size_t b_resid;			/* # of bytes not transferred */
61*043036a2SApple OSS Distributions 	size_t b_bufsize;		/* size of allocated buffer */
62*043036a2SApple OSS Distributions 	caddr_t b_iodone;		/* I/O completion routine */
63*043036a2SApple OSS Distributions 	int b_error;			/* expanded error field */
64*043036a2SApple OSS Distributions 	dev_t b_edev;			/* extended device */
65*043036a2SApple OSS Distributions } bufinfo_t;
66*043036a2SApple OSS Distributions 
67*043036a2SApple OSS Distributions #pragma D binding "1.0" translator
68*043036a2SApple OSS Distributions translator bufinfo_t < struct buf *B > {
69*043036a2SApple OSS Distributions 	b_flags = B->b_flags;
70*043036a2SApple OSS Distributions 	b_addr = (caddr_t)B->b_datap;
71*043036a2SApple OSS Distributions 	b_bcount = B->b_bcount;
72*043036a2SApple OSS Distributions 	b_lblkno = B->b_lblkno;
73*043036a2SApple OSS Distributions 	b_blkno = B->b_blkno;
74*043036a2SApple OSS Distributions 	b_resid = B->b_resid;
75*043036a2SApple OSS Distributions 	b_bufsize = B->b_bufsize;
76*043036a2SApple OSS Distributions 	b_iodone = (caddr_t)B->b_iodone;
77*043036a2SApple OSS Distributions 	b_error = B->b_error;
78*043036a2SApple OSS Distributions 	b_edev = B->b_dev;
79*043036a2SApple OSS Distributions };
80*043036a2SApple OSS Distributions 
81*043036a2SApple OSS Distributions typedef struct devinfo {
82*043036a2SApple OSS Distributions 	int dev_major;			/* major number */
83*043036a2SApple OSS Distributions 	int dev_minor;			/* minor number */
84*043036a2SApple OSS Distributions 	int dev_instance;		/* instance number */
85*043036a2SApple OSS Distributions 	string dev_name;		/* name of device */
86*043036a2SApple OSS Distributions 	string dev_statname;		/* name of device + instance/minor */
87*043036a2SApple OSS Distributions 	string dev_pathname;		/* pathname of device */
88*043036a2SApple OSS Distributions } devinfo_t;
89*043036a2SApple OSS Distributions 
90*043036a2SApple OSS Distributions #pragma D binding "1.0" translator
91*043036a2SApple OSS Distributions translator devinfo_t < struct buf *B > {
92*043036a2SApple OSS Distributions 	dev_major = getmajor(B->b_dev);
93*043036a2SApple OSS Distributions 	dev_minor = getminor(B->b_dev);
94*043036a2SApple OSS Distributions 	dev_instance = getminor(B->b_dev);
95*043036a2SApple OSS Distributions 	dev_name = "??"; /* XXX */
96*043036a2SApple OSS Distributions 	dev_statname = "??"; /* XXX */
97*043036a2SApple OSS Distributions 	dev_pathname = "??"; /* XXX */
98*043036a2SApple OSS Distributions };
99*043036a2SApple OSS Distributions 
100*043036a2SApple OSS Distributions typedef off_t offset_t;
101*043036a2SApple OSS Distributions 
102*043036a2SApple OSS Distributions typedef struct fileinfo {
103*043036a2SApple OSS Distributions 	string fi_name;			/* name (basename of fi_pathname) */
104*043036a2SApple OSS Distributions 	string fi_dirname;		/* directory (dirname of fi_pathname) */
105*043036a2SApple OSS Distributions 	string fi_pathname;		/* full pathname */
106*043036a2SApple OSS Distributions 	offset_t fi_offset;		/* offset within file */
107*043036a2SApple OSS Distributions 	string fi_fs;			/* filesystem */
108*043036a2SApple OSS Distributions 	string fi_mount;		/* mount point of file system */
109*043036a2SApple OSS Distributions 	int fi_oflags;          /* open(2) flags for file descriptor */
110*043036a2SApple OSS Distributions } fileinfo_t;
111*043036a2SApple OSS Distributions 
112*043036a2SApple OSS Distributions #pragma D binding "1.0" translator
113*043036a2SApple OSS Distributions translator fileinfo_t < struct buf *B > {
114*043036a2SApple OSS Distributions 	fi_name = B->b_vp->v_name == NULL ? "<unknown (NULL v_name)>" : B->b_vp->v_name;
115*043036a2SApple OSS Distributions 
116*043036a2SApple OSS Distributions 	fi_dirname = B->b_vp->v_parent == NULL ? "<unknown (NULL v_parent)>" :
117*043036a2SApple OSS Distributions 			(B->b_vp->v_parent->v_name == NULL ? "<unknown (NULL v_name)>" : B->b_vp->v_parent->v_name);
118*043036a2SApple OSS Distributions 
119*043036a2SApple OSS Distributions 	fi_pathname = strjoin("??/",
120*043036a2SApple OSS Distributions 			strjoin(B->b_vp->v_parent == NULL ? "<unknown (NULL v_parent)>" :
121*043036a2SApple OSS Distributions 				(B->b_vp->v_parent->v_name == NULL ? "<unknown (NULL v_name)>" : B->b_vp->v_parent->v_name),
122*043036a2SApple OSS Distributions 				strjoin("/",
123*043036a2SApple OSS Distributions 					B->b_vp->v_name == NULL ? "<unknown (NULL v_name)>" : B->b_vp->v_name)));
124*043036a2SApple OSS Distributions 
125*043036a2SApple OSS Distributions 	fi_offset = B->b_upl == NULL ? -1 : ((upl_t)B->b_upl)->u_offset;
126*043036a2SApple OSS Distributions 
127*043036a2SApple OSS Distributions 	fi_fs = B->b_vp->v_mount->mnt_vtable->vfc_name;
128*043036a2SApple OSS Distributions 
129*043036a2SApple OSS Distributions 	fi_mount = B->b_vp->v_mount->mnt_vnodecovered == NULL ? "/" : B->b_vp->v_mount->mnt_vnodecovered->v_name;
130*043036a2SApple OSS Distributions 
131*043036a2SApple OSS Distributions 	fi_oflags = 0;
132*043036a2SApple OSS Distributions };
133*043036a2SApple OSS Distributions 
134*043036a2SApple OSS Distributions /*
135*043036a2SApple OSS Distributions  * The following inline constants can be used to examine fi_oflags when using
136*043036a2SApple OSS Distributions  * the fds[] array or a translated fileglob *.  Note that the various open
137*043036a2SApple OSS Distributions  * flags behave as a bit-field *except* for O_RDONLY, O_WRONLY, and O_RDWR.
138*043036a2SApple OSS Distributions  * To test the open mode, you write code similar to that used with the fcntl(2)
139*043036a2SApple OSS Distributions  * F_GET[X]FL command, such as: if ((fi_oflags & O_ACCMODE) == O_WRONLY).
140*043036a2SApple OSS Distributions  */
141*043036a2SApple OSS Distributions inline int O_ACCMODE = 0x0003;
142*043036a2SApple OSS Distributions #pragma D binding "1.1" O_ACCMODE
143*043036a2SApple OSS Distributions 
144*043036a2SApple OSS Distributions inline int O_RDONLY = 0x0000;
145*043036a2SApple OSS Distributions #pragma D binding "1.1" O_RDONLY
146*043036a2SApple OSS Distributions inline int O_WRONLY = 0x0001;
147*043036a2SApple OSS Distributions #pragma D binding "1.1" O_WRONLY
148*043036a2SApple OSS Distributions inline int O_RDWR = 0x0002;
149*043036a2SApple OSS Distributions #pragma D binding "1.1" O_RDWR
150*043036a2SApple OSS Distributions 
151*043036a2SApple OSS Distributions inline int O_NONBLOCK = 0x0004;
152*043036a2SApple OSS Distributions #pragma D binding "1.1" O_NONBLOCK
153*043036a2SApple OSS Distributions inline int O_APPEND = 0x0008;
154*043036a2SApple OSS Distributions #pragma D binding "1.1" O_APPEND
155*043036a2SApple OSS Distributions inline int O_SHLOCK = 0x0010;
156*043036a2SApple OSS Distributions #pragma D binding "1.1" O_SHLOCK
157*043036a2SApple OSS Distributions inline int O_EXLOCK = 0x0020;
158*043036a2SApple OSS Distributions #pragma D binding "1.1" O_EXLOCK
159*043036a2SApple OSS Distributions inline int O_ASYNC = 0x0040;
160*043036a2SApple OSS Distributions #pragma D binding "1.1" O_ASYNC
161*043036a2SApple OSS Distributions inline int O_SYNC = 0x0080;
162*043036a2SApple OSS Distributions #pragma D binding "1.1" O_SYNC
163*043036a2SApple OSS Distributions inline int O_NOFOLLOW = 0x0100;
164*043036a2SApple OSS Distributions #pragma D binding "1.1" O_NOFOLLOW
165*043036a2SApple OSS Distributions inline int O_CREAT = 0x0200;
166*043036a2SApple OSS Distributions #pragma D binding "1.1" O_CREAT
167*043036a2SApple OSS Distributions inline int O_TRUNC = 0x0400;
168*043036a2SApple OSS Distributions #pragma D binding "1.1" O_TRUNC
169*043036a2SApple OSS Distributions inline int O_EXCL = 0x0800;
170*043036a2SApple OSS Distributions #pragma D binding "1.1" O_EXCL
171*043036a2SApple OSS Distributions inline int O_RESOLVE_BENEATH = 0x1000;
172*043036a2SApple OSS Distributions #pragma D binding "1.1" O_RESOLVE_BENEATH
173*043036a2SApple OSS Distributions inline int O_EVTONLY = 0x8000;
174*043036a2SApple OSS Distributions #pragma D binding "1.1" O_EVTONLY
175*043036a2SApple OSS Distributions inline int O_NOCTTY = 0x20000;
176*043036a2SApple OSS Distributions #pragma D binding "1.1" O_NOCTTY
177*043036a2SApple OSS Distributions inline int O_DIRECTORY = 0x100000;
178*043036a2SApple OSS Distributions #pragma D binding "1.1" O_DIRECTORY
179*043036a2SApple OSS Distributions inline int O_SYMLINK = 0x200000;
180*043036a2SApple OSS Distributions #pragma D binding "1.1" O_SYMLINK
181*043036a2SApple OSS Distributions inline int O_NOFOLLOW_ANY = 0x20000000;
182*043036a2SApple OSS Distributions #pragma D binding "1.1" O_NOFOLLOW_ANY
183*043036a2SApple OSS Distributions 
184*043036a2SApple OSS Distributions /* From bsd/sys/file_internal.h */
185*043036a2SApple OSS Distributions inline int DTYPE_VNODE = 1;
186*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_VNODE
187*043036a2SApple OSS Distributions inline int DTYPE_SOCKET = 2;
188*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_SOCKET
189*043036a2SApple OSS Distributions inline int DTYPE_PSXSHM = 3;
190*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_PSXSHM
191*043036a2SApple OSS Distributions inline int DTYPE_PSXSEM = 4;
192*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_PSXSEM
193*043036a2SApple OSS Distributions inline int DTYPE_KQUEUE = 5;
194*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_KQUEUE
195*043036a2SApple OSS Distributions inline int DTYPE_PIPE = 6;
196*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_PIPE
197*043036a2SApple OSS Distributions inline int DTYPE_FSEVENTS = 7;
198*043036a2SApple OSS Distributions #pragma D binding "1.1" DTYPE_FSEVENTS
199*043036a2SApple OSS Distributions 
200*043036a2SApple OSS Distributions #pragma D binding "1.1" translator
201*043036a2SApple OSS Distributions translator fileinfo_t < struct fileglob *F > {
202*043036a2SApple OSS Distributions 	fi_name = (F == NULL) ? "<none>" :
203*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_VNODE ?
204*043036a2SApple OSS Distributions 			((struct vnode *)F->fg_data)->v_name == NULL ? "<unknown (NULL v_name)>" : ((struct vnode *)F->fg_data)->v_name :
205*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_SOCKET ? "<socket>" :
206*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_PSXSHM ? "<shared memory>" :
207*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_PSXSEM ? "<semaphore>" :
208*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_KQUEUE ? "<kqueue>" :
209*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_PIPE ? "<pipe>" :
210*043036a2SApple OSS Distributions 		F->fg_ops->fo_type == DTYPE_FSEVENTS ? "<fsevents>" : "<unknown (BAD fo_type)>";
211*043036a2SApple OSS Distributions 
212*043036a2SApple OSS Distributions 	fi_dirname = (F == NULL) ? "<none>" :
213*043036a2SApple OSS Distributions 		F->fg_ops->fo_type != DTYPE_VNODE ? "<unknown (not a vnode)>" :
214*043036a2SApple OSS Distributions 			((struct vnode *)F->fg_data)->v_parent == NULL ? "<unknown (NULL v_parent)>" :
215*043036a2SApple OSS Distributions 			(((struct vnode *)F->fg_data)->v_parent->v_name == NULL ? "<unknown (NULL v_name)>" :
216*043036a2SApple OSS Distributions 			 ((struct vnode *)F->fg_data)->v_parent->v_name);
217*043036a2SApple OSS Distributions 
218*043036a2SApple OSS Distributions 	fi_pathname = (F == NULL) ? "<none>" :
219*043036a2SApple OSS Distributions 		F->fg_ops->fo_type != DTYPE_VNODE ? "<unknown (not a vnode)>" :
220*043036a2SApple OSS Distributions 			strjoin("??/",
221*043036a2SApple OSS Distributions 			strjoin(((struct vnode *)F->fg_data)->v_parent == NULL ? "<unknown (NULL v_parent)>" :
222*043036a2SApple OSS Distributions 				(((struct vnode *)F->fg_data)->v_parent->v_name == NULL ? "<unknown (NULL v_name)>" :
223*043036a2SApple OSS Distributions 				 ((struct vnode *)F->fg_data)->v_parent->v_name),
224*043036a2SApple OSS Distributions 				strjoin("/",
225*043036a2SApple OSS Distributions 					((struct vnode *)F->fg_data)->v_name == NULL ? "<unknown (NULL v_name)>" :
226*043036a2SApple OSS Distributions 					((struct vnode *)F->fg_data)->v_name)));
227*043036a2SApple OSS Distributions 
228*043036a2SApple OSS Distributions 	fi_offset = (F == NULL) ? 0 :
229*043036a2SApple OSS Distributions 			F->fg_offset;
230*043036a2SApple OSS Distributions 
231*043036a2SApple OSS Distributions 	fi_fs = (F == NULL) ? "<none>" :
232*043036a2SApple OSS Distributions 		F->fg_ops->fo_type != DTYPE_VNODE ? "<unknown (not a vnode)>" :
233*043036a2SApple OSS Distributions 			((struct vnode *)F->fg_data)->v_mount->mnt_vtable->vfc_name;
234*043036a2SApple OSS Distributions 
235*043036a2SApple OSS Distributions 	fi_mount = (F == NULL) ? "<none>" :
236*043036a2SApple OSS Distributions 		F->fg_ops->fo_type != DTYPE_VNODE ? "<unknown (not a vnode)>" :
237*043036a2SApple OSS Distributions 			((struct vnode *)F->fg_data)->v_mount->mnt_vnodecovered == NULL ? "/" :
238*043036a2SApple OSS Distributions 			((struct vnode *)F->fg_data)->v_mount->mnt_vnodecovered->v_name;
239*043036a2SApple OSS Distributions 
240*043036a2SApple OSS Distributions 	fi_oflags = (F == NULL) ? 0 :
241*043036a2SApple OSS Distributions 			F->fg_flag - 1; /* Subtract one to map FREAD/FWRITE bitfield to O_RD/WR open() flags. */
242*043036a2SApple OSS Distributions };
243*043036a2SApple OSS Distributions 
244*043036a2SApple OSS Distributions inline fileinfo_t fds[int fd] = xlate <fileinfo_t> (
245*043036a2SApple OSS Distributions 	(fd >= 0 && fd < curproc->p_fd.fd_afterlast) ?
246*043036a2SApple OSS Distributions 		(struct fileglob *)(curproc->p_fd.fd_ofiles[fd]->fp_glob) :
247*043036a2SApple OSS Distributions 		(struct fileglob *)NULL);
248*043036a2SApple OSS Distributions 
249*043036a2SApple OSS Distributions #pragma D attributes Stable/Stable/Common fds
250*043036a2SApple OSS Distributions #pragma D binding "1.1" fds
251*043036a2SApple OSS Distributions 
252*043036a2SApple OSS Distributions #pragma D binding "1.2" translator
253*043036a2SApple OSS Distributions translator fileinfo_t < struct vnode *V > {
254*043036a2SApple OSS Distributions 	fi_name = V->v_name == NULL ? "<unknown (NULL v_name)>" : V->v_name;
255*043036a2SApple OSS Distributions 
256*043036a2SApple OSS Distributions 	fi_dirname = V->v_parent == NULL ? "<unknown (NULL v_parent)>" :
257*043036a2SApple OSS Distributions 			(V->v_parent->v_name == NULL ? "<unknown (NULL v_name)>" : V->v_parent->v_name);
258*043036a2SApple OSS Distributions 
259*043036a2SApple OSS Distributions 	fi_pathname = strjoin("??/",
260*043036a2SApple OSS Distributions 			strjoin(V->v_parent == NULL ? "<unknown (NULL v_parent)>" :
261*043036a2SApple OSS Distributions 				(V->v_parent->v_name == NULL ? "<unknown (NULL v_name)>" : V->v_parent->v_name),
262*043036a2SApple OSS Distributions 				strjoin("/",
263*043036a2SApple OSS Distributions 					V->v_name == NULL ? "<unknown (NULL v_name)>" : V->v_name)));
264*043036a2SApple OSS Distributions 
265*043036a2SApple OSS Distributions 	fi_fs = V->v_mount->mnt_vtable->vfc_name;
266*043036a2SApple OSS Distributions 
267*043036a2SApple OSS Distributions 	fi_mount = V->v_mount->mnt_vnodecovered == NULL ? "/" : V->v_mount->mnt_vnodecovered->v_name;
268*043036a2SApple OSS Distributions };
269*043036a2SApple OSS Distributions 
270