Fix: statements with side-effects in assert statements
[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
4fd2697f
FD
29static const char *str_all = "ALL";
30static const char *str_tracepoint = "Tracepoint";
31static const char *str_syscall = "Syscall";
32static const char *str_probe = "Probe";
33static const char *str_userspace_probe = "Userspace Probe";
34static const char *str_function = "Function";
b9dfb167 35
1dac0189
PPM
36static
37char *_get_session_name(int quiet)
f3ed775e 38{
4f00620d
JG
39 const char *path;
40 char *session_name = NULL;
f3ed775e
DG
41
42 /* Get path to config file */
feb0f3e5 43 path = utils_get_home_dir();
f3ed775e
DG
44 if (path == NULL) {
45 goto error;
46 }
47
48 /* Get session name from config */
1dac0189
PPM
49 session_name = quiet ? config_read_session_name_quiet(path) :
50 config_read_session_name(path);
f3ed775e 51 if (session_name == NULL) {
58a97671 52 goto error;
f3ed775e
DG
53 }
54
3183dbb0 55 DBG2("Config file path found: %s", path);
cd80958d 56 DBG("Session name found: %s", session_name);
f3ed775e 57 return session_name;
3183dbb0
DG
58
59error:
60 return NULL;
f3ed775e 61}
679b4943 62
1dac0189
PPM
63/*
64 * get_session_name
65 *
66 * Return allocated string with the session name found in the config
67 * directory.
68 */
69char *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 */
80char *get_session_name_quiet(void)
81{
82 return _get_session_name(1);
83}
84
3c9bd23c
SM
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 */
91void 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}
679b4943
SM
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 */
110void 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}
8ce58bad 125
b083f028
JR
126/*
127 * Same as list_cmd_options, but for options specified for argpar.
128 */
129void 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
8ce58bad
MD
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)
150static inline
151unsigned 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
a1e4ab8b 165#if defined(__x86_64) && defined(__LP64__)
8ce58bad
MD
166static inline
167unsigned 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
182static __attribute__((unused))
183unsigned 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
219static __attribute__((unused))
220unsigned 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
250static
251unsigned 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 */
264int 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 */
276int 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 */
288int get_count_order_ulong(unsigned long x)
289{
290 if (!x)
291 return -1;
292
293 return fls_ulong(x - 1);
294}
b9dfb167 295
4fd2697f
FD
296const 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
8960e9cd
DG
327/*
328 * Spawn a lttng relayd daemon by forking and execv.
329 */
330int 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 {
6f04ed72 357 PERROR("execlp");
8960e9cd
DG
358 }
359 kill(getppid(), SIGTERM); /* wake parent */
360 exit(EXIT_FAILURE);
361 } else if (pid > 0) {
362 goto end;
363 } else {
6f04ed72 364 PERROR("fork");
8960e9cd
DG
365 ret = -1;
366 goto end;
367 }
368
369end:
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 */
378int 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) {
6f04ed72 385 PERROR("socket check relayd");
dd02a4c1
DG
386 ret = -1;
387 goto error_socket;
8960e9cd
DG
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) {
6f04ed72 394 PERROR("inet_pton check relayd");
dd02a4c1 395 ret = -1;
8960e9cd
DG
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 */
56efeab3 403 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
8960e9cd
DG
404 if (ret < 0) {
405 /* Not found. */
406 ret = 0;
407 } else {
408 /* Already spawned. */
409 ret = 1;
410 }
411
8960e9cd 412error:
dd02a4c1 413 if (close(fd) < 0) {
6f04ed72 414 PERROR("close relayd fd");
dd02a4c1
DG
415 }
416error_socket:
417 return ret;
8960e9cd 418}
3ecec76a 419
3533d06b
JG
420int print_missing_or_multiple_domains(unsigned int domain_count,
421 bool include_agent_domains)
3ecec76a
PP
422{
423 int ret = 0;
424
3533d06b
JG
425 if (domain_count == 0) {
426 ERR("Please specify a domain (--kernel/--userspace%s).",
427 include_agent_domains ?
428 "/--jul/--log4j/--python" :
429 "");
3ecec76a 430 ret = -1;
3533d06b
JG
431 } else if (domain_count > 1) {
432 ERR("Only one domain must be specified.");
3ecec76a
PP
433 ret = -1;
434 }
435
436 return ret;
437}
20fb9e02
JD
438
439/*
440 * Get the discarded events and lost packet counts.
441 */
442void print_session_stats(const char *session_name)
443{
58f237ca
JG
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
453int get_session_stats_str(const char *session_name, char **out_str)
454{
455 int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
092545c3
SM
456 struct lttng_domain *domains = NULL;
457 struct lttng_channel *channels = NULL;
3e8f2238
JG
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;
58f237ca
JG
461 char *stats_str = NULL;
462 bool print_discarded_events = false, print_lost_packets = false;
3e8f2238
JG
463
464 count = lttng_list_sessions(&sessions);
465 if (count < 1) {
466 ERR("Failed to retrieve session descriptions while printing session statistics.");
58f237ca 467 ret = -1;
3e8f2238
JG
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);
58f237ca 480 ret = -1;
3e8f2238
JG
481 goto end;
482 }
20fb9e02
JD
483
484 nb_domains = lttng_list_domains(session_name, &domains);
485 if (nb_domains < 0) {
58f237ca 486 ret = -1;
20fb9e02
JD
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) {
3e8f2238 494 ERR("Failed to create session handle while printing session statistics.");
58f237ca 495 ret = -1;
20fb9e02
JD
496 goto end;
497 }
498
092545c3
SM
499 free(channels);
500 channels = NULL;
20fb9e02
JD
501 count = lttng_list_channels(handle, &channels);
502 for (channel_idx = 0; channel_idx < count; channel_idx++) {
3e8f2238 503 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
504 struct lttng_channel *channel = &channels[channel_idx];
505
506 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 507 &discarded_events);
20fb9e02
JD
508 if (ret) {
509 ERR("Failed to retrieve discarded event count from channel %s",
510 channel->name);
511 }
512
513 ret = lttng_channel_get_lost_packet_count(channel,
3e8f2238 514 &lost_packets);
20fb9e02
JD
515 if (ret) {
516 ERR("Failed to retrieve lost packet count from channel %s",
517 channel->name);
518 }
519
3e8f2238
JG
520 discarded_events_total += discarded_events;
521 lost_packets_total += lost_packets;
20fb9e02
JD
522 }
523 lttng_destroy_handle(handle);
524 }
58f237ca
JG
525
526 print_discarded_events = discarded_events_total > 0 &&
527 !selected_session->snapshot_mode;
528 print_lost_packets = lost_packets_total > 0 &&
529 !selected_session->snapshot_mode;
530
531 if (print_discarded_events && print_lost_packets) {
532 ret = asprintf(&stats_str,
533 "Warning: %" PRIu64
534 " events were discarded and %" PRIu64
535 " packets were lost, please refer to "
536 "the documentation on channel configuration.",
537 discarded_events_total, lost_packets_total);
538 } else if (print_discarded_events) {
539 ret = asprintf(&stats_str,
540 "Warning: %" PRIu64
541 " events were discarded, please refer to "
20fb9e02 542 "the documentation on channel configuration.",
3e8f2238 543 discarded_events_total);
58f237ca
JG
544 } else if (print_lost_packets) {
545 ret = asprintf(&stats_str,
546 "Warning: %" PRIu64
547 " packets were lost, please refer to "
20fb9e02 548 "the documentation on channel configuration.",
3e8f2238 549 lost_packets_total);
58f237ca
JG
550 } else {
551 ret = 0;
20fb9e02
JD
552 }
553
58f237ca
JG
554 if (ret < 0) {
555 ERR("Failed to format lost packet and discarded events statistics");
556 } else {
557 *out_str = stats_str;
558 ret = 0;
559 }
20fb9e02 560end:
3e8f2238 561 free(sessions);
092545c3
SM
562 free(channels);
563 free(domains);
58f237ca 564 return ret;
20fb9e02 565}
4ba92f18 566
4fc83d94 567int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
568{
569 int ret;
570 char page_name[32];
571
572 ret = sprintf(page_name, "lttng-%s", cmd_name);
573 assert(ret > 0 && ret < 32);
4fc83d94
PP
574 ret = utils_show_help(1, page_name, help_msg);
575 if (ret && !help_msg) {
576 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
577 perror("exec");
578 }
4ba92f18 579
4fc83d94 580 return ret;
4ba92f18 581}
bbbfd849
JG
582
583int print_trace_archive_location(
584 const struct lttng_trace_archive_location *location,
585 const char *session_name)
586{
587 int ret = 0;
588 enum lttng_trace_archive_location_type location_type;
589 enum lttng_trace_archive_location_status status;
590 bool printed_location = false;
591
592 location_type = lttng_trace_archive_location_get_type(location);
593
594 _MSG("Trace chunk archive for session %s is now readable",
595 session_name);
596 switch (location_type) {
597 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
598 {
599 const char *absolute_path;
600
601 status = lttng_trace_archive_location_local_get_absolute_path(
602 location, &absolute_path);
603 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
604 ret = -1;
605 goto end;
606 }
607 MSG(" at %s", absolute_path);
608 printed_location = true;
609 break;
610 }
611 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
612 {
613 uint16_t control_port, data_port;
614 const char *host, *relative_path, *protocol_str;
615 enum lttng_trace_archive_location_relay_protocol_type protocol;
616
617 /* Fetch all relay location parameters. */
618 status = lttng_trace_archive_location_relay_get_protocol_type(
619 location, &protocol);
620 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
621 ret = -1;
622 goto end;
623 }
624
625 status = lttng_trace_archive_location_relay_get_host(
626 location, &host);
627 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
628 ret = -1;
629 goto end;
630 }
631
632 status = lttng_trace_archive_location_relay_get_control_port(
633 location, &control_port);
634 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
635 ret = -1;
636 goto end;
637 }
638
639 status = lttng_trace_archive_location_relay_get_data_port(
640 location, &data_port);
641 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
642 ret = -1;
643 goto end;
644 }
645
646 status = lttng_trace_archive_location_relay_get_relative_path(
647 location, &relative_path);
648 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
649 ret = -1;
650 goto end;
651 }
652
653 switch (protocol) {
654 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
655 protocol_str = "tcp";
656 break;
657 default:
658 protocol_str = "unknown";
659 break;
660 }
661
662 MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
663 PRIu16 "]", protocol_str, host,
664 relative_path, control_port, data_port);
665 printed_location = true;
666 break;
667 }
668 default:
669 break;
670 }
671end:
672 if (!printed_location) {
673 MSG(" at an unknown location");
674 }
675 return ret;
676}
This page took 0.085532 seconds and 4 git commands to generate.