2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "exception.hpp"
14 #include <common/defaults.hpp>
15 #include <common/error.hpp>
16 #include <common/exception.hpp>
17 #include <common/make-unique-wrapper.hpp>
18 #include <common/utils.hpp>
20 #include <arpa/inet.h>
26 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
33 static const char *str_all
= "ALL";
34 static const char *str_tracepoint
= "Tracepoint";
35 static const char *str_syscall
= "Syscall";
36 static const char *str_probe
= "Probe";
37 static const char *str_userspace_probe
= "Userspace Probe";
38 static const char *str_function
= "Function";
40 static char *_get_session_name(int quiet
)
43 char *session_name
= nullptr;
45 /* Get path to config file */
46 path
= utils_get_home_dir();
47 if (path
== nullptr) {
51 /* Get session name from config */
52 session_name
= quiet
? config_read_session_name_quiet(path
) :
53 config_read_session_name(path
);
54 if (session_name
== nullptr) {
58 DBG2("Config file path found: %s", path
);
59 DBG("Session name found: %s", session_name
);
69 * Return allocated string with the session name found in the config
72 char *get_session_name()
74 return _get_session_name(0);
78 * get_session_name_quiet (no warnings/errors emitted)
80 * Return allocated string with the session name found in the config
83 char *get_session_name_quiet()
85 return _get_session_name(1);
91 * List commands line by line. This is mostly for bash auto completion and to
92 * avoid difficult parsing.
94 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
97 struct cmd_struct
*cmd
= nullptr;
100 while (cmd
->name
!= nullptr) {
101 fprintf(ofp
, "%s\n", cmd
->name
);
110 * Prints a simple list of the options available to a command. This is intended
111 * to be easily parsed for bash completion.
113 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
116 struct poptOption
*option
= nullptr;
118 for (i
= 0; options
[i
].longName
!= nullptr; i
++) {
119 option
= &options
[i
];
121 fprintf(ofp
, "--%s\n", option
->longName
);
123 if (isprint(option
->shortName
)) {
124 fprintf(ofp
, "-%c\n", option
->shortName
);
130 * Same as list_cmd_options, but for options specified for argpar.
132 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
136 for (i
= 0; options
[i
].long_name
!= nullptr; i
++) {
137 const struct argpar_opt_descr
*option
= &options
[i
];
139 fprintf(ofp
, "--%s\n", option
->long_name
);
141 if (isprint(option
->short_name
)) {
142 fprintf(ofp
, "-%c\n", option
->short_name
);
148 * fls: returns the position of the most significant bit.
149 * Returns 0 if no bit is set, else returns the position of the most
150 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
152 #if defined(__i386) || defined(__x86_64)
153 static inline unsigned int fls_u32(uint32_t x
)
168 #if defined(__x86_64) && defined(__LP64__)
169 static inline unsigned int fls_u64(uint64_t x
)
185 static __attribute__((unused
)) unsigned int fls_u64(uint64_t x
)
192 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
196 if (!(x
& 0xFFFF000000000000ULL
)) {
200 if (!(x
& 0xFF00000000000000ULL
)) {
204 if (!(x
& 0xF000000000000000ULL
)) {
208 if (!(x
& 0xC000000000000000ULL
)) {
212 if (!(x
& 0x8000000000000000ULL
)) {
221 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
227 if (!(x
& 0xFFFF0000U
)) {
231 if (!(x
& 0xFF000000U
)) {
235 if (!(x
& 0xF0000000U
)) {
239 if (!(x
& 0xC0000000U
)) {
243 if (!(x
& 0x80000000U
)) {
251 static unsigned int fls_ulong(unsigned long x
)
253 #if (CAA_BITS_PER_LONG == 32)
261 * Return the minimum order for which x <= (1UL << order).
262 * Return -1 if x is 0.
264 int get_count_order_u32(uint32_t x
)
269 return fls_u32(x
- 1);
273 * Return the minimum order for which x <= (1UL << order).
274 * Return -1 if x is 0.
276 int get_count_order_u64(uint64_t x
)
281 return fls_u64(x
- 1);
285 * Return the minimum order for which x <= (1UL << order).
286 * Return -1 if x is 0.
288 int get_count_order_ulong(unsigned long x
)
293 return fls_ulong(x
- 1);
296 const char *get_event_type_str(enum lttng_event_type type
)
298 const char *str_event_type
;
301 case LTTNG_EVENT_ALL
:
302 str_event_type
= str_all
;
304 case LTTNG_EVENT_TRACEPOINT
:
305 str_event_type
= str_tracepoint
;
307 case LTTNG_EVENT_SYSCALL
:
308 str_event_type
= str_syscall
;
310 case LTTNG_EVENT_PROBE
:
311 str_event_type
= str_probe
;
313 case LTTNG_EVENT_USERSPACE_PROBE
:
314 str_event_type
= str_userspace_probe
;
316 case LTTNG_EVENT_FUNCTION
:
317 str_event_type
= str_function
;
320 /* Should not have an unknown event type or else define it. */
324 return str_event_type
;
328 * Spawn a lttng relayd daemon by forking and execv.
330 int spawn_relayd(const char *pathname
, int port
)
337 port
= DEFAULT_NETWORK_VIEWER_PORT
;
340 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
345 MSG("Spawning a relayd daemon");
349 * Spawn session daemon and tell
350 * it to signal us when ready.
352 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
353 /* execlp only returns if error happened */
354 if (errno
== ENOENT
) {
355 ERR("No relayd found. Use --relayd-path.");
359 kill(getppid(), SIGTERM
); /* wake parent */
361 } else if (pid
> 0) {
374 * Check if relayd is alive.
376 * Return 1 if found else 0 if NOT found. Negative value on error.
381 struct sockaddr_in sin
;
383 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
385 PERROR("socket check relayd");
390 sin
.sin_family
= AF_INET
;
391 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
392 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
394 PERROR("inet_pton check relayd");
400 * A successful connect means the relayd exists thus returning 0 else a
401 * negative value means it does NOT exists.
403 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
408 /* Already spawned. */
414 PERROR("close relayd fd");
420 int print_missing_or_multiple_domains(unsigned int domain_count
, bool include_agent_domains
)
424 if (domain_count
== 0) {
425 ERR("Please specify a domain (--kernel/--userspace%s).",
426 include_agent_domains
? "/--jul/--log4j/--python" : "");
428 } else if (domain_count
> 1) {
429 ERR("Only one domain must be specified.");
437 * Get the discarded events and lost packet counts.
439 void print_session_stats(const char *session_name
)
442 const int ret
= get_session_stats_str(session_name
, &str
);
444 if (ret
>= 0 && str
) {
450 int get_session_stats_str(const char *session_name
, char **out_str
)
452 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
453 struct lttng_domain
*domains
= nullptr;
454 struct lttng_channel
*channels
= nullptr;
455 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
456 struct lttng_session
*sessions
= nullptr;
457 const struct lttng_session
*selected_session
= nullptr;
458 char *stats_str
= nullptr;
459 bool print_discarded_events
= false, print_lost_packets
= false;
461 count
= lttng_list_sessions(&sessions
);
463 ERR("Failed to retrieve session descriptions while printing session statistics.");
468 /* Identify the currently-selected sessions. */
469 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
470 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
471 selected_session
= &sessions
[session_idx
];
475 if (!selected_session
) {
476 ERR("Failed to retrieve session \"%s\" description while printing session statistics.",
482 nb_domains
= lttng_list_domains(session_name
, &domains
);
483 if (nb_domains
< 0) {
487 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
488 struct lttng_handle
*handle
=
489 lttng_create_handle(session_name
, &domains
[domain_idx
]);
492 ERR("Failed to create session handle while printing session statistics.");
499 count
= lttng_list_channels(handle
, &channels
);
500 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
501 uint64_t discarded_events
= 0, lost_packets
= 0;
502 struct lttng_channel
*channel
= &channels
[channel_idx
];
504 ret
= lttng_channel_get_discarded_event_count(channel
, &discarded_events
);
506 ERR("Failed to retrieve discarded event count from channel %s",
510 ret
= lttng_channel_get_lost_packet_count(channel
, &lost_packets
);
512 ERR("Failed to retrieve lost packet count from channel %s",
516 discarded_events_total
+= discarded_events
;
517 lost_packets_total
+= lost_packets
;
519 lttng_destroy_handle(handle
);
522 print_discarded_events
= discarded_events_total
> 0 && !selected_session
->snapshot_mode
;
523 print_lost_packets
= lost_packets_total
> 0 && !selected_session
->snapshot_mode
;
525 if (print_discarded_events
&& print_lost_packets
) {
526 ret
= asprintf(&stats_str
,
527 "Warning: %" PRIu64
" events were discarded and %" PRIu64
528 " packets were lost, please refer to "
529 "the documentation on channel configuration.",
530 discarded_events_total
,
532 } else if (print_discarded_events
) {
533 ret
= asprintf(&stats_str
,
534 "Warning: %" PRIu64
" events were discarded, please refer to "
535 "the documentation on channel configuration.",
536 discarded_events_total
);
537 } else if (print_lost_packets
) {
538 ret
= asprintf(&stats_str
,
539 "Warning: %" PRIu64
" packets were lost, please refer to "
540 "the documentation on channel configuration.",
547 ERR("Failed to format lost packet and discarded events statistics");
549 *out_str
= stats_str
;
559 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
564 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
565 LTTNG_ASSERT(ret
> 0 && ret
< 32);
566 ret
= utils_show_help(1, page_name
, help_msg
);
567 if (ret
&& !help_msg
) {
568 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
575 int print_trace_archive_location(const struct lttng_trace_archive_location
*location
,
576 const char *session_name
)
579 enum lttng_trace_archive_location_type location_type
;
580 enum lttng_trace_archive_location_status status
;
581 bool printed_location
= false;
583 location_type
= lttng_trace_archive_location_get_type(location
);
585 _MSG("Trace chunk archive for session %s is now readable", session_name
);
586 switch (location_type
) {
587 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
589 const char *absolute_path
;
591 status
= lttng_trace_archive_location_local_get_absolute_path(location
,
593 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
597 MSG(" at %s", absolute_path
);
598 printed_location
= true;
601 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
603 uint16_t control_port
, data_port
;
604 const char *host
, *relative_path
, *protocol_str
;
605 enum lttng_trace_archive_location_relay_protocol_type protocol
;
607 /* Fetch all relay location parameters. */
608 status
= lttng_trace_archive_location_relay_get_protocol_type(location
, &protocol
);
609 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
614 status
= lttng_trace_archive_location_relay_get_host(location
, &host
);
615 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
620 status
= lttng_trace_archive_location_relay_get_control_port(location
,
622 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
627 status
= lttng_trace_archive_location_relay_get_data_port(location
, &data_port
);
628 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
633 status
= lttng_trace_archive_location_relay_get_relative_path(location
,
635 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
641 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
642 protocol_str
= "tcp";
645 protocol_str
= "unknown";
649 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %" PRIu16
"]",
655 printed_location
= true;
662 if (!printed_location
) {
663 MSG(" at an unknown location");
669 template <typename FilterFunctionType
>
670 lttng::cli::session_list
get_sessions(const FilterFunctionType
& filter
,
671 bool return_first_match_only
= false)
673 lttng::cli::session_list list
= []() {
675 struct lttng_session
*psessions
;
677 list_ret
= lttng_list_sessions(&psessions
);
680 LTTNG_THROW_CTL("Failed to list sessions",
681 static_cast<lttng_error_code
>(list_ret
));
684 return lttng::cli::session_list(psessions
, list_ret
);
687 std::size_t write_to
= 0;
688 for (std::size_t read_from
= 0; read_from
< list
.size(); ++read_from
) {
689 if (!filter(list
[read_from
])) {
693 if (read_from
!= write_to
) {
694 list
[write_to
] = list
[read_from
];
699 if (return_first_match_only
) {
700 return lttng::cli::session_list(std::move(list
), 1);
704 list
.resize(write_to
);
710 lttng::cli::session_list
lttng::cli::list_sessions(const struct session_spec
& spec
)
712 switch (spec
.type_
) {
713 case lttng::cli::session_spec::type::NAME
:
714 if (spec
.value
== nullptr) {
715 const auto configured_name
=
716 lttng::make_unique_wrapper
<char, lttng::memory::free
>(
719 if (!configured_name
) {
720 LTTNG_THROW_CLI_NO_DEFAULT_SESSION();
723 const struct lttng::cli::session_spec
new_spec(
724 lttng::cli::session_spec::type::NAME
, configured_name
.get());
726 return list_sessions(new_spec
);
730 [&spec
](const lttng_session
& session
) {
731 return strcmp(session
.name
, spec
.value
) == 0;
734 case lttng::cli::session_spec::type::GLOB_PATTERN
:
735 return get_sessions([&spec
](const lttng_session
& session
) {
736 return fnmatch(spec
.value
, session
.name
, 0) == 0;
738 case lttng::cli::session_spec::type::ALL
:
739 return get_sessions([](const lttng_session
&) { return true; });
742 return lttng::cli::session_list();
745 void print_kernel_tracer_status_error()
751 enum lttng_kernel_tracer_status kernel_tracer_status
;
752 const auto ret
= lttng_get_kernel_tracer_status(&kernel_tracer_status
);
755 ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret
));
757 switch (kernel_tracer_status
) {
758 case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED
:
760 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN
:
761 std::cerr
<< "\tKernel module loading failed" << std::endl
;
763 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING
:
764 std::cerr
<< "\tMissing one or more required kernel modules" << std::endl
;
766 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE
:
768 << "\tKernel module signature error prevented loading of one or more required kernel modules"
771 case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT
:
772 std::cerr
<< "\tlttng-sessiond isn't running as root" << std::endl
;
774 case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER
:
775 std::cerr
<< "\tFailed to setup notifiers" << std::endl
;
777 case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG
:
778 std::cerr
<< "\tlttng-sessiond failed to open /proc/lttng" << std::endl
;
780 case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH
:
781 std::cerr
<< "\tVersion mismatch between kernel tracer and kernel tracer ABI"
785 std::cerr
<< lttng::format("\t\tUnknown kernel tracer status (%d)",
786 static_cast<int>(kernel_tracer_status
))
791 std::cerr
<< "\tConsult lttng-sessiond logs for more information" << std::endl
;