Fix: lttng-destroy: missing newline on session destruction message
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
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.
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
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <signal.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30
31 #include <common/error.h>
32 #include <common/utils.h>
33 #include <common/defaults.h>
34
35 #include "conf.h"
36 #include "utils.h"
37 #include "command.h"
38
39 static const char *str_kernel = "Kernel";
40 static const char *str_ust = "UST";
41 static const char *str_jul = "JUL";
42 static const char *str_log4j = "LOG4J";
43 static const char *str_python = "Python";
44 static const char *str_all = "ALL";
45 static const char *str_tracepoint = "Tracepoint";
46 static const char *str_syscall = "Syscall";
47 static const char *str_probe = "Probe";
48 static const char *str_userspace_probe = "Userspace Probe";
49 static const char *str_function = "Function";
50
51 static
52 char *_get_session_name(int quiet)
53 {
54 const char *path;
55 char *session_name = NULL;
56
57 /* Get path to config file */
58 path = utils_get_home_dir();
59 if (path == NULL) {
60 goto error;
61 }
62
63 /* Get session name from config */
64 session_name = quiet ? config_read_session_name_quiet(path) :
65 config_read_session_name(path);
66 if (session_name == NULL) {
67 goto error;
68 }
69
70 DBG2("Config file path found: %s", path);
71 DBG("Session name found: %s", session_name);
72 return session_name;
73
74 error:
75 return NULL;
76 }
77
78 /*
79 * get_session_name
80 *
81 * Return allocated string with the session name found in the config
82 * directory.
83 */
84 char *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 */
95 char *get_session_name_quiet(void)
96 {
97 return _get_session_name(1);
98 }
99
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 */
106 void 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 }
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 */
125 void 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 }
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)
147 static inline
148 unsigned 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)
163 static inline
164 unsigned 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
179 static __attribute__((unused))
180 unsigned 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
216 static __attribute__((unused))
217 unsigned 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
247 static
248 unsigned 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 */
261 int 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 */
273 int 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 */
285 int get_count_order_ulong(unsigned long x)
286 {
287 if (!x)
288 return -1;
289
290 return fls_ulong(x - 1);
291 }
292
293 const 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;
307 case LTTNG_DOMAIN_LOG4J:
308 str_dom = str_log4j;
309 break;
310 case LTTNG_DOMAIN_PYTHON:
311 str_dom = str_python;
312 break;
313 default:
314 /* Should not have an unknown domain or else define it. */
315 assert(0);
316 }
317
318 return str_dom;
319 }
320
321 const 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
352 /*
353 * Spawn a lttng relayd daemon by forking and execv.
354 */
355 int 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 {
382 PERROR("execlp");
383 }
384 kill(getppid(), SIGTERM); /* wake parent */
385 exit(EXIT_FAILURE);
386 } else if (pid > 0) {
387 goto end;
388 } else {
389 PERROR("fork");
390 ret = -1;
391 goto end;
392 }
393
394 end:
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 */
403 int 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) {
410 PERROR("socket check relayd");
411 ret = -1;
412 goto error_socket;
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) {
419 PERROR("inet_pton check relayd");
420 ret = -1;
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 */
428 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
429 if (ret < 0) {
430 /* Not found. */
431 ret = 0;
432 } else {
433 /* Already spawned. */
434 ret = 1;
435 }
436
437 error:
438 if (close(fd) < 0) {
439 PERROR("close relayd fd");
440 }
441 error_socket:
442 return ret;
443 }
444
445 int print_missing_or_multiple_domains(unsigned int domain_count,
446 bool include_agent_domains)
447 {
448 int ret = 0;
449
450 if (domain_count == 0) {
451 ERR("Please specify a domain (--kernel/--userspace%s).",
452 include_agent_domains ?
453 "/--jul/--log4j/--python" :
454 "");
455 ret = -1;
456 } else if (domain_count > 1) {
457 ERR("Only one domain must be specified.");
458 ret = -1;
459 }
460
461 return ret;
462 }
463
464 /*
465 * Get the discarded events and lost packet counts.
466 */
467 void print_session_stats(const char *session_name)
468 {
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
478 int get_session_stats_str(const char *session_name, char **out_str)
479 {
480 int count, nb_domains, domain_idx, channel_idx, session_idx, ret;
481 struct lttng_domain *domains;
482 struct lttng_channel *channels;
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;
486 char *stats_str = NULL;
487 bool print_discarded_events = false, print_lost_packets = false;
488
489 count = lttng_list_sessions(&sessions);
490 if (count < 1) {
491 ERR("Failed to retrieve session descriptions while printing session statistics.");
492 ret = -1;
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);
505 ret = -1;
506 goto end;
507 }
508
509 nb_domains = lttng_list_domains(session_name, &domains);
510 if (nb_domains < 0) {
511 ret = -1;
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) {
519 ERR("Failed to create session handle while printing session statistics.");
520 ret = -1;
521 goto end;
522 }
523
524 count = lttng_list_channels(handle, &channels);
525 for (channel_idx = 0; channel_idx < count; channel_idx++) {
526 uint64_t discarded_events = 0, lost_packets = 0;
527 struct lttng_channel *channel = &channels[channel_idx];
528
529 ret = lttng_channel_get_discarded_event_count(channel,
530 &discarded_events);
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,
537 &lost_packets);
538 if (ret) {
539 ERR("Failed to retrieve lost packet count from channel %s",
540 channel->name);
541 }
542
543 discarded_events_total += discarded_events;
544 lost_packets_total += lost_packets;
545 }
546 lttng_destroy_handle(handle);
547 }
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 "
565 "the documentation on channel configuration.",
566 discarded_events_total);
567 } else if (print_lost_packets) {
568 ret = asprintf(&stats_str,
569 "Warning: %" PRIu64
570 " packets were lost, please refer to "
571 "the documentation on channel configuration.",
572 lost_packets_total);
573 } else {
574 ret = 0;
575 }
576
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 }
583 end:
584 free(sessions);
585 return ret;
586 }
587
588 int show_cmd_help(const char *cmd_name, const char *help_msg)
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);
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 }
600
601 return ret;
602 }
603
604 int 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 }
692 end:
693 if (!printed_location) {
694 MSG(" at an unknown location");
695 }
696 return ret;
697 }
This page took 0.043931 seconds and 4 git commands to generate.