| 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 | class session_list; |
| 50 | |
| 51 | namespace details { |
| 52 | class session_storage { |
| 53 | public: |
| 54 | session_storage(lttng_session *raw_sessions, std::size_t sessions_count) : |
| 55 | _array(raw_sessions), _count(sessions_count) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | session_storage(session_storage&& original) : |
| 60 | _array(std::move(original._array)), _count(original._count) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | session_storage(session_storage&& original, std::size_t new_count) : |
| 65 | _array(std::move(original._array)), _count(new_count) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | std::unique_ptr<lttng_session, |
| 70 | lttng::memory::create_deleter_class<lttng_session, lttng::free>::deleter> |
| 71 | _array = nullptr; |
| 72 | std::size_t _count = 0; |
| 73 | }; |
| 74 | |
| 75 | class session_list_operations { |
| 76 | public: |
| 77 | static lttng_session& get(const lttng::cli::details::session_storage& storage, |
| 78 | std::size_t index) noexcept |
| 79 | { |
| 80 | return storage._array.get()[index]; |
| 81 | } |
| 82 | |
| 83 | static std::size_t size(const lttng::cli::details::session_storage& storage) |
| 84 | { |
| 85 | return storage._count; |
| 86 | } |
| 87 | }; |
| 88 | } /* namespace details */ |
| 89 | |
| 90 | /* |
| 91 | * We don't use a std::vector here because it would make a copy of the C array. |
| 92 | */ |
| 93 | class session_list |
| 94 | : public lttng::utils::random_access_container_wrapper<details::session_storage, |
| 95 | lttng_session&, |
| 96 | details::session_list_operations> { |
| 97 | public: |
| 98 | friend details::session_list_operations; |
| 99 | |
| 100 | session_list() : |
| 101 | lttng::utils::random_access_container_wrapper<details::session_storage, |
| 102 | lttng_session&, |
| 103 | details::session_list_operations>( |
| 104 | { nullptr, 0 }) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | session_list(session_list&& original) : |
| 109 | lttng::utils::random_access_container_wrapper<details::session_storage, |
| 110 | lttng_session&, |
| 111 | details::session_list_operations>( |
| 112 | std::move(original._container)) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | session_list(session_list&& original, std::size_t new_count) : |
| 117 | lttng::utils::random_access_container_wrapper<details::session_storage, |
| 118 | lttng_session&, |
| 119 | details::session_list_operations>( |
| 120 | { std::move(original._container), new_count }) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | session_list(lttng_session *raw_sessions, std::size_t raw_sessions_count) : |
| 125 | lttng::utils::random_access_container_wrapper<details::session_storage, |
| 126 | lttng_session&, |
| 127 | details::session_list_operations>( |
| 128 | { raw_sessions, raw_sessions_count }) |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | void resize(std::size_t new_size) noexcept |
| 133 | { |
| 134 | _container._count = new_size; |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | lttng::cli::session_list list_sessions(const struct session_spec& spec); |
| 139 | } /* namespace cli */ |
| 140 | } /* namespace lttng */ |
| 141 | |
| 142 | char *get_session_name(void); |
| 143 | char *get_session_name_quiet(void); |
| 144 | void list_commands(struct cmd_struct *commands, FILE *ofp); |
| 145 | void list_cmd_options(FILE *ofp, struct poptOption *options); |
| 146 | void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options); |
| 147 | |
| 148 | /* |
| 149 | * Return the minimum order for which x <= (1UL << order). |
| 150 | * Return -1 if x is 0. |
| 151 | */ |
| 152 | int get_count_order_u32(uint32_t x); |
| 153 | |
| 154 | /* |
| 155 | * Return the minimum order for which x <= (1UL << order). |
| 156 | * Return -1 if x is 0. |
| 157 | */ |
| 158 | int get_count_order_u64(uint64_t x); |
| 159 | |
| 160 | /* |
| 161 | * Return the minimum order for which x <= (1UL << order). |
| 162 | * Return -1 if x is 0. |
| 163 | */ |
| 164 | int get_count_order_ulong(unsigned long x); |
| 165 | |
| 166 | const char *get_event_type_str(enum lttng_event_type event_type); |
| 167 | |
| 168 | int print_missing_or_multiple_domains(unsigned int domain_count, bool include_agent_domains); |
| 169 | |
| 170 | int spawn_relayd(const char *pathname, int port); |
| 171 | int check_relayd(void); |
| 172 | void print_session_stats(const char *session_name); |
| 173 | int get_session_stats_str(const char *session_name, char **str); |
| 174 | int show_cmd_help(const char *cmd_name, const char *help_msg); |
| 175 | |
| 176 | int print_trace_archive_location(const struct lttng_trace_archive_location *location, |
| 177 | const char *session_name); |
| 178 | |
| 179 | int validate_exclusion_list(const char *event_name, |
| 180 | const struct lttng_dynamic_pointer_array *exclusions); |
| 181 | |
| 182 | #endif /* _LTTNG_UTILS_H */ |