From 2a8d72ed132b6c6034abcbfa76e06aef2358575c Mon Sep 17 00:00:00 2001 From: Olivier Dion Date: Mon, 6 Feb 2023 16:11:56 -0500 Subject: [PATCH] lttng: Add --all, --glob options to lttng-stop MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ida7c8f68f9b52f7ab4414bf060383864bd2e3046 Signed-off-by: Olivier Dion Signed-off-by: Jérémie Galarneau --- doc/man/lttng-stop.1.txt | 27 ++++++++++++- src/bin/lttng/commands/stop.cpp | 68 +++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/doc/man/lttng-stop.1.txt b/doc/man/lttng-stop.1.txt index 0ad1f0e3c..ca1129df3 100644 --- a/doc/man/lttng-stop.1.txt +++ b/doc/man/lttng-stop.1.txt @@ -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[] diff --git a/src/bin/lttng/commands/stop.cpp b/src/bin/lttng/commands/stop.cpp index a6d3d6711..ce66b096a 100644 --- a/src/bin/lttng/commands/stop.cpp +++ b/src/bin/lttng/commands/stop.cpp @@ -7,7 +7,9 @@ #define _LGPL_SOURCE #include "../command.hpp" +#include "../utils.hpp" +#include #include #include @@ -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; } -- 2.34.1