kern_ktrace.c revision 1.72 1 1.72 darrenr /* $NetBSD: kern_ktrace.c,v 1.72 2003/06/28 14:21:54 darrenr Exp $ */
2 1.11 cgd
3 1.1 cgd /*
4 1.9 cgd * Copyright (c) 1989, 1993
5 1.9 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.25 fvdl * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
36 1.1 cgd */
37 1.55 lukem
38 1.55 lukem #include <sys/cdefs.h>
39 1.72 darrenr __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.72 2003/06/28 14:21:54 darrenr Exp $");
40 1.29 thorpej
41 1.29 thorpej #include "opt_ktrace.h"
42 1.62 manu #include "opt_compat_mach.h"
43 1.1 cgd
44 1.7 mycroft #include <sys/param.h>
45 1.13 cgd #include <sys/systm.h>
46 1.7 mycroft #include <sys/proc.h>
47 1.7 mycroft #include <sys/file.h>
48 1.7 mycroft #include <sys/namei.h>
49 1.7 mycroft #include <sys/vnode.h>
50 1.7 mycroft #include <sys/ktrace.h>
51 1.7 mycroft #include <sys/malloc.h>
52 1.7 mycroft #include <sys/syslog.h>
53 1.28 christos #include <sys/filedesc.h>
54 1.42 sommerfe #include <sys/ioctl.h>
55 1.1 cgd
56 1.13 cgd #include <sys/mount.h>
57 1.67 thorpej #include <sys/sa.h>
58 1.13 cgd #include <sys/syscallargs.h>
59 1.22 christos
60 1.51 jdolecek #ifdef KTRACE
61 1.51 jdolecek
62 1.72 darrenr int ktrace_common(struct lwp *, int, int, int, struct file *);
63 1.72 darrenr void ktrinitheader(struct ktr_header *, struct lwp *, int);
64 1.72 darrenr int ktrops(struct lwp *, struct proc *, int, int, struct file *);
65 1.72 darrenr int ktrsetchildren(struct lwp *, struct proc *, int, int,
66 1.47 thorpej struct file *);
67 1.72 darrenr int ktrwrite(struct lwp *, struct ktr_header *);
68 1.47 thorpej int ktrcanset(struct proc *, struct proc *);
69 1.47 thorpej int ktrsamefile(struct file *, struct file *);
70 1.44 sommerfe
71 1.44 sommerfe /*
72 1.44 sommerfe * "deep" compare of two files for the purposes of clearing a trace.
73 1.44 sommerfe * Returns true if they're the same open file, or if they point at the
74 1.44 sommerfe * same underlying vnode/socket.
75 1.44 sommerfe */
76 1.44 sommerfe
77 1.44 sommerfe int
78 1.62 manu ktrsamefile(f1, f2)
79 1.62 manu struct file *f1;
80 1.62 manu struct file *f2;
81 1.44 sommerfe {
82 1.44 sommerfe return ((f1 == f2) ||
83 1.45 sommerfe ((f1 != NULL) && (f2 != NULL) &&
84 1.45 sommerfe (f1->f_type == f2->f_type) &&
85 1.44 sommerfe (f1->f_data == f2->f_data)));
86 1.44 sommerfe }
87 1.22 christos
88 1.28 christos void
89 1.62 manu ktrderef(p)
90 1.62 manu struct proc *p;
91 1.28 christos {
92 1.42 sommerfe struct file *fp = p->p_tracep;
93 1.42 sommerfe p->p_traceflag = 0;
94 1.42 sommerfe if (fp == NULL)
95 1.28 christos return;
96 1.68 pk simple_lock(&fp->f_slock);
97 1.42 sommerfe FILE_USE(fp);
98 1.59 jdolecek
99 1.59 jdolecek /*
100 1.59 jdolecek * ktrace file descriptor can't be watched (are not visible to
101 1.59 jdolecek * userspace), so no kqueue stuff here
102 1.59 jdolecek */
103 1.42 sommerfe closef(fp, NULL);
104 1.28 christos
105 1.28 christos p->p_tracep = NULL;
106 1.28 christos }
107 1.28 christos
108 1.28 christos void
109 1.62 manu ktradref(p)
110 1.62 manu struct proc *p;
111 1.28 christos {
112 1.42 sommerfe struct file *fp = p->p_tracep;
113 1.28 christos
114 1.42 sommerfe fp->f_count++;
115 1.28 christos }
116 1.28 christos
117 1.39 thorpej void
118 1.72 darrenr ktrinitheader(kth, l, type)
119 1.62 manu struct ktr_header *kth;
120 1.72 darrenr struct lwp *l;
121 1.62 manu int type;
122 1.1 cgd {
123 1.72 darrenr struct proc *p = l->l_proc;
124 1.1 cgd
125 1.39 thorpej memset(kth, 0, sizeof(*kth));
126 1.1 cgd kth->ktr_type = type;
127 1.1 cgd microtime(&kth->ktr_time);
128 1.1 cgd kth->ktr_pid = p->p_pid;
129 1.32 perry memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
130 1.1 cgd }
131 1.1 cgd
132 1.17 cgd void
133 1.72 darrenr ktrsyscall(l, code, realcode, callp, args)
134 1.72 darrenr struct lwp *l;
135 1.62 manu register_t code;
136 1.62 manu register_t realcode;
137 1.66 manu const struct sysent *callp;
138 1.62 manu register_t args[];
139 1.1 cgd {
140 1.72 darrenr struct proc *p = l->l_proc;
141 1.72 darrenr struct ktr_syscall *ktp;
142 1.39 thorpej struct ktr_header kth;
143 1.17 cgd register_t *argp;
144 1.57 fvdl int argsize;
145 1.57 fvdl size_t len;
146 1.60 thorpej u_int i;
147 1.57 fvdl
148 1.66 manu if (callp == NULL)
149 1.66 manu callp = p->p_emul->e_sysent;
150 1.66 manu
151 1.66 manu argsize = callp[code].sy_narg * sizeof (register_t);
152 1.57 fvdl len = sizeof(struct ktr_syscall) + argsize;
153 1.1 cgd
154 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
155 1.72 darrenr ktrinitheader(&kth, l, KTR_SYSCALL);
156 1.39 thorpej ktp = malloc(len, M_TEMP, M_WAITOK);
157 1.61 manu ktp->ktr_code = realcode;
158 1.17 cgd ktp->ktr_argsize = argsize;
159 1.17 cgd argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
160 1.31 perry for (i = 0; i < (argsize / sizeof(*argp)); i++)
161 1.1 cgd *argp++ = args[i];
162 1.39 thorpej kth.ktr_buf = (caddr_t)ktp;
163 1.39 thorpej kth.ktr_len = len;
164 1.72 darrenr (void) ktrwrite(l, &kth);
165 1.39 thorpej free(ktp, M_TEMP);
166 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
167 1.1 cgd }
168 1.1 cgd
169 1.17 cgd void
170 1.72 darrenr ktrsysret(l, code, error, retval)
171 1.72 darrenr struct lwp *l;
172 1.62 manu register_t code;
173 1.62 manu int error;
174 1.71 dsl register_t *retval;
175 1.1 cgd {
176 1.72 darrenr struct proc *p = l->l_proc;
177 1.39 thorpej struct ktr_header kth;
178 1.1 cgd struct ktr_sysret ktp;
179 1.1 cgd
180 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
181 1.72 darrenr ktrinitheader(&kth, l, KTR_SYSRET);
182 1.1 cgd ktp.ktr_code = code;
183 1.34 kleink ktp.ktr_eosys = 0; /* XXX unused */
184 1.1 cgd ktp.ktr_error = error;
185 1.71 dsl ktp.ktr_retval = retval ? retval[0] : 0;
186 1.71 dsl ktp.ktr_retval_1 = retval ? retval[1] : 0;
187 1.1 cgd
188 1.39 thorpej kth.ktr_buf = (caddr_t)&ktp;
189 1.39 thorpej kth.ktr_len = sizeof(struct ktr_sysret);
190 1.1 cgd
191 1.72 darrenr (void) ktrwrite(l, &kth);
192 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
193 1.1 cgd }
194 1.1 cgd
195 1.17 cgd void
196 1.72 darrenr ktrnamei(l, path)
197 1.72 darrenr struct lwp *l;
198 1.62 manu char *path;
199 1.1 cgd {
200 1.72 darrenr struct proc *p = l->l_proc;
201 1.39 thorpej struct ktr_header kth;
202 1.1 cgd
203 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
204 1.72 darrenr ktrinitheader(&kth, l, KTR_NAMEI);
205 1.39 thorpej kth.ktr_len = strlen(path);
206 1.39 thorpej kth.ktr_buf = path;
207 1.18 christos
208 1.72 darrenr (void) ktrwrite(l, &kth);
209 1.18 christos p->p_traceflag &= ~KTRFAC_ACTIVE;
210 1.18 christos }
211 1.18 christos
212 1.18 christos void
213 1.72 darrenr ktremul(l)
214 1.72 darrenr struct lwp *l;
215 1.18 christos {
216 1.39 thorpej struct ktr_header kth;
217 1.72 darrenr const char *emul;
218 1.72 darrenr struct proc *p;
219 1.18 christos
220 1.72 darrenr p = l->l_proc;
221 1.72 darrenr emul = p->p_emul->e_name;
222 1.18 christos p->p_traceflag |= KTRFAC_ACTIVE;
223 1.72 darrenr ktrinitheader(&kth, l, KTR_EMUL);
224 1.39 thorpej kth.ktr_len = strlen(emul);
225 1.50 scw kth.ktr_buf = (caddr_t)emul;
226 1.1 cgd
227 1.72 darrenr (void) ktrwrite(l, &kth);
228 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
229 1.1 cgd }
230 1.1 cgd
231 1.17 cgd void
232 1.72 darrenr ktrgenio(l, fd, rw, iov, len, error)
233 1.72 darrenr struct lwp *l;
234 1.62 manu int fd;
235 1.62 manu enum uio_rw rw;
236 1.62 manu struct iovec *iov;
237 1.62 manu int len;
238 1.62 manu int error;
239 1.1 cgd {
240 1.72 darrenr struct proc *p = l->l_proc;
241 1.39 thorpej struct ktr_header kth;
242 1.28 christos struct ktr_genio *ktp;
243 1.72 darrenr int resid = len, cnt;
244 1.28 christos caddr_t cp;
245 1.39 thorpej int buflen;
246 1.39 thorpej
247 1.1 cgd if (error)
248 1.1 cgd return;
249 1.39 thorpej
250 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
251 1.39 thorpej
252 1.39 thorpej buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
253 1.39 thorpej
254 1.72 darrenr ktrinitheader(&kth, l, KTR_GENIO);
255 1.39 thorpej ktp = malloc(buflen, M_TEMP, M_WAITOK);
256 1.1 cgd ktp->ktr_fd = fd;
257 1.1 cgd ktp->ktr_rw = rw;
258 1.39 thorpej
259 1.39 thorpej kth.ktr_buf = (caddr_t)ktp;
260 1.39 thorpej
261 1.31 perry cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
262 1.39 thorpej buflen -= sizeof(struct ktr_genio);
263 1.39 thorpej
264 1.1 cgd while (resid > 0) {
265 1.67 thorpej #if 0 /* XXX NJWLWP */
266 1.46 thorpej KDASSERT(p->p_cpu != NULL);
267 1.46 thorpej KDASSERT(p->p_cpu == curcpu());
268 1.67 thorpej #endif
269 1.67 thorpej /* XXX NJWLWP */
270 1.67 thorpej if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
271 1.67 thorpej preempt(1);
272 1.39 thorpej
273 1.39 thorpej cnt = min(iov->iov_len, buflen);
274 1.39 thorpej if (cnt > resid)
275 1.1 cgd cnt = resid;
276 1.39 thorpej if (copyin(iov->iov_base, cp, cnt))
277 1.39 thorpej break;
278 1.39 thorpej
279 1.39 thorpej kth.ktr_len = cnt + sizeof(struct ktr_genio);
280 1.39 thorpej
281 1.72 darrenr if (__predict_false(ktrwrite(l, &kth) != 0))
282 1.39 thorpej break;
283 1.39 thorpej
284 1.39 thorpej iov->iov_base = (caddr_t)iov->iov_base + cnt;
285 1.39 thorpej iov->iov_len -= cnt;
286 1.39 thorpej
287 1.39 thorpej if (iov->iov_len == 0)
288 1.39 thorpej iov++;
289 1.39 thorpej
290 1.1 cgd resid -= cnt;
291 1.1 cgd }
292 1.1 cgd
293 1.39 thorpej free(ktp, M_TEMP);
294 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
295 1.1 cgd }
296 1.1 cgd
297 1.17 cgd void
298 1.72 darrenr ktrpsig(l, sig, action, mask, code)
299 1.72 darrenr struct lwp *l;
300 1.62 manu int sig;
301 1.62 manu sig_t action;
302 1.62 manu sigset_t *mask;
303 1.62 manu int code;
304 1.1 cgd {
305 1.72 darrenr struct proc *p = l->l_proc;
306 1.39 thorpej struct ktr_header kth;
307 1.1 cgd struct ktr_psig kp;
308 1.1 cgd
309 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
310 1.72 darrenr ktrinitheader(&kth, l, KTR_PSIG);
311 1.1 cgd kp.signo = (char)sig;
312 1.1 cgd kp.action = action;
313 1.33 mycroft kp.mask = *mask;
314 1.1 cgd kp.code = code;
315 1.39 thorpej kth.ktr_buf = (caddr_t)&kp;
316 1.39 thorpej kth.ktr_len = sizeof(struct ktr_psig);
317 1.1 cgd
318 1.72 darrenr (void) ktrwrite(l, &kth);
319 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
320 1.9 cgd }
321 1.9 cgd
322 1.17 cgd void
323 1.72 darrenr ktrcsw(l, out, user)
324 1.72 darrenr struct lwp *l;
325 1.62 manu int out;
326 1.62 manu int user;
327 1.9 cgd {
328 1.72 darrenr struct proc *p = l->l_proc;
329 1.39 thorpej struct ktr_header kth;
330 1.39 thorpej struct ktr_csw kc;
331 1.9 cgd
332 1.9 cgd p->p_traceflag |= KTRFAC_ACTIVE;
333 1.72 darrenr ktrinitheader(&kth, l, KTR_CSW);
334 1.9 cgd kc.out = out;
335 1.9 cgd kc.user = user;
336 1.39 thorpej kth.ktr_buf = (caddr_t)&kc;
337 1.39 thorpej kth.ktr_len = sizeof(struct ktr_csw);
338 1.9 cgd
339 1.72 darrenr (void) ktrwrite(l, &kth);
340 1.9 cgd p->p_traceflag &= ~KTRFAC_ACTIVE;
341 1.1 cgd }
342 1.1 cgd
343 1.51 jdolecek void
344 1.72 darrenr ktruser(l, id, addr, len, ustr)
345 1.72 darrenr struct lwp *l;
346 1.51 jdolecek const char *id;
347 1.51 jdolecek void *addr;
348 1.51 jdolecek size_t len;
349 1.51 jdolecek int ustr;
350 1.51 jdolecek {
351 1.72 darrenr struct proc *p = l->l_proc;
352 1.51 jdolecek struct ktr_header kth;
353 1.51 jdolecek struct ktr_user *ktp;
354 1.51 jdolecek caddr_t user_dta;
355 1.51 jdolecek
356 1.51 jdolecek p->p_traceflag |= KTRFAC_ACTIVE;
357 1.72 darrenr ktrinitheader(&kth, l, KTR_USER);
358 1.51 jdolecek ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
359 1.51 jdolecek if (ustr) {
360 1.51 jdolecek if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
361 1.51 jdolecek ktp->ktr_id[0] = '\0';
362 1.51 jdolecek } else
363 1.51 jdolecek strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
364 1.51 jdolecek ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
365 1.51 jdolecek
366 1.51 jdolecek user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
367 1.51 jdolecek if (copyin(addr, (void *) user_dta, len) != 0)
368 1.51 jdolecek len = 0;
369 1.51 jdolecek
370 1.51 jdolecek kth.ktr_buf = (void *)ktp;
371 1.51 jdolecek kth.ktr_len = sizeof(struct ktr_user) + len;
372 1.72 darrenr (void) ktrwrite(l, &kth);
373 1.51 jdolecek
374 1.51 jdolecek free(ktp, M_TEMP);
375 1.51 jdolecek p->p_traceflag &= ~KTRFAC_ACTIVE;
376 1.51 jdolecek
377 1.51 jdolecek }
378 1.51 jdolecek
379 1.62 manu void
380 1.72 darrenr ktrmmsg(l, msgh, size)
381 1.72 darrenr struct lwp *l;
382 1.63 christos const void *msgh;
383 1.62 manu size_t size;
384 1.62 manu {
385 1.72 darrenr struct proc *p = l->l_proc;
386 1.62 manu struct ktr_header kth;
387 1.62 manu struct ktr_mmsg *kp;
388 1.62 manu
389 1.62 manu p->p_traceflag |= KTRFAC_ACTIVE;
390 1.72 darrenr ktrinitheader(&kth, l, KTR_MMSG);
391 1.64 manu
392 1.64 manu kp = (struct ktr_mmsg *)msgh;
393 1.62 manu kth.ktr_buf = (caddr_t)kp;
394 1.62 manu kth.ktr_len = size;
395 1.72 darrenr (void) ktrwrite(l, &kth);
396 1.62 manu p->p_traceflag &= ~KTRFAC_ACTIVE;
397 1.62 manu }
398 1.62 manu
399 1.1 cgd /* Interface and common routines */
400 1.1 cgd
401 1.17 cgd int
402 1.72 darrenr ktrace_common(l, ops, facs, pid, fp)
403 1.72 darrenr struct lwp *l;
404 1.62 manu int ops;
405 1.62 manu int facs;
406 1.62 manu int pid;
407 1.62 manu struct file *fp;
408 1.28 christos {
409 1.72 darrenr struct proc *curp = l->l_proc;
410 1.72 darrenr struct pgrp *pg;
411 1.72 darrenr struct proc *p;
412 1.72 darrenr int error = 0;
413 1.42 sommerfe int ret = 0;
414 1.42 sommerfe int one = 1;
415 1.42 sommerfe int descend;
416 1.28 christos
417 1.28 christos curp->p_traceflag |= KTRFAC_ACTIVE;
418 1.42 sommerfe descend = ops & KTRFLAG_DESCEND;
419 1.42 sommerfe facs = facs & ~((unsigned) KTRFAC_ROOT);
420 1.28 christos
421 1.28 christos /*
422 1.28 christos * Clear all uses of the tracefile
423 1.28 christos */
424 1.28 christos if (KTROP(ops) == KTROP_CLEARFILE) {
425 1.37 thorpej proclist_lock_read();
426 1.39 thorpej for (p = LIST_FIRST(&allproc); p != NULL;
427 1.39 thorpej p = LIST_NEXT(p, p_list)) {
428 1.44 sommerfe if (ktrsamefile(p->p_tracep, fp)) {
429 1.28 christos if (ktrcanset(curp, p))
430 1.28 christos ktrderef(p);
431 1.28 christos else
432 1.28 christos error = EPERM;
433 1.28 christos }
434 1.28 christos }
435 1.36 thorpej proclist_unlock_read();
436 1.28 christos goto done;
437 1.28 christos }
438 1.42 sommerfe
439 1.42 sommerfe /*
440 1.42 sommerfe * Mark fp non-blocking, to avoid problems from possible deadlocks.
441 1.42 sommerfe */
442 1.42 sommerfe
443 1.43 sommerfe if (fp != NULL) {
444 1.43 sommerfe fp->f_flag |= FNONBLOCK;
445 1.72 darrenr (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, l);
446 1.43 sommerfe }
447 1.42 sommerfe
448 1.28 christos /*
449 1.28 christos * need something to (un)trace (XXX - why is this here?)
450 1.28 christos */
451 1.28 christos if (!facs) {
452 1.28 christos error = EINVAL;
453 1.28 christos goto done;
454 1.28 christos }
455 1.28 christos /*
456 1.28 christos * do it
457 1.28 christos */
458 1.42 sommerfe if (pid < 0) {
459 1.28 christos /*
460 1.28 christos * by process group
461 1.28 christos */
462 1.42 sommerfe pg = pgfind(-pid);
463 1.28 christos if (pg == NULL) {
464 1.28 christos error = ESRCH;
465 1.28 christos goto done;
466 1.28 christos }
467 1.39 thorpej for (p = LIST_FIRST(&pg->pg_members); p != NULL;
468 1.39 thorpej p = LIST_NEXT(p, p_pglist)) {
469 1.28 christos if (descend)
470 1.72 darrenr ret |= ktrsetchildren(l, p, ops, facs, fp);
471 1.28 christos else
472 1.72 darrenr ret |= ktrops(l, p, ops, facs, fp);
473 1.39 thorpej }
474 1.28 christos
475 1.28 christos } else {
476 1.28 christos /*
477 1.28 christos * by pid
478 1.28 christos */
479 1.42 sommerfe p = pfind(pid);
480 1.28 christos if (p == NULL) {
481 1.28 christos error = ESRCH;
482 1.28 christos goto done;
483 1.28 christos }
484 1.28 christos if (descend)
485 1.72 darrenr ret |= ktrsetchildren(l, p, ops, facs, fp);
486 1.28 christos else
487 1.72 darrenr ret |= ktrops(l, p, ops, facs, fp);
488 1.28 christos }
489 1.28 christos if (!ret)
490 1.28 christos error = EPERM;
491 1.28 christos done:
492 1.28 christos curp->p_traceflag &= ~KTRFAC_ACTIVE;
493 1.28 christos return (error);
494 1.28 christos }
495 1.28 christos
496 1.28 christos /*
497 1.28 christos * ktrace system call
498 1.28 christos */
499 1.28 christos /* ARGSUSED */
500 1.28 christos int
501 1.67 thorpej sys_fktrace(l, v, retval)
502 1.67 thorpej struct lwp *l;
503 1.62 manu void *v;
504 1.62 manu register_t *retval;
505 1.42 sommerfe {
506 1.42 sommerfe struct sys_fktrace_args /* {
507 1.42 sommerfe syscallarg(int) fd;
508 1.42 sommerfe syscallarg(int) ops;
509 1.42 sommerfe syscallarg(int) facs;
510 1.42 sommerfe syscallarg(int) pid;
511 1.42 sommerfe } */ *uap = v;
512 1.72 darrenr struct proc *curp;
513 1.42 sommerfe struct file *fp = NULL;
514 1.72 darrenr struct filedesc *fdp = l->l_proc->p_fd;
515 1.70 yamt int error;
516 1.42 sommerfe
517 1.72 darrenr curp = l->l_proc;
518 1.72 darrenr fdp = curp->p_fd;
519 1.54 thorpej if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
520 1.54 thorpej return (EBADF);
521 1.54 thorpej
522 1.70 yamt FILE_USE(fp);
523 1.70 yamt
524 1.54 thorpej if ((fp->f_flag & FWRITE) == 0)
525 1.70 yamt error = EBADF;
526 1.70 yamt else
527 1.72 darrenr error = ktrace_common(l, SCARG(uap, ops),
528 1.70 yamt SCARG(uap, facs), SCARG(uap, pid), fp);
529 1.70 yamt
530 1.72 darrenr FILE_UNUSE(fp, l);
531 1.42 sommerfe
532 1.70 yamt return error;
533 1.42 sommerfe }
534 1.42 sommerfe
535 1.42 sommerfe /*
536 1.42 sommerfe * ktrace system call
537 1.42 sommerfe */
538 1.42 sommerfe /* ARGSUSED */
539 1.42 sommerfe int
540 1.67 thorpej sys_ktrace(l, v, retval)
541 1.67 thorpej struct lwp *l;
542 1.62 manu void *v;
543 1.62 manu register_t *retval;
544 1.19 thorpej {
545 1.28 christos struct sys_ktrace_args /* {
546 1.24 mycroft syscallarg(const char *) fname;
547 1.13 cgd syscallarg(int) ops;
548 1.13 cgd syscallarg(int) facs;
549 1.13 cgd syscallarg(int) pid;
550 1.19 thorpej } */ *uap = v;
551 1.67 thorpej struct proc *curp = l->l_proc;
552 1.28 christos struct vnode *vp = NULL;
553 1.42 sommerfe struct file *fp = NULL;
554 1.42 sommerfe int ops = SCARG(uap, ops);
555 1.72 darrenr struct nameidata nd;
556 1.1 cgd int error = 0;
557 1.72 darrenr int fd;
558 1.1 cgd
559 1.42 sommerfe ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
560 1.42 sommerfe
561 1.9 cgd curp->p_traceflag |= KTRFAC_ACTIVE;
562 1.69 christos if ((ops & KTROP_CLEAR) == 0) {
563 1.1 cgd /*
564 1.1 cgd * an operation which requires a file argument.
565 1.1 cgd */
566 1.13 cgd NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
567 1.72 darrenr l);
568 1.22 christos if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
569 1.9 cgd curp->p_traceflag &= ~KTRFAC_ACTIVE;
570 1.1 cgd return (error);
571 1.9 cgd }
572 1.1 cgd vp = nd.ni_vp;
573 1.25 fvdl VOP_UNLOCK(vp, 0);
574 1.1 cgd if (vp->v_type != VREG) {
575 1.72 darrenr (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, l);
576 1.9 cgd curp->p_traceflag &= ~KTRFAC_ACTIVE;
577 1.1 cgd return (EACCES);
578 1.1 cgd }
579 1.1 cgd /*
580 1.42 sommerfe * XXX This uses up a file descriptor slot in the
581 1.42 sommerfe * tracing process for the duration of this syscall.
582 1.42 sommerfe * This is not expected to be a problem. If
583 1.42 sommerfe * falloc(NULL, ...) DTRT we could skip that part, but
584 1.42 sommerfe * that would require changing its interface to allow
585 1.42 sommerfe * the caller to pass in a ucred..
586 1.42 sommerfe *
587 1.42 sommerfe * This will FILE_USE the fp it returns, if any.
588 1.42 sommerfe * Keep it in use until we return.
589 1.1 cgd */
590 1.42 sommerfe if ((error = falloc(curp, &fp, &fd)) != 0)
591 1.1 cgd goto done;
592 1.42 sommerfe
593 1.42 sommerfe fp->f_flag = FWRITE|FAPPEND;
594 1.42 sommerfe fp->f_type = DTYPE_VNODE;
595 1.42 sommerfe fp->f_ops = &vnops;
596 1.42 sommerfe fp->f_data = (caddr_t)vp;
597 1.54 thorpej FILE_SET_MATURE(fp);
598 1.42 sommerfe vp = NULL;
599 1.42 sommerfe }
600 1.72 darrenr error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
601 1.42 sommerfe SCARG(uap, pid), fp);
602 1.42 sommerfe done:
603 1.1 cgd if (vp != NULL)
604 1.72 darrenr (void) vn_close(vp, FWRITE, curp->p_ucred, l);
605 1.42 sommerfe if (fp != NULL) {
606 1.72 darrenr FILE_UNUSE(fp, l); /* release file */
607 1.72 darrenr fdrelease(l, fd); /* release fd table slot */
608 1.42 sommerfe }
609 1.1 cgd return (error);
610 1.1 cgd }
611 1.1 cgd
612 1.4 andrew int
613 1.72 darrenr ktrops(l, p, ops, facs, fp)
614 1.72 darrenr struct lwp *l;
615 1.62 manu struct proc *p;
616 1.62 manu int ops;
617 1.62 manu int facs;
618 1.62 manu struct file *fp;
619 1.1 cgd {
620 1.72 darrenr struct proc *curp = l->l_proc;
621 1.1 cgd
622 1.1 cgd if (!ktrcanset(curp, p))
623 1.1 cgd return (0);
624 1.28 christos if (KTROP(ops) == KTROP_SET) {
625 1.42 sommerfe if (p->p_tracep != fp) {
626 1.1 cgd /*
627 1.1 cgd * if trace file already in use, relinquish
628 1.1 cgd */
629 1.28 christos ktrderef(p);
630 1.42 sommerfe p->p_tracep = fp;
631 1.28 christos ktradref(p);
632 1.1 cgd }
633 1.1 cgd p->p_traceflag |= facs;
634 1.1 cgd if (curp->p_ucred->cr_uid == 0)
635 1.1 cgd p->p_traceflag |= KTRFAC_ROOT;
636 1.1 cgd } else {
637 1.1 cgd /* KTROP_CLEAR */
638 1.1 cgd if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
639 1.1 cgd /* no more tracing */
640 1.28 christos ktrderef(p);
641 1.1 cgd }
642 1.1 cgd }
643 1.21 christos
644 1.21 christos /*
645 1.21 christos * Emit an emulation record, every time there is a ktrace
646 1.21 christos * change/attach request.
647 1.21 christos */
648 1.21 christos if (KTRPOINT(p, KTR_EMUL))
649 1.72 darrenr ktremul(l);
650 1.49 martin #ifdef __HAVE_SYSCALL_INTERN
651 1.48 mycroft (*p->p_emul->e_syscall_intern)(p);
652 1.49 martin #endif
653 1.1 cgd
654 1.1 cgd return (1);
655 1.1 cgd }
656 1.1 cgd
657 1.22 christos int
658 1.72 darrenr ktrsetchildren(l, top, ops, facs, fp)
659 1.72 darrenr struct lwp *l;
660 1.62 manu struct proc *top;
661 1.62 manu int ops;
662 1.62 manu int facs;
663 1.62 manu struct file *fp;
664 1.1 cgd {
665 1.28 christos struct proc *p;
666 1.28 christos int ret = 0;
667 1.1 cgd
668 1.1 cgd p = top;
669 1.1 cgd for (;;) {
670 1.72 darrenr ret |= ktrops(l, p, ops, facs, fp);
671 1.1 cgd /*
672 1.1 cgd * If this process has children, descend to them next,
673 1.1 cgd * otherwise do any siblings, and if done with this level,
674 1.1 cgd * follow back up the tree (but not past top).
675 1.1 cgd */
676 1.39 thorpej if (LIST_FIRST(&p->p_children) != NULL)
677 1.39 thorpej p = LIST_FIRST(&p->p_children);
678 1.1 cgd else for (;;) {
679 1.1 cgd if (p == top)
680 1.1 cgd return (ret);
681 1.39 thorpej if (LIST_NEXT(p, p_sibling) != NULL) {
682 1.39 thorpej p = LIST_NEXT(p, p_sibling);
683 1.1 cgd break;
684 1.1 cgd }
685 1.12 mycroft p = p->p_pptr;
686 1.1 cgd }
687 1.1 cgd }
688 1.1 cgd /*NOTREACHED*/
689 1.1 cgd }
690 1.1 cgd
691 1.39 thorpej int
692 1.72 darrenr ktrwrite(l, kth)
693 1.72 darrenr struct lwp *l;
694 1.62 manu struct ktr_header *kth;
695 1.1 cgd {
696 1.1 cgd struct iovec aiov[2];
697 1.42 sommerfe int error, tries;
698 1.72 darrenr struct uio auio;
699 1.72 darrenr struct file *fp;
700 1.72 darrenr struct proc *p;
701 1.72 darrenr
702 1.72 darrenr p = l->l_proc;
703 1.72 darrenr fp = p->p_tracep;
704 1.1 cgd
705 1.42 sommerfe if (fp == NULL)
706 1.42 sommerfe return 0;
707 1.42 sommerfe
708 1.1 cgd auio.uio_iov = &aiov[0];
709 1.1 cgd auio.uio_offset = 0;
710 1.1 cgd auio.uio_segflg = UIO_SYSSPACE;
711 1.1 cgd auio.uio_rw = UIO_WRITE;
712 1.1 cgd aiov[0].iov_base = (caddr_t)kth;
713 1.1 cgd aiov[0].iov_len = sizeof(struct ktr_header);
714 1.1 cgd auio.uio_resid = sizeof(struct ktr_header);
715 1.1 cgd auio.uio_iovcnt = 1;
716 1.72 darrenr auio.uio_lwp = (struct lwp *)0;
717 1.1 cgd if (kth->ktr_len > 0) {
718 1.1 cgd auio.uio_iovcnt++;
719 1.1 cgd aiov[1].iov_base = kth->ktr_buf;
720 1.1 cgd aiov[1].iov_len = kth->ktr_len;
721 1.1 cgd auio.uio_resid += kth->ktr_len;
722 1.1 cgd }
723 1.72 darrenr kth->ktr_buf = (caddr_t)l->l_lid;
724 1.28 christos
725 1.68 pk simple_lock(&fp->f_slock);
726 1.42 sommerfe FILE_USE(fp);
727 1.42 sommerfe
728 1.42 sommerfe tries = 0;
729 1.42 sommerfe do {
730 1.30 thorpej error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
731 1.30 thorpej fp->f_cred, FOF_UPDATE_OFFSET);
732 1.42 sommerfe if (error == EWOULDBLOCK)
733 1.67 thorpej preempt(1);
734 1.72 darrenr else
735 1.72 darrenr tries++;
736 1.42 sommerfe } while ((error == EWOULDBLOCK) && (tries < 3));
737 1.42 sommerfe FILE_UNUSE(fp, NULL);
738 1.28 christos
739 1.40 thorpej if (__predict_true(error == 0))
740 1.39 thorpej return (0);
741 1.1 cgd /*
742 1.38 darrenr * If error encountered, give up tracing on this vnode. Don't report
743 1.38 darrenr * EPIPE as this can easily happen with fktrace()/ktruss.
744 1.1 cgd */
745 1.38 darrenr if (error != EPIPE)
746 1.38 darrenr log(LOG_NOTICE,
747 1.38 darrenr "ktrace write failed, errno %d, tracing stopped\n",
748 1.38 darrenr error);
749 1.37 thorpej proclist_lock_read();
750 1.39 thorpej for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
751 1.44 sommerfe if (ktrsamefile(p->p_tracep, fp))
752 1.28 christos ktrderef(p);
753 1.1 cgd }
754 1.36 thorpej proclist_unlock_read();
755 1.39 thorpej
756 1.39 thorpej return (error);
757 1.1 cgd }
758 1.1 cgd
759 1.1 cgd /*
760 1.1 cgd * Return true if caller has permission to set the ktracing state
761 1.1 cgd * of target. Essentially, the target can't possess any
762 1.1 cgd * more permissions than the caller. KTRFAC_ROOT signifies that
763 1.1 cgd * root previously set the tracing status on the target process, and
764 1.1 cgd * so, only root may further change it.
765 1.1 cgd *
766 1.1 cgd * TODO: check groups. use caller effective gid.
767 1.1 cgd */
768 1.22 christos int
769 1.62 manu ktrcanset(callp, targetp)
770 1.62 manu struct proc *callp;
771 1.62 manu struct proc *targetp;
772 1.1 cgd {
773 1.28 christos struct pcred *caller = callp->p_cred;
774 1.28 christos struct pcred *target = targetp->p_cred;
775 1.1 cgd
776 1.1 cgd if ((caller->pc_ucred->cr_uid == target->p_ruid &&
777 1.1 cgd target->p_ruid == target->p_svuid &&
778 1.1 cgd caller->p_rgid == target->p_rgid && /* XXX */
779 1.1 cgd target->p_rgid == target->p_svgid &&
780 1.58 itojun (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
781 1.58 itojun (targetp->p_flag & P_SUGID) == 0) ||
782 1.1 cgd caller->pc_ucred->cr_uid == 0)
783 1.1 cgd return (1);
784 1.1 cgd
785 1.1 cgd return (0);
786 1.1 cgd }
787 1.47 thorpej #endif /* KTRACE */
788 1.51 jdolecek
789 1.51 jdolecek /*
790 1.51 jdolecek * Put user defined entry to ktrace records.
791 1.51 jdolecek */
792 1.51 jdolecek int
793 1.67 thorpej sys_utrace(l, v, retval)
794 1.67 thorpej struct lwp *l;
795 1.51 jdolecek void *v;
796 1.51 jdolecek register_t *retval;
797 1.51 jdolecek {
798 1.51 jdolecek #ifdef KTRACE
799 1.51 jdolecek struct sys_utrace_args /* {
800 1.52 jdolecek syscallarg(const char *) label;
801 1.51 jdolecek syscallarg(void *) addr;
802 1.51 jdolecek syscallarg(size_t) len;
803 1.51 jdolecek } */ *uap = v;
804 1.67 thorpej struct proc *p = l->l_proc;
805 1.51 jdolecek if (!KTRPOINT(p, KTR_USER))
806 1.51 jdolecek return (0);
807 1.53 jdolecek
808 1.53 jdolecek if (SCARG(uap, len) > KTR_USER_MAXLEN)
809 1.53 jdolecek return (EINVAL);
810 1.51 jdolecek
811 1.72 darrenr ktruser(l, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
812 1.51 jdolecek
813 1.51 jdolecek return (0);
814 1.51 jdolecek #else /* !KTRACE */
815 1.51 jdolecek return ENOSYS;
816 1.51 jdolecek #endif /* KTRACE */
817 1.51 jdolecek }
818