From 5716705821202372fd16168f66f347ba293ef6b4 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Thu, 21 Apr 2011 14:50:18 -0400 Subject: [PATCH] Add listing session option The lttng client can now list available sessions using the liblttngctl API and session daemon support. Signed-off-by: David Goulet --- include/lttng/liblttngctl.h | 13 +++++-- liblttngctl/liblttngctl.c | 45 +++++++++++++++++++++++++ liblttsessiondcomm/liblttsessiondcomm.c | 1 + liblttsessiondcomm/liblttsessiondcomm.h | 6 ++++ ltt-sessiond/ltt-sessiond.c | 24 +++++++++++++ lttng/lttng.c | 39 +++++++++++++++++++++ lttng/lttng.h | 1 + lttng/options.c | 5 +++ 8 files changed, 132 insertions(+), 2 deletions(-) diff --git a/include/lttng/liblttngctl.h b/include/lttng/liblttngctl.h index eadc74a60..ea6d001e1 100644 --- a/include/lttng/liblttngctl.h +++ b/include/lttng/liblttngctl.h @@ -19,6 +19,8 @@ #ifndef _LIBLTTNGCTL_H #define _LIBLTTNGCTL_H +#include + /* Default unix group name for tracing. */ #define DEFAULT_TRACING_GROUP "tracing" @@ -28,16 +30,23 @@ */ #define LTTNG_SESSIOND_PATH_ENV "LTTNG_SESSIOND_PATH" -/* - * From libuuid +/* From libuuid */ #define UUID_STR_LEN 37 +/* Simple structure representing a session. + */ +struct lttng_session { + char name[NAME_MAX]; + char uuid[UUID_STR_LEN]; +}; + extern int lttng_create_session(const char *name, char *session_id); extern int lttng_connect_sessiond(void); extern int lttng_set_tracing_group(const char *name); extern int lttng_check_session_daemon(void); extern const char *lttng_get_readable_code(int code); extern size_t lttng_ust_list_apps(pid_t **pids); +extern size_t lttng_list_sessions(struct lttng_session **sessions); #endif /* _LIBLTTNGCTL_H */ diff --git a/liblttngctl/liblttngctl.c b/liblttngctl/liblttngctl.c index d8481496d..658f646f0 100644 --- a/liblttngctl/liblttngctl.c +++ b/liblttngctl/liblttngctl.c @@ -201,6 +201,51 @@ error: return ret; } +/* + * lttng_list_sessions + * + * Ask the session daemon for all available sessions. + * + * Return number of sessions + */ +size_t lttng_list_sessions(struct lttng_session **sessions) +{ + int ret, first = 0; + size_t size = 0; + struct lttng_session *ls = NULL; + + lsm.cmd_type = LTTNG_LIST_SESSIONS; + + ret = ask_sessiond(); + if (ret < 0) { + goto error; + } + + do { + ret = recvfrom_sessiond(); + if (ret < 0) { + goto error; + } + + if (first == 0) { + first = 1; + size = llm.num_pckt; + ls = malloc(sizeof(struct lttng_session) * size); + } + strncpy(ls[size - llm.num_pckt].name, llm.u.list_sessions.name, + sizeof(ls[size - llm.num_pckt].name)); + strncpy(ls[size - llm.num_pckt].uuid, llm.u.list_sessions.uuid, + sizeof(ls[size - llm.num_pckt].uuid)); + } while ((llm.num_pckt - 1) != 0); + + *sessions = ls; + + return size; + +error: + return ret; +} + /* * lttng_connect_sessiond * diff --git a/liblttsessiondcomm/liblttsessiondcomm.c b/liblttsessiondcomm/liblttsessiondcomm.c index d2d22bc5d..adae84104 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.c +++ b/liblttsessiondcomm/liblttsessiondcomm.c @@ -39,6 +39,7 @@ static const char *lttcomm_readable_code[] = { [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found", [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps", [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found", + [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESS) ] = "No session found", }; /* diff --git a/liblttsessiondcomm/liblttsessiondcomm.h b/liblttsessiondcomm/liblttsessiondcomm.h index 84385ce71..f4180cf0a 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.h +++ b/liblttsessiondcomm/liblttsessiondcomm.h @@ -74,6 +74,7 @@ enum lttcomm_return_code { LTTCOMM_START_FAIL, /* Start tracing fail */ LTTCOMM_LIST_FAIL, /* Listing apps fail */ LTTCOMM_NO_APPS, /* No traceable application */ + LTTCOMM_NO_SESS, /* No sessions available */ LTTCOMM_NR, /* Last element */ }; @@ -132,6 +133,11 @@ struct lttcomm_lttng_msg { struct { pid_t pid; } list_apps; + /* LTTNG_LIST_SESSIONS */ + struct { + char name[NAME_MAX]; + char uuid[37]; /* See libuuid not exported size UUID_STR_LEN */ + } list_sessions; } u; }; diff --git a/ltt-sessiond/ltt-sessiond.c b/ltt-sessiond/ltt-sessiond.c index 050c67397..f257ddb09 100644 --- a/ltt-sessiond/ltt-sessiond.c +++ b/ltt-sessiond/ltt-sessiond.c @@ -56,6 +56,7 @@ static int connect_app(pid_t); static int init_daemon_socket(void); static int process_client_msg(int sock, struct lttcomm_session_msg*); static int send_unix_sock(int sock, void *buf, size_t len); +static size_t ust_list_apps(pid_t **pids); static void *thread_manage_clients(void *); static void *thread_manage_apps(void *); @@ -487,6 +488,29 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) break; } + case LTTNG_LIST_SESSIONS: + { + struct ltt_session *iter = NULL; + + llm.num_pckt = session_count; + if (llm.num_pckt == 0) { + ret = LTTCOMM_NO_SESS; + goto error; + } + + cds_list_for_each_entry(iter, <t_session_list.head, list) { + uuid_unparse(iter->uuid, llm.u.list_sessions.uuid); + strncpy(llm.u.list_sessions.name, iter->name, + sizeof(llm.u.list_sessions.name)); + ret = send_unix_sock(sock, (void*) &llm, sizeof(llm)); + if (ret < 0) { + goto send_error; + } + llm.num_pckt--; + } + + break; + } default: { /* Undefined command */ diff --git a/lttng/lttng.c b/lttng/lttng.c index 907984370..095ebe5e1 100644 --- a/lttng/lttng.c +++ b/lttng/lttng.c @@ -41,6 +41,7 @@ static char *progname; /* Prototypes */ static int process_client_opt(void); static int process_opt_list_apps(void); +static int process_opt_list_sessions(void); static void sighandler(int sig); static int set_signal_handler(void); @@ -68,6 +69,13 @@ static int process_client_opt(void) } } + if (opt_list_session) { + ret = process_opt_list_sessions(); + if (ret < 0) { + goto end; + } + } + return 0; end: @@ -75,6 +83,37 @@ end: return ret; } +/* + * process_opt_list_sessions + * + * Get the list of available sessions from + * the session daemon and print it to user. + */ +static int process_opt_list_sessions(void) +{ + int ret, count, i; + struct lttng_session *sess; + + count = lttng_list_sessions(&sess); + if (count < 0) { + ret = count; + goto error; + } + + MSG("Available sessions [Name (uuid)]:"); + for (i = 0; i < count; i++) { + MSG("\tName: %s (uuid: %s)", sess[i].name, sess[i].uuid); + } + + free(sess); + MSG("\nTo select a session, use --session UUID."); + + return 0; + +error: + return ret; +} + /* * process_opt_list_apps * diff --git a/lttng/lttng.h b/lttng/lttng.h index 4a4d181d5..be32bf7ef 100644 --- a/lttng/lttng.h +++ b/lttng/lttng.h @@ -31,6 +31,7 @@ extern char *opt_session_name; extern char *opt_sessiond_path; extern int opt_list_apps; extern int opt_no_sessiond; +extern int opt_list_session; #define SESSIOND_PATH_NUM 6 diff --git a/lttng/options.c b/lttng/options.c index ee13f120b..78e1764f9 100644 --- a/lttng/options.c +++ b/lttng/options.c @@ -30,6 +30,7 @@ int opt_quiet = 0; int opt_verbose = 0; int opt_list_apps = 0; int opt_no_sessiond = 0; +int opt_list_session = 0; enum { OPT_HELP = 42, @@ -47,6 +48,7 @@ static struct poptOption long_options[] = { {"list-apps", 'l', POPT_ARG_VAL, &opt_list_apps, 1, 0}, {"no-sessiond", 0, POPT_ARG_VAL, &opt_no_sessiond, 1, 0}, {"sessiond-path", 0, POPT_ARG_STRING, &opt_sessiond_path, 0, 0}, + {"list-session", 0, POPT_ARG_VAL, &opt_list_session, 1, 0}, {0, 0, 0, 0, 0, 0} }; @@ -67,6 +69,9 @@ static void usage(FILE *ofp) fprintf(ofp, " --no-sessiond Don't spawn a session daemon.\n"); fprintf(ofp, " --sessiond-path Session daemon full path\n"); fprintf(ofp, "\n"); + fprintf(ofp, "Session options:\n"); + fprintf(ofp, " --list-session List all available sessions\n"); + fprintf(ofp, "\n"); fprintf(ofp, "Tracing options:\n"); //fprintf(ofp, " --session [NAME] Specify tracing session. If no NAME is given\n"); //fprintf(ofp, " or option is ommited, a session will be created\n"); -- 2.34.1