From 8e28dacdd9f4dd423cce0f1e6b9ddc20e86b4217 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Fri, 17 Jun 2016 15:39:25 +0200
Subject: [PATCH] add a small utility to extract a configuration file from a
 log

---
 common/utils/T/tracer/Makefile         |  6 ++-
 common/utils/T/tracer/extract_config.c | 75 ++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 common/utils/T/tracer/extract_config.c

diff --git a/common/utils/T/tracer/Makefile b/common/utils/T/tracer/Makefile
index 38a660562d..4345db1f70 100644
--- a/common/utils/T/tracer/Makefile
+++ b/common/utils/T/tracer/Makefile
@@ -5,7 +5,7 @@ CFLAGS=-Wall -g -pthread -DT_TRACER -I.
 
 LIBS=-lX11 -lm -lpng -lXft
 
-all: record replay textlog enb vcd
+all: record replay extract_config textlog enb vcd
 
 record: utils.o record.o database.o config.o
 	$(CC) $(CFLAGS) -o record $^ $(LIBS)
@@ -13,6 +13,9 @@ record: utils.o record.o database.o config.o
 replay: utils.o replay.o
 	$(CC) $(CFLAGS) -o replay $^ $(LIBS)
 
+extract_config: extract_config.o
+	$(CC) $(CFLAGS) -o extract_config $^ $(LIBS)
+
 textlog: utils.o textlog.o database.o event.o handler.o config.o \
          event_selector.o view/view.a gui/gui.a logger/logger.a \
          filter/filter.a
@@ -47,6 +50,7 @@ filter/filter.a:
 
 clean:
 	rm -f *.o core tracer_remote textlog enb vcd record replay
+	rm -f extract_config
 	cd gui && make clean
 	cd view && make clean
 	cd logger && make clean
diff --git a/common/utils/T/tracer/extract_config.c b/common/utils/T/tracer/extract_config.c
new file mode 100644
index 0000000000..f35d765cec
--- /dev/null
+++ b/common/utils/T/tracer/extract_config.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../T_defs.h"
+
+void usage(void)
+{
+  printf(
+"options:\n"
+"    -i <input file>           this option is mandatory\n"
+  );
+  exit(1);
+}
+
+#define ERR printf("ERROR: read file %s failed\n", input_filename)
+
+int main(int n, char **v)
+{
+  char *input_filename = NULL;
+  int i;
+  FILE *in;
+
+  for (i = 1; i < n; i++) {
+    if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage();
+    if (!strcmp(v[i], "-i"))
+      { if (i > n-2) usage(); input_filename = v[++i]; continue; }
+    usage();
+  }
+
+  if (input_filename == NULL) {
+    printf("ERROR: provide an input file (-i)\n");
+    exit(1);
+  }
+
+  in = fopen(input_filename, "r");
+  if (in == NULL) { perror(input_filename); abort(); }
+
+  while (1) {
+    int type;
+    int32_t length;
+    char v[T_BUFFER_MAX];
+    int vpos = 0;
+
+    /* read event from file */
+    if (fread(&length, 4, 1, in) != 1) break;
+    memcpy(v+vpos, &length, 4);
+    vpos += 4;
+#ifdef T_SEND_TIME
+    if (length < sizeof(struct timespec)) { ERR; break; }
+    if (fread(v+vpos, sizeof(struct timespec), 1, in) != 1) { ERR; break; }
+    vpos += sizeof(struct timespec);
+    length -= sizeof(struct timespec);
+#endif
+    if (length < sizeof(int)) { ERR; break; }
+    if (fread(&type, sizeof(int), 1, in) != 1) { ERR; break; }
+    memcpy(v+vpos, &type, sizeof(int));
+    vpos += sizeof(int);
+    length -= sizeof(int);
+    if (length) if (fread(v+vpos, length, 1, in) != 1) { ERR; break; }
+    vpos += length;
+
+    if (type == -1) {
+      if (length < sizeof(int)) { ERR; break; }
+      length -= sizeof(int);
+      if (fwrite(v+vpos-length, length, 1, stdout) != 1) { ERR; break; }
+    }
+
+    /* TODO: parse all file? */
+    if (type == -2) break;
+  }
+
+  fclose(in);
+
+  return 0;
+}
-- 
GitLab