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