X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=formats%2Fctf%2Fmetadata%2Fctf-visitor-generate-io-struct.c;h=70fd5df200c388be088871ce37d97c594a8a2f37;hp=9dffdcbf50a0cf5c938035ebff6f3746df21e3e8;hb=b3ab6665619a8d9aecbdbaeeb5356f96e22dcfa0;hpb=a0fe7d97d4a551665af7bd91f7571dc785a75951 diff --git a/formats/ctf/metadata/ctf-visitor-generate-io-struct.c b/formats/ctf/metadata/ctf-visitor-generate-io-struct.c index 9dffdcbf..70fd5df2 100644 --- a/formats/ctf/metadata/ctf-visitor-generate-io-struct.c +++ b/formats/ctf/metadata/ctf-visitor-generate-io-struct.c @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include @@ -104,6 +104,23 @@ int get_unary_unsigned(struct cds_list_head *head, uint64_t *value) return 0; } +static +int get_unary_signed(struct cds_list_head *head, int64_t *value) +{ + struct ctf_node *node; + int i = 0; + + cds_list_for_each_entry(node, head, siblings) { + assert(node->type == NODE_UNARY_EXPRESSION); + assert(node->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT); + assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN); + assert(i == 0); + *value = node->u.unary_expression.u.signed_constant; + i++; + } + return 0; +} + static int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid) { @@ -119,7 +136,7 @@ int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid) assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN); assert(i == 0); src_string = node->u.unary_expression.u.string; - ret = uuid_parse(node->u.unary_expression.u.string, *uuid); + ret = uuid_parse(src_string, *uuid); } return ret; } @@ -325,7 +342,7 @@ struct declaration *ctf_type_declarator_visit(FILE *fd, int depth, */ integer_declaration = integer_declaration_new(integer_declaration->len, integer_declaration->byte_order, integer_declaration->signedness, - integer_declaration->p.alignment, 16); + integer_declaration->p.alignment, 16, integer_declaration->encoding); nested_declaration = &integer_declaration->p; } } @@ -350,6 +367,11 @@ struct declaration *ctf_type_declarator_visit(FILE *fd, int depth, /* TYPEDEC_NESTED */ + if (!nested_declaration) { + fprintf(fd, "[error] %s: nested type is unknown.\n", __func__); + return NULL; + } + /* create array/sequence, pass nested_declaration as child. */ if (cds_list_empty(&node_type_declarator->u.type_declarator.u.nested.length)) { fprintf(fd, "[error] %s: expecting length field reference or value.\n", __func__); @@ -426,6 +448,13 @@ int ctf_struct_type_declarators_visit(FILE *fd, int depth, fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__); return -EINVAL; } + + /* Check if field with same name already exists */ + if (struct_declaration_lookup_field_index(struct_declaration, field_name) >= 0) { + fprintf(fd, "[error] %s: duplicate field %s in struct\n", __func__, g_quark_to_string(field_name)); + return -EINVAL; + } + struct_declaration_add_field(struct_declaration, g_quark_to_string(field_name), field_declaration); @@ -456,6 +485,13 @@ int ctf_variant_type_declarators_visit(FILE *fd, int depth, fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__); return -EINVAL; } + + if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration, field_name) != NULL) { + fprintf(fd, "[error] %s: duplicate field %s in variant\n", __func__, g_quark_to_string(field_name)); + return -EINVAL; + } + + untagged_variant_declaration_add_field(untagged_variant_declaration, g_quark_to_string(field_name), field_declaration); @@ -565,7 +601,9 @@ int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope, return 0; error: - type_declaration->declaration_free(type_declaration); + if (type_declaration) { + type_declaration->declaration_free(type_declaration); + } return err; } @@ -705,7 +743,7 @@ struct declaration *ctf_declaration_struct_visit(FILE *fd, ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter, struct_declaration, trace); if (ret) - goto error; + goto error_free_declaration; } if (name) { ret = register_struct_declaration(g_quark_from_string(name), @@ -715,8 +753,9 @@ struct declaration *ctf_declaration_struct_visit(FILE *fd, } return &struct_declaration->p; } -error: +error_free_declaration: struct_declaration->p.declaration_free(&struct_declaration->p); +error: return NULL; } @@ -1071,11 +1110,12 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, struct ctf_trace *trace) { struct ctf_node *expression; - uint64_t alignment, size; + uint64_t alignment = 1, size = 0; int byte_order = trace->byte_order; int signedness = 0; int has_alignment = 0, has_size = 0; int base = 0; + enum ctf_string_encoding encoding = CTF_STRING_NONE; struct declaration_integer *integer_declaration; cds_list_for_each_entry(expression, expressions, siblings) { @@ -1164,10 +1204,56 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, __func__); return NULL; } + } else if (!strcmp(left->u.unary_expression.u.string, "encoding")) { + char *s_right; + + if (right->u.unary_expression.type != UNARY_STRING) { + fprintf(fd, "[error] %s: encoding: expecting unary string\n", + __func__); + return NULL; + } + s_right = concatenate_unary_strings(&expression->u.ctf_expression.right); + if (!s_right) { + fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__); + g_free(s_right); + return NULL; + } + if (!strcmp(s_right, "UTF8") + || !strcmp(s_right, "utf8") + || !strcmp(s_right, "utf-8") + || !strcmp(s_right, "UTF-8")) + encoding = CTF_STRING_UTF8; + else if (!strcmp(s_right, "ASCII") + || !strcmp(s_right, "ascii")) + encoding = CTF_STRING_ASCII; + else if (!strcmp(s_right, "none")) + encoding = CTF_STRING_NONE; + else { + fprintf(fd, "[error] %s: unknown string encoding \"%s\"\n", __func__, s_right); + g_free(s_right); + return NULL; + } + g_free(s_right); + } else if (!strcmp(left->u.unary_expression.u.string, "map")) { + char *s_right; + + if (right->u.unary_expression.type != UNARY_STRING) { + fprintf(fd, "[error] %s: map: expecting identifier\n", + __func__); + return NULL; + } + s_right = concatenate_unary_strings(&expression->u.ctf_expression.right); + if (!s_right) { + fprintf(fd, "[error] %s: unexpected unary expression for integer map\n", __func__); + g_free(s_right); + return NULL; + } + /* TODO: lookup */ + } else { - fprintf(fd, "[error] %s: unknown attribute name %s\n", + fprintf(fd, "[warning] %s: unknown attribute name %s\n", __func__, left->u.unary_expression.u.string); - return NULL; + /* Fall-through after warning */ } } if (!has_size) { @@ -1184,7 +1270,8 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, } } integer_declaration = integer_declaration_new(size, - byte_order, signedness, alignment, base); + byte_order, signedness, alignment, + base, encoding); return &integer_declaration->p; } @@ -1194,7 +1281,8 @@ struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth, struct ctf_trace *trace) { struct ctf_node *expression; - uint64_t alignment, exp_dig, mant_dig, byte_order = trace->byte_order; + uint64_t alignment = 1, exp_dig = 0, mant_dig = 0, + byte_order = trace->byte_order; int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0; struct declaration_float *float_declaration; @@ -1239,9 +1327,9 @@ struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth, } has_alignment = 1; } else { - fprintf(fd, "[error] %s: unknown attribute name %s\n", + fprintf(fd, "[warning] %s: unknown attribute name %s\n", __func__, left->u.unary_expression.u.string); - return NULL; + /* Fall-through after warning */ } } if (!has_mant_dig) { @@ -1290,9 +1378,9 @@ struct declaration *ctf_declaration_string_visit(FILE *fd, int depth, } encoding_c = right->u.unary_expression.u.string; } else { - fprintf(fd, "[error] %s: unknown attribute name %s\n", + fprintf(fd, "[warning] %s: unknown attribute name %s\n", __func__, left->u.unary_expression.u.string); - return NULL; + /* Fall-through after warning */ } } if (encoding_c && !strcmp(encoding_c, "ASCII")) @@ -1495,10 +1583,39 @@ int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru goto error; } event->fields_decl = container_of(declaration, struct declaration_struct, p); + } else if (!strcmp(left, "loglevel.identifier")) { + char *right; + + if (CTF_EVENT_FIELD_IS_SET(event, loglevel_identifier)) { + fprintf(fd, "[error] %s: identifier already declared in event declaration\n", __func__); + ret = -EPERM; + goto error; + } + right = concatenate_unary_strings(&node->u.ctf_expression.right); + if (!right) { + fprintf(fd, "[error] %s: unexpected unary expression for event identifier\n", __func__); + ret = -EINVAL; + goto error; + } + event->loglevel_identifier = g_quark_from_string(right); + g_free(right); + CTF_EVENT_SET_FIELD(event, loglevel_identifier); + } else if (!strcmp(left, "loglevel.value")) { + if (CTF_EVENT_FIELD_IS_SET(event, loglevel_value)) { + fprintf(fd, "[error] %s: loglevel value already declared in event declaration\n", __func__); + ret = -EPERM; + goto error; + } + ret = get_unary_signed(&node->u.ctf_expression.right, &event->loglevel_value); + if (ret) { + fprintf(fd, "[error] %s: unexpected unary expression for event loglevel value\n", __func__); + ret = -EINVAL; + goto error; + } + CTF_EVENT_SET_FIELD(event, loglevel_value); } else { - fprintf(fd, "[error] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left); - ret = -EINVAL; - goto error; + fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left); + /* Fall-through after warning */ } error: g_free(left); @@ -1519,7 +1636,6 @@ int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node, int ret = 0; struct ctf_node *iter; struct ctf_event *event; - struct definition_scope *parent_def_scope; event = g_new0(struct ctf_event, 1); event->declaration_scope = new_declaration_scope(parent_declaration_scope); @@ -1564,38 +1680,9 @@ int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node, g_hash_table_insert(event->stream->event_quark_to_id, (gpointer)(unsigned long) event->name, &event->id); - parent_def_scope = event->stream->definition_scope; - if (event->context_decl) { - struct definition *definition = - event->context_decl->p.definition_new(&event->context_decl->p, - parent_def_scope, 0, 0, "event.context"); - if (!definition) { - ret = -EINVAL; - goto error; - } - event->context = container_of(definition, - struct definition_struct, p); - parent_def_scope = event->context->scope; - } - if (event->fields_decl) { - struct definition *definition = - event->fields_decl->p.definition_new(&event->fields_decl->p, - parent_def_scope, 0, 0, "event.fields"); - if (!definition) { - ret = -EINVAL; - goto error; - } - event->fields = container_of(definition, - struct definition_struct, p); - parent_def_scope = event->fields->scope; - } return 0; error: - if (event->context) - definition_unref(&event->context->p); - if (event->fields) - definition_unref(&event->fields->p); if (event->fields_decl) declaration_unref(&event->fields_decl->p); if (event->context_decl) @@ -1711,9 +1798,8 @@ int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, str } stream->packet_context_decl = container_of(declaration, struct declaration_struct, p); } else { - fprintf(fd, "[error] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left); - ret = -EINVAL; - goto error; + fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left); + /* Fall-through after warning */ } error: @@ -1735,13 +1821,12 @@ int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, int ret = 0; struct ctf_node *iter; struct ctf_stream_class *stream; - struct definition_scope *parent_def_scope; stream = g_new0(struct ctf_stream_class, 1); stream->declaration_scope = new_declaration_scope(parent_declaration_scope); stream->events_by_id = g_ptr_array_new(); stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal); - stream->files = g_ptr_array_new(); + stream->streams = g_ptr_array_new(); if (node) { cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace); @@ -1769,62 +1854,18 @@ int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, if (trace->streams->len <= stream->stream_id) g_ptr_array_set_size(trace->streams, stream->stream_id + 1); g_ptr_array_index(trace->streams, stream->stream_id) = stream; - - parent_def_scope = trace->definition_scope; - if (stream->packet_context_decl) { - struct definition *definition = - stream->packet_context_decl->p.definition_new(&stream->packet_context_decl->p, - parent_def_scope, 0, 0, "stream.packet.context"); - if (!definition) { - ret = -EINVAL; - goto error; - } - stream->packet_context = container_of(definition, - struct definition_struct, p); - parent_def_scope = stream->packet_context->scope; - } - if (stream->event_header_decl) { - struct definition *definition = - stream->event_header_decl->p.definition_new(&stream->event_header_decl->p, - parent_def_scope, 0, 0, "stream.event.header"); - if (!definition) { - ret = -EINVAL; - goto error; - } - stream->event_header = - container_of(definition, struct definition_struct, p); - parent_def_scope = stream->event_header->scope; - } - if (stream->event_context_decl) { - struct definition *definition = - stream->event_context_decl->p.definition_new(&stream->event_context_decl->p, - parent_def_scope, 0, 0, "stream.event.context"); - if (!definition) { - ret = -EINVAL; - goto error; - } - stream->event_context = - container_of(definition, struct definition_struct, p); - parent_def_scope = stream->event_context->scope; - } - stream->definition_scope = parent_def_scope; + stream->trace = trace; return 0; error: - if (stream->event_context) - definition_unref(&stream->event_context->p); - if (stream->event_header) - definition_unref(&stream->event_header->p); - if (stream->packet_context) - definition_unref(&stream->packet_context->p); if (stream->event_header_decl) declaration_unref(&stream->event_header_decl->p); if (stream->event_context_decl) declaration_unref(&stream->event_context_decl->p); if (stream->packet_context_decl) declaration_unref(&stream->packet_context_decl->p); - g_ptr_array_free(stream->files, TRUE); + g_ptr_array_free(stream->streams, TRUE); g_ptr_array_free(stream->events_by_id, TRUE); g_hash_table_destroy(stream->event_quark_to_id); free_declaration_scope(stream->declaration_scope); @@ -1919,7 +1960,18 @@ int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru ret = -EPERM; goto error; } else { - trace->byte_order = byte_order; + if (byte_order != trace->byte_order) { + trace->byte_order = byte_order; + /* + * We need to restart + * construction of the + * intermediate representation. + */ + trace->field_mask = 0; + CTF_TRACE_SET_FIELD(trace, byte_order); + ret = -EINTR; + goto error; + } } CTF_TRACE_SET_FIELD(trace, byte_order); } else if (!strcmp(left, "packet.header")) { @@ -1944,9 +1996,7 @@ int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru } trace->packet_header_decl = container_of(declaration, struct declaration_struct, p); } else { - fprintf(fd, "[error] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left); - ret = -EINVAL; - goto error; + fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left); } error: @@ -1964,7 +2014,6 @@ error: static int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) { - struct definition_scope *parent_def_scope; int ret = 0; struct ctf_node *iter; @@ -1998,24 +2047,9 @@ int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace goto error; } - parent_def_scope = NULL; - if (trace->packet_header_decl) { - struct definition *definition = - trace->packet_header_decl->p.definition_new(&trace->packet_header_decl->p, - parent_def_scope, 0, 0, "trace.packet.header"); - if (!definition) { - ret = -EINVAL; - goto error; - } - trace->packet_header = - container_of(definition, struct definition_struct, p); - parent_def_scope = trace->packet_header->scope; - } - trace->definition_scope = parent_def_scope; - if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { /* check that the packet header contains a "magic" field */ - if (!trace->packet_header + if (!trace->packet_header_decl || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) { ret = -EPERM; fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__); @@ -2025,12 +2059,13 @@ int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace return 0; error: - if (trace->packet_header) - definition_unref(&trace->packet_header->p); - if (trace->packet_header_decl) + if (trace->packet_header_decl) { declaration_unref(&trace->packet_header_decl->p); + trace->packet_header_decl = NULL; + } g_ptr_array_free(trace->streams, TRUE); free_declaration_scope(trace->declaration_scope); + trace->declaration_scope = NULL; return ret; } @@ -2086,9 +2121,11 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, struct ctf_node *iter; printf_verbose("CTF visitor: metadata construction... "); - trace->root_declaration_scope = new_declaration_scope(NULL); trace->byte_order = byte_order; +retry: + trace->root_declaration_scope = new_declaration_scope(NULL); + switch (node->type) { case NODE_ROOT: cds_list_for_each_entry(iter, &node->u.root.declaration_list, @@ -2101,6 +2138,15 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, } cds_list_for_each_entry(iter, &node->u.root.trace, siblings) { ret = ctf_trace_visit(fd, depth + 1, iter, trace); + if (ret == -EINTR) { + free_declaration_scope(trace->root_declaration_scope); + /* + * Need to restart creation of type + * definitions, aliases and + * trace header declarations. + */ + goto retry; + } if (ret) { fprintf(fd, "[error] %s: trace declaration error\n", __func__); goto error;