lttng: Add list_sessions utility
authorOlivier Dion <odion@efficios.com>
Mon, 6 Feb 2023 20:55:39 +0000 (15:55 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 21 Apr 2023 15:47:48 +0000 (11:47 -0400)
Get a list of sessions given a session specification.

The `NAME' specification returns the session that has the given
name. The `GLOB_PATTERN' specification returns all sessions that match a
pattern. The `ALL' specification returns all sessions.

Change-Id: If69698d6dd59d54ba16669571ef49ed5bb9b8997
Signed-off-by: Olivier Dion <odion@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng/utils.cpp
src/bin/lttng/utils.hpp

index 90ca90fdeb3a4892424e62827d01e920d7951ef5..5b0c461d98a2d8f58627e76e3c6695914f606194 100644 (file)
 
 #include <common/defaults.hpp>
 #include <common/error.hpp>
+#include <common/exception.hpp>
+#include <common/make-unique-wrapper.hpp>
 #include <common/utils.hpp>
 
 #include <arpa/inet.h>
 #include <ctype.h>
+#include <fnmatch.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <netinet/in.h>
@@ -659,3 +662,81 @@ end:
        }
        return ret;
 }
+
+namespace {
+template <typename FilterFunctionType>
+session_list get_sessions(const FilterFunctionType& filter, bool return_first_match_only = false)
+{
+       session_list list;
+
+       {
+               int list_ret;
+               struct lttng_session *psessions;
+
+               list_ret = lttng_list_sessions(&psessions);
+
+               if (list_ret < 0) {
+                       LTTNG_THROW_CTL("Failed to list sessions",
+                                       static_cast<lttng_error_code>(list_ret));
+               }
+
+               list = session_list(psessions, list_ret);
+       }
+
+       std::size_t write_to = 0;
+       for (std::size_t read_from = 0; read_from < list.size(); ++read_from) {
+               if (!filter(list[read_from])) {
+                       continue;
+               }
+
+               if (read_from != write_to) {
+                       list[write_to] = list[read_from];
+               }
+
+               ++write_to;
+
+               if (return_first_match_only) {
+                       return session_list(std::move(list), 1);
+               }
+       }
+
+       list.resize(write_to);
+
+       return list;
+}
+} /* namespace */
+
+session_list list_sessions(const struct session_spec& spec)
+{
+       switch (spec.type) {
+       case session_spec::NAME:
+               if (spec.value == nullptr) {
+                       const auto configured_name =
+                               lttng::make_unique_wrapper<char, lttng::free>(get_session_name());
+
+                       if (configured_name) {
+                               const struct session_spec new_spec = {
+                                       .type = session_spec::NAME, .value = configured_name.get()
+                               };
+
+                               return list_sessions(new_spec);
+                       }
+
+                       return session_list();
+               }
+
+               return get_sessions(
+                       [&spec](const lttng_session& session) {
+                               return strcmp(session.name, spec.value) == 0;
+                       },
+                       true);
+       case session_spec::GLOB_PATTERN:
+               return get_sessions([&spec](const lttng_session& session) {
+                       return fnmatch(spec.value, session.name, 0) == 0;
+               });
+       case session_spec::ALL:
+               return get_sessions([](const lttng_session&) { return true; });
+       }
+
+       return session_list();
+}
index 64605575e94c33da7c3fdb0f2e4d52bf67a817b0..c3b442056cc1ecc6d88d518089c3168ea00679e0 100644 (file)
 
 #include <common/argpar/argpar.h>
 #include <common/dynamic-array.hpp>
+#include <common/make-unique-wrapper.hpp>
 
 #include <lttng/lttng.h>
 
+#include <iterator>
+#include <memory>
 #include <popt.h>
 
 extern char *opt_relayd_path;
@@ -22,6 +25,127 @@ extern pid_t sessiond_pid;
 
 struct cmd_struct;
 
+struct session_spec {
+       enum type {
+               NAME,
+               GLOB_PATTERN,
+               ALL,
+       };
+
+       type type;
+       const char *value;
+};
+
+/*
+ * We don't use a std::vector here because it would make a copy of the C array.
+ */
+class session_list {
+       class iterator : public std::iterator<std::random_access_iterator_tag, std::size_t> {
+       public:
+               explicit iterator(session_list& list, std::size_t k) : _list(list), _index(k)
+               {
+               }
+
+               iterator& operator++() noexcept
+               {
+                       ++_index;
+                       return *this;
+               }
+
+               iterator& operator--() noexcept
+               {
+                       --_index;
+                       return *this;
+               }
+
+               iterator& operator++(int) noexcept
+               {
+                       _index++;
+                       return *this;
+               }
+
+               iterator& operator--(int) noexcept
+               {
+                       _index--;
+                       return *this;
+               }
+
+               bool operator==(iterator other) const noexcept
+               {
+                       return _index == other._index;
+               }
+
+               bool operator!=(iterator other) const noexcept
+               {
+                       return !(*this == other);
+               }
+
+               lttng_session& operator*() const noexcept
+               {
+                       return _list[_index];
+               }
+
+       private:
+               session_list& _list;
+               std::size_t _index;
+       };
+
+public:
+       session_list() : _sessions_count(0), _sessions(nullptr)
+       {
+       }
+
+       session_list(session_list&& original, std::size_t new_count)
+       {
+               _sessions_count = new_count;
+               _sessions = std::move(original._sessions);
+       }
+
+       session_list(struct lttng_session *raw_sessions, std::size_t raw_sessions_count)
+       {
+               _sessions_count = raw_sessions_count;
+               _sessions.reset(raw_sessions);
+       }
+
+       iterator begin() noexcept
+       {
+               return iterator(*this, 0);
+       }
+
+       iterator end() noexcept
+       {
+               return iterator(*this, _sessions_count);
+       }
+
+       std::size_t size() const noexcept
+       {
+               return _sessions_count;
+       }
+
+       void resize(std::size_t new_size) noexcept
+       {
+               _sessions_count = new_size;
+       }
+
+       lttng_session& operator[](std::size_t index)
+       {
+               LTTNG_ASSERT(index < _sessions_count);
+               return _sessions.get()[index];
+       }
+
+       const lttng_session& operator[](std::size_t index) const
+       {
+               LTTNG_ASSERT(index < _sessions_count);
+               return _sessions.get()[index];
+       }
+
+private:
+       std::size_t _sessions_count;
+       std::unique_ptr<lttng_session,
+                       lttng::details::create_unique_class<lttng_session, lttng::free>>
+               _sessions;
+};
+
 char *get_session_name(void);
 char *get_session_name_quiet(void);
 void list_commands(struct cmd_struct *commands, FILE *ofp);
@@ -62,4 +186,6 @@ int print_trace_archive_location(const struct lttng_trace_archive_location *loca
 int validate_exclusion_list(const char *event_name,
                            const struct lttng_dynamic_pointer_array *exclusions);
 
+session_list list_sessions(const struct session_spec& spec);
+
 #endif /* _LTTNG_UTILS_H */
This page took 0.028323 seconds and 4 git commands to generate.