Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
index 4ace64920109dd38e58e75dad6452a78518b47b8..236c2c98f9d80f98b2e43b7c74ba9175893a64e3 100644 (file)
@@ -8,7 +8,6 @@
  */
 
 #define _LGPL_SOURCE
-#include <assert.h>
 #include <poll.h>
 #include <pthread.h>
 #include <stdlib.h>
@@ -74,7 +73,7 @@ int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
 int lttng_kconsumer_sample_snapshot_positions(
                struct lttng_consumer_stream *stream)
 {
-       assert(stream);
+       LTTNG_ASSERT(stream);
 
        return kernctl_snapshot_sample_positions(stream->wait_fd);
 }
@@ -174,7 +173,7 @@ static int lttng_kconsumer_snapshot_channel(
                 */
                pthread_mutex_lock(&stream->lock);
 
-               assert(channel->trace_chunk);
+               LTTNG_ASSERT(channel->trace_chunk);
                if (!lttng_trace_chunk_get(channel->trace_chunk)) {
                        /*
                         * Can't happen barring an internal error as the channel
@@ -184,7 +183,7 @@ static int lttng_kconsumer_snapshot_channel(
                        ret = -1;
                        goto end_unlock;
                }
-               assert(!stream->trace_chunk);
+               LTTNG_ASSERT(!stream->trace_chunk);
                stream->trace_chunk = channel->trace_chunk;
 
                /*
@@ -366,7 +365,7 @@ static int lttng_kconsumer_snapshot_metadata(
        ssize_t ret_read;
        struct lttng_consumer_stream *metadata_stream;
 
-       assert(ctx);
+       LTTNG_ASSERT(ctx);
 
        DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
                        key, path);
@@ -374,11 +373,11 @@ static int lttng_kconsumer_snapshot_metadata(
        rcu_read_lock();
 
        metadata_stream = metadata_channel->metadata_stream;
-       assert(metadata_stream);
+       LTTNG_ASSERT(metadata_stream);
 
        pthread_mutex_lock(&metadata_stream->lock);
-       assert(metadata_channel->trace_chunk);
-       assert(metadata_stream->trace_chunk);
+       LTTNG_ASSERT(metadata_channel->trace_chunk);
+       LTTNG_ASSERT(metadata_stream->trace_chunk);
 
        /* Flag once that we have a valid relayd for the stream. */
        if (relayd_id != (uint64_t) -1ULL) {
@@ -403,16 +402,12 @@ static int lttng_kconsumer_snapshot_metadata(
 
                ret_read = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
                if (ret_read < 0) {
-                       if (ret_read != -EAGAIN) {
-                               ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
-                                               ret_read);
-                               ret = ret_read;
-                               goto error_snapshot;
-                       }
-                       /* ret_read is negative at this point so we will exit the loop. */
-                       continue;
+                       ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
+                                       ret_read);
+                       ret = ret_read;
+                       goto error_snapshot;
                }
-       } while (ret_read >= 0);
+       } while (ret_read > 0);
 
        if (use_relayd) {
                close_relayd_stream(metadata_stream);
@@ -474,7 +469,7 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
        health_code_update();
 
        /* Deprecated command */
-       assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
+       LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP);
 
        health_code_update();
 
@@ -494,7 +489,7 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
        case LTTNG_CONSUMER_ADD_CHANNEL:
        {
                struct lttng_consumer_channel *new_channel;
-               int ret_send_status, ret_add_channel;
+               int ret_send_status, ret_add_channel = 0;
                const uint64_t chunk_id = msg.u.channel.chunk_id.value;
 
                health_code_update();
@@ -546,7 +541,7 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
                        new_channel->type = msg.u.channel.type;
                        break;
                default:
-                       assert(0);
+                       abort();
                        goto end_nosignal;
                };
 
@@ -1035,13 +1030,13 @@ error_streams_sent_nosignal:
                 * This command should ONLY be issued for channel with streams set in
                 * no monitor mode.
                 */
-               assert(!channel->monitor);
+               LTTNG_ASSERT(!channel->monitor);
 
                /*
                 * The refcount should ALWAYS be 0 in the case of a channel in no
                 * monitor mode.
                 */
-               assert(!uatomic_sub_return(&channel->refcount, 1));
+               LTTNG_ASSERT(!uatomic_sub_return(&channel->refcount, 1));
 
                consumer_del_channel(channel);
 end_destroy_channel:
@@ -1436,7 +1431,7 @@ enum sync_metadata_status lttng_kconsumer_sync_metadata(
        int ret;
        enum sync_metadata_status status;
 
-       assert(metadata);
+       LTTNG_ASSERT(metadata);
 
        ret = kernctl_buffer_flush(metadata->wait_fd);
        if (ret < 0) {
@@ -1587,68 +1582,94 @@ end:
 }
 
 static
-int get_subbuffer_common(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_subbuffer_common(
+               struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
        int ret;
+       enum get_next_subbuffer_status status;
 
        ret = kernctl_get_next_subbuf(stream->wait_fd);
-       if (ret) {
+       switch (ret) {
+       case 0:
+               status = GET_NEXT_SUBBUFFER_STATUS_OK;
+               break;
+       case -ENODATA:
+               case -EAGAIN:
+               /*
+                * The caller only expects -ENODATA when there is no data to
+                * read, but the kernel tracer returns -EAGAIN when there is
+                * currently no data for a non-finalized stream, and -ENODATA
+                * when there is no data for a finalized stream. Those can be
+                * combined into a -ENODATA return value.
+                */
+               status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
+               goto end;
+       default:
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
                goto end;
        }
 
        ret = stream->read_subbuffer_ops.extract_subbuffer_info(
-                       stream, subbuffer);
+               stream, subbuffer);
+       if (ret) {
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
+       }
 end:
-       return ret;
+       return status;
 }
 
 static
-int get_next_subbuffer_splice(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_next_subbuffer_splice(
+               struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
-       int ret;
+       const enum get_next_subbuffer_status status =
+                       get_subbuffer_common(stream, subbuffer);
 
-       ret = get_subbuffer_common(stream, subbuffer);
-       if (ret) {
+       if (status != GET_NEXT_SUBBUFFER_STATUS_OK) {
                goto end;
        }
 
        subbuffer->buffer.fd = stream->wait_fd;
 end:
-       return ret;
+       return status;
 }
 
 static
-int get_next_subbuffer_mmap(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_next_subbuffer_mmap(
+               struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
        int ret;
+       enum get_next_subbuffer_status status;
        const char *addr;
 
-       ret = get_subbuffer_common(stream, subbuffer);
-       if (ret) {
+       status = get_subbuffer_common(stream, subbuffer);
+       if (status != GET_NEXT_SUBBUFFER_STATUS_OK) {
                goto end;
        }
 
        ret = get_current_subbuf_addr(stream, &addr);
        if (ret) {
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
                goto end;
        }
 
        subbuffer->buffer.buffer = lttng_buffer_view_init(
                        addr, 0, subbuffer->info.data.padded_subbuf_size);
 end:
-       return ret;
+       return status;
 }
 
 static
-int get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
        int ret;
        const char *addr;
        bool coherent;
+       enum get_next_subbuffer_status status;
 
        ret = kernctl_get_next_subbuf_metadata_check(stream->wait_fd,
                        &coherent);
@@ -1675,7 +1696,33 @@ int get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
                        subbuffer->info.metadata.padded_subbuf_size,
                        coherent ? "true" : "false");
 end:
-       return ret;
+       /*
+        * The caller only expects -ENODATA when there is no data to read, but
+        * the kernel tracer returns -EAGAIN when there is currently no data
+        * for a non-finalized stream, and -ENODATA when there is no data for a
+        * finalized stream. Those can be combined into a -ENODATA return value.
+        */
+       switch (ret) {
+       case 0:
+               status = GET_NEXT_SUBBUFFER_STATUS_OK;
+               break;
+       case -ENODATA:
+       case -EAGAIN:
+               /*
+                * The caller only expects -ENODATA when there is no data to
+                * read, but the kernel tracer returns -EAGAIN when there is
+                * currently no data for a non-finalized stream, and -ENODATA
+                * when there is no data for a finalized stream. Those can be
+                * combined into a -ENODATA return value.
+                */
+               status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
+               break;
+       default:
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
+               break;
+       }
+
+       return status;
 }
 
 static
@@ -1699,8 +1746,23 @@ int put_next_subbuffer(struct lttng_consumer_stream *stream,
 static
 bool is_get_next_check_metadata_available(int tracer_fd)
 {
-       return kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL) !=
-                       -ENOTTY;
+       const int ret = kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL);
+       const bool available = ret != -ENOTTY;
+
+       if (ret == 0) {
+               /* get succeeded, make sure to put the subbuffer. */
+               kernctl_put_subbuf(tracer_fd);
+       }
+
+       return available;
+}
+
+static
+int signal_metadata(struct lttng_consumer_stream *stream,
+               struct lttng_consumer_local_data *ctx)
+{
+       ASSERT_LOCKED(stream->metadata_rdv_lock);
+       return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
 }
 
 static
@@ -1733,6 +1795,8 @@ int lttng_kconsumer_set_stream_ops(
                        metadata_bucket_destroy(stream->metadata_bucket);
                        stream->metadata_bucket = NULL;
                }
+
+               stream->read_subbuffer_ops.on_sleep = signal_metadata;
        }
 
        if (!stream->read_subbuffer_ops.get_next_subbuffer) {
@@ -1766,7 +1830,7 @@ int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
 {
        int ret;
 
-       assert(stream);
+       LTTNG_ASSERT(stream);
 
        /*
         * Don't create anything if this is set for streaming or if there is
@@ -1813,7 +1877,7 @@ error_close_fd:
                int err;
 
                err = close(stream->out_fd);
-               assert(!err);
+               LTTNG_ASSERT(!err);
                stream->out_fd = -1;
        }
 error:
@@ -1832,7 +1896,7 @@ int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
 {
        int ret;
 
-       assert(stream);
+       LTTNG_ASSERT(stream);
 
        if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
                ret = 0;
@@ -1843,7 +1907,7 @@ int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
        if (ret == 0) {
                /* There is still data so let's put back this subbuffer. */
                ret = kernctl_put_subbuf(stream->wait_fd);
-               assert(ret == 0);
+               LTTNG_ASSERT(ret == 0);
                ret = 1;   /* Data is pending */
                goto end;
        }
This page took 0.028975 seconds and 4 git commands to generate.