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