X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=formats%2Fctf%2Fmetadata%2Fctf-visitor-generate-io-struct.c;h=507233c97cdf3b4fbdf4ce8e24548be2a8fb6967;hp=a2919ee2c7ce036b353e1002c1c05aba2d11bc05;hb=2527e1494b783a60d8a874f28835700046f55dda;hpb=068665f5ffd43270ed9f664bde1c165d077da47c diff --git a/formats/ctf/metadata/ctf-visitor-generate-io-struct.c b/formats/ctf/metadata/ctf-visitor-generate-io-struct.c index a2919ee2..507233c9 100644 --- a/formats/ctf/metadata/ctf-visitor-generate-io-struct.c +++ b/formats/ctf/metadata/ctf-visitor-generate-io-struct.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,10 @@ struct declaration *ctf_type_specifier_list_visit(FILE *fd, struct declaration_scope *declaration_scope, struct ctf_trace *trace); +static +int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, + struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace); + /* * String returned must be freed by the caller using g_free. */ @@ -120,7 +125,7 @@ int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid) } static -struct ctf_stream *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id) +struct ctf_stream_class *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id) { if (trace->streams->len <= stream_id) return NULL; @@ -308,6 +313,22 @@ struct declaration *ctf_type_declarator_visit(FILE *fd, int depth, fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q)); return NULL; } + if (nested_declaration->id == CTF_TYPE_INTEGER) { + struct declaration_integer *integer_declaration = + container_of(nested_declaration, struct declaration_integer, p); + /* For base to 16 for pointers (expected pretty-print) */ + if (!integer_declaration->base) { + /* + * We need to do a copy of the + * integer declaration to modify it. There could be other references to + * it. + */ + integer_declaration = integer_declaration_new(integer_declaration->len, + integer_declaration->byte_order, integer_declaration->signedness, + integer_declaration->p.alignment, 16); + nested_declaration = &integer_declaration->p; + } + } } else { nested_declaration = ctf_type_specifier_list_visit(fd, depth, type_specifier_list, declaration_scope, trace); @@ -351,20 +372,29 @@ struct declaration *ctf_type_declarator_visit(FILE *fd, int depth, declaration = &array_declaration->p; break; } - case NODE_INTEGER: - case NODE_TYPE_SPECIFIER: + case NODE_TYPE_SPECIFIER_LIST: { struct declaration_sequence *sequence_declaration; struct declaration_integer *integer_declaration; - GQuark dummy_id; - declaration = ctf_type_declarator_visit(fd, depth, - length, - &dummy_id, NULL, - declaration_scope, - NULL, trace); - assert(declaration->id == CTF_TYPE_INTEGER); + declaration = ctf_type_specifier_list_visit(fd, depth, + length, declaration_scope, trace); + if (!declaration) { + fprintf(fd, "[error] %s: unable to find declaration type for sequence length\n", __func__); + return NULL; + } + if (declaration->id != CTF_TYPE_INTEGER) { + fprintf(fd, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__); + declaration_unref(declaration); + return NULL; + } integer_declaration = container_of(declaration, struct declaration_integer, p); + if (integer_declaration->signedness != false) { + fprintf(fd, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__); + declaration_unref(declaration); + return NULL; + } + sequence_declaration = sequence_declaration_new(integer_declaration, nested_declaration, declaration_scope); declaration = &sequence_declaration->p; @@ -402,6 +432,10 @@ int ctf_struct_type_declarators_visit(FILE *fd, int depth, &field_name, iter, struct_declaration->scope, NULL, trace); + if (!field_declaration) { + fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__); + return -EINVAL; + } struct_declaration_add_field(struct_declaration, g_quark_to_string(field_name), field_declaration); @@ -428,6 +462,10 @@ int ctf_variant_type_declarators_visit(FILE *fd, int depth, &field_name, iter, untagged_variant_declaration->scope, NULL, trace); + if (!field_declaration) { + fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__); + return -EINVAL; + } untagged_variant_declaration_add_field(untagged_variant_declaration, g_quark_to_string(field_name), field_declaration); @@ -452,6 +490,19 @@ int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope, type_specifier_list, &identifier, iter, scope, NULL, trace); + if (!type_declaration) { + fprintf(fd, "[error] %s: problem creating type declaration\n", __func__); + return -EINVAL; + } + /* + * Don't allow typedef and typealias of untagged + * variants. + */ + if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) { + fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__); + declaration_unref(type_declaration); + return -EPERM; + } ret = register_declaration(identifier, type_declaration, scope); if (ret) { type_declaration->declaration_free(type_declaration); @@ -492,6 +543,15 @@ int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope, err = -EINVAL; goto error; } + /* + * Don't allow typedef and typealias of untagged + * variants. + */ + if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) { + fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__); + declaration_unref(type_declaration); + return -EPERM; + } /* * The semantic validator does not check whether the target is * abstract or not (if it has an identifier). Check it here. @@ -609,7 +669,8 @@ int ctf_variant_declaration_list_visit(FILE *fd, int depth, static struct declaration *ctf_declaration_struct_visit(FILE *fd, int depth, const char *name, struct cds_list_head *declaration_list, - int has_body, struct declaration_scope *declaration_scope, + int has_body, struct cds_list_head *min_align, + struct declaration_scope *declaration_scope, struct ctf_trace *trace) { struct declaration_struct *struct_declaration; @@ -628,6 +689,8 @@ struct declaration *ctf_declaration_struct_visit(FILE *fd, declaration_scope); return &struct_declaration->p; } else { + uint64_t min_align_value = 0; + /* For unnamed struct, create type */ /* For named struct (with body), create type and add to declaration scope */ if (name) { @@ -638,7 +701,16 @@ struct declaration *ctf_declaration_struct_visit(FILE *fd, return NULL; } } - struct_declaration = struct_declaration_new(declaration_scope); + if (!cds_list_empty(min_align)) { + ret = get_unary_unsigned(min_align, &min_align_value); + if (ret) { + fprintf(fd, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__); + ret = -EINVAL; + goto error; + } + } + struct_declaration = struct_declaration_new(declaration_scope, + min_align_value); cds_list_for_each_entry(iter, declaration_list, siblings) { ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter, struct_declaration, trace); @@ -719,7 +791,7 @@ struct declaration *ctf_declaration_variant_visit(FILE *fd, return &variant_declaration->p; } error: - untagged_variant_declaration->p.declaration_free(&variant_declaration->p); + untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p); return NULL; } @@ -845,24 +917,28 @@ struct declaration *ctf_declaration_enum_visit(FILE *fd, int depth, } } if (!container_type) { - fprintf(fd, "[error] %s: missing container type for enumeration\n", __func__); + declaration = lookup_declaration(g_quark_from_static_string("int"), + declaration_scope); + if (!declaration) { + fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__); return NULL; - - } - switch (container_type->type) { - case NODE_INTEGER: - case NODE_TYPE_SPECIFIER: + } + } else { declaration = ctf_type_declarator_visit(fd, depth, container_type, &dummy_id, NULL, declaration_scope, NULL, trace); - assert(declaration->id == CTF_TYPE_INTEGER); - integer_declaration = container_of(declaration, struct declaration_integer, p); - break; - default: - assert(0); } + if (!declaration) { + fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__); + return NULL; + } + if (declaration->id != CTF_TYPE_INTEGER) { + fprintf(fd, "[error] %s: container type for enumeration is not integer\n", __func__); + return NULL; + } + integer_declaration = container_of(declaration, struct declaration_integer, p); enum_declaration = enum_declaration_new(integer_declaration); declaration_unref(&integer_declaration->p); /* leave ref to enum */ cds_list_for_each_entry(iter, enumerator_list, siblings) { @@ -950,6 +1026,28 @@ int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression) } +static +int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression) +{ + int byte_order; + + if (unary_expression->u.unary_expression.type != UNARY_STRING) { + fprintf(fd, "[error] %s: byte_order: expecting string\n", + __func__); + return -EINVAL; + } + if (!strcmp(unary_expression->u.unary_expression.u.string, "be")) + byte_order = BIG_ENDIAN; + else if (!strcmp(unary_expression->u.unary_expression.u.string, "le")) + byte_order = LITTLE_ENDIAN; + else { + fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n", + __func__, unary_expression->u.unary_expression.u.string); + return -EINVAL; + } + return byte_order; +} + static int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression, struct ctf_trace *trace) @@ -987,6 +1085,7 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, int byte_order = trace->byte_order; int signedness = 0; int has_alignment = 0, has_size = 0; + int base = 0; struct declaration_integer *integer_declaration; cds_list_for_each_entry(expression, expressions, siblings) { @@ -1018,7 +1117,63 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, return NULL; } alignment = right->u.unary_expression.u.unsigned_constant; + /* Make sure alignment is a power of two */ + if (alignment == 0 || (alignment & (alignment - 1)) != 0) { + fprintf(fd, "[error] %s: align: expecting power of two\n", + __func__); + return NULL; + } has_alignment = 1; + } else if (!strcmp(left->u.unary_expression.u.string, "base")) { + switch (right->u.unary_expression.type) { + case UNARY_UNSIGNED_CONSTANT: + switch (right->u.unary_expression.u.unsigned_constant) { + case 2: + case 8: + case 10: + case 16: + base = right->u.unary_expression.u.unsigned_constant; + break; + default: + fprintf(fd, "[error] %s: base not supported (%" PRIu64 ")\n", + __func__, right->u.unary_expression.u.unsigned_constant); + return NULL; + } + break; + case UNARY_STRING: + { + char *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, "decimal") || !strcmp(s_right, "dec") || !strcmp(s_right, "d") + || !strcmp(s_right, "i") || !strcmp(s_right, "u")) { + base = 10; + } else if (!strcmp(s_right, "hexadecimal") || !strcmp(s_right, "hex") + || !strcmp(s_right, "x") || !strcmp(s_right, "X") + || !strcmp(s_right, "p")) { + base = 16; + } else if (!strcmp(s_right, "octal") || !strcmp(s_right, "oct") + || !strcmp(s_right, "o")) { + base = 8; + } else if (!strcmp(s_right, "binary") || !strcmp(s_right, "b")) { + base = 2; + } else { + fprintf(fd, "[error] %s: unexpected expression for integer base (%s)\n", __func__, s_right); + g_free(s_right); + return NULL; + } + + g_free(s_right); + break; + } + default: + fprintf(fd, "[error] %s: base: expecting unsigned constant or unary string\n", + __func__); + return NULL; + } } else { fprintf(fd, "[error] %s: unknown attribute name %s\n", __func__, left->u.unary_expression.u.string); @@ -1039,7 +1194,7 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, } } integer_declaration = integer_declaration_new(size, - byte_order, signedness, alignment); + byte_order, signedness, alignment, base); return &integer_declaration->p; } @@ -1086,6 +1241,12 @@ struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth, return NULL; } alignment = right->u.unary_expression.u.unsigned_constant; + /* Make sure alignment is a power of two */ + if (alignment == 0 || (alignment & (alignment - 1)) != 0) { + fprintf(fd, "[error] %s: align: expecting power of two\n", + __func__); + return NULL; + } has_alignment = 1; } else { fprintf(fd, "[error] %s: unknown attribute name %s\n", @@ -1177,12 +1338,13 @@ struct declaration *ctf_type_specifier_list_visit(FILE *fd, &node->u.integer.expressions, trace); case TYPESPEC_STRING: return ctf_declaration_string_visit(fd, depth, - &first->u.string.expressions, trace); + &node->u.string.expressions, trace); case TYPESPEC_STRUCT: return ctf_declaration_struct_visit(fd, depth, node->u._struct.name, &node->u._struct.declaration_list, node->u._struct.has_body, + &node->u._struct.min_align, declaration_scope, trace); case TYPESPEC_VARIANT: @@ -1257,12 +1419,14 @@ int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru if (CTF_EVENT_FIELD_IS_SET(event, name)) { fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } right = concatenate_unary_strings(&node->u.ctf_expression.right); if (!right) { fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } event->name = g_quark_from_string(right); g_free(right); @@ -1270,28 +1434,33 @@ int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru } else if (!strcmp(left, "id")) { if (CTF_EVENT_FIELD_IS_SET(event, id)) { fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id); if (ret) { fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } CTF_EVENT_SET_FIELD(event, id); } else if (!strcmp(left, "stream_id")) { if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) { fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id); if (ret) { fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } event->stream = trace_stream_lookup(trace, event->stream_id); if (!event->stream) { fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id); - return -EINVAL; + ret = -EINVAL; + goto error; } CTF_EVENT_SET_FIELD(event, stream_id); } else if (!strcmp(left, "context")) { @@ -1299,34 +1468,45 @@ int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru if (event->context_decl) { fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } declaration = ctf_type_specifier_list_visit(fd, depth, _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings), event->declaration_scope, trace); - if (!declaration) - return -EPERM; - if (declaration->id != CTF_TYPE_STRUCT) - return -EPERM; + if (!declaration) { + ret = -EPERM; + goto error; + } + if (declaration->id != CTF_TYPE_STRUCT) { + ret = -EPERM; + goto error; + } event->context_decl = container_of(declaration, struct declaration_struct, p); } else if (!strcmp(left, "fields")) { struct declaration *declaration; if (event->fields_decl) { fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } declaration = ctf_type_specifier_list_visit(fd, depth, _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings), event->declaration_scope, trace); - if (!declaration) - return -EPERM; - if (declaration->id != CTF_TYPE_STRUCT) - return -EPERM; + if (!declaration) { + ret = -EPERM; + goto error; + } + if (declaration->id != CTF_TYPE_STRUCT) { + ret = -EPERM; + goto error; + } event->fields_decl = container_of(declaration, struct declaration_struct, p); } +error: g_free(left); break; } @@ -1335,7 +1515,7 @@ int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru /* TODO: declaration specifier should be added. */ } - return 0; + return ret; } static @@ -1359,14 +1539,29 @@ int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node, fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__); goto error; } - if (!CTF_EVENT_FIELD_IS_SET(event, id)) { - ret = -EPERM; - fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__); - goto error; - } if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) { + /* Allow missing stream_id if there is only a single stream */ + switch (trace->streams->len) { + case 0: /* Create stream if there was none. */ + ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace); + if (ret) + goto error; + /* Fall-through */ + case 1: + event->stream_id = 0; + event->stream = trace_stream_lookup(trace, event->stream_id); + break; + default: + ret = -EPERM; + fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__); + goto error; + } + } + /* Allow only one event without id per stream */ + if (!CTF_EVENT_FIELD_IS_SET(event, id) + && event->stream->events_by_id->len != 0) { ret = -EPERM; - fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__); + fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__); goto error; } if (event->stream->events_by_id->len <= event->id) @@ -1412,7 +1607,7 @@ error: static -int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream *stream, struct ctf_trace *trace) +int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_class *stream, struct ctf_trace *trace) { int ret = 0; @@ -1442,12 +1637,14 @@ int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, str if (!strcmp(left, "id")) { if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id); if (ret) { fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } CTF_STREAM_SET_FIELD(stream, stream_id); } else if (!strcmp(left, "event.header")) { @@ -1455,50 +1652,66 @@ int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, str if (stream->event_header_decl) { fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } declaration = ctf_type_specifier_list_visit(fd, depth, _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings), stream->declaration_scope, trace); - if (!declaration) - return -EPERM; - if (declaration->id != CTF_TYPE_STRUCT) - return -EPERM; + if (!declaration) { + ret = -EPERM; + goto error; + } + if (declaration->id != CTF_TYPE_STRUCT) { + ret = -EPERM; + goto error; + } stream->event_header_decl = container_of(declaration, struct declaration_struct, p); } else if (!strcmp(left, "event.context")) { struct declaration *declaration; if (stream->event_context_decl) { fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } declaration = ctf_type_specifier_list_visit(fd, depth, _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings), stream->declaration_scope, trace); - if (!declaration) - return -EPERM; - if (declaration->id != CTF_TYPE_STRUCT) - return -EPERM; + if (!declaration) { + ret = -EPERM; + goto error; + } + if (declaration->id != CTF_TYPE_STRUCT) { + ret = -EPERM; + goto error; + } stream->event_context_decl = container_of(declaration, struct declaration_struct, p); } else if (!strcmp(left, "packet.context")) { struct declaration *declaration; if (stream->packet_context_decl) { fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } declaration = ctf_type_specifier_list_visit(fd, depth, _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings), stream->declaration_scope, trace); - if (!declaration) - return -EPERM; - if (declaration->id != CTF_TYPE_STRUCT) - return -EPERM; + if (!declaration) { + ret = -EPERM; + goto error; + } + if (declaration->id != CTF_TYPE_STRUCT) { + ret = -EPERM; + goto error; + } stream->packet_context_decl = container_of(declaration, struct declaration_struct, p); } +error: g_free(left); break; } @@ -1507,7 +1720,7 @@ int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, str /* TODO: declaration specifier should be added. */ } - return 0; + return ret; } static @@ -1516,28 +1729,43 @@ int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, { int ret = 0; struct ctf_node *iter; - struct ctf_stream *stream; + struct ctf_stream_class *stream; struct definition_scope *parent_def_scope; - stream = g_new0(struct ctf_stream, 1); + 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); - cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { - ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace); - if (ret) - goto error; + stream->files = 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); + if (ret) + goto error; + } } - if (!CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { - ret = -EPERM; - fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__); - goto error; + if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { + /* check that packet header has stream_id field. */ + if (!trace->packet_header_decl + || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) { + ret = -EPERM; + fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__); + goto error; + } + } else { + /* Allow only one id-less stream */ + if (trace->streams->len != 0) { + ret = -EPERM; + fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__); + goto error; + } + stream->stream_id = 0; } 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 = NULL; + parent_def_scope = trace->definition_scope; if (stream->packet_context_decl) { stream->packet_context = container_of( @@ -1582,6 +1810,7 @@ error: declaration_unref(&stream->event_header_decl->p); declaration_unref(&stream->event_context_decl->p); declaration_unref(&stream->packet_context_decl->p); + g_ptr_array_free(stream->files, 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); @@ -1620,48 +1849,80 @@ int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru if (!strcmp(left, "major")) { if (CTF_TRACE_FIELD_IS_SET(trace, major)) { fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major); if (ret) { fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } CTF_TRACE_SET_FIELD(trace, major); } else if (!strcmp(left, "minor")) { if (CTF_TRACE_FIELD_IS_SET(trace, minor)) { fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor); if (ret) { fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } CTF_TRACE_SET_FIELD(trace, minor); - } else if (!strcmp(left, "word_size")) { - if (CTF_TRACE_FIELD_IS_SET(trace, word_size)) { - fprintf(fd, "[error] %s: word_size already declared in trace declaration\n", __func__); - return -EPERM; - } - ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->word_size); - if (ret) { - fprintf(fd, "[error] %s: unexpected unary expression for trace word_size\n", __func__); - return -EINVAL; - } - CTF_TRACE_SET_FIELD(trace, word_size); } else if (!strcmp(left, "uuid")) { if (CTF_TRACE_FIELD_IS_SET(trace, uuid)) { fprintf(fd, "[error] %s: uuid already declared in trace declaration\n", __func__); - return -EPERM; + ret = -EPERM; + goto error; } ret = get_unary_uuid(&node->u.ctf_expression.right, &trace->uuid); if (ret) { fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__); - return -EINVAL; + ret = -EINVAL; + goto error; } 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; + CTF_TRACE_SET_FIELD(trace, byte_order); + } else if (!strcmp(left, "packet.header")) { + struct declaration *declaration; + + if (trace->packet_header_decl) { + fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__); + ret = -EINVAL; + goto error; + } + declaration = ctf_type_specifier_list_visit(fd, depth, + _cds_list_first_entry(&node->u.ctf_expression.right, + struct ctf_node, siblings), + trace->declaration_scope, trace); + if (!declaration) { + ret = -EPERM; + goto error; + } + if (declaration->id != CTF_TYPE_STRUCT) { + ret = -EPERM; + goto error; + } + trace->packet_header_decl = container_of(declaration, struct declaration_struct, p); } +error: g_free(left); break; } @@ -1670,12 +1931,13 @@ int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, stru /* TODO: declaration specifier should be added. */ } - return 0; + return ret; } 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; @@ -1703,13 +1965,40 @@ 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, word_size)) { + if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { ret = -EPERM; - fprintf(fd, "[error] %s: missing word_size field in trace declaration\n", __func__); + fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__); goto error; } + + parent_def_scope = NULL; + if (trace->packet_header_decl) { + trace->packet_header = + container_of( + trace->packet_header_decl->p.definition_new(&trace->packet_header_decl->p, + parent_def_scope, 0, 0), + struct definition_struct, p); + set_dynamic_definition_scope(&trace->packet_header->p, + trace->packet_header->scope, + "trace.packet.header"); + parent_def_scope = trace->packet_header->scope; + declaration_unref(&trace->packet_header_decl->p); + } + 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 + || 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__); + goto error_free_def; + } + } return 0; +error_free_def: + definition_unref(&trace->packet_header->p); error: g_ptr_array_free(trace->streams, TRUE); free_declaration_scope(trace->declaration_scope); @@ -1767,8 +2056,9 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, int ret = 0; struct ctf_node *iter; - fprintf(fd, "CTF visitor: metadata construction... "); + 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) { @@ -1788,6 +2078,10 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, return ret; } } + if (!trace->streams) { + fprintf(fd, "[error] %s: missing trace declaration\n", __func__); + return -EINVAL; + } cds_list_for_each_entry(iter, &node->u.root.stream, siblings) { ret = ctf_stream_visit(fd, depth + 1, iter, trace->root_declaration_scope, trace); @@ -1811,6 +2105,6 @@ int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, (int) node->type); return -EINVAL; } - fprintf(fd, "done.\n"); + printf_verbose("done.\n"); return ret; }