2005-12-24 22:12:23 +03:00
|
|
|
/* $NetBSD: kern_ktrace.c,v 1.100 2005/12/24 19:12:23 perry Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-18 09:12:10 +04:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2005-12-24 22:12:23 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.100 2005/12/24 19:12:23 perry Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1998-06-26 01:17:15 +04:00
|
|
|
#include "opt_ktrace.h"
|
2002-12-10 00:29:20 +03:00
|
|
|
#include "opt_compat_mach.h"
|
1998-06-26 01:17:15 +04:00
|
|
|
|
1993-12-18 06:59:02 +03:00
|
|
|
#include <sys/param.h>
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/systm.h>
|
1993-12-18 06:59:02 +03:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
2004-09-23 02:15:03 +04:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/kthread.h>
|
1993-12-18 06:59:02 +03:00
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/syslog.h>
|
1998-05-02 22:33:19 +04:00
|
|
|
#include <sys/filedesc.h>
|
2000-05-27 04:40:29 +04:00
|
|
|
#include <sys/ioctl.h>
|
2004-09-23 02:15:03 +04:00
|
|
|
#include <sys/callout.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/mount.h>
|
2003-01-18 13:06:22 +03:00
|
|
|
#include <sys/sa.h>
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
2000-12-28 14:10:15 +03:00
|
|
|
#ifdef KTRACE
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
/*
|
|
|
|
* XXX:
|
|
|
|
* - need better error reporting?
|
|
|
|
* - p->p_tracep access lock. lock p_lock, lock ktd if !NULL, inc ref.
|
|
|
|
* - userland utility to sort ktrace.out by timestamp.
|
|
|
|
* - keep minimum information in ktrace_entry when rest of alloc failed.
|
|
|
|
* - enlarge ktrace_entry so that small entry won't require additional
|
|
|
|
* alloc?
|
|
|
|
* - per trace control of configurable parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ktrace_entry {
|
|
|
|
TAILQ_ENTRY(ktrace_entry) kte_list;
|
|
|
|
struct ktr_header kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
void *kte_buf;
|
2004-09-23 02:15:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ktr_desc {
|
|
|
|
TAILQ_ENTRY(ktr_desc) ktd_list;
|
|
|
|
int ktd_flags;
|
|
|
|
#define KTDF_WAIT 0x0001
|
|
|
|
#define KTDF_DONE 0x0002
|
|
|
|
#define KTDF_BLOCKING 0x0004
|
|
|
|
#define KTDF_INTERACTIVE 0x0008
|
|
|
|
int ktd_error;
|
|
|
|
#define KTDE_ENOMEM 0x0001
|
|
|
|
#define KTDE_ENOSPC 0x0002
|
|
|
|
int ktd_errcnt;
|
|
|
|
int ktd_ref; /* # of reference */
|
|
|
|
int ktd_qcount; /* # of entry in the queue */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Params to control behaviour.
|
|
|
|
*/
|
|
|
|
int ktd_delayqcnt; /* # of entry allowed to delay */
|
|
|
|
int ktd_wakedelay; /* delay of wakeup in *tick* */
|
|
|
|
int ktd_intrwakdl; /* ditto, but when interactive */
|
|
|
|
|
|
|
|
struct file *ktd_fp; /* trace output file */
|
|
|
|
struct proc *ktd_proc; /* our kernel thread */
|
|
|
|
TAILQ_HEAD(, ktrace_entry) ktd_queue;
|
|
|
|
struct callout ktd_wakch; /* delayed wakeup */
|
|
|
|
struct simplelock ktd_slock;
|
|
|
|
};
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
static void ktrinitheader(struct ktr_header *, struct lwp *, int);
|
2004-09-23 02:15:03 +04:00
|
|
|
static void ktrwrite(struct ktr_desc *, struct ktrace_entry *);
|
|
|
|
static int ktrace_common(struct proc *, int, int, int, struct file *);
|
|
|
|
static int ktrops(struct proc *, struct proc *, int, int,
|
|
|
|
struct ktr_desc *);
|
|
|
|
static int ktrsetchildren(struct proc *, struct proc *, int, int,
|
|
|
|
struct ktr_desc *);
|
|
|
|
static int ktrcanset(struct proc *, struct proc *);
|
|
|
|
static int ktrsamefile(struct file *, struct file *);
|
|
|
|
|
|
|
|
static struct ktr_desc *
|
|
|
|
ktd_lookup(struct file *);
|
|
|
|
static void ktdrel(struct ktr_desc *);
|
|
|
|
static void ktdref(struct ktr_desc *);
|
2005-12-11 15:16:03 +03:00
|
|
|
static void ktraddentry(struct lwp *, struct ktrace_entry *, int);
|
2004-09-23 02:15:03 +04:00
|
|
|
/* Flags for ktraddentry (3rd arg) */
|
|
|
|
#define KTA_NOWAIT 0x0000
|
|
|
|
#define KTA_WAITOK 0x0001
|
|
|
|
#define KTA_LARGE 0x0002
|
|
|
|
static void ktefree(struct ktrace_entry *);
|
|
|
|
static void ktd_logerrl(struct ktr_desc *, int);
|
|
|
|
static void ktd_logerr(struct proc *, int);
|
|
|
|
static void ktrace_thread(void *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default vaules.
|
|
|
|
*/
|
|
|
|
#define KTD_MAXENTRY 1000 /* XXX: tune */
|
|
|
|
#define KTD_TIMEOUT 5 /* XXX: tune */
|
|
|
|
#define KTD_DELAYQCNT 100 /* XXX: tune */
|
|
|
|
#define KTD_WAKEDELAY 5000 /* XXX: tune */
|
|
|
|
#define KTD_INTRWAKDL 100 /* XXX: tune */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Patchable variables.
|
|
|
|
*/
|
|
|
|
int ktd_maxentry = KTD_MAXENTRY; /* max # of entry in the queue */
|
|
|
|
int ktd_timeout = KTD_TIMEOUT; /* timeout in seconds */
|
|
|
|
int ktd_delayqcnt = KTD_DELAYQCNT; /* # of entry allowed to delay */
|
|
|
|
int ktd_wakedelay = KTD_WAKEDELAY; /* delay of wakeup in *ms* */
|
|
|
|
int ktd_intrwakdl = KTD_INTRWAKDL; /* ditto, but when interactive */
|
|
|
|
|
|
|
|
static struct simplelock ktdq_slock = SIMPLELOCK_INITIALIZER;
|
|
|
|
static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq);
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_KTRACE, "ktrace", "ktrace data buffer");
|
|
|
|
POOL_INIT(kte_pool, sizeof(struct ktrace_entry), 0, 0, 0,
|
|
|
|
"ktepl", &pool_allocator_nointr);
|
|
|
|
|
2005-12-24 22:12:23 +03:00
|
|
|
static inline void
|
2004-09-23 02:15:03 +04:00
|
|
|
ktd_wakeup(struct ktr_desc *ktd)
|
|
|
|
{
|
|
|
|
|
|
|
|
callout_stop(&ktd->ktd_wakch);
|
|
|
|
wakeup(ktd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ktd_logerrl(struct ktr_desc *ktd, int error)
|
|
|
|
{
|
|
|
|
|
|
|
|
ktd->ktd_error |= error;
|
|
|
|
ktd->ktd_errcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ktd_logerr(struct proc *p, int error)
|
|
|
|
{
|
|
|
|
struct ktr_desc *ktd = p->p_tracep;
|
|
|
|
|
|
|
|
if (ktd == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
ktd_logerrl(ktd, error);
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release a reference. Called with ktd_slock held.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ktdrel(struct ktr_desc *ktd)
|
|
|
|
{
|
|
|
|
|
|
|
|
KDASSERT(ktd->ktd_ref != 0);
|
|
|
|
KASSERT(ktd->ktd_ref > 0);
|
|
|
|
if (--ktd->ktd_ref <= 0) {
|
|
|
|
ktd->ktd_flags |= KTDF_DONE;
|
|
|
|
wakeup(ktd);
|
|
|
|
}
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ktdref(struct ktr_desc *ktd)
|
|
|
|
{
|
|
|
|
|
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
ktd->ktd_ref++;
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ktr_desc *
|
|
|
|
ktd_lookup(struct file *fp)
|
|
|
|
{
|
|
|
|
struct ktr_desc *ktd;
|
|
|
|
|
|
|
|
simple_lock(&ktdq_slock);
|
|
|
|
for (ktd = TAILQ_FIRST(&ktdq); ktd != NULL;
|
|
|
|
ktd = TAILQ_NEXT(ktd, ktd_list)) {
|
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
if (ktrsamefile(ktd->ktd_fp, fp)) {
|
|
|
|
ktd->ktd_ref++;
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
}
|
|
|
|
simple_unlock(&ktdq_slock);
|
|
|
|
return (ktd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(struct lwp *l, struct ktrace_entry *kte, int flags)
|
2004-09-23 02:15:03 +04:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktr_desc *ktd;
|
|
|
|
#ifdef DEBUG
|
|
|
|
struct timeval t;
|
|
|
|
int s;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (p->p_traceflag & KTRFAC_TRC_EMUL) {
|
|
|
|
/* Add emulation trace before first entry for this process */
|
|
|
|
p->p_traceflag &= ~KTRFAC_TRC_EMUL;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktremul(l);
|
2004-09-23 02:15:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tracing may be canceled while we were sleeping waiting for
|
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
ktd = p->p_tracep;
|
|
|
|
if (ktd == NULL)
|
|
|
|
goto freekte;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bump reference count so that the object will remain while
|
|
|
|
* we are here. Note that the trace is controlled by other
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
ktdref(ktd);
|
|
|
|
|
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
if (ktd->ktd_flags & KTDF_DONE)
|
|
|
|
goto relktd;
|
|
|
|
|
|
|
|
if (ktd->ktd_qcount > ktd_maxentry) {
|
|
|
|
ktd_logerrl(ktd, KTDE_ENOSPC);
|
|
|
|
goto relktd;
|
|
|
|
}
|
|
|
|
TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list);
|
|
|
|
ktd->ktd_qcount++;
|
|
|
|
if (ktd->ktd_flags & KTDF_BLOCKING)
|
|
|
|
goto skip_sync;
|
|
|
|
|
|
|
|
if (flags & KTA_WAITOK &&
|
|
|
|
(/* flags & KTA_LARGE */0 || ktd->ktd_flags & KTDF_WAIT ||
|
|
|
|
ktd->ktd_qcount > ktd_maxentry >> 1))
|
|
|
|
/*
|
|
|
|
* Sync with writer thread since we're requesting rather
|
|
|
|
* big one or many requests are pending.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
ktd->ktd_flags |= KTDF_WAIT;
|
|
|
|
ktd_wakeup(ktd);
|
|
|
|
#ifdef DEBUG
|
|
|
|
s = splclock();
|
|
|
|
t = mono_time;
|
|
|
|
splx(s);
|
|
|
|
#endif
|
|
|
|
if (ltsleep(&ktd->ktd_flags, PWAIT, "ktrsync",
|
|
|
|
ktd_timeout * hz, &ktd->ktd_slock) != 0) {
|
|
|
|
ktd->ktd_flags |= KTDF_BLOCKING;
|
|
|
|
/*
|
|
|
|
* Maybe the writer thread is blocking
|
|
|
|
* completely for some reason, but
|
|
|
|
* don't stop target process forever.
|
|
|
|
*/
|
|
|
|
log(LOG_NOTICE, "ktrace timeout\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
s = splclock();
|
|
|
|
timersub(&mono_time, &t, &t);
|
|
|
|
splx(s);
|
|
|
|
if (t.tv_sec > 0)
|
|
|
|
log(LOG_NOTICE,
|
|
|
|
"ktrace long wait: %ld.%06ld\n",
|
|
|
|
t.tv_sec, t.tv_usec);
|
|
|
|
#endif
|
|
|
|
} while (p->p_tracep == ktd &&
|
|
|
|
(ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT);
|
|
|
|
else {
|
|
|
|
/* Schedule delayed wakeup */
|
|
|
|
if (ktd->ktd_qcount > ktd->ktd_delayqcnt)
|
|
|
|
ktd_wakeup(ktd); /* Wakeup now */
|
|
|
|
else if (!callout_pending(&ktd->ktd_wakch))
|
|
|
|
callout_reset(&ktd->ktd_wakch,
|
|
|
|
ktd->ktd_flags & KTDF_INTERACTIVE ?
|
|
|
|
ktd->ktd_intrwakdl : ktd->ktd_wakedelay,
|
|
|
|
(void (*)(void *))wakeup, ktd);
|
|
|
|
}
|
|
|
|
|
|
|
|
skip_sync:
|
|
|
|
ktdrel(ktd);
|
|
|
|
return;
|
|
|
|
|
|
|
|
relktd:
|
|
|
|
ktdrel(ktd);
|
|
|
|
|
|
|
|
freekte:
|
|
|
|
ktefree(kte);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ktefree(struct ktrace_entry *kte)
|
|
|
|
{
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if (kte->kte_buf != NULL)
|
2004-09-23 02:15:03 +04:00
|
|
|
free(kte->kte_buf, M_KTRACE);
|
|
|
|
pool_put(&kte_pool, kte);
|
|
|
|
}
|
2000-05-30 02:04:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "deep" compare of two files for the purposes of clearing a trace.
|
|
|
|
* Returns true if they're the same open file, or if they point at the
|
|
|
|
* same underlying vnode/socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
ktrsamefile(struct file *f1, struct file *f2)
|
2000-05-30 02:04:11 +04:00
|
|
|
{
|
2004-02-26 00:40:40 +03:00
|
|
|
|
2000-05-30 02:04:11 +04:00
|
|
|
return ((f1 == f2) ||
|
2000-05-30 02:29:01 +04:00
|
|
|
((f1 != NULL) && (f2 != NULL) &&
|
|
|
|
(f1->f_type == f2->f_type) &&
|
2000-05-30 02:04:11 +04:00
|
|
|
(f1->f_data == f2->f_data)));
|
|
|
|
}
|
1996-02-04 05:15:01 +03:00
|
|
|
|
1998-05-02 22:33:19 +04:00
|
|
|
void
|
2004-04-30 11:51:59 +04:00
|
|
|
ktrderef(struct proc *p)
|
1998-05-02 22:33:19 +04:00
|
|
|
{
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktr_desc *ktd = p->p_tracep;
|
|
|
|
|
2000-05-27 04:40:29 +04:00
|
|
|
p->p_traceflag = 0;
|
2004-09-23 02:15:03 +04:00
|
|
|
if (ktd == NULL)
|
1998-05-02 22:33:19 +04:00
|
|
|
return;
|
2003-12-15 01:56:45 +03:00
|
|
|
p->p_tracep = NULL;
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
wakeup(&ktd->ktd_flags);
|
|
|
|
ktdrel(ktd);
|
1998-05-02 22:33:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-30 11:51:59 +04:00
|
|
|
ktradref(struct proc *p)
|
1998-05-02 22:33:19 +04:00
|
|
|
{
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktr_desc *ktd = p->p_tracep;
|
1998-05-02 22:33:19 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
ktdref(ktd);
|
1998-05-02 22:33:19 +04:00
|
|
|
}
|
|
|
|
|
2000-04-19 23:14:17 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(struct ktr_header *kth, struct lwp *l, int type)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-06-24 03:05:48 +04:00
|
|
|
(void)memset(kth, 0, sizeof(*kth));
|
1993-03-21 12:45:37 +03:00
|
|
|
kth->ktr_type = type;
|
|
|
|
kth->ktr_pid = p->p_pid;
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
|
2005-12-11 15:16:03 +03:00
|
|
|
|
|
|
|
kth->ktr_version = KTRFAC_VERSION(p->p_traceflag);
|
|
|
|
|
|
|
|
switch (KTRFAC_VERSION(p->p_traceflag)) {
|
|
|
|
case 0:
|
|
|
|
/* This is the original format */
|
|
|
|
microtime(&kth->ktr_tv);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
kth->ktr_lid = l->l_lid;
|
|
|
|
nanotime(&kth->ktr_time);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrsyscall(struct lwp *l, register_t code, register_t realcode,
|
2004-04-30 11:51:59 +04:00
|
|
|
const struct sysent *callp, register_t args[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
2003-06-30 02:28:00 +04:00
|
|
|
struct ktr_syscall *ktp;
|
1995-03-26 11:48:47 +04:00
|
|
|
register_t *argp;
|
2004-09-23 02:15:03 +04:00
|
|
|
int argsize;
|
2002-06-18 12:01:30 +04:00
|
|
|
size_t len;
|
2002-11-10 06:28:59 +03:00
|
|
|
u_int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-12-21 19:23:56 +03:00
|
|
|
if (callp == NULL)
|
|
|
|
callp = p->p_emul->e_sysent;
|
2004-02-26 00:40:40 +03:00
|
|
|
|
2003-08-04 22:53:06 +04:00
|
|
|
argsize = callp[code].sy_argsize;
|
2004-01-16 08:03:02 +03:00
|
|
|
#ifdef _LP64
|
|
|
|
if (p->p_flag & P_32)
|
|
|
|
argsize = argsize << 1;
|
|
|
|
#endif
|
2002-06-18 12:01:30 +04:00
|
|
|
len = sizeof(struct ktr_syscall) + argsize;
|
|
|
|
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_SYSCALL);
|
2004-09-23 02:15:03 +04:00
|
|
|
|
|
|
|
ktp = malloc(len, M_KTRACE, M_WAITOK);
|
2002-11-15 23:06:00 +03:00
|
|
|
ktp->ktr_code = realcode;
|
1995-03-26 11:48:47 +04:00
|
|
|
ktp->ktr_argsize = argsize;
|
2004-09-23 02:15:03 +04:00
|
|
|
argp = (register_t *)(ktp + 1);
|
1998-08-01 02:50:48 +04:00
|
|
|
for (i = 0; i < (argsize / sizeof(*argp)); i++)
|
1993-03-21 12:45:37 +03:00
|
|
|
*argp++ = args[i];
|
2004-09-23 02:15:03 +04:00
|
|
|
kth->ktr_len = len;
|
|
|
|
kte->kte_buf = ktp;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrsysret(struct lwp *l, register_t code, int error, register_t *retval)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
|
|
|
struct ktr_sysret *ktp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_SYSRET);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
ktp = malloc(sizeof(struct ktr_sysret), M_KTRACE, M_WAITOK);
|
|
|
|
ktp->ktr_code = code;
|
|
|
|
ktp->ktr_eosys = 0; /* XXX unused */
|
|
|
|
ktp->ktr_error = error;
|
|
|
|
ktp->ktr_retval = retval ? retval[0] : 0;
|
|
|
|
ktp->ktr_retval_1 = retval ? retval[1] : 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
kth->ktr_len = sizeof(struct ktr_sysret);
|
|
|
|
kte->kte_buf = ktp;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
/*
|
|
|
|
* XXX: ndp->ni_pathlen should be passed.
|
|
|
|
*/
|
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrnamei(struct lwp *l, char *path)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrkmem(l, KTR_NAMEI, path, strlen(path));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktremul(struct lwp *l)
|
1995-07-19 19:19:08 +04:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
const char *emul = l->l_proc->p_emul->e_name;
|
1995-07-19 19:19:08 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrkmem(l, KTR_EMUL, emul, strlen(emul));
|
1995-07-19 19:19:08 +04:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrkmem(struct lwp *l, int type, const void *bf, size_t len)
|
2003-07-17 02:42:47 +04:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
2003-07-17 02:42:47 +04:00
|
|
|
|
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, type);
|
2003-07-17 02:42:47 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
kth->ktr_len = len;
|
|
|
|
kte->kte_buf = malloc(len, M_KTRACE, M_WAITOK);
|
2005-05-30 02:24:14 +04:00
|
|
|
memcpy(kte->kte_buf, bf, len);
|
2004-09-23 02:15:03 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
2003-07-17 02:42:47 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrgenio(struct lwp *l, int fd, enum uio_rw rw, struct iovec *iov,
|
2004-04-30 11:51:59 +04:00
|
|
|
int len, int error)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
1998-05-02 22:33:19 +04:00
|
|
|
struct ktr_genio *ktp;
|
2003-06-30 02:28:00 +04:00
|
|
|
int resid = len, cnt;
|
2005-12-11 15:16:03 +03:00
|
|
|
caddr_t cp;
|
2000-04-19 23:14:17 +04:00
|
|
|
int buflen;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (error)
|
2004-09-23 02:15:03 +04:00
|
|
|
return;
|
2000-04-19 23:14:17 +04:00
|
|
|
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
next:
|
|
|
|
buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
|
|
|
|
|
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_GENIO);
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
ktp = malloc(buflen, M_KTRACE, M_WAITOK);
|
1993-03-21 12:45:37 +03:00
|
|
|
ktp->ktr_fd = fd;
|
|
|
|
ktp->ktr_rw = rw;
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
kte->kte_buf = ktp;
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
cp = (caddr_t)(ktp + 1);
|
2000-04-19 23:14:17 +04:00
|
|
|
buflen -= sizeof(struct ktr_genio);
|
2004-09-23 02:15:03 +04:00
|
|
|
kth->ktr_len = sizeof(struct ktr_genio);
|
|
|
|
|
|
|
|
while (buflen > 0) {
|
|
|
|
cnt = min(iov->iov_len, buflen);
|
|
|
|
if (copyin(iov->iov_base, cp, cnt) != 0)
|
|
|
|
goto out;
|
|
|
|
kth->ktr_len += cnt;
|
|
|
|
buflen -= cnt;
|
|
|
|
resid -= cnt;
|
|
|
|
iov->iov_len -= cnt;
|
|
|
|
if (iov->iov_len == 0)
|
|
|
|
iov++;
|
|
|
|
else
|
|
|
|
iov->iov_base = (caddr_t)iov->iov_base + cnt;
|
|
|
|
}
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
/*
|
|
|
|
* Don't push so many entry at once. It will cause kmem map
|
|
|
|
* shortage.
|
|
|
|
*/
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
|
2004-09-23 02:15:03 +04:00
|
|
|
if (resid > 0) {
|
2003-01-18 13:06:22 +03:00
|
|
|
#if 0 /* XXX NJWLWP */
|
2000-05-31 09:02:31 +04:00
|
|
|
KDASSERT(p->p_cpu != NULL);
|
|
|
|
KDASSERT(p->p_cpu == curcpu());
|
2003-01-18 13:06:22 +03:00
|
|
|
#endif
|
|
|
|
/* XXX NJWLWP */
|
|
|
|
if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
|
|
|
|
preempt(1);
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
goto next;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
ktefree(kte);
|
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrpsig(struct lwp *l, int sig, sig_t action, const sigset_t *mask,
|
2004-04-30 11:51:59 +04:00
|
|
|
const ksiginfo_t *ksi)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
2003-09-20 02:50:02 +04:00
|
|
|
struct {
|
|
|
|
struct ktr_psig kp;
|
|
|
|
siginfo_t si;
|
2004-09-23 02:15:03 +04:00
|
|
|
} *kbuf;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_PSIG);
|
2004-09-23 02:15:03 +04:00
|
|
|
|
|
|
|
kbuf = malloc(sizeof(*kbuf), M_KTRACE, M_WAITOK);
|
|
|
|
kbuf->kp.signo = (char)sig;
|
|
|
|
kbuf->kp.action = action;
|
|
|
|
kbuf->kp.mask = *mask;
|
|
|
|
kte->kte_buf = kbuf;
|
2003-09-20 02:50:02 +04:00
|
|
|
if (ksi) {
|
2004-09-23 02:15:03 +04:00
|
|
|
kbuf->kp.code = KSI_TRAPCODE(ksi);
|
|
|
|
(void)memset(&kbuf->si, 0, sizeof(kbuf->si));
|
|
|
|
kbuf->si._info = ksi->ksi_info;
|
2004-10-26 10:58:05 +04:00
|
|
|
kth->ktr_len = sizeof(*kbuf);
|
2003-09-20 02:50:02 +04:00
|
|
|
} else {
|
2004-09-23 02:15:03 +04:00
|
|
|
kbuf->kp.code = 0;
|
|
|
|
kth->ktr_len = sizeof(struct ktr_psig);
|
2003-09-20 02:50:02 +04:00
|
|
|
}
|
2004-09-23 02:15:03 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrcsw(struct lwp *l, int out, int user)
|
1994-05-18 09:12:10 +04:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
|
|
|
struct ktr_csw *kc;
|
1994-05-18 09:12:10 +04:00
|
|
|
|
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
/*
|
|
|
|
* We can't sleep if we're already going to sleep (if original
|
|
|
|
* condition is met during sleep, we hang up).
|
|
|
|
*/
|
|
|
|
kte = pool_get(&kte_pool, out ? PR_NOWAIT : PR_WAITOK);
|
|
|
|
if (kte == NULL) {
|
|
|
|
ktd_logerr(p, KTDE_ENOMEM);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_CSW);
|
2004-09-23 02:15:03 +04:00
|
|
|
|
|
|
|
kc = malloc(sizeof(struct ktr_csw), M_KTRACE,
|
|
|
|
out ? M_NOWAIT : M_WAITOK);
|
|
|
|
if (kc == NULL) {
|
|
|
|
ktd_logerr(p, KTDE_ENOMEM);
|
|
|
|
goto free_kte;
|
|
|
|
}
|
|
|
|
kc->out = out;
|
|
|
|
kc->user = user;
|
|
|
|
kth->ktr_len = sizeof(struct ktr_csw);
|
|
|
|
kte->kte_buf = kc;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, out ? KTA_NOWAIT : KTA_WAITOK);
|
2004-09-23 02:15:03 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
return;
|
|
|
|
|
|
|
|
free_kte:
|
|
|
|
pool_put(&kte_pool, kte);
|
|
|
|
out:
|
1994-05-18 09:12:10 +04:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktruser(struct lwp *l, const char *id, void *addr, size_t len, int ustr)
|
2000-12-28 14:10:15 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
2000-12-28 14:10:15 +03:00
|
|
|
struct ktr_user *ktp;
|
|
|
|
caddr_t user_dta;
|
|
|
|
|
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_USER);
|
2004-09-23 02:15:03 +04:00
|
|
|
|
|
|
|
ktp = malloc(sizeof(struct ktr_user) + len, M_KTRACE, M_WAITOK);
|
2000-12-28 14:10:15 +03:00
|
|
|
if (ustr) {
|
|
|
|
if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
|
|
|
|
ktp->ktr_id[0] = '\0';
|
|
|
|
} else
|
|
|
|
strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
|
|
|
|
ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
user_dta = (caddr_t)(ktp + 1);
|
|
|
|
if (copyin(addr, (void *)user_dta, len) != 0)
|
2000-12-28 14:10:15 +03:00
|
|
|
len = 0;
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
kth->ktr_len = sizeof(struct ktr_user) + len;
|
|
|
|
kte->kte_buf = ktp;
|
2000-12-28 14:10:15 +03:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
2000-12-28 14:10:15 +03:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrmmsg(struct lwp *l, const void *msgh, size_t size)
|
2002-12-10 00:29:20 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrkmem(l, KTR_MMSG, msgh, size);
|
2002-12-10 00:29:20 +03:00
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrmool(struct lwp *l, const void *kaddr, size_t size, const void *uaddr)
|
2003-11-24 19:51:33 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
2003-11-24 19:51:33 +03:00
|
|
|
struct ktr_mool *kp;
|
2005-05-30 02:24:14 +04:00
|
|
|
struct ktr_mool *bf;
|
2003-11-24 19:51:33 +03:00
|
|
|
|
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
2004-09-23 02:15:03 +04:00
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
ktrinitheader(kth, l, KTR_MOOL);
|
2003-11-24 19:51:33 +03:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
kp = malloc(size + sizeof(*kp), M_KTRACE, M_WAITOK);
|
2003-11-24 19:51:33 +03:00
|
|
|
kp->uaddr = uaddr;
|
|
|
|
kp->size = size;
|
2005-05-30 02:24:14 +04:00
|
|
|
bf = kp + 1; /* Skip uaddr and size */
|
|
|
|
(void)memcpy(bf, kaddr, size);
|
2004-02-26 00:40:40 +03:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
kth->ktr_len = size + sizeof(*kp);
|
|
|
|
kte->kte_buf = kp;
|
2003-11-24 19:51:33 +03:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
2003-11-24 19:51:33 +03:00
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
}
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
void
|
|
|
|
ktrsaupcall(struct lwp *l, int type, int nevent, int nint, void *sas,
|
|
|
|
void *ap)
|
|
|
|
{
|
|
|
|
struct proc *p = l->l_proc;
|
|
|
|
struct ktrace_entry *kte;
|
|
|
|
struct ktr_header *kth;
|
|
|
|
struct ktr_saupcall *ktp;
|
|
|
|
size_t len;
|
|
|
|
struct sa_t **sapp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
p->p_traceflag |= KTRFAC_ACTIVE;
|
|
|
|
kte = pool_get(&kte_pool, PR_WAITOK);
|
|
|
|
kth = &kte->kte_kth;
|
|
|
|
ktrinitheader(kth, l, KTR_SAUPCALL);
|
|
|
|
|
|
|
|
len = sizeof(struct ktr_saupcall);
|
|
|
|
ktp = malloc(len + sizeof(struct sa_t) * (nevent + nint + 1), M_KTRACE,
|
|
|
|
M_WAITOK);
|
|
|
|
|
|
|
|
ktp->ktr_type = type;
|
|
|
|
ktp->ktr_nevent = nevent;
|
|
|
|
ktp->ktr_nint = nint;
|
|
|
|
ktp->ktr_sas = sas;
|
|
|
|
ktp->ktr_ap = ap;
|
|
|
|
/*
|
|
|
|
* Copy the sa_t's
|
|
|
|
*/
|
|
|
|
sapp = (struct sa_t **) sas;
|
|
|
|
|
|
|
|
for (i = nevent + nint; i >= 0; i--) {
|
|
|
|
if (copyin(*sapp, (char *)ktp + len, sizeof(struct sa_t)) == 0)
|
|
|
|
len += sizeof(struct sa_t);
|
|
|
|
sapp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
kth->ktr_len = len;
|
|
|
|
kte->kte_buf = ktp;
|
|
|
|
|
|
|
|
ktraddentry(l, kte, KTA_WAITOK);
|
|
|
|
p->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
}
|
2003-11-24 19:51:33 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* Interface and common routines */
|
|
|
|
|
1998-05-02 22:33:19 +04:00
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
|
1998-05-02 22:33:19 +04:00
|
|
|
{
|
2004-09-23 02:15:03 +04:00
|
|
|
struct proc *p;
|
|
|
|
struct pgrp *pg;
|
|
|
|
struct ktr_desc *ktd = NULL;
|
2003-06-28 18:20:43 +04:00
|
|
|
int ret = 0;
|
2003-06-30 02:28:00 +04:00
|
|
|
int error = 0;
|
2000-05-27 04:40:29 +04:00
|
|
|
int descend;
|
1998-05-02 22:33:19 +04:00
|
|
|
|
|
|
|
curp->p_traceflag |= KTRFAC_ACTIVE;
|
2000-05-27 04:40:29 +04:00
|
|
|
descend = ops & KTRFLAG_DESCEND;
|
|
|
|
facs = facs & ~((unsigned) KTRFAC_ROOT);
|
1998-05-02 22:33:19 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
switch (KTROP(ops)) {
|
|
|
|
|
|
|
|
case KTROP_CLEARFILE:
|
|
|
|
/*
|
|
|
|
* Clear all uses of the tracefile
|
|
|
|
*/
|
|
|
|
|
|
|
|
ktd = ktd_lookup(fp);
|
|
|
|
if (ktd == NULL)
|
|
|
|
goto done;
|
|
|
|
|
1999-07-25 10:30:33 +04:00
|
|
|
proclist_lock_read();
|
2004-10-01 20:30:52 +04:00
|
|
|
PROCLIST_FOREACH(p, &allproc) {
|
2004-09-23 02:15:03 +04:00
|
|
|
if (p->p_tracep == ktd) {
|
1998-05-02 22:33:19 +04:00
|
|
|
if (ktrcanset(curp, p))
|
|
|
|
ktrderef(p);
|
|
|
|
else
|
|
|
|
error = EPERM;
|
|
|
|
}
|
|
|
|
}
|
1999-07-23 01:08:30 +04:00
|
|
|
proclist_unlock_read();
|
1998-05-02 22:33:19 +04:00
|
|
|
goto done;
|
2000-05-27 04:40:29 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
case KTROP_SET:
|
|
|
|
ktd = ktd_lookup(fp);
|
|
|
|
if (ktd == NULL) {
|
|
|
|
ktd = malloc(sizeof(struct ktr_desc),
|
|
|
|
M_KTRACE, M_WAITOK);
|
|
|
|
TAILQ_INIT(&ktd->ktd_queue);
|
|
|
|
simple_lock_init(&ktd->ktd_slock);
|
|
|
|
callout_init(&ktd->ktd_wakch);
|
|
|
|
ktd->ktd_flags = ktd->ktd_qcount =
|
|
|
|
ktd->ktd_error = ktd->ktd_errcnt = 0;
|
|
|
|
ktd->ktd_ref = 1;
|
|
|
|
ktd->ktd_delayqcnt = ktd_delayqcnt;
|
|
|
|
ktd->ktd_wakedelay = mstohz(ktd_wakedelay);
|
|
|
|
ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl);
|
|
|
|
/*
|
|
|
|
* XXX: not correct. needs an way to detect
|
|
|
|
* whether ktruss or ktrace.
|
|
|
|
*/
|
|
|
|
if (fp->f_type == DTYPE_PIPE)
|
|
|
|
ktd->ktd_flags |= KTDF_INTERACTIVE;
|
|
|
|
|
|
|
|
error = kthread_create1(ktrace_thread, ktd,
|
|
|
|
&ktd->ktd_proc, "ktr %p", ktd);
|
|
|
|
if (error != 0) {
|
|
|
|
free(ktd, M_KTRACE);
|
|
|
|
goto done;
|
|
|
|
}
|
2000-05-27 04:40:29 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
simple_lock(&fp->f_slock);
|
|
|
|
fp->f_count++;
|
|
|
|
simple_unlock(&fp->f_slock);
|
|
|
|
ktd->ktd_fp = fp;
|
|
|
|
|
|
|
|
simple_lock(&ktdq_slock);
|
|
|
|
TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
|
|
|
|
simple_unlock(&ktdq_slock);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KTROP_CLEAR:
|
|
|
|
break;
|
2000-05-28 19:27:51 +04:00
|
|
|
}
|
2004-02-26 00:40:40 +03:00
|
|
|
|
1998-05-02 22:33:19 +04:00
|
|
|
/*
|
|
|
|
* need something to (un)trace (XXX - why is this here?)
|
|
|
|
*/
|
|
|
|
if (!facs) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2004-09-23 02:15:03 +04:00
|
|
|
|
2004-02-26 00:40:40 +03:00
|
|
|
/*
|
1998-05-02 22:33:19 +04:00
|
|
|
* do it
|
|
|
|
*/
|
2000-05-27 04:40:29 +04:00
|
|
|
if (pid < 0) {
|
1998-05-02 22:33:19 +04:00
|
|
|
/*
|
|
|
|
* by process group
|
|
|
|
*/
|
2003-11-13 00:07:37 +03:00
|
|
|
pg = pg_find(-pid, PFIND_UNLOCK_FAIL);
|
1998-05-02 22:33:19 +04:00
|
|
|
if (pg == NULL) {
|
|
|
|
error = ESRCH;
|
|
|
|
goto done;
|
|
|
|
}
|
2003-11-02 15:01:40 +03:00
|
|
|
LIST_FOREACH(p, &pg->pg_members, p_pglist) {
|
1998-05-02 22:33:19 +04:00
|
|
|
if (descend)
|
2004-09-23 02:15:03 +04:00
|
|
|
ret |= ktrsetchildren(curp, p, ops, facs, ktd);
|
2004-02-26 00:40:40 +03:00
|
|
|
else
|
2004-09-23 02:15:03 +04:00
|
|
|
ret |= ktrops(curp, p, ops, facs, ktd);
|
2000-04-19 23:14:17 +04:00
|
|
|
}
|
2004-02-26 00:40:40 +03:00
|
|
|
|
1998-05-02 22:33:19 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* by pid
|
|
|
|
*/
|
2003-11-13 00:07:37 +03:00
|
|
|
p = p_find(pid, PFIND_UNLOCK_FAIL);
|
1998-05-02 22:33:19 +04:00
|
|
|
if (p == NULL) {
|
|
|
|
error = ESRCH;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (descend)
|
2004-09-23 02:15:03 +04:00
|
|
|
ret |= ktrsetchildren(curp, p, ops, facs, ktd);
|
1998-05-02 22:33:19 +04:00
|
|
|
else
|
2004-09-23 02:15:03 +04:00
|
|
|
ret |= ktrops(curp, p, ops, facs, ktd);
|
1998-05-02 22:33:19 +04:00
|
|
|
}
|
2003-11-13 00:07:37 +03:00
|
|
|
proclist_unlock_read(); /* taken by p{g}_find */
|
1998-05-02 22:33:19 +04:00
|
|
|
if (!ret)
|
|
|
|
error = EPERM;
|
|
|
|
done:
|
2005-02-09 19:15:16 +03:00
|
|
|
if (ktd != NULL) {
|
|
|
|
if (error != 0) {
|
|
|
|
/*
|
|
|
|
* Wakeup the thread so that it can be die if we
|
|
|
|
* can't trace any process.
|
|
|
|
*/
|
|
|
|
ktd_wakeup(ktd);
|
|
|
|
}
|
|
|
|
if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE) {
|
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
ktdrel(ktd);
|
|
|
|
}
|
2004-09-23 02:15:03 +04:00
|
|
|
}
|
1998-05-02 22:33:19 +04:00
|
|
|
curp->p_traceflag &= ~KTRFAC_ACTIVE;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2000-05-27 04:40:29 +04:00
|
|
|
/*
|
2004-09-23 02:15:03 +04:00
|
|
|
* fktrace system call
|
2000-05-27 04:40:29 +04:00
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
sys_fktrace(struct lwp *l, void *v, register_t *retval)
|
2000-05-27 04:40:29 +04:00
|
|
|
{
|
|
|
|
struct sys_fktrace_args /* {
|
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(int) ops;
|
|
|
|
syscallarg(int) facs;
|
|
|
|
syscallarg(int) pid;
|
|
|
|
} */ *uap = v;
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *curp;
|
2000-05-27 04:40:29 +04:00
|
|
|
struct file *fp = NULL;
|
2005-12-11 15:16:03 +03:00
|
|
|
struct filedesc *fdp = l->l_proc->p_fd;
|
2003-05-02 16:43:01 +04:00
|
|
|
int error;
|
2000-05-27 04:40:29 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
curp = l->l_proc;
|
|
|
|
fdp = curp->p_fd;
|
2001-06-15 00:32:41 +04:00
|
|
|
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
|
|
|
|
return (EBADF);
|
|
|
|
|
2003-05-02 16:43:01 +04:00
|
|
|
FILE_USE(fp);
|
|
|
|
|
2001-06-15 00:32:41 +04:00
|
|
|
if ((fp->f_flag & FWRITE) == 0)
|
2003-05-02 16:43:01 +04:00
|
|
|
error = EBADF;
|
|
|
|
else
|
2003-06-30 02:28:00 +04:00
|
|
|
error = ktrace_common(curp, SCARG(uap, ops),
|
2003-05-02 16:43:01 +04:00
|
|
|
SCARG(uap, facs), SCARG(uap, pid), fp);
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
FILE_UNUSE(fp, l);
|
2000-05-27 04:40:29 +04:00
|
|
|
|
2003-05-02 16:43:01 +04:00
|
|
|
return error;
|
2000-05-27 04:40:29 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* ktrace system call
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1995-03-26 11:48:47 +04:00
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
sys_ktrace(struct lwp *l, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
1998-05-02 22:33:19 +04:00
|
|
|
struct sys_ktrace_args /* {
|
1997-10-19 06:00:19 +04:00
|
|
|
syscallarg(const char *) fname;
|
1994-10-20 07:22:35 +03:00
|
|
|
syscallarg(int) ops;
|
|
|
|
syscallarg(int) facs;
|
|
|
|
syscallarg(int) pid;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *curp = l->l_proc;
|
1998-05-02 22:33:19 +04:00
|
|
|
struct vnode *vp = NULL;
|
2000-05-27 04:40:29 +04:00
|
|
|
struct file *fp = NULL;
|
|
|
|
int ops = SCARG(uap, ops);
|
2003-06-30 02:28:00 +04:00
|
|
|
struct nameidata nd;
|
2005-12-11 15:16:03 +03:00
|
|
|
int error = 0;
|
|
|
|
int fd;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2000-05-27 04:40:29 +04:00
|
|
|
ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
|
|
|
|
|
1994-05-18 09:12:10 +04:00
|
|
|
curp->p_traceflag |= KTRFAC_ACTIVE;
|
2003-04-03 22:54:16 +04:00
|
|
|
if ((ops & KTROP_CLEAR) == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* an operation which requires a file argument.
|
|
|
|
*/
|
1994-10-20 07:22:35 +03:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
|
2005-12-11 15:16:03 +03:00
|
|
|
l);
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
|
1994-05-18 09:12:10 +04:00
|
|
|
curp->p_traceflag &= ~KTRFAC_ACTIVE;
|
1993-03-21 12:45:37 +03:00
|
|
|
return (error);
|
1994-05-18 09:12:10 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
vp = nd.ni_vp;
|
1998-03-01 05:20:01 +03:00
|
|
|
VOP_UNLOCK(vp, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (vp->v_type != VREG) {
|
2005-12-11 15:16:03 +03:00
|
|
|
(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, l);
|
1994-05-18 09:12:10 +04:00
|
|
|
curp->p_traceflag &= ~KTRFAC_ACTIVE;
|
1993-03-21 12:45:37 +03:00
|
|
|
return (EACCES);
|
|
|
|
}
|
|
|
|
/*
|
2000-05-27 04:40:29 +04:00
|
|
|
* XXX This uses up a file descriptor slot in the
|
|
|
|
* tracing process for the duration of this syscall.
|
|
|
|
* This is not expected to be a problem. If
|
|
|
|
* falloc(NULL, ...) DTRT we could skip that part, but
|
|
|
|
* that would require changing its interface to allow
|
|
|
|
* the caller to pass in a ucred..
|
|
|
|
*
|
2004-02-26 00:40:40 +03:00
|
|
|
* This will FILE_USE the fp it returns, if any.
|
2000-05-27 04:40:29 +04:00
|
|
|
* Keep it in use until we return.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2000-05-27 04:40:29 +04:00
|
|
|
if ((error = falloc(curp, &fp, &fd)) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
goto done;
|
2004-02-26 00:40:40 +03:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
fp->f_flag = FWRITE;
|
2000-05-27 04:40:29 +04:00
|
|
|
fp->f_type = DTYPE_VNODE;
|
|
|
|
fp->f_ops = &vnops;
|
|
|
|
fp->f_data = (caddr_t)vp;
|
2001-06-15 00:32:41 +04:00
|
|
|
FILE_SET_MATURE(fp);
|
2000-05-27 04:40:29 +04:00
|
|
|
vp = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-06-30 02:28:00 +04:00
|
|
|
error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
|
2000-05-27 04:40:29 +04:00
|
|
|
SCARG(uap, pid), fp);
|
2004-02-26 00:40:40 +03:00
|
|
|
done:
|
1993-03-21 12:45:37 +03:00
|
|
|
if (vp != NULL)
|
2005-12-11 15:16:03 +03:00
|
|
|
(void) vn_close(vp, FWRITE, curp->p_ucred, l);
|
2000-05-27 04:40:29 +04:00
|
|
|
if (fp != NULL) {
|
2005-12-11 15:16:03 +03:00
|
|
|
FILE_UNUSE(fp, l); /* release file */
|
|
|
|
fdrelease(l, fd); /* release fd table slot */
|
2000-05-27 04:40:29 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
ktrops(struct proc *curp, struct proc *p, int ops, int facs,
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktr_desc *ktd)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
int vers = ops & KTRFAC_VER_MASK;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!ktrcanset(curp, p))
|
|
|
|
return (0);
|
2005-12-11 15:16:03 +03:00
|
|
|
|
|
|
|
switch (vers) {
|
|
|
|
case KTRFACv0:
|
|
|
|
case KTRFACv1:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
1998-05-02 22:33:19 +04:00
|
|
|
if (KTROP(ops) == KTROP_SET) {
|
2004-09-23 02:15:03 +04:00
|
|
|
if (p->p_tracep != ktd) {
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* if trace file already in use, relinquish
|
|
|
|
*/
|
1998-05-02 22:33:19 +04:00
|
|
|
ktrderef(p);
|
2004-09-23 02:15:03 +04:00
|
|
|
p->p_tracep = ktd;
|
1998-05-02 22:33:19 +04:00
|
|
|
ktradref(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
p->p_traceflag |= facs;
|
|
|
|
if (curp->p_ucred->cr_uid == 0)
|
|
|
|
p->p_traceflag |= KTRFAC_ROOT;
|
2004-02-26 00:40:40 +03:00
|
|
|
} else {
|
1993-03-21 12:45:37 +03:00
|
|
|
/* KTROP_CLEAR */
|
|
|
|
if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
|
|
|
|
/* no more tracing */
|
1998-05-02 22:33:19 +04:00
|
|
|
ktrderef(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
if (p->p_traceflag)
|
|
|
|
p->p_traceflag |= vers;
|
1995-10-22 03:35:06 +03:00
|
|
|
/*
|
|
|
|
* Emit an emulation record, every time there is a ktrace
|
2004-02-26 00:40:40 +03:00
|
|
|
* change/attach request.
|
1995-10-22 03:35:06 +03:00
|
|
|
*/
|
|
|
|
if (KTRPOINT(p, KTR_EMUL))
|
2003-12-15 01:56:45 +03:00
|
|
|
p->p_traceflag |= KTRFAC_TRC_EMUL;
|
2000-12-11 22:53:06 +03:00
|
|
|
#ifdef __HAVE_SYSCALL_INTERN
|
2000-12-11 19:39:01 +03:00
|
|
|
(*p->p_emul->e_syscall_intern)(p);
|
2000-12-11 22:53:06 +03:00
|
|
|
#endif
|
1995-10-22 03:35:06 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
|
2004-09-23 02:15:03 +04:00
|
|
|
struct ktr_desc *ktd)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-05-02 22:33:19 +04:00
|
|
|
struct proc *p;
|
|
|
|
int ret = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p = top;
|
|
|
|
for (;;) {
|
2004-09-23 02:15:03 +04:00
|
|
|
ret |= ktrops(curp, p, ops, facs, ktd);
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* If this process has children, descend to them next,
|
|
|
|
* otherwise do any siblings, and if done with this level,
|
|
|
|
* follow back up the tree (but not past top).
|
|
|
|
*/
|
2003-11-13 00:07:37 +03:00
|
|
|
if (LIST_FIRST(&p->p_children) != NULL) {
|
2000-04-19 23:14:17 +04:00
|
|
|
p = LIST_FIRST(&p->p_children);
|
2003-11-13 00:07:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (;;) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (p == top)
|
|
|
|
return (ret);
|
2000-04-19 23:14:17 +04:00
|
|
|
if (LIST_NEXT(p, p_sibling) != NULL) {
|
|
|
|
p = LIST_NEXT(p, p_sibling);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
1994-08-30 07:04:28 +04:00
|
|
|
p = p->p_pptr;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
void
|
|
|
|
ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-06-30 02:28:00 +04:00
|
|
|
struct uio auio;
|
2004-09-23 02:15:03 +04:00
|
|
|
struct iovec aiov[64], *iov;
|
|
|
|
struct ktrace_entry *top = kte;
|
|
|
|
struct ktr_header *kth;
|
|
|
|
struct file *fp = ktd->ktd_fp;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
next:
|
|
|
|
auio.uio_iov = iov = &aiov[0];
|
1993-03-21 12:45:37 +03:00
|
|
|
auio.uio_offset = 0;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_WRITE;
|
2004-09-23 02:15:03 +04:00
|
|
|
auio.uio_resid = 0;
|
|
|
|
auio.uio_iovcnt = 0;
|
2005-12-13 16:12:18 +03:00
|
|
|
auio.uio_lwp = curlwp;
|
2004-09-23 02:15:03 +04:00
|
|
|
do {
|
|
|
|
kth = &kte->kte_kth;
|
2005-12-11 15:16:03 +03:00
|
|
|
|
|
|
|
if (kth->ktr_version == 0) {
|
|
|
|
/*
|
|
|
|
* Convert back to the old format fields
|
|
|
|
*/
|
|
|
|
TIMESPEC_TO_TIMEVAL(&kth->ktr_tv, &kth->ktr_time);
|
|
|
|
kth->ktr_unused = NULL;
|
|
|
|
}
|
2004-09-23 02:15:03 +04:00
|
|
|
iov->iov_base = (caddr_t)kth;
|
|
|
|
iov++->iov_len = sizeof(struct ktr_header);
|
|
|
|
auio.uio_resid += sizeof(struct ktr_header);
|
1993-03-21 12:45:37 +03:00
|
|
|
auio.uio_iovcnt++;
|
2004-09-23 02:15:03 +04:00
|
|
|
if (kth->ktr_len > 0) {
|
|
|
|
iov->iov_base = kte->kte_buf;
|
|
|
|
iov++->iov_len = kth->ktr_len;
|
|
|
|
auio.uio_resid += kth->ktr_len;
|
|
|
|
auio.uio_iovcnt++;
|
|
|
|
}
|
|
|
|
} while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL &&
|
|
|
|
auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1);
|
1998-05-02 22:33:19 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
again:
|
2003-02-23 17:37:32 +03:00
|
|
|
simple_lock(&fp->f_slock);
|
2000-05-27 04:40:29 +04:00
|
|
|
FILE_USE(fp);
|
2004-09-23 02:15:03 +04:00
|
|
|
error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
|
|
|
|
fp->f_cred, FOF_UPDATE_OFFSET);
|
2000-05-27 04:40:29 +04:00
|
|
|
FILE_UNUSE(fp, NULL);
|
2004-09-23 02:15:03 +04:00
|
|
|
switch (error) {
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
if (auio.uio_resid > 0)
|
|
|
|
goto again;
|
|
|
|
if (kte != NULL)
|
|
|
|
goto next;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
preempt(1);
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* If error encountered, give up tracing on this
|
|
|
|
* vnode. Don't report EPIPE as this can easily
|
|
|
|
* happen with fktrace()/ktruss.
|
|
|
|
*/
|
|
|
|
#ifndef DEBUG
|
|
|
|
if (error != EPIPE)
|
|
|
|
#endif
|
|
|
|
log(LOG_NOTICE,
|
|
|
|
"ktrace write failed, errno %d, tracing stopped\n",
|
|
|
|
error);
|
|
|
|
proclist_lock_read();
|
2004-10-01 20:30:52 +04:00
|
|
|
PROCLIST_FOREACH(p, &allproc) {
|
2004-09-23 02:15:03 +04:00
|
|
|
if (p->p_tracep == ktd)
|
|
|
|
ktrderef(p);
|
|
|
|
}
|
|
|
|
proclist_unlock_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((kte = top) != NULL) {
|
|
|
|
top = TAILQ_NEXT(top, kte_list);
|
|
|
|
ktefree(kte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ktrace_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct ktr_desc *ktd = arg;
|
|
|
|
struct file *fp = ktd->ktd_fp;
|
|
|
|
struct ktrace_entry *kte;
|
|
|
|
int ktrerr, errcnt;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
simple_lock(&ktd->ktd_slock);
|
|
|
|
kte = TAILQ_FIRST(&ktd->ktd_queue);
|
|
|
|
if (kte == NULL) {
|
|
|
|
if (ktd->ktd_flags & KTDF_WAIT) {
|
|
|
|
ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING);
|
|
|
|
wakeup(&ktd->ktd_flags);
|
|
|
|
}
|
|
|
|
if (ktd->ktd_ref == 0)
|
|
|
|
break;
|
|
|
|
ltsleep(ktd, PWAIT | PNORELOCK, "ktrwait", 0,
|
|
|
|
&ktd->ktd_slock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TAILQ_INIT(&ktd->ktd_queue);
|
|
|
|
ktd->ktd_qcount = 0;
|
|
|
|
ktrerr = ktd->ktd_error;
|
|
|
|
errcnt = ktd->ktd_errcnt;
|
|
|
|
ktd->ktd_error = ktd->ktd_errcnt = 0;
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
|
|
|
|
if (ktrerr) {
|
|
|
|
log(LOG_NOTICE,
|
|
|
|
"ktrace failed, fp %p, error 0x%x, total %d\n",
|
|
|
|
fp, ktrerr, errcnt);
|
|
|
|
}
|
|
|
|
ktrwrite(ktd, kte);
|
|
|
|
}
|
|
|
|
simple_unlock(&ktd->ktd_slock);
|
|
|
|
|
|
|
|
simple_lock(&ktdq_slock);
|
|
|
|
TAILQ_REMOVE(&ktdq, ktd, ktd_list);
|
|
|
|
simple_unlock(&ktdq_slock);
|
|
|
|
|
|
|
|
simple_lock(&fp->f_slock);
|
|
|
|
FILE_USE(fp);
|
1998-05-02 22:33:19 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2004-09-23 02:15:03 +04:00
|
|
|
* ktrace file descriptor can't be watched (are not visible to
|
|
|
|
* userspace), so no kqueue stuff here
|
|
|
|
* XXX: The above comment is wrong, because the fktrace file
|
|
|
|
* descriptor is available in userland.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2004-09-23 02:15:03 +04:00
|
|
|
closef(fp, NULL);
|
2000-04-19 23:14:17 +04:00
|
|
|
|
2004-09-23 02:15:03 +04:00
|
|
|
callout_stop(&ktd->ktd_wakch);
|
|
|
|
free(ktd, M_KTRACE);
|
|
|
|
|
|
|
|
kthread_exit(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if caller has permission to set the ktracing state
|
|
|
|
* of target. Essentially, the target can't possess any
|
|
|
|
* more permissions than the caller. KTRFAC_ROOT signifies that
|
2004-02-26 00:40:40 +03:00
|
|
|
* root previously set the tracing status on the target process, and
|
1993-03-21 12:45:37 +03:00
|
|
|
* so, only root may further change it.
|
|
|
|
*
|
|
|
|
* TODO: check groups. use caller effective gid.
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
ktrcanset(struct proc *callp, struct proc *targetp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-05-02 22:33:19 +04:00
|
|
|
struct pcred *caller = callp->p_cred;
|
|
|
|
struct pcred *target = targetp->p_cred;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((caller->pc_ucred->cr_uid == target->p_ruid &&
|
2004-02-26 00:40:40 +03:00
|
|
|
target->p_ruid == target->p_svuid &&
|
|
|
|
caller->p_rgid == target->p_rgid && /* XXX */
|
|
|
|
target->p_rgid == target->p_svgid &&
|
|
|
|
(targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
|
|
|
|
(targetp->p_flag & P_SUGID) == 0) ||
|
|
|
|
caller->pc_ucred->cr_uid == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2000-08-01 08:57:28 +04:00
|
|
|
#endif /* KTRACE */
|
2000-12-28 14:10:15 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Put user defined entry to ktrace records.
|
|
|
|
*/
|
|
|
|
int
|
2004-04-30 11:51:59 +04:00
|
|
|
sys_utrace(struct lwp *l, void *v, register_t *retval)
|
2000-12-28 14:10:15 +03:00
|
|
|
{
|
|
|
|
#ifdef KTRACE
|
|
|
|
struct sys_utrace_args /* {
|
2001-01-06 00:42:08 +03:00
|
|
|
syscallarg(const char *) label;
|
2000-12-28 14:10:15 +03:00
|
|
|
syscallarg(void *) addr;
|
|
|
|
syscallarg(size_t) len;
|
|
|
|
} */ *uap = v;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2004-02-26 00:40:40 +03:00
|
|
|
|
2000-12-28 14:10:15 +03:00
|
|
|
if (!KTRPOINT(p, KTR_USER))
|
|
|
|
return (0);
|
|
|
|
|
2001-01-06 01:25:26 +03:00
|
|
|
if (SCARG(uap, len) > KTR_USER_MAXLEN)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
ktruser(l, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
|
2000-12-28 14:10:15 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
#else /* !KTRACE */
|
|
|
|
return ENOSYS;
|
|
|
|
#endif /* KTRACE */
|
|
|
|
}
|