Rename liblttsessiondcomm to liblttng-sessiond-comm, install it.
[lttng-tools.git] / liblttngctl / liblttngctl.c
index 31693942dd6fbd31bfa7ef2fc445912bf63f976f..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
  *
@@ -89,70 +105,9 @@ end:
 }
 
 /*
- *  ask_sessiond
- *
- *  Ask the session daemon a specific command and put the data into buf.
- *
- *  Return size of data (only payload, not header).
- */
-static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
-{
-       int ret;
-       size_t size;
-       void *data = NULL;
-
-       ret = lttng_connect_sessiond();
-       if (ret < 0) {
-               goto end;
-       }
-
-       lsm.cmd_type = lct;
-
-       /* Send command to session daemon */
-       ret = send_data_sessiond();
-       if (ret < 0) {
-               goto end;
-       }
-
-       /* Get header from data transmission */
-       ret = recv_data_sessiond(&llm, sizeof(llm));
-       if (ret < 0) {
-               goto end;
-       }
-
-       /* Check error code if OK */
-       if (llm.ret_code != LTTCOMM_OK) {
-               ret = -llm.ret_code;
-               goto end;
-       }
-
-       size = llm.data_size;
-       if (size == 0) {
-               goto end;
-       }
-
-       data = (void*) malloc(size);
-
-       /* Get payload data */
-       ret = recv_data_sessiond(data, size);
-       if (ret < 0) {
-               free(data);
-               goto end;
-       }
-
-       *buf = data;
-       ret = size;
-
-end:
-       lttng_disconnect_sessiond();
-       return ret;
-}
-
-/*
- *  check_tracing_group
- *
  *  Check if the specified group name exist.
- *  If yes, 0, else -1
+ *
+ *  If yes return 0, else return -1.
  */
 static int check_tracing_group(const char *grp_name)
 {
@@ -201,10 +156,8 @@ end:
 }
 
 /*
- *  set_session_daemon_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.
  */
 static int set_session_daemon_path(void)
 {
@@ -213,245 +166,320 @@ 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;
 }
 
 /*
- *  lttng_start_tracing
+ *  Connect to the LTTng session daemon.
  *
- *  Start tracing for all trace of the session.
+ *  On success, return 0. On error, return -1.
  */
-int lttng_start_tracing(char *session_name)
+static int connect_sessiond(void)
 {
-       strncpy(lsm.session_name, session_name, NAME_MAX);
-       return ask_sessiond(LTTNG_START_TRACE, NULL);
+       int ret;
+
+       ret = set_session_daemon_path();
+       if (ret < 0) {
+               return ret;
+       }
+
+       /* Connect to the sesssion daemon */
+       ret = lttcomm_connect_unix_sock(sessiond_sock_path);
+       if (ret < 0) {
+               return ret;
+       }
+
+       sessiond_socket = ret;
+       connected = 1;
+
+       return 0;
 }
 
 /*
- *  lttng_stop_tracing
- *
- *  Stop tracing for all trace of the session.
+ *  Clean disconnect the session daemon.
  */
-int lttng_stop_tracing(char *session_name)
+static int disconnect_sessiond(void)
 {
-       strncpy(lsm.session_name, session_name, NAME_MAX);
-       return ask_sessiond(LTTNG_STOP_TRACE, NULL);
+       int ret = 0;
+
+       if (connected) {
+               ret = lttcomm_close_unix_sock(sessiond_socket);
+               sessiond_socket = 0;
+               connected = 0;
+       }
+
+       return ret;
 }
 
 /*
- * BEGIN Kernel control API
+ * Reset the session message structure.
  */
+static void reset_session_msg(void)
+{
+       memset(&lsm, 0, sizeof(struct lttcomm_session_msg));
+}
 
 /*
- *  lttng_kernel_add_context
+ *  ask_sessiond
+ *
+ *  Ask the session daemon a specific command and put the data into buf.
+ *
+ *  Return size of data (only payload, not header).
  */
-int lttng_kernel_add_context(struct lttng_kernel_context *ctx,
-               char *event_name, char *channel_name)
+static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
 {
-       if (strlen(channel_name) != 0) {
-               strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
+       int ret;
+       size_t size;
+       void *data = NULL;
+
+       ret = connect_sessiond();
+       if (ret < 0) {
+               goto end;
        }
 
-       if (strlen(event_name) != 0) {
-               strncpy(lsm.u.context.event_name, event_name, NAME_MAX);
+       lsm.cmd_type = lct;
+
+       /* Send command to session daemon */
+       ret = send_data_sessiond();
+       if (ret < 0) {
+               goto end;
        }
 
-       memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_kernel_context));
-       return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL);
-}
+       /* Get header from data transmission */
+       ret = recv_data_sessiond(&llm, sizeof(llm));
+       if (ret < 0) {
+               goto end;
+       }
 
-/*
- *  lttng_kernel_enable_event
- */
-int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
-{
-       int ret;
+       /* Check error code if OK */
+       if (llm.ret_code != LTTCOMM_OK) {
+               ret = -llm.ret_code;
+               goto end;
+       }
 
-       if (strlen(channel_name) == 0) {
-               strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
-       } else {
-               strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
+       size = llm.data_size;
+       if (size == 0) {
+               ret = 0;
+               goto end;
        }
 
-       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);
+       data = (void*) malloc(size);
+
+       /* Get payload data */
+       ret = recv_data_sessiond(data, size);
+       if (ret < 0) {
+               free(data);
+               goto end;
        }
 
+       *buf = data;
+       ret = size;
+
+end:
+       disconnect_sessiond();
+       reset_session_msg();
        return ret;
 }
 
 /*
- *  lttng_kernel_disable_event
- *
- *  Disable an event in the kernel tracer.
+ * Copy domain to lttcomm_session_msg domain. If unknown domain, default domain
+ * will be the kernel.
  */
-int lttng_kernel_disable_event(char *name, char *channel_name)
+static void copy_lttng_domain(struct lttng_domain *dom)
 {
-       int ret;
-
-       if (strlen(channel_name) == 0) {
-               strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
-       } else {
-               strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
-       }
-
-       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);
+       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;
+               }
        }
-
-       return ret;
 }
 
 /*
- *  lttng_kernel_enable_channel
- *
- *  Enable recording for a channel for the kernel tracer.
+ *  Start tracing for all trace of the session.
  */
-int lttng_kernel_enable_channel(char *name)
+int lttng_start_tracing(const char *session_name)
 {
-       strncpy(lsm.u.enable.channel_name, name, NAME_MAX);
-       return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
+       copy_string(lsm.session.name, session_name, NAME_MAX);
+       return ask_sessiond(LTTNG_START_TRACE, NULL);
 }
 
 /*
- *  lttng_kernel_disable_channel
- *
- *  Disable recording for the channel for the kernel tracer.
+ *  Stop tracing for all trace of the session.
  */
-int lttng_kernel_disable_channel(char *name)
+int lttng_stop_tracing(const char *session_name)
 {
-       strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
-       return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
+       copy_string(lsm.session.name, session_name, NAME_MAX);
+       return ask_sessiond(LTTNG_STOP_TRACE, NULL);
 }
 
 /*
- *  lttng_kernel_create_channel
- *
- *  Create a channel in the kernel tracer.
+ *  lttng_add_context
  */
-int lttng_kernel_create_channel(struct lttng_channel *chan)
+int lttng_add_context(struct lttng_domain *domain,
+               struct lttng_event_context *ctx, const char *event_name,
+               const char *channel_name)
 {
-       memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
-       return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, NULL);
+       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 (ctx) {
+               memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
+       }
+
+       return ask_sessiond(LTTNG_ADD_CONTEXT, NULL);
 }
 
 /*
- *  lttng_list_events
- *
- *  List all available events in the kernel.
- *
- *  Return the size (bytes) of the list and set the event_list array.
- *  On error, return negative value.
+ *  lttng_enable_event
  */
-int lttng_kernel_list_events(char **event_list)
+int lttng_enable_event(struct lttng_domain *domain,
+               struct lttng_event *ev, const char *channel_name)
 {
-       return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
+       int ret;
+
+       if (channel_name == NULL) {
+               copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
+       } else {
+               copy_string(lsm.u.enable.channel_name, channel_name, NAME_MAX);
+       }
+
+       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;
 }
 
 /*
- * END Kernel control API
+ * Disable event of a channel and domain.
  */
+int lttng_disable_event(struct lttng_domain *domain, const char *name,
+               const char *channel_name)
+{
+       int ret = -1;
+
+       if (channel_name == NULL) {
+               copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
+       } else {
+               copy_string(lsm.u.disable.channel_name, channel_name, NAME_MAX);
+       }
+
+       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;
+}
 
 /*
- *  lttng_get_readable_code
- *
- *  Return a human readable string of code
+ * Enable channel per domain
  */
-const char *lttng_get_readable_code(int code)
+int lttng_enable_channel(struct lttng_domain *domain,
+               struct lttng_channel *chan)
 {
-       if (code > -LTTCOMM_OK) {
-               return "Ended with errors";
+       if (chan) {
+               memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
        }
 
-       return lttcomm_get_readable_code(code);
+       copy_lttng_domain(domain);
+
+       return ask_sessiond(LTTNG_ENABLE_CHANNEL, NULL);
 }
 
 /*
- *  lttng_ust_list_apps
- *
- *  Ask the session daemon for all UST traceable applications.
- *
- *  Return the number of pids.
- *  On error, return negative value.
+ * All tracing will be stopped for registered events of the channel.
  */
-int lttng_ust_list_traceable_apps(pid_t **pids)
+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);
 
-       ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids);
-       if (ret < 0) {
-               return ret;
-       }
-
-       return ret / sizeof(pid_t);
+       return ask_sessiond(LTTNG_DISABLE_CHANNEL, NULL);
 }
 
 /*
- *  lttng_list_traces
- *
- *  Ask the session daemon for all traces (kernel and ust) for the session
- *  identified by name.
+ * List all available tracepoints of domain.
  *
- *  Return the number of traces.
- *  On error, return negative value.
+ * Return the size (bytes) of the list and set the events array.
+ * On error, return negative value.
  */
-/*
-int lttng_list_traces(char *session_name, struct lttng_trace **traces)
+int lttng_list_tracepoints(struct lttng_domain *domain,
+               struct lttng_event **events)
 {
        int ret;
 
-       strncpy(lsm.session_name, session_name, NAME_MAX);
+       copy_lttng_domain(domain);
 
-       ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
+       ret = ask_sessiond(LTTNG_LIST_TRACEPOINTS, (void **) events);
        if (ret < 0) {
                return ret;
        }
 
-       return ret / sizeof(struct lttng_trace);
+       return ret / sizeof(struct lttng_event);
+}
+
+/*
+ *  Return a human readable string of code
+ */
+const char *lttng_get_readable_code(int code)
+{
+       if (code > -LTTCOMM_OK) {
+               return "Ended with errors";
+       }
+
+       return lttcomm_get_readable_code(code);
 }
-*/
 
 /*
- *  lttng_create_session
- *
  *  Create a brand new session using name.
  */
-int lttng_create_session(char *name, char *path)
+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);
 }
 
 /*
- *  lttng_destroy_session
- *
  *  Destroy session using name.
  */
-int lttng_destroy_session(char *name)
+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);
 }
 
 /*
- *  lttng_list_sessions
- *
  *  Ask the session daemon for all available sessions.
  *
  *  Return number of session.
@@ -470,54 +498,67 @@ int lttng_list_sessions(struct lttng_session **sessions)
 }
 
 /*
- *  lttng_connect_sessiond
- *
- *  Connect to the LTTng session daemon.
- *  On success, return 0
- *  On error, return a negative value
+ * List domain of a session.
  */
-int lttng_connect_sessiond(void)
+int lttng_list_domains(const char *session_name, struct lttng_domain **domains)
 {
        int ret;
 
-       ret = set_session_daemon_path();
+       copy_string(lsm.session.name, session_name, NAME_MAX);
+       ret = ask_sessiond(LTTNG_LIST_DOMAINS, (void**) domains);
        if (ret < 0) {
                return ret;
        }
 
-       /* Connect to the sesssion daemon */
-       ret = lttcomm_connect_unix_sock(sessiond_sock_path);
+       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;
        }
 
-       sessiond_socket = ret;
-       connected = 1;
-
-       return 0;
+       return ret / sizeof(struct lttng_channel);
 }
 
 /*
- *  lttng_disconnect_sessiond
- *
- *  Clean disconnect the session daemon.
+ * List events of a session channel.
  */
-int lttng_disconnect_sessiond(void)
+int lttng_list_events(struct lttng_domain *domain,
+               const char *session_name, const char *channel_name,
+               struct lttng_event **events)
 {
-       int ret = 0;
+       int ret;
 
-       if (connected) {
-               ret = lttcomm_close_unix_sock(sessiond_socket);
-               sessiond_socket = 0;
-               connected = 0;
+       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;
+       return ret / sizeof(struct lttng_event);
 }
 
-void lttng_set_session_name(char *name)
+/*
+ * 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);
 }
 
 /*
@@ -535,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
  *
@@ -552,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.031503 seconds and 4 git commands to generate.