2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <common/defaults.hpp>
14 #include <common/error.hpp>
15 #include <common/exception.hpp>
16 #include <common/make-unique-wrapper.hpp>
17 #include <common/utils.hpp>
19 #include <arpa/inet.h>
25 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
32 static const char *str_all
= "ALL";
33 static const char *str_tracepoint
= "Tracepoint";
34 static const char *str_syscall
= "Syscall";
35 static const char *str_probe
= "Probe";
36 static const char *str_userspace_probe
= "Userspace Probe";
37 static const char *str_function
= "Function";
39 static char *_get_session_name(int quiet
)
42 char *session_name
= nullptr;
44 /* Get path to config file */
45 path
= utils_get_home_dir();
46 if (path
== nullptr) {
50 /* Get session name from config */
51 session_name
= quiet
? config_read_session_name_quiet(path
) :
52 config_read_session_name(path
);
53 if (session_name
== nullptr) {
57 DBG2("Config file path found: %s", path
);
58 DBG("Session name found: %s", session_name
);
68 * Return allocated string with the session name found in the config
71 char *get_session_name()
73 return _get_session_name(0);
77 * get_session_name_quiet (no warnings/errors emitted)
79 * Return allocated string with the session name found in the config
82 char *get_session_name_quiet()
84 return _get_session_name(1);
90 * List commands line by line. This is mostly for bash auto completion and to
91 * avoid difficult parsing.
93 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
96 struct cmd_struct
*cmd
= nullptr;
99 while (cmd
->name
!= nullptr) {
100 fprintf(ofp
, "%s\n", cmd
->name
);
109 * Prints a simple list of the options available to a command. This is intended
110 * to be easily parsed for bash completion.
112 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
115 struct poptOption
*option
= nullptr;
117 for (i
= 0; options
[i
].longName
!= nullptr; i
++) {
118 option
= &options
[i
];
120 fprintf(ofp
, "--%s\n", option
->longName
);
122 if (isprint(option
->shortName
)) {
123 fprintf(ofp
, "-%c\n", option
->shortName
);
129 * Same as list_cmd_options, but for options specified for argpar.
131 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
135 for (i
= 0; options
[i
].long_name
!= nullptr; i
++) {
136 const struct argpar_opt_descr
*option
= &options
[i
];
138 fprintf(ofp
, "--%s\n", option
->long_name
);
140 if (isprint(option
->short_name
)) {
141 fprintf(ofp
, "-%c\n", option
->short_name
);
147 * fls: returns the position of the most significant bit.
148 * Returns 0 if no bit is set, else returns the position of the most
149 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
151 #if defined(__i386) || defined(__x86_64)
152 static inline unsigned int fls_u32(uint32_t x
)
167 #if defined(__x86_64) && defined(__LP64__)
168 static inline unsigned int fls_u64(uint64_t x
)
184 static __attribute__((unused
)) unsigned int fls_u64(uint64_t x
)
191 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
195 if (!(x
& 0xFFFF000000000000ULL
)) {
199 if (!(x
& 0xFF00000000000000ULL
)) {
203 if (!(x
& 0xF000000000000000ULL
)) {
207 if (!(x
& 0xC000000000000000ULL
)) {
211 if (!(x
& 0x8000000000000000ULL
)) {
220 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
226 if (!(x
& 0xFFFF0000U
)) {
230 if (!(x
& 0xFF000000U
)) {
234 if (!(x
& 0xF0000000U
)) {
238 if (!(x
& 0xC0000000U
)) {
242 if (!(x
& 0x80000000U
)) {
250 static unsigned int fls_ulong(unsigned long x
)
252 #if (CAA_BITS_PER_LONG == 32)
260 * Return the minimum order for which x <= (1UL << order).
261 * Return -1 if x is 0.
263 int get_count_order_u32(uint32_t x
)
268 return fls_u32(x
- 1);
272 * Return the minimum order for which x <= (1UL << order).
273 * Return -1 if x is 0.
275 int get_count_order_u64(uint64_t x
)
280 return fls_u64(x
- 1);
284 * Return the minimum order for which x <= (1UL << order).
285 * Return -1 if x is 0.
287 int get_count_order_ulong(unsigned long x
)
292 return fls_ulong(x
- 1);
295 const char *get_event_type_str(enum lttng_event_type type
)
297 const char *str_event_type
;
300 case LTTNG_EVENT_ALL
:
301 str_event_type
= str_all
;
303 case LTTNG_EVENT_TRACEPOINT
:
304 str_event_type
= str_tracepoint
;
306 case LTTNG_EVENT_SYSCALL
:
307 str_event_type
= str_syscall
;
309 case LTTNG_EVENT_PROBE
:
310 str_event_type
= str_probe
;
312 case LTTNG_EVENT_USERSPACE_PROBE
:
313 str_event_type
= str_userspace_probe
;
315 case LTTNG_EVENT_FUNCTION
:
316 str_event_type
= str_function
;
319 /* Should not have an unknown event type or else define it. */
323 return str_event_type
;
327 * Spawn a lttng relayd daemon by forking and execv.
329 int spawn_relayd(const char *pathname
, int port
)
336 port
= DEFAULT_NETWORK_VIEWER_PORT
;
339 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
344 MSG("Spawning a relayd daemon");
348 * Spawn session daemon and tell
349 * it to signal us when ready.
351 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
352 /* execlp only returns if error happened */
353 if (errno
== ENOENT
) {
354 ERR("No relayd found. Use --relayd-path.");
358 kill(getppid(), SIGTERM
); /* wake parent */
360 } else if (pid
> 0) {
373 * Check if relayd is alive.
375 * Return 1 if found else 0 if NOT found. Negative value on error.
380 struct sockaddr_in sin
;
382 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
384 PERROR("socket check relayd");
389 sin
.sin_family
= AF_INET
;
390 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
391 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
393 PERROR("inet_pton check relayd");
399 * A successful connect means the relayd exists thus returning 0 else a
400 * negative value means it does NOT exists.
402 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
407 /* Already spawned. */
413 PERROR("close relayd fd");
419 int print_missing_or_multiple_domains(unsigned int domain_count
, bool include_agent_domains
)
423 if (domain_count
== 0) {
424 ERR("Please specify a domain (--kernel/--userspace%s).",
425 include_agent_domains
? "/--jul/--log4j/--python" : "");
427 } else if (domain_count
> 1) {
428 ERR("Only one domain must be specified.");
436 * Get the discarded events and lost packet counts.
438 void print_session_stats(const char *session_name
)
441 const int ret
= get_session_stats_str(session_name
, &str
);
443 if (ret
>= 0 && str
) {
449 int get_session_stats_str(const char *session_name
, char **out_str
)
451 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
452 struct lttng_domain
*domains
= nullptr;
453 struct lttng_channel
*channels
= nullptr;
454 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
455 struct lttng_session
*sessions
= nullptr;
456 const struct lttng_session
*selected_session
= nullptr;
457 char *stats_str
= nullptr;
458 bool print_discarded_events
= false, print_lost_packets
= false;
460 count
= lttng_list_sessions(&sessions
);
462 ERR("Failed to retrieve session descriptions while printing session statistics.");
467 /* Identify the currently-selected sessions. */
468 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
469 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
470 selected_session
= &sessions
[session_idx
];
474 if (!selected_session
) {
475 ERR("Failed to retrieve session \"%s\" description while printing session statistics.",
481 nb_domains
= lttng_list_domains(session_name
, &domains
);
482 if (nb_domains
< 0) {
486 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
487 struct lttng_handle
*handle
=
488 lttng_create_handle(session_name
, &domains
[domain_idx
]);
491 ERR("Failed to create session handle while printing session statistics.");
498 count
= lttng_list_channels(handle
, &channels
);
499 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
500 uint64_t discarded_events
= 0, lost_packets
= 0;
501 struct lttng_channel
*channel
= &channels
[channel_idx
];
503 ret
= lttng_channel_get_discarded_event_count(channel
, &discarded_events
);
505 ERR("Failed to retrieve discarded event count from channel %s",
509 ret
= lttng_channel_get_lost_packet_count(channel
, &lost_packets
);
511 ERR("Failed to retrieve lost packet count from channel %s",
515 discarded_events_total
+= discarded_events
;
516 lost_packets_total
+= lost_packets
;
518 lttng_destroy_handle(handle
);
521 print_discarded_events
= discarded_events_total
> 0 && !selected_session
->snapshot_mode
;
522 print_lost_packets
= lost_packets_total
> 0 && !selected_session
->snapshot_mode
;
524 if (print_discarded_events
&& print_lost_packets
) {
525 ret
= asprintf(&stats_str
,
526 "Warning: %" PRIu64
" events were discarded and %" PRIu64
527 " packets were lost, please refer to "
528 "the documentation on channel configuration.",
529 discarded_events_total
,
531 } else if (print_discarded_events
) {
532 ret
= asprintf(&stats_str
,
533 "Warning: %" PRIu64
" events were discarded, please refer to "
534 "the documentation on channel configuration.",
535 discarded_events_total
);
536 } else if (print_lost_packets
) {
537 ret
= asprintf(&stats_str
,
538 "Warning: %" PRIu64
" packets were lost, please refer to "
539 "the documentation on channel configuration.",
546 ERR("Failed to format lost packet and discarded events statistics");
548 *out_str
= stats_str
;
558 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
563 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
564 LTTNG_ASSERT(ret
> 0 && ret
< 32);
565 ret
= utils_show_help(1, page_name
, help_msg
);
566 if (ret
&& !help_msg
) {
567 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
574 int print_trace_archive_location(const struct lttng_trace_archive_location
*location
,
575 const char *session_name
)
578 enum lttng_trace_archive_location_type location_type
;
579 enum lttng_trace_archive_location_status status
;
580 bool printed_location
= false;
582 location_type
= lttng_trace_archive_location_get_type(location
);
584 _MSG("Trace chunk archive for session %s is now readable", session_name
);
585 switch (location_type
) {
586 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
588 const char *absolute_path
;
590 status
= lttng_trace_archive_location_local_get_absolute_path(location
,
592 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
596 MSG(" at %s", absolute_path
);
597 printed_location
= true;
600 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
602 uint16_t control_port
, data_port
;
603 const char *host
, *relative_path
, *protocol_str
;
604 enum lttng_trace_archive_location_relay_protocol_type protocol
;
606 /* Fetch all relay location parameters. */
607 status
= lttng_trace_archive_location_relay_get_protocol_type(location
, &protocol
);
608 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
613 status
= lttng_trace_archive_location_relay_get_host(location
, &host
);
614 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
619 status
= lttng_trace_archive_location_relay_get_control_port(location
,
621 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
626 status
= lttng_trace_archive_location_relay_get_data_port(location
, &data_port
);
627 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
632 status
= lttng_trace_archive_location_relay_get_relative_path(location
,
634 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
640 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
641 protocol_str
= "tcp";
644 protocol_str
= "unknown";
648 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %" PRIu16
"]",
654 printed_location
= true;
661 if (!printed_location
) {
662 MSG(" at an unknown location");
668 template <typename FilterFunctionType
>
669 lttng::cli::session_list
get_sessions(const FilterFunctionType
& filter
,
670 bool return_first_match_only
= false)
672 lttng::cli::session_list list
= []() {
674 struct lttng_session
*psessions
;
676 list_ret
= lttng_list_sessions(&psessions
);
679 LTTNG_THROW_CTL("Failed to list sessions",
680 static_cast<lttng_error_code
>(list_ret
));
683 return lttng::cli::session_list(psessions
, list_ret
);
686 std::size_t write_to
= 0;
687 for (std::size_t read_from
= 0; read_from
< list
.size(); ++read_from
) {
688 if (!filter(list
[read_from
])) {
692 if (read_from
!= write_to
) {
693 list
[write_to
] = list
[read_from
];
698 if (return_first_match_only
) {
699 return lttng::cli::session_list(std::move(list
), 1);
703 list
.resize(write_to
);
709 lttng::cli::session_list
lttng::cli::list_sessions(const struct session_spec
& spec
)
711 switch (spec
.type_
) {
712 case lttng::cli::session_spec::type::NAME
:
713 if (spec
.value
== nullptr) {
714 const auto configured_name
=
715 lttng::make_unique_wrapper
<char, lttng::free
>(get_session_name());
717 if (configured_name
) {
718 const struct lttng::cli::session_spec
new_spec(
719 lttng::cli::session_spec::type::NAME
,
720 configured_name
.get());
722 return list_sessions(new_spec
);
725 return lttng::cli::session_list();
729 [&spec
](const lttng_session
& session
) {
730 return strcmp(session
.name
, spec
.value
) == 0;
733 case lttng::cli::session_spec::type::GLOB_PATTERN
:
734 return get_sessions([&spec
](const lttng_session
& session
) {
735 return fnmatch(spec
.value
, session
.name
, 0) == 0;
737 case lttng::cli::session_spec::type::ALL
:
738 return get_sessions([](const lttng_session
&) { return true; });
741 return lttng::cli::session_list();
744 void print_kernel_tracer_status_error()
750 enum lttng_kernel_tracer_status kernel_tracer_status
;
751 const auto ret
= lttng_get_kernel_tracer_status(&kernel_tracer_status
);
754 ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret
));
756 switch (kernel_tracer_status
) {
757 case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED
:
759 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN
:
760 std::cerr
<< "\tKernel module loading failed" << std::endl
;
762 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING
:
763 std::cerr
<< "\tMissing one or more required kernel modules" << std::endl
;
765 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE
:
767 << "\tKernel module signature error prevented loading of one or more required kernel modules"
770 case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT
:
771 std::cerr
<< "\tlttng-sessiond isn't running as root" << std::endl
;
773 case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER
:
774 std::cerr
<< "\tFailed to setup notifiers" << std::endl
;
776 case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG
:
777 std::cerr
<< "\tlttng-sessiond failed to open /proc/lttng" << std::endl
;
779 case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH
:
781 << "\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
;