.gitignore: ignore local vscode workspace settings file
[lttng-tools.git] / src / bin / lttng / commands / view.cpp
index a2623fe49e0ccb66b4baecbf1f27552d22ffad43..9a7557e40dbd07144c557bdd463e4ba537f637be 100644 (file)
@@ -6,6 +6,10 @@
  */
 
 #define _LGPL_SOURCE
+#include "../command.hpp"
+
+#include <common/spawn-viewer.hpp>
+
 #include <popt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <common/spawn-viewer.h>
-#include "../command.h"
-
-static char *opt_session_name;
 static char *opt_viewer;
 static char *opt_trace_path;
 
 #ifdef LTTNG_EMBED_HELP
 static const char help_msg[] =
 #include <lttng-view.1.h>
-;
+       ;
 #endif
 
 enum {
@@ -34,11 +34,11 @@ enum {
 
 static struct poptOption long_options[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
-       {"help",        'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
-       {"list-options", 0,  POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
-       {"viewer",      'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
-       {"trace-path",  't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
-       {0, 0, 0, 0, 0, 0, 0}
+       { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
+       { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
+       { "viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, nullptr, nullptr },
+       { "trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, nullptr, nullptr },
+       { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
 };
 
 /* Is the session we are trying to view is in live mode. */
@@ -50,7 +50,7 @@ static int session_live_mode;
 static char *build_live_path(char *session_name)
 {
        int ret;
-       char *path = NULL;
+       char *path = nullptr;
        char hostname[LTTNG_HOST_NAME_MAX];
 
        ret = gethostname(hostname, sizeof(hostname));
@@ -59,8 +59,7 @@ static char *build_live_path(char *session_name)
                goto error;
        }
 
-       ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
-                       session_name);
+       ret = asprintf(&path, "net://localhost/host/%s/%s", hostname, session_name);
        if (ret < 0) {
                PERROR("asprintf live path");
                goto error;
@@ -73,11 +72,11 @@ error:
 /*
  * Exec viewer if found and use session name path.
  */
-static int view_trace(void)
+static int view_trace(const char *arg_session_name)
 {
        int ret;
-       char *session_name, *trace_path = NULL;
-       struct lttng_session *sessions = NULL;
+       char *session_name, *trace_path = nullptr;
+       struct lttng_session *sessions = nullptr;
        bool free_trace_path = false;
 
        /*
@@ -98,15 +97,21 @@ static int view_trace(void)
 
        /* User define trace path override the session name */
        if (opt_trace_path) {
-               session_name = NULL;
-       } else if(opt_session_name == NULL) {
-               session_name = get_session_name();
-               if (session_name == NULL) {
+               session_name = nullptr;
+       } else {
+               if (arg_session_name == nullptr) {
+                       session_name = get_session_name();
+               } else {
+                       session_name = strdup(arg_session_name);
+                       if (session_name == nullptr) {
+                               PERROR("Failed to copy session name");
+                       }
+               }
+
+               if (session_name == nullptr) {
                        ret = CMD_ERROR;
                        goto error;
                }
-       } else {
-               session_name = opt_session_name;
        }
 
        DBG("Viewing trace for session %s", session_name);
@@ -117,8 +122,7 @@ static int view_trace(void)
                /* Getting all sessions */
                count = lttng_list_sessions(&sessions);
                if (count < 0) {
-                       ERR("Unable to list sessions. Session name %s not found.",
-                                       session_name);
+                       ERR("Unable to list sessions. Session name %s not found.", session_name);
                        MSG("Is there a session daemon running?");
                        ret = CMD_ERROR;
                        goto free_error;
@@ -144,7 +148,7 @@ static int view_trace(void)
 
                if (sessions[i].enabled && !session_live_mode) {
                        WARN("Session %s is running. Please stop it before reading it.",
-                                       session_name);
+                            session_name);
                        ret = CMD_ERROR;
                        goto free_sessions;
                }
@@ -179,9 +183,7 @@ free_sessions:
        }
        free(sessions);
 free_error:
-       if (opt_session_name == NULL) {
-               free(session_name);
-       }
+       free(session_name);
 error:
        return ret;
 }
@@ -193,9 +195,10 @@ int cmd_view(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS;
        static poptContext pc;
-       const char *leftover = NULL;
+       const char *arg_session_name = nullptr;
+       const char *leftover = nullptr;
 
-       pc = poptGetContext(NULL, argc, argv, long_options, 0);
+       pc = poptGetContext(nullptr, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
 
        if (lttng_opt_mi) {
@@ -216,7 +219,7 @@ int cmd_view(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char*) poptGetArg(pc);
+       arg_session_name = poptGetArg(pc);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -225,7 +228,7 @@ int cmd_view(int argc, const char **argv)
                goto end;
        }
 
-       ret = view_trace();
+       ret = view_trace(arg_session_name);
 
 end:
        poptFreeContext(pc);
This page took 0.031758 seconds and 4 git commands to generate.