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