Decode kevent structures logged via ktrace(2) in kdump.

- Add a new KTR_STRUCT_ARRAY ktrace record type which dumps an array of
  structures.

  The structure name in the record payload is preceded by a size_t
  containing the size of the individual structures.  Use this to
  replace the previous code that dumped the kevent arrays dumped for
  kevent().  kdump is now able to decode the kevent structures rather
  than dumping their contents via a hexdump.

  One change from before is that the 'changes' and 'events' arrays are
  not marked with separate 'read' and 'write' annotations in kdump
  output.  Instead, the first array is the 'changes' array, and the
  second array (only present if kevent doesn't fail with an error) is
  the 'events' array.  For kevent(), empty arrays are denoted by an
  entry with an array containing zero entries rather than no record.

- Move kevent decoding tables from truss to libsysdecode.

  This adds three new functions to decode members of struct kevent:
  sysdecode_kevent_filter, sysdecode_kevent_flags, and
  sysdecode_kevent_fflags.

  kdump uses these helper functions to pretty-print kevent fields.

- Move structure definitions for freebsd11 and freebsd32 kevent
  structures to <sys/event.h> so that they can be shared with userland.
  The 32-bit structures are only exposed if _WANT_KEVENT32 is defined.
  The freebsd11 structures are only exposed if _WANT_FREEBSD11_KEVENT is
  defined.  The 32-bit freebsd11 structure requires both.

- Decode freebsd11 kevent structures in truss for the compat11.kevent()
  system call.

- Log 32-bit kevent structures via ktrace for 32-bit compat kevent()
  system calls.

- While here, constify the 'void *data' argument to ktrstruct().

Reviewed by:	kib (earlier version)
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D12470
This commit is contained in:
John Baldwin 2017-11-25 04:49:12 +00:00
parent 2edb60b9d5
commit ffb6607984
16 changed files with 605 additions and 158 deletions

View File

@ -17,6 +17,7 @@ MAN= sysdecode.3 \
sysdecode_cap_rights.3 \
sysdecode_enum.3 \
sysdecode_fcntl_arg.3 \
sysdecode_kevent.3 \
sysdecode_ioctlname.3 \
sysdecode_mask.3 \
sysdecode_quotactl_cmd.3 \
@ -69,6 +70,9 @@ MLINKS+=sysdecode_enum.3 sysdecode_acltype.3 \
sysdecode_enum.3 sysdecode_vmresult.3 \
sysdecode_enum.3 sysdecode_whence.3
MLINKS+=sysdecode_fcntl_arg.3 sysdecode_fcntl_arg_p.3
MLINKS+=sysdecode_kevent.3 sysdecode_kevent_fflags.3 \
sysdecode_kevent.3 sysdecode_kevent_filter.3 \
sysdecode_kevent.3 sysdecode_kevent_flags.3
MLINKS+=sysdecode_mask.3 sysdecode_accessmode.3 \
sysdecode_mask.3 sysdecode_atflags.3 \
sysdecode_mask.3 sysdecode_capfcntlrights.3 \

View File

@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/acl.h>
#include <sys/capsicum.h>
#include <sys/event.h>
#include <sys/extattr.h>
#include <sys/linker.h>
#include <sys/mman.h>
@ -501,6 +502,116 @@ sysdecode_getrusage_who(int who)
return (lookup_value(rusage, who));
}
static struct name_table kevent_user_ffctrl[] = {
X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY)
XEND
};
static struct name_table kevent_rdwr_fflags[] = {
X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND
};
static struct name_table kevent_vnode_fflags[] = {
X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB)
X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) X(NOTE_OPEN) X(NOTE_CLOSE)
X(NOTE_CLOSE_WRITE) X(NOTE_READ) XEND
};
static struct name_table kevent_proc_fflags[] = {
X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR)
X(NOTE_CHILD) XEND
};
static struct name_table kevent_timer_fflags[] = {
X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS)
X(NOTE_ABSTIME) XEND
};
void
sysdecode_kevent_fflags(FILE *fp, short filter, int fflags, int base)
{
int rem;
if (fflags == 0) {
fputs("0", fp);
return;
}
switch (filter) {
case EVFILT_READ:
case EVFILT_WRITE:
if (!print_mask_int(fp, kevent_rdwr_fflags, fflags, &rem))
fprintf(fp, "%#x", rem);
else if (rem != 0)
fprintf(fp, "|%#x", rem);
break;
case EVFILT_VNODE:
if (!print_mask_int(fp, kevent_vnode_fflags, fflags, &rem))
fprintf(fp, "%#x", rem);
else if (rem != 0)
fprintf(fp, "|%#x", rem);
break;
case EVFILT_PROC:
case EVFILT_PROCDESC:
if (!print_mask_int(fp, kevent_proc_fflags, fflags, &rem))
fprintf(fp, "%#x", rem);
else if (rem != 0)
fprintf(fp, "|%#x", rem);
break;
case EVFILT_TIMER:
if (!print_mask_int(fp, kevent_timer_fflags, fflags, &rem))
fprintf(fp, "%#x", rem);
else if (rem != 0)
fprintf(fp, "|%#x", rem);
break;
case EVFILT_USER: {
unsigned int ctrl, data;
ctrl = fflags & NOTE_FFCTRLMASK;
data = fflags & NOTE_FFLAGSMASK;
if (fflags & NOTE_TRIGGER) {
fputs("NOTE_TRIGGER", fp);
if (fflags == NOTE_TRIGGER)
return;
fputc('|', fp);
}
/*
* An event with 'ctrl' == NOTE_FFNOP is either a reported
* (output) event for which only 'data' should be output
* or a pointless input event. Assume that pointless
* input events don't occur in practice. An event with
* NOTE_TRIGGER is always an input event.
*/
if (ctrl != NOTE_FFNOP || fflags & NOTE_TRIGGER) {
fprintf(fp, "%s|%#x",
lookup_value(kevent_user_ffctrl, ctrl), data);
} else {
print_integer(fp, data, base);
}
break;
}
default:
print_integer(fp, fflags, base);
break;
}
}
bool
sysdecode_kevent_flags(FILE *fp, int flags, int *rem)
{
return (print_mask_int(fp, keventflags, flags, rem));
}
const char *
sysdecode_kevent_filter(int filter)
{
return (lookup_value(keventfilters, filter));
}
const char *
sysdecode_kldsym_cmd(int cmd)
{

View File

@ -107,6 +107,8 @@ gen_table "mmapprot" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/
gen_table "ngbtsolevel" "SOL_[A-Z0-9]+[[:space:]]+0x[0-9A-Fa-f]+" "netgraph/bluetooth/include/ng_btsocket.h"
gen_table "fileflags" "[SU]F_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/stat.h" "UF_COMPRESSED|UF_TRACKED|UF_SETTABLE|SF_SETTABLE"
gen_table "filemode" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
gen_table "keventflags" "EV_[A-Z]+[[:space:]]+0x[0-9]+" "sys/event.h" "EV_SYSFLAGS|EV_DROP|EV_FLAG[12]"
gen_table "keventfilters" "EVFILT_[A-Z]+[[:space:]]+\(-[0-9]+\)" "sys/event.h"
gen_table "mountflags" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
gen_table "msyncflags" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
gen_table "nfssvcflags" "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+" "nfs/nfssvc.h"

View File

@ -25,7 +25,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd June 3, 2017
.Dd November 24, 2017
.Dt SYSDECODE 3
.Os
.Sh NAME
@ -73,6 +73,7 @@ A placeholder for use when the ABI is not known.
.Xr sysdecode_enum 3 ,
.Xr sysdecode_fcntl_arg 3 ,
.Xr sysdecode_ioctlname 3 ,
.Xr sysdecode_kevent 3 ,
.Xr sysdecode_mask 3 ,
.Xr sysdecode_quotactl_cmd 3 ,
.Xr sysdecode_sigcode 3 ,

View File

@ -60,6 +60,10 @@ const char *sysdecode_getrusage_who(int _who);
const char *sysdecode_idtype(int _idtype);
const char *sysdecode_ioctlname(unsigned long _val);
const char *sysdecode_ipproto(int _protocol);
void sysdecode_kevent_fflags(FILE *_fp, short _filter, int _fflags,
int _base);
const char *sysdecode_kevent_filter(int _filter);
bool sysdecode_kevent_flags(FILE *_fp, int _flags, int *_rem);
const char *sysdecode_kldsym_cmd(int _cmd);
const char *sysdecode_kldunload_flags(int _flags);
const char *sysdecode_lio_listio_mode(int _mode);

View File

@ -0,0 +1,126 @@
.\"
.\" Copyright (c) 2017 John Baldwin <jhb@FreeBSD.org>
.\" All rights reserved.
.\"
.\" 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 AUTHOR 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 AUTHOR 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.
.\"
.\" $FreeBSD$
.\"
.Dd November 24, 2017
.Dt sysdecode_kevent 3
.Os
.Sh NAME
.Nm sysdecode_kevent ,
.Nm sysdecode_kevent_fflags ,
.Nm sysdecode_kevent_filter ,
.Nm sysdecode_kevent_flags
.Nd output description of kevent structure fields
.Sh LIBRARY
.Lb libsysdecode
.Sh SYNOPSIS
.In sys/types.h
.In stdbool.h
.In stdio.h
.In sysdecode.h
.Ft void
.Fn sysdecode_kevent_fflags "FILE *fp" "short filter" "int fflags" "int base"
.Ft bool
.Fn sysdecode_kevent_flags "FILE *fp" "int flags" "int *rem"
.Ft const char *
.Fn sysdecode_kevent_filter "int filter"
.Sh DESCRIPTION
These functions provide text descriptions of
.Vt struct kevent
fields.
.Pp
The
.Fn sysdecode_kevent_fflags
function outputs a text description of the
.Fa fflags
member of a
.Vt struct kevent
to the stream
.Fa fp .
For the
.Dv EVFILT_READ ,
.Dv EVFILT_WRITE ,
.Dv EVFILT_VNODE ,
.Dv EVFILT_PROC ,
.Dv EVFILT_PROCDESC ,
.Dv EVFILT_TIMER ,
and
.Dv EVFILT_USER
filters,
.Fn sysdecode_kevent_fflags
outputs a bitmask of filter-specific
.Dv NOTE_*
flags as documented in
.Xr kevent 2 .
For other values of
.Fa filter ,
the value of
.Fa fflags
is output in the indicated
.Fa base
.Pq one of 8, 10, or 16 .
.Pp
The
.Fn sysdecode_kevent_filter
function returns a text description of the
.Fa filter
member of a
.Vt struct kevent .
.Dv NULL
is returned if the
.Fa filter
value is unknown.
.Pp
The
.Fn sysdecode_kevent_flags
function outputs a text description of the
.Fa flags
member of a
.Vt struct kevent
to the stream
.Fa fp .
This function uses the same calling convention and formatting as the other
functions described in
.Xr sysdecode_mask 3 .
.Sh RETURN VALUES
The
.Nm sysdecode_kevent_filter
function returns the name of a filter or
.Dv NULL if the filter value is unknown.
.Pp
The
.Nm sysdecode_kevent_flags
function returns
.Dv true
if any flags in the
.Fa flags
field were decoded and
.Dv false
if no flags were decoded.
.Sh SEE ALSO
.Xr sysdecode 3 ,
.Xr sysdecode_enum 3 ,
.Xr sysdecode_mask 3

View File

@ -145,22 +145,6 @@ struct statfs32 {
int32_t f_spare[2];
};
struct kevent32 {
uint32_t ident; /* identifier for this event */
short filter; /* filter for event */
u_short flags;
u_int fflags;
#ifndef __amd64__
uint32_t pad0;
#endif
int32_t data1, data2;
uint32_t udata; /* opaque user data identifier */
#ifndef __amd64__
uint32_t pad1;
#endif
uint32_t ext64[8];
};
struct iovec32 {
u_int32_t iov_base;
int iov_len;

View File

@ -30,9 +30,14 @@ __FBSDID("$FreeBSD$");
#include "opt_compat.h"
#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_ktrace.h"
#define __ELF_WORD_SIZE 32
#ifdef COMPAT_FREEBSD11
#define _WANT_FREEBSD11_KEVENT
#endif
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/capsicum.h>
@ -83,6 +88,9 @@ __FBSDID("$FreeBSD$");
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#ifdef KTRACE
#include <sys/ktrace.h>
#endif
#ifdef INET
#include <netinet/in.h>
@ -714,6 +722,9 @@ freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
.k_copyout = freebsd32_kevent_copyout,
.k_copyin = freebsd32_kevent_copyin,
};
#ifdef KTRACE
struct kevent32 *eventlist = uap->eventlist;
#endif
int error;
if (uap->timeout) {
@ -725,21 +736,22 @@ freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
tsp = &ts;
} else
tsp = NULL;
#ifdef KTRACE
if (KTRPOINT(td, KTR_STRUCT_ARRAY))
ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
uap->nchanges, sizeof(struct kevent32));
#endif
error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
&k_ops, tsp);
#ifdef KTRACE
if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
td->td_retval[0], sizeof(struct kevent32));
#endif
return (error);
}
#ifdef COMPAT_FREEBSD11
struct kevent32_freebsd11 {
u_int32_t ident; /* identifier for this event */
short filter; /* filter for event */
u_short flags;
u_int fflags;
int32_t data;
u_int32_t udata; /* opaque user data identifier */
};
static int
freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
{
@ -807,6 +819,9 @@ freebsd11_freebsd32_kevent(struct thread *td,
.k_copyout = freebsd32_kevent11_copyout,
.k_copyin = freebsd32_kevent11_copyin,
};
#ifdef KTRACE
struct kevent32_freebsd11 *eventlist = uap->eventlist;
#endif
int error;
if (uap->timeout) {
@ -818,8 +833,20 @@ freebsd11_freebsd32_kevent(struct thread *td,
tsp = &ts;
} else
tsp = NULL;
#ifdef KTRACE
if (KTRPOINT(td, KTR_STRUCT_ARRAY))
ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
uap->changelist, uap->nchanges,
sizeof(struct kevent32_freebsd11));
#endif
error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
&k_ops, tsp);
#ifdef KTRACE
if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
eventlist, td->td_retval[0],
sizeof(struct kevent32_freebsd11));
#endif
return (error);
}
#endif

View File

@ -33,6 +33,10 @@ __FBSDID("$FreeBSD$");
#include "opt_ktrace.h"
#include "opt_kqueue.h"
#ifdef COMPAT_FREEBSD11
#define _WANT_FREEBSD11_KEVENT
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/capsicum.h>
@ -115,7 +119,7 @@ static void kqueue_fo_release(int filt);
struct g_kevent_args;
static int kern_kevent_generic(struct thread *td,
struct g_kevent_args *uap,
struct kevent_copyops *k_ops);
struct kevent_copyops *k_ops, const char *struct_name);
static fo_ioctl_t kqueue_ioctl;
static fo_poll_t kqueue_poll;
@ -905,17 +909,6 @@ kern_kqueue(struct thread *td, int flags, struct filecaps *fcaps)
return (0);
}
#ifdef KTRACE
static size_t
kev_iovlen(int n, u_int kgio, size_t kevent_size)
{
if (n < 0 || n >= kgio / kevent_size)
return (kgio);
return (n * kevent_size);
}
#endif
struct g_kevent_args {
int fd;
void *changelist;
@ -943,22 +936,18 @@ sys_kevent(struct thread *td, struct kevent_args *uap)
.timeout = uap->timeout,
};
return (kern_kevent_generic(td, &gk_args, &k_ops));
return (kern_kevent_generic(td, &gk_args, &k_ops, "kevent"));
}
static int
kern_kevent_generic(struct thread *td, struct g_kevent_args *uap,
struct kevent_copyops *k_ops)
struct kevent_copyops *k_ops, const char *struct_name)
{
struct timespec ts, *tsp;
int error;
#ifdef KTRACE
struct uio ktruio;
struct iovec ktriov;
struct uio *ktruioin = NULL;
struct uio *ktruioout = NULL;
u_int kgio;
struct kevent *eventlist = uap->eventlist;
#endif
int error;
if (uap->timeout != NULL) {
error = copyin(uap->timeout, &ts, sizeof(ts));
@ -969,35 +958,18 @@ kern_kevent_generic(struct thread *td, struct g_kevent_args *uap,
tsp = NULL;
#ifdef KTRACE
if (KTRPOINT(td, KTR_GENIO)) {
kgio = ktr_geniosize;
ktriov.iov_base = uap->changelist;
ktriov.iov_len = kev_iovlen(uap->nchanges, kgio,
k_ops->kevent_size);
ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
.uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
.uio_td = td };
ktruioin = cloneuio(&ktruio);
ktriov.iov_base = uap->eventlist;
ktriov.iov_len = kev_iovlen(uap->nevents, kgio,
k_ops->kevent_size);
ktriov.iov_len = uap->nevents * k_ops->kevent_size;
ktruioout = cloneuio(&ktruio);
}
if (KTRPOINT(td, KTR_STRUCT_ARRAY))
ktrstructarray(struct_name, UIO_USERSPACE, uap->changelist,
uap->nchanges, k_ops->kevent_size);
#endif
error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
k_ops, tsp);
#ifdef KTRACE
if (ktruioin != NULL) {
ktruioin->uio_resid = kev_iovlen(uap->nchanges, kgio,
k_ops->kevent_size);
ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
ktruioout->uio_resid = kev_iovlen(td->td_retval[0], kgio,
k_ops->kevent_size);
ktrgenio(uap->fd, UIO_READ, ktruioout, error);
}
if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
ktrstructarray(struct_name, UIO_USERSPACE, eventlist,
td->td_retval[0], k_ops->kevent_size);
#endif
return (error);
@ -1040,15 +1012,6 @@ kevent_copyin(void *arg, struct kevent *kevp, int count)
}
#ifdef COMPAT_FREEBSD11
struct kevent_freebsd11 {
__uintptr_t ident; /* identifier for this event */
short filter; /* filter for event */
unsigned short flags;
unsigned int fflags;
__intptr_t data;
void *udata; /* opaque user data identifier */
};
static int
kevent11_copyout(void *arg, struct kevent *kevp, int count)
{
@ -1123,7 +1086,7 @@ freebsd11_kevent(struct thread *td, struct freebsd11_kevent_args *uap)
.timeout = uap->timeout,
};
return (kern_kevent_generic(td, &gk_args, &k_ops));
return (kern_kevent_generic(td, &gk_args, &k_ops, "kevent_freebsd11"));
}
#endif

View File

@ -106,6 +106,7 @@ struct ktr_request {
struct ktr_csw ktr_csw;
struct ktr_fault ktr_fault;
struct ktr_faultend ktr_faultend;
struct ktr_struct_array ktr_struct_array;
} ktr_data;
STAILQ_ENTRY(ktr_request) ktr_list;
};
@ -125,6 +126,7 @@ static int data_lengths[] = {
[KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
[KTR_FAULT] = sizeof(struct ktr_fault),
[KTR_FAULTEND] = sizeof(struct ktr_faultend),
[KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
};
static STAILQ_HEAD(, ktr_request) ktr_free;
@ -742,7 +744,7 @@ ktrcsw(int out, int user, const char *wmesg)
}
void
ktrstruct(const char *name, void *data, size_t datalen)
ktrstruct(const char *name, const void *data, size_t datalen)
{
struct ktr_request *req;
char *buf;
@ -764,6 +766,52 @@ ktrstruct(const char *name, void *data, size_t datalen)
ktr_submitrequest(curthread, req);
}
void
ktrstructarray(const char *name, enum uio_seg seg, const void *data,
int num_items, size_t struct_size)
{
struct ktr_request *req;
struct ktr_struct_array *ksa;
char *buf;
size_t buflen, datalen, namelen;
int max_items;
/* Trim array length to genio size. */
max_items = ktr_geniosize / struct_size;
if (num_items > max_items) {
if (max_items == 0)
num_items = 1;
else
num_items = max_items;
}
datalen = num_items * struct_size;
if (data == NULL)
datalen = 0;
namelen = strlen(name) + 1;
buflen = namelen + datalen;
buf = malloc(buflen, M_KTRACE, M_WAITOK);
strcpy(buf, name);
if (seg == UIO_SYSSPACE)
bcopy(data, buf + namelen, datalen);
else {
if (copyin(data, buf + namelen, datalen) != 0) {
free(buf, M_KTRACE);
return;
}
}
if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
free(buf, M_KTRACE);
return;
}
ksa = &req->ktr_data.ktr_struct_array;
ksa->struct_size = struct_size;
req->ktr_buffer = buf;
req->ktr_header.ktr_len = buflen;
ktr_submitrequest(curthread, req);
}
void
ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
const cap_rights_t *held)

View File

@ -71,6 +71,47 @@ struct kevent {
__uint64_t ext[4];
};
#if defined(_WANT_FREEBSD11_KEVENT)
/* Older structure used in FreeBSD 11.x and older. */
struct kevent_freebsd11 {
__uintptr_t ident; /* identifier for this event */
short filter; /* filter for event */
unsigned short flags;
unsigned int fflags;
__intptr_t data;
void *udata; /* opaque user data identifier */
};
#endif
#if defined(_WANT_KEVENT32) || (defined(_KERNEL) && defined(__LP64__))
struct kevent32 {
uint32_t ident; /* identifier for this event */
short filter; /* filter for event */
u_short flags;
u_int fflags;
#ifndef __amd64__
uint32_t pad0;
#endif
int32_t data1, data2;
uint32_t udata; /* opaque user data identifier */
#ifndef __amd64__
uint32_t pad1;
#endif
uint32_t ext64[8];
};
#ifdef _WANT_FREEBSD11_KEVENT
struct kevent32_freebsd11 {
u_int32_t ident; /* identifier for this event */
short filter; /* filter for event */
u_short flags;
u_int fflags;
int32_t data;
u_int32_t udata; /* opaque user data identifier */
};
#endif
#endif
/* actions */
#define EV_ADD 0x0001 /* add event to kq (implies enable) */
#define EV_DELETE 0x0002 /* delete event from kq */

View File

@ -220,6 +220,18 @@ struct ktr_faultend {
int result;
};
/*
* KTR_STRUCT_ARRAY - array of misc. structs
*/
#define KTR_STRUCT_ARRAY 15
struct ktr_struct_array {
size_t struct_size;
/*
* Followed by null-terminated structure name and then payload
* contents.
*/
};
/*
* KTR_DROP - If this bit is set in ktr_type, then at least one event
* between the previous record and this record was dropped.
@ -244,6 +256,7 @@ struct ktr_faultend {
#define KTRFAC_CAPFAIL (1<<KTR_CAPFAIL)
#define KTRFAC_FAULT (1<<KTR_FAULT)
#define KTRFAC_FAULTEND (1<<KTR_FAULTEND)
#define KTRFAC_STRUCT_ARRAY (1<<KTR_STRUCT_ARRAY)
/*
* trace flags (also in p_traceflags)
@ -267,7 +280,8 @@ void ktrprocexec(struct proc *, struct ucred **, struct vnode **);
void ktrprocexit(struct thread *);
void ktrprocfork(struct proc *, struct proc *);
void ktruserret(struct thread *);
void ktrstruct(const char *, void *, size_t);
void ktrstruct(const char *, const void *, size_t);
void ktrstructarray(const char *, enum uio_seg, const void *, int, size_t);
void ktrcapfail(enum ktr_cap_fail_type, const cap_rights_t *,
const cap_rights_t *);
#define ktrcaprights(s) \

View File

@ -44,11 +44,16 @@ static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
__FBSDID("$FreeBSD$");
#define _WANT_KERNEL_ERRNO
#ifdef __LP64__
#define _WANT_KEVENT32
#endif
#define _WANT_FREEBSD11_KEVENT
#include <sys/param.h>
#include <sys/capsicum.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/event.h>
#include <sys/ktrace.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
@ -111,6 +116,8 @@ void ktrstruct(char *, size_t);
void ktrcapfail(struct ktr_cap_fail *);
void ktrfault(struct ktr_fault *);
void ktrfaultend(struct ktr_faultend *);
void ktrkevent(struct kevent *);
void ktrstructarray(struct ktr_struct_array *, size_t);
void usage(void);
#define TIMESTAMP_NONE 0x0
@ -546,6 +553,9 @@ main(int argc, char *argv[])
case KTR_FAULTEND:
ktrfaultend((struct ktr_faultend *)m);
break;
case KTR_STRUCT_ARRAY:
ktrstructarray((struct ktr_struct_array *)m, ktrlen);
break;
default:
printf("\n");
break;
@ -680,6 +690,7 @@ dumpheader(struct ktr_header *kth)
type = "USER";
break;
case KTR_STRUCT:
case KTR_STRUCT_ARRAY:
type = "STRU";
break;
case KTR_SYSCTL:
@ -2071,6 +2082,137 @@ ktrfaultend(struct ktr_faultend *ktr)
printf("\n");
}
void
ktrkevent(struct kevent *kev)
{
printf("{ ident=");
switch (kev->filter) {
case EVFILT_READ:
case EVFILT_WRITE:
case EVFILT_VNODE:
case EVFILT_PROC:
case EVFILT_TIMER:
case EVFILT_PROCDESC:
case EVFILT_EMPTY:
printf("%ju", (uintmax_t)kev->ident);
break;
case EVFILT_SIGNAL:
print_signal(kev->ident);
break;
default:
printf("%p", (void *)kev->ident);
}
printf(", filter=");
print_integer_arg(sysdecode_kevent_filter, kev->filter);
printf(", flags=");
print_mask_arg0(sysdecode_kevent_flags, kev->flags);
printf(", fflags=");
sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
decimal ? 10 : 16);
printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
}
void
ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
{
struct kevent kev;
char *name, *data;
size_t namelen, datalen;
int i;
bool first;
buflen -= sizeof(*ksa);
for (name = (char *)(ksa + 1), namelen = 0;
namelen < buflen && name[namelen] != '\0';
++namelen)
/* nothing */;
if (namelen == buflen)
goto invalid;
if (name[namelen] != '\0')
goto invalid;
/* sanity check */
for (i = 0; i < (int)namelen; ++i)
if (!isalnum(name[i]) && name[i] != '_')
goto invalid;
data = name + namelen + 1;
datalen = buflen - namelen - 1;
printf("struct %s[] = { ", name);
first = true;
for (; datalen >= ksa->struct_size;
data += ksa->struct_size, datalen -= ksa->struct_size) {
if (!first)
printf("\n ");
else
first = false;
if (strcmp(name, "kevent") == 0) {
if (ksa->struct_size != sizeof(kev))
goto bad_size;
memcpy(&kev, data, sizeof(kev));
ktrkevent(&kev);
} else if (strcmp(name, "kevent_freebsd11") == 0) {
struct kevent_freebsd11 kev11;
if (ksa->struct_size != sizeof(kev11))
goto bad_size;
memcpy(&kev11, data, sizeof(kev11));
memset(&kev, 0, sizeof(kev));
kev.ident = kev11.ident;
kev.filter = kev11.filter;
kev.flags = kev11.flags;
kev.fflags = kev11.fflags;
kev.data = kev11.data;
kev.udata = kev11.udata;
ktrkevent(&kev);
#ifdef _WANT_KEVENT32
} else if (strcmp(name, "kevent32") == 0) {
struct kevent32 kev32;
if (ksa->struct_size != sizeof(kev32))
goto bad_size;
memcpy(&kev32, data, sizeof(kev32));
memset(&kev, 0, sizeof(kev));
kev.ident = kev32.ident;
kev.filter = kev32.filter;
kev.flags = kev32.flags;
kev.fflags = kev32.fflags;
#if BYTE_ORDER == BIG_ENDIAN
kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
#else
kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
#endif
kev.udata = (void *)(uintptr_t)kev32.udata;
ktrkevent(&kev);
} else if (strcmp(name, "kevent32_freebsd11") == 0) {
struct kevent32_freebsd11 kev32;
if (ksa->struct_size != sizeof(kev32))
goto bad_size;
memcpy(&kev32, data, sizeof(kev32));
memset(&kev, 0, sizeof(kev));
kev.ident = kev32.ident;
kev.filter = kev32.filter;
kev.flags = kev32.flags;
kev.fflags = kev32.fflags;
kev.data = kev32.data;
kev.udata = (void *)(uintptr_t)kev32.udata;
ktrkevent(&kev);
#endif
} else {
printf("<unknown structure> }\n");
return;
}
}
printf(" }\n");
return;
invalid:
printf("invalid record\n");
return;
bad_size:
printf("<bad size> }\n");
return;
}
void
usage(void)
{

View File

@ -34,7 +34,8 @@
#define DEF_POINTS (KTRFAC_SYSCALL | KTRFAC_SYSRET | KTRFAC_NAMEI | \
KTRFAC_GENIO | KTRFAC_PSIG | KTRFAC_USER | \
KTRFAC_STRUCT | KTRFAC_SYSCTL | KTRFAC_CAPFAIL)
KTRFAC_STRUCT | KTRFAC_SYSCTL | KTRFAC_CAPFAIL | \
KTRFAC_STRUCT_ARRAY)
#define PROC_ABI_POINTS (KTRFAC_PROCCTOR | KTRFAC_PROCDTOR)

View File

@ -52,7 +52,7 @@ enum Argtype { None = 1, Hex, Octal, Int, UInt, LongHex, Name, Ptr, Stat, Stat11
Sockoptname, Msgflags, CapRights, PUInt, PQuadHex, Acltype,
Extattrnamespace, Minherit, Mlockall, Mountflags, Msync, Priowhich,
Ptraceop, Quotactlcmd, Reboothowto, Rtpriofunc, Schedpolicy, Schedparam,
PSig, Siginfo,
PSig, Siginfo, Kevent11,
CloudABIAdvice, CloudABIClockID, ClouduABIFDSFlags,
CloudABIFDStat, CloudABIFileStat, CloudABIFileType,

View File

@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$");
#include <sys/capsicum.h>
#include <sys/types.h>
#define _WANT_FREEBSD11_KEVENT
#include <sys/event.h>
#include <sys/ioccom.h>
#include <sys/mount.h>
@ -152,6 +153,9 @@ static struct syscall decoded_syscalls[] = {
{ .name = "compat11.fstatat", .ret_type = 1, .nargs = 4,
.args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat11 | OUT, 2 },
{ Atflags, 3 } } },
{ .name = "compat11.kevent", .ret_type = 1, .nargs = 6,
.args = { { Int, 0 }, { Kevent11, 1 }, { Int, 2 },
{ Kevent11 | OUT, 3 }, { Int, 4 }, { Timespec, 5 } } },
{ .name = "compat11.lstat", .ret_type = 1, .nargs = 2,
.args = { { Name | IN, 0 }, { Stat11 | OUT, 1 } } },
{ .name = "compat11.stat", .ret_type = 1, .nargs = 2,
@ -650,43 +654,6 @@ struct xlat {
#define X(a) { a, #a },
#define XEND { 0, NULL }
static struct xlat kevent_filters[] = {
X(EVFILT_READ) X(EVFILT_WRITE) X(EVFILT_AIO) X(EVFILT_VNODE)
X(EVFILT_PROC) X(EVFILT_SIGNAL) X(EVFILT_TIMER)
X(EVFILT_PROCDESC) X(EVFILT_FS) X(EVFILT_LIO) X(EVFILT_USER)
X(EVFILT_SENDFILE) XEND
};
static struct xlat kevent_flags[] = {
X(EV_ADD) X(EV_DELETE) X(EV_ENABLE) X(EV_DISABLE) X(EV_ONESHOT)
X(EV_CLEAR) X(EV_RECEIPT) X(EV_DISPATCH) X(EV_FORCEONESHOT)
X(EV_DROP) X(EV_FLAG1) X(EV_ERROR) X(EV_EOF) XEND
};
static struct xlat kevent_user_ffctrl[] = {
X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY)
XEND
};
static struct xlat kevent_rdwr_fflags[] = {
X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND
};
static struct xlat kevent_vnode_fflags[] = {
X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB)
X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) XEND
};
static struct xlat kevent_proc_fflags[] = {
X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR)
X(NOTE_CHILD) XEND
};
static struct xlat kevent_timer_fflags[] = {
X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS)
XEND
};
static struct xlat poll_flags[] = {
X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR)
X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND)
@ -1137,7 +1104,7 @@ strsig2(int sig)
}
static void
print_kevent(FILE *fp, struct kevent *ke, int input)
print_kevent(FILE *fp, struct kevent *ke)
{
switch (ke->filter) {
@ -1147,6 +1114,7 @@ print_kevent(FILE *fp, struct kevent *ke, int input)
case EVFILT_PROC:
case EVFILT_TIMER:
case EVFILT_PROCDESC:
case EVFILT_EMPTY:
fprintf(fp, "%ju", (uintmax_t)ke->ident);
break;
case EVFILT_SIGNAL:
@ -1155,42 +1123,12 @@ print_kevent(FILE *fp, struct kevent *ke, int input)
default:
fprintf(fp, "%p", (void *)ke->ident);
}
fprintf(fp, ",%s,%s,", xlookup(kevent_filters, ke->filter),
xlookup_bits(kevent_flags, ke->flags));
switch (ke->filter) {
case EVFILT_READ:
case EVFILT_WRITE:
fputs(xlookup_bits(kevent_rdwr_fflags, ke->fflags), fp);
break;
case EVFILT_VNODE:
fputs(xlookup_bits(kevent_vnode_fflags, ke->fflags), fp);
break;
case EVFILT_PROC:
case EVFILT_PROCDESC:
fputs(xlookup_bits(kevent_proc_fflags, ke->fflags), fp);
break;
case EVFILT_TIMER:
fputs(xlookup_bits(kevent_timer_fflags, ke->fflags), fp);
break;
case EVFILT_USER: {
int ctrl, data;
ctrl = ke->fflags & NOTE_FFCTRLMASK;
data = ke->fflags & NOTE_FFLAGSMASK;
if (input) {
fputs(xlookup(kevent_user_ffctrl, ctrl), fp);
if (ke->fflags & NOTE_TRIGGER)
fputs("|NOTE_TRIGGER", fp);
if (data != 0)
fprintf(fp, "|%#x", data);
} else {
fprintf(fp, "%#x", data);
}
break;
}
default:
fprintf(fp, "%#x", ke->fflags);
}
fprintf(fp, ",");
print_integer_arg(sysdecode_kevent_filter, fp, ke->filter);
fprintf(fp, ",");
print_mask_arg(sysdecode_kevent_flags, fp, ke->flags);
fprintf(fp, ",");
sysdecode_kevent_fflags(fp, ke->filter, ke->fflags, 16);
fprintf(fp, ",%#jx,%p", (uintmax_t)ke->data, ke->udata);
}
@ -1795,7 +1733,7 @@ print_arg(struct syscall_args *sc, unsigned long *args, long *retval,
fputc('{', fp);
for (i = 0; i < numevents; i++) {
fputc(' ', fp);
print_kevent(fp, &ke[i], sc->offset == 1);
print_kevent(fp, &ke[i]);
}
fputs(" }", fp);
} else {
@ -1804,6 +1742,47 @@ print_arg(struct syscall_args *sc, unsigned long *args, long *retval,
free(ke);
break;
}
case Kevent11: {
struct kevent_freebsd11 *ke11;
struct kevent ke;
int numevents = -1;
size_t bytes;
int i;
if (sc->offset == 1)
numevents = args[sc->offset+1];
else if (sc->offset == 3 && retval[0] != -1)
numevents = retval[0];
if (numevents >= 0) {
bytes = sizeof(struct kevent_freebsd11) * numevents;
if ((ke11 = malloc(bytes)) == NULL)
err(1,
"Cannot malloc %zu bytes for kevent array",
bytes);
} else
ke11 = NULL;
memset(&ke, 0, sizeof(ke));
if (numevents >= 0 && get_struct(pid, (void *)args[sc->offset],
ke11, bytes) != -1) {
fputc('{', fp);
for (i = 0; i < numevents; i++) {
fputc(' ', fp);
ke.ident = ke11[i].ident;
ke.filter = ke11[i].filter;
ke.flags = ke11[i].flags;
ke.fflags = ke11[i].fflags;
ke.data = ke11[i].data;
ke.udata = ke11[i].udata;
print_kevent(fp, &ke);
}
fputs(" }", fp);
} else {
fprintf(fp, "0x%lx", args[sc->offset]);
}
free(ke11);
break;
}
case Stat: {
struct stat st;