Fix: lttng: incorrect domain list printed when no domain is provided
[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{
3e8f2238 469 int count, nb_domains, domain_idx, channel_idx, session_idx;
20fb9e02
JD
470 struct lttng_domain *domains;
471 struct lttng_channel *channels;
3e8f2238
JG
472 uint64_t discarded_events_total = 0, lost_packets_total = 0;
473 struct lttng_session *sessions = NULL;
474 const struct lttng_session *selected_session = NULL;
475
476 count = lttng_list_sessions(&sessions);
477 if (count < 1) {
478 ERR("Failed to retrieve session descriptions while printing session statistics.");
479 goto end;
480 }
481
482 /* Identify the currently-selected sessions. */
483 for (session_idx = 0; session_idx < count; session_idx++) {
484 if (!strcmp(session_name, sessions[session_idx].name)) {
485 selected_session = &sessions[session_idx];
486 break;
487 }
488 }
489 if (!selected_session) {
490 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
491 goto end;
492 }
20fb9e02
JD
493
494 nb_domains = lttng_list_domains(session_name, &domains);
495 if (nb_domains < 0) {
496 goto end;
497 }
498 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
499 struct lttng_handle *handle = lttng_create_handle(session_name,
500 &domains[domain_idx]);
501
502 if (!handle) {
3e8f2238 503 ERR("Failed to create session handle while printing session statistics.");
20fb9e02
JD
504 goto end;
505 }
506
507 count = lttng_list_channels(handle, &channels);
508 for (channel_idx = 0; channel_idx < count; channel_idx++) {
509 int ret;
3e8f2238 510 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
511 struct lttng_channel *channel = &channels[channel_idx];
512
513 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 514 &discarded_events);
20fb9e02
JD
515 if (ret) {
516 ERR("Failed to retrieve discarded event count from channel %s",
517 channel->name);
518 }
519
520 ret = lttng_channel_get_lost_packet_count(channel,
3e8f2238 521 &lost_packets);
20fb9e02
JD
522 if (ret) {
523 ERR("Failed to retrieve lost packet count from channel %s",
524 channel->name);
525 }
526
3e8f2238
JG
527 discarded_events_total += discarded_events;
528 lost_packets_total += lost_packets;
20fb9e02
JD
529 }
530 lttng_destroy_handle(handle);
531 }
3e8f2238 532 if (discarded_events_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
533 MSG("[warning] %" PRIu64 " events discarded, please refer to "
534 "the documentation on channel configuration.",
3e8f2238 535 discarded_events_total);
20fb9e02 536 }
3e8f2238 537 if (lost_packets_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
538 MSG("[warning] %" PRIu64 " packets lost, please refer to "
539 "the documentation on channel configuration.",
3e8f2238 540 lost_packets_total);
20fb9e02
JD
541 }
542
543end:
3e8f2238 544 free(sessions);
20fb9e02 545}
4ba92f18 546
4fc83d94 547int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
548{
549 int ret;
550 char page_name[32];
551
552 ret = sprintf(page_name, "lttng-%s", cmd_name);
553 assert(ret > 0 && ret < 32);
4fc83d94
PP
554 ret = utils_show_help(1, page_name, help_msg);
555 if (ret && !help_msg) {
556 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
557 perror("exec");
558 }
4ba92f18 559
4fc83d94 560 return ret;
4ba92f18 561}
8bb32452
JG
562
563int print_trace_archive_location(
564 const struct lttng_trace_archive_location *location,
565 const char *session_name)
566{
567 int ret = 0;
568 enum lttng_trace_archive_location_type location_type;
569 enum lttng_trace_archive_location_status status;
570 bool printed_location = false;
571
572 location_type = lttng_trace_archive_location_get_type(location);
573
574 _MSG("Trace chunk archive for session %s is now readable",
575 session_name);
576 switch (location_type) {
577 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
578 {
579 const char *absolute_path;
580
581 status = lttng_trace_archive_location_local_get_absolute_path(
582 location, &absolute_path);
583 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
584 ret = -1;
585 goto end;
586 }
587 MSG(" at %s", absolute_path);
588 printed_location = true;
589 break;
590 }
591 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
592 {
593 uint16_t control_port, data_port;
594 const char *host, *relative_path, *protocol_str;
595 enum lttng_trace_archive_location_relay_protocol_type protocol;
596
597 /* Fetch all relay location parameters. */
598 status = lttng_trace_archive_location_relay_get_protocol_type(
599 location, &protocol);
600 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
601 ret = -1;
602 goto end;
603 }
604
605 status = lttng_trace_archive_location_relay_get_host(
606 location, &host);
607 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
608 ret = -1;
609 goto end;
610 }
611
612 status = lttng_trace_archive_location_relay_get_control_port(
613 location, &control_port);
614 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
615 ret = -1;
616 goto end;
617 }
618
619 status = lttng_trace_archive_location_relay_get_data_port(
620 location, &data_port);
621 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
622 ret = -1;
623 goto end;
624 }
625
626 status = lttng_trace_archive_location_relay_get_relative_path(
627 location, &relative_path);
628 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
629 ret = -1;
630 goto end;
631 }
632
633 switch (protocol) {
634 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
635 protocol_str = "tcp";
636 break;
637 default:
638 protocol_str = "unknown";
639 break;
640 }
641
642 MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
643 PRIu16 "]", protocol_str, host,
644 relative_path, control_port, data_port);
645 printed_location = true;
646 break;
647 }
648 default:
649 break;
650 }
651end:
652 if (!printed_location) {
653 MSG(" at an unknown location");
654 }
655 return ret;
656}
This page took 0.117193 seconds and 4 git commands to generate.