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>
24 #include <netinet/in.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
31 static const char *str_all
= "ALL";
32 static const char *str_tracepoint
= "Tracepoint";
33 static const char *str_syscall
= "Syscall";
34 static const char *str_probe
= "Probe";
35 static const char *str_userspace_probe
= "Userspace Probe";
36 static const char *str_function
= "Function";
38 static char *_get_session_name(int quiet
)
41 char *session_name
= nullptr;
43 /* Get path to config file */
44 path
= utils_get_home_dir();
45 if (path
== nullptr) {
49 /* Get session name from config */
50 session_name
= quiet
? config_read_session_name_quiet(path
) :
51 config_read_session_name(path
);
52 if (session_name
== nullptr) {
56 DBG2("Config file path found: %s", path
);
57 DBG("Session name found: %s", session_name
);
67 * Return allocated string with the session name found in the config
70 char *get_session_name()
72 return _get_session_name(0);
76 * get_session_name_quiet (no warnings/errors emitted)
78 * Return allocated string with the session name found in the config
81 char *get_session_name_quiet()
83 return _get_session_name(1);
89 * List commands line by line. This is mostly for bash auto completion and to
90 * avoid difficult parsing.
92 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
95 struct cmd_struct
*cmd
= nullptr;
98 while (cmd
->name
!= nullptr) {
99 fprintf(ofp
, "%s\n", cmd
->name
);
108 * Prints a simple list of the options available to a command. This is intended
109 * to be easily parsed for bash completion.
111 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
114 struct poptOption
*option
= nullptr;
116 for (i
= 0; options
[i
].longName
!= nullptr; i
++) {
117 option
= &options
[i
];
119 fprintf(ofp
, "--%s\n", option
->longName
);
121 if (isprint(option
->shortName
)) {
122 fprintf(ofp
, "-%c\n", option
->shortName
);
128 * Same as list_cmd_options, but for options specified for argpar.
130 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
134 for (i
= 0; options
[i
].long_name
!= nullptr; i
++) {
135 const struct argpar_opt_descr
*option
= &options
[i
];
137 fprintf(ofp
, "--%s\n", option
->long_name
);
139 if (isprint(option
->short_name
)) {
140 fprintf(ofp
, "-%c\n", option
->short_name
);
146 * fls: returns the position of the most significant bit.
147 * Returns 0 if no bit is set, else returns the position of the most
148 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
150 #if defined(__i386) || defined(__x86_64)
151 static inline unsigned int fls_u32(uint32_t x
)
166 #if defined(__x86_64) && defined(__LP64__)
167 static inline unsigned int fls_u64(uint64_t x
)
183 static __attribute__((unused
)) unsigned int fls_u64(uint64_t x
)
190 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
194 if (!(x
& 0xFFFF000000000000ULL
)) {
198 if (!(x
& 0xFF00000000000000ULL
)) {
202 if (!(x
& 0xF000000000000000ULL
)) {
206 if (!(x
& 0xC000000000000000ULL
)) {
210 if (!(x
& 0x8000000000000000ULL
)) {
219 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
225 if (!(x
& 0xFFFF0000U
)) {
229 if (!(x
& 0xFF000000U
)) {
233 if (!(x
& 0xF0000000U
)) {
237 if (!(x
& 0xC0000000U
)) {
241 if (!(x
& 0x80000000U
)) {
249 static unsigned int fls_ulong(unsigned long x
)
251 #if (CAA_BITS_PER_LONG == 32)
259 * Return the minimum order for which x <= (1UL << order).
260 * Return -1 if x is 0.
262 int get_count_order_u32(uint32_t x
)
267 return fls_u32(x
- 1);
271 * Return the minimum order for which x <= (1UL << order).
272 * Return -1 if x is 0.
274 int get_count_order_u64(uint64_t x
)
279 return fls_u64(x
- 1);
283 * Return the minimum order for which x <= (1UL << order).
284 * Return -1 if x is 0.
286 int get_count_order_ulong(unsigned long x
)
291 return fls_ulong(x
- 1);
294 const char *get_event_type_str(enum lttng_event_type type
)
296 const char *str_event_type
;
299 case LTTNG_EVENT_ALL
:
300 str_event_type
= str_all
;
302 case LTTNG_EVENT_TRACEPOINT
:
303 str_event_type
= str_tracepoint
;
305 case LTTNG_EVENT_SYSCALL
:
306 str_event_type
= str_syscall
;
308 case LTTNG_EVENT_PROBE
:
309 str_event_type
= str_probe
;
311 case LTTNG_EVENT_USERSPACE_PROBE
:
312 str_event_type
= str_userspace_probe
;
314 case LTTNG_EVENT_FUNCTION
:
315 str_event_type
= str_function
;
318 /* Should not have an unknown event type or else define it. */
322 return str_event_type
;
326 * Spawn a lttng relayd daemon by forking and execv.
328 int spawn_relayd(const char *pathname
, int port
)
335 port
= DEFAULT_NETWORK_VIEWER_PORT
;
338 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
343 MSG("Spawning a relayd daemon");
347 * Spawn session daemon and tell
348 * it to signal us when ready.
350 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
351 /* execlp only returns if error happened */
352 if (errno
== ENOENT
) {
353 ERR("No relayd found. Use --relayd-path.");
357 kill(getppid(), SIGTERM
); /* wake parent */
359 } else if (pid
> 0) {
372 * Check if relayd is alive.
374 * Return 1 if found else 0 if NOT found. Negative value on error.
379 struct sockaddr_in sin
;
381 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
383 PERROR("socket check relayd");
388 sin
.sin_family
= AF_INET
;
389 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
390 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
392 PERROR("inet_pton check relayd");
398 * A successful connect means the relayd exists thus returning 0 else a
399 * negative value means it does NOT exists.
401 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
406 /* Already spawned. */
412 PERROR("close relayd fd");
418 int print_missing_or_multiple_domains(unsigned int domain_count
, bool include_agent_domains
)
422 if (domain_count
== 0) {
423 ERR("Please specify a domain (--kernel/--userspace%s).",
424 include_agent_domains
? "/--jul/--log4j/--python" : "");
426 } else if (domain_count
> 1) {
427 ERR("Only one domain must be specified.");
435 * Get the discarded events and lost packet counts.
437 void print_session_stats(const char *session_name
)
440 const int ret
= get_session_stats_str(session_name
, &str
);
442 if (ret
>= 0 && str
) {
448 int get_session_stats_str(const char *session_name
, char **out_str
)
450 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
451 struct lttng_domain
*domains
= nullptr;
452 struct lttng_channel
*channels
= nullptr;
453 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
454 struct lttng_session
*sessions
= nullptr;
455 const struct lttng_session
*selected_session
= nullptr;
456 char *stats_str
= nullptr;
457 bool print_discarded_events
= false, print_lost_packets
= false;
459 count
= lttng_list_sessions(&sessions
);
461 ERR("Failed to retrieve session descriptions while printing session statistics.");
466 /* Identify the currently-selected sessions. */
467 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
468 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
469 selected_session
= &sessions
[session_idx
];
473 if (!selected_session
) {
474 ERR("Failed to retrieve session \"%s\" description while printing session statistics.",
480 nb_domains
= lttng_list_domains(session_name
, &domains
);
481 if (nb_domains
< 0) {
485 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
486 struct lttng_handle
*handle
=
487 lttng_create_handle(session_name
, &domains
[domain_idx
]);
490 ERR("Failed to create session handle while printing session statistics.");
497 count
= lttng_list_channels(handle
, &channels
);
498 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
499 uint64_t discarded_events
= 0, lost_packets
= 0;
500 struct lttng_channel
*channel
= &channels
[channel_idx
];
502 ret
= lttng_channel_get_discarded_event_count(channel
, &discarded_events
);
504 ERR("Failed to retrieve discarded event count from channel %s",
508 ret
= lttng_channel_get_lost_packet_count(channel
, &lost_packets
);
510 ERR("Failed to retrieve lost packet count from channel %s",
514 discarded_events_total
+= discarded_events
;
515 lost_packets_total
+= lost_packets
;
517 lttng_destroy_handle(handle
);
520 print_discarded_events
= discarded_events_total
> 0 && !selected_session
->snapshot_mode
;
521 print_lost_packets
= lost_packets_total
> 0 && !selected_session
->snapshot_mode
;
523 if (print_discarded_events
&& print_lost_packets
) {
524 ret
= asprintf(&stats_str
,
525 "Warning: %" PRIu64
" events were discarded and %" PRIu64
526 " packets were lost, please refer to "
527 "the documentation on channel configuration.",
528 discarded_events_total
,
530 } else if (print_discarded_events
) {
531 ret
= asprintf(&stats_str
,
532 "Warning: %" PRIu64
" events were discarded, please refer to "
533 "the documentation on channel configuration.",
534 discarded_events_total
);
535 } else if (print_lost_packets
) {
536 ret
= asprintf(&stats_str
,
537 "Warning: %" PRIu64
" packets were lost, please refer to "
538 "the documentation on channel configuration.",
545 ERR("Failed to format lost packet and discarded events statistics");
547 *out_str
= stats_str
;
557 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
562 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
563 LTTNG_ASSERT(ret
> 0 && ret
< 32);
564 ret
= utils_show_help(1, page_name
, help_msg
);
565 if (ret
&& !help_msg
) {
566 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
573 int print_trace_archive_location(const struct lttng_trace_archive_location
*location
,
574 const char *session_name
)
577 enum lttng_trace_archive_location_type location_type
;
578 enum lttng_trace_archive_location_status status
;
579 bool printed_location
= false;
581 location_type
= lttng_trace_archive_location_get_type(location
);
583 _MSG("Trace chunk archive for session %s is now readable", session_name
);
584 switch (location_type
) {
585 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
587 const char *absolute_path
;
589 status
= lttng_trace_archive_location_local_get_absolute_path(location
,
591 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
595 MSG(" at %s", absolute_path
);
596 printed_location
= true;
599 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
601 uint16_t control_port
, data_port
;
602 const char *host
, *relative_path
, *protocol_str
;
603 enum lttng_trace_archive_location_relay_protocol_type protocol
;
605 /* Fetch all relay location parameters. */
606 status
= lttng_trace_archive_location_relay_get_protocol_type(location
, &protocol
);
607 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
612 status
= lttng_trace_archive_location_relay_get_host(location
, &host
);
613 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
618 status
= lttng_trace_archive_location_relay_get_control_port(location
,
620 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
625 status
= lttng_trace_archive_location_relay_get_data_port(location
, &data_port
);
626 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
631 status
= lttng_trace_archive_location_relay_get_relative_path(location
,
633 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
639 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
640 protocol_str
= "tcp";
643 protocol_str
= "unknown";
647 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %" PRIu16
"]",
653 printed_location
= true;
660 if (!printed_location
) {
661 MSG(" at an unknown location");
667 template <typename FilterFunctionType
>
668 session_list
get_sessions(const FilterFunctionType
& filter
, bool return_first_match_only
= false)
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 list
= 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 session_list(std::move(list
), 1);
703 list
.resize(write_to
);
709 session_list
list_sessions(const struct session_spec
& spec
)
711 switch (spec
.type_
) {
712 case 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 session_spec
new_spec(session_spec::type::NAME
,
719 configured_name
.get());
721 return list_sessions(new_spec
);
724 return session_list();
728 [&spec
](const lttng_session
& session
) {
729 return strcmp(session
.name
, spec
.value
) == 0;
732 case session_spec::type::GLOB_PATTERN
:
733 return get_sessions([&spec
](const lttng_session
& session
) {
734 return fnmatch(spec
.value
, session
.name
, 0) == 0;
736 case session_spec::type::ALL
:
737 return get_sessions([](const lttng_session
&) { return true; });
740 return session_list();