2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
31 #include <common/error.h>
32 #include <common/utils.h>
33 #include <common/defaults.h>
39 static const char *str_kernel
= "Kernel";
40 static const char *str_ust
= "UST";
41 static const char *str_jul
= "JUL";
42 static const char *str_log4j
= "LOG4J";
43 static const char *str_python
= "Python";
44 static const char *str_all
= "ALL";
45 static const char *str_tracepoint
= "Tracepoint";
46 static const char *str_syscall
= "Syscall";
47 static const char *str_probe
= "Probe";
48 static const char *str_userspace_probe
= "Userspace Probe";
49 static const char *str_function
= "Function";
52 char *_get_session_name(int quiet
)
54 char *path
, *session_name
= NULL
;
56 /* Get path to config file */
57 path
= utils_get_home_dir();
62 /* Get session name from config */
63 session_name
= quiet
? config_read_session_name_quiet(path
) :
64 config_read_session_name(path
);
65 if (session_name
== NULL
) {
69 DBG2("Config file path found: %s", path
);
70 DBG("Session name found: %s", session_name
);
80 * Return allocated string with the session name found in the config
83 char *get_session_name(void)
85 return _get_session_name(0);
89 * get_session_name_quiet (no warnings/errors emitted)
91 * Return allocated string with the session name found in the config
94 char *get_session_name_quiet(void)
96 return _get_session_name(1);
102 * List commands line by line. This is mostly for bash auto completion and to
103 * avoid difficult parsing.
105 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
108 struct cmd_struct
*cmd
= NULL
;
111 while (cmd
->name
!= NULL
) {
112 fprintf(ofp
, "%s\n", cmd
->name
);
121 * Prints a simple list of the options available to a command. This is intended
122 * to be easily parsed for bash completion.
124 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
127 struct poptOption
*option
= NULL
;
129 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
130 option
= &options
[i
];
132 fprintf(ofp
, "--%s\n", option
->longName
);
134 if (isprint(option
->shortName
)) {
135 fprintf(ofp
, "-%c\n", option
->shortName
);
141 * fls: returns the position of the most significant bit.
142 * Returns 0 if no bit is set, else returns the position of the most
143 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
145 #if defined(__i386) || defined(__x86_64)
147 unsigned int fls_u32(uint32_t x
)
155 : "=r" (r
) : "rm" (x
));
161 #if defined(__x86_64)
163 unsigned int fls_u64(uint64_t x
)
171 : "=r" (r
) : "rm" (x
));
178 static __attribute__((unused
))
179 unsigned int fls_u64(uint64_t x
)
186 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
190 if (!(x
& 0xFFFF000000000000ULL
)) {
194 if (!(x
& 0xFF00000000000000ULL
)) {
198 if (!(x
& 0xF000000000000000ULL
)) {
202 if (!(x
& 0xC000000000000000ULL
)) {
206 if (!(x
& 0x8000000000000000ULL
)) {
215 static __attribute__((unused
))
216 unsigned int fls_u32(uint32_t x
)
222 if (!(x
& 0xFFFF0000U
)) {
226 if (!(x
& 0xFF000000U
)) {
230 if (!(x
& 0xF0000000U
)) {
234 if (!(x
& 0xC0000000U
)) {
238 if (!(x
& 0x80000000U
)) {
247 unsigned int fls_ulong(unsigned long x
)
249 #if (CAA_BITS_PER_LONG == 32)
257 * Return the minimum order for which x <= (1UL << order).
258 * Return -1 if x is 0.
260 int get_count_order_u32(uint32_t x
)
265 return fls_u32(x
- 1);
269 * Return the minimum order for which x <= (1UL << order).
270 * Return -1 if x is 0.
272 int get_count_order_u64(uint64_t x
)
277 return fls_u64(x
- 1);
281 * Return the minimum order for which x <= (1UL << order).
282 * Return -1 if x is 0.
284 int get_count_order_ulong(unsigned long x
)
289 return fls_ulong(x
- 1);
292 const char *get_domain_str(enum lttng_domain_type domain
)
297 case LTTNG_DOMAIN_KERNEL
:
298 str_dom
= str_kernel
;
300 case LTTNG_DOMAIN_UST
:
303 case LTTNG_DOMAIN_JUL
:
306 case LTTNG_DOMAIN_LOG4J
:
309 case LTTNG_DOMAIN_PYTHON
:
310 str_dom
= str_python
;
313 /* Should not have an unknown domain or else define it. */
320 const char *get_event_type_str(enum lttng_event_type type
)
322 const char *str_event_type
;
325 case LTTNG_EVENT_ALL
:
326 str_event_type
= str_all
;
328 case LTTNG_EVENT_TRACEPOINT
:
329 str_event_type
= str_tracepoint
;
331 case LTTNG_EVENT_SYSCALL
:
332 str_event_type
= str_syscall
;
334 case LTTNG_EVENT_PROBE
:
335 str_event_type
= str_probe
;
337 case LTTNG_EVENT_USERSPACE_PROBE
:
338 str_event_type
= str_userspace_probe
;
340 case LTTNG_EVENT_FUNCTION
:
341 str_event_type
= str_function
;
344 /* Should not have an unknown event type or else define it. */
348 return str_event_type
;
352 * Spawn a lttng relayd daemon by forking and execv.
354 int spawn_relayd(const char *pathname
, int port
)
361 port
= DEFAULT_NETWORK_VIEWER_PORT
;
364 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
369 MSG("Spawning a relayd daemon");
373 * Spawn session daemon and tell
374 * it to signal us when ready.
376 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
377 /* execlp only returns if error happened */
378 if (errno
== ENOENT
) {
379 ERR("No relayd found. Use --relayd-path.");
383 kill(getppid(), SIGTERM
); /* wake parent */
385 } else if (pid
> 0) {
398 * Check if relayd is alive.
400 * Return 1 if found else 0 if NOT found. Negative value on error.
402 int check_relayd(void)
405 struct sockaddr_in sin
;
407 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
409 PERROR("socket check relayd");
414 sin
.sin_family
= AF_INET
;
415 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
416 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
418 PERROR("inet_pton check relayd");
424 * A successful connect means the relayd exists thus returning 0 else a
425 * negative value means it does NOT exists.
427 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
432 /* Already spawned. */
438 PERROR("close relayd fd");
444 int print_missing_or_multiple_domains(unsigned int sum
)
449 ERR("Please specify a domain (-k/-u/-j).");
451 } else if (sum
> 1) {
452 ERR("Multiple domains specified.");
460 * Get the discarded events and lost packet counts.
462 void print_session_stats(const char *session_name
)
464 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
;
465 struct lttng_domain
*domains
;
466 struct lttng_channel
*channels
;
467 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
468 struct lttng_session
*sessions
= NULL
;
469 const struct lttng_session
*selected_session
= NULL
;
471 count
= lttng_list_sessions(&sessions
);
473 ERR("Failed to retrieve session descriptions while printing session statistics.");
477 /* Identify the currently-selected sessions. */
478 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
479 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
480 selected_session
= &sessions
[session_idx
];
484 if (!selected_session
) {
485 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
489 nb_domains
= lttng_list_domains(session_name
, &domains
);
490 if (nb_domains
< 0) {
493 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
494 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
495 &domains
[domain_idx
]);
498 ERR("Failed to create session handle while printing session statistics.");
502 count
= lttng_list_channels(handle
, &channels
);
503 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
505 uint64_t discarded_events
= 0, lost_packets
= 0;
506 struct lttng_channel
*channel
= &channels
[channel_idx
];
508 ret
= lttng_channel_get_discarded_event_count(channel
,
511 ERR("Failed to retrieve discarded event count from channel %s",
515 ret
= lttng_channel_get_lost_packet_count(channel
,
518 ERR("Failed to retrieve lost packet count from channel %s",
522 discarded_events_total
+= discarded_events
;
523 lost_packets_total
+= lost_packets
;
525 lttng_destroy_handle(handle
);
527 if (discarded_events_total
> 0 && !selected_session
->snapshot_mode
) {
528 MSG("[warning] %" PRIu64
" events discarded, please refer to "
529 "the documentation on channel configuration.",
530 discarded_events_total
);
532 if (lost_packets_total
> 0 && !selected_session
->snapshot_mode
) {
533 MSG("[warning] %" PRIu64
" packets lost, please refer to "
534 "the documentation on channel configuration.",
542 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
547 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
548 assert(ret
> 0 && ret
< 32);
549 ret
= utils_show_help(1, page_name
, help_msg
);
550 if (ret
&& !help_msg
) {
551 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
558 int print_trace_archive_location(
559 const struct lttng_trace_archive_location
*location
,
560 const char *session_name
)
563 enum lttng_trace_archive_location_type location_type
;
564 enum lttng_trace_archive_location_status status
;
565 bool printed_location
= false;
567 location_type
= lttng_trace_archive_location_get_type(location
);
569 _MSG("Trace chunk archive for session %s is now readable",
571 switch (location_type
) {
572 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
574 const char *absolute_path
;
576 status
= lttng_trace_archive_location_local_get_absolute_path(
577 location
, &absolute_path
);
578 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
582 MSG(" at %s", absolute_path
);
583 printed_location
= true;
586 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
588 uint16_t control_port
, data_port
;
589 const char *host
, *relative_path
, *protocol_str
;
590 enum lttng_trace_archive_location_relay_protocol_type protocol
;
592 /* Fetch all relay location parameters. */
593 status
= lttng_trace_archive_location_relay_get_protocol_type(
594 location
, &protocol
);
595 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
600 status
= lttng_trace_archive_location_relay_get_host(
602 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
607 status
= lttng_trace_archive_location_relay_get_control_port(
608 location
, &control_port
);
609 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
614 status
= lttng_trace_archive_location_relay_get_data_port(
615 location
, &data_port
);
616 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
621 status
= lttng_trace_archive_location_relay_get_relative_path(
622 location
, &relative_path
);
623 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
629 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
630 protocol_str
= "tcp";
633 protocol_str
= "unknown";
637 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
638 PRIu16
"]", protocol_str
, host
,
639 relative_path
, control_port
, data_port
);
640 printed_location
= true;
647 if (!printed_location
) {
648 MSG(" at an unknown location");