Fix: add_clock_classes(): do not add existing clock classes
[babeltrace.git] / plugins / lttng-utils / copy.c
index b71d8c9bb2fb6493d690f7f2cb88f1a09de67e23..52d5b4899b7b3f63abaa10ba1da4e4963809eb11 100644 (file)
 #include <ctfcopytrace.h>
 #include "debug-info.h"
 
+static
+struct bt_ctf_stream *insert_new_stream(
+               struct debug_info_iterator *debug_it,
+               struct bt_ctf_stream *stream,
+               struct debug_info_trace *di_trace);
+
 static
 void unref_stream(struct bt_ctf_stream *stream)
 {
@@ -561,11 +567,13 @@ enum debug_info_stream_state *insert_new_stream_state(
        if (!v) {
                fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
+               goto end;
        }
        *v = DEBUG_INFO_UNKNOWN_STREAM;
 
        g_hash_table_insert(di_trace->stream_states, stream, v);
 
+end:
        return v;
 }
 
@@ -590,7 +598,7 @@ BT_HIDDEN
 void debug_info_close_trace(struct debug_info_iterator *debug_it,
                struct debug_info_trace *di_trace)
 {
-       if (di_trace->static_listener_id > 0) {
+       if (di_trace->static_listener_id >= 0) {
                bt_ctf_trace_remove_is_static_listener(di_trace->trace,
                                di_trace->static_listener_id);
        }
@@ -621,12 +629,89 @@ void debug_info_close_trace(struct debug_info_iterator *debug_it,
        g_hash_table_destroy(di_trace->trace_debug_map);
 }
 
+static
+int sync_event_classes(struct debug_info_iterator *debug_it,
+               struct bt_ctf_stream *stream,
+               struct bt_ctf_stream *writer_stream)
+{
+       int int_ret;
+       struct bt_ctf_stream_class *stream_class = NULL,
+                                  *writer_stream_class = NULL;
+       enum bt_component_status ret;
+
+       stream_class = bt_ctf_stream_get_class(stream);
+       if (!stream_class) {
+               fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
+       }
+
+       writer_stream_class = bt_ctf_stream_get_class(writer_stream);
+       if (!writer_stream_class) {
+               fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
+       }
+
+       ret = ctf_copy_event_classes(debug_it->err, stream_class,
+                       writer_stream_class);
+       if (ret != BT_COMPONENT_STATUS_OK) {
+               goto error;
+       }
+
+       int_ret = 0;
+       goto end;
+
+error:
+       int_ret = -1;
+end:
+       bt_put(stream_class);
+       bt_put(writer_stream_class);
+       return int_ret;
+}
+
 static
 void trace_is_static_listener(struct bt_ctf_trace *trace, void *data)
 {
        struct debug_info_trace *di_trace = data;
-       int trace_completed = 1;
+       struct debug_info_iterator *debug_it = di_trace->debug_it;
+       int trace_completed = 1, ret, nr_stream, i;
+       struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
+       struct bt_ctf_trace *writer_trace = di_trace->writer_trace;
+
+       /*
+        * When the trace becomes static, make sure that we have all
+        * the event classes in our stream_class copies before setting it
+        * static as well.
+        */
+       nr_stream = bt_ctf_trace_get_stream_count(trace);
+       for (i = 0; i < nr_stream; i++) {
+               stream = bt_ctf_trace_get_stream_by_index(trace, i);
+               if (!stream) {
+                       fprintf(debug_it->err,
+                                       "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
+               writer_stream = bt_ctf_trace_get_stream_by_index(writer_trace, i);
+               if (!writer_stream) {
+                       fprintf(debug_it->err,
+                                       "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
+               ret = sync_event_classes(di_trace->debug_it, stream, writer_stream);
+               if (ret) {
+                       fprintf(debug_it->err,
+                                       "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
+               BT_PUT(stream);
+               BT_PUT(writer_stream);
+       }
 
+       bt_ctf_trace_set_is_static(di_trace->writer_trace);
        di_trace->trace_static = 1;
 
        g_hash_table_foreach(di_trace->stream_states,
@@ -636,6 +721,10 @@ void trace_is_static_listener(struct bt_ctf_trace *trace, void *data)
                g_hash_table_remove(di_trace->debug_it->trace_map,
                                di_trace->trace);
        }
+
+error:
+       bt_put(writer_stream);
+       bt_put(stream);
 }
 
 static
@@ -645,6 +734,7 @@ struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it,
        struct debug_info_trace *di_trace = NULL;
        struct bt_ctf_trace *trace = NULL;
        struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_stream *writer_stream = NULL;
        int ret, nr_stream, i;
 
        writer_trace = bt_ctf_trace_create();
@@ -696,6 +786,7 @@ struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it,
                        g_direct_equal, NULL, (GDestroyNotify) unref_debug_info);
        di_trace->stream_states = g_hash_table_new_full(g_direct_hash,
                        g_direct_equal, NULL, destroy_stream_state_key);
+       g_hash_table_insert(debug_it->trace_map, (gpointer) trace, di_trace);
 
        /* Set all the existing streams in the unknown state. */
        nr_stream = bt_ctf_trace_get_stream_count(trace);
@@ -708,6 +799,22 @@ struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it,
                        goto error;
                }
                insert_new_stream_state(debug_it, di_trace, stream);
+               writer_stream = insert_new_stream(debug_it, stream, di_trace);
+               if (!writer_stream) {
+                       fprintf(debug_it->err,
+                                       "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
+               bt_get(writer_stream);
+               ret = sync_event_classes(debug_it, stream, writer_stream);
+               if (ret) {
+                       fprintf(debug_it->err,
+                                       "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
+               BT_PUT(writer_stream);
                BT_PUT(stream);
        }
 
@@ -715,6 +822,7 @@ struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it,
        if (bt_ctf_trace_is_static(trace)) {
                di_trace->trace_static = 1;
                di_trace->static_listener_id = -1;
+               bt_ctf_trace_set_is_static(writer_trace);
        } else {
                ret = bt_ctf_trace_add_is_static_listener(trace,
                                trace_is_static_listener, di_trace);
@@ -727,7 +835,6 @@ struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it,
                di_trace->static_listener_id = ret;
        }
 
-       g_hash_table_insert(debug_it->trace_map, (gpointer) trace, di_trace);
 
        goto end;
 
@@ -736,6 +843,8 @@ error:
        g_free(di_trace);
        di_trace = NULL;
 end:
+       bt_put(stream);
+       bt_put(writer_stream);
        bt_put(stream_class);
        bt_put(trace);
        return di_trace;
@@ -758,15 +867,28 @@ struct bt_ctf_packet *insert_new_packet(struct debug_info_iterator *debug_it,
                struct debug_info_trace *di_trace)
 {
        struct bt_ctf_packet *writer_packet;
+       int ret;
 
        writer_packet = bt_ctf_packet_create(writer_stream);
        if (!writer_packet) {
                fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
-       g_hash_table_insert(di_trace->packet_map, (gpointer) packet, writer_packet);
 
+       ret = ctf_packet_copy_header(debug_it->err, packet, writer_packet);
+       if (ret) {
+               fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
+       }
+
+       g_hash_table_insert(di_trace->packet_map, (gpointer) packet,
+                       writer_packet);
+       goto end;
+
+error:
+       BT_PUT(writer_packet);
 end:
        return writer_packet;
 }
@@ -923,11 +1045,7 @@ struct bt_ctf_stream_class *copy_stream_class_debug_info(FILE *err,
        int ret_int;
        const char *name = bt_ctf_stream_class_get_name(stream_class);
 
-       if (strlen(name) == 0) {
-               name = NULL;
-       }
-
-       writer_stream_class = bt_ctf_stream_class_create(name);
+       writer_stream_class = bt_ctf_stream_class_create_empty(name);
        if (!writer_stream_class) {
                fprintf(err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
@@ -935,36 +1053,28 @@ struct bt_ctf_stream_class *copy_stream_class_debug_info(FILE *err,
        }
 
        type = bt_ctf_stream_class_get_packet_context_type(stream_class);
-       if (!type) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto error;
-       }
-
-       ret_int = bt_ctf_stream_class_set_packet_context_type(
-                       writer_stream_class, type);
-       if (ret_int < 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto error;
+       if (type) {
+               ret_int = bt_ctf_stream_class_set_packet_context_type(
+                               writer_stream_class, type);
+               if (ret_int < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
+                       goto error;
+               }
+               BT_PUT(type);
        }
-       BT_PUT(type);
 
        type = bt_ctf_stream_class_get_event_header_type(stream_class);
-       if (!type) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto error;
-       }
-
-       ret_int = bt_ctf_stream_class_set_event_header_type(
-                       writer_stream_class, type);
-       if (ret_int < 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto error;
+       if (type) {
+               ret_int = bt_ctf_stream_class_set_event_header_type(
+                               writer_stream_class, type);
+               if (ret_int < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
+                       goto error;
+               }
+               BT_PUT(type);
        }
-       BT_PUT(type);
 
        type = bt_ctf_stream_class_get_event_context_type(stream_class);
        if (type) {
@@ -1020,6 +1130,7 @@ int add_clock_classes(FILE *err, struct bt_ctf_trace *writer_trace,
        for (i = 0; i < clock_class_count; i++) {
                struct bt_ctf_clock_class *clock_class =
                        bt_ctf_trace_get_clock_class_by_index(trace, i);
+               struct bt_ctf_clock_class *existing_clock_class = NULL;
 
                if (!clock_class) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
@@ -1027,6 +1138,14 @@ int add_clock_classes(FILE *err, struct bt_ctf_trace *writer_trace,
                        goto error;
                }
 
+               existing_clock_class = bt_ctf_trace_get_clock_class_by_name(
+                       writer_trace, bt_ctf_clock_class_get_name(clock_class));
+               bt_put(existing_clock_class);
+               if (existing_clock_class) {
+                       bt_put(clock_class);
+                       continue;
+               }
+
                ret = bt_ctf_trace_add_clock_class(writer_trace, clock_class);
                BT_PUT(clock_class);
                if (ret != 0) {
@@ -1263,8 +1382,8 @@ struct bt_ctf_packet *debug_info_new_packet(
                struct bt_ctf_packet *packet)
 {
        struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
-       struct bt_ctf_field *writer_packet_context = NULL;
        struct bt_ctf_packet *writer_packet = NULL;
+       struct bt_ctf_field *packet_context = NULL;
        struct debug_info_trace *di_trace;
        int int_ret;
 
@@ -1307,19 +1426,16 @@ struct bt_ctf_packet *debug_info_new_packet(
                goto error;
        }
 
-       writer_packet_context = ctf_copy_packet_context(debug_it->err, packet,
-                       writer_stream);
-       if (!writer_packet_context) {
-               fprintf(debug_it->err, "[error] %s in %s:%d\n",
-                               __func__, __FILE__, __LINE__);
-               goto error;
-       }
-
-       int_ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context);
-       if (int_ret) {
-               fprintf(debug_it->err, "[error] %s in %s:%d\n",
-                               __func__, __FILE__, __LINE__);
-               goto error;
+       packet_context = bt_ctf_packet_get_context(packet);
+       if (packet_context) {
+               int_ret = ctf_packet_copy_context(debug_it->err,
+                               packet, writer_stream, writer_packet);
+               if (int_ret < 0) {
+                       fprintf(debug_it->err, "[error] %s in %s:%d\n",
+                                       __func__, __FILE__, __LINE__);
+                       goto error;
+               }
+               BT_PUT(packet_context);
        }
 
        bt_get(writer_packet);
@@ -1328,7 +1444,7 @@ struct bt_ctf_packet *debug_info_new_packet(
 error:
 
 end:
-       bt_put(writer_packet_context);
+       bt_put(packet_context);
        bt_put(writer_stream);
        bt_put(stream);
        return writer_packet;
@@ -1376,7 +1492,7 @@ struct bt_ctf_stream *debug_info_stream_begin(
                struct debug_info_iterator *debug_it,
                struct bt_ctf_stream *stream)
 {
-       struct bt_ctf_stream *writer_stream;
+       struct bt_ctf_stream *writer_stream = NULL;
        enum debug_info_stream_state *state;
        struct debug_info_trace *di_trace = NULL;
 
@@ -1390,13 +1506,6 @@ struct bt_ctf_stream *debug_info_stream_begin(
                }
        }
 
-       writer_stream = lookup_stream(debug_it, stream, di_trace);
-       if (writer_stream) {
-               fprintf(debug_it->err, "[error] %s in %s:%d\n",
-                               __func__, __FILE__, __LINE__);
-               goto error;
-       }
-
        /* Set the stream as active */
        state = g_hash_table_lookup(di_trace->stream_states, stream);
        if (!state) {
@@ -1407,6 +1516,11 @@ struct bt_ctf_stream *debug_info_stream_begin(
                }
                state = insert_new_stream_state(debug_it, di_trace,
                                stream);
+               if (!state) {
+                       fprintf(debug_it->err, "[error] Adding a new stream "
+                                       "on a static trace\n");
+                       goto error;
+               }
        }
        if (*state != DEBUG_INFO_UNKNOWN_STREAM) {
                fprintf(debug_it->err, "[error] Unexpected stream state %d\n",
@@ -1415,7 +1529,10 @@ struct bt_ctf_stream *debug_info_stream_begin(
        }
        *state = DEBUG_INFO_ACTIVE_STREAM;
 
-       writer_stream = insert_new_stream(debug_it, stream, di_trace);
+       writer_stream = lookup_stream(debug_it, stream, di_trace);
+       if (!writer_stream) {
+               writer_stream = insert_new_stream(debug_it, stream, di_trace);
+       }
        bt_get(writer_stream);
 
        goto end;
@@ -1430,7 +1547,7 @@ BT_HIDDEN
 struct bt_ctf_stream *debug_info_stream_end(struct debug_info_iterator *debug_it,
                struct bt_ctf_stream *stream)
 {
-       struct bt_ctf_stream *writer_stream;
+       struct bt_ctf_stream *writer_stream = NULL;
        struct debug_info_trace *di_trace = NULL;
        enum debug_info_stream_state *state;
 
@@ -1699,7 +1816,8 @@ int copy_set_debug_info_stream_event_context(FILE *err,
                                goto error;
                        }
 
-                       ret = bt_ctf_field_structure_set_field(writer_event_context,
+                       ret = bt_ctf_field_structure_set_field_by_name(
+                                       writer_event_context,
                                        field_name, copy_field);
                        if (ret) {
                                fprintf(err, "[error] %s in %s:%d\n", __func__,
@@ -1742,6 +1860,11 @@ struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err,
                goto end;
        }
 
+       if (!bt_ctf_trace_get_clock_class_count(trace)) {
+               /* No clock. */
+               goto end;
+       }
+
        /* FIXME multi-clock? */
        clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0);
 
@@ -1789,13 +1912,12 @@ int set_event_clock_value(FILE *err, struct bt_ctf_event *event,
 {
        struct bt_ctf_clock_class *clock_class = NULL;
        struct bt_ctf_clock_value *clock_value = NULL;
-       int ret;
+       int ret = 0;
 
        clock_class = event_get_clock_class(err, event);
        if (!clock_class) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto error;
+               /* No clock on input trace. */
+               goto end;
        }
 
        clock_value = bt_ctf_event_get_clock_value(event, clock_class);
This page took 0.031095 seconds and 4 git commands to generate.