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_new, dyxlat_delete, dyxlat_may_add_pair): New functions
declarations.
(struct dyxlat): New opaque data type.

* dyxlat.c: New file.

Signed-off-by: Masatake YAMATO <yam...@redhat.com>
---
 Makefile.am |   1 +
 defs.h      |   7 ++++
 dyxlat.c    | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 dyxlat.c

diff --git a/Makefile.am b/Makefile.am
index 3d6eba1..3a76b85 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..b9bdbb2 100644
--- a/defs.h
+++ b/defs.h
@@ -1018,4 +1018,11 @@ scno_is_valid(kernel_ulong_t scno)
 #define PRI__u64 PRI__64"u"
 #define PRI__x64 PRI__64"x"
 
+/* Dynamic Xlat */
+struct dyxlat;
+
+struct dyxlat *dyxlat_new    (int allocation);
+void dyxlat_delete (struct dyxlat *dyxlat);
+struct xlat *dyxlat_get (struct dyxlat *dyxlat);
+void dyxlat_may_add_pair (struct dyxlat *dyxlat, uint64_t val, const char 
*str);
 #endif /* !STRACE_DEFS_H */
diff --git a/dyxlat.c b/dyxlat.c
new file mode 100644
index 0000000..415cee6
--- /dev/null
+++ b/dyxlat.c
@@ -0,0 +1,120 @@
+/*
+ * 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 {
+       int used;
+       int allocated;
+       struct xlat *xlat;
+};
+
+#define MARK_END(xlat)                         \
+       do {                                    \
+               (xlat).val = 0;                 \
+               (xlat).str = 0;                 \
+       } while (0)
+
+static void
+dyxlat_init(struct dyxlat *dyxlat, int allocation)
+{
+       dyxlat->used = 1;
+       dyxlat->allocated = allocation? allocation: 10;
+       dyxlat->xlat = xcalloc(dyxlat->allocated, sizeof(struct xlat));
+       MARK_END(dyxlat->xlat[0]);
+}
+
+static void
+dyxlat_fini(struct dyxlat *dyxlat)
+{
+       int 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;
+}
+
+struct dyxlat *
+dyxlat_new(int allocation)
+{
+       struct dyxlat *dyxlat;
+
+       dyxlat = xmalloc(sizeof(*dyxlat));
+       dyxlat_init(dyxlat, allocation);
+       return dyxlat;
+}
+
+void
+dyxlat_delete(struct dyxlat *dyxlat)
+{
+       dyxlat_fini(dyxlat);
+       free(dyxlat);
+}
+
+struct xlat *
+dyxlat_get(struct dyxlat *dyxlat)
+{
+       return dyxlat->xlat;
+}
+
+static void
+dyxlat_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str)
+{
+       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]);
+}
+
+void
+dyxlat_may_add_pair(struct dyxlat *dyxlat, uint64_t val, const char *str)
+{
+       int i;
+
+       for (i = 0; i < dyxlat->used - 1; i++) {
+               if (dyxlat->xlat [i].val == val) {
+                       if (strcmp(dyxlat->xlat [i].str, str) == 0)
+                               return;
+                       else {
+                               free((void *)(dyxlat->xlat [i].str));
+                               dyxlat->xlat [i].str = xstrdup(str);
+                               return;
+                       }
+               }
+       }
+       dyxlat_add_pair(dyxlat, val, str);
+}
-- 
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

Reply via email to