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/utils.hpp>
17 #include <arpa/inet.h>
21 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
28 static const char *str_all
= "ALL";
29 static const char *str_tracepoint
= "Tracepoint";
30 static const char *str_syscall
= "Syscall";
31 static const char *str_probe
= "Probe";
32 static const char *str_userspace_probe
= "Userspace Probe";
33 static const char *str_function
= "Function";
35 static char *_get_session_name(int quiet
)
38 char *session_name
= nullptr;
40 /* Get path to config file */
41 path
= utils_get_home_dir();
42 if (path
== nullptr) {
46 /* Get session name from config */
47 session_name
= quiet
? config_read_session_name_quiet(path
) :
48 config_read_session_name(path
);
49 if (session_name
== nullptr) {
53 DBG2("Config file path found: %s", path
);
54 DBG("Session name found: %s", session_name
);
64 * Return allocated string with the session name found in the config
67 char *get_session_name()
69 return _get_session_name(0);
73 * get_session_name_quiet (no warnings/errors emitted)
75 * Return allocated string with the session name found in the config
78 char *get_session_name_quiet()
80 return _get_session_name(1);
86 * List commands line by line. This is mostly for bash auto completion and to
87 * avoid difficult parsing.
89 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
92 struct cmd_struct
*cmd
= nullptr;
95 while (cmd
->name
!= nullptr) {
96 fprintf(ofp
, "%s\n", cmd
->name
);
105 * Prints a simple list of the options available to a command. This is intended
106 * to be easily parsed for bash completion.
108 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
111 struct poptOption
*option
= nullptr;
113 for (i
= 0; options
[i
].longName
!= nullptr; i
++) {
114 option
= &options
[i
];
116 fprintf(ofp
, "--%s\n", option
->longName
);
118 if (isprint(option
->shortName
)) {
119 fprintf(ofp
, "-%c\n", option
->shortName
);
125 * Same as list_cmd_options, but for options specified for argpar.
127 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
131 for (i
= 0; options
[i
].long_name
!= nullptr; i
++) {
132 const struct argpar_opt_descr
*option
= &options
[i
];
134 fprintf(ofp
, "--%s\n", option
->long_name
);
136 if (isprint(option
->short_name
)) {
137 fprintf(ofp
, "-%c\n", option
->short_name
);
143 * fls: returns the position of the most significant bit.
144 * Returns 0 if no bit is set, else returns the position of the most
145 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
147 #if defined(__i386) || defined(__x86_64)
148 static inline unsigned int fls_u32(uint32_t x
)
163 #if defined(__x86_64) && defined(__LP64__)
164 static inline unsigned int fls_u64(uint64_t x
)
180 static __attribute__((unused
)) unsigned int fls_u64(uint64_t x
)
187 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
191 if (!(x
& 0xFFFF000000000000ULL
)) {
195 if (!(x
& 0xFF00000000000000ULL
)) {
199 if (!(x
& 0xF000000000000000ULL
)) {
203 if (!(x
& 0xC000000000000000ULL
)) {
207 if (!(x
& 0x8000000000000000ULL
)) {
216 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
222 if (!(x
& 0xFFFF0000U
)) {
226 if (!(x
& 0xFF000000U
)) {
230 if (!(x
& 0xF0000000U
)) {
234 if (!(x
& 0xC0000000U
)) {
238 if (!(x
& 0x80000000U
)) {
246 static unsigned int fls_ulong(unsigned long x
)
248 #if (CAA_BITS_PER_LONG == 32)
256 * Return the minimum order for which x <= (1UL << order).
257 * Return -1 if x is 0.
259 int get_count_order_u32(uint32_t x
)
264 return fls_u32(x
- 1);
268 * Return the minimum order for which x <= (1UL << order).
269 * Return -1 if x is 0.
271 int get_count_order_u64(uint64_t x
)
276 return fls_u64(x
- 1);
280 * Return the minimum order for which x <= (1UL << order).
281 * Return -1 if x is 0.
283 int get_count_order_ulong(unsigned long x
)
288 return fls_ulong(x
- 1);
291 const char *get_event_type_str(enum lttng_event_type type
)
293 const char *str_event_type
;
296 case LTTNG_EVENT_ALL
:
297 str_event_type
= str_all
;
299 case LTTNG_EVENT_TRACEPOINT
:
300 str_event_type
= str_tracepoint
;
302 case LTTNG_EVENT_SYSCALL
:
303 str_event_type
= str_syscall
;
305 case LTTNG_EVENT_PROBE
:
306 str_event_type
= str_probe
;
308 case LTTNG_EVENT_USERSPACE_PROBE
:
309 str_event_type
= str_userspace_probe
;
311 case LTTNG_EVENT_FUNCTION
:
312 str_event_type
= str_function
;
315 /* Should not have an unknown event type or else define it. */
319 return str_event_type
;
323 * Spawn a lttng relayd daemon by forking and execv.
325 int spawn_relayd(const char *pathname
, int port
)
332 port
= DEFAULT_NETWORK_VIEWER_PORT
;
335 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
340 MSG("Spawning a relayd daemon");
344 * Spawn session daemon and tell
345 * it to signal us when ready.
347 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
348 /* execlp only returns if error happened */
349 if (errno
== ENOENT
) {
350 ERR("No relayd found. Use --relayd-path.");
354 kill(getppid(), SIGTERM
); /* wake parent */
356 } else if (pid
> 0) {
369 * Check if relayd is alive.
371 * Return 1 if found else 0 if NOT found. Negative value on error.
376 struct sockaddr_in sin
;
378 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
380 PERROR("socket check relayd");
385 sin
.sin_family
= AF_INET
;
386 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
387 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
389 PERROR("inet_pton check relayd");
395 * A successful connect means the relayd exists thus returning 0 else a
396 * negative value means it does NOT exists.
398 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
403 /* Already spawned. */
409 PERROR("close relayd fd");
415 int print_missing_or_multiple_domains(unsigned int domain_count
, bool include_agent_domains
)
419 if (domain_count
== 0) {
420 ERR("Please specify a domain (--kernel/--userspace%s).",
421 include_agent_domains
? "/--jul/--log4j/--python" : "");
423 } else if (domain_count
> 1) {
424 ERR("Only one domain must be specified.");
432 * Get the discarded events and lost packet counts.
434 void print_session_stats(const char *session_name
)
437 const int ret
= get_session_stats_str(session_name
, &str
);
439 if (ret
>= 0 && str
) {
445 int get_session_stats_str(const char *session_name
, char **out_str
)
447 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
448 struct lttng_domain
*domains
= nullptr;
449 struct lttng_channel
*channels
= nullptr;
450 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
451 struct lttng_session
*sessions
= nullptr;
452 const struct lttng_session
*selected_session
= nullptr;
453 char *stats_str
= nullptr;
454 bool print_discarded_events
= false, print_lost_packets
= false;
456 count
= lttng_list_sessions(&sessions
);
458 ERR("Failed to retrieve session descriptions while printing session statistics.");
463 /* Identify the currently-selected sessions. */
464 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
465 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
466 selected_session
= &sessions
[session_idx
];
470 if (!selected_session
) {
471 ERR("Failed to retrieve session \"%s\" description while printing session statistics.",
477 nb_domains
= lttng_list_domains(session_name
, &domains
);
478 if (nb_domains
< 0) {
482 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
483 struct lttng_handle
*handle
=
484 lttng_create_handle(session_name
, &domains
[domain_idx
]);
487 ERR("Failed to create session handle while printing session statistics.");
494 count
= lttng_list_channels(handle
, &channels
);
495 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
496 uint64_t discarded_events
= 0, lost_packets
= 0;
497 struct lttng_channel
*channel
= &channels
[channel_idx
];
499 ret
= lttng_channel_get_discarded_event_count(channel
, &discarded_events
);
501 ERR("Failed to retrieve discarded event count from channel %s",
505 ret
= lttng_channel_get_lost_packet_count(channel
, &lost_packets
);
507 ERR("Failed to retrieve lost packet count from channel %s",
511 discarded_events_total
+= discarded_events
;
512 lost_packets_total
+= lost_packets
;
514 lttng_destroy_handle(handle
);
517 print_discarded_events
= discarded_events_total
> 0 && !selected_session
->snapshot_mode
;
518 print_lost_packets
= lost_packets_total
> 0 && !selected_session
->snapshot_mode
;
520 if (print_discarded_events
&& print_lost_packets
) {
521 ret
= asprintf(&stats_str
,
522 "Warning: %" PRIu64
" events were discarded and %" PRIu64
523 " packets were lost, please refer to "
524 "the documentation on channel configuration.",
525 discarded_events_total
,
527 } else if (print_discarded_events
) {
528 ret
= asprintf(&stats_str
,
529 "Warning: %" PRIu64
" events were discarded, please refer to "
530 "the documentation on channel configuration.",
531 discarded_events_total
);
532 } else if (print_lost_packets
) {
533 ret
= asprintf(&stats_str
,
534 "Warning: %" PRIu64
" packets were lost, please refer to "
535 "the documentation on channel configuration.",
542 ERR("Failed to format lost packet and discarded events statistics");
544 *out_str
= stats_str
;
554 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
559 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
560 LTTNG_ASSERT(ret
> 0 && ret
< 32);
561 ret
= utils_show_help(1, page_name
, help_msg
);
562 if (ret
&& !help_msg
) {
563 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
570 int print_trace_archive_location(const struct lttng_trace_archive_location
*location
,
571 const char *session_name
)
574 enum lttng_trace_archive_location_type location_type
;
575 enum lttng_trace_archive_location_status status
;
576 bool printed_location
= false;
578 location_type
= lttng_trace_archive_location_get_type(location
);
580 _MSG("Trace chunk archive for session %s is now readable", session_name
);
581 switch (location_type
) {
582 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
584 const char *absolute_path
;
586 status
= lttng_trace_archive_location_local_get_absolute_path(location
,
588 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
592 MSG(" at %s", absolute_path
);
593 printed_location
= true;
596 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
598 uint16_t control_port
, data_port
;
599 const char *host
, *relative_path
, *protocol_str
;
600 enum lttng_trace_archive_location_relay_protocol_type protocol
;
602 /* Fetch all relay location parameters. */
603 status
= lttng_trace_archive_location_relay_get_protocol_type(location
, &protocol
);
604 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
609 status
= lttng_trace_archive_location_relay_get_host(location
, &host
);
610 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
615 status
= lttng_trace_archive_location_relay_get_control_port(location
,
617 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
622 status
= lttng_trace_archive_location_relay_get_data_port(location
, &data_port
);
623 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
628 status
= lttng_trace_archive_location_relay_get_relative_path(location
,
630 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
636 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
637 protocol_str
= "tcp";
640 protocol_str
= "unknown";
644 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %" PRIu16
"]",
650 printed_location
= true;
657 if (!printed_location
) {
658 MSG(" at an unknown location");