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
)
138 for (i
= 0; options
[i
].long_name
!= NULL
; i
++) {
139 const struct argpar_opt_descr
*option
= &options
[i
];
141 fprintf(ofp
, "--%s\n", option
->long_name
);
143 if (isprint(option
->short_name
)) {
144 fprintf(ofp
, "-%c\n", option
->short_name
);
150 * fls: returns the position of the most significant bit.
151 * Returns 0 if no bit is set, else returns the position of the most
152 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
154 #if defined(__i386) || defined(__x86_64)
156 unsigned int fls_u32(uint32_t x
)
164 : "=r" (r
) : "rm" (x
));
170 #if defined(__x86_64) && defined(__LP64__)
172 unsigned int fls_u64(uint64_t x
)
180 : "=r" (r
) : "rm" (x
));
187 static __attribute__((unused
))
188 unsigned int fls_u64(uint64_t x
)
195 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
199 if (!(x
& 0xFFFF000000000000ULL
)) {
203 if (!(x
& 0xFF00000000000000ULL
)) {
207 if (!(x
& 0xF000000000000000ULL
)) {
211 if (!(x
& 0xC000000000000000ULL
)) {
215 if (!(x
& 0x8000000000000000ULL
)) {
224 static __attribute__((unused
))
225 unsigned int fls_u32(uint32_t x
)
231 if (!(x
& 0xFFFF0000U
)) {
235 if (!(x
& 0xFF000000U
)) {
239 if (!(x
& 0xF0000000U
)) {
243 if (!(x
& 0xC0000000U
)) {
247 if (!(x
& 0x80000000U
)) {
256 unsigned int fls_ulong(unsigned long x
)
258 #if (CAA_BITS_PER_LONG == 32)
266 * Return the minimum order for which x <= (1UL << order).
267 * Return -1 if x is 0.
269 int get_count_order_u32(uint32_t x
)
274 return fls_u32(x
- 1);
278 * Return the minimum order for which x <= (1UL << order).
279 * Return -1 if x is 0.
281 int get_count_order_u64(uint64_t x
)
286 return fls_u64(x
- 1);
290 * Return the minimum order for which x <= (1UL << order).
291 * Return -1 if x is 0.
293 int get_count_order_ulong(unsigned long x
)
298 return fls_ulong(x
- 1);
301 const char *get_domain_str(enum lttng_domain_type domain
)
306 case LTTNG_DOMAIN_KERNEL
:
307 str_dom
= str_kernel
;
309 case LTTNG_DOMAIN_UST
:
312 case LTTNG_DOMAIN_JUL
:
315 case LTTNG_DOMAIN_LOG4J
:
318 case LTTNG_DOMAIN_PYTHON
:
319 str_dom
= str_python
;
322 /* Should not have an unknown domain or else define it. */
329 const char *get_event_type_str(enum lttng_event_type type
)
331 const char *str_event_type
;
334 case LTTNG_EVENT_ALL
:
335 str_event_type
= str_all
;
337 case LTTNG_EVENT_TRACEPOINT
:
338 str_event_type
= str_tracepoint
;
340 case LTTNG_EVENT_SYSCALL
:
341 str_event_type
= str_syscall
;
343 case LTTNG_EVENT_PROBE
:
344 str_event_type
= str_probe
;
346 case LTTNG_EVENT_USERSPACE_PROBE
:
347 str_event_type
= str_userspace_probe
;
349 case LTTNG_EVENT_FUNCTION
:
350 str_event_type
= str_function
;
353 /* Should not have an unknown event type or else define it. */
357 return str_event_type
;
361 * Spawn a lttng relayd daemon by forking and execv.
363 int spawn_relayd(const char *pathname
, int port
)
370 port
= DEFAULT_NETWORK_VIEWER_PORT
;
373 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
378 MSG("Spawning a relayd daemon");
382 * Spawn session daemon and tell
383 * it to signal us when ready.
385 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
386 /* execlp only returns if error happened */
387 if (errno
== ENOENT
) {
388 ERR("No relayd found. Use --relayd-path.");
392 kill(getppid(), SIGTERM
); /* wake parent */
394 } else if (pid
> 0) {
407 * Check if relayd is alive.
409 * Return 1 if found else 0 if NOT found. Negative value on error.
411 int check_relayd(void)
414 struct sockaddr_in sin
;
416 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
418 PERROR("socket check relayd");
423 sin
.sin_family
= AF_INET
;
424 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
425 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
427 PERROR("inet_pton check relayd");
433 * A successful connect means the relayd exists thus returning 0 else a
434 * negative value means it does NOT exists.
436 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
441 /* Already spawned. */
447 PERROR("close relayd fd");
453 int print_missing_or_multiple_domains(unsigned int domain_count
,
454 bool include_agent_domains
)
458 if (domain_count
== 0) {
459 ERR("Please specify a domain (--kernel/--userspace%s).",
460 include_agent_domains
?
461 "/--jul/--log4j/--python" :
464 } else if (domain_count
> 1) {
465 ERR("Only one domain must be specified.");
473 * Get the discarded events and lost packet counts.
475 void print_session_stats(const char *session_name
)
478 const int ret
= get_session_stats_str(session_name
, &str
);
480 if (ret
>= 0 && str
) {
486 int get_session_stats_str(const char *session_name
, char **out_str
)
488 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
489 struct lttng_domain
*domains
;
490 struct lttng_channel
*channels
;
491 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
492 struct lttng_session
*sessions
= NULL
;
493 const struct lttng_session
*selected_session
= NULL
;
494 char *stats_str
= NULL
;
495 bool print_discarded_events
= false, print_lost_packets
= false;
497 count
= lttng_list_sessions(&sessions
);
499 ERR("Failed to retrieve session descriptions while printing session statistics.");
504 /* Identify the currently-selected sessions. */
505 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
506 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
507 selected_session
= &sessions
[session_idx
];
511 if (!selected_session
) {
512 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
517 nb_domains
= lttng_list_domains(session_name
, &domains
);
518 if (nb_domains
< 0) {
522 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
523 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
524 &domains
[domain_idx
]);
527 ERR("Failed to create session handle while printing session statistics.");
532 count
= lttng_list_channels(handle
, &channels
);
533 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
534 uint64_t discarded_events
= 0, lost_packets
= 0;
535 struct lttng_channel
*channel
= &channels
[channel_idx
];
537 ret
= lttng_channel_get_discarded_event_count(channel
,
540 ERR("Failed to retrieve discarded event count from channel %s",
544 ret
= lttng_channel_get_lost_packet_count(channel
,
547 ERR("Failed to retrieve lost packet count from channel %s",
551 discarded_events_total
+= discarded_events
;
552 lost_packets_total
+= lost_packets
;
554 lttng_destroy_handle(handle
);
557 print_discarded_events
= discarded_events_total
> 0 &&
558 !selected_session
->snapshot_mode
;
559 print_lost_packets
= lost_packets_total
> 0 &&
560 !selected_session
->snapshot_mode
;
562 if (print_discarded_events
&& print_lost_packets
) {
563 ret
= asprintf(&stats_str
,
565 " events were discarded and %" PRIu64
566 " packets were lost, please refer to "
567 "the documentation on channel configuration.",
568 discarded_events_total
, lost_packets_total
);
569 } else if (print_discarded_events
) {
570 ret
= asprintf(&stats_str
,
572 " events were discarded, please refer to "
573 "the documentation on channel configuration.",
574 discarded_events_total
);
575 } else if (print_lost_packets
) {
576 ret
= asprintf(&stats_str
,
578 " packets were lost, please refer to "
579 "the documentation on channel configuration.",
586 ERR("Failed to format lost packet and discarded events statistics");
588 *out_str
= stats_str
;
596 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
601 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
602 assert(ret
> 0 && ret
< 32);
603 ret
= utils_show_help(1, page_name
, help_msg
);
604 if (ret
&& !help_msg
) {
605 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
612 int print_trace_archive_location(
613 const struct lttng_trace_archive_location
*location
,
614 const char *session_name
)
617 enum lttng_trace_archive_location_type location_type
;
618 enum lttng_trace_archive_location_status status
;
619 bool printed_location
= false;
621 location_type
= lttng_trace_archive_location_get_type(location
);
623 _MSG("Trace chunk archive for session %s is now readable",
625 switch (location_type
) {
626 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
628 const char *absolute_path
;
630 status
= lttng_trace_archive_location_local_get_absolute_path(
631 location
, &absolute_path
);
632 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
636 MSG(" at %s", absolute_path
);
637 printed_location
= true;
640 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
642 uint16_t control_port
, data_port
;
643 const char *host
, *relative_path
, *protocol_str
;
644 enum lttng_trace_archive_location_relay_protocol_type protocol
;
646 /* Fetch all relay location parameters. */
647 status
= lttng_trace_archive_location_relay_get_protocol_type(
648 location
, &protocol
);
649 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
654 status
= lttng_trace_archive_location_relay_get_host(
656 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
661 status
= lttng_trace_archive_location_relay_get_control_port(
662 location
, &control_port
);
663 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
668 status
= lttng_trace_archive_location_relay_get_data_port(
669 location
, &data_port
);
670 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
675 status
= lttng_trace_archive_location_relay_get_relative_path(
676 location
, &relative_path
);
677 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
683 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
684 protocol_str
= "tcp";
687 protocol_str
= "unknown";
691 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
692 PRIu16
"]", protocol_str
, host
,
693 relative_path
, control_port
, data_port
);
694 printed_location
= true;
701 if (!printed_location
) {
702 MSG(" at an unknown location");