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. 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 80f8a34..d004ac6 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 6449bce..52f40b3 100644 --- a/defs.h +++ b/defs.h @@ -504,6 +504,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 allocation); +void dyxlat_free(struct dyxlat *dyxlat); +struct xlat *dyxlat_get(struct dyxlat *dyxlat); +void dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str); + 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..c0952ed --- /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 allocation) +{ + struct dyxlat *dyxlat; + + dyxlat = xmalloc(sizeof(*dyxlat)); + + dyxlat->used = 1; + dyxlat->allocated = allocation ? allocation : 10; + 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); +} + +struct xlat * +dyxlat_get(struct dyxlat *dyxlat) +{ + return dyxlat->xlat; +} + +void +dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str) +{ + size_t i; + + for (i = 0; i < dyxlat->used - 1; i++) { + if (dyxlat->xlat[i].val == val) { + if (strcmp(dyxlat->xlat[i].str, str) == 0) + return; + + free((void *)(dyxlat->xlat[i].str)); + dyxlat->xlat[i].str = xstrdup(str); + return; + } + } + + dyxlat->used++; + if (dyxlat->used == dyxlat->allocated) { + dyxlat->allocated *= 2; + dyxlat->xlat = xreallocarray(dyxlat->xlat, dyxlat->allocated, + sizeof(struct xlat)); + } + + dyxlat->xlat[dyxlat->used - 2].val = val; + dyxlat->xlat[dyxlat->used - 2].str = xstrdup(str); + MARK_END(dyxlat->xlat[dyxlat->used - 1]); +} -- 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