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