Rename liblttsessiondcomm to liblttng-sessiond-comm, install it.
[lttng-tools.git] / liblttngctl / liblttngctl.c
index e693123b9c1c384b5d18f8f9eaa1d64984fe991d..ec01859a9aa00b3394f6bfe29ac89abbfb976ae6 100644 (file)
@@ -1,19 +1,23 @@
 /*
+ * liblttngctl.c
+ *
+ * Linux Trace Toolkit Control Library
+ *
  * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- * 
- * This program is distributed in the hope that it will be useful,
+ * 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 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 General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * 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
  */
 
 #define _GNU_SOURCE
@@ -26,7 +30,7 @@
 
 #include <lttng/lttng.h>
 
-#include "liblttsessiondcomm.h"
+#include <lttng/lttng-sessiond-comm.h>
 #include "lttngerr.h"
 #include "lttng-share.h"
 
@@ -42,6 +46,18 @@ static struct lttcomm_lttng_msg llm;
 static char *tracing_group;
 static int connected;
 
+/*
+ * Copy string from src to dst and enforce null terminated byte.
+ */
+static void copy_string(char *dst, const char *src, size_t len)
+{
+       if (src && dst) {
+               strncpy(dst, src, len);
+               /* Enforce the NULL terminated byte */
+               dst[len - 1] = '\0';
+       }
+}
+
 /*
  *  send_data_sessiond
  *
@@ -150,13 +166,14 @@ static int set_session_daemon_path(void)
        /* Are we in the tracing group ? */
        ret = check_tracing_group(tracing_group);
        if (ret < 0 && getuid() != 0) {
-               if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
-                                       getenv("HOME")) < 0) {
+               if (snprintf(sessiond_sock_path, PATH_MAX,
+                            DEFAULT_HOME_CLIENT_UNIX_SOCK,
+                            getenv("HOME")) < 0) {
                        return -ENOMEM;
                }
        } else {
-               strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
-                               sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
+               copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
+                           PATH_MAX);
        }
 
        return 0;
@@ -204,6 +221,14 @@ static int disconnect_sessiond(void)
        return ret;
 }
 
+/*
+ * Reset the session message structure.
+ */
+static void reset_session_msg(void)
+{
+       memset(&lsm, 0, sizeof(struct lttcomm_session_msg));
+}
+
 /*
  *  ask_sessiond
  *
@@ -262,15 +287,38 @@ static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
 
 end:
        disconnect_sessiond();
+       reset_session_msg();
        return ret;
 }
 
+/*
+ * Copy domain to lttcomm_session_msg domain. If unknown domain, default domain
+ * will be the kernel.
+ */
+static void copy_lttng_domain(struct lttng_domain *dom)
+{
+       if (dom) {
+               switch (dom->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(&lsm.domain, dom, sizeof(struct lttng_domain));
+                       break;
+               default:
+                       lsm.domain.type = LTTNG_DOMAIN_KERNEL;
+                       break;
+               }
+       }
+}
+
 /*
  *  Start tracing for all trace of the session.
  */
 int lttng_start_tracing(const char *session_name)
 {
-       strncpy(lsm.session_name, session_name, NAME_MAX);
+       copy_string(lsm.session.name, session_name, NAME_MAX);
        return ask_sessiond(LTTNG_START_TRACE, NULL);
 }
 
@@ -279,7 +327,7 @@ int lttng_start_tracing(const char *session_name)
  */
 int lttng_stop_tracing(const char *session_name)
 {
-       strncpy(lsm.session_name, session_name, NAME_MAX);
+       copy_string(lsm.session.name, session_name, NAME_MAX);
        return ask_sessiond(LTTNG_STOP_TRACE, NULL);
 }
 
@@ -290,31 +338,15 @@ int lttng_add_context(struct lttng_domain *domain,
                struct lttng_event_context *ctx, const char *event_name,
                const char *channel_name)
 {
-       int ret;
-
-       if (channel_name != NULL) {
-               strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
-       }
+       copy_string(lsm.u.context.channel_name, channel_name, NAME_MAX);
+       copy_string(lsm.u.context.event_name, event_name, NAME_MAX);
+       copy_lttng_domain(domain);
 
-       if (event_name != NULL) {
-               strncpy(lsm.u.context.event_name, event_name, NAME_MAX);
+       if (ctx) {
+               memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
        }
 
-       memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
-
-       switch (domain->type) {
-       case LTTNG_DOMAIN_KERNEL:
-               ret = ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL);
-               break;
-       case LTTNG_DOMAIN_UST:
-               ret = LTTCOMM_NOT_IMPLEMENTED;
-               break;
-       default:
-               ret = LTTCOMM_UNKNOWN_DOMAIN;
-               break;
-       };
-
-       return ret;
+       return ask_sessiond(LTTNG_ADD_CONTEXT, NULL);
 }
 
 /*
@@ -326,137 +358,94 @@ int lttng_enable_event(struct lttng_domain *domain,
        int ret;
 
        if (channel_name == NULL) {
-               strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
+               copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
        } else {
-               strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
+               copy_string(lsm.u.enable.channel_name, channel_name, NAME_MAX);
        }
 
-       switch (domain->type) {
-               case LTTNG_DOMAIN_KERNEL:
-                       if (ev == NULL) {
-                               ret = ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT, NULL);
-                       } else {
-                               memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
-                               ret = ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT, NULL);
-                       }
-                       break;
-               case LTTNG_DOMAIN_UST:
-                       ret = LTTCOMM_NOT_IMPLEMENTED;
-                       break;
-               default:
-                       ret = LTTCOMM_UNKNOWN_DOMAIN;
-                       break;
-       };
+       copy_lttng_domain(domain);
+
+       if (ev == NULL) {
+               ret = ask_sessiond(LTTNG_ENABLE_ALL_EVENT, NULL);
+       } else {
+               memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
+               ret = ask_sessiond(LTTNG_ENABLE_EVENT, NULL);
+       }
 
        return ret;
 }
 
 /*
- * Disable an event in the kernel tracer.
+ * Disable event of a channel and domain.
  */
 int lttng_disable_event(struct lttng_domain *domain, const char *name,
                const char *channel_name)
 {
-       int ret;
+       int ret = -1;
 
        if (channel_name == NULL) {
-               strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
+               copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
        } else {
-               strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
+               copy_string(lsm.u.disable.channel_name, channel_name, NAME_MAX);
        }
 
-       switch (domain->type) {
-               case LTTNG_DOMAIN_KERNEL:
-                       if (name == NULL) {
-                               ret = ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT, NULL);
-                       } else {
-                               strncpy(lsm.u.disable.name, name, NAME_MAX);
-                               ret = ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT, NULL);
-                       }
-                       break;
-               case LTTNG_DOMAIN_UST:
-                       ret = LTTCOMM_NOT_IMPLEMENTED;
-                       break;
-               default:
-                       ret = LTTCOMM_UNKNOWN_DOMAIN;
-                       break;
-       };
+       copy_lttng_domain(domain);
+
+       if (name == NULL) {
+               ret = ask_sessiond(LTTNG_DISABLE_ALL_EVENT, NULL);
+       } else {
+               copy_string(lsm.u.disable.name, name, NAME_MAX);
+               ret = ask_sessiond(LTTNG_DISABLE_EVENT, NULL);
+       }
 
        return ret;
 }
 
 /*
- * Enable recording for a channel for the kernel tracer.
+ * Enable channel per domain
  */
 int lttng_enable_channel(struct lttng_domain *domain,
                struct lttng_channel *chan)
 {
-       int ret;
+       if (chan) {
+               memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
+       }
 
-       memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
+       copy_lttng_domain(domain);
 
-       switch (domain->type) {
-               case LTTNG_DOMAIN_KERNEL:
-                       ret = ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
-                       break;
-               case LTTNG_DOMAIN_UST:
-                       ret = LTTCOMM_NOT_IMPLEMENTED;
-                       break;
-               default:
-                       ret = LTTCOMM_UNKNOWN_DOMAIN;
-                       break;
-       };
-
-       return ret;
+       return ask_sessiond(LTTNG_ENABLE_CHANNEL, NULL);
 }
 
 /*
- * Disable recording for the channel for the kernel tracer.
+ * All tracing will be stopped for registered events of the channel.
  */
 int lttng_disable_channel(struct lttng_domain *domain, const char *name)
 {
-       int ret;
+       copy_string(lsm.u.disable.channel_name, name, NAME_MAX);
+       copy_lttng_domain(domain);
 
-       strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
-
-       switch (domain->type) {
-               case LTTNG_DOMAIN_KERNEL:
-                       ret = ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
-                       break;
-               case LTTNG_DOMAIN_UST:
-                       ret = LTTCOMM_NOT_IMPLEMENTED;
-                       break;
-               default:
-                       ret = LTTCOMM_UNKNOWN_DOMAIN;
-                       break;
-       };
-
-       return ret;
+       return ask_sessiond(LTTNG_DISABLE_CHANNEL, NULL);
 }
 
 /*
- * List all available events in the kernel.
+ * List all available tracepoints of domain.
  *
- * Return the size (bytes) of the list and set the event_list array.
+ * Return the size (bytes) of the list and set the events array.
  * On error, return negative value.
  */
-int lttng_list_events(struct lttng_domain *domain, char **event_list)
+int lttng_list_tracepoints(struct lttng_domain *domain,
+               struct lttng_event **events)
 {
        int ret;
 
-       switch (domain->type) {
-               case LTTNG_DOMAIN_KERNEL:
-                       ret = ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
-                       break;
-               case LTTNG_DOMAIN_UST:
-                       ret = LTTCOMM_NOT_IMPLEMENTED;
-                       break;
-               default:
-                       ret = LTTCOMM_UNKNOWN_DOMAIN;
-                       break;
-       };
+       copy_lttng_domain(domain);
 
-       return ret;
+       ret = ask_sessiond(LTTNG_LIST_TRACEPOINTS, (void **) events);
+       if (ret < 0) {
+               return ret;
+       }
+
+       return ret / sizeof(struct lttng_event);
 }
 
 /*
@@ -476,8 +465,8 @@ const char *lttng_get_readable_code(int code)
  */
 int lttng_create_session(const char *name, const char *path)
 {
-       strncpy(lsm.session_name, name, NAME_MAX);
-       strncpy(lsm.path, path, PATH_MAX);
+       copy_string(lsm.session.name, name, NAME_MAX);
+       copy_string(lsm.session.path, path, PATH_MAX);
        return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
 }
 
@@ -486,7 +475,7 @@ int lttng_create_session(const char *name, const char *path)
  */
 int lttng_destroy_session(const char *name)
 {
-       strncpy(lsm.session_name, name, NAME_MAX);
+       copy_string(lsm.session.name, name, NAME_MAX);
        return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
 }
 
@@ -508,12 +497,68 @@ int lttng_list_sessions(struct lttng_session **sessions)
        return ret / sizeof(struct lttng_session);
 }
 
+/*
+ * List domain of a session.
+ */
+int lttng_list_domains(const char *session_name, struct lttng_domain **domains)
+{
+       int ret;
+
+       copy_string(lsm.session.name, session_name, NAME_MAX);
+       ret = ask_sessiond(LTTNG_LIST_DOMAINS, (void**) domains);
+       if (ret < 0) {
+               return ret;
+       }
+
+       return ret / sizeof(struct lttng_domain);
+}
+
+/*
+ * List channels of a session
+ */
+int lttng_list_channels(struct lttng_domain *domain,
+               const char *session_name, struct lttng_channel **channels)
+{
+       int ret;
+
+       copy_string(lsm.session.name, session_name, NAME_MAX);
+       copy_lttng_domain(domain);
+
+       ret = ask_sessiond(LTTNG_LIST_CHANNELS, (void**) channels);
+       if (ret < 0) {
+               return ret;
+       }
+
+       return ret / sizeof(struct lttng_channel);
+}
+
+/*
+ * List events of a session channel.
+ */
+int lttng_list_events(struct lttng_domain *domain,
+               const char *session_name, const char *channel_name,
+               struct lttng_event **events)
+{
+       int ret;
+
+       copy_string(lsm.session.name, session_name, NAME_MAX);
+       copy_string(lsm.u.list.channel_name, channel_name, NAME_MAX);
+       copy_lttng_domain(domain);
+
+       ret = ask_sessiond(LTTNG_LIST_EVENTS, (void**) events);
+       if (ret < 0) {
+               return ret;
+       }
+
+       return ret / sizeof(struct lttng_event);
+}
+
 /*
  * Set session name for the current lsm.
  */
 void lttng_set_session_name(const char *name)
 {
-       strncpy(lsm.session_name, name, NAME_MAX);
+       copy_string(lsm.session.name, name, NAME_MAX);
 }
 
 /*
@@ -531,6 +576,22 @@ int lttng_set_tracing_group(const char *name)
        return 0;
 }
 
+/*
+ *  lttng_calibrate
+ */
+int lttng_calibrate(struct lttng_domain *domain,
+               struct lttng_calibrate *calibrate)
+{
+       int ret;
+
+       copy_lttng_domain(domain);
+
+       memcpy(&lsm.u.calibrate, calibrate, sizeof(struct lttng_calibrate));
+       ret = ask_sessiond(LTTNG_CALIBRATE, NULL);
+
+       return ret;
+}
+
 /*
  *  lttng_check_session_daemon
  *
@@ -548,13 +609,22 @@ int lttng_session_daemon_alive(void)
                return ret;
        }
 
-       /* If socket exist, we consider the daemon started */
+       /* If socket exist, we check if the daemon listens to connect. */
        ret = access(sessiond_sock_path, F_OK);
        if (ret < 0) {
                /* Not alive */
                return 0;
        }
 
+       ret = lttcomm_connect_unix_sock(sessiond_sock_path);
+       if (ret < 0) {
+               /* Not alive */
+               return 0;
+       }
+       ret = lttcomm_close_unix_sock(ret);
+       if (ret < 0)
+               perror("lttcomm_close_unix_sock");
+
        /* Is alive */
        return 1;
 }
This page took 0.029374 seconds and 4 git commands to generate.