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_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
, 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) {
374 * Check if relayd is alive.
376 * Return 1 if found else 0 if NOT found. Negative value on error.
378 int check_relayd(void)
381 struct sockaddr_in sin
;
383 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
385 PERROR("socket check relayd");
390 sin
.sin_family
= AF_INET
;
391 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
392 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
394 PERROR("inet_pton check relayd");
400 * A successful connect means the relayd exists thus returning 0 else a
401 * negative value means it does NOT exists.
403 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
408 /* Already spawned. */
414 PERROR("close relayd fd");
420 int print_missing_or_multiple_domains(unsigned int domain_count
,
421 bool include_agent_domains
)
425 if (domain_count
== 0) {
426 ERR("Please specify a domain (--kernel/--userspace%s).",
427 include_agent_domains
?
428 "/--jul/--log4j/--python" :
431 } else if (domain_count
> 1) {
432 ERR("Only one domain must be specified.");
440 * Get the discarded events and lost packet counts.
442 void print_session_stats(const char *session_name
)
445 const int ret
= get_session_stats_str(session_name
, &str
);
447 if (ret
>= 0 && str
) {
453 int get_session_stats_str(const char *session_name
, char **out_str
)
455 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
, ret
;
456 struct lttng_domain
*domains
;
457 struct lttng_channel
*channels
;
458 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
459 struct lttng_session
*sessions
= NULL
;
460 const struct lttng_session
*selected_session
= NULL
;
461 char *stats_str
= NULL
;
462 bool print_discarded_events
= false, print_lost_packets
= false;
464 count
= lttng_list_sessions(&sessions
);
466 ERR("Failed to retrieve session descriptions while printing session statistics.");
471 /* Identify the currently-selected sessions. */
472 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
473 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
474 selected_session
= &sessions
[session_idx
];
478 if (!selected_session
) {
479 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
484 nb_domains
= lttng_list_domains(session_name
, &domains
);
485 if (nb_domains
< 0) {
489 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
490 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
491 &domains
[domain_idx
]);
494 ERR("Failed to create session handle while printing session statistics.");
499 count
= lttng_list_channels(handle
, &channels
);
500 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
501 uint64_t discarded_events
= 0, lost_packets
= 0;
502 struct lttng_channel
*channel
= &channels
[channel_idx
];
504 ret
= lttng_channel_get_discarded_event_count(channel
,
507 ERR("Failed to retrieve discarded event count from channel %s",
511 ret
= lttng_channel_get_lost_packet_count(channel
,
514 ERR("Failed to retrieve lost packet count from channel %s",
518 discarded_events_total
+= discarded_events
;
519 lost_packets_total
+= lost_packets
;
521 lttng_destroy_handle(handle
);
524 print_discarded_events
= discarded_events_total
> 0 &&
525 !selected_session
->snapshot_mode
;
526 print_lost_packets
= lost_packets_total
> 0 &&
527 !selected_session
->snapshot_mode
;
529 if (print_discarded_events
&& print_lost_packets
) {
530 ret
= asprintf(&stats_str
,
532 " events were discarded and %" PRIu64
533 " packets were lost, please refer to "
534 "the documentation on channel configuration.",
535 discarded_events_total
, lost_packets_total
);
536 } else if (print_discarded_events
) {
537 ret
= asprintf(&stats_str
,
539 " events were discarded, please refer to "
540 "the documentation on channel configuration.",
541 discarded_events_total
);
542 } else if (print_lost_packets
) {
543 ret
= asprintf(&stats_str
,
545 " packets were lost, please refer to "
546 "the documentation on channel configuration.",
553 ERR("Failed to format lost packet and discarded events statistics");
555 *out_str
= stats_str
;
563 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
568 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
569 assert(ret
> 0 && ret
< 32);
570 ret
= utils_show_help(1, page_name
, help_msg
);
571 if (ret
&& !help_msg
) {
572 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
579 int print_trace_archive_location(
580 const struct lttng_trace_archive_location
*location
,
581 const char *session_name
)
584 enum lttng_trace_archive_location_type location_type
;
585 enum lttng_trace_archive_location_status status
;
586 bool printed_location
= false;
588 location_type
= lttng_trace_archive_location_get_type(location
);
590 _MSG("Trace chunk archive for session %s is now readable",
592 switch (location_type
) {
593 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
595 const char *absolute_path
;
597 status
= lttng_trace_archive_location_local_get_absolute_path(
598 location
, &absolute_path
);
599 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
603 MSG(" at %s", absolute_path
);
604 printed_location
= true;
607 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
609 uint16_t control_port
, data_port
;
610 const char *host
, *relative_path
, *protocol_str
;
611 enum lttng_trace_archive_location_relay_protocol_type protocol
;
613 /* Fetch all relay location parameters. */
614 status
= lttng_trace_archive_location_relay_get_protocol_type(
615 location
, &protocol
);
616 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
621 status
= lttng_trace_archive_location_relay_get_host(
623 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
628 status
= lttng_trace_archive_location_relay_get_control_port(
629 location
, &control_port
);
630 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
635 status
= lttng_trace_archive_location_relay_get_data_port(
636 location
, &data_port
);
637 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
642 status
= lttng_trace_archive_location_relay_get_relative_path(
643 location
, &relative_path
);
644 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
650 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
651 protocol_str
= "tcp";
654 protocol_str
= "unknown";
658 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
659 PRIu16
"]", protocol_str
, host
,
660 relative_path
, control_port
, data_port
);
661 printed_location
= true;
668 if (!printed_location
) {
669 MSG(" at an unknown location");