src.ctf.lttng-live: make `lttng_live_attach_session()` return status
[babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.c
index a79d02b10d3f7abb87c462c2dddf764cb6a04e94..fa493cbb3853fc7960764ecaf34ee00f1b33a9ee 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <glib.h>
 #include <inttypes.h>
+#include <stdbool.h>
 #include <unistd.h>
 
 #include "common/assert.h"
@@ -43,6 +44,7 @@
 #include <babeltrace2/types.h>
 
 #include "plugins/common/muxing/muxing.h"
+#include "plugins/common/param-validation/param-validation.h"
 
 #include "data-stream.h"
 #include "metadata.h"
@@ -185,7 +187,7 @@ struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_session *sess
        trace->trace_class = NULL;
        trace->trace = NULL;
        trace->stream_iterators = g_ptr_array_new_with_free_func(
-                       (GDestroyNotify) lttng_live_stream_iterator_destroy);
+               (GDestroyNotify) lttng_live_stream_iterator_destroy);
        BT_ASSERT(trace->stream_iterators);
        trace->new_metadata_needed = true;
        g_ptr_array_add(session->traces, trace);
@@ -236,7 +238,7 @@ int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter,
        session->self_comp = lttng_live_msg_iter->self_comp;
        session->id = session_id;
        session->traces = g_ptr_array_new_with_free_func(
-                       (GDestroyNotify) lttng_live_destroy_trace);
+               (GDestroyNotify) lttng_live_destroy_trace);
        BT_ASSERT(session->traces);
        session->lttng_live_msg_iter = lttng_live_msg_iter;
        session->new_streams_needed = true;
@@ -273,9 +275,8 @@ void lttng_live_destroy_session(struct lttng_live_session *session)
        BT_COMP_LOGD("Destroy lttng live session");
        if (session->id != -1ULL) {
                if (lttng_live_detach_session(session)) {
-                       if (session->lttng_live_msg_iter &&
-                                       !lttng_live_graph_is_canceled(
-                                               session->lttng_live_msg_iter)) {
+                       if (!lttng_live_graph_is_canceled(
+                                       session->lttng_live_msg_iter)) {
                                /* Old relayd cannot detach sessions. */
                                BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64,
                                        session->id);
@@ -377,10 +378,9 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stre
 {
        bt_logging_level log_level = lttng_live_msg_iter->log_level;
        bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
-       enum lttng_live_iterator_status ret =
-                       LTTNG_LIVE_ITERATOR_STATUS_OK;
-       struct packet_index index;
+       enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
        enum lttng_live_stream_state orig_state = lttng_live_stream->state;
+       struct packet_index index;
 
        if (lttng_live_stream->trace->new_metadata_needed) {
                ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
@@ -399,7 +399,7 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stre
        if (ret != LTTNG_LIVE_ITERATOR_STATUS_OK) {
                goto end;
        }
-       BT_ASSERT(lttng_live_stream->state != LTTNG_LIVE_STREAM_EOF);
+       BT_ASSERT_DBG(lttng_live_stream->state != LTTNG_LIVE_STREAM_EOF);
        if (lttng_live_stream->state == LTTNG_LIVE_STREAM_QUIESCENT) {
                uint64_t last_inact_ts = lttng_live_stream->last_inactivity_ts,
                         curr_inact_ts = lttng_live_stream->current_inactivity_ts;
@@ -437,13 +437,12 @@ enum lttng_live_iterator_status lttng_live_get_session(
 {
        enum lttng_live_iterator_status status;
        uint64_t trace_idx;
-       int ret = 0;
 
        if (!session->attached) {
-               ret = lttng_live_attach_session(session);
-               if (ret) {
-                       if (lttng_live_msg_iter && lttng_live_graph_is_canceled(
-                                       lttng_live_msg_iter)) {
+               enum lttng_live_attach_session_status attach_status =
+                       lttng_live_attach_session(session);
+               if (attach_status != LTTNG_LIVE_ATTACH_SESSION_STATUS_OK) {
+                       if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
                                status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
                        } else {
                                status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
@@ -457,13 +456,30 @@ enum lttng_live_iterator_status lttng_live_get_session(
                        status != LTTNG_LIVE_ITERATOR_STATUS_END) {
                goto end;
        }
-       for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) {
+       trace_idx = 0;
+       while (trace_idx < session->traces->len) {
                struct lttng_live_trace *trace =
                        g_ptr_array_index(session->traces, trace_idx);
 
                status = lttng_live_metadata_update(trace);
-               if (status != LTTNG_LIVE_ITERATOR_STATUS_OK &&
-                               status != LTTNG_LIVE_ITERATOR_STATUS_END) {
+               switch (status) {
+               case LTTNG_LIVE_ITERATOR_STATUS_OK:
+                       trace_idx++;
+                       break;
+               case LTTNG_LIVE_ITERATOR_STATUS_END:
+                       /*
+                        * The trace has ended. Remove it of the array an
+                        * continue the iteration.
+                        * We can remove the trace safely when using the
+                        * g_ptr_array_remove_index_fast because it replaces
+                        * the element at trace_idx with the array's last
+                        * element. trace_idx is not incremented because of
+                        * that.
+                        */
+                       (void) g_ptr_array_remove_index_fast(session->traces,
+                               trace_idx);
+                       break;
+               default:
                        goto end;
                }
        }
@@ -510,8 +526,7 @@ enum lttng_live_iterator_status
 lttng_live_iterator_handle_new_streams_and_metadata(
                struct lttng_live_msg_iter *lttng_live_msg_iter)
 {
-       enum lttng_live_iterator_status ret =
-                       LTTNG_LIVE_ITERATOR_STATUS_OK;
+       enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
        uint64_t session_idx = 0, nr_sessions_opened = 0;
        struct lttng_live_session *session;
        enum session_not_found_action sess_not_found_act =
@@ -572,16 +587,14 @@ enum lttng_live_iterator_status emit_inactivity_message(
                struct lttng_live_stream_iterator *stream_iter,
                bt_message **message, uint64_t timestamp)
 {
-       enum lttng_live_iterator_status ret =
-                       LTTNG_LIVE_ITERATOR_STATUS_OK;
+       enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
        bt_message *msg = NULL;
 
        BT_ASSERT(stream_iter->trace->clock_class);
 
        msg = bt_message_message_iterator_inactivity_create(
-                       lttng_live_msg_iter->self_msg_iter,
-                       stream_iter->trace->clock_class,
-                       timestamp);
+               lttng_live_msg_iter->self_msg_iter,
+               stream_iter->trace->clock_class, timestamp);
        if (!msg) {
                goto error;
        }
@@ -602,8 +615,7 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_st
                struct lttng_live_stream_iterator *lttng_live_stream,
                bt_message **message)
 {
-       enum lttng_live_iterator_status ret =
-                       LTTNG_LIVE_ITERATOR_STATUS_OK;
+       enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
 
        if (lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT) {
                return LTTNG_LIVE_ITERATOR_STATUS_OK;
@@ -617,10 +629,10 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_st
        }
 
        ret = emit_inactivity_message(lttng_live_msg_iter, lttng_live_stream,
-                       message, lttng_live_stream->current_inactivity_ts);
+               message, lttng_live_stream->current_inactivity_ts);
 
        lttng_live_stream->last_inactivity_ts =
-                       lttng_live_stream->current_inactivity_ts;
+               lttng_live_stream->current_inactivity_ts;
 end:
        return ret;
 }
@@ -637,8 +649,8 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
        bt_logging_level log_level = lttng_live_msg_iter->log_level;
        bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
 
-       BT_ASSERT(msg);
-       BT_ASSERT(ts_ns);
+       BT_ASSERT_DBG(msg);
+       BT_ASSERT_DBG(ts_ns);
 
        BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
                "last-msg-ts=%" PRId64, lttng_live_msg_iter, msg,
@@ -646,17 +658,15 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
 
        switch (bt_message_get_type(msg)) {
        case BT_MESSAGE_TYPE_EVENT:
-               clock_class =
-                       bt_message_event_borrow_stream_class_default_clock_class_const(
+               clock_class = bt_message_event_borrow_stream_class_default_clock_class_const(
                                msg);
-               BT_ASSERT(clock_class);
+               BT_ASSERT_DBG(clock_class);
 
                clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
                        msg);
                break;
        case BT_MESSAGE_TYPE_PACKET_BEGINNING:
-               clock_class =
-                       bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
+               clock_class = bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
                        msg);
                BT_ASSERT(clock_class);
 
@@ -664,8 +674,7 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
                        msg);
                break;
        case BT_MESSAGE_TYPE_PACKET_END:
-               clock_class =
-                       bt_message_packet_end_borrow_stream_class_default_clock_class_const(
+               clock_class = bt_message_packet_end_borrow_stream_class_default_clock_class_const(
                        msg);
                BT_ASSERT(clock_class);
 
@@ -673,8 +682,7 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
                        msg);
                break;
        case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
-               clock_class =
-                       bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
+               clock_class = bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
                        msg);
                BT_ASSERT(clock_class);
 
@@ -682,8 +690,7 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
                        msg);
                break;
        case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
-               clock_class =
-                       bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
+               clock_class = bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
                        msg);
                BT_ASSERT(clock_class);
 
@@ -691,9 +698,8 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
                        msg);
                break;
        case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
-               clock_snapshot =
-                       bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
-                               msg);
+               clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
+                       msg);
                break;
        default:
                /* All the other messages have a higher priority */
@@ -703,7 +709,7 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
        }
 
        clock_class = bt_clock_snapshot_borrow_clock_class_const(clock_snapshot);
-       BT_ASSERT(clock_class);
+       BT_ASSERT_DBG(clock_class);
 
        ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
        if (ret) {
@@ -735,10 +741,10 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_
                bt_message **message)
 {
        enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
-       enum bt_msg_iter_status status;
-       uint64_t session_idx, trace_idx;
        bt_logging_level log_level = lttng_live_msg_iter->log_level;
        bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
+       enum bt_msg_iter_status status;
+       uint64_t session_idx, trace_idx;
 
        for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len;
                        session_idx++) {
@@ -809,9 +815,9 @@ enum lttng_live_iterator_status lttng_live_iterator_close_stream(
         * `bt_msg_iter` should simply realize that it needs to close the
         * stream properly by emitting the necessary stream end message.
         */
-       enum bt_msg_iter_status status =
-               bt_msg_iter_get_next_message(stream_iter->msg_iter,
-                       lttng_live_msg_iter->self_msg_iter, curr_msg);
+       enum bt_msg_iter_status status = bt_msg_iter_get_next_message(
+               stream_iter->msg_iter, lttng_live_msg_iter->self_msg_iter,
+               curr_msg);
 
        if (status == BT_MSG_ITER_STATUS_ERROR) {
                live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
@@ -896,12 +902,12 @@ enum lttng_live_iterator_status lttng_live_iterator_next_msg_on_stream(
 retry:
        print_stream_state(stream_iter);
        live_status = lttng_live_iterator_handle_new_streams_and_metadata(
-                       lttng_live_msg_iter);
+               lttng_live_msg_iter);
        if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
                goto end;
        }
        live_status = lttng_live_iterator_next_handle_one_no_data_stream(
-                       lttng_live_msg_iter, stream_iter);
+               lttng_live_msg_iter, stream_iter);
 
        if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
                if (live_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
@@ -915,7 +921,7 @@ retry:
                goto end;
        }
        live_status = lttng_live_iterator_next_handle_one_quiescent_stream(
-                       lttng_live_msg_iter, stream_iter, curr_msg);
+               lttng_live_msg_iter, stream_iter, curr_msg);
        if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
                BT_ASSERT(!*curr_msg);
                goto end;
@@ -924,7 +930,7 @@ retry:
                goto end;
        }
        live_status = lttng_live_iterator_next_handle_one_active_data_stream(
-                       lttng_live_msg_iter, stream_iter, curr_msg);
+               lttng_live_msg_iter, stream_iter, curr_msg);
        if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
                BT_ASSERT(!*curr_msg);
        }
@@ -944,14 +950,14 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                struct lttng_live_stream_iterator **youngest_trace_stream_iter)
 {
        struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL;
+       bt_logging_level log_level = lttng_live_msg_iter->log_level;
+       bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
        enum lttng_live_iterator_status stream_iter_status;;
        int64_t youngest_candidate_msg_ts = INT64_MAX;
        uint64_t stream_iter_idx;
-       bt_logging_level log_level = lttng_live_msg_iter->log_level;
-       bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
 
-       BT_ASSERT(live_trace);
-       BT_ASSERT(live_trace->stream_iterators);
+       BT_ASSERT_DBG(live_trace);
+       BT_ASSERT_DBG(live_trace->stream_iterators);
        /*
         * Update the current message of every stream iterators of this trace.
         * The current msg of every stream must have a timestamp equal or
@@ -963,17 +969,7 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                bool stream_iter_is_ended = false;
                struct lttng_live_stream_iterator *stream_iter =
                        g_ptr_array_index(live_trace->stream_iterators,
-                                       stream_iter_idx);
-
-               /*
-                * Since we may remove elements from the GPtrArray as we
-                * iterate over it, it's possible to see the same element more
-                * than once.
-                */
-               if (stream_iter == youngest_candidate_stream_iter) {
-                       stream_iter_idx++;
-                       continue;
-               }
+                               stream_iter_idx);
 
                /*
                 * Find if there is are now current message for this stream
@@ -983,10 +979,10 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                        bt_message *msg = NULL;
                        int64_t curr_msg_ts_ns = INT64_MAX;
                        stream_iter_status = lttng_live_iterator_next_msg_on_stream(
-                                       lttng_live_msg_iter, stream_iter, &msg);
+                               lttng_live_msg_iter, stream_iter, &msg);
 
                        BT_COMP_LOGD("live stream iterator returned status :%s",
-                                       print_live_iterator_status(stream_iter_status));
+                               print_live_iterator_status(stream_iter_status));
                        if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
                                stream_iter_is_ended = true;
                                break;
@@ -996,7 +992,7 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                                goto end;
                        }
 
-                       BT_ASSERT(msg);
+                       BT_ASSERT_DBG(msg);
 
                        /*
                         * Get the timestamp in nanoseconds from origin of this
@@ -1032,7 +1028,7 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                        }
                }
 
-               BT_ASSERT(stream_iter != youngest_candidate_stream_iter);
+               BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter);
 
                if (!stream_iter_is_ended) {
                        if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) ||
@@ -1049,7 +1045,7 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                                 * Order the messages in an arbitrary but
                                 * deterministic way.
                                 */
-                               BT_ASSERT(stream_iter != youngest_candidate_stream_iter);
+                               BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter);
                                int ret = common_muxing_compare_messages(
                                        stream_iter->current_msg,
                                        youngest_candidate_stream_iter->current_msg);
@@ -1077,14 +1073,17 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace(
                        stream_iter_idx++;
                } else {
                        /*
-                        * The live stream iterator is ENDed. We remove that
-                        * iterator from the list and we restart the iteration
-                        * at the beginning of the live stream iterator array
-                        * to because the removal will shuffle the array.
+                        * The live stream iterator has ended. That
+                        * iterator is removed from the array, but
+                        * there is no need to increment
+                        * stream_iter_idx as
+                        * g_ptr_array_remove_index_fast replaces the
+                        * removed element with the array's last
+                        * element.
                         */
-                       g_ptr_array_remove_index_fast(live_trace->stream_iterators,
+                       g_ptr_array_remove_index_fast(
+                               live_trace->stream_iterators,
                                stream_iter_idx);
-                       stream_iter_idx = 0;
                }
        }
 
@@ -1128,13 +1127,8 @@ enum lttng_live_iterator_status next_stream_iterator_for_session(
                goto end;
        }
 
-       BT_ASSERT(session->traces);
+       BT_ASSERT_DBG(session->traces);
 
-       /*
-        * Use while loops here rather then for loops so we can restart the
-        * iteration if an element is removed from the array during the
-        * looping.
-        */
        while (trace_idx < session->traces->len) {
                bool trace_is_ended = false;
                struct lttng_live_stream_iterator *stream_iter;
@@ -1154,7 +1148,7 @@ enum lttng_live_iterator_status next_stream_iterator_for_session(
                }
 
                if (!trace_is_ended) {
-                       BT_ASSERT(stream_iter);
+                       BT_ASSERT_DBG(stream_iter);
 
                        if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) ||
                                        stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) {
@@ -1185,8 +1179,13 @@ enum lttng_live_iterator_status next_stream_iterator_for_session(
                        }
                        trace_idx++;
                } else {
-                       g_ptr_array_remove_index_fast(session->traces, trace_idx);
-                       trace_idx = 0;
+                       /*
+                        * trace_idx is not incremented since
+                        * g_ptr_array_remove_index_fast replaces the
+                        * element at trace_idx with the array's last element.
+                        */
+                       g_ptr_array_remove_index_fast(session->traces,
+                               trace_idx);
                }
        }
        if (youngest_candidate_stream_iter) {
@@ -1237,7 +1236,7 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
 
        *count = 0;
 
-       BT_ASSERT(lttng_live_msg_iter);
+       BT_ASSERT_DBG(lttng_live_msg_iter);
 
        /*
         * Clear all the invalid message reference that might be left over in
@@ -1296,18 +1295,8 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
                          *candidate_stream_iter = NULL;
                int64_t youngest_msg_ts_ns = INT64_MAX;
 
-               BT_ASSERT(lttng_live_msg_iter->sessions);
+               BT_ASSERT_DBG(lttng_live_msg_iter->sessions);
                session_idx = 0;
-               /*
-                * Use a while loop instead of a for loop so we can restart the
-                * iteration if we remove an element. We can safely call
-                * next_stream_iterator_for_session() multiple times on the
-                * same session as we only fetch a new message if there is no
-                * current next message for each live stream iterator.
-                * If all live stream iterator of that session already have a
-                * current next message, the function will simply exit return
-                * the same candidate live stream iterator every time.
-                */
                while (session_idx < lttng_live_msg_iter->sessions->len) {
                        struct lttng_live_session *session =
                                g_ptr_array_index(lttng_live_msg_iter->sessions,
@@ -1325,14 +1314,15 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
                        if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
                                if (session->closed && session->traces->len == 0) {
                                        /*
-                                        * Remove the session from the list and restart the
-                                        * iteration at the beginning of the array since the
-                                        * removal shuffle the elements of the array.
+                                        * Remove the session from the list.
+                                        * session_idx is not modified since
+                                        * g_ptr_array_remove_index_fast
+                                        * replaces the the removed element with
+                                        * the array's last element.
                                         */
                                        g_ptr_array_remove_index_fast(
                                                lttng_live_msg_iter->sessions,
                                                session_idx);
-                                       session_idx = 0;
                                } else {
                                        session_idx++;
                                }
@@ -1386,9 +1376,9 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
                        goto end;
                }
 
-               BT_ASSERT(youngest_stream_iter->current_msg);
+               BT_ASSERT_DBG(youngest_stream_iter->current_msg);
                /* Ensure monotonicity. */
-               BT_ASSERT(lttng_live_msg_iter->last_msg_ts_ns <=
+               BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <=
                        youngest_stream_iter->current_msg_ts_ns);
 
                /*
@@ -1443,13 +1433,14 @@ no_session:
 }
 
 BT_HIDDEN
-bt_component_class_message_iterator_init_method_status lttng_live_msg_iter_init(
+bt_component_class_message_iterator_initialize_method_status lttng_live_msg_iter_init(
                bt_self_message_iterator *self_msg_it,
+               bt_self_message_iterator_configuration *config,
                bt_self_component_source *self_comp_src,
                bt_self_component_port_output *self_port)
 {
-       bt_component_class_message_iterator_init_method_status ret =
-               BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
+       bt_component_class_message_iterator_initialize_method_status ret =
+               BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK;
        bt_self_component *self_comp =
                bt_self_component_source_as_self_component(self_comp_src);
        struct lttng_live_component *lttng_live;
@@ -1468,7 +1459,7 @@ bt_component_class_message_iterator_init_method_status lttng_live_msg_iter_init(
 
        lttng_live_msg_iter = g_new0(struct lttng_live_msg_iter, 1);
        if (!lttng_live_msg_iter) {
-               ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
+               ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR;
                goto end;
        }
 
@@ -1520,17 +1511,8 @@ bt_component_class_message_iterator_init_method_status lttng_live_msg_iter_init(
                                SESS_NOT_FOUND_ACTION_END_STR,
                                lttng_live->params.url->str);
                        break;
-               case SESSION_NOT_FOUND_ACTION_UNKNOWN:
                default:
-                       /* Fallthrough */
-                       BT_COMP_LOGE("Unknown action for session not found"
-                               "error. Fail the message iterator"
-                               "initialization because of %s=\"%s\" "
-                               "component parameter: url =\"%s\"",
-                               SESS_NOT_FOUND_ACTION_PARAM,
-                               SESS_NOT_FOUND_ACTION_FAIL_STR,
-                               lttng_live->params.url->str);
-                       break;
+                       abort();
                }
        }
 
@@ -1538,48 +1520,53 @@ bt_component_class_message_iterator_init_method_status lttng_live_msg_iter_init(
 
        goto end;
 error:
-       ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
+       ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR;
        lttng_live_msg_iter_destroy(lttng_live_msg_iter);
 end:
        return ret;
 }
 
+static struct bt_param_validation_map_value_entry_descr list_sessions_params[] = {
+       { URL_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_STRING } },
+       BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
+};
+
 static
 bt_component_class_query_method_status lttng_live_query_list_sessions(
                const bt_value *params, const bt_value **result,
+               bt_self_component_class *self_comp_class,
                bt_logging_level log_level)
 {
-       bt_component_class_query_method_status status =
-               BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
+       bt_component_class_query_method_status status;
        const bt_value *url_value = NULL;
        const char *url;
        struct live_viewer_connection *viewer_connection = NULL;
-       bt_self_component *self_comp = NULL;
+       enum bt_param_validation_status validation_status;
+       gchar *validate_error = NULL;
 
-       url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM);
-       if (!url_value) {
-               BT_COMP_LOGE("Mandatory `%s` parameter missing", URL_PARAM);
-               status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
+       validation_status = bt_param_validation_validate(params,
+               list_sessions_params, &validate_error);
+       if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
+               status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
                goto error;
-       }
-
-       if (!bt_value_is_string(url_value)) {
-               BT_COMP_LOGE("`%s` parameter is required to be a string value",
-                       URL_PARAM);
+       } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
                status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
+               BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "%s", validate_error);
                goto error;
        }
 
+       url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM);
        url = bt_value_string_get(url_value);
 
        viewer_connection = live_viewer_connection_create(url, true, NULL,
                log_level);
        if (!viewer_connection) {
+               status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
                goto error;
        }
 
        status = live_viewer_connection_list_sessions(viewer_connection,
-                       result);
+               result);
        if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) {
                goto error;
        }
@@ -1597,6 +1584,9 @@ end:
        if (viewer_connection) {
                live_viewer_connection_destroy(viewer_connection);
        }
+
+       g_free(validate_error);
+
        return status;
 }
 
@@ -1686,13 +1676,15 @@ bt_component_class_query_method_status lttng_live_query(
        bt_component_class_query_method_status status =
                BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
        bt_self_component *self_comp = NULL;
+       bt_self_component_class *self_comp_class =
+               bt_self_component_class_source_as_self_component_class(comp_class);
        bt_logging_level log_level = bt_query_executor_get_logging_level(
                bt_private_query_executor_as_query_executor_const(
                        priv_query_exec));
 
        if (strcmp(object, "sessions") == 0) {
                status = lttng_live_query_list_sessions(params, result,
-                       log_level);
+                       self_comp_class, log_level);
        } else if (strcmp(object, "babeltrace.support-info") == 0) {
                status = lttng_live_query_support_info(params, result,
                        log_level);
@@ -1722,7 +1714,7 @@ BT_HIDDEN
 void lttng_live_component_finalize(bt_self_component_source *component)
 {
        void *data = bt_self_component_get_data(
-                       bt_self_component_source_as_self_component(component));
+               bt_self_component_source_as_self_component(component));
 
        if (!data) {
                return;
@@ -1735,32 +1727,72 @@ enum session_not_found_action parse_session_not_found_action_param(
        const bt_value *no_session_param)
 {
        enum session_not_found_action action;
-       const char *no_session_act_str;
-       no_session_act_str = bt_value_string_get(no_session_param);
+       const char *no_session_act_str = bt_value_string_get(no_session_param);
+
        if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_CONTINUE_STR) == 0) {
                action = SESSION_NOT_FOUND_ACTION_CONTINUE;
        } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_FAIL_STR) == 0) {
                action = SESSION_NOT_FOUND_ACTION_FAIL;
-       } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0) {
-               action = SESSION_NOT_FOUND_ACTION_END;
        } else {
-               action = SESSION_NOT_FOUND_ACTION_UNKNOWN;
+               BT_ASSERT(strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0);
+               action = SESSION_NOT_FOUND_ACTION_END;
        }
 
        return action;
 }
 
-struct lttng_live_component *lttng_live_component_create(const bt_value *params,
-               bt_logging_level log_level, bt_self_component *self_comp)
+static struct bt_param_validation_value_descr inputs_elem_descr = {
+       .type = BT_VALUE_TYPE_STRING,
+};
+
+static const char *sess_not_found_action_choices[] = {
+       SESS_NOT_FOUND_ACTION_CONTINUE_STR,
+       SESS_NOT_FOUND_ACTION_FAIL_STR,
+       SESS_NOT_FOUND_ACTION_END_STR,
+};
+
+static struct bt_param_validation_map_value_entry_descr params_descr[] = {
+       { INPUTS_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
+               .min_length = 1,
+               .max_length = 1,
+               .element_type = &inputs_elem_descr,
+       } } },
+       { SESS_NOT_FOUND_ACTION_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = {
+               .choices = sess_not_found_action_choices,
+       } } },
+       BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
+};
+
+static
+bt_component_class_initialize_method_status lttng_live_component_create(
+               const bt_value *params,
+               bt_logging_level log_level,
+               bt_self_component *self_comp,
+               struct lttng_live_component **component)
 {
-       struct lttng_live_component *lttng_live;
+       struct lttng_live_component *lttng_live = NULL;
        const bt_value *inputs_value;
        const bt_value *url_value;
        const bt_value *value;
        const char *url;
+       enum bt_param_validation_status validation_status;
+       gchar *validation_error = NULL;
+       bt_component_class_initialize_method_status status;
+
+       validation_status = bt_param_validation_validate(params, params_descr,
+               &validation_error);
+       if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
+               goto error;
+       } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
+               BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validation_error);
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
+               goto error;
+       }
 
        lttng_live = g_new0(struct lttng_live_component, 1);
        if (!lttng_live) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto end;
        }
        lttng_live->log_level = log_level;
@@ -1768,58 +1800,23 @@ struct lttng_live_component *lttng_live_component_create(const bt_value *params,
        lttng_live->max_query_size = MAX_QUERY_SIZE;
        lttng_live->has_msg_iter = false;
 
-       inputs_value = bt_value_map_borrow_entry_value_const(params,
-               INPUTS_PARAM);
-       if (!inputs_value) {
-               BT_COMP_LOGE("Mandatory `%s` parameter missing", INPUTS_PARAM);
-               goto error;
-       }
-
-       if (!bt_value_is_array(inputs_value)) {
-               BT_COMP_LOGE("`%s` parameter is required to be an array value",
-                       INPUTS_PARAM);
-               goto error;
-       }
-
-       if (bt_value_array_get_length(inputs_value) != 1) {
-               BT_COMP_LOGE("`%s` parameter's length is required to be 1",
-                       INPUTS_PARAM);
-               goto error;
-       }
-
-       url_value = bt_value_array_borrow_element_by_index_const(inputs_value,
-               0);
-       BT_ASSERT(url_value);
-
-       if (!bt_value_is_string(url_value)) {
-               BT_COMP_LOGE("First element of `%s` parameter is required to be a string value (URL)",
-                       INPUTS_PARAM);
-               goto error;
-       }
-
+       inputs_value =
+               bt_value_map_borrow_entry_value_const(params, INPUTS_PARAM);
+       url_value =
+               bt_value_array_borrow_element_by_index_const(inputs_value, 0);
        url = bt_value_string_get(url_value);
+
        lttng_live->params.url = g_string_new(url);
        if (!lttng_live->params.url) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto error;
        }
 
        value = bt_value_map_borrow_entry_value_const(params,
                SESS_NOT_FOUND_ACTION_PARAM);
        if (value) {
-               if (!bt_value_is_string(value)) {
-                       BT_COMP_LOGE("`%s` parameter is required to be a string value",
-                               SESS_NOT_FOUND_ACTION_PARAM);
-                       goto error;
-               }
-
                lttng_live->params.sess_not_found_act =
                        parse_session_not_found_action_param(value);
-               if (lttng_live->params.sess_not_found_act == SESSION_NOT_FOUND_ACTION_UNKNOWN) {
-                       BT_COMP_LOGE("Unexpected value for `%s` parameter: "
-                               "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM,
-                               bt_value_string_get(value));
-                       goto error;
-               }
        } else {
                BT_COMP_LOGI("Optional `%s` parameter is missing: "
                        "defaulting to `%s`.",
@@ -1829,48 +1826,44 @@ struct lttng_live_component *lttng_live_component_create(const bt_value *params,
                        SESSION_NOT_FOUND_ACTION_CONTINUE;
        }
 
+       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
        goto end;
 
 error:
        lttng_live_component_destroy_data(lttng_live);
        lttng_live = NULL;
 end:
-       return lttng_live;
+       g_free(validation_error);
+
+       *component = lttng_live;
+       return status;
 }
 
 BT_HIDDEN
-bt_component_class_init_method_status lttng_live_component_init(
+bt_component_class_initialize_method_status lttng_live_component_init(
                bt_self_component_source *self_comp_src,
                bt_self_component_source_configuration *config,
                const bt_value *params,
                __attribute__((unused)) void *init_method_data)
 {
        struct lttng_live_component *lttng_live;
-       bt_component_class_init_method_status ret =
-               BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
+       bt_component_class_initialize_method_status ret;
        bt_self_component *self_comp =
                bt_self_component_source_as_self_component(self_comp_src);
        bt_logging_level log_level = bt_component_get_logging_level(
                bt_self_component_as_component(self_comp));
        bt_self_component_add_port_status add_port_status;
 
-       lttng_live = lttng_live_component_create(params, log_level, self_comp);
-       if (!lttng_live) {
-               ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
+       ret = lttng_live_component_create(params, log_level, self_comp, &lttng_live);
+       if (ret != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
                goto error;
        }
 
        add_port_status = bt_self_component_source_add_output_port(
                self_comp_src, "out", NULL, NULL);
-       switch (add_port_status) {
-       case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
-               ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
-               goto error;
-       case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
-               ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
-               goto error;
-       default:
-               break;
+       if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
+               ret = (int) add_port_status;
+               goto end;
        }
 
        bt_self_component_set_data(self_comp, lttng_live);
This page took 0.035202 seconds and 4 git commands to generate.