From eaa8ef76a52ba21e42d7d95355af72ff3a85a116 Mon Sep 17 00:00:00 2001 From: Olivier Dion Date: Mon, 6 Feb 2023 16:06:10 -0500 Subject: [PATCH] lttng: Add --all, --glob options to lttng-start MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I8ff806c7ea7a51b8fa69206bd427b5e106706073 Signed-off-by: Olivier Dion Signed-off-by: Jérémie Galarneau --- doc/man/lttng-start.1.txt | 27 +++++++++++- src/bin/lttng/commands/start.cpp | 73 +++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/doc/man/lttng-start.1.txt b/doc/man/lttng-start.1.txt index b595b2f5c..f3ee0e785 100644 --- a/doc/man/lttng-start.1.txt +++ b/doc/man/lttng-start.1.txt @@ -11,7 +11,7 @@ lttng-start - Start an LTTng recording session SYNOPSIS -------- [verse] -*lttng* ['linkgenoptions:(GENERAL OPTIONS)'] *start* ['SESSION'] +*lttng* ['linkgenoptions:(GENERAL OPTIONS)'] *start* [option::--all | option::--glob 'SESSION' | 'SESSION' ] DESCRIPTION @@ -44,6 +44,11 @@ include::common-lttng-cmd-options-head.txt[] include::common-lttng-cmd-help-options.txt[] +option:-a, option:--all:: + Start all sessions. + +option:-g, option:--glob:: + Interpret SESSION as a globbing pattern. include::common-lttng-cmd-after-options.txt[] @@ -67,6 +72,26 @@ $ lttng start my-session ---- ==== +.Start all sessions. +==== +See the option::--all option. + +[role="term"] +---- +$ lttng start --all +---- +==== + +.Start all sessions with the suffix foo. +==== +See the option::--glob option. + +[role="term"] +---- +$ lttng start --glob '*foo' +---- +==== + include::common-footer.txt[] diff --git a/src/bin/lttng/commands/start.cpp b/src/bin/lttng/commands/start.cpp index d8e356976..556c1120d 100644 --- a/src/bin/lttng/commands/start.cpp +++ b/src/bin/lttng/commands/start.cpp @@ -7,7 +7,9 @@ #define _LGPL_SOURCE #include "../command.hpp" +#include "../utils.hpp" +#include #include #include @@ -30,16 +32,20 @@ static const char help_msg[] = enum { OPT_HELP = 1, OPT_LIST_OPTIONS, + OPT_ENABLE_GLOB, + OPT_ALL, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, 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 } }; -static int mi_print_session(char *session_name, int enabled) +static int mi_print_session(const char *session_name, int enabled) { int ret; @@ -72,19 +78,9 @@ end: * * Start tracing for all trace of the session. */ -static int start_tracing(const char *arg_session_name) +static int start_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; @@ -103,7 +99,7 @@ static int start_tracing(const char *arg_session_name) ERR("%s", lttng_strerror(ret)); break; } - goto free_name; + goto error; } ret = CMD_SUCCESS; @@ -113,16 +109,46 @@ static int start_tracing(const char *arg_session_name) ret = mi_print_session(session_name, 1); if (ret) { ret = CMD_ERROR; - goto free_name; + goto error; } } -free_name: - free(session_name); error: return ret; } +static int start_tracing(const struct session_spec& spec) +{ + int ret = CMD_SUCCESS; + bool had_warning = false; + + try { + for (const auto& session : list_sessions(spec)) { + const auto sub_ret = start_tracing(session.name); + + switch (sub_ret) { + case CMD_WARNING: + had_warning = true; + /* fall-through. */ + case CMD_SUCCESS: + continue; + default: + ret = sub_ret; + break; + } + } + } catch (const std::exception& e) { + ERR_FMT("{}", e.what()); + return CMD_FATAL; + } + + if (ret == CMD_SUCCESS && had_warning) { + ret = CMD_WARNING; + } + + return ret; +} + /* * cmd_start * @@ -132,8 +158,11 @@ int cmd_start(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); @@ -146,13 +175,19 @@ int cmd_start(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; } } - arg_session_name = poptGetArg(pc); + session_spec.value = poptGetArg(pc); leftover = poptGetArg(pc); if (leftover) { @@ -194,7 +229,7 @@ int cmd_start(int argc, const char **argv) } } - command_ret = start_tracing(arg_session_name); + command_ret = start_tracing(session_spec); if (command_ret) { success = 0; } -- 2.34.1