From d6fb4894981bcd8641fbe8cab52057dc2a58ad3a Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Tue, 15 Dec 2015 00:05:07 +0000 Subject: [PATCH] Start on a new library (libsysdecode) that provides routines for decoding system call information such as system call arguments. Initially this will consist of pulling duplicated code out of truss and kdump though it may prove useful for other utilities in the future. This commit moves the shared utrace(2) record parser out of kdump into the library and updates kdump and truss to use it. One difference from the previous version is that the library version treats unknown events that start with the "RTLD" signature as unknown events. This simplifies the interface and allows the consumer to decide how to handle all non-recognized events. Instead, this function only generates a string description for known malloc() and RTLD records. Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D4537 --- contrib/mdocml/lib.in | 1 + lib/Makefile | 1 + lib/libc/sys/utrace.2 | 5 +- lib/libsysdecode/Makefile | 13 ++++ lib/libsysdecode/sysdecode.3 | 47 +++++++++++++ lib/libsysdecode/sysdecode.h | 34 +++++++++ lib/libsysdecode/sysdecode_utrace.3 | 73 ++++++++++++++++++++ {usr.bin/kdump => lib/libsysdecode}/utrace.c | 26 +++---- share/mk/bsd.libnames.mk | 1 + share/mk/src.libnames.mk | 1 + usr.bin/kdump/Makefile | 5 +- usr.bin/kdump/Makefile.depend | 1 + usr.bin/kdump/kdump.c | 4 +- usr.bin/truss/Makefile | 3 +- usr.bin/truss/Makefile.depend.amd64 | 1 + usr.bin/truss/syscalls.c | 6 +- 16 files changed, 192 insertions(+), 30 deletions(-) create mode 100644 lib/libsysdecode/Makefile create mode 100644 lib/libsysdecode/sysdecode.3 create mode 100644 lib/libsysdecode/sysdecode.h create mode 100644 lib/libsysdecode/sysdecode_utrace.3 rename {usr.bin/kdump => lib/libsysdecode}/utrace.c (90%) diff --git a/contrib/mdocml/lib.in b/contrib/mdocml/lib.in index d69a6542f38d..d6388b2471c2 100644 --- a/contrib/mdocml/lib.in +++ b/contrib/mdocml/lib.in @@ -110,6 +110,7 @@ LINE("libsdp", "Bluetooth Service Discovery Protocol User Library (libsdp, \\-l LINE("libssp", "Buffer Overflow Protection Library (libssp, \\-lssp)") LINE("libstdthreads", "C11 Threads Library (libstdthreads, \\-lstdthreads)") LINE("libSystem", "System Library (libSystem, \\-lSystem)") +LINE("libsysdcode", "System Argument Decoding Library (libsysdecode, \\-lsysdecode)") LINE("libtacplus", "TACACS+ Client Library (libtacplus, \\-ltacplus)") LINE("libtcplay", "TrueCrypt-compatible API library (libtcplay, \\-ltcplay)") LINE("libtermcap", "Termcap Access Library (libtermcap, \\-ltermcap)") diff --git a/lib/Makefile b/lib/Makefile index e1ee9a91da5d..de0acb370e70 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -98,6 +98,7 @@ SUBDIR= ${SUBDIR_ORDERED} \ libstand \ libstdbuf \ libstdthreads \ + libsysdecode \ libtacplus \ ${_libtelnet} \ ${_libthr} \ diff --git a/lib/libc/sys/utrace.2 b/lib/libc/sys/utrace.2 index 345c1fd9f49c..9f48ca95f05a 100644 --- a/lib/libc/sys/utrace.2 +++ b/lib/libc/sys/utrace.2 @@ -28,7 +28,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October 5, 2015 +.Dd December 11, 2015 .Dt UTRACE 2 .Os .Sh NAME @@ -71,7 +71,8 @@ support .Xr kdump 1 , .Xr ktrace 1 , .Xr ktrace 2 , -.Xr truss 1 +.Xr truss 1 , +.Xr sysdecode_utrace 3 .Sh HISTORY The .Fn utrace diff --git a/lib/libsysdecode/Makefile b/lib/libsysdecode/Makefile new file mode 100644 index 000000000000..8eb7908d5a39 --- /dev/null +++ b/lib/libsysdecode/Makefile @@ -0,0 +1,13 @@ +# $FreeBSD$ + +.include + +LIB= sysdecode + +SRCS= utrace.c +INCS= sysdecode.h + +MAN+= sysdecode.3 \ + sysdecode_utrace.3 + +.include diff --git a/lib/libsysdecode/sysdecode.3 b/lib/libsysdecode/sysdecode.3 new file mode 100644 index 000000000000..fd1677d12996 --- /dev/null +++ b/lib/libsysdecode/sysdecode.3 @@ -0,0 +1,47 @@ +.\" +.\" Copyright (c) 2015 John Baldwin +.\" 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 December 10, 2015 +.Dt SYSDECODE 3 +.Os +.Sh NAME +.Nm sysdecode +.Nd system argument decoding library +.Sh LIBRARY +.Lb libsysdecode +.Sh DESCRIPTION +The +.Nm +library includes several functions that provide descriptive names of +values associated with system calls. +.Sh SEE ALSO +.Xr sysdecode_utrace 3 +.Sh HISTORY +The +.Nm +library first appeared in +.Fx 11.0 . diff --git a/lib/libsysdecode/sysdecode.h b/lib/libsysdecode/sysdecode.h new file mode 100644 index 000000000000..10feee10456f --- /dev/null +++ b/lib/libsysdecode/sysdecode.h @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2015 John H. Baldwin + * 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$ + */ + +#ifndef __SYSDECODE_H__ +#define __SYSDECODE_H__ + +int sysdecode_utrace(FILE *_fp, void *_buf, size_t _len); + +#endif /* !__SYSDECODE_H__ */ diff --git a/lib/libsysdecode/sysdecode_utrace.3 b/lib/libsysdecode/sysdecode_utrace.3 new file mode 100644 index 000000000000..8bf2e8595677 --- /dev/null +++ b/lib/libsysdecode/sysdecode_utrace.3 @@ -0,0 +1,73 @@ +.\" +.\" Copyright (c) 2015 John Baldwin +.\" 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 December 11, 2015 +.Dt sysdecode_utrace 3 +.Os +.Sh NAME +.Nm sysdecode_utrace +.Nd produce text description of a utrace record +.Sh LIBRARY +.Lb libsysdecode +.Sh SYNOPSIS +.Ft int +.Fn sysdecode_utrace "FILE *fp" "void *buf" "size_t len" "int decimal" +.Sh DESCRIPTION +The +.Fn sysdecode_utrace +function outputs a textual representation of a +.Xr utrace 2 +record identified by +.Fa buf +and +.Fa len +to the output stream +.Fa fp . +.Pp +The function only outputs a representation for certain types of records. +If a record is recognized, +the function outputs the description and returns a non-zero value. +If the record is not recognized, +the function does not output anything and returns zero. +The +.Fn sysdecode_utrace +function currently recognizes +.Xr utrace 2 +records generated by +.Xr malloc 3 +and +.Xr rtld 1 . +.Sh RETURN VALUES +The +.Fn sysdecode_utrace +function returns a non-zero value if it recognizes a +.Xr utrace 2 +record; +otherwise it returns zero. +.Sh SEE ALSO +.Xr utrace 2 , +.Xr sysdecode 3 diff --git a/usr.bin/kdump/utrace.c b/lib/libsysdecode/utrace.c similarity index 90% rename from usr.bin/kdump/utrace.c rename to lib/libsysdecode/utrace.c index e7193659183b..6a251390b2bc 100644 --- a/usr.bin/kdump/utrace.c +++ b/lib/libsysdecode/utrace.c @@ -34,8 +34,7 @@ __FBSDID("$FreeBSD$"); #include #include #include - -int kdump_print_utrace(FILE *, void *, size_t, int); +#include #define UTRACE_DLOPEN_START 1 #define UTRACE_DLOPEN_STOP 2 @@ -60,11 +59,10 @@ struct utrace_rtld { char name[MAXPATHLEN]; }; -static void -print_utrace_rtld(FILE *fp, void *p, size_t len, int decimal) +static int +print_utrace_rtld(FILE *fp, void *p) { struct utrace_rtld *ut = p; - unsigned char *cp; void *parent; int mode; @@ -136,16 +134,9 @@ print_utrace_rtld(FILE *fp, void *p, size_t len, int decimal) ut->name); break; default: - cp = p; - cp += 4; - len -= 4; - fprintf(fp, "RTLD: %zu ", len); - while (len--) - if (decimal) - fprintf(fp, " %d", *cp++); - else - fprintf(fp, " %02x", *cp++); + return (0); } + return (1); } struct utrace_malloc { @@ -170,12 +161,11 @@ print_utrace_malloc(FILE *fp, void *p) } int -kdump_print_utrace(FILE *fp, void *p, size_t len, int decimal) +sysdecode_utrace(FILE *fp, void *p, size_t len) { - if (len >= 8 && bcmp(p, "RTLD", 4) == 0) { - print_utrace_rtld(fp, p, len, decimal); - return (1); + if (len == sizeof(struct utrace_rtld) && bcmp(p, "RTLD", 4) == 0) { + return (print_utrace_rtld(fp, p)); } if (len == sizeof(struct utrace_malloc)) { diff --git a/share/mk/bsd.libnames.mk b/share/mk/bsd.libnames.mk index 596aa0a62faf..d22f99a89c10 100644 --- a/share/mk/bsd.libnames.mk +++ b/share/mk/bsd.libnames.mk @@ -134,6 +134,7 @@ LIBSSP_NONSHARED?= ${DESTDIR}${LIBDIR}/libssp_nonshared.a LIBSTAND?= ${DESTDIR}${LIBDIR}/libstand.a LIBSTDCPLUSPLUS?= ${DESTDIR}${LIBDIR}/libstdc++.a LIBSTDTHREADS?= ${DESTDIR}${LIBDIR}/libstdthreads.a +LIBSYSDECODE?= ${DESTDIR}${LIBDIR}/libsysdecode.a LIBTACPLUS?= ${DESTDIR}${LIBDIR}/libtacplus.a LIBTERMCAP?= ${DESTDIR}${LIBDIR}/libtermcap.a LIBTERMCAPW?= ${DESTDIR}${LIBDIR}/libtermcapw.a diff --git a/share/mk/src.libnames.mk b/share/mk/src.libnames.mk index 089bf9e3329e..4a99fa2f238a 100644 --- a/share/mk/src.libnames.mk +++ b/share/mk/src.libnames.mk @@ -148,6 +148,7 @@ _LIBRARIES= \ ssp_nonshared \ stdthreads \ supcplusplus \ + sysdecode \ tacplus \ termcap \ termcapw \ diff --git a/usr.bin/kdump/Makefile b/usr.bin/kdump/Makefile index 5048fdd67227..52c0a09bcee2 100644 --- a/usr.bin/kdump/Makefile +++ b/usr.bin/kdump/Makefile @@ -6,11 +6,12 @@ .PATH: ${.CURDIR}/../ktrace PROG= kdump -SRCS= kdump_subr.c kdump_subr.h kdump.c ioctl.c subr.c utrace.c +SRCS= kdump_subr.c kdump_subr.h kdump.c ioctl.c subr.c CFLAGS+= -I${.CURDIR}/../ktrace -I${.CURDIR} -I${.CURDIR}/../.. -I. +LIBADD= sysdecode .if ${MK_CASPER} != "no" -LIBADD= capsicum +LIBADD+= capsicum CFLAGS+=-DHAVE_LIBCAPSICUM .endif diff --git a/usr.bin/kdump/Makefile.depend b/usr.bin/kdump/Makefile.depend index e0397feda9ff..f86656e4c92b 100644 --- a/usr.bin/kdump/Makefile.depend +++ b/usr.bin/kdump/Makefile.depend @@ -13,6 +13,7 @@ DIRDEPS = \ lib/libcapsicum \ lib/libcompiler_rt \ lib/libnv \ + lib/libsysdecode \ .include diff --git a/usr.bin/kdump/kdump.c b/usr.bin/kdump/kdump.c index 4804a81c5a49..8165bc719453 100644 --- a/usr.bin/kdump/kdump.c +++ b/usr.bin/kdump/kdump.c @@ -83,6 +83,7 @@ extern int errno; #include #include #include +#include #include #include #include @@ -116,7 +117,6 @@ void ktrfaultend(struct ktr_faultend *); void limitfd(int fd); void usage(void); void ioctlname(unsigned long, int); -int kdump_print_utrace(FILE *, void *, size_t, int); #define TIMESTAMP_NONE 0x0 #define TIMESTAMP_ABSOLUTE 0x1 @@ -1541,7 +1541,7 @@ ktruser(int len, void *p) { unsigned char *cp; - if (kdump_print_utrace(stdout, p, len, decimal)) { + if (sysdecode_utrace(stdout, p, len)) { printf("\n"); return; } diff --git a/usr.bin/truss/Makefile b/usr.bin/truss/Makefile index e78436513c6c..a300da17b398 100644 --- a/usr.bin/truss/Makefile +++ b/usr.bin/truss/Makefile @@ -4,8 +4,7 @@ NO_WERROR= PROG= truss SRCS= cloudabi.c ioctl.c main.c setup.c syscalls.c -.PATH: ${.CURDIR:H}/kdump -SRCS+= utrace.c +LIBADD= sysdecode CFLAGS+= -I${.CURDIR} -I. -I${.CURDIR}/../../sys CLEANFILES= ioctl.c diff --git a/usr.bin/truss/Makefile.depend.amd64 b/usr.bin/truss/Makefile.depend.amd64 index bf32fa45b6f3..ad9c8b3d5906 100644 --- a/usr.bin/truss/Makefile.depend.amd64 +++ b/usr.bin/truss/Makefile.depend.amd64 @@ -11,6 +11,7 @@ DIRDEPS = \ lib/${CSU_DIR} \ lib/libc \ lib/libcompiler_rt \ + lib/libsysdecode \ .include diff --git a/usr.bin/truss/syscalls.c b/usr.bin/truss/syscalls.c index ec74de9e8bcf..990403e657a4 100644 --- a/usr.bin/truss/syscalls.c +++ b/usr.bin/truss/syscalls.c @@ -65,6 +65,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -75,9 +76,6 @@ __FBSDID("$FreeBSD$"); #include "extern.h" #include "syscall.h" -/* usr.bin/kdump/utrace.c */ -int kdump_print_utrace(FILE *, void *, size_t, int); - /* 64-bit alignment on 32-bit platforms. */ #if !defined(__LP64__) && defined(__powerpc__) #define QUAD_ALIGN 1 @@ -1108,7 +1106,7 @@ print_utrace(FILE *fp, void *utrace_addr, size_t len) unsigned char *utrace_buffer; fprintf(fp, "{ "); - if (kdump_print_utrace(fp, utrace_addr, len, 0)) { + if (sysdecode_utrace(fp, utrace_addr, len)) { fprintf(fp, " }"); return; }