lttng rotate command
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index 14bb7b32dab3f1559a0468cdc484c456961fdbd5..0468b6746f51cdf20e33821cc6cb3d3de9acda9d 100644 (file)
@@ -36,6 +36,9 @@
 #include <common/utils.h>
 #include <lttng/lttng.h>
 #include <lttng/health-internal.h>
+#include <lttng/trigger/trigger-internal.h>
+#include <lttng/endpoint.h>
+#include <lttng/channel-internal.h>
 
 #include "filter/filter-ast.h"
 #include "filter/filter-parser.h"
@@ -392,6 +395,54 @@ static int disconnect_sessiond(void)
        return ret;
 }
 
+static int recv_sessiond_optional_data(size_t len, void **user_buf,
+       size_t *user_len)
+{
+       int ret = 0;
+       void *buf = NULL;
+
+       if (len) {
+               if (!user_len) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto end;
+               }
+
+               buf = zmalloc(len);
+               if (!buf) {
+                       ret = -ENOMEM;
+                       goto end;
+               }
+
+               ret = recv_data_sessiond(buf, len);
+               if (ret < 0) {
+                       goto end;
+               }
+
+               if (!user_buf) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto end;
+               }
+
+               /* Move ownership of command header buffer to user. */
+               *user_buf = buf;
+               buf = NULL;
+               *user_len = len;
+       } else {
+               /* No command header. */
+               if (user_len) {
+                       *user_len = 0;
+               }
+
+               if (user_buf) {
+                       *user_buf = NULL;
+               }
+       }
+
+end:
+       free(buf);
+       return ret;
+}
+
 /*
  * Ask the session daemon a specific command and put the data into buf.
  * Takes extra var. len. data as input to send to the session daemon.
@@ -400,11 +451,12 @@ static int disconnect_sessiond(void)
  */
 LTTNG_HIDDEN
 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
-               const void *vardata, size_t varlen, void **buf)
+               const void *vardata, size_t vardata_len,
+               void **user_payload_buf, void **user_cmd_header_buf,
+               size_t *user_cmd_header_len)
 {
        int ret;
-       size_t size;
-       void *data = NULL;
+       size_t payload_len;
        struct lttcomm_lttng_msg llm;
 
        ret = connect_sessiond();
@@ -420,7 +472,7 @@ int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
                goto end;
        }
        /* Send var len data */
-       ret = send_session_varlen(vardata, varlen);
+       ret = send_session_varlen(vardata, vardata_len);
        if (ret < 0) {
                /* Ret value is a valid lttng error code. */
                goto end;
@@ -439,41 +491,21 @@ int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
                goto end;
        }
 
-       size = llm.data_size;
-       if (size == 0) {
-               /* If client free with size 0 */
-               if (buf != NULL) {
-                       *buf = NULL;
-               }
-               ret = 0;
-               goto end;
-       }
-
-       data = zmalloc(size);
-       if (!data) {
-               ret = -ENOMEM;
-               goto end;
-       }
-
-       /* Get payload data */
-       ret = recv_data_sessiond(data, size);
+       /* Get command header from data transmission */
+       ret = recv_sessiond_optional_data(llm.cmd_header_size,
+               user_cmd_header_buf, user_cmd_header_len);
        if (ret < 0) {
-               free(data);
                goto end;
        }
 
-       /*
-        * Extra protection not to dereference a NULL pointer. If buf is NULL at
-        * this point, an error is returned and data is freed.
-        */
-       if (buf == NULL) {
-               ret = -LTTNG_ERR_INVALID;
-               free(data);
+       /* Get payload from data transmission */
+       ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
+               &payload_len);
+       if (ret < 0) {
                goto end;
        }
 
-       *buf = data;
-       ret = size;
+       ret = llm.data_size;
 
 end:
        disconnect_sessiond();
@@ -490,10 +522,6 @@ struct lttng_handle *lttng_create_handle(const char *session_name,
 {
        struct lttng_handle *handle = NULL;
 
-       if (domain == NULL) {
-               goto end;
-       }
-
        handle = zmalloc(sizeof(struct lttng_handle));
        if (handle == NULL) {
                PERROR("malloc handle");
@@ -504,8 +532,10 @@ struct lttng_handle *lttng_create_handle(const char *session_name,
        lttng_ctl_copy_string(handle->session_name, session_name,
                        sizeof(handle->session_name));
 
-       /* Copy lttng domain */
-       lttng_ctl_copy_lttng_domain(&handle->domain, domain);
+       /* Copy lttng domain or leave initialized to 0. */
+       if (domain) {
+               lttng_ctl_copy_lttng_domain(&handle->domain, domain);
+       }
 
 end:
        return handle;
@@ -707,11 +737,18 @@ int lttng_add_context(struct lttng_handle *handle,
                memcpy(buf + provider_len, ctx_name, ctx_len);
        }
        memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
-       /* Don't leak application addresses to the sessiond. */
-       lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
-       lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, buf, len, NULL);
+       if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
+               /*
+                * Don't leak application addresses to the sessiond.
+                * This is only necessary when ctx is for an app ctx otherwise
+                * the values inside the union (type & config) are overwritten.
+                */
+               lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
+               lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
+       }
+
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
 end:
        free(buf);
        return ret;
@@ -760,7 +797,7 @@ static char *set_agent_filter(const char *filter, struct lttng_event *ev)
        assert(ev);
 
        /* Don't add filter for the '*' event. */
-       if (ev->name[0] != '*') {
+       if (strcmp(ev->name, "*") != 0) {
                if (filter) {
                        err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
                                        ev->name);
@@ -883,12 +920,28 @@ static int generate_filter(char *filter_expression,
                ret = -LTTNG_ERR_FILTER_INVAL;
                goto parse_error;
        }
+
+       /* Normalize globbing patterns in the expression. */
+       ret = filter_visitor_ir_normalize_glob_patterns(ctx);
+       if (ret) {
+               ret = -LTTNG_ERR_FILTER_INVAL;
+               goto parse_error;
+       }
+
        /* Validate strings used as literals in the expression. */
        ret = filter_visitor_ir_validate_string(ctx);
        if (ret) {
                ret = -LTTNG_ERR_FILTER_INVAL;
                goto parse_error;
        }
+
+       /* Validate globbing patterns in the expression. */
+       ret = filter_visitor_ir_validate_globbing(ctx);
+       if (ret) {
+               ret = -LTTNG_ERR_FILTER_INVAL;
+               goto parse_error;
+       }
+
        dbg_printf("done\n");
 
        dbg_printf("Generating bytecode... ");
@@ -1075,7 +1128,7 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
                        lsm.u.enable.bytecode_len);
        }
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, varlen_data,
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
                        (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) +
                        lsm.u.enable.bytecode_len + lsm.u.enable.expression_len,
                        NULL);
@@ -1234,7 +1287,7 @@ int lttng_disable_event_ext(struct lttng_handle *handle,
                        lsm.u.disable.bytecode_len);
        }
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, varlen_data,
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
                        lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
        free(varlen_data);
 
@@ -1283,26 +1336,111 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name,
        return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
 }
 
+struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
+{
+       struct lttng_channel *channel = NULL;
+       struct lttng_channel_extended *extended = NULL;
+
+       if (!domain) {
+               goto error;
+       }
+
+       /* Validate domain. */
+       switch (domain->type) {
+       case LTTNG_DOMAIN_UST:
+               switch (domain->buf_type) {
+               case LTTNG_BUFFER_PER_UID:
+               case LTTNG_BUFFER_PER_PID:
+                       break;
+               default:
+                       goto error;
+               }
+               break;
+       case LTTNG_DOMAIN_KERNEL:
+               if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
+                       goto error;
+               }
+               break;
+       default:
+               goto error;
+       }
+
+       channel = zmalloc(sizeof(*channel));
+       if (!channel) {
+               goto error;
+       }
+
+       extended = zmalloc(sizeof(*extended));
+       if (!extended) {
+               goto error;
+       }
+
+       channel->attr.extended.ptr = extended;
+
+       lttng_channel_set_default_attr(domain, &channel->attr);
+       return channel;
+error:
+       free(channel);
+       free(extended);
+       return NULL;
+}
+
+void lttng_channel_destroy(struct lttng_channel *channel)
+{
+       if (!channel) {
+               return;
+       }
+
+       if (channel->attr.extended.ptr) {
+               free(channel->attr.extended.ptr);
+       }
+       free(channel);
+}
+
 /*
  * Enable channel per domain
  * Returns size of returned session payload data or a negative error code.
  */
 int lttng_enable_channel(struct lttng_handle *handle,
-               struct lttng_channel *chan)
+               struct lttng_channel *in_chan)
 {
        struct lttcomm_session_msg lsm;
 
        /* NULL arguments are forbidden. No default values. */
-       if (handle == NULL || chan == NULL) {
+       if (handle == NULL || in_chan == NULL) {
                return -LTTNG_ERR_INVALID;
        }
 
        memset(&lsm, 0, sizeof(lsm));
+       memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan));
+       lsm.u.channel.chan.attr.extended.ptr = NULL;
 
-       memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
+       if (!in_chan->attr.extended.ptr) {
+               struct lttng_channel *channel;
+               struct lttng_channel_extended *extended;
 
-       lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
+               channel = lttng_channel_create(&handle->domain);
+               if (!channel) {
+                       return -LTTNG_ERR_NOMEM;
+               }
 
+               /*
+                * Create a new channel in order to use default extended
+                * attribute values.
+                */
+               extended = (struct lttng_channel_extended *)
+                               channel->attr.extended.ptr;
+               memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
+               lttng_channel_destroy(channel);
+       } else {
+               struct lttng_channel_extended *extended;
+
+               extended = (struct lttng_channel_extended *)
+                               in_chan->attr.extended.ptr;
+               memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
+       }
+
+       lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
        lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
 
        lttng_ctl_copy_string(lsm.session.name, handle->session_name,
@@ -1514,7 +1652,7 @@ int lttng_create_session(const char *name, const char *url)
 
        lsm.u.uri.size = size;
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
                        sizeof(struct lttng_uri) * size, NULL);
 
        free(uris);
@@ -1525,7 +1663,8 @@ int lttng_create_session(const char *name, const char *url)
  * Destroy session using name.
  * Returns size of returned session payload data or a negative error code.
  */
-int lttng_destroy_session(const char *session_name)
+static
+int _lttng_destroy_session(const char *session_name)
 {
        struct lttcomm_session_msg lsm;
 
@@ -1542,6 +1681,48 @@ int lttng_destroy_session(const char *session_name)
        return lttng_ctl_ask_sessiond(&lsm, NULL);
 }
 
+/*
+ * Stop the session and wait for the data before destroying it
+ */
+int lttng_destroy_session(const char *session_name)
+{
+       int ret;
+
+       /*
+        * Stop the tracing and wait for the data.
+        */
+       ret = _lttng_stop_tracing(session_name, 1);
+       if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
+               goto end;
+       }
+
+       ret = _lttng_destroy_session(session_name);
+end:
+       return ret;
+}
+
+/*
+ * Destroy the session without waiting for the data.
+ */
+int lttng_destroy_session_no_wait(const char *session_name)
+{
+       int ret;
+
+       /*
+        * Stop the tracing without waiting for the data.
+        * The session might already have been stopped, so just
+        * skip this error.
+        */
+       ret = _lttng_stop_tracing(session_name, 0);
+       if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
+               goto end;
+       }
+
+       ret = _lttng_destroy_session(session_name);
+end:
+       return ret;
+}
+
 /*
  * Ask the session daemon for all available sessions.
  * Sets the contents of the sessions array.
@@ -1623,10 +1804,15 @@ int lttng_list_channels(struct lttng_handle *handle,
                struct lttng_channel **channels)
 {
        int ret;
+       size_t channel_count, i;
+       const size_t channel_size = sizeof(struct lttng_channel) +
+                       sizeof(struct lttng_channel_extended);
        struct lttcomm_session_msg lsm;
+       void *extended_at;
 
        if (handle == NULL) {
-               return -LTTNG_ERR_INVALID;
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
        }
 
        memset(&lsm, 0, sizeof(lsm));
@@ -1638,10 +1824,30 @@ int lttng_list_channels(struct lttng_handle *handle,
 
        ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
        if (ret < 0) {
-               return ret;
+               goto end;
        }
 
-       return ret / sizeof(struct lttng_channel);
+       if (ret % channel_size) {
+               ret = -LTTNG_ERR_UNK;
+               free(*channels);
+               *channels = NULL;
+               goto end;
+       }
+       channel_count = (size_t) ret / channel_size;
+
+       /* Set extended info pointers */
+       extended_at = ((void *) *channels) +
+                       channel_count * sizeof(struct lttng_channel);
+       for (i = 0; i < channel_count; i++) {
+               struct lttng_channel *chan = &(*channels)[i];
+
+               chan->attr.extended.ptr = extended_at;
+               extended_at += sizeof(struct lttng_channel_extended);
+       }
+
+       ret = (int) channel_count;
+end:
+       return ret;
 }
 
 /*
@@ -1655,6 +1861,10 @@ int lttng_list_events(struct lttng_handle *handle,
 {
        int ret;
        struct lttcomm_session_msg lsm;
+       struct lttcomm_event_command_header *cmd_header = NULL;
+       size_t cmd_header_len;
+       uint32_t nb_events, i;
+       void *extended_at;
 
        /* Safety check. An handle and channel name are mandatory */
        if (handle == NULL || channel_name == NULL) {
@@ -1667,15 +1877,140 @@ int lttng_list_events(struct lttng_handle *handle,
                        sizeof(lsm.session.name));
        lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
                        sizeof(lsm.u.list.channel_name));
-
        lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
 
-       ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
+       ret = lttng_ctl_ask_sessiond_varlen(&lsm, NULL, 0, (void **) events,
+               (void **) &cmd_header, &cmd_header_len);
        if (ret < 0) {
-               return ret;
+               goto error;
        }
 
-       return ret / sizeof(struct lttng_event);
+       /* Set number of events and free command header */
+       nb_events = cmd_header->nb_events;
+       if (nb_events > INT_MAX) {
+               ret = -EOVERFLOW;
+               goto error;
+       }
+       ret = (int) nb_events;
+       free(cmd_header);
+       cmd_header = NULL;
+
+       /* Set extended info pointers */
+       extended_at = ((void*) (*events)) +
+                       nb_events * sizeof(struct lttng_event);
+
+       for (i = 0; i < nb_events; i++) {
+               struct lttcomm_event_extended_header *ext_header;
+               struct lttng_event *event = &(*events)[i];
+
+               event->extended.ptr = extended_at;
+               ext_header =
+                       (struct lttcomm_event_extended_header *) extended_at;
+               extended_at += sizeof(*ext_header);
+               extended_at += ext_header->filter_len;
+               extended_at +=
+                       ext_header->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
+       }
+
+       return ret;
+error:
+       free(cmd_header);
+       free(*events);
+       return ret;
+}
+
+int lttng_event_get_filter_expression(struct lttng_event *event,
+       const char **filter_expression)
+{
+       int ret = 0;
+       struct lttcomm_event_extended_header *ext_header;
+
+       if (!event || !filter_expression) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ext_header = event->extended.ptr;
+
+       if (!ext_header) {
+               /*
+                * This can happen since the lttng_event structure is
+                * used for other tasks where this pointer is never set.
+                */
+               *filter_expression = NULL;
+               goto end;
+       }
+
+       if (ext_header->filter_len) {
+               *filter_expression = ((const char *) (ext_header)) +
+                               sizeof(*ext_header);
+       } else {
+               *filter_expression = NULL;
+       }
+
+end:
+       return ret;
+}
+
+int lttng_event_get_exclusion_name_count(struct lttng_event *event)
+{
+       int ret;
+       struct lttcomm_event_extended_header *ext_header;
+
+       if (!event) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ext_header = event->extended.ptr;
+       if (!ext_header) {
+               /*
+                * This can happen since the lttng_event structure is
+                * used for other tasks where this pointer is never set.
+                */
+               ret = 0;
+               goto end;
+       }
+
+       if (ext_header->nb_exclusions > INT_MAX) {
+               ret = -LTTNG_ERR_OVERFLOW;
+               goto end;
+       }
+       ret = (int) ext_header->nb_exclusions;
+end:
+       return ret;
+}
+
+int lttng_event_get_exclusion_name(struct lttng_event *event,
+               size_t index, const char **exclusion_name)
+{
+       int ret = 0;
+       struct lttcomm_event_extended_header *ext_header;
+       void *at;
+
+       if (!event || !exclusion_name) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ext_header = event->extended.ptr;
+       if (!ext_header) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (index >= ext_header->nb_exclusions) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       at = (void *) ext_header + sizeof(*ext_header);
+       at += ext_header->filter_len;
+       at += index * LTTNG_SYMBOL_NAME_LEN;
+       *exclusion_name = at;
+
+end:
+       return ret;
 }
 
 /*
@@ -1696,26 +2031,13 @@ int lttng_set_tracing_group(const char *name)
        return 0;
 }
 
-/*
- * Returns size of returned session payload data or a negative error code.
- */
 int lttng_calibrate(struct lttng_handle *handle,
                struct lttng_calibrate *calibrate)
 {
-       struct lttcomm_session_msg lsm;
-
-       /* Safety check. NULL pointer are forbidden */
-       if (handle == NULL || calibrate == NULL) {
-               return -LTTNG_ERR_INVALID;
-       }
-
-       memset(&lsm, 0, sizeof(lsm));
-       lsm.cmd_type = LTTNG_CALIBRATE;
-       lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
-
-       memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
-
-       return lttng_ctl_ask_sessiond(&lsm, NULL);
+       /*
+        * This command was removed in LTTng 2.9.
+        */
+       return -LTTNG_ERR_UND;
 }
 
 /*
@@ -1725,11 +2047,14 @@ int lttng_calibrate(struct lttng_handle *handle,
 void lttng_channel_set_default_attr(struct lttng_domain *domain,
                struct lttng_channel_attr *attr)
 {
+       struct lttng_channel_extended *extended;
+
        /* Safety check */
        if (attr == NULL || domain == NULL) {
                return;
        }
 
+       extended = (struct lttng_channel_extended *) attr->extended.ptr;
        memset(attr, 0, sizeof(struct lttng_channel_attr));
 
        /* Same for all domains. */
@@ -1745,6 +2070,12 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                attr->subbuf_size = default_get_kernel_channel_subbuf_size();
                attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
                attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+               if (extended) {
+                       extended->monitor_timer_interval =
+                                       DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
+                       extended->blocking_timeout =
+                                       DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
+               }
                break;
        case LTTNG_DOMAIN_UST:
                switch (domain->buf_type) {
@@ -1756,6 +2087,12 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                                        DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
                        attr->read_timer_interval =
                                        DEFAULT_UST_UID_CHANNEL_READ_TIMER;
+                       if (extended) {
+                               extended->monitor_timer_interval =
+                                               DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
+                               extended->blocking_timeout =
+                                               DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
+                       }
                        break;
                case LTTNG_BUFFER_PER_PID:
                default:
@@ -1766,12 +2103,165 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                                        DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
                        attr->read_timer_interval =
                                        DEFAULT_UST_PID_CHANNEL_READ_TIMER;
+                       if (extended) {
+                               extended->monitor_timer_interval =
+                                               DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
+                               extended->blocking_timeout =
+                                               DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
+                       }
                        break;
                }
        default:
                /* Default behavior: leave set to 0. */
                break;
        }
+
+       attr->extended.ptr = extended;
+}
+
+int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
+               uint64_t *discarded_events)
+{
+       int ret = 0;
+       struct lttng_channel_extended *chan_ext;
+
+       if (!channel || !discarded_events) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       chan_ext = channel->attr.extended.ptr;
+       if (!chan_ext) {
+               /*
+                * This can happen since the lttng_channel structure is
+                * used for other tasks where this pointer is never set.
+                */
+               *discarded_events = 0;
+               goto end;
+       }
+
+       *discarded_events = chan_ext->discarded_events;
+end:
+       return ret;
+}
+
+int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
+               uint64_t *lost_packets)
+{
+       int ret = 0;
+       struct lttng_channel_extended *chan_ext;
+
+       if (!channel || !lost_packets) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       chan_ext = channel->attr.extended.ptr;
+       if (!chan_ext) {
+               /*
+                * This can happen since the lttng_channel structure is
+                * used for other tasks where this pointer is never set.
+                */
+               *lost_packets = 0;
+               goto end;
+       }
+
+       *lost_packets = chan_ext->lost_packets;
+end:
+       return ret;
+}
+
+int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
+               uint64_t *monitor_timer_interval)
+{
+       int ret = 0;
+
+       if (!chan || !monitor_timer_interval) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (!chan->attr.extended.ptr) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       *monitor_timer_interval = ((struct lttng_channel_extended *)
+                       chan->attr.extended.ptr)->monitor_timer_interval;
+end:
+       return ret;
+}
+
+int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
+               uint64_t monitor_timer_interval)
+{
+       int ret = 0;
+
+       if (!chan || !chan->attr.extended.ptr) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ((struct lttng_channel_extended *)
+                       chan->attr.extended.ptr)->monitor_timer_interval =
+                       monitor_timer_interval;
+end:
+       return ret;
+}
+
+int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
+               int64_t *blocking_timeout)
+{
+       int ret = 0;
+
+       if (!chan || !blocking_timeout) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (!chan->attr.extended.ptr) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       *blocking_timeout = ((struct lttng_channel_extended *)
+                       chan->attr.extended.ptr)->blocking_timeout;
+end:
+       return ret;
+}
+
+int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
+               int64_t blocking_timeout)
+{
+       int ret = 0;
+       int64_t msec_timeout;
+
+       if (!chan || !chan->attr.extended.ptr) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (blocking_timeout < 0 && blocking_timeout != -1) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       /*
+        * LTTng-ust's use of poll() to implement this timeout mechanism forces
+        * us to accept a narrower range of values (msecs expressed as a signed
+        * 32-bit integer).
+        */
+       msec_timeout = blocking_timeout / 1000;
+       if (msec_timeout != (int32_t) msec_timeout) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ((struct lttng_channel_extended *)
+                       chan->attr.extended.ptr)->blocking_timeout =
+                       blocking_timeout;
+end:
+       return ret;
 }
 
 /*
@@ -1840,7 +2330,7 @@ int lttng_set_consumer_url(struct lttng_handle *handle,
 
        lsm.u.uri.size = size;
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
                        sizeof(struct lttng_uri) * size, NULL);
 
        free(uris);
@@ -1923,7 +2413,7 @@ int _lttng_create_session_ext(const char *name, const char *url,
                }
        }
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
                        sizeof(struct lttng_uri) * size, NULL);
 
 error:
@@ -1995,9 +2485,22 @@ int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
 
        lsm.u.uri.size = size;
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
+       /*
+        * If the user does not specify a custom subdir, use the session name.
+        */
+       if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
+               ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
+               if (ret < 0) {
+                       PERROR("snprintf uri subdir");
+                       ret = -LTTNG_ERR_FATAL;
+                       goto error;
+               }
+       }
+
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
                        sizeof(struct lttng_uri) * size, NULL);
 
+error:
        free(uris);
        return ret;
 }
@@ -2043,7 +2546,7 @@ int lttng_create_session_live(const char *name, const char *url,
        lsm.u.session_live.nb_uri = size;
        lsm.u.session_live.timer_interval = timer_interval;
 
-       ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
                        sizeof(struct lttng_uri) * size, NULL);
 
 end:
@@ -2097,10 +2600,210 @@ int lttng_list_tracker_pids(struct lttng_handle *handle,
        return 0;
 }
 
+/*
+ * Regenerate the metadata for a session.
+ * Return 0 on success, a negative error code on error.
+ */
+int lttng_regenerate_metadata(const char *session_name)
+{
+       int ret;
+       struct lttcomm_session_msg lsm;
+
+       if (!session_name) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       memset(&lsm, 0, sizeof(lsm));
+       lsm.cmd_type = LTTNG_REGENERATE_METADATA;
+
+       lttng_ctl_copy_string(lsm.session.name, session_name,
+                       sizeof(lsm.session.name));
+
+       ret = lttng_ctl_ask_sessiond(&lsm, NULL);
+       if (ret < 0) {
+               goto end;
+       }
+
+       ret = 0;
+end:
+       return ret;
+}
+
+/*
+ * Deprecated, replaced by lttng_regenerate_metadata.
+ */
+int lttng_metadata_regenerate(const char *session_name)
+{
+       return lttng_regenerate_metadata(session_name);
+}
+
+/*
+ * Regenerate the statedump of a session.
+ * Return 0 on success, a negative error code on error.
+ */
+int lttng_regenerate_statedump(const char *session_name)
+{
+       int ret;
+       struct lttcomm_session_msg lsm;
+
+       if (!session_name) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       memset(&lsm, 0, sizeof(lsm));
+       lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
+
+       lttng_ctl_copy_string(lsm.session.name, session_name,
+                       sizeof(lsm.session.name));
+
+       ret = lttng_ctl_ask_sessiond(&lsm, NULL);
+       if (ret < 0) {
+               goto end;
+       }
+
+       ret = 0;
+end:
+       return ret;
+}
+
+int lttng_register_trigger(struct lttng_trigger *trigger)
+{
+       int ret;
+       struct lttcomm_session_msg lsm;
+       char *trigger_buf = NULL;
+       ssize_t trigger_size;
+
+       if (!trigger) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (!lttng_trigger_validate(trigger)) {
+               ret = -LTTNG_ERR_INVALID_TRIGGER;
+               goto end;
+       }
+
+       trigger_size = lttng_trigger_serialize(trigger, NULL);
+       if (trigger_size < 0) {
+               ret = -LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       trigger_buf = zmalloc(trigger_size);
+       if (!trigger_buf) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       memset(&lsm, 0, sizeof(lsm));
+       lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
+       if (lttng_trigger_serialize(trigger, trigger_buf) < 0) {
+               ret = -LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       lsm.u.trigger.length = (uint32_t) trigger_size;
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, trigger_buf,
+                       trigger_size, NULL);
+end:
+       free(trigger_buf);
+       return ret;
+}
+
+int lttng_unregister_trigger(struct lttng_trigger *trigger)
+{
+       int ret;
+       struct lttcomm_session_msg lsm;
+       char *trigger_buf = NULL;
+       ssize_t trigger_size;
+
+       if (!trigger) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (!lttng_trigger_validate(trigger)) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       trigger_size = lttng_trigger_serialize(trigger, NULL);
+       if (trigger_size < 0) {
+               ret = -LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       trigger_buf = zmalloc(trigger_size);
+       if (!trigger_buf) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       memset(&lsm, 0, sizeof(lsm));
+       lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
+       if (lttng_trigger_serialize(trigger, trigger_buf) < 0) {
+               ret = -LTTNG_ERR_UNK;
+               goto end;
+       }
+
+       lsm.u.trigger.length = (uint32_t) trigger_size;
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, trigger_buf,
+                       trigger_size, NULL);
+end:
+       free(trigger_buf);
+       return ret;
+}
+
+int lttng_session_get_current_archive_location(const char *session_name,
+               char **chunk_path)
+{
+       struct lttcomm_session_msg lsm;
+       struct lttng_session_get_current_output_return *output_return = NULL;
+       int ret;
+       size_t path_len;
+
+       memset(&lsm, 0, sizeof(lsm));
+       lsm.cmd_type = LTTNG_SESSION_GET_CURRENT_OUTPUT;
+       ret = lttng_strncpy(lsm.session.name, session_name,
+                       sizeof(lsm.session.name));
+       if (ret) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ret = lttng_ctl_ask_sessiond(&lsm, (void **) &output_return);
+       if (ret < 0) {
+               ret = -1;
+               goto end;
+       }
+
+       path_len = lttng_strnlen(output_return->path,
+                       sizeof(output_return->path));
+       if (path_len == 0 || path_len == sizeof(output_return->path)) {
+               ret = -LTTNG_ERR_NO_SESSION_OUTPUT;
+               goto end;
+       }
+
+       *chunk_path = zmalloc(path_len + 1);
+       if (!*chunk_path) {
+               ret = -1;
+               goto end;
+       }
+       memcpy(*chunk_path, output_return->path, path_len);
+
+       ret = 0;
+
+end:
+       free(output_return);
+       return ret;
+}
+
 /*
  * lib constructor.
  */
-static void __attribute__((constructor)) init()
+static void __attribute__((constructor)) init(void)
 {
        /* Set default session group */
        lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
@@ -2109,7 +2812,7 @@ static void __attribute__((constructor)) init()
 /*
  * lib destructor.
  */
-static void __attribute__((destructor)) lttng_ctl_exit()
+static void __attribute__((destructor)) lttng_ctl_exit(void)
 {
        free(tracing_group);
 }
This page took 0.037135 seconds and 5 git commands to generate.