Test and fix packet-based metadata parsing
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 17 May 2011 12:55:49 +0000 (08:55 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 17 May 2011 12:55:49 +0000 (08:55 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf/ctf.c
formats/ctf/metadata/ctf-visitor-generate-io-struct.c
include/babeltrace/ctf/metadata.h
tests/ctf-traces/succeed/packet-based-metadata/datastream [new file with mode: 0644]
tests/ctf-traces/succeed/packet-based-metadata/metadata [new file with mode: 0644]

index b0d3ebcfece2cd897c0e209104e5f120e281214f..7c354bf0a40710a9eed836226d6bb273f94b2148 100644 (file)
 #define WRITE_PACKET_LEN       (getpagesize() * 8 * CHAR_BIT)
 #define UUID_LEN 16    /* uuid by value len */
 
+#ifndef min
+#define min(a, b)      (((a) < (b)) ? (a) : (b))
+#endif
+
 extern int yydebug;
 
 struct trace_descriptor *ctf_open_trace(const char *path, int flags);
@@ -380,7 +384,7 @@ int packet_metadata(struct ctf_trace *td, FILE *fp)
        int ret = 0;
 
        len = fread(&magic, sizeof(magic), 1, fp);
-       if (len != sizeof(magic)) {
+       if (len != 1) {
                goto end;
        }
        if (magic == TSDL_MAGIC) {
@@ -391,6 +395,7 @@ int packet_metadata(struct ctf_trace *td, FILE *fp)
                td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
                                        LITTLE_ENDIAN : BIG_ENDIAN;
        }
+       CTF_TRACE_SET_FIELD(td, byte_order);
 end:
        rewind(fp);
        return ret;
@@ -401,12 +406,12 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
                                        FILE *out)
 {
        struct metadata_packet_header header;
-       size_t readlen, writelen;
+       size_t readlen, writelen, toread;
        char buf[4096];
        int ret = 0;
 
        readlen = fread(&header, sizeof(header), 1, in);
-       if (readlen < sizeof(header))
+       if (readlen < 1)
                return -EINVAL;
 
        if (td->byte_order != BYTE_ORDER) {
@@ -440,12 +445,15 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
                        return -EINVAL;
        }
 
-       while (!feof(in)) {
-               readlen = fread(buf, sizeof(char), sizeof(buf), in);
+       toread = header.content_size / CHAR_BIT;
+
+       for (;;) {
+               readlen = fread(buf, sizeof(char), min(sizeof(buf), toread), in);
                if (ferror(in)) {
                        ret = -EINVAL;
                        break;
                }
+               printf("read %s\n", buf);
                writelen = fwrite(buf, sizeof(char), readlen, out);
                if (writelen < readlen) {
                        ret = -EIO;
@@ -455,6 +463,15 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
                        ret = -EINVAL;
                        break;
                }
+               toread -= readlen;
+               if (!toread) {
+                       ret = -EOF;
+                       break;
+               }
+               if (feof(in)) {
+                       ret = -EINVAL;
+                       break;
+               }
        }
        return ret;
 }
@@ -862,7 +879,6 @@ int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
 readdir_error:
        free(dirent);
 error_metadata:
-       g_ptr_array_free(td->streams, TRUE);
        close(td->dirfd);
 error_dirfd:
        closedir(td->dir);
index 9a5cda096f321509527492b24d0510dc613dda4b..9dffdcbf50a0cf5c938035ebff6f3746df21e3e8 100644 (file)
@@ -1888,7 +1888,8 @@ int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru
                        CTF_TRACE_SET_FIELD(trace, minor);
                } else if (!strcmp(left, "uuid")) {
                        uuid_t uuid;
-                       ret = get_unary_uuid(&node->u.ctf_expression.right, &trace->uuid);
+
+                       ret = get_unary_uuid(&node->u.ctf_expression.right, &uuid);
                        if (ret) {
                                fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
                                ret = -EINVAL;
@@ -1899,22 +1900,27 @@ int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru
                                fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
                                ret = -EPERM;
                                goto error;
+                       } else {
+                               memcpy(trace->uuid, uuid, sizeof(uuid));
                        }
                        CTF_TRACE_SET_FIELD(trace, uuid);
                } else if (!strcmp(left, "byte_order")) {
                        struct ctf_node *right;
                        int byte_order;
 
-                       if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
-                               fprintf(fd, "[error] %s: endianness already declared in trace declaration\n", __func__);
-                               ret = -EPERM;
-                               goto error;
-                       }
                        right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
                        byte_order = get_trace_byte_order(fd, depth, right);
                        if (byte_order < 0)
                                return -EINVAL;
-                       trace->byte_order = byte_order;
+
+                       if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
+                               && byte_order != trace->byte_order) {
+                               fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
+                               ret = -EPERM;
+                               goto error;
+                       } else {
+                               trace->byte_order = byte_order;
+                       }
                        CTF_TRACE_SET_FIELD(trace, byte_order);
                } else if (!strcmp(left, "packet.header")) {
                        struct declaration *declaration;
@@ -2081,7 +2087,6 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
 
        printf_verbose("CTF visitor: metadata construction... ");
        trace->root_declaration_scope = new_declaration_scope(NULL);
-       trace->streams = g_ptr_array_new();
        trace->byte_order = byte_order;
 
        switch (node->type) {
@@ -2091,26 +2096,27 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
                        ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
                        if (ret) {
                                fprintf(fd, "[error] %s: root declaration error\n", __func__);
-                               return ret;
+                               goto error;
                        }
                }
                cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
                        ret = ctf_trace_visit(fd, depth + 1, iter, trace);
                        if (ret) {
                                fprintf(fd, "[error] %s: trace declaration error\n", __func__);
-                               return ret;
+                               goto error;
                        }
                }
                if (!trace->streams) {
                        fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
-                       return -EINVAL;
+                       ret = -EINVAL;
+                       goto error;
                }
                cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
                        ret = ctf_stream_visit(fd, depth + 1, iter,
                                               trace->root_declaration_scope, trace);
                        if (ret) {
                                fprintf(fd, "[error] %s: stream declaration error\n", __func__);
-                               return ret;
+                               goto error;
                        }
                }
                cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
@@ -2118,7 +2124,7 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
                                              trace->root_declaration_scope, trace);
                        if (ret) {
                                fprintf(fd, "[error] %s: event declaration error\n", __func__);
-                               return ret;
+                               goto error;
                        }
                }
                break;
@@ -2126,8 +2132,13 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
        default:
                fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
                        (int) node->type);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto error;
        }
        printf_verbose("done.\n");
        return ret;
+
+error:
+       free_declaration_scope(trace->root_declaration_scope);
+       return ret;
 }
index 6ebd0c7c298d16cc7f188be56a8273a06c9a551f..c197624793f932a9a3fb4495ec648c90a1760385 100644 (file)
@@ -188,6 +188,6 @@ struct metadata_packet_header {
        uint8_t  compression_scheme;    /* 0 if unused */
        uint8_t  encryption_scheme;     /* 0 if unused */
        uint8_t  checksum_scheme;       /* 0 if unused */
-};
+} __attribute__((packed));
 
 #endif /* _BABELTRACE_CTF_METADATA_H */
diff --git a/tests/ctf-traces/succeed/packet-based-metadata/datastream b/tests/ctf-traces/succeed/packet-based-metadata/datastream
new file mode 100644 (file)
index 0000000..13bff2e
Binary files /dev/null and b/tests/ctf-traces/succeed/packet-based-metadata/datastream differ
diff --git a/tests/ctf-traces/succeed/packet-based-metadata/metadata b/tests/ctf-traces/succeed/packet-based-metadata/metadata
new file mode 100644 (file)
index 0000000..fa21914
Binary files /dev/null and b/tests/ctf-traces/succeed/packet-based-metadata/metadata differ
This page took 0.047785 seconds and 4 git commands to generate.