ir: trace: pass remove listeners when adding listeners
[babeltrace.git] / lib / ctf-ir / trace.c
index 37fa2763f93859fb265cabdfb15c40ee50c6428b..9482e097fd56ee9a7cf986a6c9e0c55df9a2f8ad 100644 (file)
@@ -64,6 +64,7 @@ struct listener_wrapper {
 
 struct bt_ctf_trace_is_static_listener_elem {
        bt_ctf_trace_is_static_listener func;
+       bt_ctf_trace_listener_removed removed;
        void *data;
 };
 
@@ -101,7 +102,7 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
        }
 
        BT_LOGD_STR("Creating trace object.");
-       trace->native_byte_order = BT_CTF_BYTE_ORDER_NONE;
+       trace->native_byte_order = BT_CTF_BYTE_ORDER_UNSPECIFIED;
        bt_object_init(trace, bt_ctf_trace_destroy);
        trace->clocks = g_ptr_array_new_with_free_func(
                (GDestroyNotify) bt_put);
@@ -273,6 +274,30 @@ void bt_ctf_trace_destroy(struct bt_object *obj)
        BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
                trace, bt_ctf_trace_get_name(trace));
 
+       /*
+        * Call remove listeners first so that everything else still
+        * exists in the trace.
+        */
+       if (trace->is_static_listeners) {
+               size_t i;
+
+               for (i = 0; i < trace->is_static_listeners->len; i++) {
+                       struct bt_ctf_trace_is_static_listener_elem elem =
+                               g_array_index(trace->is_static_listeners,
+                                       struct bt_ctf_trace_is_static_listener_elem, i);
+
+                       if (elem.removed) {
+                               elem.removed(trace, elem.data);
+                       }
+               }
+
+               g_array_free(trace->is_static_listeners, TRUE);
+       }
+
+       if (trace->listeners) {
+               g_ptr_array_free(trace->listeners, TRUE);
+       }
+
        if (trace->environment) {
                BT_LOGD_STR("Destroying environment attributes.");
                bt_ctf_attributes_destroy(trace->environment);
@@ -297,14 +322,6 @@ void bt_ctf_trace_destroy(struct bt_object *obj)
                g_ptr_array_free(trace->stream_classes, TRUE);
        }
 
-       if (trace->listeners) {
-               g_ptr_array_free(trace->listeners, TRUE);
-       }
-
-       if (trace->is_static_listeners) {
-               g_array_free(trace->is_static_listeners, TRUE);
-       }
-
        BT_LOGD_STR("Putting packet header field type.");
        bt_put(trace->packet_header_type);
        g_free(trace);
@@ -793,6 +810,30 @@ bool packet_header_field_type_is_valid(struct bt_ctf_trace *trace,
                BT_PUT(field_type);
        }
 
+       /*
+        * If there's a `packet_seq_num` field, it must be an unsigned
+        * integer field type.
+        */
+       field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "packet_seq_num");
+       if (field_type) {
+               if (!bt_ctf_field_type_is_integer(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
+                               "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
+                               field_type,
+                               bt_ctf_field_type_id_string(field_type->id));
+                       goto invalid;
+               }
+
+               if (bt_ctf_field_type_integer_is_signed(field_type)) {
+                       BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
+                               "packet-seq-num-ft-addr=%p", field_type);
+                       goto invalid;
+               }
+
+               BT_PUT(field_type);
+       }
+
        goto end;
 
 invalid:
@@ -1641,8 +1682,8 @@ int append_trace_metadata(struct bt_ctf_trace *trace,
        int ret = 0;
 
        if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE ||
-                       trace->native_byte_order == BT_CTF_BYTE_ORDER_NONE) {
-               BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_NONE at this point; "
+                       trace->native_byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
+               BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
                        "set it with bt_ctf_trace_set_native_byte_order(): "
                        "addr=%p, name=\"%s\"",
                        trace, bt_ctf_trace_get_name(trace));
@@ -1845,8 +1886,8 @@ int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
        }
 
        if (trace->is_created_by_writer &&
-                       byte_order == BT_CTF_BYTE_ORDER_NONE) {
-               BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_NONE byte order is not allowed for a CTF writer trace: "
+                       byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
+               BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
                        "addr=%p, name=\"%s\"",
                        trace, bt_ctf_trace_get_name(trace));
                ret = -1;
@@ -2142,11 +2183,13 @@ end:
 }
 
 int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace *trace,
-               bt_ctf_trace_is_static_listener listener, void *data)
+               bt_ctf_trace_is_static_listener listener,
+               bt_ctf_trace_listener_removed listener_removed, void *data)
 {
        int i;
        struct bt_ctf_trace_is_static_listener_elem new_elem = {
                .func = listener,
+               .removed = listener_removed,
                .data = data,
        };
 
@@ -2170,6 +2213,14 @@ int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace *trace,
                goto end;
        }
 
+       if (trace->in_remove_listener) {
+               BT_LOGW("Cannot call this function during the execution of a remove listener: "
+                       "addr=%p, name=\"%s\"",
+                       trace, bt_ctf_trace_get_name(trace));
+               i = -1;
+               goto end;
+       }
+
        /* Find the next available spot */
        for (i = 0; i < trace->is_static_listeners->len; i++) {
                struct bt_ctf_trace_is_static_listener_elem elem =
@@ -2208,6 +2259,15 @@ int bt_ctf_trace_remove_is_static_listener(
                goto end;
        }
 
+       if (trace->in_remove_listener) {
+               BT_LOGW("Cannot call this function during the execution of a remove listener: "
+                       "addr=%p, name=\"%s\", listener-id=%d",
+                       trace, bt_ctf_trace_get_name(trace),
+                       listener_id);
+               ret = -1;
+               goto end;
+       }
+
        if (listener_id < 0) {
                BT_LOGW("Invalid listener ID: must be zero or positive: "
                        "listener-id=%d", listener_id);
@@ -2236,7 +2296,19 @@ int bt_ctf_trace_remove_is_static_listener(
                goto end;
        }
 
+       if (elem->removed) {
+               /* Call remove listener */
+               BT_LOGV("Calling remove listener: "
+                       "trace-addr=%p, trace-name=\"%s\", "
+                       "listener-id=%d", trace, bt_ctf_trace_get_name(trace),
+                       listener_id);
+               trace->in_remove_listener = BT_TRUE;
+               elem->removed(trace, elem->data);
+               trace->in_remove_listener = BT_FALSE;
+       }
+
        elem->func = NULL;
+       elem->removed = NULL;
        elem->data = NULL;
        BT_LOGV("Removed \"trace is static\" listener: "
                "trace-addr=%p, trace-name=\"%s\", "
This page took 0.030951 seconds and 4 git commands to generate.