Re: utrace(2)

2011-07-18 Thread Otto Moerbeek
On Tue, Jul 12, 2011 at 05:52:09PM +0200, Otto Moerbeek wrote:

 Hi,
 
 The diff below implements utrace(2), a syscall to introduce data into
 a ktrace stream from a userland program.
 
 The interface is taken from netbsd, freebsd also has a utrace(2)
 syscall, but lacks the label argument. 
 
 You could uise this as a debugging aid, to get tarce information
 intermixed with sysclal info, or set more specific tracepoint and only
 see the utrace records.
 
 Do we want this? If so, this should be reviewed and tested. And a man
 page for utrace(2) should be written.
 
 Output of utrace(It is I, some data, 10) looks like:
 
  24413 a.outCALL  utrace(0x500a09,0x5009ff,0xa)
  24413 a.outUSER  It is I: 10 bytes
some data\0
  24413 a.outRET   utrace 0


new diff, with comments incorporated and a manpage.  Since the
utrace(2) syscall always exists now, I've put it in sys_generic.c. Is
that a right place? 

-0tto


Index: lib/libc/sys/Makefile.inc
===
RCS file: /cvs/src/lib/libc/sys/Makefile.inc,v
retrieving revision 1.92
diff -u -p -r1.92 Makefile.inc
--- lib/libc/sys/Makefile.inc   3 Jul 2010 04:44:51 -   1.92
+++ lib/libc/sys/Makefile.inc   18 Jul 2011 09:28:15 -
@@ -64,7 +64,7 @@ ASM=  accept.o access.o acct.o adjfreq.o 
setuid.o shmat.o shmctl.o shmdt.o shmget.o shutdown.o sigaction.o \
sigaltstack.o socket.o socketpair.o stat.o statfs.o swapctl.o \
symlink.o sync.o sysarch.o umask.o unlink.o unmount.o \
-   utimes.o wait4.o write.o writev.o nnpfspioctl.o __semctl.o \
+   utimes.o utrace.o wait4.o write.o writev.o nnpfspioctl.o __semctl.o \
__syscall.o __sysctl.o __getcwd.o sched_yield.o getthrid.o \
thrsleep.o thrwakeup.o threxit.o thrsigdivert.o \
setrtable.o getrtable.o
@@ -233,7 +233,7 @@ MAN+=   accept.2 access.2 acct.2 adjfreq.2
shutdown.2 sigaction.2 sigaltstack.2 sigpending.2 sigprocmask.2 \
sigreturn.2 sigstack.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
+   umask.2 unlink.2 utimes.2 utrace.2 vfork.2 wait.2 write.2
 
 MAN+=  msgctl.2 shmctl.2 shmat.2 semop.2 semget.2 semctl.2 msgsnd.2 msgrcv.2 \
msgget.2 shmget.2
Index: lib/libc/sys/ktrace.2
===
RCS file: /cvs/src/lib/libc/sys/ktrace.2,v
retrieving revision 1.20
diff -u -p -r1.20 ktrace.2
--- lib/libc/sys/ktrace.2   8 Jul 2011 19:30:32 -   1.20
+++ lib/libc/sys/ktrace.2   18 Jul 2011 09:28:15 -
@@ -94,6 +94,8 @@ generate much output).
 .It Dv KTRFAC_EMUL Trace emulation changes.
 .It Dv KTRFAC_CSW  Trace context switch points.
 .It Dv KTRFAC_STRUCT   Trace various structs
+.It Dv KTRFAC_USER Trace user data coming from
+.Xr utrace 2
 .It Dv KTRFAC_INHERIT  Inherit tracing to future children.
 .El
 .Pp
@@ -174,7 +176,8 @@ No process can be found corresponding to
 .El
 .Sh SEE ALSO
 .Xr kdump 1 ,
-.Xr ktrace 1
+.Xr ktrace 1 ,
+.Xr utrace 2
 .Sh HISTORY
 A
 .Fn ktrace
Index: lib/libc/sys/utrace.2
===
RCS file: lib/libc/sys/utrace.2
diff -N lib/libc/sys/utrace.2
--- /dev/null   1 Jan 1970 00:00:00 -
+++ lib/libc/sys/utrace.2   18 Jul 2011 09:28:15 -
@@ -0,0 +1,93 @@
+.\$OpenBSD: utrace.2,v 1.13 2008/05/02 18:38:32 martin 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.mcga...@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 

Re: utrace(2)

2011-07-12 Thread Ted Unangst
On Tue, Jul 12, 2011, Otto Moerbeek wrote:

 The diff below implements utrace(2), a syscall to introduce data into
 a ktrace stream from a userland program.

 Index: sys/kern/kern_ktrace.c
 ===
 RCS file: /cvs/src/sys/kern/kern_ktrace.c,v
 retrieving revision 1.54
 diff -u -p -r1.54 kern_ktrace.c
 --- sys/kern/kern_ktrace.c11 Jul 2011 15:40:47 -  1.54
 +++ sys/kern/kern_ktrace.c12 Jul 2011 14:59:03 -
 +int
 +ktruser(struct proc *p, const char *id, void *addr, size_t len, int ustr)
 +{
 + struct ktr_header kth;
 + struct ktr_user *ktp;
 + int error;
 +
 + if (!KTRPOINT(p, KTR_USER))
 + return (0);
 + if (len  KTR_USER_MAXLEN)
 + return ENOSPC;
 +
 + p-p_traceflag |= KTRFAC_ACTIVE;
 + ktrinitheader(kth, p, KTR_USER);
 + ktp = malloc(sizeof(*ktp) + len, M_TEMP, M_WAITOK);
 + bzero(ktp-ktr_id, KTR_USER_MAXIDLEN);
 + if (ustr) {
 + error = copyinstr(id, ktp-ktr_id, KTR_USER_MAXIDLEN, NULL);
 + if (error)
 + goto out;
 + } else
 + strlcpy(ktp-ktr_id, id, KTR_USER_MAXIDLEN);

There is only one caller, so we don't need the ustr argument.

 +sys_utrace(struct proc *curp, void *v, register_t *retval)
 +{
 + struct sys_utrace_args /* {
 + syscallarg(const char *) label;
 + syscallarg(void) addr;

missing * here.



Re: utrace(2)

2011-07-12 Thread Philip Guenther
On Tue, Jul 12, 2011 at 8:52 AM, Otto Moerbeek o...@drijf.net wrote:
...
 diff -u -p -r1.117 syscalls.master
 --- sys/kern/syscalls.master9 Jul 2011 05:46:26 -   1.117
 +++ sys/kern/syscalls.master12 Jul 2011 14:59:03 -
 @@ -572,3 +572,9 @@
int flag); }
  326STD { int sys_utimensat(int fd, const char *path, \
const struct timespec *times, int flag); }
 +#ifdef KTRACE
 +327STD { int sys_utrace(const char *label, void *addr, \
 +   size_t len); }
 +#else
 +327UNIMPL  utrace
 +#endif

Hmm, if a process calls utrace() with a kernel that doesn't have
KTRACE defined, should it really kill the process (SIGSYS) or just do
nothing?  (Just pondering a day when ld.so or libc calls utrace().
Don't want to have to add another stub to the ramdisk build bits...)


Philip Guenther