X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=liblttngctl%2Fliblttngctl.c;h=e05496a9449891f5f6174af5ed4de3c9ce1f5f0e;hp=bd02a13b2c451fad17d6b0612472bba7b5652023;hb=94cf3c47fe93b891dac6206bbcf712d042d15727;hpb=520ff6876d5fdf7e5421733a8f2a7d0c20e43c8c diff --git a/liblttngctl/liblttngctl.c b/liblttngctl/liblttngctl.c index bd02a13b2..e05496a94 100644 --- a/liblttngctl/liblttngctl.c +++ b/liblttngctl/liblttngctl.c @@ -24,10 +24,11 @@ #include #include -#include +#include #include "liblttsessiondcomm.h" #include "lttngerr.h" +#include "lttng-share.h" /* Socket to session daemon for communication */ static int sessiond_socket; @@ -37,13 +38,6 @@ static char sessiond_sock_path[PATH_MAX]; static struct lttcomm_session_msg lsm; static struct lttcomm_lttng_msg llm; -/* Prototypes */ -static int check_tracing_group(const char *grp_name); -static int ask_sessiond(enum lttcomm_command_type lct, void **buf); -static int recv_data_sessiond(void *buf, size_t len); -static int send_data_sessiond(void); -static int set_session_daemon_path(void); - /* Variables */ static char *tracing_group; static int connected; @@ -89,9 +83,6 @@ static int recv_data_sessiond(void *buf, size_t len) } ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); - if (ret < 0) { - goto end; - } end: return ret; @@ -100,17 +91,21 @@ end: /* * ask_sessiond * - * Ask the session daemon a specific command - * and put the data into buf. + * 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_command_type lct, void **buf) +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 */ @@ -131,7 +126,7 @@ static int ask_sessiond(enum lttcomm_command_type lct, void **buf) goto end; } - size = llm.size_payload; + size = llm.data_size; if (size == 0) { goto end; } @@ -141,6 +136,7 @@ static int ask_sessiond(enum lttcomm_command_type lct, void **buf) /* Get payload data */ ret = recv_data_sessiond(data, size); if (ret < 0) { + free(data); goto end; } @@ -148,84 +144,256 @@ static int ask_sessiond(enum lttcomm_command_type lct, void **buf) ret = size; end: - /* Reset lsm data struct */ - memset(&lsm, 0, sizeof(lsm)); + lttng_disconnect_sessiond(); return ret; } /* - * lttng_get_readable_code + * check_tracing_group * - * Return a human readable string of code + * Check if the specified group name exist. + * If yes, 0, else -1 */ -const char *lttng_get_readable_code(int code) +static int check_tracing_group(const char *grp_name) { - if (code > -LTTCOMM_OK) { - return "Ended with errors"; + struct group *grp_tracing; /* no free(). See getgrnam(3) */ + gid_t *grp_list; + int grp_list_size, grp_id, i; + int ret = -1; + + /* 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"); + } + goto end; } - return lttcomm_get_readable_code(code); + /* Get number of supplementary group IDs */ + grp_list_size = getgroups(0, NULL); + if (grp_list_size < 0) { + perror("getgroups"); + goto end; + } + + /* Alloc group list of the right size */ + grp_list = malloc(grp_list_size * sizeof(gid_t)); + grp_id = getgroups(grp_list_size, grp_list); + if (grp_id < -1) { + perror("getgroups"); + goto free_list; + } + + for (i = 0; i < grp_list_size; i++) { + if (grp_list[i] == grp_tracing->gr_gid) { + ret = 0; + break; + } + } + +free_list: + free(grp_list); + +end: + return ret; } /* - * lttng_ust_start_trace + * set_session_daemon_path * - * Request a trace start for pid. + * Set sessiond socket path by putting it in + * the global sessiond_sock_path variable. */ -int lttng_ust_start_trace(pid_t pid) +static int set_session_daemon_path(void) { int ret; - lsm.pid = pid; - ret = ask_sessiond(UST_START_TRACE, NULL); + /* 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) { + return -ENOMEM; + } + } else { + strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, + sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); + } - return ret; + return 0; +} + +/* + * lttng_start_tracing + * + * Start tracing for all trace of the session. + */ +int lttng_start_tracing(char *session_name) +{ + strncpy(lsm.session_name, session_name, NAME_MAX); + return ask_sessiond(LTTNG_START_TRACE, NULL); } /* - * lttng_ust_stop_trace + * lttng_stop_tracing * - * Request a trace stop for pid. + * Stop tracing for all trace of the session. */ -int lttng_ust_stop_trace(pid_t pid) +int lttng_stop_tracing(char *session_name) +{ + strncpy(lsm.session_name, session_name, NAME_MAX); + return ask_sessiond(LTTNG_STOP_TRACE, NULL); +} + +/* + * BEGIN Kernel control API + */ + +/* + * lttng_kernel_add_context + */ +int lttng_kernel_add_context(struct lttng_kernel_context *ctx, + char *event_name, char *channel_name) +{ + if (channel_name != NULL) { + strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX); + } + + if (event_name != NULL) { + strncpy(lsm.u.context.event_name, event_name, NAME_MAX); + } + + memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_kernel_context)); + return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL); +} + +/* + * lttng_kernel_enable_event + */ +int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name) { int ret; - lsm.pid = pid; - ret = ask_sessiond(UST_STOP_TRACE, NULL); + if (channel_name == NULL) { + strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX); + } else { + strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX); + } + + 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); + } return ret; } /* - * lttng_ust_create_trace + * lttng_kernel_disable_event * - * Request a trace creation for pid. + * Disable an event in the kernel tracer. */ -int lttng_ust_create_trace(pid_t pid) +int lttng_kernel_disable_event(char *name, char *channel_name) { int ret; - lsm.pid = pid; - ret = ask_sessiond(UST_CREATE_TRACE, NULL); + if (channel_name == NULL) { + 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); + } return ret; } +/* + * lttng_kernel_enable_channel + * + * Enable recording for a channel for the kernel tracer. + */ +int lttng_kernel_enable_channel(char *name) +{ + strncpy(lsm.u.enable.channel_name, name, NAME_MAX); + return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL); +} + +/* + * lttng_kernel_disable_channel + * + * Disable recording for the channel for the kernel tracer. + */ +int lttng_kernel_disable_channel(char *name) +{ + strncpy(lsm.u.disable.channel_name, name, NAME_MAX); + return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL); +} + +/* + * lttng_kernel_create_channel + * + * Create a channel in the kernel tracer. + */ +int lttng_kernel_create_channel(struct lttng_channel *chan) +{ + memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel)); + return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, 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. + */ +int lttng_kernel_list_events(char **event_list) +{ + return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list); +} + +/* + * END Kernel control API + */ + +/* + * lttng_get_readable_code + * + * 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_ust_list_apps * - * Ask the session daemon for all UST traceable - * applications. + * Ask the session daemon for all UST traceable applications. * * Return the number of pids. * On error, return negative value. */ -int lttng_ust_list_apps(pid_t **pids) +int lttng_ust_list_traceable_apps(pid_t **pids) { int ret; - ret = ask_sessiond(UST_LIST_APPS, (void**) pids); + ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids); if (ret < 0) { return ret; } @@ -236,16 +404,18 @@ int lttng_ust_list_apps(pid_t **pids) /* * lttng_list_traces * - * Ask the session daemon for all traces (kernel and ust) - * for the session identified by uuid. + * Ask the session daemon for all traces (kernel and ust) for the session + * identified by name. * * Return the number of traces. + * On error, return negative value. */ -int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces) +/* +int lttng_list_traces(char *session_name, struct lttng_trace **traces) { int ret; - uuid_copy(lsm.session_id, *uuid); + strncpy(lsm.session_name, session_name, NAME_MAX); ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces); if (ret < 0) { @@ -254,29 +424,18 @@ int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces) return ret / sizeof(struct lttng_trace); } +*/ /* * lttng_create_session * - * Create a brand new session using name. Allocate - * the session_id param pointing to the UUID. + * Create a brand new session using name. */ -int lttng_create_session(char *name, uuid_t *session_id) +int lttng_create_session(char *name, char *path) { - int ret; - - strncpy(lsm.session_name, name, sizeof(lsm.session_name)); - lsm.session_name[sizeof(lsm.session_name) - 1] = '\0'; - - ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL); - if (ret < 0) { - goto end; - } - - uuid_copy(*session_id, llm.session_id); - -end: - return ret; + strncpy(lsm.session_name, name, NAME_MAX); + strncpy(lsm.path, path, PATH_MAX); + return ask_sessiond(LTTNG_CREATE_SESSION, NULL); } /* @@ -284,19 +443,10 @@ end: * * Destroy session using name. */ -int lttng_destroy_session(uuid_t *uuid) +int lttng_destroy_session(char *name) { - int ret; - - uuid_copy(lsm.session_id, *uuid); - - ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL); - if (ret < 0) { - goto end; - } - -end: - return ret; + strncpy(lsm.session_name, name, NAME_MAX); + return ask_sessiond(LTTNG_DESTROY_SESSION, NULL); } /* @@ -365,14 +515,9 @@ int lttng_disconnect_sessiond(void) return ret; } -/* - * lttng_set_current_session_uuid - * - * Set the session uuid for current lsm. - */ -void lttng_set_current_session_uuid(char *uuid) +void lttng_set_session_name(char *name) { - uuid_parse(uuid, lsm.session_id); + strncpy(lsm.session_name, name, NAME_MAX); } /* @@ -393,102 +538,29 @@ int lttng_set_tracing_group(const char *name) /* * lttng_check_session_daemon * - * Return 0 if a sesssion daemon is available - * else return -1 + * Yes, return 1 + * No, return 0 + * Error, return negative value */ -int lttng_check_session_daemon(void) +int lttng_session_daemon_alive(void) { int ret; ret = set_session_daemon_path(); if (ret < 0) { + /* Error */ return ret; } /* If socket exist, we consider the daemon started */ ret = access(sessiond_sock_path, F_OK); if (ret < 0) { - return ret; - } - - return 0; -} - -/* - * set_session_daemon_path - * - * Set sessiond socket path by putting it in - * the global sessiond_sock_path variable. - */ -static int set_session_daemon_path(void) -{ - int ret; - - /* Are we in the tracing group ? */ - ret = check_tracing_group(tracing_group); - if (ret < 0) { - if (sprintf(sessiond_sock_path, 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)); + /* Not alive */ + return 0; } - return 0; -} - -/* - * check_tracing_group - * - * Check if the specified group name exist. - * If yes, 0, else -1 - */ -static int check_tracing_group(const char *grp_name) -{ - struct group *grp_tracing; /* no free(). See getgrnam(3) */ - gid_t *grp_list; - int grp_list_size, grp_id, i; - int ret = -1; - - /* 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"); - } - goto end; - } - - /* Get number of supplementary group IDs */ - grp_list_size = getgroups(0, NULL); - if (grp_list_size < 0) { - perror("getgroups"); - goto end; - } - - /* Alloc group list of the right size */ - grp_list = malloc(grp_list_size * sizeof(gid_t)); - grp_id = getgroups(grp_list_size, grp_list); - if (grp_id < -1) { - perror("getgroups"); - goto free_list; - } - - for (i = 0; i < grp_list_size; i++) { - if (grp_list[i] == grp_tracing->gr_gid) { - ret = 0; - break; - } - } - -free_list: - free(grp_list); - -end: - return ret; + /* Is alive */ + return 1; } /* @@ -497,5 +569,5 @@ end: static void __attribute__((constructor)) init() { /* Set default session group */ - lttng_set_tracing_group(DEFAULT_TRACING_GROUP); + lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP); }