Save/load: support session trace format
[lttng-tools.git] / src / bin / lttng / utils.cpp
CommitLineData
f3ed775e 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
f3ed775e 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
f3ed775e 5 *
f3ed775e
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
026a8516 9#include <arpa/inet.h>
679b4943 10#include <ctype.h>
026a8516 11#include <inttypes.h>
3badf2bf 12#include <limits.h>
8960e9cd 13#include <netinet/in.h>
026a8516
JR
14#include <signal.h>
15#include <stdlib.h>
16#include <sys/socket.h>
17#include <sys/types.h>
18#include <sys/wait.h>
4ba92f18 19#include <unistd.h>
f3ed775e 20
c9e313bc
SM
21#include <common/error.hpp>
22#include <common/utils.hpp>
23#include <common/defaults.hpp>
f3ed775e 24
c9e313bc
SM
25#include "conf.hpp"
26#include "utils.hpp"
27#include "command.hpp"
f3ed775e 28
4fd2697f
FD
29static const char *str_all = "ALL";
30static const char *str_tracepoint = "Tracepoint";
31static const char *str_syscall = "Syscall";
32static const char *str_probe = "Probe";
33static const char *str_userspace_probe = "Userspace Probe";
34static const char *str_function = "Function";
b9dfb167 35
1dac0189
PPM
36static
37char *_get_session_name(int quiet)
f3ed775e 38{
4f00620d
JG
39 const char *path;
40 char *session_name = NULL;
f3ed775e
DG
41
42 /* Get path to config file */
feb0f3e5 43 path = utils_get_home_dir();
f3ed775e
DG
44 if (path == NULL) {
45 goto error;
46 }
47
48 /* Get session name from config */
1dac0189
PPM
49 session_name = quiet ? config_read_session_name_quiet(path) :
50 config_read_session_name(path);
f3ed775e 51 if (session_name == NULL) {
58a97671 52 goto error;
f3ed775e
DG
53 }
54
3183dbb0 55 DBG2("Config file path found: %s", path);
cd80958d 56 DBG("Session name found: %s", session_name);
f3ed775e 57 return session_name;
3183dbb0
DG
58
59error:
60 return NULL;
f3ed775e 61}
679b4943 62
1dac0189
PPM
63/*
64 * get_session_name
65 *
66 * Return allocated string with the session name found in the config
67 * directory.
68 */
69char *get_session_name(void)
70{
71 return _get_session_name(0);
72}
73
74/*
75 * get_session_name_quiet (no warnings/errors emitted)
76 *
77 * Return allocated string with the session name found in the config
78 * directory.
79 */
80char *get_session_name_quiet(void)
81{
82 return _get_session_name(1);
83}
84
3c9bd23c
SM
85/*
86 * list_commands
87 *
88 * List commands line by line. This is mostly for bash auto completion and to
89 * avoid difficult parsing.
90 */
91void list_commands(struct cmd_struct *commands, FILE *ofp)
92{
93 int i = 0;
94 struct cmd_struct *cmd = NULL;
95
96 cmd = &commands[i];
97 while (cmd->name != NULL) {
98 fprintf(ofp, "%s\n", cmd->name);
99 i++;
100 cmd = &commands[i];
101 }
102}
679b4943
SM
103
104/*
105 * list_cmd_options
106 *
107 * Prints a simple list of the options available to a command. This is intended
108 * to be easily parsed for bash completion.
109 */
110void list_cmd_options(FILE *ofp, struct poptOption *options)
111{
112 int i;
113 struct poptOption *option = NULL;
114
115 for (i = 0; options[i].longName != NULL; i++) {
116 option = &options[i];
117
118 fprintf(ofp, "--%s\n", option->longName);
119
120 if (isprint(option->shortName)) {
121 fprintf(ofp, "-%c\n", option->shortName);
122 }
123 }
124}
8ce58bad 125
b083f028
JR
126/*
127 * Same as list_cmd_options, but for options specified for argpar.
128 */
129void list_cmd_options_argpar(FILE *ofp, const struct argpar_opt_descr *options)
130{
131 int i;
132
133 for (i = 0; options[i].long_name != NULL; i++) {
134 const struct argpar_opt_descr *option = &options[i];
135
136 fprintf(ofp, "--%s\n", option->long_name);
137
138 if (isprint(option->short_name)) {
139 fprintf(ofp, "-%c\n", option->short_name);
140 }
141 }
142}
143
8ce58bad
MD
144/*
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).
148 */
149#if defined(__i386) || defined(__x86_64)
150static inline
151unsigned int fls_u32(uint32_t x)
152{
153 int r;
154
155 asm("bsrl %1,%0\n\t"
156 "jnz 1f\n\t"
157 "movl $-1,%0\n\t"
158 "1:\n\t"
159 : "=r" (r) : "rm" (x));
160 return r + 1;
161}
162#define HAS_FLS_U32
163#endif
164
a1e4ab8b 165#if defined(__x86_64) && defined(__LP64__)
8ce58bad
MD
166static inline
167unsigned int fls_u64(uint64_t x)
168{
169 long r;
170
171 asm("bsrq %1,%0\n\t"
172 "jnz 1f\n\t"
173 "movq $-1,%0\n\t"
174 "1:\n\t"
175 : "=r" (r) : "rm" (x));
176 return r + 1;
177}
178#define HAS_FLS_U64
179#endif
180
181#ifndef HAS_FLS_U64
182static __attribute__((unused))
183unsigned int fls_u64(uint64_t x)
184{
185 unsigned int r = 64;
186
187 if (!x)
188 return 0;
189
190 if (!(x & 0xFFFFFFFF00000000ULL)) {
191 x <<= 32;
192 r -= 32;
193 }
194 if (!(x & 0xFFFF000000000000ULL)) {
195 x <<= 16;
196 r -= 16;
197 }
198 if (!(x & 0xFF00000000000000ULL)) {
199 x <<= 8;
200 r -= 8;
201 }
202 if (!(x & 0xF000000000000000ULL)) {
203 x <<= 4;
204 r -= 4;
205 }
206 if (!(x & 0xC000000000000000ULL)) {
207 x <<= 2;
208 r -= 2;
209 }
210 if (!(x & 0x8000000000000000ULL)) {
211 x <<= 1;
212 r -= 1;
213 }
214 return r;
215}
216#endif
217
218#ifndef HAS_FLS_U32
219static __attribute__((unused))
220unsigned int fls_u32(uint32_t x)
221{
222 unsigned int r = 32;
223
224 if (!x)
225 return 0;
226 if (!(x & 0xFFFF0000U)) {
227 x <<= 16;
228 r -= 16;
229 }
230 if (!(x & 0xFF000000U)) {
231 x <<= 8;
232 r -= 8;
233 }
234 if (!(x & 0xF0000000U)) {
235 x <<= 4;
236 r -= 4;
237 }
238 if (!(x & 0xC0000000U)) {
239 x <<= 2;
240 r -= 2;
241 }
242 if (!(x & 0x80000000U)) {
243 x <<= 1;
244 r -= 1;
245 }
246 return r;
247}
248#endif
249
250static
251unsigned int fls_ulong(unsigned long x)
252{
253#if (CAA_BITS_PER_LONG == 32)
254 return fls_u32(x);
255#else
256 return fls_u64(x);
257#endif
258}
259
260/*
261 * Return the minimum order for which x <= (1UL << order).
262 * Return -1 if x is 0.
263 */
264int get_count_order_u32(uint32_t x)
265{
266 if (!x)
267 return -1;
268
269 return fls_u32(x - 1);
270}
271
272/*
273 * Return the minimum order for which x <= (1UL << order).
274 * Return -1 if x is 0.
275 */
276int get_count_order_u64(uint64_t x)
277{
278 if (!x)
279 return -1;
280
281 return fls_u64(x - 1);
282}
283
284/*
285 * Return the minimum order for which x <= (1UL << order).
286 * Return -1 if x is 0.
287 */
288int get_count_order_ulong(unsigned long x)
289{
290 if (!x)
291 return -1;
292
293 return fls_ulong(x - 1);
294}
b9dfb167 295
4fd2697f
FD
296const char *get_event_type_str(enum lttng_event_type type)
297{
298 const char *str_event_type;
299
300 switch (type) {
301 case LTTNG_EVENT_ALL:
302 str_event_type = str_all;
303 break;
304 case LTTNG_EVENT_TRACEPOINT:
305 str_event_type = str_tracepoint;
306 break;
307 case LTTNG_EVENT_SYSCALL:
308 str_event_type = str_syscall;
309 break;
310 case LTTNG_EVENT_PROBE:
311 str_event_type = str_probe;
312 break;
313 case LTTNG_EVENT_USERSPACE_PROBE:
314 str_event_type = str_userspace_probe;
315 break;
316 case LTTNG_EVENT_FUNCTION:
317 str_event_type = str_function;
318 break;
319 default:
320 /* Should not have an unknown event type or else define it. */
a0377dfe 321 abort();
4fd2697f
FD
322 }
323
324 return str_event_type;
325}
326
8960e9cd
DG
327/*
328 * Spawn a lttng relayd daemon by forking and execv.
329 */
330int spawn_relayd(const char *pathname, int port)
331{
332 int ret = 0;
333 pid_t pid;
334 char url[255];
335
336 if (!port) {
337 port = DEFAULT_NETWORK_VIEWER_PORT;
338 }
339
340 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
341 if (ret < 0) {
342 goto end;
343 }
344
345 MSG("Spawning a relayd daemon");
346 pid = fork();
347 if (pid == 0) {
348 /*
349 * Spawn session daemon and tell
350 * it to signal us when ready.
351 */
026a8516 352 execlp(pathname, "lttng-relayd", "-L", url, "--daemonize", NULL);
8960e9cd
DG
353 /* execlp only returns if error happened */
354 if (errno == ENOENT) {
355 ERR("No relayd found. Use --relayd-path.");
356 } else {
6f04ed72 357 PERROR("execlp");
8960e9cd 358 }
026a8516 359 kill(getppid(), SIGTERM); /* wake parent */
8960e9cd
DG
360 exit(EXIT_FAILURE);
361 } else if (pid > 0) {
026a8516
JR
362 /*
363 * In daemon mode (--daemonize), lttng-relayd only exits when
364 * it's ready to accept commands.
365 */
366 for (;;) {
367 int status;
368 pid_t wait_pid_ret = waitpid(pid, &status, 0);
369
370 if (wait_pid_ret < 0) {
371 if (errno == EINTR) {
372 continue;
373 }
374 PERROR("waitpid");
375 ret = -errno;
376 goto end;
377 }
378
379 if (WIFSIGNALED(status)) {
380 ERR("Relay daemon was killed by signal %d", WTERMSIG(status));
381 ret = -1;
382 goto end;
383 } else if (WIFEXITED(status)) {
384 DBG("Relay daemon terminated normally (exit status: %d)",
385 WEXITSTATUS(status));
386
387 if (WEXITSTATUS(status) != 0) {
388 ERR("Relay daemon terminated with an error (exit status: %d)",
389 WEXITSTATUS(status));
390 ret = -1;
391 goto end;
392 }
393 break;
394 }
395 }
396
8960e9cd
DG
397 goto end;
398 } else {
6f04ed72 399 PERROR("fork");
8960e9cd
DG
400 ret = -1;
401 goto end;
402 }
403
404end:
405 return ret;
406}
407
408/*
409 * Check if relayd is alive.
410 *
411 * Return 1 if found else 0 if NOT found. Negative value on error.
412 */
413int check_relayd(void)
414{
415 int ret, fd;
416 struct sockaddr_in sin;
417
418 fd = socket(AF_INET, SOCK_STREAM, 0);
419 if (fd < 0) {
6f04ed72 420 PERROR("socket check relayd");
dd02a4c1
DG
421 ret = -1;
422 goto error_socket;
8960e9cd
DG
423 }
424
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);
428 if (ret < 1) {
6f04ed72 429 PERROR("inet_pton check relayd");
dd02a4c1 430 ret = -1;
8960e9cd
DG
431 goto error;
432 }
433
434 /*
435 * A successful connect means the relayd exists thus returning 0 else a
436 * negative value means it does NOT exists.
437 */
56efeab3 438 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
8960e9cd
DG
439 if (ret < 0) {
440 /* Not found. */
441 ret = 0;
442 } else {
443 /* Already spawned. */
444 ret = 1;
445 }
446
8960e9cd 447error:
dd02a4c1 448 if (close(fd) < 0) {
6f04ed72 449 PERROR("close relayd fd");
dd02a4c1
DG
450 }
451error_socket:
452 return ret;
8960e9cd 453}
3ecec76a 454
3533d06b
JG
455int print_missing_or_multiple_domains(unsigned int domain_count,
456 bool include_agent_domains)
3ecec76a
PP
457{
458 int ret = 0;
459
3533d06b
JG
460 if (domain_count == 0) {
461 ERR("Please specify a domain (--kernel/--userspace%s).",
462 include_agent_domains ?
463 "/--jul/--log4j/--python" :
464 "");
3ecec76a 465 ret = -1;
3533d06b
JG
466 } else if (domain_count > 1) {
467 ERR("Only one domain must be specified.");
3ecec76a
PP
468 ret = -1;
469 }
470
471 return ret;
472}
20fb9e02
JD
473
474/*
475 * Get the discarded events and lost packet counts.
476 */
477void print_session_stats(const char *session_name)
478{
58f237ca
JG
479 char *str;
480 const int ret = get_session_stats_str(session_name, &str);
481
482 if (ret >= 0 && str) {
483 MSG("%s", str);
484 free(str);
485 }
486}
487
488int get_session_stats_str(const char *session_name, char **out_str)
489{
490 int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
092545c3
SM
491 struct lttng_domain *domains = NULL;
492 struct lttng_channel *channels = NULL;
3e8f2238
JG
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;
58f237ca
JG
496 char *stats_str = NULL;
497 bool print_discarded_events = false, print_lost_packets = false;
3e8f2238
JG
498
499 count = lttng_list_sessions(&sessions);
500 if (count < 1) {
501 ERR("Failed to retrieve session descriptions while printing session statistics.");
58f237ca 502 ret = -1;
3e8f2238
JG
503 goto end;
504 }
505
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];
510 break;
511 }
512 }
513 if (!selected_session) {
514 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
58f237ca 515 ret = -1;
3e8f2238
JG
516 goto end;
517 }
20fb9e02
JD
518
519 nb_domains = lttng_list_domains(session_name, &domains);
520 if (nb_domains < 0) {
58f237ca 521 ret = -1;
20fb9e02
JD
522 goto end;
523 }
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]);
527
528 if (!handle) {
3e8f2238 529 ERR("Failed to create session handle while printing session statistics.");
58f237ca 530 ret = -1;
20fb9e02
JD
531 goto end;
532 }
533
092545c3
SM
534 free(channels);
535 channels = NULL;
20fb9e02
JD
536 count = lttng_list_channels(handle, &channels);
537 for (channel_idx = 0; channel_idx < count; channel_idx++) {
3e8f2238 538 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
539 struct lttng_channel *channel = &channels[channel_idx];
540
541 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 542 &discarded_events);
20fb9e02
JD
543 if (ret) {
544 ERR("Failed to retrieve discarded event count from channel %s",
545 channel->name);
546 }
547
548 ret = lttng_channel_get_lost_packet_count(channel,
3e8f2238 549 &lost_packets);
20fb9e02
JD
550 if (ret) {
551 ERR("Failed to retrieve lost packet count from channel %s",
552 channel->name);
553 }
554
3e8f2238
JG
555 discarded_events_total += discarded_events;
556 lost_packets_total += lost_packets;
20fb9e02
JD
557 }
558 lttng_destroy_handle(handle);
559 }
58f237ca
JG
560
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;
565
566 if (print_discarded_events && print_lost_packets) {
567 ret = asprintf(&stats_str,
568 "Warning: %" PRIu64
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,
575 "Warning: %" PRIu64
576 " events were discarded, please refer to "
20fb9e02 577 "the documentation on channel configuration.",
3e8f2238 578 discarded_events_total);
58f237ca
JG
579 } else if (print_lost_packets) {
580 ret = asprintf(&stats_str,
581 "Warning: %" PRIu64
582 " packets were lost, please refer to "
20fb9e02 583 "the documentation on channel configuration.",
3e8f2238 584 lost_packets_total);
58f237ca
JG
585 } else {
586 ret = 0;
20fb9e02
JD
587 }
588
58f237ca
JG
589 if (ret < 0) {
590 ERR("Failed to format lost packet and discarded events statistics");
591 } else {
592 *out_str = stats_str;
593 ret = 0;
594 }
20fb9e02 595end:
3e8f2238 596 free(sessions);
092545c3
SM
597 free(channels);
598 free(domains);
58f237ca 599 return ret;
20fb9e02 600}
4ba92f18 601
4fc83d94 602int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
603{
604 int ret;
605 char page_name[32];
606
607 ret = sprintf(page_name, "lttng-%s", cmd_name);
a0377dfe 608 LTTNG_ASSERT(ret > 0 && ret < 32);
4fc83d94
PP
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);
612 perror("exec");
613 }
4ba92f18 614
4fc83d94 615 return ret;
4ba92f18 616}
bbbfd849
JG
617
618int print_trace_archive_location(
619 const struct lttng_trace_archive_location *location,
620 const char *session_name)
621{
622 int ret = 0;
623 enum lttng_trace_archive_location_type location_type;
624 enum lttng_trace_archive_location_status status;
625 bool printed_location = false;
626
627 location_type = lttng_trace_archive_location_get_type(location);
628
629 _MSG("Trace chunk archive for session %s is now readable",
630 session_name);
631 switch (location_type) {
632 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
633 {
634 const char *absolute_path;
635
636 status = lttng_trace_archive_location_local_get_absolute_path(
637 location, &absolute_path);
638 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
639 ret = -1;
640 goto end;
641 }
642 MSG(" at %s", absolute_path);
643 printed_location = true;
644 break;
645 }
646 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
647 {
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;
651
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) {
656 ret = -1;
657 goto end;
658 }
659
660 status = lttng_trace_archive_location_relay_get_host(
661 location, &host);
662 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
663 ret = -1;
664 goto end;
665 }
666
667 status = lttng_trace_archive_location_relay_get_control_port(
668 location, &control_port);
669 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
670 ret = -1;
671 goto end;
672 }
673
674 status = lttng_trace_archive_location_relay_get_data_port(
675 location, &data_port);
676 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
677 ret = -1;
678 goto end;
679 }
680
681 status = lttng_trace_archive_location_relay_get_relative_path(
682 location, &relative_path);
683 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
684 ret = -1;
685 goto end;
686 }
687
688 switch (protocol) {
689 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
690 protocol_str = "tcp";
691 break;
692 default:
693 protocol_str = "unknown";
694 break;
695 }
696
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;
701 break;
702 }
703 default:
704 break;
705 }
706end:
707 if (!printed_location) {
708 MSG(" at an unknown location");
709 }
710 return ret;
711}
This page took 0.123251 seconds and 5 git commands to generate.