2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
20 #include <common/error.h>
21 #include <common/utils.h>
22 #include <common/defaults.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";
36 char *_get_session_name(int quiet
)
39 char *session_name
= NULL
;
41 /* Get path to config file */
42 path
= utils_get_home_dir();
47 /* Get session name from config */
48 session_name
= quiet
? config_read_session_name_quiet(path
) :
49 config_read_session_name(path
);
50 if (session_name
== NULL
) {
54 DBG2("Config file path found: %s", path
);
55 DBG("Session name found: %s", session_name
);
65 * Return allocated string with the session name found in the config
68 char *get_session_name(void)
70 return _get_session_name(0);
74 * get_session_name_quiet (no warnings/errors emitted)
76 * Return allocated string with the session name found in the config
79 char *get_session_name_quiet(void)
81 return _get_session_name(1);
87 * List commands line by line. This is mostly for bash auto completion and to
88 * avoid difficult parsing.
90 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
93 struct cmd_struct
*cmd
= NULL
;
96 while (cmd
->name
!= NULL
) {
97 fprintf(ofp
, "%s\n", cmd
->name
);
106 * Prints a simple list of the options available to a command. This is intended
107 * to be easily parsed for bash completion.
109 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
112 struct poptOption
*option
= NULL
;
114 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
115 option
= &options
[i
];
117 fprintf(ofp
, "--%s\n", option
->longName
);
119 if (isprint(option
->shortName
)) {
120 fprintf(ofp
, "-%c\n", option
->shortName
);
126 * Same as list_cmd_options, but for options specified for argpar.
128 void list_cmd_options_argpar(FILE *ofp
, const struct argpar_opt_descr
*options
)
132 for (i
= 0; options
[i
].long_name
!= NULL
; i
++) {
133 const struct argpar_opt_descr
*option
= &options
[i
];
135 fprintf(ofp
, "--%s\n", option
->long_name
);
137 if (isprint(option
->short_name
)) {
138 fprintf(ofp
, "-%c\n", option
->short_name
);
144 * fls: returns the position of the most significant bit.
145 * Returns 0 if no bit is set, else returns the position of the most
146 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
148 #if defined(__i386) || defined(__x86_64)
150 unsigned int fls_u32(uint32_t x
)
158 : "=r" (r
) : "rm" (x
));
164 #if defined(__x86_64) && defined(__LP64__)
166 unsigned int fls_u64(uint64_t x
)
174 : "=r" (r
) : "rm" (x
));
181 static __attribute__((unused
))
182 unsigned int fls_u64(uint64_t x
)
189 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
193 if (!(x
& 0xFFFF000000000000ULL
)) {
197 if (!(x
& 0xFF00000000000000ULL
)) {
201 if (!(x
& 0xF000000000000000ULL
)) {
205 if (!(x
& 0xC000000000000000ULL
)) {
209 if (!(x
& 0x8000000000000000ULL
)) {
218 static __attribute__((unused
))
219 unsigned int fls_u32(uint32_t x
)
225 if (!(x
& 0xFFFF0000U
)) {
229 if (!(x
& 0xFF000000U
)) {
233 if (!(x
& 0xF0000000U
)) {
237 if (!(x
& 0xC0000000U
)) {
241 if (!(x
& 0x80000000U
)) {
250 unsigned int fls_ulong(unsigned long x
)
252 #if (CAA_BITS_PER_LONG == 32)
260 * Return the minimum order for which x <= (1UL << order).
261 * Return -1 if x is 0.
263 int get_count_order_u32(uint32_t x
)
268 return fls_u32(x
- 1);
272 * Return the minimum order for which x <= (1UL << order).
273 * Return -1 if x is 0.
275 int get_count_order_u64(uint64_t x
)
280 return fls_u64(x
- 1);
284 * Return the minimum order for which x <= (1UL << order).
285 * Return -1 if x is 0.
287 int get_count_order_ulong(unsigned long x
)
292 return fls_ulong(x
- 1);
295 const char *get_event_type_str(enum lttng_event_type type
)
297 const char *str_event_type
;
300 case LTTNG_EVENT_ALL
:
301 str_event_type
= str_all
;
303 case LTTNG_EVENT_TRACEPOINT
:
304 str_event_type
= str_tracepoint
;
306 case LTTNG_EVENT_SYSCALL
:
307 str_event_type
= str_syscall
;
309 case LTTNG_EVENT_PROBE
:
310 str_event_type
= str_probe
;
312 case LTTNG_EVENT_USERSPACE_PROBE
:
313 str_event_type
= str_userspace_probe
;
315 case LTTNG_EVENT_FUNCTION
:
316 str_event_type
= str_function
;
319 /* Should not have an unknown event type or else define it. */
323 return str_event_type
;
327 * Spawn a lttng relayd daemon by forking and execv.
329 int spawn_relayd(const char *pathname
, int port
)
336 port
= DEFAULT_NETWORK_VIEWER_PORT
;
339 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
344 MSG("Spawning a relayd daemon");
348 * Spawn session daemon and tell
349 * it to signal us when ready.
351 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
352 /* execlp only returns if error happened */
353 if (errno
== ENOENT
) {
354 ERR("No relayd found. Use --relayd-path.");
358 kill(getppid(), SIGTERM
); /* wake parent */
360 } else if (pid
> 0) {
373 * Check if relayd is alive.
375 * Return 1 if found else 0 if NOT found. Negative value on error.
377 int check_relayd(void)
380 struct sockaddr_in sin
;
382 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
384 PERROR("socket check relayd");
389 sin
.sin_family
= AF_INET
;
390 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
391 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
393 PERROR("inet_pton check relayd");
399 * A successful connect means the relayd exists thus returning 0 else a
400 * negative value means it does NOT exists.
402 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
407 /* Already spawned. */
413 PERROR("close relayd fd");
419 int print_missing_or_multiple_domains(unsigned int domain_count
,
420 bool include_agent_domains
)
424 if (domain_count
== 0) {
425 ERR("Please specify a domain (--kernel/--userspace%s).",
426 include_agent_domains
?
427 "/--jul/--log4j/--python" :
430 } else if (domain_count
> 1) {
431 ERR("Only one domain must be specified.");
439 * Get the discarded events and lost packet counts.
441 void print_session_stats(const char *session_name
)
444 const int ret
= get_session_stats_str(session_name
, &str
);
446 if (ret
>= 0 && str
) {
452 int get_session_stats_str(const char *session_name
, char **out_str
)
454 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
455 struct lttng_domain
*domains
= NULL
;
456 struct lttng_channel
*channels
= NULL
;
457 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
458 struct lttng_session
*sessions
= NULL
;
459 const struct lttng_session
*selected_session
= NULL
;
460 char *stats_str
= NULL
;
461 bool print_discarded_events
= false, print_lost_packets
= false;
463 count
= lttng_list_sessions(&sessions
);
465 ERR("Failed to retrieve session descriptions while printing session statistics.");
470 /* Identify the currently-selected sessions. */
471 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
472 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
473 selected_session
= &sessions
[session_idx
];
477 if (!selected_session
) {
478 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
483 nb_domains
= lttng_list_domains(session_name
, &domains
);
484 if (nb_domains
< 0) {
488 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
489 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
490 &domains
[domain_idx
]);
493 ERR("Failed to create session handle while printing session statistics.");
500 count
= lttng_list_channels(handle
, &channels
);
501 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
502 uint64_t discarded_events
= 0, lost_packets
= 0;
503 struct lttng_channel
*channel
= &channels
[channel_idx
];
505 ret
= lttng_channel_get_discarded_event_count(channel
,
508 ERR("Failed to retrieve discarded event count from channel %s",
512 ret
= lttng_channel_get_lost_packet_count(channel
,
515 ERR("Failed to retrieve lost packet count from channel %s",
519 discarded_events_total
+= discarded_events
;
520 lost_packets_total
+= lost_packets
;
522 lttng_destroy_handle(handle
);
525 print_discarded_events
= discarded_events_total
> 0 &&
526 !selected_session
->snapshot_mode
;
527 print_lost_packets
= lost_packets_total
> 0 &&
528 !selected_session
->snapshot_mode
;
530 if (print_discarded_events
&& print_lost_packets
) {
531 ret
= asprintf(&stats_str
,
533 " events were discarded and %" PRIu64
534 " packets were lost, please refer to "
535 "the documentation on channel configuration.",
536 discarded_events_total
, lost_packets_total
);
537 } else if (print_discarded_events
) {
538 ret
= asprintf(&stats_str
,
540 " events were discarded, please refer to "
541 "the documentation on channel configuration.",
542 discarded_events_total
);
543 } else if (print_lost_packets
) {
544 ret
= asprintf(&stats_str
,
546 " packets were lost, please refer to "
547 "the documentation on channel configuration.",
554 ERR("Failed to format lost packet and discarded events statistics");
556 *out_str
= stats_str
;
566 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
571 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
572 LTTNG_ASSERT(ret
> 0 && ret
< 32);
573 ret
= utils_show_help(1, page_name
, help_msg
);
574 if (ret
&& !help_msg
) {
575 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
582 int print_trace_archive_location(
583 const struct lttng_trace_archive_location
*location
,
584 const char *session_name
)
587 enum lttng_trace_archive_location_type location_type
;
588 enum lttng_trace_archive_location_status status
;
589 bool printed_location
= false;
591 location_type
= lttng_trace_archive_location_get_type(location
);
593 _MSG("Trace chunk archive for session %s is now readable",
595 switch (location_type
) {
596 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
598 const char *absolute_path
;
600 status
= lttng_trace_archive_location_local_get_absolute_path(
601 location
, &absolute_path
);
602 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
606 MSG(" at %s", absolute_path
);
607 printed_location
= true;
610 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
612 uint16_t control_port
, data_port
;
613 const char *host
, *relative_path
, *protocol_str
;
614 enum lttng_trace_archive_location_relay_protocol_type protocol
;
616 /* Fetch all relay location parameters. */
617 status
= lttng_trace_archive_location_relay_get_protocol_type(
618 location
, &protocol
);
619 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
624 status
= lttng_trace_archive_location_relay_get_host(
626 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
631 status
= lttng_trace_archive_location_relay_get_control_port(
632 location
, &control_port
);
633 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
638 status
= lttng_trace_archive_location_relay_get_data_port(
639 location
, &data_port
);
640 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
645 status
= lttng_trace_archive_location_relay_get_relative_path(
646 location
, &relative_path
);
647 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
653 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
654 protocol_str
= "tcp";
657 protocol_str
= "unknown";
661 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
662 PRIu16
"]", protocol_str
, host
,
663 relative_path
, control_port
, data_port
);
664 printed_location
= true;
671 if (!printed_location
) {
672 MSG(" at an unknown location");