Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / bin / lttng / utils.cpp
index 528318b2af6bf10f442447720641da59c344df7e..45fe256a579d092b4a1d92ddd81e26459b315a1e 100644 (file)
@@ -6,25 +6,30 @@
  */
 
 #define _LGPL_SOURCE
-#include <stdlib.h>
+#include "command.hpp"
+#include "conf.hpp"
+#include "exception.hpp"
+#include "utils.hpp"
+
+#include <common/defaults.hpp>
+#include <common/error.hpp>
+#include <common/exception.hpp>
+#include <common/make-unique-wrapper.hpp>
+#include <common/utils.hpp>
+
+#include <arpa/inet.h>
 #include <ctype.h>
+#include <fnmatch.h>
+#include <inttypes.h>
+#include <iostream>
 #include <limits.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <signal.h>
 #include <netinet/in.h>
-#include <arpa/inet.h>
-#include <inttypes.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/types.h>
 #include <unistd.h>
 
-#include <common/error.h>
-#include <common/utils.h>
-#include <common/defaults.h>
-
-#include "conf.h"
-#include "utils.h"
-#include "command.h"
-
 static const char *str_all = "ALL";
 static const char *str_tracepoint = "Tracepoint";
 static const char *str_syscall = "Syscall";
@@ -32,22 +37,21 @@ static const char *str_probe = "Probe";
 static const char *str_userspace_probe = "Userspace Probe";
 static const char *str_function = "Function";
 
-static
-char *_get_session_name(int quiet)
+static char *_get_session_name(int quiet)
 {
        const char *path;
-       char *session_name = NULL;
+       char *session_name = nullptr;
 
        /* Get path to config file */
        path = utils_get_home_dir();
-       if (path == NULL) {
+       if (path == nullptr) {
                goto error;
        }
 
        /* Get session name from config */
        session_name = quiet ? config_read_session_name_quiet(path) :
-               config_read_session_name(path);
-       if (session_name == NULL) {
+                              config_read_session_name(path);
+       if (session_name == nullptr) {
                goto error;
        }
 
@@ -56,7 +60,7 @@ char *_get_session_name(int quiet)
        return session_name;
 
 error:
-       return NULL;
+       return nullptr;
 }
 
 /*
@@ -65,7 +69,7 @@ error:
  *  Return allocated string with the session name found in the config
  *  directory.
  */
-char *get_session_name(void)
+char *get_session_name()
 {
        return _get_session_name(0);
 }
@@ -76,7 +80,7 @@ char *get_session_name(void)
  *  Return allocated string with the session name found in the config
  *  directory.
  */
-char *get_session_name_quiet(void)
+char *get_session_name_quiet()
 {
        return _get_session_name(1);
 }
@@ -90,10 +94,10 @@ char *get_session_name_quiet(void)
 void list_commands(struct cmd_struct *commands, FILE *ofp)
 {
        int i = 0;
-       struct cmd_struct *cmd = NULL;
+       struct cmd_struct *cmd = nullptr;
 
        cmd = &commands[i];
-       while (cmd->name != NULL) {
+       while (cmd->name != nullptr) {
                fprintf(ofp, "%s\n", cmd->name);
                i++;
                cmd = &commands[i];
@@ -109,9 +113,9 @@ void list_commands(struct cmd_struct *commands, FILE *ofp)
 void list_cmd_options(FILE *ofp, struct poptOption *options)
 {
        int i;
-       struct poptOption *option = NULL;
+       struct poptOption *option = nullptr;
 
-       for (i = 0; options[i].longName != NULL; i++) {
+       for (i = 0; options[i].longName != nullptr; i++) {
                option = &options[i];
 
                fprintf(ofp, "--%s\n", option->longName);
@@ -129,7 +133,7 @@ void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options)
 {
        int i;
 
-       for (i = 0; options[i].long_name != NULL; i++) {
+       for (i = 0; options[i].long_name != nullptr; i++) {
                const struct argpar_opt_descr *option = &options[i];
 
                fprintf(ofp, "--%s\n", option->long_name);
@@ -146,8 +150,7 @@ void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options)
  * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
  */
 #if defined(__i386) || defined(__x86_64)
-static inline
-unsigned int fls_u32(uint32_t x)
+static inline unsigned int fls_u32(uint32_t x)
 {
        int r;
 
@@ -155,15 +158,15 @@ unsigned int fls_u32(uint32_t x)
            "jnz 1f\n\t"
            "movl $-1,%0\n\t"
            "1:\n\t"
-           : "=r" (r) : "rm" (x));
+           : "=r"(r)
+           : "rm"(x));
        return r + 1;
 }
 #define HAS_FLS_U32
 #endif
 
 #if defined(__x86_64) && defined(__LP64__)
-static inline
-unsigned int fls_u64(uint64_t x)
+static inline unsigned int fls_u64(uint64_t x)
 {
        long r;
 
@@ -171,15 +174,15 @@ unsigned int fls_u64(uint64_t x)
            "jnz 1f\n\t"
            "movq $-1,%0\n\t"
            "1:\n\t"
-           : "=r" (r) : "rm" (x));
+           : "=r"(r)
+           : "rm"(x));
        return r + 1;
 }
 #define HAS_FLS_U64
 #endif
 
 #ifndef HAS_FLS_U64
-static __attribute__((unused))
-unsigned int fls_u64(uint64_t x)
+static __attribute__((unused)) unsigned int fls_u64(uint64_t x)
 {
        unsigned int r = 64;
 
@@ -215,8 +218,7 @@ unsigned int fls_u64(uint64_t x)
 #endif
 
 #ifndef HAS_FLS_U32
-static __attribute__((unused))
-unsigned int fls_u32(uint32_t x)
+static __attribute__((unused)) unsigned int fls_u32(uint32_t x)
 {
        unsigned int r = 32;
 
@@ -246,8 +248,7 @@ unsigned int fls_u32(uint32_t x)
 }
 #endif
 
-static
-unsigned int fls_ulong(unsigned long x)
+static unsigned int fls_ulong(unsigned long x)
 {
 #if (CAA_BITS_PER_LONG == 32)
        return fls_u32(x);
@@ -355,7 +356,7 @@ int spawn_relayd(const char *pathname, int port)
                } else {
                        PERROR("execlp");
                }
-               kill(getppid(), SIGTERM);       /* wake parent */
+               kill(getppid(), SIGTERM); /* wake parent */
                exit(EXIT_FAILURE);
        } else if (pid > 0) {
                goto end;
@@ -374,7 +375,7 @@ end:
  *
  * Return 1 if found else 0 if NOT found. Negative value on error.
  */
-int check_relayd(void)
+int check_relayd()
 {
        int ret, fd;
        struct sockaddr_in sin;
@@ -416,16 +417,13 @@ error_socket:
        return ret;
 }
 
-int print_missing_or_multiple_domains(unsigned int domain_count,
-               bool include_agent_domains)
+int print_missing_or_multiple_domains(unsigned int domain_count, bool include_agent_domains)
 {
        int ret = 0;
 
        if (domain_count == 0) {
                ERR("Please specify a domain (--kernel/--userspace%s).",
-                               include_agent_domains ?
-                                               "/--jul/--log4j/--python" :
-                                               "");
+                   include_agent_domains ? "/--jul/--log4j/--python" : "");
                ret = -1;
        } else if (domain_count > 1) {
                ERR("Only one domain must be specified.");
@@ -452,12 +450,12 @@ void print_session_stats(const char *session_name)
 int get_session_stats_str(const char *session_name, char **out_str)
 {
        int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
-       struct lttng_domain *domains = NULL;
-       struct lttng_channel *channels = NULL;
+       struct lttng_domain *domains = nullptr;
+       struct lttng_channel *channels = nullptr;
        uint64_t discarded_events_total = 0, lost_packets_total = 0;
-       struct lttng_session *sessions = NULL;
-       const struct lttng_session *selected_session = NULL;
-       char *stats_str = NULL;
+       struct lttng_session *sessions = nullptr;
+       const struct lttng_session *selected_session = nullptr;
+       char *stats_str = nullptr;
        bool print_discarded_events = false, print_lost_packets = false;
 
        count = lttng_list_sessions(&sessions);
@@ -475,7 +473,8 @@ int get_session_stats_str(const char *session_name, char **out_str)
                }
        }
        if (!selected_session) {
-               ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
+               ERR("Failed to retrieve session \"%s\" description while printing session statistics.",
+                   session_name);
                ret = -1;
                goto end;
        }
@@ -486,8 +485,8 @@ int get_session_stats_str(const char *session_name, char **out_str)
                goto end;
        }
        for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
-               struct lttng_handle *handle = lttng_create_handle(session_name,
-                               &domains[domain_idx]);
+               struct lttng_handle *handle =
+                       lttng_create_handle(session_name, &domains[domain_idx]);
 
                if (!handle) {
                        ERR("Failed to create session handle while printing session statistics.");
@@ -496,24 +495,22 @@ int get_session_stats_str(const char *session_name, char **out_str)
                }
 
                free(channels);
-               channels = NULL;
+               channels = nullptr;
                count = lttng_list_channels(handle, &channels);
                for (channel_idx = 0; channel_idx < count; channel_idx++) {
                        uint64_t discarded_events = 0, lost_packets = 0;
                        struct lttng_channel *channel = &channels[channel_idx];
 
-                       ret = lttng_channel_get_discarded_event_count(channel,
-                                       &discarded_events);
+                       ret = lttng_channel_get_discarded_event_count(channel, &discarded_events);
                        if (ret) {
                                ERR("Failed to retrieve discarded event count from channel %s",
-                                               channel->name);
+                                   channel->name);
                        }
 
-                       ret = lttng_channel_get_lost_packet_count(channel,
-                                       &lost_packets);
+                       ret = lttng_channel_get_lost_packet_count(channel, &lost_packets);
                        if (ret) {
                                ERR("Failed to retrieve lost packet count from channel %s",
-                                               channel->name);
+                                   channel->name);
                        }
 
                        discarded_events_total += discarded_events;
@@ -522,30 +519,26 @@ int get_session_stats_str(const char *session_name, char **out_str)
                lttng_destroy_handle(handle);
        }
 
-       print_discarded_events = discarded_events_total > 0 &&
-                                !selected_session->snapshot_mode;
-       print_lost_packets = lost_packets_total > 0 &&
-                            !selected_session->snapshot_mode;
+       print_discarded_events = discarded_events_total > 0 && !selected_session->snapshot_mode;
+       print_lost_packets = lost_packets_total > 0 && !selected_session->snapshot_mode;
 
        if (print_discarded_events && print_lost_packets) {
                ret = asprintf(&stats_str,
-                               "Warning: %" PRIu64
-                               " events were discarded and %" PRIu64
-                               " packets were lost, please refer to "
-                               "the documentation on channel configuration.",
-                               discarded_events_total, lost_packets_total);
+                              "Warning: %" PRIu64 " events were discarded and %" PRIu64
+                              " packets were lost, please refer to "
+                              "the documentation on channel configuration.",
+                              discarded_events_total,
+                              lost_packets_total);
        } else if (print_discarded_events) {
                ret = asprintf(&stats_str,
-                               "Warning: %" PRIu64
-                               " events were discarded, please refer to "
-                               "the documentation on channel configuration.",
-                               discarded_events_total);
+                              "Warning: %" PRIu64 " events were discarded, please refer to "
+                              "the documentation on channel configuration.",
+                              discarded_events_total);
        } else if (print_lost_packets) {
                ret = asprintf(&stats_str,
-                               "Warning: %" PRIu64
-                               " packets were lost, please refer to "
-                               "the documentation on channel configuration.",
-                               lost_packets_total);
+                              "Warning: %" PRIu64 " packets were lost, please refer to "
+                              "the documentation on channel configuration.",
+                              lost_packets_total);
        } else {
                ret = 0;
        }
@@ -579,9 +572,8 @@ int show_cmd_help(const char *cmd_name, const char *help_msg)
        return ret;
 }
 
-int print_trace_archive_location(
-               const struct lttng_trace_archive_location *location,
-               const char *session_name)
+int print_trace_archive_location(const struct lttng_trace_archive_location *location,
+                                const char *session_name)
 {
        int ret = 0;
        enum lttng_trace_archive_location_type location_type;
@@ -590,15 +582,14 @@ int print_trace_archive_location(
 
        location_type = lttng_trace_archive_location_get_type(location);
 
-       _MSG("Trace chunk archive for session %s is now readable",
-                       session_name);
+       _MSG("Trace chunk archive for session %s is now readable", session_name);
        switch (location_type) {
        case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
        {
                const char *absolute_path;
 
-               status = lttng_trace_archive_location_local_get_absolute_path(
-                               location, &absolute_path);
+               status = lttng_trace_archive_location_local_get_absolute_path(location,
+                                                                             &absolute_path);
                if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
                        ret = -1;
                        goto end;
@@ -614,36 +605,33 @@ int print_trace_archive_location(
                enum lttng_trace_archive_location_relay_protocol_type protocol;
 
                /* Fetch all relay location parameters. */
-               status = lttng_trace_archive_location_relay_get_protocol_type(
-                               location, &protocol);
+               status = lttng_trace_archive_location_relay_get_protocol_type(location, &protocol);
                if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
                        ret = -1;
                        goto end;
                }
 
-               status = lttng_trace_archive_location_relay_get_host(
-                               location, &host);
+               status = lttng_trace_archive_location_relay_get_host(location, &host);
                if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
                        ret = -1;
                        goto end;
                }
 
-               status = lttng_trace_archive_location_relay_get_control_port(
-                               location, &control_port);
+               status = lttng_trace_archive_location_relay_get_control_port(location,
+                                                                            &control_port);
                if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
                        ret = -1;
                        goto end;
                }
 
-               status = lttng_trace_archive_location_relay_get_data_port(
-                               location, &data_port);
+               status = lttng_trace_archive_location_relay_get_data_port(location, &data_port);
                if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
                        ret = -1;
                        goto end;
                }
 
-               status = lttng_trace_archive_location_relay_get_relative_path(
-                               location, &relative_path);
+               status = lttng_trace_archive_location_relay_get_relative_path(location,
+                                                                             &relative_path);
                if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
                        ret = -1;
                        goto end;
@@ -658,9 +646,12 @@ int print_trace_archive_location(
                        break;
                }
 
-               MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
-                               PRIu16 "]", protocol_str, host,
-                               relative_path, control_port, data_port);
+               MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %" PRIu16 "]",
+                   protocol_str,
+                   host,
+                   relative_path,
+                   control_port,
+                   data_port);
                printed_location = true;
                break;
        }
@@ -673,3 +664,130 @@ end:
        }
        return ret;
 }
+
+namespace {
+template <typename FilterFunctionType>
+lttng::cli::session_list get_sessions(const FilterFunctionType& filter,
+                                     bool return_first_match_only = false)
+{
+       lttng::cli::session_list list = []() {
+               int list_ret;
+               struct lttng_session *psessions;
+
+               list_ret = lttng_list_sessions(&psessions);
+
+               if (list_ret < 0) {
+                       LTTNG_THROW_CTL("Failed to list sessions",
+                                       static_cast<lttng_error_code>(list_ret));
+               }
+
+               return lttng::cli::session_list(psessions, list_ret);
+       }();
+
+       std::size_t write_to = 0;
+       for (std::size_t read_from = 0; read_from < list.size(); ++read_from) {
+               if (!filter(list[read_from])) {
+                       continue;
+               }
+
+               if (read_from != write_to) {
+                       list[write_to] = list[read_from];
+               }
+
+               ++write_to;
+
+               if (return_first_match_only) {
+                       return lttng::cli::session_list(std::move(list), 1);
+               }
+       }
+
+       list.resize(write_to);
+
+       return list;
+}
+} /* namespace */
+
+lttng::cli::session_list lttng::cli::list_sessions(const struct session_spec& spec)
+{
+       switch (spec.type_) {
+       case lttng::cli::session_spec::type::NAME:
+               if (spec.value == nullptr) {
+                       const auto configured_name =
+                               lttng::make_unique_wrapper<char, lttng::memory::free>(
+                                       get_session_name());
+
+                       if (!configured_name) {
+                               LTTNG_THROW_CLI_NO_DEFAULT_SESSION();
+                       }
+
+                       const struct lttng::cli::session_spec new_spec(
+                               lttng::cli::session_spec::type::NAME, configured_name.get());
+
+                       return list_sessions(new_spec);
+               }
+
+               return get_sessions(
+                       [&spec](const lttng_session& session) {
+                               return strcmp(session.name, spec.value) == 0;
+                       },
+                       true);
+       case lttng::cli::session_spec::type::GLOB_PATTERN:
+               return get_sessions([&spec](const lttng_session& session) {
+                       return fnmatch(spec.value, session.name, 0) == 0;
+               });
+       case lttng::cli::session_spec::type::ALL:
+               return get_sessions([](const lttng_session&) { return true; });
+       }
+
+       return lttng::cli::session_list();
+}
+
+void print_kernel_tracer_status_error()
+{
+       if (lttng_opt_mi) {
+               return;
+       }
+
+       enum lttng_kernel_tracer_status kernel_tracer_status;
+       const auto ret = lttng_get_kernel_tracer_status(&kernel_tracer_status);
+
+       if (ret < 0) {
+               ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret));
+       } else {
+               switch (kernel_tracer_status) {
+               case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED:
+                       return;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN:
+                       std::cerr << "\tKernel module loading failed" << std::endl;
+                       break;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING:
+                       std::cerr << "\tMissing one or more required kernel modules" << std::endl;
+                       break;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE:
+                       std::cerr
+                               << "\tKernel module signature error prevented loading of one or more required kernel modules"
+                               << std::endl;
+                       break;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT:
+                       std::cerr << "\tlttng-sessiond isn't running as root" << std::endl;
+                       break;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER:
+                       std::cerr << "\tFailed to setup notifiers" << std::endl;
+                       break;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG:
+                       std::cerr << "\tlttng-sessiond failed to open /proc/lttng" << std::endl;
+                       break;
+               case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH:
+                       std::cerr << "\tVersion mismatch between kernel tracer and kernel tracer ABI"
+                                 << std::endl;
+                       break;
+               default:
+                       std::cerr << lttng::format("\t\tUnknown kernel tracer status (%d)",
+                                                  static_cast<int>(kernel_tracer_status))
+                                 << std::endl;
+                       break;
+               }
+
+               std::cerr << "\tConsult lttng-sessiond logs for more information" << std::endl;
+       }
+}
This page took 0.03141 seconds and 4 git commands to generate.