xlat tables are generating in built-time of strace. printxval is suitable for printing `family' field of Netlink GENERIC protocol. However, contents of the xlat table cannot be defined in build-time because the values for the field are given by Linux kernel.
dyxlat functions are for building xlat in run-time. Decoding the field is the primary use case of functions but they can be used another purpose. * defs.h (dyxlat_alloc, dyxlat_free, dyxlat_add_pair): New functions declarations. (struct dyxlat): New opaque data type. * dyxlat.c: New file. Changes in version 3 (suggested by ldv): * Rename dyxlat_new/dyxlat_delete to dyxlat_alloc/dyxlat_free. Rename dyxlat_may_add_pair to dyxlat_may_add_pair. * Move dyxlat declarations closer to other xlat related prototypes. * Change the field types of dyxlat structure from int to size_t. * Merge the file static functions in dyxlat.c to the exported functions in the file. Change in version 4 (suggested by ldv): * (dyxlat_alloc) use "nmemb" instead of "allocation" as a parameter name. * Don't write parameters if their names and the names of their type are the same. * (dyxlat_get) add const modifier to the return type and the parameter. * (dyxlat_add_pari) Specify the string length of value explicitly. Use xstrndup internally. * (dyxlat_alloc): Change the default nmemb to 16. Extend the array when "used" is equal to greater than "allocated". Update the xlat element before incrmenting "used" field. Signed-off-by: Masatake YAMATO <yam...@redhat.com> --- Makefile.am | 1 + defs.h | 6 ++++ dyxlat.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 dyxlat.c diff --git a/Makefile.am b/Makefile.am index e22d480..f9e78ed 100644 --- a/Makefile.am +++ b/Makefile.am @@ -104,6 +104,7 @@ strace_SOURCES = \ dirent.c \ dirent64.c \ dm.c \ + dyxlat.c \ empty.h \ epoll.c \ evdev.c \ diff --git a/defs.h b/defs.h index 5e05457..f7b5709 100644 --- a/defs.h +++ b/defs.h @@ -508,6 +508,12 @@ extern enum sock_proto getfdproto(struct tcb *, int); extern const char *xlookup(const struct xlat *, const uint64_t); extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t); +struct dyxlat; +struct dyxlat *dyxlat_alloc(size_t nmemb); +void dyxlat_free(struct dyxlat *); +const struct xlat *dyxlat_get(const struct dyxlat *); +void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len); + extern unsigned long get_pagesize(void); extern int string_to_uint_ex(const char *str, char **endptr, diff --git a/dyxlat.c b/dyxlat.c new file mode 100644 index 0000000..a5c1495 --- /dev/null +++ b/dyxlat.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2017 The strace developers. + * + * 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. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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. + */ + +#include "defs.h" + +struct dyxlat { + size_t used; + size_t allocated; + struct xlat *xlat; +}; + +#define MARK_END(xlat) \ + do { \ + (xlat).val = 0; \ + (xlat).str = 0; \ + } while (0) + +struct dyxlat * +dyxlat_alloc(size_t nmemb) +{ + struct dyxlat *dyxlat; + + dyxlat = xmalloc(sizeof(*dyxlat)); + + dyxlat->used = 1; + dyxlat->allocated = nmemb ? nmemb : 16; + dyxlat->xlat = xcalloc(dyxlat->allocated, sizeof(struct xlat)); + MARK_END(dyxlat->xlat[0]); + + return dyxlat; +} + +void +dyxlat_free(struct dyxlat *dyxlat) +{ + size_t i; + + for (i = 0; i < dyxlat->used - 1; ++i) { + free((void *)(dyxlat->xlat[i].str)); + dyxlat->xlat[i].str = NULL; + } + + free(dyxlat->xlat); + dyxlat->xlat = NULL; + free(dyxlat); +} + +const struct xlat * +dyxlat_get(const struct dyxlat *dyxlat) +{ + return dyxlat->xlat; +} + +void +dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str, size_t len) +{ + size_t i; + + for (i = 0; i < dyxlat->used - 1; i++) { + if (dyxlat->xlat[i].val == val) { + if (strncmp(dyxlat->xlat[i].str, str, len) == 0) + return; + + free((void *)(dyxlat->xlat[i].str)); + dyxlat->xlat[i].str = xstrndup(str, len); + return; + } + } + + if (dyxlat->used >= dyxlat->allocated) { + dyxlat->allocated *= 2; + dyxlat->xlat = xreallocarray(dyxlat->xlat, dyxlat->allocated, + sizeof(struct xlat)); + } + + dyxlat->xlat[dyxlat->used - 1].val = val; + dyxlat->xlat[dyxlat->used - 1].str = xstrndup(str, len); + MARK_END(dyxlat->xlat[dyxlat->used]); + dyxlat->used++; +} -- 2.9.4 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Strace-devel mailing list Strace-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/strace-devel