lttng: Add list-triggers command
[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
b083f028
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
138 for (i = 0; options[i].long_name != NULL; i++) {
139 const struct argpar_opt_descr *option = &options[i];
140
141 fprintf(ofp, "--%s\n", option->long_name);
142
143 if (isprint(option->short_name)) {
144 fprintf(ofp, "-%c\n", option->short_name);
145 }
146 }
147}
148
8ce58bad
MD
149/*
150 * fls: returns the position of the most significant bit.
151 * Returns 0 if no bit is set, else returns the position of the most
152 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
153 */
154#if defined(__i386) || defined(__x86_64)
155static inline
156unsigned int fls_u32(uint32_t x)
157{
158 int r;
159
160 asm("bsrl %1,%0\n\t"
161 "jnz 1f\n\t"
162 "movl $-1,%0\n\t"
163 "1:\n\t"
164 : "=r" (r) : "rm" (x));
165 return r + 1;
166}
167#define HAS_FLS_U32
168#endif
169
a1e4ab8b 170#if defined(__x86_64) && defined(__LP64__)
8ce58bad
MD
171static inline
172unsigned int fls_u64(uint64_t x)
173{
174 long r;
175
176 asm("bsrq %1,%0\n\t"
177 "jnz 1f\n\t"
178 "movq $-1,%0\n\t"
179 "1:\n\t"
180 : "=r" (r) : "rm" (x));
181 return r + 1;
182}
183#define HAS_FLS_U64
184#endif
185
186#ifndef HAS_FLS_U64
187static __attribute__((unused))
188unsigned int fls_u64(uint64_t x)
189{
190 unsigned int r = 64;
191
192 if (!x)
193 return 0;
194
195 if (!(x & 0xFFFFFFFF00000000ULL)) {
196 x <<= 32;
197 r -= 32;
198 }
199 if (!(x & 0xFFFF000000000000ULL)) {
200 x <<= 16;
201 r -= 16;
202 }
203 if (!(x & 0xFF00000000000000ULL)) {
204 x <<= 8;
205 r -= 8;
206 }
207 if (!(x & 0xF000000000000000ULL)) {
208 x <<= 4;
209 r -= 4;
210 }
211 if (!(x & 0xC000000000000000ULL)) {
212 x <<= 2;
213 r -= 2;
214 }
215 if (!(x & 0x8000000000000000ULL)) {
216 x <<= 1;
217 r -= 1;
218 }
219 return r;
220}
221#endif
222
223#ifndef HAS_FLS_U32
224static __attribute__((unused))
225unsigned int fls_u32(uint32_t x)
226{
227 unsigned int r = 32;
228
229 if (!x)
230 return 0;
231 if (!(x & 0xFFFF0000U)) {
232 x <<= 16;
233 r -= 16;
234 }
235 if (!(x & 0xFF000000U)) {
236 x <<= 8;
237 r -= 8;
238 }
239 if (!(x & 0xF0000000U)) {
240 x <<= 4;
241 r -= 4;
242 }
243 if (!(x & 0xC0000000U)) {
244 x <<= 2;
245 r -= 2;
246 }
247 if (!(x & 0x80000000U)) {
248 x <<= 1;
249 r -= 1;
250 }
251 return r;
252}
253#endif
254
255static
256unsigned int fls_ulong(unsigned long x)
257{
258#if (CAA_BITS_PER_LONG == 32)
259 return fls_u32(x);
260#else
261 return fls_u64(x);
262#endif
263}
264
265/*
266 * Return the minimum order for which x <= (1UL << order).
267 * Return -1 if x is 0.
268 */
269int get_count_order_u32(uint32_t x)
270{
271 if (!x)
272 return -1;
273
274 return fls_u32(x - 1);
275}
276
277/*
278 * Return the minimum order for which x <= (1UL << order).
279 * Return -1 if x is 0.
280 */
281int get_count_order_u64(uint64_t x)
282{
283 if (!x)
284 return -1;
285
286 return fls_u64(x - 1);
287}
288
289/*
290 * Return the minimum order for which x <= (1UL << order).
291 * Return -1 if x is 0.
292 */
293int get_count_order_ulong(unsigned long x)
294{
295 if (!x)
296 return -1;
297
298 return fls_ulong(x - 1);
299}
b9dfb167
DG
300
301const char *get_domain_str(enum lttng_domain_type domain)
302{
303 const char *str_dom;
304
305 switch (domain) {
306 case LTTNG_DOMAIN_KERNEL:
307 str_dom = str_kernel;
308 break;
309 case LTTNG_DOMAIN_UST:
310 str_dom = str_ust;
311 break;
312 case LTTNG_DOMAIN_JUL:
313 str_dom = str_jul;
314 break;
5cdb6027
DG
315 case LTTNG_DOMAIN_LOG4J:
316 str_dom = str_log4j;
317 break;
0e115563
DG
318 case LTTNG_DOMAIN_PYTHON:
319 str_dom = str_python;
320 break;
b9dfb167
DG
321 default:
322 /* Should not have an unknown domain or else define it. */
323 assert(0);
324 }
325
326 return str_dom;
327}
8960e9cd 328
4fd2697f
FD
329const char *get_event_type_str(enum lttng_event_type type)
330{
331 const char *str_event_type;
332
333 switch (type) {
334 case LTTNG_EVENT_ALL:
335 str_event_type = str_all;
336 break;
337 case LTTNG_EVENT_TRACEPOINT:
338 str_event_type = str_tracepoint;
339 break;
340 case LTTNG_EVENT_SYSCALL:
341 str_event_type = str_syscall;
342 break;
343 case LTTNG_EVENT_PROBE:
344 str_event_type = str_probe;
345 break;
346 case LTTNG_EVENT_USERSPACE_PROBE:
347 str_event_type = str_userspace_probe;
348 break;
349 case LTTNG_EVENT_FUNCTION:
350 str_event_type = str_function;
351 break;
352 default:
353 /* Should not have an unknown event type or else define it. */
354 assert(0);
355 }
356
357 return str_event_type;
358}
359
8960e9cd
DG
360/*
361 * Spawn a lttng relayd daemon by forking and execv.
362 */
363int spawn_relayd(const char *pathname, int port)
364{
365 int ret = 0;
366 pid_t pid;
367 char url[255];
368
369 if (!port) {
370 port = DEFAULT_NETWORK_VIEWER_PORT;
371 }
372
373 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
374 if (ret < 0) {
375 goto end;
376 }
377
378 MSG("Spawning a relayd daemon");
379 pid = fork();
380 if (pid == 0) {
381 /*
382 * Spawn session daemon and tell
383 * it to signal us when ready.
384 */
385 execlp(pathname, "lttng-relayd", "-L", url, NULL);
386 /* execlp only returns if error happened */
387 if (errno == ENOENT) {
388 ERR("No relayd found. Use --relayd-path.");
389 } else {
6f04ed72 390 PERROR("execlp");
8960e9cd
DG
391 }
392 kill(getppid(), SIGTERM); /* wake parent */
393 exit(EXIT_FAILURE);
394 } else if (pid > 0) {
395 goto end;
396 } else {
6f04ed72 397 PERROR("fork");
8960e9cd
DG
398 ret = -1;
399 goto end;
400 }
401
402end:
403 return ret;
404}
405
406/*
407 * Check if relayd is alive.
408 *
409 * Return 1 if found else 0 if NOT found. Negative value on error.
410 */
411int check_relayd(void)
412{
413 int ret, fd;
414 struct sockaddr_in sin;
415
416 fd = socket(AF_INET, SOCK_STREAM, 0);
417 if (fd < 0) {
6f04ed72 418 PERROR("socket check relayd");
dd02a4c1
DG
419 ret = -1;
420 goto error_socket;
8960e9cd
DG
421 }
422
423 sin.sin_family = AF_INET;
424 sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT);
425 ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
426 if (ret < 1) {
6f04ed72 427 PERROR("inet_pton check relayd");
dd02a4c1 428 ret = -1;
8960e9cd
DG
429 goto error;
430 }
431
432 /*
433 * A successful connect means the relayd exists thus returning 0 else a
434 * negative value means it does NOT exists.
435 */
56efeab3 436 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
8960e9cd
DG
437 if (ret < 0) {
438 /* Not found. */
439 ret = 0;
440 } else {
441 /* Already spawned. */
442 ret = 1;
443 }
444
8960e9cd 445error:
dd02a4c1 446 if (close(fd) < 0) {
6f04ed72 447 PERROR("close relayd fd");
dd02a4c1
DG
448 }
449error_socket:
450 return ret;
8960e9cd 451}
3ecec76a 452
3533d06b
JG
453int print_missing_or_multiple_domains(unsigned int domain_count,
454 bool include_agent_domains)
3ecec76a
PP
455{
456 int ret = 0;
457
3533d06b
JG
458 if (domain_count == 0) {
459 ERR("Please specify a domain (--kernel/--userspace%s).",
460 include_agent_domains ?
461 "/--jul/--log4j/--python" :
462 "");
3ecec76a 463 ret = -1;
3533d06b
JG
464 } else if (domain_count > 1) {
465 ERR("Only one domain must be specified.");
3ecec76a
PP
466 ret = -1;
467 }
468
469 return ret;
470}
20fb9e02
JD
471
472/*
473 * Get the discarded events and lost packet counts.
474 */
475void print_session_stats(const char *session_name)
476{
58f237ca
JG
477 char *str;
478 const int ret = get_session_stats_str(session_name, &str);
479
480 if (ret >= 0 && str) {
481 MSG("%s", str);
482 free(str);
483 }
484}
485
486int get_session_stats_str(const char *session_name, char **out_str)
487{
488 int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
20fb9e02
JD
489 struct lttng_domain *domains;
490 struct lttng_channel *channels;
3e8f2238
JG
491 uint64_t discarded_events_total = 0, lost_packets_total = 0;
492 struct lttng_session *sessions = NULL;
493 const struct lttng_session *selected_session = NULL;
58f237ca
JG
494 char *stats_str = NULL;
495 bool print_discarded_events = false, print_lost_packets = false;
3e8f2238
JG
496
497 count = lttng_list_sessions(&sessions);
498 if (count < 1) {
499 ERR("Failed to retrieve session descriptions while printing session statistics.");
58f237ca 500 ret = -1;
3e8f2238
JG
501 goto end;
502 }
503
504 /* Identify the currently-selected sessions. */
505 for (session_idx = 0; session_idx < count; session_idx++) {
506 if (!strcmp(session_name, sessions[session_idx].name)) {
507 selected_session = &sessions[session_idx];
508 break;
509 }
510 }
511 if (!selected_session) {
512 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
58f237ca 513 ret = -1;
3e8f2238
JG
514 goto end;
515 }
20fb9e02
JD
516
517 nb_domains = lttng_list_domains(session_name, &domains);
518 if (nb_domains < 0) {
58f237ca 519 ret = -1;
20fb9e02
JD
520 goto end;
521 }
522 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
523 struct lttng_handle *handle = lttng_create_handle(session_name,
524 &domains[domain_idx]);
525
526 if (!handle) {
3e8f2238 527 ERR("Failed to create session handle while printing session statistics.");
58f237ca 528 ret = -1;
20fb9e02
JD
529 goto end;
530 }
531
532 count = lttng_list_channels(handle, &channels);
533 for (channel_idx = 0; channel_idx < count; channel_idx++) {
3e8f2238 534 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
535 struct lttng_channel *channel = &channels[channel_idx];
536
537 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 538 &discarded_events);
20fb9e02
JD
539 if (ret) {
540 ERR("Failed to retrieve discarded event count from channel %s",
541 channel->name);
542 }
543
544 ret = lttng_channel_get_lost_packet_count(channel,
3e8f2238 545 &lost_packets);
20fb9e02
JD
546 if (ret) {
547 ERR("Failed to retrieve lost packet count from channel %s",
548 channel->name);
549 }
550
3e8f2238
JG
551 discarded_events_total += discarded_events;
552 lost_packets_total += lost_packets;
20fb9e02
JD
553 }
554 lttng_destroy_handle(handle);
555 }
58f237ca
JG
556
557 print_discarded_events = discarded_events_total > 0 &&
558 !selected_session->snapshot_mode;
559 print_lost_packets = lost_packets_total > 0 &&
560 !selected_session->snapshot_mode;
561
562 if (print_discarded_events && print_lost_packets) {
563 ret = asprintf(&stats_str,
564 "Warning: %" PRIu64
565 " events were discarded and %" PRIu64
566 " packets were lost, please refer to "
567 "the documentation on channel configuration.",
568 discarded_events_total, lost_packets_total);
569 } else if (print_discarded_events) {
570 ret = asprintf(&stats_str,
571 "Warning: %" PRIu64
572 " events were discarded, please refer to "
20fb9e02 573 "the documentation on channel configuration.",
3e8f2238 574 discarded_events_total);
58f237ca
JG
575 } else if (print_lost_packets) {
576 ret = asprintf(&stats_str,
577 "Warning: %" PRIu64
578 " packets were lost, please refer to "
20fb9e02 579 "the documentation on channel configuration.",
3e8f2238 580 lost_packets_total);
58f237ca
JG
581 } else {
582 ret = 0;
20fb9e02
JD
583 }
584
58f237ca
JG
585 if (ret < 0) {
586 ERR("Failed to format lost packet and discarded events statistics");
587 } else {
588 *out_str = stats_str;
589 ret = 0;
590 }
20fb9e02 591end:
3e8f2238 592 free(sessions);
58f237ca 593 return ret;
20fb9e02 594}
4ba92f18 595
4fc83d94 596int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
597{
598 int ret;
599 char page_name[32];
600
601 ret = sprintf(page_name, "lttng-%s", cmd_name);
602 assert(ret > 0 && ret < 32);
4fc83d94
PP
603 ret = utils_show_help(1, page_name, help_msg);
604 if (ret && !help_msg) {
605 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
606 perror("exec");
607 }
4ba92f18 608
4fc83d94 609 return ret;
4ba92f18 610}
bbbfd849
JG
611
612int print_trace_archive_location(
613 const struct lttng_trace_archive_location *location,
614 const char *session_name)
615{
616 int ret = 0;
617 enum lttng_trace_archive_location_type location_type;
618 enum lttng_trace_archive_location_status status;
619 bool printed_location = false;
620
621 location_type = lttng_trace_archive_location_get_type(location);
622
623 _MSG("Trace chunk archive for session %s is now readable",
624 session_name);
625 switch (location_type) {
626 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
627 {
628 const char *absolute_path;
629
630 status = lttng_trace_archive_location_local_get_absolute_path(
631 location, &absolute_path);
632 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
633 ret = -1;
634 goto end;
635 }
636 MSG(" at %s", absolute_path);
637 printed_location = true;
638 break;
639 }
640 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
641 {
642 uint16_t control_port, data_port;
643 const char *host, *relative_path, *protocol_str;
644 enum lttng_trace_archive_location_relay_protocol_type protocol;
645
646 /* Fetch all relay location parameters. */
647 status = lttng_trace_archive_location_relay_get_protocol_type(
648 location, &protocol);
649 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
650 ret = -1;
651 goto end;
652 }
653
654 status = lttng_trace_archive_location_relay_get_host(
655 location, &host);
656 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
657 ret = -1;
658 goto end;
659 }
660
661 status = lttng_trace_archive_location_relay_get_control_port(
662 location, &control_port);
663 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
664 ret = -1;
665 goto end;
666 }
667
668 status = lttng_trace_archive_location_relay_get_data_port(
669 location, &data_port);
670 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
671 ret = -1;
672 goto end;
673 }
674
675 status = lttng_trace_archive_location_relay_get_relative_path(
676 location, &relative_path);
677 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
678 ret = -1;
679 goto end;
680 }
681
682 switch (protocol) {
683 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
684 protocol_str = "tcp";
685 break;
686 default:
687 protocol_str = "unknown";
688 break;
689 }
690
691 MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
692 PRIu16 "]", protocol_str, host,
693 relative_path, control_port, data_port);
694 printed_location = true;
695 break;
696 }
697 default:
698 break;
699 }
700end:
701 if (!printed_location) {
702 MSG(" at an unknown location");
703 }
704 return ret;
705}
This page took 0.135631 seconds and 4 git commands to generate.