summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiod Vallat <miod@cvs.openbsd.org>2013-06-01 09:49:54 +0000
committerMiod Vallat <miod@cvs.openbsd.org>2013-06-01 09:49:54 +0000
commita275d1d143573a3af196681dd301130c293de367 (patch)
tree56040e0e516d6f3b13a85043d3031df102cca766
parenteab181d9ad5afedec5a0231154a2c2f7b0298215 (diff)
Add utrace(2), a system call allowing for userland to send its own ktrace
records. From FreeBSD via otto@, with tweaks suggested by guenther@. Commite on behalf of otto@ who is not around, to ride the libc minor bump. Causes a librthread minor bump as well (new syscall).
-rw-r--r--lib/libc/sys/Makefile.inc6
-rw-r--r--lib/libc/sys/ktrace.211
-rw-r--r--lib/libc/sys/utrace.288
-rw-r--r--lib/librthread/shlib_version2
-rw-r--r--sys/kern/kern_ktrace.c63
-rw-r--r--sys/kern/syscalls.master7
-rw-r--r--sys/sys/ktrace.h19
7 files changed, 183 insertions, 13 deletions
diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc
index 57f76b8fb16..b26ccc211af 100644
--- a/lib/libc/sys/Makefile.inc
+++ b/lib/libc/sys/Makefile.inc
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.inc,v 1.111 2013/04/29 17:06:20 matthew Exp $
+# $OpenBSD: Makefile.inc,v 1.112 2013/06/01 09:49:53 miod Exp $
# $NetBSD: Makefile.inc,v 1.35 1995/10/16 23:49:07 jtc Exp $
# @(#)Makefile.inc 8.1 (Berkeley) 6/17/93
@@ -56,7 +56,7 @@ ASM= __get_tcb.o __getcwd.o __semctl.o __set_tcb.o __syscall.o \
shmget.o shutdown.o sigaction.o sigaltstack.o socket.o \
socketpair.o stat.o statfs.o swapctl.o symlink.o \
symlinkat.o sync.o sysarch.o umask.o unlink.o unlinkat.o \
- unmount.o utimensat.o utimes.o wait4.o write.o writev.o
+ unmount.o utimensat.o utimes.o utrace.o wait4.o write.o writev.o
# modules with specific implementations on some architectures, and the
# default implementation elsewhere:
@@ -195,7 +195,7 @@ MAN+= __get_tcb.2 __thrsigdivert.2 __thrsleep.2 _exit.2 accept.2 \
sigprocmask.2 sigreturn.2 sigsuspend.2 socket.2 \
socketpair.2 stat.2 statfs.2 swapctl.2 symlink.2 \
sync.2 sysarch.2 syscall.2 truncate.2 umask.2 unlink.2 \
- utimes.2 vfork.2 wait.2 write.2
+ utimes.2 utrace.2 vfork.2 wait.2 write.2
MLINKS+=__get_tcb.2 __set_tcb.2
MLINKS+=__thrsleep.2 __thrwakeup.2
diff --git a/lib/libc/sys/ktrace.2 b/lib/libc/sys/ktrace.2
index 594a3591334..dcf8dfad10f 100644
--- a/lib/libc/sys/ktrace.2
+++ b/lib/libc/sys/ktrace.2
@@ -1,4 +1,4 @@
-.\" $OpenBSD: ktrace.2,v 1.22 2012/11/29 02:35:04 guenther Exp $
+.\" $OpenBSD: ktrace.2,v 1.23 2013/06/01 09:49:53 miod Exp $
.\" $NetBSD: ktrace.2,v 1.2 1995/02/27 12:33:58 cgd Exp $
.\"
.\" Copyright (c) 1993
@@ -30,7 +30,7 @@
.\"
.\" @(#)ktrace.2 8.1 (Berkeley) 6/4/93
.\"
-.Dd $Mdocdate: November 29 2012 $
+.Dd $Mdocdate: June 1 2013 $
.Dt KTRACE 2
.Os
.Sh NAME
@@ -110,6 +110,10 @@ Trace emulation changes.
Trace context switch points.
.It Dv KTRFAC_STRUCT
Trace various structs
+.It Dv KTRFAC_USER
+Trace user data coming from
+.Xr utrace 2
+calls.
.It Dv KTRFAC_INHERIT
Inherit tracing to future children.
.El
@@ -188,7 +192,8 @@ No process can be found corresponding to that specified by
.El
.Sh SEE ALSO
.Xr kdump 1 ,
-.Xr ktrace 1
+.Xr ktrace 1 ,
+.Xr utrace 2
.Sh HISTORY
A
.Fn ktrace
diff --git a/lib/libc/sys/utrace.2 b/lib/libc/sys/utrace.2
new file mode 100644
index 00000000000..a646f9f8500
--- /dev/null
+++ b/lib/libc/sys/utrace.2
@@ -0,0 +1,88 @@
+.\" $OpenBSD: utrace.2,v 1.1 2013/06/01 09:49:53 miod Exp $
+.\" $NetBSD: utrace.2,v 1.13 2008/05/02 18:38:32 martin Exp $
+.\"
+.\" Copyright (c) 2000 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Gregory McGarry <g.mcgarry@ieee.org>.
+.\"
+.\" 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.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+.\"
+.Dd $Mdocdate: June 1 2013 $
+.Dt UTRACE 2
+.Os
+.Sh NAME
+.Nm utrace
+.Nd insert user record in ktrace log
+.Sh SYNOPSIS
+.In sys/types.h
+.In sys/param.h
+.In sys/uio.h
+.In sys/ktrace.h
+.Ft int
+.Fn utrace "const char *label" "void *addr" "size_t len"
+.Sh DESCRIPTION
+Adds a record to the process trace with information supplied by the user.
+The record is identified by
+.Fa label
+and contains
+.Fa len
+bytes from memory pointed to by
+.Fa addr .
+This call only has an effect if the calling process is being traced.
+.Sh RETURN VALUES
+.Rv -std
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ENOSYS
+The currently running kernel was compiled without
+.Xr ktrace 2
+support (option KTRACE).
+.It Bq Er ENAMETOOLONG
+The length of the
+.Fa label
+string was longer than
+.Dv KTR_USER_MAXIDLEN\-1 .
+.It Bq Er EINVAL
+The specified data length
+.Fa len
+was bigger than
+.Dv KTR_USER_MAXLEN .
+.El
+.Sh SEE ALSO
+.Xr kdump 1 ,
+.Xr ktrace 1 ,
+.Xr ktrace 2 ,
+.Xr options 4
+.Sh HISTORY
+The
+.Fn utrace
+system call first appeared in
+.Fx 2.2 .
+It was added to
+.Ox
+in
+.Ox 5.4 .
+The
+.Fa label
+argument is an extension.
diff --git a/lib/librthread/shlib_version b/lib/librthread/shlib_version
index 730231c38d0..49b346400a8 100644
--- a/lib/librthread/shlib_version
+++ b/lib/librthread/shlib_version
@@ -1,2 +1,2 @@
major=17
-minor=1
+minor=2
diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c
index 80f167670f1..dc268aa8d5a 100644
--- a/sys/kern/kern_ktrace.c
+++ b/sys/kern/kern_ktrace.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_ktrace.c,v 1.58 2012/04/10 20:39:37 mikeb Exp $ */
+/* $OpenBSD: kern_ktrace.c,v 1.59 2013/06/01 09:49:50 miod Exp $ */
/* $NetBSD: kern_ktrace.c,v 1.23 1996/02/09 18:59:36 christos Exp $ */
/*
@@ -32,8 +32,6 @@
* @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
*/
-#ifdef KTRACE
-
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
@@ -345,6 +343,49 @@ ktrstruct(struct proc *p, const char *name, const void *data, size_t datalen)
atomic_clearbits_int(&p->p_flag, P_INKTR);
}
+int
+ktruser(struct proc *p, const char *id, const void *addr, size_t len)
+{
+ struct ktr_header kth;
+ struct ktr_user *ktp;
+ int error;
+ void *memp;
+ size_t size;
+#define STK_PARAMS 128
+ long long stkbuf[STK_PARAMS / sizeof(long long)];
+
+ if (!KTRPOINT(p, KTR_USER))
+ return (0);
+ if (len > KTR_USER_MAXLEN)
+ return EINVAL;
+
+ atomic_setbits_int(&p->p_flag, P_INKTR);
+ ktrinitheader(&kth, p, KTR_USER);
+ size = sizeof(*ktp) + len;
+ memp = NULL;
+ if (size > sizeof(stkbuf)) {
+ memp = malloc(sizeof(*ktp) + len, M_TEMP, M_WAITOK);
+ ktp = (struct ktr_user *)memp;
+ } else
+ ktp = (struct ktr_user *)stkbuf;
+ bzero(ktp->ktr_id, KTR_USER_MAXIDLEN);
+ error = copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL);
+ if (error)
+ goto out;
+
+ error = copyin(addr, (void *)(ktp + 1), len);
+ if (error)
+ goto out;
+ kth.ktr_len = sizeof(*ktp) + len;
+ ktrwrite(p, &kth, ktp);
+out:
+ if (memp != NULL)
+ free(memp, M_TEMP);
+ atomic_clearbits_int(&p->p_flag, P_INKTR);
+ return (error);
+}
+
+
/* Interface and common routines */
/*
@@ -608,4 +649,18 @@ ktrcanset(struct proc *callp, struct process *targetpr)
return (0);
}
-#endif
+/*
+ * utrace system call
+ */
+/* ARGSUSED */
+int
+sys_utrace(struct proc *curp, void *v, register_t *retval)
+{
+ struct sys_utrace_args /* {
+ syscallarg(const char *) label;
+ syscallarg(const void *) addr;
+ syscallarg(size_t) len;
+ } */ *uap = v;
+ return (ktruser(curp, SCARG(uap, label), SCARG(uap, addr),
+ SCARG(uap, len)));
+}
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index ae4c947cccd..8cdbd016b9b 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -1,4 +1,4 @@
-; $OpenBSD: syscalls.master,v 1.130 2013/04/29 17:06:20 matthew Exp $
+; $OpenBSD: syscalls.master,v 1.131 2013/06/01 09:49:50 miod Exp $
; $NetBSD: syscalls.master,v 1.32 1996/04/23 10:24:21 mycroft Exp $
; @(#)syscalls.master 8.2 (Berkeley) 1/13/94
@@ -343,7 +343,12 @@
const struct timeval *tptr); }
207 STD { pid_t sys_getpgid(pid_t pid); }
208 OBSOL nnpfspioctl
+#ifdef KTRACE
+209 STD { int sys_utrace(const char *label, const void *addr, \
+ size_t len); }
+#else
209 UNIMPL
+#endif
;
; Syscalls 210-219 are reserved for dynamically loaded syscalls
;
diff --git a/sys/sys/ktrace.h b/sys/sys/ktrace.h
index 0e7c96fb13c..f8bb063829c 100644
--- a/sys/sys/ktrace.h
+++ b/sys/sys/ktrace.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ktrace.h,v 1.15 2012/07/09 17:51:08 claudio Exp $ */
+/* $OpenBSD: ktrace.h,v 1.16 2013/06/01 09:49:52 miod Exp $ */
/* $NetBSD: ktrace.h,v 1.12 1996/02/04 02:12:29 christos Exp $ */
/*
@@ -151,6 +151,19 @@ struct sockaddr;
struct stat;
/*
+ * KTR_USER - user record
+ */
+#define KTR_USER 9
+#define KTR_USER_MAXIDLEN 20
+#define KTR_USER_MAXLEN 2048 /* maximum length of passed data */
+struct ktr_user {
+ char ktr_id[KTR_USER_MAXIDLEN]; /* string id of caller */
+ /*
+ * Followed by ktr_len - sizeof(struct ktr_user) of user data.
+ */
+};
+
+/*
* kernel trace points (in p_traceflag)
*/
#define KTRFAC_MASK 0x00ffffff
@@ -162,6 +175,7 @@ struct stat;
#define KTRFAC_CSW (1<<KTR_CSW)
#define KTRFAC_EMUL (1<<KTR_EMUL)
#define KTRFAC_STRUCT (1<<KTR_STRUCT)
+#define KTRFAC_USER (1<<KTR_USER)
/*
* trace flags (also in p_traceflags)
@@ -175,6 +189,7 @@ struct stat;
__BEGIN_DECLS
int ktrace(const char *, int, int, pid_t);
+int utrace(const char *, const void *, size_t);
__END_DECLS
#else
@@ -186,6 +201,8 @@ void ktrnamei(struct proc *, char *);
void ktrpsig(struct proc *, int, sig_t, int, int, siginfo_t *);
void ktrsyscall(struct proc *, register_t, size_t, register_t []);
void ktrsysret(struct proc *, register_t, int, register_t);
+void ktr_kuser(const char *, void *, size_t);
+int ktruser(struct proc *, const char *, const void *, size_t);
void ktrcleartrace(struct process *);
void ktrsettrace(struct process *, int, struct vnode *, struct ucred *);