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
)
55 char *session_name
= NULL
;
57 /* Get path to config file */
58 path
= utils_get_home_dir();
63 /* Get session name from config */
64 session_name
= quiet
? config_read_session_name_quiet(path
) :
65 config_read_session_name(path
);
66 if (session_name
== NULL
) {
70 DBG2("Config file path found: %s", path
);
71 DBG("Session name found: %s", session_name
);
81 * Return allocated string with the session name found in the config
84 char *get_session_name(void)
86 return _get_session_name(0);
90 * get_session_name_quiet (no warnings/errors emitted)
92 * Return allocated string with the session name found in the config
95 char *get_session_name_quiet(void)
97 return _get_session_name(1);
103 * List commands line by line. This is mostly for bash auto completion and to
104 * avoid difficult parsing.
106 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
109 struct cmd_struct
*cmd
= NULL
;
112 while (cmd
->name
!= NULL
) {
113 fprintf(ofp
, "%s\n", cmd
->name
);
122 * Prints a simple list of the options available to a command. This is intended
123 * to be easily parsed for bash completion.
125 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
128 struct poptOption
*option
= NULL
;
130 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
131 option
= &options
[i
];
133 fprintf(ofp
, "--%s\n", option
->longName
);
135 if (isprint(option
->shortName
)) {
136 fprintf(ofp
, "-%c\n", option
->shortName
);
142 * fls: returns the position of the most significant bit.
143 * Returns 0 if no bit is set, else returns the position of the most
144 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
146 #if defined(__i386) || defined(__x86_64)
148 unsigned int fls_u32(uint32_t x
)
156 : "=r" (r
) : "rm" (x
));
162 #if defined(__x86_64)
164 unsigned int fls_u64(uint64_t x
)
172 : "=r" (r
) : "rm" (x
));
179 static __attribute__((unused
))
180 unsigned int fls_u64(uint64_t x
)
187 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
191 if (!(x
& 0xFFFF000000000000ULL
)) {
195 if (!(x
& 0xFF00000000000000ULL
)) {
199 if (!(x
& 0xF000000000000000ULL
)) {
203 if (!(x
& 0xC000000000000000ULL
)) {
207 if (!(x
& 0x8000000000000000ULL
)) {
216 static __attribute__((unused
))
217 unsigned int fls_u32(uint32_t x
)
223 if (!(x
& 0xFFFF0000U
)) {
227 if (!(x
& 0xFF000000U
)) {
231 if (!(x
& 0xF0000000U
)) {
235 if (!(x
& 0xC0000000U
)) {
239 if (!(x
& 0x80000000U
)) {
248 unsigned int fls_ulong(unsigned long x
)
250 #if (CAA_BITS_PER_LONG == 32)
258 * Return the minimum order for which x <= (1UL << order).
259 * Return -1 if x is 0.
261 int get_count_order_u32(uint32_t x
)
266 return fls_u32(x
- 1);
270 * Return the minimum order for which x <= (1UL << order).
271 * Return -1 if x is 0.
273 int get_count_order_u64(uint64_t x
)
278 return fls_u64(x
- 1);
282 * Return the minimum order for which x <= (1UL << order).
283 * Return -1 if x is 0.
285 int get_count_order_ulong(unsigned long x
)
290 return fls_ulong(x
- 1);
293 const char *get_domain_str(enum lttng_domain_type domain
)
298 case LTTNG_DOMAIN_KERNEL
:
299 str_dom
= str_kernel
;
301 case LTTNG_DOMAIN_UST
:
304 case LTTNG_DOMAIN_JUL
:
307 case LTTNG_DOMAIN_LOG4J
:
310 case LTTNG_DOMAIN_PYTHON
:
311 str_dom
= str_python
;
314 /* Should not have an unknown domain or else define it. */
321 const char *get_event_type_str(enum lttng_event_type type
)
323 const char *str_event_type
;
326 case LTTNG_EVENT_ALL
:
327 str_event_type
= str_all
;
329 case LTTNG_EVENT_TRACEPOINT
:
330 str_event_type
= str_tracepoint
;
332 case LTTNG_EVENT_SYSCALL
:
333 str_event_type
= str_syscall
;
335 case LTTNG_EVENT_PROBE
:
336 str_event_type
= str_probe
;
338 case LTTNG_EVENT_USERSPACE_PROBE
:
339 str_event_type
= str_userspace_probe
;
341 case LTTNG_EVENT_FUNCTION
:
342 str_event_type
= str_function
;
345 /* Should not have an unknown event type or else define it. */
349 return str_event_type
;
353 * Spawn a lttng relayd daemon by forking and execv.
355 int spawn_relayd(const char *pathname
, int port
)
362 port
= DEFAULT_NETWORK_VIEWER_PORT
;
365 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
370 MSG("Spawning a relayd daemon");
374 * Spawn session daemon and tell
375 * it to signal us when ready.
377 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
378 /* execlp only returns if error happened */
379 if (errno
== ENOENT
) {
380 ERR("No relayd found. Use --relayd-path.");
384 kill(getppid(), SIGTERM
); /* wake parent */
386 } else if (pid
> 0) {
399 * Check if relayd is alive.
401 * Return 1 if found else 0 if NOT found. Negative value on error.
403 int check_relayd(void)
406 struct sockaddr_in sin
;
408 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
410 PERROR("socket check relayd");
415 sin
.sin_family
= AF_INET
;
416 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
417 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
419 PERROR("inet_pton check relayd");
425 * A successful connect means the relayd exists thus returning 0 else a
426 * negative value means it does NOT exists.
428 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
433 /* Already spawned. */
439 PERROR("close relayd fd");
445 int print_missing_or_multiple_domains(unsigned int sum
)
450 ERR("Please specify a domain (-k/-u/-j).");
452 } else if (sum
> 1) {
453 ERR("Multiple domains specified.");
461 * Get the discarded events and lost packet counts.
463 void print_session_stats(const char *session_name
)
465 int count
, nb_domains
, domain_idx
, channel_idx
, session_idx
;
466 struct lttng_domain
*domains
;
467 struct lttng_channel
*channels
;
468 uint64_t discarded_events_total
= 0, lost_packets_total
= 0;
469 struct lttng_session
*sessions
= NULL
;
470 const struct lttng_session
*selected_session
= NULL
;
472 count
= lttng_list_sessions(&sessions
);
474 ERR("Failed to retrieve session descriptions while printing session statistics.");
478 /* Identify the currently-selected sessions. */
479 for (session_idx
= 0; session_idx
< count
; session_idx
++) {
480 if (!strcmp(session_name
, sessions
[session_idx
].name
)) {
481 selected_session
= &sessions
[session_idx
];
485 if (!selected_session
) {
486 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name
);
490 nb_domains
= lttng_list_domains(session_name
, &domains
);
491 if (nb_domains
< 0) {
494 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
495 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
496 &domains
[domain_idx
]);
499 ERR("Failed to create session handle while printing session statistics.");
503 count
= lttng_list_channels(handle
, &channels
);
504 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
506 uint64_t discarded_events
= 0, lost_packets
= 0;
507 struct lttng_channel
*channel
= &channels
[channel_idx
];
509 ret
= lttng_channel_get_discarded_event_count(channel
,
512 ERR("Failed to retrieve discarded event count from channel %s",
516 ret
= lttng_channel_get_lost_packet_count(channel
,
519 ERR("Failed to retrieve lost packet count from channel %s",
523 discarded_events_total
+= discarded_events
;
524 lost_packets_total
+= lost_packets
;
526 lttng_destroy_handle(handle
);
528 if (discarded_events_total
> 0 && !selected_session
->snapshot_mode
) {
529 MSG("[warning] %" PRIu64
" events discarded, please refer to "
530 "the documentation on channel configuration.",
531 discarded_events_total
);
533 if (lost_packets_total
> 0 && !selected_session
->snapshot_mode
) {
534 MSG("[warning] %" PRIu64
" packets lost, please refer to "
535 "the documentation on channel configuration.",
543 int show_cmd_help(const char *cmd_name
, const char *help_msg
)
548 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
549 assert(ret
> 0 && ret
< 32);
550 ret
= utils_show_help(1, page_name
, help_msg
);
551 if (ret
&& !help_msg
) {
552 ERR("Cannot view man page `lttng-%s(1)`", cmd_name
);
559 int print_trace_archive_location(
560 const struct lttng_trace_archive_location
*location
,
561 const char *session_name
)
564 enum lttng_trace_archive_location_type location_type
;
565 enum lttng_trace_archive_location_status status
;
566 bool printed_location
= false;
568 location_type
= lttng_trace_archive_location_get_type(location
);
570 _MSG("Trace chunk archive for session %s is now readable",
572 switch (location_type
) {
573 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
575 const char *absolute_path
;
577 status
= lttng_trace_archive_location_local_get_absolute_path(
578 location
, &absolute_path
);
579 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
583 MSG(" at %s", absolute_path
);
584 printed_location
= true;
587 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
589 uint16_t control_port
, data_port
;
590 const char *host
, *relative_path
, *protocol_str
;
591 enum lttng_trace_archive_location_relay_protocol_type protocol
;
593 /* Fetch all relay location parameters. */
594 status
= lttng_trace_archive_location_relay_get_protocol_type(
595 location
, &protocol
);
596 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
601 status
= lttng_trace_archive_location_relay_get_host(
603 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
608 status
= lttng_trace_archive_location_relay_get_control_port(
609 location
, &control_port
);
610 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
615 status
= lttng_trace_archive_location_relay_get_data_port(
616 location
, &data_port
);
617 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
622 status
= lttng_trace_archive_location_relay_get_relative_path(
623 location
, &relative_path
);
624 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
630 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
631 protocol_str
= "tcp";
634 protocol_str
= "unknown";
638 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
639 PRIu16
"]", protocol_str
, host
,
640 relative_path
, control_port
, data_port
);
641 printed_location
= true;
648 if (!printed_location
) {
649 MSG(" at an unknown location");