text conversion works
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 7 May 2011 01:29:52 +0000 (21:29 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 7 May 2011 01:29:52 +0000 (21:29 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
converter/Makefile.am
converter/babeltrace-lib.c
converter/babeltrace.c
formats/ctf/ctf.c
formats/ctf/types/string.c
include/babeltrace/babeltrace.h
include/babeltrace/ctf/types.h

index 3aa7fc3d5a4efa24b11700929c88375056a2cbd4..4d573616a278bb9d37af831eee041403f4825ef5 100644 (file)
@@ -2,25 +2,19 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include -lpopt
 
 bin_PROGRAMS = babeltrace
 
-#lib_LTLIBRARIES = libbabeltrace.la
-#
-#libbabeltrace_la_SOURCES = \
-#      babeltrace-lib.c
-#
-#libbabeltrace_la_LIBADD = \
-#      $(top_builddir)/types/libbabeltrace_types.la \
-#      $(top_builddir)/formats/libbabeltrace_registry.la \
-#      $(top_builddir)/formats/ctf/libctf.la \
-#      $(top_builddir)/formats/ctf-text/libctf-text.la
+lib_LTLIBRARIES = libbabeltrace.la
 
-babeltrace_SOURCES = \
-       babeltrace.c
-
-#babeltrace_LDADD = \
-#      libbabeltrace.la
+libbabeltrace_la_SOURCES = \
+       babeltrace-lib.c
 
-babeltrace_LDADD = \
+libbabeltrace_la_LIBADD = \
        $(top_builddir)/types/libbabeltrace_types.la \
-       $(top_builddir)/formats/libbabeltrace_registry.la \
        $(top_builddir)/formats/ctf/libctf.la \
        $(top_builddir)/formats/ctf-text/libctf-text.la
+
+babeltrace_SOURCES = \
+       babeltrace.c
+
+babeltrace_LDADD = \
+       libbabeltrace.la \
+       $(top_builddir)/formats/libbabeltrace_registry.la
index 3c51d840830aba40a3c379dadd3de6fe789498cb..5be268ccf4371b22e94a2e28e3d1898ecc009835 100644 (file)
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <stdio.h>
+#include <inttypes.h>
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/format.h>
 #include <babeltrace/ctf/types.h>
 #include <babeltrace/ctf-text/types.h>
 
 static
-int print_event(struct format *fmt_write, struct ctf_text_stream_pos *sout,
-               struct format *fmt_read, struct ctf_file_stream *sin)
+int convert_event(struct ctf_text_stream_pos *sout,
+                 struct ctf_file_stream *sin)
 {
        struct ctf_stream *stream_class = sin->stream;
        struct ctf_event *event_class;
        uint64_t id = 0;
        int len_index;
 
+       if (sin->pos.offset == -EOF)
+               return -EOF;
+
        /* Read and print event header */
        if (stream_class->event_header) {
-               fmt_read->struct_read(&sin->pos.parent, stream_class->event_header);
+               generic_rw(&sin->pos.parent, &stream_class->event_header->p);
 
                /* lookup event id */
-               len_index = struct_declaration_lookup_field_index(td->packet_header->declaration,
+               len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
                                g_quark_from_static_string("id"));
                if (len_index >= 0) {
                        struct definition_integer *defint;
@@ -52,13 +56,13 @@ int print_event(struct format *fmt_write, struct ctf_text_stream_pos *sout,
                        id = defint->value._unsigned;   /* set id */
                }
 
-               fmt_write->struct_write(&sout->parent, stream_class->event_header);
+               generic_rw(&sout->parent, &stream_class->event_header->p);
        }
 
        /* Read and print stream-declared event context */
        if (stream_class->event_context) {
-               fmt_read->struct_read(&sin->pos.parent, stream_class->event_context);
-               fmt_write->struct_write(&sout->parent, stream_class->event_context);
+               generic_rw(&sin->pos.parent, &stream_class->event_context->p);
+               generic_rw(&sout->parent, &stream_class->event_context->p);
        }
 
        if (id >= stream_class->events_by_id->len) {
@@ -73,29 +77,29 @@ int print_event(struct format *fmt_write, struct ctf_text_stream_pos *sout,
 
        /* Read and print event-declared event context */
        if (event_class->context) {
-               fmt_read->struct_read(&sin->pos.parent, event_class->context);
-               fmt_write->struct_write(&sout->parent, event_class->context);
+               generic_rw(&sin->pos.parent, &event_class->context->p);
+               generic_rw(&sout->parent, &event_class->context->p);
        }
 
        /* Read and print event payload */
        if (event_class->fields) {
-               fmt_read->struct_read(&sin->pos.parent, event_class->fields);
-               fmt_write->struct_write(&sout->parent, event_class->fields);
+               generic_rw(&sin->pos.parent, &event_class->fields->p);
+               generic_rw(&sout->parent, &event_class->fields->p);
        }
 
        return 0;
 }
 
 static
-int print_stream(struct format *fmt_write, struct ctf_text_stream_pos *sout,
-                struct format *fmt_read, struct ctf_file_stream *sin)
+int convert_stream(struct ctf_text_stream_pos *sout,
+                  struct ctf_file_stream *sin)
 {
        int ret;
 
        /* For each event, print header, context, payload */
        /* TODO: order events by timestamps across streams */
        for (;;) {
-               ret = print_event(fmt_write, sout, fmt_read, sin);
+               ret = convert_event(sout, sin);
                if (ret == -EOF)
                        break;
                else if (ret) {
@@ -110,13 +114,12 @@ error:
        return ret;
 }
 
-int print_trace(struct format *fmt_write,
-               struct trace_descriptor *td_write,
-               struct format *fmt_read,
-               struct trace_descriptor *td_read)
+int convert_trace(struct trace_descriptor *td_write,
+                 struct trace_descriptor *td_read)
 {
        struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
-       struct ctf_text_stream_pos *sout = container_of(td_write, struct ctf_text_stream_pos, parent);
+       struct ctf_text_stream_pos *sout =
+               container_of(td_write, struct ctf_text_stream_pos, trace_descriptor);
        int stream_id, filenr;
        int ret;
 
@@ -128,7 +131,7 @@ int print_trace(struct format *fmt_write,
                        continue;
                for (filenr = 0; filenr < stream->files->len; filenr++) {
                        struct ctf_file_stream *file_stream = g_ptr_array_index(stream->files, filenr);
-                       ret = print_stream(fmt_write, sout, fmt_read, file_stream);
+                       ret = convert_stream(sout, file_stream);
                        if (ret) {
                                fprintf(stdout, "[error] Printing stream %d failed.\n", stream_id);
                                goto error;
index 0ec30162a0dcc16d56216a38d93ba08c3ae62ede..70e43ccf7af67f197d3fc36057a82d679f6d6e9e 100644 (file)
@@ -206,13 +206,13 @@ int main(int argc, char **argv)
                        opt_output_path);
                goto error_td_write;
        }
-#if 0
-       ret = print_trace(fmt_write, td_write, fmt_read, td_read);
+
+       ret = convert_trace(td_write, td_read);
        if (ret) {
                fprintf(stdout, "Error printing trace.\n\n");
                goto error_copy_trace;
        }
-#endif //0
+
        fmt_write->close_trace(td_write);
        fmt_read->close_trace(td_read);
        exit(EXIT_SUCCESS);
index a63eb60b496c1ca85c9b2621ec7a3985d9c4635d..e375039b84c1bbf132add88d1dd9d29afc84e043 100644 (file)
@@ -107,7 +107,7 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
                pos->flags = MAP_SHARED;
                pos->parent.rw_table = write_dispatch_table;
                if (fd >= 0)
-                       ctf_move_pos_slow(pos, 0);      /* position for write */
+                       ctf_move_pos_slow(pos, 0, SEEK_SET);    /* position for write */
                break;
        default:
                assert(0);
@@ -132,12 +132,14 @@ void ctf_fini_pos(struct ctf_stream_pos *pos)
        (void) g_array_free(pos->packet_index, TRUE);
 }
 
-void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset)
+void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
 {
        int ret;
        off_t off;
        struct packet_index *index;
 
+       /* Only allow random seek in read mode */
+       assert(pos->prot != PROT_WRITE || whence == SEEK_CUR);
 
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
@@ -150,6 +152,7 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset)
                                strerror(errno));
                        assert(0);
                }
+               pos->base = NULL;
        }
 
        /*
@@ -170,16 +173,25 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset)
                pos->packet_size = WRITE_PACKET_LEN;
                off = posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT);
                assert(off >= 0);
+               pos->offset = 0;
        } else {
-               /* The reader will expect us to skip padding */
-               assert(pos->offset + offset == pos->content_size);
-
-               /*
-                * Don't increment for initial stream move (only condition where
-                * pos->offset can be 0).
-                */
-               if (pos->offset)
+               switch (whence) {
+               case SEEK_CUR:
+                       /* The reader will expect us to skip padding */
+                       assert(pos->offset + offset == pos->content_size);
                        ++pos->cur_index;
+                       break;
+               case SEEK_SET:
+                       assert(offset == 0);    /* only seek supported for now */
+                       pos->cur_index = 0;
+                       break;
+               default:
+                       assert(0);
+               }
+               if (pos->cur_index >= pos->packet_index->len) {
+                       pos->offset = -EOF;
+                       return;
+               }
                index = &g_array_index(pos->packet_index, struct packet_index,
                                       pos->cur_index);
                pos->mmap_offset = index->offset;
@@ -187,11 +199,16 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset)
                /* Lookup context/packet size in index */
                pos->content_size = index->content_size;
                pos->packet_size = index->packet_size;
+               pos->offset = index->data_offset;
        }
        /* map new base. Need mapping length from header. */
        pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
                         pos->flags, pos->fd, pos->mmap_offset);
-       pos->offset = 0;
+       if (pos->base == MAP_FAILED) {
+               fprintf(stdout, "[error] mmap error %s.\n",
+                       strerror(errno));
+               assert(0);
+       }
 }
 
 /*
@@ -431,6 +448,8 @@ int create_stream_packet_index(struct ctf_trace *td,
                        /* Use content size if non-zero, else file size */
                        packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
                }
+               /* Save position after header and context */
+               packet_index.data_offset = pos->offset;
 
                /* add index to packet array */
                g_array_append_val(file_stream->pos.packet_index, packet_index);
@@ -438,6 +457,9 @@ int create_stream_packet_index(struct ctf_trace *td,
                pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
        }
 
+       /* Move pos back to beginning of file */
+       ctf_move_pos_slow(pos, 0, SEEK_SET);    /* position for write */
+
        return 0;
 }
 
index b648b817aecb4944e462e29c6726a77c918c12cb..6a478abfd202072f3a022b068ff49db40c607d61 100644 (file)
@@ -16,6 +16,7 @@
  * all copies or substantial portions of the Software.
  */
 
+#include <babeltrace/babeltrace.h>
 #include <babeltrace/ctf/types.h>
 #include <limits.h>            /* C99 limits */
 #include <string.h>
@@ -38,9 +39,10 @@ void ctf_string_read(struct stream_pos *ppos, struct definition *definition)
                        g_realloc(string_definition->value, len);
                string_definition->alloc_len = len;
        }
+       printf_debug("CTF string read %s\n", srcaddr);
        memcpy(string_definition->value, srcaddr, len);
        string_definition->len = len;
-       ctf_move_pos(pos, len);
+       ctf_move_pos(pos, len * CHAR_BIT);
 }
 
 void ctf_string_write(struct stream_pos *ppos,
@@ -62,5 +64,5 @@ void ctf_string_write(struct stream_pos *ppos,
        destaddr = ctf_get_pos_addr(pos);
        memcpy(destaddr, string_definition->value, len);
 end:
-       ctf_move_pos(pos, len);
+       ctf_move_pos(pos, len * CHAR_BIT);
 }
index 8e410b59c23635eb995c8f5281c1f44ae9f69f87..fe23fefd7f2730005231ced58fd43a7b59308b42 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef _BABELTRACE_H
 #define _BABELTRACE_H
 
+#include <stdio.h>
+
 #define BABELTRACE_VERSION_MAJOR       0
 #define BABELTRACE_VERSION_MINOR       1
 
@@ -20,6 +22,7 @@ extern int babeltrace_verbose, babeltrace_debug;
 
 struct trace_descriptor;
 
-int copy_trace(struct trace_descriptor *td_write, struct trace_descriptor *td_read);
+int convert_trace(struct trace_descriptor *td_write,
+                 struct trace_descriptor *td_read);
 
 #endif
index 2396c4ff5b6f2261f5e8b7205be98944ad348c74..8018b5b363d8261665653fbe88ece38d37208c82 100644 (file)
 #include <unistd.h>
 #include <glib.h>
 
+
 struct packet_index {
        off_t offset;           /* offset of the packet in the file, in bytes */
+       off_t data_offset;      /* offset of data within the packet */
        size_t packet_size;     /* packet size, in bits */
        size_t content_size;    /* content size, in bits */
 };
@@ -51,7 +53,7 @@ struct ctf_stream_pos {
        size_t content_size;    /* current content size, in bits */
        uint32_t *content_size_loc; /* pointer to current content size */
        char *base;             /* mmap base address */
-       size_t offset;          /* offset from base, in bits */
+       ssize_t offset;         /* offset from base, in bits. -EOF for end of file. */
        size_t cur_index;       /* current index in packet index */
 
        int dummy;              /* dummy position, for length calculation */
@@ -76,7 +78,7 @@ void ctf_variant_rw(struct stream_pos *pos, struct definition *definition);
 void ctf_array_rw(struct stream_pos *pos, struct definition *definition);
 void ctf_sequence_rw(struct stream_pos *pos, struct definition *definition);
 
-void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset);
+void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence);
 
 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags);
 void ctf_fini_pos(struct ctf_stream_pos *pos);
@@ -94,7 +96,7 @@ void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset)
                      && (pos->offset + bit_offset >= pos->content_size))
                    || ((pos->prot == PROT_WRITE)
                      && (pos->offset + bit_offset >= pos->packet_size))) {
-                       ctf_move_pos_slow(pos, bit_offset);
+                       ctf_move_pos_slow(pos, bit_offset, SEEK_CUR);
                        return;
                }
        }
This page took 0.045418 seconds and 4 git commands to generate.