| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTTNG_UTILS_H |
| 9 | #define _LTTNG_UTILS_H |
| 10 | |
| 11 | #include <common/argpar/argpar.h> |
| 12 | #include <common/container-wrapper.hpp> |
| 13 | #include <common/dynamic-array.hpp> |
| 14 | #include <common/make-unique-wrapper.hpp> |
| 15 | |
| 16 | #include <lttng/lttng.h> |
| 17 | |
| 18 | #include <iterator> |
| 19 | #include <memory> |
| 20 | #include <popt.h> |
| 21 | |
| 22 | extern char *opt_relayd_path; |
| 23 | extern int opt_no_sessiond; |
| 24 | extern char *opt_sessiond_path; |
| 25 | extern pid_t sessiond_pid; |
| 26 | |
| 27 | struct cmd_struct; |
| 28 | |
| 29 | namespace lttng { |
| 30 | namespace cli { |
| 31 | |
| 32 | struct session_spec { |
| 33 | enum class type { |
| 34 | NAME, |
| 35 | GLOB_PATTERN, |
| 36 | ALL, |
| 37 | }; |
| 38 | |
| 39 | explicit session_spec(type spec_type, const char *name_or_pattern = nullptr) noexcept : |
| 40 | type_(spec_type), value(name_or_pattern) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /* Disambiguate type enum from the member for buggy g++ versions. */ |
| 45 | type type_; |
| 46 | const char *value; |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * We don't use a std::vector here because it would make a copy of the C array. |
| 51 | */ |
| 52 | class session_list { |
| 53 | template <typename ContainerType, typename DereferenceReturnType> |
| 54 | class _iterator : public std::iterator<std::random_access_iterator_tag, std::size_t> { |
| 55 | public: |
| 56 | explicit _iterator(ContainerType& list, std::size_t k) : _list(list), _index(k) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | _iterator& operator++() noexcept |
| 61 | { |
| 62 | ++_index; |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | _iterator& operator--() noexcept |
| 67 | { |
| 68 | --_index; |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | _iterator& operator++(int) noexcept |
| 73 | { |
| 74 | _index++; |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | _iterator& operator--(int) noexcept |
| 79 | { |
| 80 | _index--; |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | bool operator==(_iterator other) const noexcept |
| 85 | { |
| 86 | return _index == other._index; |
| 87 | } |
| 88 | |
| 89 | bool operator!=(_iterator other) const noexcept |
| 90 | { |
| 91 | return !(*this == other); |
| 92 | } |
| 93 | |
| 94 | DereferenceReturnType& operator*() const noexcept |
| 95 | { |
| 96 | return _list[_index]; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | ContainerType& _list; |
| 101 | std::size_t _index; |
| 102 | }; |
| 103 | |
| 104 | using iterator = _iterator<session_list, lttng_session>; |
| 105 | using const_iterator = _iterator<const session_list, const lttng_session>; |
| 106 | |
| 107 | public: |
| 108 | session_list() : _sessions_count(0), _sessions(nullptr) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | session_list(session_list&& original, std::size_t new_count) |
| 113 | { |
| 114 | _sessions_count = new_count; |
| 115 | _sessions = std::move(original._sessions); |
| 116 | } |
| 117 | |
| 118 | session_list(struct lttng_session *raw_sessions, std::size_t raw_sessions_count) |
| 119 | { |
| 120 | _sessions_count = raw_sessions_count; |
| 121 | _sessions.reset(raw_sessions); |
| 122 | } |
| 123 | |
| 124 | iterator begin() noexcept |
| 125 | { |
| 126 | return iterator(*this, 0); |
| 127 | } |
| 128 | |
| 129 | iterator end() noexcept |
| 130 | { |
| 131 | return iterator(*this, _sessions_count); |
| 132 | } |
| 133 | |
| 134 | const_iterator begin() const noexcept |
| 135 | { |
| 136 | return const_iterator(*this, 0); |
| 137 | } |
| 138 | |
| 139 | const_iterator end() const noexcept |
| 140 | { |
| 141 | return const_iterator(*this, _sessions_count); |
| 142 | } |
| 143 | |
| 144 | std::size_t size() const noexcept |
| 145 | { |
| 146 | return _sessions_count; |
| 147 | } |
| 148 | |
| 149 | void resize(std::size_t new_size) noexcept |
| 150 | { |
| 151 | _sessions_count = new_size; |
| 152 | } |
| 153 | |
| 154 | lttng_session& operator[](std::size_t index) |
| 155 | { |
| 156 | LTTNG_ASSERT(index < _sessions_count); |
| 157 | return _sessions.get()[index]; |
| 158 | } |
| 159 | |
| 160 | const lttng_session& operator[](std::size_t index) const |
| 161 | { |
| 162 | LTTNG_ASSERT(index < _sessions_count); |
| 163 | return _sessions.get()[index]; |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | std::size_t _sessions_count; |
| 168 | std::unique_ptr<lttng_session, |
| 169 | lttng::memory::create_deleter_class<lttng_session, lttng::free>::deleter> |
| 170 | _sessions; |
| 171 | }; |
| 172 | |
| 173 | lttng::cli::session_list list_sessions(const struct session_spec& spec); |
| 174 | } /* namespace cli */ |
| 175 | } /* namespace lttng */ |
| 176 | |
| 177 | char *get_session_name(void); |
| 178 | char *get_session_name_quiet(void); |
| 179 | void list_commands(struct cmd_struct *commands, FILE *ofp); |
| 180 | void list_cmd_options(FILE *ofp, struct poptOption *options); |
| 181 | void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options); |
| 182 | |
| 183 | /* |
| 184 | * Return the minimum order for which x <= (1UL << order). |
| 185 | * Return -1 if x is 0. |
| 186 | */ |
| 187 | int get_count_order_u32(uint32_t x); |
| 188 | |
| 189 | /* |
| 190 | * Return the minimum order for which x <= (1UL << order). |
| 191 | * Return -1 if x is 0. |
| 192 | */ |
| 193 | int get_count_order_u64(uint64_t x); |
| 194 | |
| 195 | /* |
| 196 | * Return the minimum order for which x <= (1UL << order). |
| 197 | * Return -1 if x is 0. |
| 198 | */ |
| 199 | int get_count_order_ulong(unsigned long x); |
| 200 | |
| 201 | const char *get_event_type_str(enum lttng_event_type event_type); |
| 202 | |
| 203 | int print_missing_or_multiple_domains(unsigned int domain_count, bool include_agent_domains); |
| 204 | |
| 205 | int spawn_relayd(const char *pathname, int port); |
| 206 | int check_relayd(void); |
| 207 | void print_session_stats(const char *session_name); |
| 208 | int get_session_stats_str(const char *session_name, char **str); |
| 209 | int show_cmd_help(const char *cmd_name, const char *help_msg); |
| 210 | |
| 211 | int print_trace_archive_location(const struct lttng_trace_archive_location *location, |
| 212 | const char *session_name); |
| 213 | |
| 214 | int validate_exclusion_list(const char *event_name, |
| 215 | const struct lttng_dynamic_pointer_array *exclusions); |
| 216 | |
| 217 | #endif /* _LTTNG_UTILS_H */ |