CTF: open trace read
[babeltrace.git] / tests / test-dummytrace.c
index 81e97b98a72240d21618022147d2f87c2b8ba786..ed00f34b0a1aa75b8e9dc6d5df0cbdfed40a89e8 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * text-to-ctf.c
+ *
+ * BabelTrace - Convert Text to CTF
+ *
+ * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * Depends on glibc 2.10 for getline().
+ */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <uuid/uuid.h>
 #include <string.h>
 
-#define FILE_LEN       (4 * 4096)
+#include <babeltrace/babeltrace.h>
+#include <babeltrace/ctf/types.h>
+
+int babeltrace_debug = 0;
+
+static uuid_t s_uuid;
+
+static
+void write_packet_header(struct stream_pos *pos, uuid_t uuid)
+{
+       struct stream_pos dummy;
+
+       /* magic */
+       dummy_pos(pos, &dummy);
+       align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       assert(!pos_packet(&dummy));
+       
+       align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+       *(uint32_t *) get_pos_addr(pos) = 0xC1FC1FC1;
+       move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+
+       /* trace_uuid */
+       dummy_pos(pos, &dummy);
+       align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
+       move_pos(&dummy, 16 * CHAR_BIT);
+       assert(!pos_packet(&dummy));
+
+       align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
+       memcpy(get_pos_addr(pos), uuid, 16);
+       move_pos(pos, 16 * CHAR_BIT);
+}
+
+static
+void write_packet_context(struct stream_pos *pos)
+{
+       struct stream_pos dummy;
+
+       /* content_size */
+       dummy_pos(pos, &dummy);
+       align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       assert(!pos_packet(&dummy));
+       
+       align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+       *(uint32_t *) get_pos_addr(pos) = -1U;  /* Not known yet */
+       pos->content_size_loc = (uint32_t *) get_pos_addr(pos);
+       move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+
+       /* packet_size */
+       dummy_pos(pos, &dummy);
+       align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+       assert(!pos_packet(&dummy));
+       
+       align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+       *(uint32_t *) get_pos_addr(pos) = pos->packet_size;
+       move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+}
+
+static
+void trace_string(char *line, struct stream_pos *pos, size_t len)
+{
+       struct stream_pos dummy;
+       int attempt = 0;
+
+       printf_debug("read: %s\n", line);
+retry:
+       dummy_pos(pos, &dummy);
+       align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
+       move_pos(&dummy, len * CHAR_BIT);
+       if (pos_packet(&dummy)) {
+               /* TODO write content size */
+               pos_pad_packet(pos);
+               write_packet_header(pos, s_uuid);
+               write_packet_context(pos);
+               if (attempt++ == 1) {
+                       fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
+                               pos->packet_size / CHAR_BIT / 1024);
+                       return;
+               }
+               goto retry;
+       }
+
+       align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
+       memcpy(get_pos_addr(pos), line, len);
+       move_pos(pos, len * CHAR_BIT);
+}
+
+static
+void trace_text(FILE *input, int output)
+{
+       struct stream_pos pos;
+       ssize_t len;
+       char *line = NULL, *nl;
+       size_t linesize;
+
+       init_pos(&pos, output);
+
+       write_packet_header(&pos, s_uuid);
+       write_packet_context(&pos);
+       for (;;) {
+               len = getline(&line, &linesize, input);
+               if (len < 0)
+                       break;
+               nl = strrchr(line, '\n');
+               if (nl)
+                       *nl = '\0';
+               trace_string(line, &pos, nl - line + 1);
+       }
+       fini_pos(&pos);
+}
 
 int main(int argc, char **argv)
 {
-       char *base, *pos;
        int fd, ret;
-       off_t off;
-       uuid_t uuid;
 
+       ret = unlink("dummystream");
+       if (ret < 0) {
+               perror("unlink");
+               return -1;
+       }
        fd = open("dummystream", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
        if (fd < 0) {
                perror("open");
                return -1;
        }
-       off = posix_fallocate(fd, 0, FILE_LEN);
-       if (off < 0) {
-               printf("Error in fallocate\n");
-               return -1;
-       }
-#if 0
-       {
-               ssize_t len;
-
-               off = lseek(fd, FILE_LEN - 1, SEEK_CUR);
-               if (off < 0) {
-                       perror("lseek");
-                       return -1;
-               }
-               len = write(fd, "", 1);
-               if (len < 0) {
-                       perror("write");
-                       return -1;
-               }
-       }
-#endif
-       base = mmap(NULL, FILE_LEN, PROT_READ|PROT_WRITE,
-               MAP_SHARED, fd, 0);
-       if (!base) {
-               perror("mmap");
-               return -1;
-       }
-       pos = base;
-
-       /* magic */
-       *(uint32_t *) pos = 0xC1FC1FC1;
-       pos += sizeof(uint32_t);
 
-       /* trace_uuid */
-       ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", uuid);
+       ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid);
        if (ret) {
                printf("uuid parse error\n");
+               close(fd);
                return -1;
        }
-       memcpy(pos, uuid, 16);
-       pos += 16;
-
-       /* stream_id */
-       *(uint32_t *) pos = 0;
-        pos += sizeof(uint32_t);
 
-       ret = munmap(base, FILE_LEN);
-       if (ret) {
-               perror("munmap");
-               return -1;
-       }
+       trace_text(stdin, fd);
        close(fd);
        return 0;
 }
-
-
This page took 0.026068 seconds and 4 git commands to generate.