lttng: start: ensure a cmd_error_code is returned by the command
[lttng-tools.git] / src / bin / lttng / utils.hpp
CommitLineData
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 21extern char *opt_relayd_path;
92360082 22extern int opt_no_sessiond;
28f23191 23extern char *opt_sessiond_path;
92360082 24extern pid_t sessiond_pid;
8960e9cd 25
3c9bd23c
SM
26struct cmd_struct;
27
ca938aa5
OD
28struct 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 */
42class session_list {
9497e2f7
JG
43 template <typename ContainerType, typename DereferenceReturnType>
44 class iterator_template : public std::iterator<std::random_access_iterator_tag, std::size_t> {
ca938aa5 45 public:
9497e2f7 46 explicit iterator_template(ContainerType& list, std::size_t k) : _list(list), _index(k)
ca938aa5
OD
47 {
48 }
49
9497e2f7 50 iterator_template& operator++() noexcept
ca938aa5
OD
51 {
52 ++_index;
53 return *this;
54 }
55
9497e2f7 56 iterator_template& operator--() noexcept
ca938aa5
OD
57 {
58 --_index;
59 return *this;
60 }
61
9497e2f7 62 iterator_template& operator++(int) noexcept
ca938aa5
OD
63 {
64 _index++;
65 return *this;
66 }
67
9497e2f7 68 iterator_template& operator--(int) noexcept
ca938aa5
OD
69 {
70 _index--;
71 return *this;
72 }
73
9497e2f7 74 bool operator==(iterator_template other) const noexcept
ca938aa5
OD
75 {
76 return _index == other._index;
77 }
78
9497e2f7 79 bool operator!=(iterator_template other) const noexcept
ca938aa5
OD
80 {
81 return !(*this == other);
82 }
83
9497e2f7 84 DereferenceReturnType& operator*() const noexcept
ca938aa5
OD
85 {
86 return _list[_index];
87 }
88
89 private:
9497e2f7 90 ContainerType& _list;
ca938aa5
OD
91 std::size_t _index;
92 };
93
9497e2f7
JG
94 using iterator = iterator_template<session_list, lttng_session>;
95 using const_iterator = iterator_template<const session_list, const lttng_session>;
96
ca938aa5
OD
97public:
98 session_list() : _sessions_count(0), _sessions(nullptr)
99 {
100 }
101
102 session_list(session_list&& original, std::size_t new_count)
103 {
104 _sessions_count = new_count;
105 _sessions = std::move(original._sessions);
106 }
107
108 session_list(struct lttng_session *raw_sessions, std::size_t raw_sessions_count)
109 {
110 _sessions_count = raw_sessions_count;
111 _sessions.reset(raw_sessions);
112 }
113
114 iterator begin() noexcept
115 {
116 return iterator(*this, 0);
117 }
118
119 iterator end() noexcept
120 {
121 return iterator(*this, _sessions_count);
122 }
123
9497e2f7
JG
124 const_iterator begin() const noexcept
125 {
126 return const_iterator(*this, 0);
127 }
128
129 const_iterator end() const noexcept
130 {
131 return const_iterator(*this, _sessions_count);
132 }
133
ca938aa5
OD
134 std::size_t size() const noexcept
135 {
136 return _sessions_count;
137 }
138
139 void resize(std::size_t new_size) noexcept
140 {
141 _sessions_count = new_size;
142 }
143
144 lttng_session& operator[](std::size_t index)
145 {
146 LTTNG_ASSERT(index < _sessions_count);
147 return _sessions.get()[index];
148 }
149
150 const lttng_session& operator[](std::size_t index) const
151 {
152 LTTNG_ASSERT(index < _sessions_count);
153 return _sessions.get()[index];
154 }
155
156private:
157 std::size_t _sessions_count;
158 std::unique_ptr<lttng_session,
159 lttng::details::create_unique_class<lttng_session, lttng::free>>
160 _sessions;
161};
162
f3ed775e 163char *get_session_name(void);
1dac0189 164char *get_session_name_quiet(void);
3c9bd23c 165void list_commands(struct cmd_struct *commands, FILE *ofp);
679b4943 166void list_cmd_options(FILE *ofp, struct poptOption *options);
b083f028 167void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options);
f3ed775e 168
8ce58bad
MD
169/*
170 * Return the minimum order for which x <= (1UL << order).
171 * Return -1 if x is 0.
172 */
173int get_count_order_u32(uint32_t x);
174
175/*
176 * Return the minimum order for which x <= (1UL << order).
177 * Return -1 if x is 0.
178 */
179int get_count_order_u64(uint64_t x);
180
181/*
182 * Return the minimum order for which x <= (1UL << order).
183 * Return -1 if x is 0.
184 */
185int get_count_order_ulong(unsigned long x);
186
4fd2697f 187const char *get_event_type_str(enum lttng_event_type event_type);
b9dfb167 188
28f23191 189int print_missing_or_multiple_domains(unsigned int domain_count, bool include_agent_domains);
b9dfb167 190
8960e9cd
DG
191int spawn_relayd(const char *pathname, int port);
192int check_relayd(void);
20fb9e02 193void print_session_stats(const char *session_name);
58f237ca 194int get_session_stats_str(const char *session_name, char **str);
4fc83d94 195int show_cmd_help(const char *cmd_name, const char *help_msg);
8960e9cd 196
28f23191
JG
197int print_trace_archive_location(const struct lttng_trace_archive_location *location,
198 const char *session_name);
bbbfd849 199
e358ddd5 200int validate_exclusion_list(const char *event_name,
28f23191 201 const struct lttng_dynamic_pointer_array *exclusions);
e358ddd5 202
ca938aa5
OD
203session_list list_sessions(const struct session_spec& spec);
204
f3ed775e 205#endif /* _LTTNG_UTILS_H */
This page took 0.096883 seconds and 4 git commands to generate.