Cleanup: rename `get_domain_str()` -> `lttng_domain_type_str()`
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <limits.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <signal.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <inttypes.h>
19 #include <unistd.h>
20
21 #include <common/error.h>
22 #include <common/utils.h>
23 #include <common/defaults.h>
24
25 #include "conf.h"
26 #include "utils.h"
27 #include "command.h"
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 assert(0);
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, 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 goto end;
363 } else {
364 PERROR("fork");
365 ret = -1;
366 goto end;
367 }
368
369 end:
370 return ret;
371 }
372
373 /*
374 * Check if relayd is alive.
375 *
376 * Return 1 if found else 0 if NOT found. Negative value on error.
377 */
378 int check_relayd(void)
379 {
380 int ret, fd;
381 struct sockaddr_in sin;
382
383 fd = socket(AF_INET, SOCK_STREAM, 0);
384 if (fd < 0) {
385 PERROR("socket check relayd");
386 ret = -1;
387 goto error_socket;
388 }
389
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);
393 if (ret < 1) {
394 PERROR("inet_pton check relayd");
395 ret = -1;
396 goto error;
397 }
398
399 /*
400 * A successful connect means the relayd exists thus returning 0 else a
401 * negative value means it does NOT exists.
402 */
403 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
404 if (ret < 0) {
405 /* Not found. */
406 ret = 0;
407 } else {
408 /* Already spawned. */
409 ret = 1;
410 }
411
412 error:
413 if (close(fd) < 0) {
414 PERROR("close relayd fd");
415 }
416 error_socket:
417 return ret;
418 }
419
420 int print_missing_or_multiple_domains(unsigned int domain_count,
421 bool include_agent_domains)
422 {
423 int ret = 0;
424
425 if (domain_count == 0) {
426 ERR("Please specify a domain (--kernel/--userspace%s).",
427 include_agent_domains ?
428 "/--jul/--log4j/--python" :
429 "");
430 ret = -1;
431 } else if (domain_count > 1) {
432 ERR("Only one domain must be specified.");
433 ret = -1;
434 }
435
436 return ret;
437 }
438
439 /*
440 * Get the discarded events and lost packet counts.
441 */
442 void print_session_stats(const char *session_name)
443 {
444 char *str;
445 const int ret = get_session_stats_str(session_name, &str);
446
447 if (ret >= 0 && str) {
448 MSG("%s", str);
449 free(str);
450 }
451 }
452
453 int get_session_stats_str(const char *session_name, char **out_str)
454 {
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;
463
464 count = lttng_list_sessions(&sessions);
465 if (count < 1) {
466 ERR("Failed to retrieve session descriptions while printing session statistics.");
467 ret = -1;
468 goto end;
469 }
470
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];
475 break;
476 }
477 }
478 if (!selected_session) {
479 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
480 ret = -1;
481 goto end;
482 }
483
484 nb_domains = lttng_list_domains(session_name, &domains);
485 if (nb_domains < 0) {
486 ret = -1;
487 goto end;
488 }
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]);
492
493 if (!handle) {
494 ERR("Failed to create session handle while printing session statistics.");
495 ret = -1;
496 goto end;
497 }
498
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];
503
504 ret = lttng_channel_get_discarded_event_count(channel,
505 &discarded_events);
506 if (ret) {
507 ERR("Failed to retrieve discarded event count from channel %s",
508 channel->name);
509 }
510
511 ret = lttng_channel_get_lost_packet_count(channel,
512 &lost_packets);
513 if (ret) {
514 ERR("Failed to retrieve lost packet count from channel %s",
515 channel->name);
516 }
517
518 discarded_events_total += discarded_events;
519 lost_packets_total += lost_packets;
520 }
521 lttng_destroy_handle(handle);
522 }
523
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;
528
529 if (print_discarded_events && print_lost_packets) {
530 ret = asprintf(&stats_str,
531 "Warning: %" PRIu64
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,
538 "Warning: %" PRIu64
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,
544 "Warning: %" PRIu64
545 " packets were lost, please refer to "
546 "the documentation on channel configuration.",
547 lost_packets_total);
548 } else {
549 ret = 0;
550 }
551
552 if (ret < 0) {
553 ERR("Failed to format lost packet and discarded events statistics");
554 } else {
555 *out_str = stats_str;
556 ret = 0;
557 }
558 end:
559 free(sessions);
560 return ret;
561 }
562
563 int show_cmd_help(const char *cmd_name, const char *help_msg)
564 {
565 int ret;
566 char page_name[32];
567
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);
573 perror("exec");
574 }
575
576 return ret;
577 }
578
579 int print_trace_archive_location(
580 const struct lttng_trace_archive_location *location,
581 const char *session_name)
582 {
583 int ret = 0;
584 enum lttng_trace_archive_location_type location_type;
585 enum lttng_trace_archive_location_status status;
586 bool printed_location = false;
587
588 location_type = lttng_trace_archive_location_get_type(location);
589
590 _MSG("Trace chunk archive for session %s is now readable",
591 session_name);
592 switch (location_type) {
593 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
594 {
595 const char *absolute_path;
596
597 status = lttng_trace_archive_location_local_get_absolute_path(
598 location, &absolute_path);
599 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
600 ret = -1;
601 goto end;
602 }
603 MSG(" at %s", absolute_path);
604 printed_location = true;
605 break;
606 }
607 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
608 {
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;
612
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) {
617 ret = -1;
618 goto end;
619 }
620
621 status = lttng_trace_archive_location_relay_get_host(
622 location, &host);
623 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
624 ret = -1;
625 goto end;
626 }
627
628 status = lttng_trace_archive_location_relay_get_control_port(
629 location, &control_port);
630 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
631 ret = -1;
632 goto end;
633 }
634
635 status = lttng_trace_archive_location_relay_get_data_port(
636 location, &data_port);
637 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
638 ret = -1;
639 goto end;
640 }
641
642 status = lttng_trace_archive_location_relay_get_relative_path(
643 location, &relative_path);
644 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
645 ret = -1;
646 goto end;
647 }
648
649 switch (protocol) {
650 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
651 protocol_str = "tcp";
652 break;
653 default:
654 protocol_str = "unknown";
655 break;
656 }
657
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;
662 break;
663 }
664 default:
665 break;
666 }
667 end:
668 if (!printed_location) {
669 MSG(" at an unknown location");
670 }
671 return ret;
672 }
This page took 0.066005 seconds and 4 git commands to generate.