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