From dc48ecad637fc7fb8479da563ef2dfd3948cee73 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 5 May 2011 12:52:41 -0400 Subject: [PATCH] Fix integer/float/string read: keep value in definition structure Makes magic number read works. Signed-off-by: Mathieu Desnoyers --- formats/ctf/ctf.c | 83 +++++++++++-------- .../ctf-test/succeed/ctf-embedded-2.txt | 1 + .../metadata/ctf-visitor-generate-io-struct.c | 5 ++ include/babeltrace/ctf/types.h | 2 +- include/babeltrace/format.h | 5 ++ types/float.c | 14 ++-- types/integer.c | 2 + types/string.c | 15 ++-- 8 files changed, 72 insertions(+), 55 deletions(-) diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 515e4783..54aaed7e 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -59,6 +59,8 @@ static struct format ctf_format = { .int_write = ctf_int_write, .double_read = ctf_double_read, .double_write = ctf_double_write, + .ldouble_read = ctf_ldouble_read, + .ldouble_write = ctf_ldouble_write, .float_copy = ctf_float_copy, .string_copy = ctf_string_copy, .string_read = ctf_string_read, @@ -209,8 +211,8 @@ int create_stream_packet_index(struct trace_descriptor *td, /* map new base. Need mapping length from header. */ pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN, PROT_READ, MAP_PRIVATE, pos->fd, pos->mmap_offset); - pos->content_size = 0; /* Unknown at this point */ - pos->packet_size = 0; /* Unknown at this point */ + pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ + pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ pos->offset = 0; /* Position of the packet header */ /* read and check header, set stream id (and check) */ @@ -299,38 +301,45 @@ int create_stream_packet_index(struct trace_descriptor *td, } first_packet = 0; - /* Read packet context */ - stream->packet_context->p.declaration->copy(NULL, NULL, - pos, &ctf_format, &stream->packet_context->p); - - /* read content size from header */ - len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size")); - if (len_index >= 0) { - struct definition_integer *defint; - struct field *field; - - field = struct_definition_get_field_from_index(stream->packet_context, len_index); - assert(field->definition->declaration->id == CTF_TYPE_INTEGER); - defint = container_of(field->definition, struct definition_integer, p); - assert(defint->declaration->signedness == FALSE); - pos->content_size = defint->value._unsigned; + if (stream->packet_context) { + /* Read packet context */ + stream->packet_context->p.declaration->copy(NULL, NULL, + pos, &ctf_format, &stream->packet_context->p); + + /* read content size from header */ + len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size")); + if (len_index >= 0) { + struct definition_integer *defint; + struct field *field; + + field = struct_definition_get_field_from_index(stream->packet_context, len_index); + assert(field->definition->declaration->id == CTF_TYPE_INTEGER); + defint = container_of(field->definition, struct definition_integer, p); + assert(defint->declaration->signedness == FALSE); + pos->content_size = defint->value._unsigned; + } else { + /* Use file size for packet size */ + pos->content_size = filestats.st_size * CHAR_BIT; + } + + /* read packet size from header */ + len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size")); + if (len_index >= 0) { + struct definition_integer *defint; + struct field *field; + + field = struct_definition_get_field_from_index(stream->packet_context, len_index); + assert(field->definition->declaration->id == CTF_TYPE_INTEGER); + defint = container_of(field->definition, struct definition_integer, p); + assert(defint->declaration->signedness == FALSE); + pos->packet_size = defint->value._unsigned; + } else { + /* Use content size if non-zero, else file size */ + pos->packet_size = pos->content_size ? : filestats.st_size * CHAR_BIT; + } } else { /* Use file size for packet size */ pos->content_size = filestats.st_size * CHAR_BIT; - } - - /* read packet size from header */ - len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size")); - if (len_index >= 0) { - struct definition_integer *defint; - struct field *field; - - field = struct_definition_get_field_from_index(stream->packet_context, len_index); - assert(field->definition->declaration->id == CTF_TYPE_INTEGER); - defint = container_of(field->definition, struct definition_integer, p); - assert(defint->declaration->signedness == FALSE); - pos->packet_size = defint->value._unsigned; - } else { /* Use content size if non-zero, else file size */ pos->packet_size = pos->content_size ? : filestats.st_size * CHAR_BIT; } @@ -392,14 +401,14 @@ int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags /* Open trace directory */ td->ctf_trace.dir = opendir(path); if (!td->ctf_trace.dir) { - fprintf(stdout, "Unable to open trace directory.\n"); + fprintf(stdout, "[error] Unable to open trace directory.\n"); ret = -ENOENT; goto error; } td->ctf_trace.dirfd = open(path, 0); if (td->ctf_trace.dirfd < 0) { - fprintf(stdout, "Unable to open trace directory file descriptor.\n"); + fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n"); ret = -ENOENT; goto error_dirfd; } @@ -429,7 +438,7 @@ int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags for (;;) { ret = readdir_r(td->ctf_trace.dir, dirent, &diriter); if (ret) { - fprintf(stdout, "Readdir error.\n"); + fprintf(stdout, "[error] Readdir error.\n"); goto readdir_error; } if (!diriter) @@ -438,7 +447,11 @@ int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags || !strcmp(diriter->d_name, "..") || !strcmp(diriter->d_name, "metadata")) continue; - /* TODO: open file stream */ + ret = ctf_open_file_stream_read(td, diriter->d_name, flags); + if (ret) { + fprintf(stdout, "[error] Open file stream error.\n"); + goto readdir_error; + } } free(dirent); diff --git a/formats/ctf/metadata/ctf-test/succeed/ctf-embedded-2.txt b/formats/ctf/metadata/ctf-test/succeed/ctf-embedded-2.txt index db436ae3..b9340158 100644 --- a/formats/ctf/metadata/ctf-test/succeed/ctf-embedded-2.txt +++ b/formats/ctf/metadata/ctf-test/succeed/ctf-embedded-2.txt @@ -11,6 +11,7 @@ trace { major = 0; minor = 1; uuid = "2a6422d0-6cee-11e0-8c08-cb07d7b3a564"; + byte_order = be; packet.header := struct { uint32_t magic; uint8_t trace_uuid[16]; diff --git a/formats/ctf/metadata/ctf-visitor-generate-io-struct.c b/formats/ctf/metadata/ctf-visitor-generate-io-struct.c index 1981832e..f5430711 100644 --- a/formats/ctf/metadata/ctf-visitor-generate-io-struct.c +++ b/formats/ctf/metadata/ctf-visitor-generate-io-struct.c @@ -1859,6 +1859,11 @@ int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__); goto error; } + if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { + ret = -EPERM; + fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__); + goto error; + } parent_def_scope = NULL; if (trace->packet_header_decl) { diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 91c3303c..8c98ff2c 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -133,7 +133,7 @@ void init_pos(struct stream_pos *pos, int fd) static inline void move_pos(struct stream_pos *pos, size_t offset) { - if (pos->fd >= 0 && pos->offset + offset >= pos->content_size) + if (pos->fd >= 0 && (pos->offset + offset >= pos->content_size)) move_pos_slow(pos, offset); else pos->offset += offset; diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index cddc41a1..151bd65a 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -48,6 +48,11 @@ struct format { void (*double_write)(struct stream_pos *pos, const struct declaration_float *float_declaration, double v); + long double (*ldouble_read)(struct stream_pos *pos, + const struct declaration_float *float_declaration); + void (*ldouble_write)(struct stream_pos *pos, + const struct declaration_float *float_declaration, + long double v); void (*string_copy)(struct stream_pos *dest, struct stream_pos *src, const struct declaration_string *string_declaration); diff --git a/types/float.c b/types/float.c index 3b2523e2..9113bb8c 100644 --- a/types/float.c +++ b/types/float.c @@ -35,16 +35,12 @@ void float_copy(struct stream_pos *destp, struct definition_float *_float = container_of(definition, struct definition_float, p); struct declaration_float *float_declaration = _float->declaration; + long double v; - if (fsrc->float_copy == fdest->float_copy) { - fsrc->float_copy(destp, srcp, float_declaration); - } else { - double v; - - v = fsrc->double_read(srcp, float_declaration); - if (fdest) - fdest->double_write(destp, float_declaration, v); - } + v = fsrc->ldouble_read(srcp, float_declaration); + _float->value = v; + if (fdest) + fdest->ldouble_write(destp, float_declaration, v); } static diff --git a/types/integer.c b/types/integer.c index 16cd903d..254dcbcb 100644 --- a/types/integer.c +++ b/types/integer.c @@ -40,12 +40,14 @@ void integer_copy(struct stream_pos *dest, const struct format *fdest, uint64_t v; v = fsrc->uint_read(src, integer_declaration); + integer->value._unsigned = v; if (fdest) fdest->uint_write(dest, integer_declaration, v); } else { int64_t v; v = fsrc->int_read(src, integer_declaration); + integer->value._signed = v; if (fdest) fdest->int_write(dest, integer_declaration, v); } diff --git a/types/string.c b/types/string.c index 796f05cd..6e5c6f47 100644 --- a/types/string.c +++ b/types/string.c @@ -35,16 +35,11 @@ void string_copy(struct stream_pos *dest, const struct format *fdest, container_of(definition, struct definition_string, p); struct declaration_string *string_declaration = string->declaration; - if (fdest && (fsrc->string_copy == fdest->string_copy)) { - fsrc->string_copy(dest, src, string_declaration); - } else { - char *tmp = NULL; - - fsrc->string_read(&tmp, src, string_declaration); - if (fdest) - fdest->string_write(dest, tmp, string_declaration); - fsrc->string_free_temp(tmp); - } + g_free(string->value); + string->value = NULL; + fsrc->string_read(&string->value, src, string_declaration); + if (fdest) + fdest->string_write(dest, string->value, string_declaration); } static -- 2.34.1