Save/load: support session trace format
[deliverable/lttng-tools.git] / src / bin / lttng / utils.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <arpa/inet.h>
10 #include <ctype.h>
11 #include <inttypes.h>
12 #include <limits.h>
13 #include <netinet/in.h>
14 #include <signal.h>
15 #include <stdlib.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include <common/error.hpp>
22 #include <common/utils.hpp>
23 #include <common/defaults.hpp>
24
25 #include "conf.hpp"
26 #include "utils.hpp"
27 #include "command.hpp"
28
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";
35
36 static
37 char *_get_session_name(int quiet)
38 {
39 const char *path;
40 char *session_name = NULL;
41
42 /* Get path to config file */
43 path = utils_get_home_dir();
44 if (path == NULL) {
45 goto error;
46 }
47
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) {
52 goto error;
53 }
54
55 DBG2("Config file path found: %s", path);
56 DBG("Session name found: %s", session_name);
57 return session_name;
58
59 error:
60 return NULL;
61 }
62
63 /*
64 * get_session_name
65 *
66 * Return allocated string with the session name found in the config
67 * directory.
68 */
69 char *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 */
80 char *get_session_name_quiet(void)
81 {
82 return _get_session_name(1);
83 }
84
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 */
91 void 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 }
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 */
110 void 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 }
125
126 /*
127 * Same as list_cmd_options, but for options specified for argpar.
128 */
129 void 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
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)
150 static inline
151 unsigned 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
165 #if defined(__x86_64) && defined(__LP64__)
166 static inline
167 unsigned 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
182 static __attribute__((unused))
183 unsigned 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
219 static __attribute__((unused))
220 unsigned 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
250 static
251 unsigned 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 */
264 int 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 */
276 int 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 */
288 int get_count_order_ulong(unsigned long x)
289 {
290 if (!x)
291 return -1;
292
293 return fls_ulong(x - 1);
294 }
295
296 const 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. */
321 abort();
322 }
323
324 return str_event_type;
325 }
326
327 /*
328 * Spawn a lttng relayd daemon by forking and execv.
329 */
330 int 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 */
352 execlp(pathname, "lttng-relayd", "-L", url, "--daemonize", NULL);
353 /* execlp only returns if error happened */
354 if (errno == ENOENT) {
355 ERR("No relayd found. Use --relayd-path.");
356 } else {
357 PERROR("execlp");
358 }
359 kill(getppid(), SIGTERM); /* wake parent */
360 exit(EXIT_FAILURE);
361 } else if (pid > 0) {
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
397 goto end;
398 } else {
399 PERROR("fork");
400 ret = -1;
401 goto end;
402 }
403
404 end:
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 */
413 int 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) {
420 PERROR("socket check relayd");
421 ret = -1;
422 goto error_socket;
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) {
429 PERROR("inet_pton check relayd");
430 ret = -1;
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 */
438 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
439 if (ret < 0) {
440 /* Not found. */
441 ret = 0;
442 } else {
443 /* Already spawned. */
444 ret = 1;
445 }
446
447 error:
448 if (close(fd) < 0) {
449 PERROR("close relayd fd");
450 }
451 error_socket:
452 return ret;
453 }
454
455 int print_missing_or_multiple_domains(unsigned int domain_count,
456 bool include_agent_domains)
457 {
458 int ret = 0;
459
460 if (domain_count == 0) {
461 ERR("Please specify a domain (--kernel/--userspace%s).",
462 include_agent_domains ?
463 "/--jul/--log4j/--python" :
464 "");
465 ret = -1;
466 } else if (domain_count > 1) {
467 ERR("Only one domain must be specified.");
468 ret = -1;
469 }
470
471 return ret;
472 }
473
474 /*
475 * Get the discarded events and lost packet counts.
476 */
477 void print_session_stats(const char *session_name)
478 {
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
488 int get_session_stats_str(const char *session_name, char **out_str)
489 {
490 int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
491 struct lttng_domain *domains = NULL;
492 struct lttng_channel *channels = NULL;
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;
496 char *stats_str = NULL;
497 bool print_discarded_events = false, print_lost_packets = false;
498
499 count = lttng_list_sessions(&sessions);
500 if (count < 1) {
501 ERR("Failed to retrieve session descriptions while printing session statistics.");
502 ret = -1;
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);
515 ret = -1;
516 goto end;
517 }
518
519 nb_domains = lttng_list_domains(session_name, &domains);
520 if (nb_domains < 0) {
521 ret = -1;
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) {
529 ERR("Failed to create session handle while printing session statistics.");
530 ret = -1;
531 goto end;
532 }
533
534 free(channels);
535 channels = NULL;
536 count = lttng_list_channels(handle, &channels);
537 for (channel_idx = 0; channel_idx < count; channel_idx++) {
538 uint64_t discarded_events = 0, lost_packets = 0;
539 struct lttng_channel *channel = &channels[channel_idx];
540
541 ret = lttng_channel_get_discarded_event_count(channel,
542 &discarded_events);
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,
549 &lost_packets);
550 if (ret) {
551 ERR("Failed to retrieve lost packet count from channel %s",
552 channel->name);
553 }
554
555 discarded_events_total += discarded_events;
556 lost_packets_total += lost_packets;
557 }
558 lttng_destroy_handle(handle);
559 }
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 "
577 "the documentation on channel configuration.",
578 discarded_events_total);
579 } else if (print_lost_packets) {
580 ret = asprintf(&stats_str,
581 "Warning: %" PRIu64
582 " packets were lost, please refer to "
583 "the documentation on channel configuration.",
584 lost_packets_total);
585 } else {
586 ret = 0;
587 }
588
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 }
595 end:
596 free(sessions);
597 free(channels);
598 free(domains);
599 return ret;
600 }
601
602 int show_cmd_help(const char *cmd_name, const char *help_msg)
603 {
604 int ret;
605 char page_name[32];
606
607 ret = sprintf(page_name, "lttng-%s", cmd_name);
608 LTTNG_ASSERT(ret > 0 && ret < 32);
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 }
614
615 return ret;
616 }
617
618 int 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 }
706 end:
707 if (!printed_location) {
708 MSG(" at an unknown location");
709 }
710 return ret;
711 }
This page took 0.045493 seconds and 5 git commands to generate.