xref: /xnu-11215/bsd/dev/dtrace/scripts/darwin.d (revision 1031c584)
1 /*
2  * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * The contents of this file constitute Original Code as defined in and
7  * are subject to the Apple Public Source License Version 1.1 (the
8  * "License").  You may not use this file except in compliance with the
9  * License.  Please obtain a copy of the License at
10  * http://www.apple.com/publicsource and read it before using this file.
11  *
12  * This Original Code and all software distributed under the License are
13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17  * License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * @APPLE_LICENSE_HEADER_END@
21  */
22 
23 inline int UT_VFORK = 0x02000000;  /* thread has vfork children */
24 #pragma D binding "1.0" UT_VFORK
25 
26 inline uthread_t uthread = (mach_kernel`uthread_t)(&curthread[1]); /* ` */
27 #pragma D binding "1.0" uthread
28 
29 inline struct proc * curproc =
30 	((struct proc *)(curthread->t_tro->tro_proc)) != NULL ? ((struct proc *)(curthread->t_tro->tro_proc)) :
31 	mach_kernel`kernproc; /* ` */
32 #pragma D binding "1.0" curproc
33 
34 /*
35  * curthread->thread_tag will have this set if the thread is the main thread
36  */
37 inline uint32_t THREAD_TAG_MAINTHREAD = 0x1;
38 inline uint32_t THREAD_TAG_CALLOUT = 0x2;
39 inline uint32_t THREAD_TAG_IOWORKLOOP = 0x4;
40 
41 /*
42  * mach thread scheduler state
43  */
44 inline int TH_WAIT		= 0x01;
45 #pragma D binding "1.0" TH_WAIT
46 inline int TH_SUSP		= 0x02;
47 #pragma D binding "1.0" TH_SUSP
48 inline int TH_RUN		= 0x04;
49 #pragma D binding "1.0" TH_RUN
50 inline int TH_UNINT		= 0x08;
51 #pragma D binding "1.0" TH_UNINT
52 inline int TH_TERMINATE  	= 0x10;
53 #pragma D binding "1.0" TH_TERMINATE
54 inline int TH_TERMINATE2	= 0x20;
55 #pragma D binding "1.0" TH_TERMINATE2
56 inline int TH_IDLE		= 0x80;
57 #pragma D binding "1.0" TH_IDLE
58 
59 /*
60  * The following miscellaneous constants are used by the proc(4) translators
61  * defined below.
62  */
63 inline char SIDL = 1;
64 #pragma D binding "1.0" SIDL
65 inline char SRUN = 2;
66 #pragma D binding "1.0" SRUN
67 inline char SSLEEP = 3;
68 #pragma D binding "1.0" SSLEEP
69 inline char SSTOP = 4;
70 #pragma D binding "1.0" SSTOP
71 inline char SZOMB = 5;
72 #pragma D binding "1.0" SZOMB
73 /*
74  * SONPROC defined here for compatability with ported scripts
75  */
76 inline char SONPROC = 6;
77 #pragma D binding "1.0" SONPROC
78 
79 inline char SOBJ_NONE = 0;
80 #pragma D binding "1.0" SOBJ_NONE
81 /*
82  * SOBJ_* defined here for compatability with ported scripts
83  */
84 inline char SOBJ_MUTEX = 1;
85 #pragma D binding "1.0" SOBJ_MUTEX
86 inline char SOBJ_RWLOCK = 2;
87 #pragma D binding "1.0" SOBJ_RWLOCK
88 inline char SOBJ_CV = 3;
89 #pragma D binding "1.0" SOBJ_CV
90 inline char SOBJ_SEMA = 4;
91 #pragma D binding "1.0" SOBJ_SEMA
92 inline char SOBJ_USER = 5;
93 #pragma D binding "1.0" SOBJ_USER
94 inline char SOBJ_USER_PI = 6;
95 #pragma D binding "1.0" SOBJ_USER_PI
96 inline char SOBJ_SHUTTLE = 7;
97 #pragma D binding "1.0" SOBJ_SHUTTLE
98 
99 inline char PR_MODEL_ILP32 = 1;
100 #pragma D binding "1.0" PR_MODEL_ILP32
101 inline char PR_MODEL_LP64 = 2;
102 #pragma D binding "1.0" PR_MODEL_LP64
103 
104 /*
105  * PR_* defined here for compatability with ported scripts
106  */
107 inline int PR_STOPPED = 0x00000001;
108 #pragma D binding "1.0" PR_STOPPED
109 inline int PR_ISTOP = 0x00000002;
110 #pragma D binding "1.0" PR_ISTOP
111 inline int PR_DSTOP = 0x00000004;
112 #pragma D binding "1.0" PR_DSTOP
113 inline int PR_STEP = 0x00000008;
114 #pragma D binding "1.0" PR_STEP
115 inline int PR_ASLEEP = 0x00000010;
116 #pragma D binding "1.0" PR_ASLEEP
117 inline int PR_PCINVAL = 0x00000020;
118 #pragma D binding "1.0" PR_PCINVAL
119 inline int PR_ASLWP = 0x00000040;
120 #pragma D binding "1.0" PR_ASLWP
121 inline int PR_AGENT = 0x00000080;
122 #pragma D binding "1.0" PR_AGENT
123 inline int PR_DETACH = 0x00000100;
124 #pragma D binding "1.0" PR_DETACH
125 inline int PR_DAEMON = 0x00000200;
126 #pragma D binding "1.0" PR_DAEMON
127 inline int PR_ISSYS = 0x00001000;
128 #pragma D binding "1.0" PR_ISSYS
129 inline int PR_VFORKP = 0x00002000;
130 #pragma D binding "1.0" PR_VFORKP
131 inline int PR_ORPHAN = 0x00004000;
132 #pragma D binding "1.0" PR_ORPHAN
133 inline int PR_FORK = 0x00100000;
134 #pragma D binding "1.0" PR_FORK
135 inline int PR_RLC = 0x00200000;
136 #pragma D binding "1.0" PR_RLC
137 inline int PR_KLC = 0x00400000;
138 #pragma D binding "1.0" PR_KLC
139 inline int PR_ASYNC = 0x00800000;
140 #pragma D binding "1.0" PR_ASYNC
141 inline int PR_MSACCT = 0x01000000;
142 #pragma D binding "1.0" PR_MSACCT
143 inline int PR_BPTADJ = 0x02000000;
144 #pragma D binding "1.0" PR_BPTADJ
145 inline int PR_PTRACE = 0x04000000;
146 #pragma D binding "1.0" PR_PTRACE
147 inline int PR_MSFORK = 0x08000000;
148 #pragma D binding "1.0" PR_MSFORK
149 inline int PR_IDLE = 0x10000000;
150 #pragma D binding "1.0" PR_IDLE
151 
152 /*
153  * Translate from the kernel's proc_t structure to a proc(4) psinfo_t struct.
154  * We do not provide support for pr_size, pr_rssize, pr_pctcpu, and pr_pctmem.
155  * We also do not fill in pr_lwp (the lwpsinfo_t for the representative LWP)
156  * because we do not have the ability to select and stop any representative.
157  * Also, for the moment, pr_wstat, pr_time, and pr_ctime are not supported,
158  * but these could be supported by DTrace in the future using subroutines.
159  * Note that any member added to this translator should also be added to the
160  * kthread_t-to-psinfo_t translator, below.
161  */
162 typedef int taskid_t;
163 typedef int projid_t;
164 typedef int poolid_t;
165 typedef struct timespec timestruc_t; /* (SECONDS, NANOSECONDS) */
166 
167 typedef struct psinfo {
168 	int	pr_nlwp;	/* number of active lwps in the process */
169 	pid_t	pr_pid;		/* unique process id */
170 	pid_t	pr_ppid;	/* process id of parent */
171 	pid_t	pr_pgid;	/* pid of process group leader */
172 	pid_t	pr_sid;		/* session id */
173 	uid_t	pr_uid;		/* real user id */
174 	uid_t	pr_euid;	/* effective user id */
175 	gid_t	pr_gid;		/* real group id */
176 	gid_t	pr_egid;	/* effective group id */
177 	uintptr_t pr_addr;	/* address of process */
178 	dev_t	pr_ttydev;	/* controlling tty device (or PRNODEV) */
179 	timestruc_t pr_start;/* process start time, DEPRECATED, see pr_start_tv below */
180 	char pr_fname[16];	/* name of execed file */
181 	char pr_psargs[80];	/* initial characters of arg list */
182 	int pr_argc;    	/* initial argument count */
183 	user_addr_t pr_argv;  /* address of initial argument vector */
184 	user_addr_t pr_envp;  /* address of initial environment vector */
185 	char    pr_dmodel;  /* data model of the process */
186 	taskid_t pr_taskid; /* task id */
187 	projid_t pr_projid; /* project id */
188 	poolid_t pr_poolid; /* pool id */
189 	zoneid_t pr_zoneid; /* zone id */
190 
191 	struct timeval pr_start_tv; /* process start time, from the epoch (SECONDS, MICROSECONDS) */
192 } psinfo_t;
193 
194 inline int P_LP64 = 0x00000004;  /* Process is LP64 */
195 #pragma D binding "1.0" P_LP64
196 
197 #pragma D binding "1.0" translator
198 translator psinfo_t < struct proc * P > {
199 	pr_nlwp = 	((struct task *)(P->p_proc_ro->pr_task))->thread_count;
200 	pr_pid = 	P->p_pid;
201 	pr_ppid = 	P->p_ppid;
202 	pr_pgid = 	P->p_pgrp.__smr_ptr->pg_id;
203 	pr_sid = 	P->p_pgrp.__smr_ptr->pg_session->s_sid;
204  	pr_uid = 	P->p_proc_ro->p_ucred.__smr_ptr->cr_posix.cr_ruid;
205  	pr_euid = 	P->p_proc_ro->p_ucred.__smr_ptr->cr_posix.cr_uid;
206  	pr_gid = 	P->p_proc_ro->p_ucred.__smr_ptr->cr_posix.cr_rgid;
207  	pr_egid = 	P->p_proc_ro->p_ucred.__smr_ptr->cr_posix.cr_groups[0];
208 	pr_addr = 	(uintptr_t)P;
209 
210 	pr_ttydev = (P->p_pgrp.__smr_ptr->pg_session->s_ttyvp == NULL) ? (dev_t)-1 :
211 		P->p_pgrp.__smr_ptr->pg_session->s_ttyp->t_dev;
212 
213 	/*
214 	 * timestruct_t (SECONDS, NANOSECONDS) is not available directly nor can a further translation
215 	 * be specified here. Zero the structure. Use pr_start_tv instead.
216 	 */
217 	pr_start = *((timestruc_t *)`dtrace_zero); /* ` */
218 
219 	pr_fname = 	P->p_comm;
220 	pr_psargs = P->p_comm; /* XXX omits command line arguments XXX */
221 	pr_argc = P->p_argc;
222 	pr_argv = P->p_dtrace_argv;
223 	pr_envp = P->p_dtrace_envp;
224 
225 	pr_dmodel = (P->p_flag & P_LP64) ? PR_MODEL_LP64 : PR_MODEL_ILP32;
226 
227 	pr_taskid = 0;
228 	pr_projid = 0;
229 	pr_poolid = 0;
230 	pr_zoneid = 0;
231 
232 	/*
233 	 * pstats->pstart is a struct timeval: (SECONDS, MICROSECONDS).
234 	 */
235 	pr_start_tv = P->p_start;
236 };
237 
238 /*
239  * Translate from the kernel's kthread_t structure to a proc(4) psinfo_t
240  * struct.  Lacking a facility to define one translator only in terms of
241  * another, we explicitly define each member by using the proc_t-to-psinfo_t
242  * translator, above; any members added to that translator should also be
243  * added here.  (The only exception to this is pr_start, which -- due to it
244  * being a structure -- cannot be defined in terms of a translator at all.)
245  */
246 #pragma D binding "1.0" translator
247 translator psinfo_t < thread_t T > {
248 	pr_nlwp = xlate <psinfo_t> (T->t_tro->tro_proc).pr_nlwp;
249 	pr_pid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_pid;
250 	pr_ppid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_ppid;
251 	pr_pgid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_pgid;
252 	pr_sid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_sid;
253 	pr_uid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_uid;
254 	pr_euid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_euid;
255 	pr_gid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_gid;
256 	pr_egid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_egid;
257 	pr_addr = xlate <psinfo_t> (T->t_tro->tro_proc).pr_addr;
258 	pr_ttydev = xlate <psinfo_t> (T->t_tro->tro_proc).pr_ttydev;
259 	pr_start = xlate <psinfo_t> (T->t_tro->tro_proc).pr_start;
260 	pr_fname = xlate <psinfo_t> (T->t_tro->tro_proc).pr_fname;
261 	pr_psargs = xlate <psinfo_t> (T->t_tro->tro_proc).pr_psargs; /* XXX omits command line arguments XXX */
262 	pr_argc = xlate <psinfo_t> (T->t_tro->tro_proc).pr_argc;
263 	pr_argv = xlate <psinfo_t> (T->t_tro->tro_proc).pr_argv;
264 	pr_envp = xlate <psinfo_t> (T->t_tro->tro_proc).pr_envp;
265 	pr_dmodel = xlate <psinfo_t> (T->t_tro->tro_proc).pr_dmodel;
266 	pr_taskid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_taskid;
267 	pr_projid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_projid;
268 	pr_poolid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_poolid;
269 	pr_zoneid = xlate <psinfo_t> (T->t_tro->tro_proc).pr_zoneid;
270 
271 	pr_start_tv = xlate <psinfo_t> (T->t_tro->tro_proc).pr_start_tv;
272 };
273 
274 /*
275  * Translate from the kernel's kthread_t structure to a proc(4) lwpsinfo_t.
276  * We do not provide support for pr_nice, pr_oldpri, pr_cpu, or pr_pctcpu.
277  * Also, for the moment, pr_start and pr_time are not supported, but these
278  * could be supported by DTrace in the future using subroutines.
279  */
280 
281 inline processor_t PROCESSOR_NULL = ((processor_t) 0);
282 #pragma D binding "1.0" PROCESSOR_NULL
283 
284 typedef int psetid_t;
285 
286 typedef struct lwpsinfo {
287 	int	pr_flag;	/* lwp flags (DEPRECATED; do not use) */
288 	id_t	pr_lwpid;	/* lwp id */
289 	uintptr_t pr_addr;	/* internal address of lwp */
290 	uintptr_t pr_wchan;	/* wait addr for sleeping lwp */
291 	char	pr_stype;	/* synchronization event type */
292 	char	pr_state;	/* numeric lwp state */
293 	char	pr_sname;	/* printable character for pr_state */
294 	short	pr_syscall;	/* system call number (if in syscall) */
295 	int	pr_pri;		/* priority, high value is high priority */
296 	char	pr_clname[8];	/* scheduling class name */
297 	int	pr_thstate;		/* mach thread scheduler state */
298 	processorid_t pr_onpro;		/* processor which last ran this lwp */
299 	processorid_t pr_bindpro;	/* processor to which lwp is bound */
300 	psetid_t pr_bindpset;	/* processor set to which lwp is bound */
301 } lwpsinfo_t;
302 
303 #pragma D binding "1.0" translator
304 translator lwpsinfo_t < thread_t T > {
305 	pr_flag = 0; /* lwp flags (DEPRECATED; do not use) */
306 	pr_lwpid = (id_t)T->thread_id;
307 	pr_addr = (uintptr_t)T;
308 	pr_wchan = (uintptr_t)(((uthread_t)&T[1])->uu_wchan);
309 
310 	pr_stype = SOBJ_NONE; /* XXX Undefined synch object (or none) XXX */
311 	pr_state = curproc->p_stat;
312 	pr_sname = (curproc->p_stat == SIDL) ? 'I' :
313 			(curproc->p_stat == SRUN) ? 'R' :
314 			(curproc->p_stat == SSLEEP) ? 'S' :
315 			(curproc->p_stat == SSTOP) ? 'T' :
316 			(curproc->p_stat == SZOMB) ? 'Z' : '?';
317 
318 	pr_syscall = ((uthread_t)&T[1])->uu_code;
319 	pr_pri = T->sched_pri;
320 
321 	pr_clname = (T->sched_mode & 0x0001) ? "RT" :
322 			(T->sched_mode & 0x0002) ? "TS" : "SYS";
323 
324 	pr_onpro = (T->last_processor == PROCESSOR_NULL) ? -1 : T->last_processor->cpu_id;
325 	pr_bindpro = -1; /* Darwin does not bind threads to processors. */
326 	pr_bindpset = -1; /* Darwin does not partition processors. */
327 	pr_thstate = T->state;
328 };
329 
330 inline psinfo_t *curpsinfo = xlate <psinfo_t *> (curproc);
331 #pragma D attributes Stable/Stable/Common curpsinfo
332 #pragma D binding "1.0" curpsinfo
333 
334 inline lwpsinfo_t *curlwpsinfo = xlate <lwpsinfo_t *> (curthread);
335 #pragma D attributes Stable/Stable/Common curlwpsinfo
336 #pragma D binding "1.0" curlwpsinfo
337 
338 /* XXX Really want vn_getpath(curproc->p_fd.fd_cdir, , ) but that takes namecache_rw_lock XXX */
339 inline string cwd = curproc->p_fd.fd_cdir->v_name == NULL ?
340 	"<unknown>" : stringof(curproc->p_fd.fd_cdir->v_name);
341 #pragma D attributes Stable/Stable/Common cwd
342 #pragma D binding "1.0" cwd
343 
344 /* XXX Really want vn_getpath(curproc->p_fd.fd_rdir, , ) but that takes namecache_rw_lock XXX */
345 inline string root = curproc->p_fd.fd_rdir == NULL ? "/" :
346 	curproc->p_fd.fd_rdir->v_name == NULL ? "<unknown>" :
347 	stringof(curproc->p_fd.fd_rdir->v_name);
348 #pragma D attributes Stable/Stable/Common root
349 #pragma D binding "1.0" root
350 
351 /* Used with livedump. */
352 inline unsigned DT_HV = 0;
353 #pragma D binding "1.19" DT_HV
354