2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <sys/types.h>
14 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
21 #include <common/error.h>
22 #include <common/utils.h>
23 #include <common/defaults.h>
29 static const char *str_kernel
= "Kernel";
30 static const char *str_ust
= "UST";
31 static const char *str_jul
= "JUL";
32 static const char *str_log4j
= "LOG4J";
33 static const char *str_python
= "Python";
34 static const char *str_all
= "ALL";
35 static const char *str_tracepoint
= "Tracepoint";
36 static const char *str_syscall
= "Syscall";
37 static const char *str_probe
= "Probe";
38 static const char *str_userspace_probe
= "Userspace Probe";
39 static const char *str_function
= "Function";
42 char *_get_session_name(int quiet
)
45 char *session_name
= NULL
;
47 /* Get path to config file */
48 path
= utils_get_home_dir();
53 /* Get session name from config */
54 session_name
= quiet
? config_read_session_name_quiet(path
) :
55 config_read_session_name(path
);
56 if (session_name
== NULL
) {
60 DBG2("Config file path found: %s", path
);
61 DBG("Session name found: %s", session_name
);
71 * Return allocated string with the session name found in the config
74 char *get_session_name(void)
76 return _get_session_name(0);
80 * get_session_name_quiet (no warnings/errors emitted)
82 * Return allocated string with the session name found in the config
85 char *get_session_name_quiet(void)
87 return _get_session_name(1);
93 * List commands line by line. This is mostly for bash auto completion and to
94 * avoid difficult parsing.
96 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
99 struct cmd_struct
*cmd
= NULL
;
102 while (cmd
->name
!= NULL
) {
103 fprintf(ofp
, "%s\n", cmd
->name
);
112 * Prints a simple list of the options available to a command. This is intended
113 * to be easily parsed for bash completion.
115 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
118 struct poptOption
*option
= NULL
;
120 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
121 option
= &options
[i
];
123 fprintf(ofp
, "--%s\n", option
->longName
);
125 if (isprint(option
->shortName
)) {
126 fprintf(ofp
, "-%c\n", option
->shortName
);
132 * Same as list_cmd_options, but for options specified for argpar.
134 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
137 const struct argpar_opt_descr
*option
= NULL
;
139 for (i
= 0; options
[i
].long_name
!= NULL
; i
++) {
140 option
= &options
[i
];
142 fprintf(ofp
, "--%s\n", option
->long_name
);
144 if (isprint(option
->short_name
)) {
145 fprintf(ofp
, "-%c\n", option
->short_name
);
151 * fls: returns the position of the most significant bit.
152 * Returns 0 if no bit is set, else returns the position of the most
153 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
155 #if defined(__i386) || defined(__x86_64)
157 unsigned int fls_u32(uint32_t x
)
165 : "=r" (r
) : "rm" (x
));
171 #if defined(__x86_64) && defined(__LP64__)
173 unsigned int fls_u64(uint64_t x
)
181 : "=r" (r
) : "rm" (x
));
188 static __attribute__((unused
))
189 unsigned int fls_u64(uint64_t x
)
196 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
200 if (!(x
& 0xFFFF000000000000ULL
)) {
204 if (!(x
& 0xFF00000000000000ULL
)) {
208 if (!(x
& 0xF000000000000000ULL
)) {
212 if (!(x
& 0xC000000000000000ULL
)) {
216 if (!(x
& 0x8000000000000000ULL
)) {
225 static __attribute__((unused
))
226 unsigned int fls_u32(uint32_t x
)
232 if (!(x
& 0xFFFF0000U
)) {
236 if (!(x
& 0xFF000000U
)) {
240 if (!(x
& 0xF0000000U
)) {
244 if (!(x
& 0xC0000000U
)) {
248 if (!(x
& 0x80000000U
)) {
257 unsigned int fls_ulong(unsigned long x
)
259 #if (CAA_BITS_PER_LONG == 32)
267 * Return the minimum order for which x <= (1UL << order).
268 * Return -1 if x is 0.
270 int get_count_order_u32(uint32_t x
)
275 return fls_u32(x
- 1);
279 * Return the minimum order for which x <= (1UL << order).
280 * Return -1 if x is 0.
282 int get_count_order_u64(uint64_t x
)
287 return fls_u64(x
- 1);
291 * Return the minimum order for which x <= (1UL << order).
292 * Return -1 if x is 0.
294 int get_count_order_ulong(unsigned long x
)
299 return fls_ulong(x
- 1);
302 const char *get_domain_str(enum lttng_domain_type domain
)
307 case LTTNG_DOMAIN_KERNEL
:
308 str_dom
= str_kernel
;
310 case LTTNG_DOMAIN_UST
:
313 case LTTNG_DOMAIN_JUL
:
316 case LTTNG_DOMAIN_LOG4J
:
319 case LTTNG_DOMAIN_PYTHON
:
320 str_dom
= str_python
;
323 /* Should not have an unknown domain or else define it. */
330 const char *get_event_type_str(enum lttng_event_type type
)
332 const char *str_event_type
;
335 case LTTNG_EVENT_ALL
:
336 str_event_type
= str_all
;
338 case LTTNG_EVENT_TRACEPOINT
:
339 str_event_type
= str_tracepoint
;
341 case LTTNG_EVENT_SYSCALL
:
342 str_event_type
= str_syscall
;
344 case LTTNG_EVENT_PROBE
:
345 str_event_type
= str_probe
;
347 case LTTNG_EVENT_USERSPACE_PROBE
:
348 str_event_type
= str_userspace_probe
;
350 case LTTNG_EVENT_FUNCTION
:
351 str_event_type
= str_function
;
354 /* Should not have an unknown event type or else define it. */
358 return str_event_type
;
362 * Spawn a lttng relayd daemon by forking and execv.
364 int spawn_relayd(const char *pathname
, int port
)
371 port
= DEFAULT_NETWORK_VIEWER_PORT
;
374 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
379 MSG("Spawning a relayd daemon");
383 * Spawn session daemon and tell
384 * it to signal us when ready.
386 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
387 /* execlp only returns if error happened */
388 if (errno
== ENOENT
) {
389 ERR("No relayd found. Use --relayd-path.");
393 kill(getppid(), SIGTERM
); /* wake parent */
395 } else if (pid
> 0) {
408 * Check if relayd is alive.
410 * Return 1 if found else 0 if NOT found. Negative value on error.
412 int check_relayd(void)
415 struct sockaddr_in sin
;
417 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
419 PERROR("socket check relayd");
424 sin
.sin_family
= AF_INET
;
425 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
426 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
428 PERROR("inet_pton check relayd");
434 * A successful connect means the relayd exists thus returning 0 else a
435 * negative value means it does NOT exists.
437 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
442 /* Already spawned. */
448 PERROR("close relayd fd");
454 int print_missing_or_multiple_domains(unsigned int domain_count
,
455 bool include_agent_domains
)
459 if (domain_count
== 0) {
460 ERR("Please specify a domain (--kernel/--userspace%s).",
461 include_agent_domains
?
462 "/--jul/--log4j/--python" :
465 } else if (domain_count
> 1) {
466 ERR("Only one domain must be specified.");
474 * Get the discarded events and lost packet counts.
476 void print_session_stats(const char *session_name
)
479 const int ret
= get_session_stats_str(session_name
, &str
);
481 if (ret
>= 0 && str
) {
487 int get_session_stats_str(const char *session_name
, char **out_str
)
489 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
490 struct lttng_domain
*domains
;
491 struct lttng_channel
*channels
;
492 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
493 struct lttng_session
*sessions
= NULL
;
494 const struct lttng_session
*selected_session
= NULL
;
495 char *stats_str
= NULL
;
496 bool print_discarded_events
= false, print_lost_packets
= false;
498 count
= lttng_list_sessions(&sessions
);
500 ERR("Failed to retrieve session descriptions while printing session statistics.");
505 /* Identify the currently-selected sessions. */
506 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
507 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
508 selected_session
= &sessions
[session_idx
];
512 if (!selected_session
) {
513 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
518 nb_domains
= lttng_list_domains(session_name
, &domains
);
519 if (nb_domains
< 0) {
523 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
524 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
525 &domains
[domain_idx
]);
528 ERR("Failed to create session handle while printing session statistics.");
533 count
= lttng_list_channels(handle
, &channels
);
534 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
535 uint64_t discarded_events
= 0, lost_packets
= 0;
536 struct lttng_channel
*channel
= &channels
[channel_idx
];
538 ret
= lttng_channel_get_discarded_event_count(channel
,
541 ERR("Failed to retrieve discarded event count from channel %s",
545 ret
= lttng_channel_get_lost_packet_count(channel
,
548 ERR("Failed to retrieve lost packet count from channel %s",
552 discarded_events_total
+= discarded_events
;
553 lost_packets_total
+= lost_packets
;
555 lttng_destroy_handle(handle
);
558 print_discarded_events
= discarded_events_total
> 0 &&
559 !selected_session
->snapshot_mode
;
560 print_lost_packets
= lost_packets_total
> 0 &&
561 !selected_session
->snapshot_mode
;
563 if (print_discarded_events
&& print_lost_packets
) {
564 ret
= asprintf(&stats_str
,
566 " events were discarded and %" PRIu64
567 " packets were lost, please refer to "
568 "the documentation on channel configuration.",
569 discarded_events_total
, lost_packets_total
);
570 } else if (print_discarded_events
) {
571 ret
= asprintf(&stats_str
,
573 " events were discarded, please refer to "
574 "the documentation on channel configuration.",
575 discarded_events_total
);
576 } else if (print_lost_packets
) {
577 ret
= asprintf(&stats_str
,
579 " packets were lost, please refer to "
580 "the documentation on channel configuration.",
587 ERR("Failed to format lost packet and discarded events statistics");
589 *out_str
= stats_str
;
597 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
602 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
603 assert(ret
> 0 && ret
< 32);
604 ret
= utils_show_help(1, page_name
, help_msg
);
605 if (ret
&& !help_msg
) {
606 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
613 int print_trace_archive_location(
614 const struct lttng_trace_archive_location
*location
,
615 const char *session_name
)
618 enum lttng_trace_archive_location_type location_type
;
619 enum lttng_trace_archive_location_status status
;
620 bool printed_location
= false;
622 location_type
= lttng_trace_archive_location_get_type(location
);
624 _MSG("Trace chunk archive for session %s is now readable",
626 switch (location_type
) {
627 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
629 const char *absolute_path
;
631 status
= lttng_trace_archive_location_local_get_absolute_path(
632 location
, &absolute_path
);
633 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
637 MSG(" at %s", absolute_path
);
638 printed_location
= true;
641 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
643 uint16_t control_port
, data_port
;
644 const char *host
, *relative_path
, *protocol_str
;
645 enum lttng_trace_archive_location_relay_protocol_type protocol
;
647 /* Fetch all relay location parameters. */
648 status
= lttng_trace_archive_location_relay_get_protocol_type(
649 location
, &protocol
);
650 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
655 status
= lttng_trace_archive_location_relay_get_host(
657 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
662 status
= lttng_trace_archive_location_relay_get_control_port(
663 location
, &control_port
);
664 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
669 status
= lttng_trace_archive_location_relay_get_data_port(
670 location
, &data_port
);
671 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
676 status
= lttng_trace_archive_location_relay_get_relative_path(
677 location
, &relative_path
);
678 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
684 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
685 protocol_str
= "tcp";
688 protocol_str
= "unknown";
692 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
693 PRIu16
"]", protocol_str
, host
,
694 relative_path
, control_port
, data_port
);
695 printed_location
= true;
702 if (!printed_location
) {
703 MSG(" at an unknown location");