2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <netinet/in.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
21 #include <common/error.hpp>
22 #include <common/utils.hpp>
23 #include <common/defaults.hpp>
27 #include "command.hpp"
29 static const char *str_all
= "ALL";
30 static const char *str_tracepoint
= "Tracepoint";
31 static const char *str_syscall
= "Syscall";
32 static const char *str_probe
= "Probe";
33 static const char *str_userspace_probe
= "Userspace Probe";
34 static const char *str_function
= "Function";
37 char *_get_session_name(int quiet
)
40 char *session_name
= NULL
;
42 /* Get path to config file */
43 path
= utils_get_home_dir();
48 /* Get session name from config */
49 session_name
= quiet
? config_read_session_name_quiet(path
) :
50 config_read_session_name(path
);
51 if (session_name
== NULL
) {
55 DBG2("Config file path found: %s", path
);
56 DBG("Session name found: %s", session_name
);
66 * Return allocated string with the session name found in the config
69 char *get_session_name(void)
71 return _get_session_name(0);
75 * get_session_name_quiet (no warnings/errors emitted)
77 * Return allocated string with the session name found in the config
80 char *get_session_name_quiet(void)
82 return _get_session_name(1);
88 * List commands line by line. This is mostly for bash auto completion and to
89 * avoid difficult parsing.
91 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
94 struct cmd_struct
*cmd
= NULL
;
97 while (cmd
->name
!= NULL
) {
98 fprintf(ofp
, "%s\n", cmd
->name
);
107 * Prints a simple list of the options available to a command. This is intended
108 * to be easily parsed for bash completion.
110 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
113 struct poptOption
*option
= NULL
;
115 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
116 option
= &options
[i
];
118 fprintf(ofp
, "--%s\n", option
->longName
);
120 if (isprint(option
->shortName
)) {
121 fprintf(ofp
, "-%c\n", option
->shortName
);
127 * Same as list_cmd_options, but for options specified for argpar.
129 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
133 for (i
= 0; options
[i
].long_name
!= NULL
; i
++) {
134 const struct argpar_opt_descr
*option
= &options
[i
];
136 fprintf(ofp
, "--%s\n", option
->long_name
);
138 if (isprint(option
->short_name
)) {
139 fprintf(ofp
, "-%c\n", option
->short_name
);
145 * fls: returns the position of the most significant bit.
146 * Returns 0 if no bit is set, else returns the position of the most
147 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
149 #if defined(__i386) || defined(__x86_64)
151 unsigned int fls_u32(uint32_t x
)
159 : "=r" (r
) : "rm" (x
));
165 #if defined(__x86_64) && defined(__LP64__)
167 unsigned int fls_u64(uint64_t x
)
175 : "=r" (r
) : "rm" (x
));
182 static __attribute__((unused
))
183 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
))
220 unsigned int fls_u32(uint32_t x
)
226 if (!(x
& 0xFFFF0000U
)) {
230 if (!(x
& 0xFF000000U
)) {
234 if (!(x
& 0xF0000000U
)) {
238 if (!(x
& 0xC0000000U
)) {
242 if (!(x
& 0x80000000U
)) {
251 unsigned int fls_ulong(unsigned long x
)
253 #if (CAA_BITS_PER_LONG == 32)
261 * Return the minimum order for which x <= (1UL << order).
262 * Return -1 if x is 0.
264 int get_count_order_u32(uint32_t x
)
269 return fls_u32(x
- 1);
273 * Return the minimum order for which x <= (1UL << order).
274 * Return -1 if x is 0.
276 int get_count_order_u64(uint64_t x
)
281 return fls_u64(x
- 1);
285 * Return the minimum order for which x <= (1UL << order).
286 * Return -1 if x is 0.
288 int get_count_order_ulong(unsigned long x
)
293 return fls_ulong(x
- 1);
296 const char *get_event_type_str(enum lttng_event_type type
)
298 const char *str_event_type
;
301 case LTTNG_EVENT_ALL
:
302 str_event_type
= str_all
;
304 case LTTNG_EVENT_TRACEPOINT
:
305 str_event_type
= str_tracepoint
;
307 case LTTNG_EVENT_SYSCALL
:
308 str_event_type
= str_syscall
;
310 case LTTNG_EVENT_PROBE
:
311 str_event_type
= str_probe
;
313 case LTTNG_EVENT_USERSPACE_PROBE
:
314 str_event_type
= str_userspace_probe
;
316 case LTTNG_EVENT_FUNCTION
:
317 str_event_type
= str_function
;
320 /* Should not have an unknown event type or else define it. */
324 return str_event_type
;
328 * Spawn a lttng relayd daemon by forking and execv.
330 int spawn_relayd(const char *pathname
, int port
)
337 port
= DEFAULT_NETWORK_VIEWER_PORT
;
340 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
345 MSG("Spawning a relayd daemon");
349 * Spawn session daemon and tell
350 * it to signal us when ready.
352 execlp(pathname
, "lttng-relayd", "-L", url
, "--daemonize", NULL
);
353 /* execlp only returns if error happened */
354 if (errno
== ENOENT
) {
355 ERR("No relayd found. Use --relayd-path.");
359 kill(getppid(), SIGTERM
); /* wake parent */
361 } else if (pid
> 0) {
363 * In daemon mode (--daemonize), lttng-relayd only exits when
364 * it's ready to accept commands.
368 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
370 if (wait_pid_ret
< 0) {
371 if (errno
== EINTR
) {
379 if (WIFSIGNALED(status
)) {
380 ERR("Relay daemon was killed by signal %d", WTERMSIG(status
));
383 } else if (WIFEXITED(status
)) {
384 DBG("Relay daemon terminated normally (exit status: %d)",
385 WEXITSTATUS(status
));
387 if (WEXITSTATUS(status
) != 0) {
388 ERR("Relay daemon terminated with an error (exit status: %d)",
389 WEXITSTATUS(status
));
409 * Check if relayd is alive.
411 * Return 1 if found else 0 if NOT found. Negative value on error.
413 int check_relayd(void)
416 struct sockaddr_in sin
;
418 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
420 PERROR("socket check relayd");
425 sin
.sin_family
= AF_INET
;
426 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
427 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
429 PERROR("inet_pton check relayd");
435 * A successful connect means the relayd exists thus returning 0 else a
436 * negative value means it does NOT exists.
438 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
443 /* Already spawned. */
449 PERROR("close relayd fd");
455 int print_missing_or_multiple_domains(unsigned int domain_count
,
456 bool include_agent_domains
)
460 if (domain_count
== 0) {
461 ERR("Please specify a domain (--kernel/--userspace%s).",
462 include_agent_domains
?
463 "/--jul/--log4j/--python" :
466 } else if (domain_count
> 1) {
467 ERR("Only one domain must be specified.");
475 * Get the discarded events and lost packet counts.
477 void print_session_stats(const char *session_name
)
480 const int ret
= get_session_stats_str(session_name
, &str
);
482 if (ret
>= 0 && str
) {
488 int get_session_stats_str(const char *session_name
, char **out_str
)
490 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
491 struct lttng_domain
*domains
= NULL
;
492 struct lttng_channel
*channels
= NULL
;
493 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
494 struct lttng_session
*sessions
= NULL
;
495 const struct lttng_session
*selected_session
= NULL
;
496 char *stats_str
= NULL
;
497 bool print_discarded_events
= false, print_lost_packets
= false;
499 count
= lttng_list_sessions(&sessions
);
501 ERR("Failed to retrieve session descriptions while printing session statistics.");
506 /* Identify the currently-selected sessions. */
507 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
508 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
509 selected_session
= &sessions
[session_idx
];
513 if (!selected_session
) {
514 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
519 nb_domains
= lttng_list_domains(session_name
, &domains
);
520 if (nb_domains
< 0) {
524 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
525 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
526 &domains
[domain_idx
]);
529 ERR("Failed to create session handle while printing session statistics.");
536 count
= lttng_list_channels(handle
, &channels
);
537 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
538 uint64_t discarded_events
= 0, lost_packets
= 0;
539 struct lttng_channel
*channel
= &channels
[channel_idx
];
541 ret
= lttng_channel_get_discarded_event_count(channel
,
544 ERR("Failed to retrieve discarded event count from channel %s",
548 ret
= lttng_channel_get_lost_packet_count(channel
,
551 ERR("Failed to retrieve lost packet count from channel %s",
555 discarded_events_total
+= discarded_events
;
556 lost_packets_total
+= lost_packets
;
558 lttng_destroy_handle(handle
);
561 print_discarded_events
= discarded_events_total
> 0 &&
562 !selected_session
->snapshot_mode
;
563 print_lost_packets
= lost_packets_total
> 0 &&
564 !selected_session
->snapshot_mode
;
566 if (print_discarded_events
&& print_lost_packets
) {
567 ret
= asprintf(&stats_str
,
569 " events were discarded and %" PRIu64
570 " packets were lost, please refer to "
571 "the documentation on channel configuration.",
572 discarded_events_total
, lost_packets_total
);
573 } else if (print_discarded_events
) {
574 ret
= asprintf(&stats_str
,
576 " events were discarded, please refer to "
577 "the documentation on channel configuration.",
578 discarded_events_total
);
579 } else if (print_lost_packets
) {
580 ret
= asprintf(&stats_str
,
582 " packets were lost, please refer to "
583 "the documentation on channel configuration.",
590 ERR("Failed to format lost packet and discarded events statistics");
592 *out_str
= stats_str
;
602 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
607 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
608 LTTNG_ASSERT(ret
> 0 && ret
< 32);
609 ret
= utils_show_help(1, page_name
, help_msg
);
610 if (ret
&& !help_msg
) {
611 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
618 int print_trace_archive_location(
619 const struct lttng_trace_archive_location
*location
,
620 const char *session_name
)
623 enum lttng_trace_archive_location_type location_type
;
624 enum lttng_trace_archive_location_status status
;
625 bool printed_location
= false;
627 location_type
= lttng_trace_archive_location_get_type(location
);
629 _MSG("Trace chunk archive for session %s is now readable",
631 switch (location_type
) {
632 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
634 const char *absolute_path
;
636 status
= lttng_trace_archive_location_local_get_absolute_path(
637 location
, &absolute_path
);
638 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
642 MSG(" at %s", absolute_path
);
643 printed_location
= true;
646 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
648 uint16_t control_port
, data_port
;
649 const char *host
, *relative_path
, *protocol_str
;
650 enum lttng_trace_archive_location_relay_protocol_type protocol
;
652 /* Fetch all relay location parameters. */
653 status
= lttng_trace_archive_location_relay_get_protocol_type(
654 location
, &protocol
);
655 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
660 status
= lttng_trace_archive_location_relay_get_host(
662 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
667 status
= lttng_trace_archive_location_relay_get_control_port(
668 location
, &control_port
);
669 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
674 status
= lttng_trace_archive_location_relay_get_data_port(
675 location
, &data_port
);
676 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
681 status
= lttng_trace_archive_location_relay_get_relative_path(
682 location
, &relative_path
);
683 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
689 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
690 protocol_str
= "tcp";
693 protocol_str
= "unknown";
697 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
698 PRIu16
"]", protocol_str
, host
,
699 relative_path
, control_port
, data_port
);
700 printed_location
= true;
707 if (!printed_location
) {
708 MSG(" at an unknown location");