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