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