lttng: Add --all, --glob options to lttng-stop
authorOlivier Dion <odion@efficios.com>
Mon, 6 Feb 2023 21:11:56 +0000 (16:11 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 26 Apr 2023 17:55:11 +0000 (13:55 -0400)
Change-Id: Ida7c8f68f9b52f7ab4414bf060383864bd2e3046
Signed-off-by: Olivier Dion <odion@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
doc/man/lttng-stop.1.txt
src/bin/lttng/commands/stop.cpp

index 0ad1f0e3c32b29c1809fb8fd5061c2400a6f58c5..ca1129df3ac5769297296cd411cd652be90ffe4b 100644 (file)
@@ -11,7 +11,7 @@ lttng-stop - Stop an LTTng recording session
 SYNOPSIS
 --------
 [verse]
-*lttng* ['linkgenoptions:(GENERAL OPTIONS)'] *stop* [option:--no-wait] ['SESSION']
+*lttng* ['linkgenoptions:(GENERAL OPTIONS)'] *stop* [option:--no-wait] [option:--all | option:--glob 'SESSION' | 'SESSION']
 
 
 DESCRIPTION
@@ -62,6 +62,11 @@ option:-n, option:--no-wait::
     Do :not: ensure that the trace data of the selected recording
     session is valid before exiting.
 
+option:-a, option:--all::
+    Stop all sessions.
+
+option:-g, option:--glob::
+    Interpret SESSION as a globbing pattern.
 
 include::common-lttng-cmd-help-options.txt[]
 
@@ -98,6 +103,26 @@ $ lttng stop --no-wait
 ----
 ====
 
+.Stop all sessions.
+====
+See the option:--all option.
+
+[role="term"]
+----
+$ lttng stop --all
+----
+====
+
+.Stop all sessions with the suffix foo.
+====
+See the option:--glob option.
+
+[role="term"]
+----
+$ lttng stop --glob '*foo'
+----
+====
+
 
 include::common-footer.txt[]
 
index a6d3d6711acc9ad083a1ccaa10348cddf0797a1b..ce66b096a7e591ee4248fd60ed54bbdebc3ebe17 100644 (file)
@@ -7,7 +7,9 @@
 
 #define _LGPL_SOURCE
 #include "../command.hpp"
+#include "../utils.hpp"
 
+#include <common/exception.hpp>
 #include <common/mi-lttng.hpp>
 #include <common/sessiond-comm/sessiond-comm.hpp>
 
@@ -31,6 +33,8 @@ static const char help_msg[] =
 enum {
        OPT_HELP = 1,
        OPT_LIST_OPTIONS,
+       OPT_ENABLE_GLOB,
+       OPT_ALL,
 };
 
 static struct poptOption long_options[] = {
@@ -38,13 +42,15 @@ static struct poptOption long_options[] = {
        { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
        { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
        { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr },
+       { "glob", 'g', POPT_ARG_NONE, nullptr, OPT_ENABLE_GLOB, nullptr, nullptr },
+       { "all", 'a', POPT_ARG_NONE, nullptr, OPT_ALL, nullptr, nullptr },
        { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
 };
 
 /*
  * Mi print of partial session
  */
-static int mi_print_session(char *session_name, int enabled)
+static int mi_print_session(const char *session_name, int enabled)
 {
        int ret;
        LTTNG_ASSERT(writer);
@@ -78,24 +84,9 @@ end:
 /*
  * Start tracing for all trace of the session.
  */
-static int stop_tracing(const char *arg_session_name)
+static int stop_tracing(const char *session_name)
 {
        int ret;
-       char *session_name;
-
-       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;
-       }
 
        ret = lttng_stop_tracing_no_wait(session_name);
        if (ret < 0) {
@@ -107,7 +98,7 @@ static int stop_tracing(const char *arg_session_name)
                        ERR("%s", lttng_strerror(ret));
                        break;
                }
-               goto free_name;
+               goto error;
        }
 
        if (!opt_no_wait) {
@@ -117,7 +108,7 @@ static int stop_tracing(const char *arg_session_name)
                        ret = lttng_data_pending(session_name);
                        if (ret < 0) {
                                /* Return the data available call error. */
-                               goto free_name;
+                               goto error;
                        }
 
                        /*
@@ -139,15 +130,27 @@ static int stop_tracing(const char *arg_session_name)
        MSG("Tracing stopped for session %s", session_name);
        if (lttng_opt_mi) {
                ret = mi_print_session(session_name, 0);
-               if (ret) {
-                       goto free_name;
-               }
        }
+error:
+       return ret;
+}
 
-free_name:
-       free(session_name);
+static int stop_tracing(const struct session_spec& spec)
+{
+       int ret = CMD_SUCCESS;
+
+       try {
+               for (const auto& session : list_sessions(spec)) {
+                       int const sub_ret = stop_tracing(session.name);
+
+                       if (sub_ret != CMD_SUCCESS) {
+                               ret = sub_ret;
+                       }
+               }
+       } catch (lttng::ctl::error& error) {
+               ret = error.code();
+       }
 
-error:
        return ret;
 }
 
@@ -160,8 +163,11 @@ int cmd_stop(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
-       const char *arg_session_name = nullptr;
        const char *leftover = nullptr;
+       struct session_spec session_spec = {
+               .type = session_spec::NAME,
+               .value = nullptr,
+       };
 
        pc = poptGetContext(nullptr, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -174,6 +180,12 @@ int cmd_stop(int argc, const char **argv)
                case OPT_LIST_OPTIONS:
                        list_cmd_options(stdout, long_options);
                        goto end;
+               case OPT_ENABLE_GLOB:
+                       session_spec.type = session_spec::GLOB_PATTERN;
+                       break;
+               case OPT_ALL:
+                       session_spec.type = session_spec::ALL;
+                       break;
                default:
                        ret = CMD_UNDEFINED;
                        goto end;
@@ -213,7 +225,7 @@ int cmd_stop(int argc, const char **argv)
                }
        }
 
-       arg_session_name = poptGetArg(pc);
+       session_spec.value = poptGetArg(pc);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -222,7 +234,7 @@ int cmd_stop(int argc, const char **argv)
                goto end;
        }
 
-       command_ret = stop_tracing(arg_session_name);
+       command_ret = stop_tracing(session_spec);
        if (command_ret) {
                success = 0;
        }
This page took 0.028474 seconds and 4 git commands to generate.