Implement filter expression to bytecode compiler in liblttng-ctl
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index c62dcfa1d4b335748f5c192fe8b712120a71decb..a05a8049ec5dd7186fe83fa01ce83478a41f392f 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
@@ -41,6 +40,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.
  */
@@ -64,18 +75,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;
                }
        }
 }
@@ -137,11 +149,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;
        }
 
@@ -227,35 +236,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;
 }
 
@@ -468,7 +475,10 @@ 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.
  */
@@ -483,6 +493,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 */
@@ -517,6 +529,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,
@@ -556,6 +570,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));
@@ -595,6 +611,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;
@@ -620,6 +638,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,
@@ -660,6 +680,33 @@ int lttng_list_tracepoints(struct lttng_handle *handle,
        return ret / sizeof(struct lttng_event);
 }
 
+/*
+ *  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).
@@ -689,6 +736,39 @@ 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.
@@ -860,7 +940,7 @@ int lttng_calibrate(struct lttng_handle *handle,
 
 /*
  * Set default channel attributes.
- * If either or both of the arguments are null, nothing happens.
+ * 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)
@@ -870,6 +950,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;
@@ -895,8 +977,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;
        }
 }
@@ -932,6 +1013,74 @@ 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
  */
This page took 0.028476 seconds and 4 git commands to generate.