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 * fls: returns the position of the most significant bit.
133 * Returns 0 if no bit is set, else returns the position of the most
134 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
136 #if defined(__i386) || defined(__x86_64)
138 unsigned int fls_u32(uint32_t x
)
146 : "=r" (r
) : "rm" (x
));
152 #if defined(__x86_64) && defined(__LP64__)
154 unsigned int fls_u64(uint64_t x
)
162 : "=r" (r
) : "rm" (x
));
169 static __attribute__((unused
))
170 unsigned int fls_u64(uint64_t x
)
177 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
181 if (!(x
& 0xFFFF000000000000ULL
)) {
185 if (!(x
& 0xFF00000000000000ULL
)) {
189 if (!(x
& 0xF000000000000000ULL
)) {
193 if (!(x
& 0xC000000000000000ULL
)) {
197 if (!(x
& 0x8000000000000000ULL
)) {
206 static __attribute__((unused
))
207 unsigned int fls_u32(uint32_t x
)
213 if (!(x
& 0xFFFF0000U
)) {
217 if (!(x
& 0xFF000000U
)) {
221 if (!(x
& 0xF0000000U
)) {
225 if (!(x
& 0xC0000000U
)) {
229 if (!(x
& 0x80000000U
)) {
238 unsigned int fls_ulong(unsigned long x
)
240 #if (CAA_BITS_PER_LONG == 32)
248 * Return the minimum order for which x <= (1UL << order).
249 * Return -1 if x is 0.
251 int get_count_order_u32(uint32_t x
)
256 return fls_u32(x
- 1);
260 * Return the minimum order for which x <= (1UL << order).
261 * Return -1 if x is 0.
263 int get_count_order_u64(uint64_t x
)
268 return fls_u64(x
- 1);
272 * Return the minimum order for which x <= (1UL << order).
273 * Return -1 if x is 0.
275 int get_count_order_ulong(unsigned long x
)
280 return fls_ulong(x
- 1);
283 const char *get_domain_str(enum lttng_domain_type domain
)
288 case LTTNG_DOMAIN_KERNEL
:
289 str_dom
= str_kernel
;
291 case LTTNG_DOMAIN_UST
:
294 case LTTNG_DOMAIN_JUL
:
297 case LTTNG_DOMAIN_LOG4J
:
300 case LTTNG_DOMAIN_PYTHON
:
301 str_dom
= str_python
;
304 /* Should not have an unknown domain or else define it. */
311 const char *get_event_type_str(enum lttng_event_type type
)
313 const char *str_event_type
;
316 case LTTNG_EVENT_ALL
:
317 str_event_type
= str_all
;
319 case LTTNG_EVENT_TRACEPOINT
:
320 str_event_type
= str_tracepoint
;
322 case LTTNG_EVENT_SYSCALL
:
323 str_event_type
= str_syscall
;
325 case LTTNG_EVENT_PROBE
:
326 str_event_type
= str_probe
;
328 case LTTNG_EVENT_USERSPACE_PROBE
:
329 str_event_type
= str_userspace_probe
;
331 case LTTNG_EVENT_FUNCTION
:
332 str_event_type
= str_function
;
335 /* Should not have an unknown event type or else define it. */
339 return str_event_type
;
343 * Spawn a lttng relayd daemon by forking and execv.
345 int spawn_relayd(const char *pathname
, int port
)
352 port
= DEFAULT_NETWORK_VIEWER_PORT
;
355 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
360 MSG("Spawning a relayd daemon");
364 * Spawn session daemon and tell
365 * it to signal us when ready.
367 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
368 /* execlp only returns if error happened */
369 if (errno
== ENOENT
) {
370 ERR("No relayd found. Use --relayd-path.");
374 kill(getppid(), SIGTERM
); /* wake parent */
376 } else if (pid
> 0) {
389 * Check if relayd is alive.
391 * Return 1 if found else 0 if NOT found. Negative value on error.
393 int check_relayd(void)
396 struct sockaddr_in sin
;
398 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
400 PERROR("socket check relayd");
405 sin
.sin_family
= AF_INET
;
406 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
407 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
409 PERROR("inet_pton check relayd");
415 * A successful connect means the relayd exists thus returning 0 else a
416 * negative value means it does NOT exists.
418 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
423 /* Already spawned. */
429 PERROR("close relayd fd");
435 int print_missing_or_multiple_domains(unsigned int domain_count
,
436 bool include_agent_domains
)
440 if (domain_count
== 0) {
441 ERR("Please specify a domain (--kernel/--userspace%s).",
442 include_agent_domains
?
443 "/--jul/--log4j/--python" :
446 } else if (domain_count
> 1) {
447 ERR("Only one domain must be specified.");
455 * Get the discarded events and lost packet counts.
457 void print_session_stats(const char *session_name
)
460 const int ret
= get_session_stats_str(session_name
, &str
);
462 if (ret
>= 0 && str
) {
468 int get_session_stats_str(const char *session_name
, char **out_str
)
470 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
471 struct lttng_domain
*domains
;
472 struct lttng_channel
*channels
;
473 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
474 struct lttng_session
*sessions
= NULL
;
475 const struct lttng_session
*selected_session
= NULL
;
476 char *stats_str
= NULL
;
477 bool print_discarded_events
= false, print_lost_packets
= false;
479 count
= lttng_list_sessions(&sessions
);
481 ERR("Failed to retrieve session descriptions while printing session statistics.");
486 /* Identify the currently-selected sessions. */
487 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
488 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
489 selected_session
= &sessions
[session_idx
];
493 if (!selected_session
) {
494 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
499 nb_domains
= lttng_list_domains(session_name
, &domains
);
500 if (nb_domains
< 0) {
504 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
505 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
506 &domains
[domain_idx
]);
509 ERR("Failed to create session handle while printing session statistics.");
514 count
= lttng_list_channels(handle
, &channels
);
515 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
516 uint64_t discarded_events
= 0, lost_packets
= 0;
517 struct lttng_channel
*channel
= &channels
[channel_idx
];
519 ret
= lttng_channel_get_discarded_event_count(channel
,
522 ERR("Failed to retrieve discarded event count from channel %s",
526 ret
= lttng_channel_get_lost_packet_count(channel
,
529 ERR("Failed to retrieve lost packet count from channel %s",
533 discarded_events_total
+= discarded_events
;
534 lost_packets_total
+= lost_packets
;
536 lttng_destroy_handle(handle
);
539 print_discarded_events
= discarded_events_total
> 0 &&
540 !selected_session
->snapshot_mode
;
541 print_lost_packets
= lost_packets_total
> 0 &&
542 !selected_session
->snapshot_mode
;
544 if (print_discarded_events
&& print_lost_packets
) {
545 ret
= asprintf(&stats_str
,
547 " events were discarded and %" PRIu64
548 " packets were lost, please refer to "
549 "the documentation on channel configuration.",
550 discarded_events_total
, lost_packets_total
);
551 } else if (print_discarded_events
) {
552 ret
= asprintf(&stats_str
,
554 " events were discarded, please refer to "
555 "the documentation on channel configuration.",
556 discarded_events_total
);
557 } else if (print_lost_packets
) {
558 ret
= asprintf(&stats_str
,
560 " packets were lost, please refer to "
561 "the documentation on channel configuration.",
568 ERR("Failed to format lost packet and discarded events statistics");
570 *out_str
= stats_str
;
578 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
583 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
584 assert(ret
> 0 && ret
< 32);
585 ret
= utils_show_help(1, page_name
, help_msg
);
586 if (ret
&& !help_msg
) {
587 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
594 int print_trace_archive_location(
595 const struct lttng_trace_archive_location
*location
,
596 const char *session_name
)
599 enum lttng_trace_archive_location_type location_type
;
600 enum lttng_trace_archive_location_status status
;
601 bool printed_location
= false;
603 location_type
= lttng_trace_archive_location_get_type(location
);
605 _MSG("Trace chunk archive for session %s is now readable",
607 switch (location_type
) {
608 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
610 const char *absolute_path
;
612 status
= lttng_trace_archive_location_local_get_absolute_path(
613 location
, &absolute_path
);
614 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
618 MSG(" at %s", absolute_path
);
619 printed_location
= true;
622 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
624 uint16_t control_port
, data_port
;
625 const char *host
, *relative_path
, *protocol_str
;
626 enum lttng_trace_archive_location_relay_protocol_type protocol
;
628 /* Fetch all relay location parameters. */
629 status
= lttng_trace_archive_location_relay_get_protocol_type(
630 location
, &protocol
);
631 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
636 status
= lttng_trace_archive_location_relay_get_host(
638 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
643 status
= lttng_trace_archive_location_relay_get_control_port(
644 location
, &control_port
);
645 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
650 status
= lttng_trace_archive_location_relay_get_data_port(
651 location
, &data_port
);
652 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
657 status
= lttng_trace_archive_location_relay_get_relative_path(
658 location
, &relative_path
);
659 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
665 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
666 protocol_str
= "tcp";
669 protocol_str
= "unknown";
673 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
674 PRIu16
"]", protocol_str
, host
,
675 relative_path
, control_port
, data_port
);
676 printed_location
= true;
683 if (!printed_location
) {
684 MSG(" at an unknown location");