Implement filter bytecode support in lttng-session, and parse filter string
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index 838afedba2a712297f73687b44e446a17fec4537..780b44dd7cdebba0ed70c69a4459abc42df38658 100644 (file)
@@ -5,19 +5,18 @@
  *
  * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; only
- * version 2.1 of the License.
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License, version 2.1 only,
+ * as published by the Free Software Foundation.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #define _GNU_SOURCE
 #include <string.h>
 #include <unistd.h>
 
-#include <common/sessiond-comm/sessiond-comm.h>
-#include <common/lttngerr.h>
 #include <common/common.h>
 #include <common/defaults.h>
+#include <common/sessiond-comm/sessiond-comm.h>
 #include <lttng/lttng.h>
 
+#include "filter-parser.h"
+#include "filter-ast.h"
+#include "filter-bytecode.h"
+#include "memstream.h"
+
+#ifdef DEBUG
+const int print_xml = 1;
+#define dbg_printf(fmt, args...)       \
+       printf("[debug liblttng-ctl] " fmt, ## args)
+#else
+const int print_xml = 0;
+#define dbg_printf(fmt, args...)                               \
+do {                                                           \
+       /* do nothing but check printf format */                \
+       if (0)                                                  \
+               printf("[debug liblttnctl] " fmt, ## args);     \
+} while (0)
+#endif
+
+
 /* Socket to session daemon for communication */
 static int sessiond_socket;
 static char sessiond_sock_path[PATH_MAX];
@@ -42,6 +60,18 @@ static char sessiond_sock_path[PATH_MAX];
 static char *tracing_group;
 static int connected;
 
+/* Global */
+
+/*
+ * Those two variables are used by error.h to silent or control the verbosity of
+ * error message. They are global to the library so application linking with it
+ * are able to compile correctly and also control verbosity of the library.
+ *
+ * Note that it is *not* possible to silent ERR() and PERROR() macros.
+ */
+int lttng_opt_quiet;
+int lttng_opt_verbose;
+
 /*
  * Copy string from src to dst and enforce null terminated byte.
  */
@@ -65,16 +95,19 @@ static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src
 {
        if (src && dst) {
                switch (src->type) {
-                       case LTTNG_DOMAIN_KERNEL:
-                       case LTTNG_DOMAIN_UST:
-                       case LTTNG_DOMAIN_UST_EXEC_NAME:
-                       case LTTNG_DOMAIN_UST_PID:
-                       case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
-                               memcpy(dst, src, sizeof(struct lttng_domain));
-                               break;
-                       default:
-                               dst->type = LTTNG_DOMAIN_KERNEL;
-                               break;
+               case LTTNG_DOMAIN_KERNEL:
+               case LTTNG_DOMAIN_UST:
+               /*
+               case LTTNG_DOMAIN_UST_EXEC_NAME:
+               case LTTNG_DOMAIN_UST_PID:
+               case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
+               */
+                       memcpy(dst, src, sizeof(struct lttng_domain));
+                       break;
+               default:
+                       memset(dst, 0, sizeof(struct lttng_domain));
+                       dst->type = LTTNG_DOMAIN_KERNEL;
+                       break;
                }
        }
 }
@@ -82,8 +115,8 @@ static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src
 /*
  * Send lttcomm_session_msg to the session daemon.
  *
- * On success, return 0
- * On error, return error code
+ * On success, returns the number of bytes sent (>=0)
+ * On error, returns -1
  */
 static int send_session_msg(struct lttcomm_session_msg *lsm)
 {
@@ -101,11 +134,36 @@ end:
        return ret;
 }
 
+/*
+ * Send var len data to the session daemon.
+ *
+ * On success, returns the number of bytes sent (>=0)
+ * On error, returns -1
+ */
+static int send_session_varlen(void *data, size_t len)
+{
+       int ret;
+
+       if (!connected) {
+               ret = -ENOTCONN;
+               goto end;
+       }
+       if (!data || !len) {
+               ret = 0;
+               goto end;
+       }
+
+       ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
+
+end:
+       return ret;
+}
+
 /*
  * Receive data from the sessiond socket.
  *
- * On success, return 0
- * On error, return recv() error code
+ * On success, returns the number of bytes received (>=0)
+ * On error, returns -1 (recvmsg() error) or -ENOTCONN
  */
 static int recv_data_sessiond(void *buf, size_t len)
 {
@@ -123,7 +181,7 @@ end:
 }
 
 /*
- *  Check if the specified group name exist.
+ *  Check if we are in the specified group.
  *
  *  If yes return 1, else return -1.
  */
@@ -136,11 +194,8 @@ static int check_tracing_group(const char *grp_name)
 
        /* Get GID of group 'tracing' */
        grp_tracing = getgrnam(grp_name);
-       if (grp_tracing == NULL) {
-               /* NULL means not found also. getgrnam(3) */
-               if (errno != 0) {
-                       perror("getgrnam");
-               }
+       if (!grp_tracing) {
+               /* If grp_tracing is NULL, the group does not exist. */
                goto end;
        }
 
@@ -154,11 +209,11 @@ static int check_tracing_group(const char *grp_name)
        /* Alloc group list of the right size */
        grp_list = malloc(grp_list_size * sizeof(gid_t));
        if (!grp_list) {
-               ret = -1;
+               perror("malloc");
                goto end;
        }
        grp_id = getgroups(grp_list_size, grp_list);
-       if (grp_id < -1) {
+       if (grp_id < 0) {
                perror("getgroups");
                goto free_list;
        }
@@ -208,8 +263,10 @@ static int try_connect_sessiond(const char *sock_path)
 }
 
 /*
- * Set sessiond socket path by putting it in the global sessiond_sock_path
- * variable.
+ * Set sessiond socket path by putting it in the global
+ * sessiond_sock_path variable.
+ * Returns 0 on success,
+ * -ENOMEM on failure (the sessiond socket path is somehow too long)
  */
 static int set_session_daemon_path(void)
 {
@@ -224,35 +281,33 @@ static int set_session_daemon_path(void)
                in_tgroup = check_tracing_group(tracing_group);
        }
 
-       if (uid == 0) {
-               /* Root */
-               copy_string(sessiond_sock_path,
-                               DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
-                               sizeof(sessiond_sock_path));
-       } else if (in_tgroup) {
-               /* Tracing group */
-               copy_string(sessiond_sock_path,
-                               DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
+       if ((uid == 0) || in_tgroup) {
+               copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
                                sizeof(sessiond_sock_path));
+       }
 
-               ret = try_connect_sessiond(sessiond_sock_path);
-               if (ret < 0) {
-                       /* Global session daemon not available */
-                       if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
-                                               DEFAULT_HOME_CLIENT_UNIX_SOCK,
-                                               getenv("HOME")) < 0) {
-                               return -ENOMEM;
+       if (uid != 0) {
+               if (in_tgroup) {
+                       /* Tracing group */
+                       ret = try_connect_sessiond(sessiond_sock_path);
+                       if (ret >= 0) {
+                               goto end;
                        }
+                       /* Global session daemon not available... */
                }
-       } else {
-               /* Not in tracing group and not root, default */
-               if (snprintf(sessiond_sock_path, PATH_MAX,
-                                       DEFAULT_HOME_CLIENT_UNIX_SOCK,
-                                       getenv("HOME")) < 0) {
+               /* ...or not in tracing group (and not root), default */
+
+               /*
+                * With GNU C <  2.1, snprintf returns -1 if the target buffer is too small;
+                * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
+                */
+               ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
+                               DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
+               if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
                        return -ENOMEM;
                }
        }
-
+end:
        return 0;
 }
 
@@ -267,7 +322,7 @@ static int connect_sessiond(void)
 
        ret = set_session_daemon_path();
        if (ret < 0) {
-               return ret;
+               return -1; /* set_session_daemon_path() returns -ENOMEM */
        }
 
        /* Connect to the sesssion daemon */
@@ -283,7 +338,8 @@ static int connect_sessiond(void)
 }
 
 /*
- *  Clean disconnect the session daemon.
+ *  Clean disconnect from the session daemon.
+ *  On success, return 0. On error, return -1.
  */
 static int disconnect_sessiond(void)
 {
@@ -300,10 +356,14 @@ static int disconnect_sessiond(void)
 
 /*
  * 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.
  *
- * Return size of data (only payload, not header).
+ * Return size of data (only payload, not header) or a negative error code.
  */
-static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
+static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
+               void *vardata,
+               size_t varlen,
+               void **buf)
 {
        int ret;
        size_t size;
@@ -320,6 +380,11 @@ static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
        if (ret < 0) {
                goto end;
        }
+       /* Send var len data */
+       ret = send_session_varlen(vardata, varlen);
+       if (ret < 0) {
+               goto end;
+       }
 
        /* Get header from data transmission */
        ret = recv_data_sessiond(&llm, sizeof(llm));
@@ -370,8 +435,19 @@ end:
        return ret;
 }
 
+/*
+ * Ask the session daemon a specific command and put the data into buf.
+ *
+ * Return size of data (only payload, not header) or a negative error code.
+ */
+static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
+{
+       return ask_sessiond_varlen(lsm, NULL, 0, buf);
+}
+
 /*
  * Create lttng handle and return pointer.
+ * The returned pointer will be NULL in case of malloc() error.
  */
 struct lttng_handle *lttng_create_handle(const char *session_name,
                struct lttng_domain *domain)
@@ -407,6 +483,7 @@ void lttng_destroy_handle(struct lttng_handle *handle)
 
 /*
  * Register an outside consumer.
+ * Returns size of returned session payload data or a negative error code.
  */
 int lttng_register_consumer(struct lttng_handle *handle,
                const char *socket_path)
@@ -424,7 +501,8 @@ int lttng_register_consumer(struct lttng_handle *handle,
 }
 
 /*
- *  Start tracing for all trace of the session.
+ *  Start tracing for all traces of the session.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_start_tracing(const char *session_name)
 {
@@ -442,7 +520,8 @@ int lttng_start_tracing(const char *session_name)
 }
 
 /*
- *  Stop tracing for all trace of the session.
+ *  Stop tracing for all traces of the session.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_stop_tracing(const char *session_name)
 {
@@ -460,7 +539,12 @@ int lttng_stop_tracing(const char *session_name)
 }
 
 /*
- * Add context to event or/and channel.
+ * Add context to event and/or channel.
+ * If event_name is NULL, the context is applied to all events of the channel.
+ * If channel_name is NULL, a lookup of the event's channel is done.
+ * If both are NULL, the context is applied to all events of all channels.
+ *
+ * Returns the size of the returned payload data or a negative error code.
  */
 int lttng_add_context(struct lttng_handle *handle,
                struct lttng_event_context *ctx, const char *event_name,
@@ -473,6 +557,8 @@ int lttng_add_context(struct lttng_handle *handle,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        lsm.cmd_type = LTTNG_ADD_CONTEXT;
 
        /* Copy channel name */
@@ -493,7 +579,10 @@ int lttng_add_context(struct lttng_handle *handle,
 }
 
 /*
- * Enable event
+ *  Enable event(s) for a channel.
+ *  If no event name is specified, all events are enabled.
+ *  If no channel name is specified, the default 'channel0' is used.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_enable_event(struct lttng_handle *handle,
                struct lttng_event *ev, const char *channel_name)
@@ -504,6 +593,8 @@ int lttng_enable_event(struct lttng_handle *handle,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        /* If no channel name, we put the default name */
        if (channel_name == NULL) {
                copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
@@ -529,7 +620,143 @@ int lttng_enable_event(struct lttng_handle *handle,
 }
 
 /*
- * Disable event of a channel and domain.
+ * set filter for an event
+ * Return negative error value on error.
+ * Return size of returned session payload data if OK.
+ */
+
+int lttng_set_event_filter(struct lttng_handle *handle,
+               const char *event_name, const char *channel_name,
+               const char *filter_expression)
+{
+       struct lttcomm_session_msg lsm;
+       struct filter_parser_ctx *ctx;
+       FILE *fmem;
+       int ret = 0;
+
+       /* Safety check. */
+       if (handle == NULL) {
+               return -1;
+       }
+
+       if (!filter_expression) {
+               return 0;
+       }
+
+       /*
+        * casting const to non-const, as the underlying function will
+        * use it in read-only mode.
+        */
+       fmem = lttng_fmemopen((void *) filter_expression,
+                       strlen(filter_expression), "r");
+       if (!fmem) {
+               fprintf(stderr, "Error opening memory as stream\n");
+               return -ENOMEM;
+       }
+       ctx = filter_parser_ctx_alloc(fmem);
+       if (!ctx) {
+               fprintf(stderr, "Error allocating parser\n");
+               ret = -ENOMEM;
+               goto alloc_error;
+       }
+       ret = filter_parser_ctx_append_ast(ctx);
+       if (ret) {
+               fprintf(stderr, "Parse error\n");
+               ret = -EINVAL;
+               goto parse_error;
+       }
+       ret = filter_visitor_set_parent(ctx);
+       if (ret) {
+               fprintf(stderr, "Set parent error\n");
+               ret = -EINVAL;
+               goto parse_error;
+       }
+       if (print_xml) {
+               ret = filter_visitor_print_xml(ctx, stdout, 0);
+               if (ret) {
+                       fflush(stdout);
+                       fprintf(stderr, "XML print error\n");
+                       ret = -EINVAL;
+                       goto parse_error;
+               }
+       }
+
+       dbg_printf("Generating IR... ");
+       fflush(stdout);
+       ret = filter_visitor_ir_generate(ctx);
+       if (ret) {
+               fprintf(stderr, "Generate IR error\n");
+               ret = -EINVAL;
+               goto parse_error;
+       }
+       dbg_printf("done\n");
+
+       dbg_printf("Validating IR... ");
+       fflush(stdout);
+       ret = filter_visitor_ir_check_binary_op_nesting(ctx);
+       if (ret) {
+               ret = -EINVAL;
+               goto parse_error;
+       }
+       dbg_printf("done\n");
+
+       dbg_printf("Generating bytecode... ");
+       fflush(stdout);
+       ret = filter_visitor_bytecode_generate(ctx);
+       if (ret) {
+               fprintf(stderr, "Generate bytecode error\n");
+               ret = -EINVAL;
+               goto parse_error;
+       }
+       dbg_printf("done\n");
+       dbg_printf("Size of bytecode generated: %u bytes.\n",
+               bytecode_get_len(&ctx->bytecode->b));
+
+       memset(&lsm, 0, sizeof(lsm));
+
+       lsm.cmd_type = LTTNG_SET_FILTER;
+
+       /* Copy channel name */
+       copy_string(lsm.u.filter.channel_name, channel_name,
+                       sizeof(lsm.u.filter.channel_name));
+       /* Copy event name */
+       copy_string(lsm.u.filter.event_name, event_name,
+                       sizeof(lsm.u.filter.event_name));
+       lsm.u.filter.bytecode_len = sizeof(ctx->bytecode->b)
+                       + bytecode_get_len(&ctx->bytecode->b);
+
+       copy_lttng_domain(&lsm.domain, &handle->domain);
+
+       copy_string(lsm.session.name, handle->session_name,
+                       sizeof(lsm.session.name));
+
+       ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
+                               lsm.u.filter.bytecode_len, NULL);
+
+       filter_bytecode_free(ctx);
+       filter_ir_free(ctx);
+       filter_parser_ctx_free(ctx);
+       if (fclose(fmem) != 0) {
+               perror("fclose");
+       }
+       return ret;
+
+parse_error:
+       filter_bytecode_free(ctx);
+       filter_ir_free(ctx);
+       filter_parser_ctx_free(ctx);
+alloc_error:
+       if (fclose(fmem) != 0) {
+               perror("fclose");
+       }
+       return ret;
+}
+
+/*
+ *  Disable event(s) of a channel and domain.
+ *  If no event name is specified, all events are disabled.
+ *  If no channel name is specified, the default 'channel0' is used.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_disable_event(struct lttng_handle *handle, const char *name,
                const char *channel_name)
@@ -540,6 +767,8 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        if (channel_name) {
                copy_string(lsm.u.disable.channel_name, channel_name,
                                sizeof(lsm.u.disable.channel_name));
@@ -564,7 +793,8 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name,
 }
 
 /*
- * Enable channel per domain
+ *  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)
@@ -578,6 +808,8 @@ int lttng_enable_channel(struct lttng_handle *handle,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
 
        lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
@@ -591,7 +823,8 @@ int lttng_enable_channel(struct lttng_handle *handle,
 }
 
 /*
- * All tracing will be stopped for registered events of the channel.
+ *  All tracing will be stopped for registered events of the channel.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
 {
@@ -602,6 +835,8 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name)
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
 
        copy_string(lsm.u.disable.channel_name, name,
@@ -616,10 +851,10 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name)
 }
 
 /*
- * List all available tracepoints of domain.
- *
- * Return the size (bytes) of the list and set the events array.
- * On error, return negative value.
+ *  Lists all available tracepoints of domain.
+ *  Sets the contents of the events array.
+ *  Returns the number of lttng_event entries in events;
+ *  on error, returns a negative value.
  */
 int lttng_list_tracepoints(struct lttng_handle *handle,
                struct lttng_event **events)
@@ -643,10 +878,39 @@ int lttng_list_tracepoints(struct lttng_handle *handle,
 }
 
 /*
- *  Return a human readable string of code
+ *  Lists all available tracepoint fields of domain.
+ *  Sets the contents of the event field array.
+ *  Returns the number of lttng_event_field entries in events;
+ *  on error, returns a negative value.
+ */
+int lttng_list_tracepoint_fields(struct lttng_handle *handle,
+               struct lttng_event_field **fields)
+{
+       int ret;
+       struct lttcomm_session_msg lsm;
+
+       if (handle == NULL) {
+               return -1;
+       }
+
+       lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
+       copy_lttng_domain(&lsm.domain, &handle->domain);
+
+       ret = ask_sessiond(&lsm, (void **) fields);
+       if (ret < 0) {
+               return ret;
+       }
+
+       return ret / sizeof(struct lttng_event_field);
+}
+
+/*
+ *  Returns a human readable string describing
+ *  the error code (a negative value).
  */
 const char *lttng_strerror(int code)
 {
+       /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
        if (code > -LTTCOMM_OK) {
                return "Ended with errors";
        }
@@ -655,7 +919,8 @@ const char *lttng_strerror(int code)
 }
 
 /*
- *  Create a brand new session using name.
+ *  Create a brand new session using name and path.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_create_session(const char *name, const char *path)
 {
@@ -668,8 +933,42 @@ int lttng_create_session(const char *name, const char *path)
        return ask_sessiond(&lsm, NULL);
 }
 
+/*
+ * Create a new tracing session using a name, URIs and a consumer enable flag.
+ */
+int lttng_create_session_uri(const char *name, struct lttng_uri *ctrl_uri,
+               struct lttng_uri *data_uri, unsigned int enable_consumer)
+{
+       struct lttcomm_session_msg lsm;
+
+       /* Name and ctrl_uri are mandatory */
+       if (name == NULL || ctrl_uri == NULL) {
+               return -1;
+       }
+
+       lsm.cmd_type = LTTNG_CREATE_SESSION_URI;
+
+       copy_string(lsm.session.name, name, sizeof(lsm.session.name));
+       /* Anything bigger than zero, the consumer(s) will be enabled */
+       lsm.u.create_uri.enable_consumer = enable_consumer;
+       memcpy(&lsm.u.create_uri.ctrl_uri, ctrl_uri,
+                       sizeof(lsm.u.create_uri.ctrl_uri));
+       if (data_uri) {
+               /*
+                * The only possible scenario where data_uri is NULL is for a local
+                * consumer where the output is at a specified path name on the
+                * filesystem.
+                */
+               memcpy(&lsm.u.create_uri.data_uri, data_uri,
+                               sizeof(lsm.u.create_uri.data_uri));
+       }
+
+       return ask_sessiond(&lsm, NULL);
+}
+
 /*
  *  Destroy session using name.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_destroy_session(const char *session_name)
 {
@@ -688,9 +987,9 @@ int lttng_destroy_session(const char *session_name)
 
 /*
  *  Ask the session daemon for all available sessions.
- *
- *  Return number of session.
- *  On error, return negative value.
+ *  Sets the contents of the sessions array.
+ *  Returns the number of lttng_session entries in sessions;
+ *  on error, returns a negative value.
  */
 int lttng_list_sessions(struct lttng_session **sessions)
 {
@@ -707,7 +1006,10 @@ int lttng_list_sessions(struct lttng_session **sessions)
 }
 
 /*
- * List domain of a session.
+ *  Ask the session daemon for all available domains of a session.
+ *  Sets the contents of the domains array.
+ *  Returns the number of lttng_domain entries in domains;
+ *  on error, returns a negative value.
  */
 int lttng_list_domains(const char *session_name,
                struct lttng_domain **domains)
@@ -732,7 +1034,10 @@ int lttng_list_domains(const char *session_name,
 }
 
 /*
- * List channels of a session
+ *  Ask the session daemon for all available channels of a session.
+ *  Sets the contents of the channels array.
+ *  Returns the number of lttng_channel entries in channels;
+ *  on error, returns a negative value.
  */
 int lttng_list_channels(struct lttng_handle *handle,
                struct lttng_channel **channels)
@@ -759,7 +1064,10 @@ int lttng_list_channels(struct lttng_handle *handle,
 }
 
 /*
- * List events of a session channel.
+ *  Ask the session daemon for all available events of a session channel.
+ *  Sets the contents of the events array.
+ *  Returns the number of lttng_event entries in events;
+ *  on error, returns a negative value.
  */
 int lttng_list_events(struct lttng_handle *handle,
                const char *channel_name, struct lttng_event **events)
@@ -789,8 +1097,9 @@ int lttng_list_events(struct lttng_handle *handle,
 }
 
 /*
- * Set tracing group variable with name. This function allocate memory pointed
- * by tracing_group.
+ * Sets the tracing_group variable with name.
+ * This function allocates memory pointed to by tracing_group.
+ * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
  */
 int lttng_set_tracing_group(const char *name)
 {
@@ -806,7 +1115,7 @@ int lttng_set_tracing_group(const char *name)
 }
 
 /*
- *  lttng_calibrate
+ * Returns size of returned session payload data or a negative error code.
  */
 int lttng_calibrate(struct lttng_handle *handle,
                struct lttng_calibrate *calibrate)
@@ -828,6 +1137,7 @@ int lttng_calibrate(struct lttng_handle *handle,
 
 /*
  * Set default channel attributes.
+ * If either or both of the arguments are null, attr content is zeroe'd.
  */
 void lttng_channel_set_default_attr(struct lttng_domain *domain,
                struct lttng_channel_attr *attr)
@@ -837,6 +1147,8 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                return;
        }
 
+       memset(attr, 0, sizeof(struct lttng_channel_attr));
+
        switch (domain->type) {
        case LTTNG_DOMAIN_KERNEL:
                attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
@@ -848,9 +1160,11 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
                break;
        case LTTNG_DOMAIN_UST:
+#if 0
        case LTTNG_DOMAIN_UST_EXEC_NAME:
        case LTTNG_DOMAIN_UST_PID:
        case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
+#endif
                attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
                attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
                attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
@@ -860,8 +1174,7 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
                break;
        default:
-               /* Default behavior */
-               memset(attr, 0, sizeof(struct lttng_channel_attr));
+               /* Default behavior: leave set to 0. */
                break;
        }
 }
@@ -870,7 +1183,7 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
  * Check if session daemon is alive.
  *
  * Return 1 if alive or 0 if not.
- * On error return -1
+ * On error returns a negative value.
  */
 int lttng_session_daemon_alive(void)
 {
@@ -897,11 +1210,79 @@ int lttng_session_daemon_alive(void)
        return 1;
 }
 
+/*
+ * Set URI for a consumer for a session and domain.
+ *
+ * Return 0 on success, else a negative value.
+ */
+int lttng_set_consumer_uri(struct lttng_handle *handle, struct lttng_uri *uri)
+{
+       struct lttcomm_session_msg lsm;
+
+       if (handle == NULL || uri == NULL) {
+               return -1;
+       }
+
+       lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
+
+       copy_string(lsm.session.name, handle->session_name,
+                       sizeof(lsm.session.name));
+       copy_lttng_domain(&lsm.domain, &handle->domain);
+
+       memcpy(&lsm.u.uri, uri, sizeof(lsm.u.uri));
+
+       return ask_sessiond(&lsm, NULL);
+}
+
+/*
+ * Enable consumer for a session and domain.
+ *
+ * Return 0 on success, else a negative value.
+ */
+int lttng_enable_consumer(struct lttng_handle *handle)
+{
+       struct lttcomm_session_msg lsm;
+
+       if (handle == NULL) {
+               return -1;
+       }
+
+       lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
+
+       copy_string(lsm.session.name, handle->session_name,
+                       sizeof(lsm.session.name));
+       copy_lttng_domain(&lsm.domain, &handle->domain);
+
+       return ask_sessiond(&lsm, NULL);
+}
+
+/*
+ * Disable consumer for a session and domain.
+ *
+ * Return 0 on success, else a negative value.
+ */
+int lttng_disable_consumer(struct lttng_handle *handle)
+{
+       struct lttcomm_session_msg lsm;
+
+       if (handle == NULL) {
+               return -1;
+       }
+
+       lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
+
+       copy_string(lsm.session.name, handle->session_name,
+                       sizeof(lsm.session.name));
+       copy_lttng_domain(&lsm.domain, &handle->domain);
+
+       return ask_sessiond(&lsm, NULL);
+}
+
 /*
  * lib constructor
  */
 static void __attribute__((constructor)) init()
 {
        /* Set default session group */
-       lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
+       lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
 }
This page took 0.03272 seconds and 4 git commands to generate.