License header fixes
[lttng-tools.git] / src / bin / lttng / commands / list.c
index f09edaef474ce76660fd67aad1867d42bc7b4550..50d6114c3d166be10483b71974b6418cf6152494 100644 (file)
@@ -1,19 +1,18 @@
 /*
  * 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; only version 2
- * of the License.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
  *
  * This program 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.
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _GNU_SOURCE
@@ -43,6 +42,7 @@ const char *indent8 = "        ";
 enum {
        OPT_HELP = 1,
        OPT_USERSPACE,
+       OPT_LIST_OPTIONS,
 };
 
 static struct lttng_handle *handle;
@@ -60,6 +60,7 @@ static struct poptOption long_options[] = {
 #endif
        {"channel",   'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
        {"domain",    'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
+       {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
        {0, 0, 0, 0, 0, 0, 0}
 };
 
@@ -68,21 +69,22 @@ static struct poptOption long_options[] = {
  */
 static void usage(FILE *ofp)
 {
-       fprintf(ofp, "usage: lttng list [[-k] [-u] [-p PID] [SESSION [<options>]]]\n");
+       fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
        fprintf(ofp, "\n");
        fprintf(ofp, "With no arguments, list available tracing session(s)\n");
        fprintf(ofp, "\n");
-       fprintf(ofp, "With -k alone, list available kernel events\n");
-       fprintf(ofp, "With -u alone, list available userspace events\n");
+       fprintf(ofp, "Without a session, -k lists available kernel events\n");
+       fprintf(ofp, "Without a session, -u lists available userspace events\n");
        fprintf(ofp, "\n");
        fprintf(ofp, "  -h, --help              Show this help\n");
+       fprintf(ofp, "      --list-options      Simple listing of options\n");
        fprintf(ofp, "  -k, --kernel            Select kernel domain\n");
        fprintf(ofp, "  -u, --userspace         Select user-space domain.\n");
 #if 0
        fprintf(ofp, "  -p, --pid PID           List user-space events by PID\n");
 #endif
        fprintf(ofp, "\n");
-       fprintf(ofp, "Options:\n");
+       fprintf(ofp, "Session Options:\n");
        fprintf(ofp, "  -c, --channel NAME      List details of a channel\n");
        fprintf(ofp, "  -d, --domain            List available domain(s)\n");
        fprintf(ofp, "\n");
@@ -141,23 +143,43 @@ const char *enabled_string(int value)
        }
 }
 
-static
-const char *loglevel_string_pre(const char *loglevel)
-{
-       if (loglevel[0] == '\0') {
-               return "";
-       } else {
-               return " (loglevel: ";
-       }
-}
-
-static
-const char *loglevel_string_post(const char *loglevel)
+static const char *loglevel_string(int value)
 {
-       if (loglevel[0] == '\0') {
+       switch (value) {
+       case -1:
                return "";
-       } else {
-               return ")";
+       case LTTNG_LOGLEVEL_EMERG:
+               return "TRACE_EMERG";
+       case LTTNG_LOGLEVEL_ALERT:
+               return "TRACE_ALERT";
+       case LTTNG_LOGLEVEL_CRIT:
+               return "TRACE_CRIT";
+       case LTTNG_LOGLEVEL_ERR:
+               return "TRACE_ERR";
+       case LTTNG_LOGLEVEL_WARNING:
+               return "TRACE_WARNING";
+       case LTTNG_LOGLEVEL_NOTICE:
+               return "TRACE_NOTICE";
+       case LTTNG_LOGLEVEL_INFO:
+               return "TRACE_INFO";
+       case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
+               return "TRACE_DEBUG_SYSTEM";
+       case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
+               return "TRACE_DEBUG_PROGRAM";
+       case LTTNG_LOGLEVEL_DEBUG_PROCESS:
+               return "TRACE_DEBUG_PROCESS";
+       case LTTNG_LOGLEVEL_DEBUG_MODULE:
+               return "TRACE_DEBUG_MODULE";
+       case LTTNG_LOGLEVEL_DEBUG_UNIT:
+               return "TRACE_DEBUG_UNIT";
+       case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
+               return "TRACE_DEBUG_FUNCTION";
+       case LTTNG_LOGLEVEL_DEBUG_LINE:
+               return "TRACE_DEBUG_LINE";
+       case LTTNG_LOGLEVEL_DEBUG:
+               return "TRACE_DEBUG";
+       default:
+               return "<<UNKNOWN>>";
        }
 }
 
@@ -169,23 +191,19 @@ static void print_events(struct lttng_event *event)
        switch (event->type) {
        case LTTNG_EVENT_TRACEPOINT:
        {
-               char ll_value[LTTNG_SYMBOL_NAME_LEN] = "";
-
-               if (event->loglevel[0] != '\0') {
-                       int ret;
-
-                       ret = snprintf(ll_value, LTTNG_SYMBOL_NAME_LEN,
-                               " (%lld)", (long long) event->loglevel_value);
-                       if (ret < 0)
-                               ERR("snprintf error");
-               }
-               MSG("%s%s%s%s%s%s (type: tracepoint)%s", indent6,
+               if (event->loglevel != -1) {
+                       MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s",
+                               indent6,
                                event->name,
-                               loglevel_string_pre(event->loglevel),
+                               loglevel_string(event->loglevel),
                                event->loglevel,
-                               ll_value,
-                               loglevel_string_post(event->loglevel),
                                enabled_string(event->enabled));
+               } else {
+                       MSG("%s%s (type: tracepoint)%s",
+                               indent6,
+                               event->name,
+                               enabled_string(event->enabled));
+               }
                break;
        }
        case LTTNG_EVENT_PROBE:
@@ -205,18 +223,13 @@ static void print_events(struct lttng_event *event)
                MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
                break;
        case LTTNG_EVENT_SYSCALL:
-               MSG("%s (type: syscall)%s", indent6,
+               MSG("%ssyscalls (type: syscall)%s", indent6,
                                enabled_string(event->enabled));
                break;
        case LTTNG_EVENT_NOOP:
                MSG("%s (type: noop)%s", indent6,
                                enabled_string(event->enabled));
                break;
-       case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
-               MSG("%s%s (type: tracepoint loglevel)%s", indent6,
-                       event->name,
-                       enabled_string(event->enabled));
-               break;
        case LTTNG_EVENT_ALL:
                /* We should never have "all" events in list. */
                assert(0);
@@ -235,6 +248,8 @@ static int list_ust_events(void)
        struct lttng_event *event_list;
        pid_t cur_pid = 0;
 
+       memset(&domain, 0, sizeof(domain));
+
        DBG("Getting UST tracing events");
 
        domain.type = LTTNG_DOMAIN_UST;
@@ -247,6 +262,7 @@ static int list_ust_events(void)
        size = lttng_list_tracepoints(handle, &event_list);
        if (size < 0) {
                ERR("Unable to list UST events");
+               lttng_destroy_handle(handle);
                return size;
        }
 
@@ -267,10 +283,12 @@ static int list_ust_events(void)
        MSG("");
 
        free(event_list);
+       lttng_destroy_handle(handle);
 
        return CMD_SUCCESS;
 
 error:
+       lttng_destroy_handle(handle);
        return -1;
 }
 
@@ -284,6 +302,8 @@ static int list_kernel_events(void)
        struct lttng_handle *handle;
        struct lttng_event *event_list;
 
+       memset(&domain, 0, sizeof(domain));
+
        DBG("Getting kernel tracing events");
 
        domain.type = LTTNG_DOMAIN_KERNEL;
@@ -296,6 +316,7 @@ static int list_kernel_events(void)
        size = lttng_list_tracepoints(handle, &event_list);
        if (size < 0) {
                ERR("Unable to list kernel events");
+               lttng_destroy_handle(handle);
                return size;
        }
 
@@ -309,9 +330,11 @@ static int list_kernel_events(void)
 
        free(event_list);
 
+       lttng_destroy_handle(handle);
        return CMD_SUCCESS;
 
 error:
+       lttng_destroy_handle(handle);
        return -1;
 }
 
@@ -390,10 +413,10 @@ static int list_channels(const char *channel_name)
        count = lttng_list_channels(handle, &channels);
        if (count < 0) {
                ret = count;
-               goto error;
+               goto error_channels;
        } else if (count == 0) {
-               MSG("No channel found");
-               goto end;
+               ERR("Channel %s not found", channel_name);
+               goto error;
        }
 
        if (channel_name == NULL) {
@@ -422,14 +445,16 @@ static int list_channels(const char *channel_name)
        }
 
        if (!chan_found && channel_name != NULL) {
-               MSG("Channel %s not found", channel_name);
+               ERR("Channel %s not found", channel_name);
+               goto error;
        }
 
-end:
-       free(channels);
        ret = CMD_SUCCESS;
 
 error:
+       free(channels);
+
+error_channels:
        return ret;
 }
 
@@ -449,6 +474,9 @@ static int list_sessions(const char *session_name)
        if (count < 0) {
                ret = count;
                goto error;
+       } else if (count == 0) {
+               MSG("Currently no available tracing session");
+               goto end;
        }
 
        if (session_name == NULL) {
@@ -466,7 +494,8 @@ static int list_sessions(const char *session_name)
                        continue;
                }
 
-               MSG("  %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path, active_string(sessions[i].enabled));
+               MSG("  %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
+                               active_string(sessions[i].enabled));
 
                if (session_found) {
                        break;
@@ -476,13 +505,16 @@ static int list_sessions(const char *session_name)
        free(sessions);
 
        if (!session_found && session_name != NULL) {
-               MSG("Session %s not found", session_name);
+               ERR("Session '%s' not found", session_name);
+               ret = CMD_ERROR;
+               goto error;
        }
 
        if (session_name == NULL) {
                MSG("\nUse lttng list <session_name> for more details");
        }
 
+end:
        return CMD_SUCCESS;
 
 error:
@@ -540,8 +572,11 @@ int cmd_list(int argc, const char **argv)
        struct lttng_domain domain;
        struct lttng_domain *domains = NULL;
 
+       memset(&domain, 0, sizeof(domain));
+
        if (argc < 1) {
                usage(stderr);
+               ret = CMD_ERROR;
                goto end;
        }
 
@@ -551,11 +586,14 @@ int cmd_list(int argc, const char **argv)
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
-                       usage(stderr);
+                       usage(stdout);
                        goto end;
                case OPT_USERSPACE:
                        opt_userspace = 1;
                        break;
+               case OPT_LIST_OPTIONS:
+                       list_cmd_options(stdout, long_options);
+                       goto end;
                default:
                        usage(stderr);
                        ret = CMD_UNDEFINED;
@@ -574,15 +612,18 @@ int cmd_list(int argc, const char **argv)
                domain.type = LTTNG_DOMAIN_UST;
        }
 
-       handle = lttng_create_handle(session_name, &domain);
-       if (handle == NULL) {
-               goto end;
+       if (opt_kernel || opt_userspace) {
+               handle = lttng_create_handle(session_name, &domain);
+               if (handle == NULL) {
+                       ret = CMD_FATAL;
+                       goto end;
+               }
        }
 
        if (session_name == NULL) {
                if (!opt_kernel && !opt_userspace) {
                        ret = list_sessions(NULL);
-                       if (ret < 0) {
+                       if (ret != 0) {
                                goto end;
                        }
                }
@@ -601,7 +642,7 @@ int cmd_list(int argc, const char **argv)
        } else {
                /* List session attributes */
                ret = list_sessions(session_name);
-               if (ret < 0) {
+               if (ret != 0) {
                        goto end;
                }
 
@@ -639,10 +680,13 @@ int cmd_list(int argc, const char **argv)
                                }
 
                                /* Clean handle before creating a new one */
-                               lttng_destroy_handle(handle);
+                               if (handle) {
+                                       lttng_destroy_handle(handle);
+                               }
 
                                handle = lttng_create_handle(session_name, &domains[i]);
                                if (handle == NULL) {
+                                       ret = CMD_FATAL;
                                        goto end;
                                }
 
@@ -658,7 +702,10 @@ end:
        if (domains) {
                free(domains);
        }
-       lttng_destroy_handle(handle);
+       if (handle) {
+               lttng_destroy_handle(handle);
+       }
 
+       poptFreeContext(pc);
        return ret;
 }
This page took 0.028935 seconds and 5 git commands to generate.