X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=plugins%2Fctf%2Fcommon%2Fmetadata%2Fvisitor-generate-ir.c;h=07f4391fcbcb0c2bf8b1e2fbab2490c14be288f5;hb=fde61d2657429be0d03b4bf899e86f3c5425e870;hp=c16c97065a868c5676c61289c2da091f58929fde;hpb=dc3fffef7b84cc4af1a7c99828fd57a106cd2257;p=babeltrace.git diff --git a/plugins/ctf/common/metadata/visitor-generate-ir.c b/plugins/ctf/common/metadata/visitor-generate-ir.c index c16c9706..07f4391f 100644 --- a/plugins/ctf/common/metadata/visitor-generate-ir.c +++ b/plugins/ctf/common/metadata/visitor-generate-ir.c @@ -30,14 +30,15 @@ #include #include #include +#include #include #include #include #include #include #include -#include -#include +#include +#include #include #include #include @@ -152,30 +153,35 @@ enum loglevel { /* Error printing wrappers */ #define _PERROR(_fmt, ...) \ do { \ + if (!ctx->efd) break; \ fprintf(ctx->efd, "[error] %s: " _fmt "\n", \ __func__, __VA_ARGS__); \ } while (0) #define _PWARNING(_fmt, ...) \ do { \ + if (!ctx->efd) break; \ fprintf(ctx->efd, "[warning] %s: " _fmt "\n", \ __func__, __VA_ARGS__); \ } while (0) #define _FPERROR(_stream, _fmt, ...) \ do { \ + if (!_stream) break; \ fprintf(_stream, "[error] %s: " _fmt "\n", \ __func__, __VA_ARGS__); \ } while (0) #define _FPWARNING(_stream, _fmt, ...) \ do { \ + if (!_stream) break; \ fprintf(_stream, "[warning] %s: " _fmt "\n", \ __func__, __VA_ARGS__); \ } while (0) #define _PERROR_DUP_ATTR(_attr, _entity) \ do { \ + if (!ctx->efd) break; \ fprintf(ctx->efd, \ "[error] %s: duplicate attribute \"" \ _attr "\" in " _entity "\n", __func__); \ @@ -199,10 +205,10 @@ struct ctx_decl_scope { }; /* - * Visitor context. + * Visitor context (private). */ struct ctx { - /* Trace being filled (weak ref.) */ + /* Trace being filled (owned by this) */ struct bt_ctf_trace *trace; /* Error stream to use during visit */ @@ -214,7 +220,14 @@ struct ctx { /* 1 if trace declaration is visited */ int is_trace_visited; + /* 1 if this is an LTTng trace */ + bool is_lttng; + + /* Offset (ns) to apply to clock classes on creation */ + uint64_t clock_class_offset_ns; + /* Trace attributes */ + enum bt_ctf_byte_order trace_bo; uint64_t trace_major; uint64_t trace_minor; unsigned char trace_uuid[BABELTRACE_UUID_LEN]; @@ -227,6 +240,11 @@ struct ctx { GHashTable *stream_classes; }; +/* + * Visitor (public). + */ +struct ctf_visitor_generate_ir { }; + static const char *loglevel_str [] = { [ LOGLEVEL_EMERG ] = "TRACE_EMERG", @@ -562,12 +580,13 @@ int ctx_decl_scope_register_variant(struct ctx_decl_scope *scope, * @returns New visitor context, or NULL on error */ static -struct ctx *ctx_create(struct bt_ctf_trace *trace, FILE *efd) +struct ctx *ctx_create(struct bt_ctf_trace *trace, FILE *efd, + uint64_t clock_class_offset_ns) { struct ctx *ctx = NULL; struct ctx_decl_scope *scope = NULL; - ctx = g_new(struct ctx, 1); + ctx = g_new0(struct ctx, 1); if (!ctx) { goto error; } @@ -587,14 +606,13 @@ struct ctx *ctx_create(struct bt_ctf_trace *trace, FILE *efd) ctx->trace = trace; ctx->efd = efd; ctx->current_scope = scope; - ctx->is_trace_visited = FALSE; - + ctx->trace_bo = BT_CTF_BYTE_ORDER_NATIVE; + ctx->clock_class_offset_ns = clock_class_offset_ns; return ctx; error: g_free(ctx); ctx_decl_scope_destroy(scope); - return NULL; } @@ -624,6 +642,7 @@ void ctx_destroy(struct ctx *ctx) scope = parent_scope; } + bt_put(ctx->trace); g_hash_table_destroy(ctx->stream_classes); g_free(ctx); @@ -1026,7 +1045,7 @@ enum bt_ctf_byte_order get_real_byte_order(struct ctx *ctx, enum bt_ctf_byte_order bo = byte_order_from_unary_expr(ctx->efd, uexpr); if (bo == BT_CTF_BYTE_ORDER_NATIVE) { - bo = bt_ctf_trace_get_byte_order(ctx->trace); + bo = bt_ctf_trace_get_native_byte_order(ctx->trace); } return bo; @@ -1273,7 +1292,7 @@ int visit_type_declarator(struct ctx *ctx, struct ctf_node *type_specifier_list, nested_decl_copy = bt_ctf_field_type_copy(nested_decl); BT_PUT(nested_decl); if (!nested_decl_copy) { - _PERROR("%s", "cannot copy nested declaration"); + _PERROR("%s", "cannot copy nested field type"); ret = -EINVAL; goto error; } @@ -1348,7 +1367,7 @@ int visit_type_declarator(struct ctx *ctx, struct ctf_node *type_specifier_list, BT_PUT(nested_decl); if (!array_decl) { _PERROR("%s", - "cannot create array declaration"); + "cannot create array field type"); ret = -ENOMEM; goto error; } @@ -1373,7 +1392,7 @@ int visit_type_declarator(struct ctx *ctx, struct ctf_node *type_specifier_list, BT_PUT(nested_decl); if (!seq_decl) { _PERROR("%s", - "cannot create sequence declaration"); + "cannot create sequence field type"); ret = -ENOMEM; goto error; } @@ -1446,7 +1465,7 @@ int visit_struct_decl_field(struct ctx *ctx, &qfield_name, iter, &field_decl, NULL); if (ret) { assert(!field_decl); - _PERROR("%s", "unable to find structure field declaration type"); + _PERROR("%s", "cannot visit type declarator"); goto error; } @@ -1505,7 +1524,7 @@ int visit_variant_decl_field(struct ctx *ctx, if (ret) { assert(!field_decl); _PERROR("%s", - "unable to find variant field declaration type"); + "cannot visit type declarator"); goto error; } @@ -1556,7 +1575,7 @@ int visit_typedef(struct ctx *ctx, struct ctf_node *type_specifier_list, ret = visit_type_declarator(ctx, type_specifier_list, &qidentifier, iter, &type_decl, NULL); if (ret) { - _PERROR("%s", "problem creating type declaration"); + _PERROR("%s", "cannot visit type declarator"); ret = -EINVAL; goto end; } @@ -1609,7 +1628,7 @@ int visit_typealias(struct ctx *ctx, struct ctf_node *target, &qdummy_field_name, node, &type_decl, NULL); if (ret) { assert(!type_decl); - _PERROR("%s", "problem creating type declaration"); + _PERROR("%s", "cannot visit type declarator"); goto end; } @@ -1777,7 +1796,7 @@ int visit_struct_decl(struct ctx *ctx, const char *name, struct_decl_copy = bt_ctf_field_type_copy(*struct_decl); if (!struct_decl_copy) { _PERROR("%s", - "cannot create copy of structure declaration"); + "cannot create copy of structure field type"); ret = -EINVAL; goto error; } @@ -1811,7 +1830,7 @@ int visit_struct_decl(struct ctx *ctx, const char *name, *struct_decl = bt_ctf_field_type_structure_create(); if (!*struct_decl) { - _PERROR("%s", "cannot create structure declaration"); + _PERROR("%s", "cannot create structure field type"); ret = -ENOMEM; goto error; } @@ -1894,7 +1913,7 @@ int visit_variant_decl(struct ctx *ctx, const char *name, untagged_variant_decl); if (!variant_decl_copy) { _PERROR("%s", - "cannot create copy of structure declaration"); + "cannot create copy of variant field type"); ret = -EINVAL; goto error; } @@ -1920,7 +1939,7 @@ int visit_variant_decl(struct ctx *ctx, const char *name, untagged_variant_decl = bt_ctf_field_type_variant_create(NULL, NULL); if (!untagged_variant_decl) { - _PERROR("%s", "cannot create variant declaration"); + _PERROR("%s", "cannot create variant field type"); ret = -ENOMEM; goto error; } @@ -2101,7 +2120,7 @@ int visit_enum_decl(struct ctx *ctx, const char *name, enum_decl_copy = bt_ctf_field_type_copy(*enum_decl); if (!enum_decl_copy) { _PERROR("%s", - "cannot create copy of enumeration declaration"); + "cannot create copy of enumeration field type"); ret = -EINVAL; goto error; } @@ -2154,7 +2173,7 @@ int visit_enum_decl(struct ctx *ctx, const char *name, *enum_decl = bt_ctf_field_type_enumeration_create(integer_decl); if (!*enum_decl) { - _PERROR("%s", "cannot create enumeration declaration"); + _PERROR("%s", "cannot create enumeration field type"); ret = -ENOMEM; goto error; } @@ -2214,7 +2233,7 @@ int visit_type_specifier(struct ctx *ctx, /* Make a copy of the type declaration */ decl_copy = bt_ctf_field_type_copy(*decl); if (!decl_copy) { - _PERROR("%s", "cannot create copy of type declaration"); + _PERROR("%s", "cannot create field type copy"); ret = -EINVAL; goto error; } @@ -2249,7 +2268,7 @@ int visit_integer_decl(struct ctx *ctx, enum bt_ctf_string_encoding encoding = BT_CTF_STRING_ENCODING_NONE; enum bt_ctf_integer_base base = BT_CTF_INTEGER_BASE_DECIMAL; enum bt_ctf_byte_order byte_order = - bt_ctf_trace_get_byte_order(ctx->trace); + bt_ctf_trace_get_native_byte_order(ctx->trace); *integer_decl = NULL; @@ -2505,7 +2524,7 @@ int visit_integer_decl(struct ctx *ctx, goto error; } - _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock: \"%s\"", + _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"", s_right); _SET(&set, _INTEGER_MAP_SET); g_free(s_right); @@ -2515,7 +2534,7 @@ int visit_integer_decl(struct ctx *ctx, mapped_clock = bt_ctf_trace_get_clock_class_by_name( ctx->trace, clock_name); if (!mapped_clock) { - _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock \"%s\"", + _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"", clock_name); ret = -EINVAL; goto error; @@ -2547,7 +2566,7 @@ int visit_integer_decl(struct ctx *ctx, *integer_decl = bt_ctf_field_type_integer_create((unsigned int) size); if (!*integer_decl) { - _PERROR("%s", "cannot create integer declaration"); + _PERROR("%s", "cannot create integer field type"); ret = -ENOMEM; goto error; } @@ -2568,7 +2587,7 @@ int visit_integer_decl(struct ctx *ctx, } if (ret) { - _PERROR("%s", "cannot configure integer declaration"); + _PERROR("%s", "cannot configure integer field type"); ret = -EINVAL; goto error; } @@ -2595,7 +2614,7 @@ int visit_floating_point_number_decl(struct ctx *ctx, struct ctf_node *expression; uint64_t alignment = 1, exp_dig = 0, mant_dig = 0; enum bt_ctf_byte_order byte_order = - bt_ctf_trace_get_byte_order(ctx->trace); + bt_ctf_trace_get_native_byte_order(ctx->trace); *float_decl = NULL; @@ -2723,7 +2742,7 @@ int visit_floating_point_number_decl(struct ctx *ctx, *float_decl = bt_ctf_field_type_floating_point_create(); if (!*float_decl) { _PERROR("%s", - "cannot create floating point number declaration"); + "cannot create floating point number field type"); ret = -ENOMEM; goto error; } @@ -2736,7 +2755,7 @@ int visit_floating_point_number_decl(struct ctx *ctx, ret |= bt_ctf_field_type_set_alignment(*float_decl, alignment); if (ret) { _PERROR("%s", - "cannot configure floating point number declaration"); + "cannot configure floating point number field type"); ret = -EINVAL; goto error; } @@ -2827,14 +2846,14 @@ int visit_string_decl(struct ctx *ctx, *string_decl = bt_ctf_field_type_string_create(); if (!*string_decl) { - _PERROR("%s", "cannot create string declaration"); + _PERROR("%s", "cannot create string field type"); ret = -ENOMEM; goto error; } ret = bt_ctf_field_type_string_set_encoding(*string_decl, encoding); if (ret) { - _PERROR("%s", "cannot configure string declaration"); + _PERROR("%s", "cannot configure string field type"); ret = -EINVAL; goto error; } @@ -3089,7 +3108,7 @@ int visit_event_decl_entry(struct ctx *ctx, struct ctf_node *node, struct ctf_node, siblings), &decl); if (ret) { - _PERROR("%s", "cannot create event payload declaration"); + _PERROR("%s", "cannot create event payload field type"); goto error; } @@ -3098,7 +3117,7 @@ int visit_event_decl_entry(struct ctx *ctx, struct ctf_node *node, event_class, decl); BT_PUT(decl); if (ret) { - _PERROR("%s", "cannot set event's payload declaration"); + _PERROR("%s", "cannot set event's payload field type"); goto error; } @@ -3333,8 +3352,9 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) char *event_name = NULL; struct bt_ctf_event_class *event_class = NULL; struct bt_ctf_event_class *eevent_class; - struct bt_ctf_stream_class *stream_class; + struct bt_ctf_stream_class *stream_class = NULL; struct bt_list_head *decl_list = &node->u.event.declaration_list; + bool pop_scope = false; if (node->visited) { goto end; @@ -3359,13 +3379,14 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) goto error; } - ret = ctx_push_scope(ctx); if (ret) { _PERROR("%s", "cannot push scope"); goto error; } + pop_scope = true; + bt_list_for_each_entry(iter, decl_list, siblings) { ret = visit_event_decl_entry(ctx, iter, event_class, &stream_id, &set); @@ -3377,11 +3398,17 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) if (!_IS_SET(&set, _EVENT_STREAM_ID_SET)) { GList *keys = NULL; struct bt_ctf_stream_class *new_stream_class; + size_t stream_class_count = + g_hash_table_size(ctx->stream_classes) + + bt_ctf_trace_get_stream_class_count(ctx->trace); - /* Allow missing stream_id if there is only a single stream */ - switch (g_hash_table_size(ctx->stream_classes)) { + /* + * Allow missing stream_id if there is only a single + * stream class. + */ + switch (stream_class_count) { case 0: - /* Create stream if there's none */ + /* Create implicit stream class if there's none */ new_stream_class = create_reset_stream_class(ctx); if (!new_stream_class) { ret = -EINVAL; @@ -3390,7 +3417,7 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) ret = bt_ctf_stream_class_set_id(new_stream_class, 0); if (ret) { - _PERROR("%s", "cannot set stream's ID"); + _PERROR("%s", "cannot set stream class's ID"); BT_PUT(new_stream_class); goto error; } @@ -3401,14 +3428,24 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) g_hash_table_insert(ctx->stream_classes, (gpointer) stream_id, new_stream_class); new_stream_class = NULL; - break; case 1: - /* Single stream: get its ID */ - keys = g_hash_table_get_keys(ctx->stream_classes); - stream_id = (int64_t) keys->data; - g_list_free(keys); - keys = NULL; + /* Single stream class: get its ID */ + if (g_hash_table_size(ctx->stream_classes) == 1) { + keys = g_hash_table_get_keys(ctx->stream_classes); + stream_id = (int64_t) keys->data; + g_list_free(keys); + } else { + assert(bt_ctf_trace_get_stream_class_count( + ctx->trace) == 1); + stream_class = + bt_ctf_trace_get_stream_class_by_index( + ctx->trace, 0); + assert(stream_class); + stream_id = bt_ctf_stream_class_get_id( + stream_class); + BT_PUT(stream_class); + } break; default: _PERROR("%s", "missing \"stream_id\" attribute in event declaration"); @@ -3417,20 +3454,25 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) } } - - assert(stream_id >= 0); - /* We have the stream ID now; borrow the stream class if found */ + /* We have the stream ID now; get the stream class if found */ stream_class = g_hash_table_lookup(ctx->stream_classes, (gpointer) stream_id); + bt_get(stream_class); if (!stream_class) { - _PERROR("cannot find stream class with ID %" PRId64, + stream_class = bt_ctf_trace_get_stream_class_by_id(ctx->trace, stream_id); - ret = -EINVAL; - goto error; + if (!stream_class) { + _PERROR("cannot find stream class with ID %" PRId64, + stream_id); + ret = -EINVAL; + goto error; + } } + assert(stream_class); + if (!_IS_SET(&set, _EVENT_ID_SET)) { /* Allow only one event without ID per stream */ if (bt_ctf_stream_class_get_event_class_count(stream_class) != @@ -3460,41 +3502,30 @@ int visit_event_decl(struct ctx *ctx, struct ctf_node *node) event_id); if (eevent_class) { BT_PUT(eevent_class); - _PERROR("%s", "duplicate event with ID %" PRId64 " in same stream"); + _PERROR("duplicate event with ID %" PRId64 " in same stream", event_id); ret = -EEXIST; goto error; } - eevent_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, - event_name); - if (eevent_class) { - BT_PUT(eevent_class); - eevent_class = NULL; - _PERROR("%s", - "duplicate event with name \"%s\" in same stream"); - ret = -EEXIST; - goto error; - } - - g_free(event_name); ret = bt_ctf_stream_class_add_event_class(stream_class, event_class); BT_PUT(event_class); - event_class = NULL; - if (ret) { _PERROR("%s", "cannot add event class to stream class"); goto error; } -end: - return 0; + goto end; error: - g_free(event_name); - BT_PUT(event_class); + bt_put(event_class); - /* stream_class is borrowed; it still belongs to the hash table */ +end: + if (pop_scope) { + ctx_pop_scope(ctx); + } + g_free(event_name); + bt_put(stream_class); return ret; } @@ -3562,7 +3593,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, ret = bt_ctf_stream_class_set_id(stream_class, id); if (ret) { _PERROR("%s", - "cannot set stream declaration's ID"); + "cannot set stream class's ID"); goto error; } @@ -3580,7 +3611,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, struct ctf_node, siblings), &decl); if (ret) { - _PERROR("%s", "cannot create event header declaration"); + _PERROR("%s", "cannot create event header field type"); goto error; } @@ -3590,7 +3621,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, stream_class, decl); BT_PUT(decl); if (ret) { - _PERROR("%s", "cannot set stream's event header declaration"); + _PERROR("%s", "cannot set stream's event header field type"); goto error; } @@ -3608,7 +3639,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, struct ctf_node, siblings), &decl); if (ret) { - _PERROR("%s", "cannot create stream event context declaration"); + _PERROR("%s", "cannot create stream event context field type"); goto error; } @@ -3618,7 +3649,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, stream_class, decl); BT_PUT(decl); if (ret) { - _PERROR("%s", "cannot set stream's event context declaration"); + _PERROR("%s", "cannot set stream's event context field type"); goto error; } @@ -3636,7 +3667,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, struct ctf_node, siblings), &decl); if (ret) { - _PERROR("%s", "cannot create packet context declaration"); + _PERROR("%s", "cannot create packet context field type"); goto error; } @@ -3646,7 +3677,7 @@ int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, stream_class, decl); BT_PUT(decl); if (ret) { - _PERROR("%s", "cannot set stream's packet context declaration"); + _PERROR("%s", "cannot set stream's packet context field type"); goto error; } @@ -3683,6 +3714,7 @@ int visit_stream_decl(struct ctx *ctx, struct ctf_node *node) int ret = 0; struct ctf_node *iter; struct bt_ctf_stream_class *stream_class = NULL; + struct bt_ctf_stream_class *existing_stream_class = NULL; struct bt_list_head *decl_list = &node->u.stream.declaration_list; if (node->visited) { @@ -3721,7 +3753,7 @@ int visit_stream_decl(struct ctx *ctx, struct ctf_node *node) bt_ctf_trace_get_packet_header_type(ctx->trace); if (!packet_header_decl) { _PERROR("%s", - "cannot get trace packet header declaration"); + "cannot get trace packet header field type"); goto error; } @@ -3761,17 +3793,35 @@ int visit_stream_decl(struct ctx *ctx, struct ctf_node *node) goto error; } + /* + * Make sure that this stream class's ID is currently unique in + * the trace. + */ + if (g_hash_table_lookup(ctx->stream_classes, (gpointer) id)) { + _PERROR("a stream class with ID: %" PRId64 " already exists in the trace", id); + ret = -EINVAL; + goto error; + } + + existing_stream_class = bt_ctf_trace_get_stream_class_by_id(ctx->trace, + id); + if (existing_stream_class) { + _PERROR("a stream class with ID: %" PRId64 " already exists in the trace", id); + ret = -EINVAL; + goto error; + } + /* Move reference to visitor's context */ g_hash_table_insert(ctx->stream_classes, (gpointer) (int64_t) id, stream_class); stream_class = NULL; - -end: - return 0; + goto end; error: - BT_PUT(stream_class); + bt_put(stream_class); +end: + bt_put(existing_stream_class); return ret; } @@ -3856,6 +3906,13 @@ int visit_trace_decl_entry(struct ctx *ctx, struct ctf_node *node, int *set) goto error; } + ret = bt_ctf_trace_set_uuid(ctx->trace, ctx->trace_uuid); + if (ret) { + _PERROR("%s", + "cannot set trace's UUID"); + goto error; + } + _SET(set, _TRACE_UUID_SET); } else if (!strcmp(left, "byte_order")) { /* Native byte order is already known at this stage */ @@ -3880,7 +3937,7 @@ int visit_trace_decl_entry(struct ctx *ctx, struct ctf_node *node, int *set) struct ctf_node, siblings), &packet_header_decl); if (ret) { - _PERROR("%s", "cannot create packet header declaration"); + _PERROR("%s", "cannot create packet header field type"); goto error; } @@ -3889,7 +3946,7 @@ int visit_trace_decl_entry(struct ctx *ctx, struct ctf_node *node, int *set) packet_header_decl); BT_PUT(packet_header_decl); if (ret) { - _PERROR("%s", "cannot set trace declaration's packet header declaration"); + _PERROR("%s", "cannot set trace's packet header field type"); goto error; } @@ -4024,6 +4081,12 @@ int visit_env(struct ctx *ctx, struct ctf_node *node) goto error; } + if (strcmp(left, "tracer_name") == 0) { + if (strncmp(right, "lttng", 5) == 0) { + ctx->is_lttng = 1; + } + } + printf_verbose("env.%s = \"%s\"\n", left, right); ret = bt_ctf_trace_set_environment_field_string( ctx->trace, left, right); @@ -4123,7 +4186,8 @@ int set_trace_byte_order(struct ctx *ctx, struct ctf_node *trace_node) goto error; } - ret = bt_ctf_trace_set_byte_order( + ctx->trace_bo = bo; + ret = bt_ctf_trace_set_native_byte_order( ctx->trace, bo); if (ret) { _PERROR("cannot set trace's byte order (%d)", @@ -4173,7 +4237,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, char *right; if (_IS_SET(set, _CLOCK_NAME_SET)) { - _PERROR_DUP_ATTR("name", "clock declaration"); + _PERROR_DUP_ATTR("name", "clock class declaration"); ret = -EPERM; goto error; } @@ -4181,14 +4245,14 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, right = concatenate_unary_strings( &entry_node->u.ctf_expression.right); if (!right) { - _PERROR("%s", "unexpected unary expression for clock declaration's \"name\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class declaration's \"name\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_name(clock, right); if (ret) { - _PERROR("%s", "cannot set clock's name"); + _PERROR("%s", "cannot set clock class's name"); g_free(right); goto error; } @@ -4199,20 +4263,20 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, unsigned char uuid[BABELTRACE_UUID_LEN]; if (_IS_SET(set, _CLOCK_UUID_SET)) { - _PERROR_DUP_ATTR("uuid", "clock declaration"); + _PERROR_DUP_ATTR("uuid", "clock class declaration"); ret = -EPERM; goto error; } ret = get_unary_uuid(&entry_node->u.ctf_expression.right, uuid); if (ret) { - _PERROR("%s", "invalid clock UUID"); + _PERROR("%s", "invalid clock class UUID"); goto error; } ret = bt_ctf_clock_class_set_uuid(clock, uuid); if (ret) { - _PERROR("%s", "cannot set clock's UUID"); + _PERROR("%s", "cannot set clock class's UUID"); goto error; } @@ -4221,7 +4285,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, char *right; if (_IS_SET(set, _CLOCK_DESCRIPTION_SET)) { - _PERROR_DUP_ATTR("description", "clock declaration"); + _PERROR_DUP_ATTR("description", "clock class declaration"); ret = -EPERM; goto error; } @@ -4229,14 +4293,14 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, right = concatenate_unary_strings( &entry_node->u.ctf_expression.right); if (!right) { - _PERROR("%s", "unexpected unary expression for clock's \"description\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class's \"description\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_description(clock, right); if (ret) { - _PERROR("%s", "cannot set clock's description"); + _PERROR("%s", "cannot set clock class's description"); g_free(right); goto error; } @@ -4247,7 +4311,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, uint64_t freq; if (_IS_SET(set, _CLOCK_FREQ_SET)) { - _PERROR_DUP_ATTR("freq", "clock declaration"); + _PERROR_DUP_ATTR("freq", "clock class declaration"); ret = -EPERM; goto error; } @@ -4255,14 +4319,14 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, ret = get_unary_unsigned( &entry_node->u.ctf_expression.right, &freq); if (ret) { - _PERROR("%s", "unexpected unary expression for clock declaration's \"freq\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class declaration's \"freq\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_frequency(clock, freq); if (ret) { - _PERROR("%s", "cannot set clock's frequency"); + _PERROR("%s", "cannot set clock class's frequency"); goto error; } @@ -4271,7 +4335,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, uint64_t precision; if (_IS_SET(set, _CLOCK_PRECISION_SET)) { - _PERROR_DUP_ATTR("precision", "clock declaration"); + _PERROR_DUP_ATTR("precision", "clock class declaration"); ret = -EPERM; goto error; } @@ -4279,14 +4343,14 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, ret = get_unary_unsigned( &entry_node->u.ctf_expression.right, &precision); if (ret) { - _PERROR("%s", "unexpected unary expression for clock declaration's \"precision\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class declaration's \"precision\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_precision(clock, precision); if (ret) { - _PERROR("%s", "cannot set clock's precision"); + _PERROR("%s", "cannot set clock class's precision"); goto error; } @@ -4295,7 +4359,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, uint64_t offset_s; if (_IS_SET(set, _CLOCK_OFFSET_S_SET)) { - _PERROR_DUP_ATTR("offset_s", "clock declaration"); + _PERROR_DUP_ATTR("offset_s", "clock class declaration"); ret = -EPERM; goto error; } @@ -4303,14 +4367,14 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, ret = get_unary_unsigned( &entry_node->u.ctf_expression.right, &offset_s); if (ret) { - _PERROR("%s", "unexpected unary expression for clock declaration's \"offset_s\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset_s\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_offset_s(clock, offset_s); if (ret) { - _PERROR("%s", "cannot set clock's offset in seconds"); + _PERROR("%s", "cannot set clock class's offset in seconds"); goto error; } @@ -4319,7 +4383,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, uint64_t offset; if (_IS_SET(set, _CLOCK_OFFSET_SET)) { - _PERROR_DUP_ATTR("offset", "clock declaration"); + _PERROR_DUP_ATTR("offset", "clock class declaration"); ret = -EPERM; goto error; } @@ -4327,14 +4391,14 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, ret = get_unary_unsigned( &entry_node->u.ctf_expression.right, &offset); if (ret) { - _PERROR("%s", "unexpected unary expression for clock declaration's \"offset\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_offset_cycles(clock, offset); if (ret) { - _PERROR("%s", "cannot set clock's offset in cycles"); + _PERROR("%s", "cannot set clock class's offset in cycles"); goto error; } @@ -4343,7 +4407,7 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, struct ctf_node *right; if (_IS_SET(set, _CLOCK_ABSOLUTE_SET)) { - _PERROR_DUP_ATTR("absolute", "clock declaration"); + _PERROR_DUP_ATTR("absolute", "clock class declaration"); ret = -EPERM; goto error; } @@ -4353,20 +4417,20 @@ int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, struct ctf_node, siblings); ret = get_boolean(ctx->efd, right); if (ret < 0) { - _PERROR("%s", "unexpected unary expression for clock declaration's \"absolute\" attribute"); + _PERROR("%s", "unexpected unary expression for clock class declaration's \"absolute\" attribute"); ret = -EINVAL; goto error; } ret = bt_ctf_clock_class_set_is_absolute(clock, ret); if (ret) { - _PERROR("%s", "cannot set clock's absolute option"); + _PERROR("%s", "cannot set clock class's absolute option"); goto error; } _SET(set, _CLOCK_ABSOLUTE_SET); } else { - _PWARNING("unknown attribute \"%s\" in clock declaration", + _PWARNING("unknown attribute \"%s\" in clock class declaration", left); } @@ -4381,6 +4445,49 @@ error: return ret; } +static +uint64_t cycles_from_ns(uint64_t frequency, uint64_t ns) +{ + uint64_t cycles; + + /* 1GHz */ + if (frequency == 1000000000ULL) { + cycles = ns; + } else { + cycles = (uint64_t) (((double) ns * (double) frequency) / 1e9); + } + + return cycles; +} + +static +int apply_clock_class_offset(struct ctx *ctx, struct bt_ctf_clock_class *clock) +{ + int ret; + uint64_t freq; + int64_t offset_cycles; + + freq = bt_ctf_clock_class_get_frequency(clock); + if (freq == -1ULL) { + _PERROR("%s", "cannot get clock class frequency"); + ret = -1; + goto end; + } + + ret = bt_ctf_clock_class_get_offset_cycles(clock, &offset_cycles); + if (ret) { + _PERROR("%s", "cannot get offset in cycles"); + ret = -1; + goto end; + } + + offset_cycles += cycles_from_ns(freq, ctx->clock_class_offset_ns); + ret = bt_ctf_clock_class_set_offset_cycles(clock, offset_cycles); + +end: + return ret; +} + static int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node) { @@ -4389,6 +4496,7 @@ int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node) struct bt_ctf_clock_class *clock; struct ctf_node *entry_node; struct bt_list_head *decl_list = &clock_node->u.clock.declaration_list; + const char *clock_class_name; if (clock_node->visited) { return 0; @@ -4411,20 +4519,36 @@ int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node) if (!_IS_SET(&set, _CLOCK_NAME_SET)) { _PERROR("%s", - "missing \"name\" attribute in clock declaration"); + "missing \"name\" attribute in clock class declaration"); ret = -EPERM; goto error; } - if (bt_ctf_trace_get_clock_class_count(ctx->trace) != 0) { - _PERROR("%s", "only CTF traces with a single clock declaration are supported as of this version"); - ret = -EINVAL; + clock_class_name = bt_ctf_clock_class_get_name(clock); + assert(clock_class_name); + if (ctx->is_lttng && strcmp(clock_class_name, "monotonic") == 0) { + /* + * Old versions of LTTng forgot to set its clock class + * as absolute, even if it is. This is important because + * it's a condition to be able to sort notifications + * from different sources. + */ + ret = bt_ctf_clock_class_set_is_absolute(clock, 1); + if (ret) { + _PERROR("%s", "cannot set clock class's absolute option"); + goto error; + } + } + + ret = apply_clock_class_offset(ctx, clock); + if (ret) { + _PERROR("%s", "cannot apply clock class offset "); goto error; } ret = bt_ctf_trace_add_clock_class(ctx->trace, clock); if (ret) { - _PERROR("%s", "cannot add clock to trace"); + _PERROR("%s", "cannot add clock class to trace"); goto error; } @@ -4490,9 +4614,9 @@ end: } static -int add_stream_classes_to_trace(struct ctx *ctx) +int move_ctx_stream_classes_to_trace(struct ctx *ctx) { - int ret; + int ret = 0; GHashTableIter iter; gpointer key, stream_class; @@ -4509,41 +4633,85 @@ int add_stream_classes_to_trace(struct ctx *ctx) } } + g_hash_table_remove_all(ctx->stream_classes); + end: return ret; } -int ctf_visitor_generate_ir(FILE *efd, struct ctf_node *node, - struct bt_ctf_trace **trace) +BT_HIDDEN +struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create(FILE *efd, + uint64_t clock_class_offset_ns, const char *name) { - int ret = 0; + int ret; struct ctx *ctx = NULL; + struct bt_ctf_trace *trace; - printf_verbose("CTF visitor: AST -> CTF IR...\n"); - - *trace = bt_ctf_trace_create(); - if (!*trace) { + trace = bt_ctf_trace_create(); + if (!trace) { _FPERROR(efd, "%s", "cannot create trace"); - ret = -ENOMEM; + goto error; + } + + ret = bt_ctf_trace_set_name(trace, name); + if (ret) { + _FPERROR(efd, "cannot set trace's name to `%s`", name); goto error; } /* Set packet header to NULL to override the default one */ - ret = bt_ctf_trace_set_packet_header_type(*trace, NULL); + ret = bt_ctf_trace_set_packet_header_type(trace, NULL); if (ret) { - _FPERROR(efd, - "%s", + _FPERROR(efd, "%s", "cannot set initial, empty packet header structure"); goto error; } - ctx = ctx_create(*trace, efd); + /* Create visitor's context */ + ctx = ctx_create(trace, efd, clock_class_offset_ns); if (!ctx) { _FPERROR(efd, "%s", "cannot create visitor context"); - ret = -ENOMEM; goto error; } + trace = NULL; + goto end; + +error: + ctx_destroy(ctx); + ctx = NULL; + +end: + bt_put(trace); + return (void *) ctx; +} + +BT_HIDDEN +void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor) +{ + ctx_destroy((void *) visitor); +} + +BT_HIDDEN +struct bt_ctf_trace *ctf_visitor_generate_ir_get_trace( + struct ctf_visitor_generate_ir *visitor) +{ + struct ctx *ctx = (void *) visitor; + + assert(ctx); + assert(ctx->trace); + return bt_get(ctx->trace); +} + +BT_HIDDEN +int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, + struct ctf_node *node) +{ + int ret = 0; + struct ctx *ctx = (void *) visitor; + + printf_verbose("CTF visitor: AST -> CTF IR...\n"); + switch (node->type) { case NODE_ROOT: { @@ -4552,44 +4720,69 @@ int ctf_visitor_generate_ir(FILE *efd, struct ctf_node *node, int found_callsite = FALSE; /* - * Find trace declaration's byte order first (for early - * type aliases). + * The first thing we need is the native byte order of + * the trace block, because early type aliases can have + * a `byte_order` attribute set to `native`. If we don't + * have the native byte order yet, and we don't have any + * trace block yet, then fail with EINCOMPLETE. */ - bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { - if (got_trace_decl) { - _PERROR("%s", "duplicate trace declaration"); - goto error; + if (ctx->trace_bo == BT_CTF_BYTE_ORDER_NATIVE) { + bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { + if (got_trace_decl) { + _PERROR("%s", "duplicate trace declaration"); + ret = -1; + goto end; + } + + ret = set_trace_byte_order(ctx, iter); + if (ret) { + _PERROR("cannot set trace's native byte order (%d)", + ret); + goto end; + } + + got_trace_decl = TRUE; + } + + if (!got_trace_decl) { + ret = -EINCOMPLETE; + goto end; } + } - ret = set_trace_byte_order(ctx, iter); + assert(ctx->trace_bo == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || + ctx->trace_bo == BT_CTF_BYTE_ORDER_BIG_ENDIAN); + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); + + /* Environment */ + bt_list_for_each_entry(iter, &node->u.root.env, siblings) { + ret = visit_env(ctx, iter); if (ret) { - _PERROR("cannot set trace's byte order (%d)", + _PERROR("error while visiting environment block (%d)", ret); - goto error; + goto end; } - - got_trace_decl = TRUE; } - if (!got_trace_decl) { - _PERROR("no trace declaration found (%d)", ret); - ret = -EPERM; - goto error; - } + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); /* - * Visit clocks first since any early integer can be mapped - * to one. + * Visit clock blocks. */ bt_list_for_each_entry(iter, &node->u.root.clock, siblings) { ret = visit_clock_decl(ctx, iter); if (ret) { - _PERROR("error while visiting clock declaration (%d)", + _PERROR("error while visiting clock class declaration (%d)", ret); - goto error; + goto end; } } + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); + /* * Visit root declarations next, as they can be used by any * following entity. @@ -4600,79 +4793,75 @@ int ctf_visitor_generate_ir(FILE *efd, struct ctf_node *node, if (ret) { _PERROR("error while visiting root declaration (%d)", ret); - goto error; + goto end; } } - /* Callsite are not supported */ + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); + + /* Callsite blocks are not supported */ bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) { found_callsite = TRUE; break; } + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); + if (found_callsite) { _PWARNING("%s", "\"callsite\" blocks are not supported as of this version"); } - /* Environment */ - bt_list_for_each_entry(iter, &node->u.root.env, siblings) { - ret = visit_env(ctx, iter); - if (ret) { - _PERROR("error while visiting environment block (%d)", - ret); - goto error; - } - } - /* Trace */ bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { ret = visit_trace_decl(ctx, iter); if (ret) { _PERROR("%s", "error while visiting trace declaration"); - goto error; + goto end; } } + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); + /* Streams */ bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { ret = visit_stream_decl(ctx, iter); if (ret) { _PERROR("%s", "error while visiting stream declaration"); - goto error; + goto end; } } + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); + /* Events */ bt_list_for_each_entry(iter, &node->u.root.event, siblings) { ret = visit_event_decl(ctx, iter); if (ret) { _PERROR("%s", "error while visiting event declaration"); - goto error; + goto end; } } + + assert(ctx->current_scope && + ctx->current_scope->parent_scope == NULL); break; } - case NODE_UNKNOWN: default: - _PERROR("unknown node type: %d", (int) node->type); + _PERROR("cannot decode node type: %d", (int) node->type); ret = -EINVAL; - goto error; + goto end; } - /* Add stream classes to trace now */ - ret = add_stream_classes_to_trace(ctx); + /* Move decoded stream classes to trace, if any */ + ret = move_ctx_stream_classes_to_trace(ctx); if (ret) { - _PERROR("%s", "cannot add stream classes to trace"); + _PERROR("%s", "cannot move stream classes to trace"); } - ctx_destroy(ctx); - printf_verbose("done!\n"); - - return ret; - -error: - ctx_destroy(ctx); - BT_PUT(*trace); - +end: return ret; }