1e3b91344d24ae58470b2cb966758402bd41c322
[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 char *path, *session_name = NULL;
55
56 /* Get path to config file */
57 path = utils_get_home_dir();
58 if (path == NULL) {
59 goto error;
60 }
61
62 /* Get session name from config */
63 session_name = quiet ? config_read_session_name_quiet(path) :
64 config_read_session_name(path);
65 if (session_name == NULL) {
66 goto error;
67 }
68
69 DBG2("Config file path found: %s", path);
70 DBG("Session name found: %s", session_name);
71 return session_name;
72
73 error:
74 return NULL;
75 }
76
77 /*
78 * get_session_name
79 *
80 * Return allocated string with the session name found in the config
81 * directory.
82 */
83 char *get_session_name(void)
84 {
85 return _get_session_name(0);
86 }
87
88 /*
89 * get_session_name_quiet (no warnings/errors emitted)
90 *
91 * Return allocated string with the session name found in the config
92 * directory.
93 */
94 char *get_session_name_quiet(void)
95 {
96 return _get_session_name(1);
97 }
98
99 /*
100 * list_commands
101 *
102 * List commands line by line. This is mostly for bash auto completion and to
103 * avoid difficult parsing.
104 */
105 void list_commands(struct cmd_struct *commands, FILE *ofp)
106 {
107 int i = 0;
108 struct cmd_struct *cmd = NULL;
109
110 cmd = &commands[i];
111 while (cmd->name != NULL) {
112 fprintf(ofp, "%s\n", cmd->name);
113 i++;
114 cmd = &commands[i];
115 }
116 }
117
118 /*
119 * list_cmd_options
120 *
121 * Prints a simple list of the options available to a command. This is intended
122 * to be easily parsed for bash completion.
123 */
124 void list_cmd_options(FILE *ofp, struct poptOption *options)
125 {
126 int i;
127 struct poptOption *option = NULL;
128
129 for (i = 0; options[i].longName != NULL; i++) {
130 option = &options[i];
131
132 fprintf(ofp, "--%s\n", option->longName);
133
134 if (isprint(option->shortName)) {
135 fprintf(ofp, "-%c\n", option->shortName);
136 }
137 }
138 }
139
140 /*
141 * fls: returns the position of the most significant bit.
142 * Returns 0 if no bit is set, else returns the position of the most
143 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
144 */
145 #if defined(__i386) || defined(__x86_64)
146 static inline
147 unsigned int fls_u32(uint32_t x)
148 {
149 int r;
150
151 asm("bsrl %1,%0\n\t"
152 "jnz 1f\n\t"
153 "movl $-1,%0\n\t"
154 "1:\n\t"
155 : "=r" (r) : "rm" (x));
156 return r + 1;
157 }
158 #define HAS_FLS_U32
159 #endif
160
161 #if defined(__x86_64)
162 static inline
163 unsigned int fls_u64(uint64_t x)
164 {
165 long r;
166
167 asm("bsrq %1,%0\n\t"
168 "jnz 1f\n\t"
169 "movq $-1,%0\n\t"
170 "1:\n\t"
171 : "=r" (r) : "rm" (x));
172 return r + 1;
173 }
174 #define HAS_FLS_U64
175 #endif
176
177 #ifndef HAS_FLS_U64
178 static __attribute__((unused))
179 unsigned int fls_u64(uint64_t x)
180 {
181 unsigned int r = 64;
182
183 if (!x)
184 return 0;
185
186 if (!(x & 0xFFFFFFFF00000000ULL)) {
187 x <<= 32;
188 r -= 32;
189 }
190 if (!(x & 0xFFFF000000000000ULL)) {
191 x <<= 16;
192 r -= 16;
193 }
194 if (!(x & 0xFF00000000000000ULL)) {
195 x <<= 8;
196 r -= 8;
197 }
198 if (!(x & 0xF000000000000000ULL)) {
199 x <<= 4;
200 r -= 4;
201 }
202 if (!(x & 0xC000000000000000ULL)) {
203 x <<= 2;
204 r -= 2;
205 }
206 if (!(x & 0x8000000000000000ULL)) {
207 x <<= 1;
208 r -= 1;
209 }
210 return r;
211 }
212 #endif
213
214 #ifndef HAS_FLS_U32
215 static __attribute__((unused))
216 unsigned int fls_u32(uint32_t x)
217 {
218 unsigned int r = 32;
219
220 if (!x)
221 return 0;
222 if (!(x & 0xFFFF0000U)) {
223 x <<= 16;
224 r -= 16;
225 }
226 if (!(x & 0xFF000000U)) {
227 x <<= 8;
228 r -= 8;
229 }
230 if (!(x & 0xF0000000U)) {
231 x <<= 4;
232 r -= 4;
233 }
234 if (!(x & 0xC0000000U)) {
235 x <<= 2;
236 r -= 2;
237 }
238 if (!(x & 0x80000000U)) {
239 x <<= 1;
240 r -= 1;
241 }
242 return r;
243 }
244 #endif
245
246 static
247 unsigned int fls_ulong(unsigned long x)
248 {
249 #if (CAA_BITS_PER_LONG == 32)
250 return fls_u32(x);
251 #else
252 return fls_u64(x);
253 #endif
254 }
255
256 /*
257 * Return the minimum order for which x <= (1UL << order).
258 * Return -1 if x is 0.
259 */
260 int get_count_order_u32(uint32_t x)
261 {
262 if (!x)
263 return -1;
264
265 return fls_u32(x - 1);
266 }
267
268 /*
269 * Return the minimum order for which x <= (1UL << order).
270 * Return -1 if x is 0.
271 */
272 int get_count_order_u64(uint64_t x)
273 {
274 if (!x)
275 return -1;
276
277 return fls_u64(x - 1);
278 }
279
280 /*
281 * Return the minimum order for which x <= (1UL << order).
282 * Return -1 if x is 0.
283 */
284 int get_count_order_ulong(unsigned long x)
285 {
286 if (!x)
287 return -1;
288
289 return fls_ulong(x - 1);
290 }
291
292 const char *get_domain_str(enum lttng_domain_type domain)
293 {
294 const char *str_dom;
295
296 switch (domain) {
297 case LTTNG_DOMAIN_KERNEL:
298 str_dom = str_kernel;
299 break;
300 case LTTNG_DOMAIN_UST:
301 str_dom = str_ust;
302 break;
303 case LTTNG_DOMAIN_JUL:
304 str_dom = str_jul;
305 break;
306 case LTTNG_DOMAIN_LOG4J:
307 str_dom = str_log4j;
308 break;
309 case LTTNG_DOMAIN_PYTHON:
310 str_dom = str_python;
311 break;
312 default:
313 /* Should not have an unknown domain or else define it. */
314 assert(0);
315 }
316
317 return str_dom;
318 }
319
320 const char *get_event_type_str(enum lttng_event_type type)
321 {
322 const char *str_event_type;
323
324 switch (type) {
325 case LTTNG_EVENT_ALL:
326 str_event_type = str_all;
327 break;
328 case LTTNG_EVENT_TRACEPOINT:
329 str_event_type = str_tracepoint;
330 break;
331 case LTTNG_EVENT_SYSCALL:
332 str_event_type = str_syscall;
333 break;
334 case LTTNG_EVENT_PROBE:
335 str_event_type = str_probe;
336 break;
337 case LTTNG_EVENT_USERSPACE_PROBE:
338 str_event_type = str_userspace_probe;
339 break;
340 case LTTNG_EVENT_FUNCTION:
341 str_event_type = str_function;
342 break;
343 default:
344 /* Should not have an unknown event type or else define it. */
345 assert(0);
346 }
347
348 return str_event_type;
349 }
350
351 /*
352 * Spawn a lttng relayd daemon by forking and execv.
353 */
354 int spawn_relayd(const char *pathname, int port)
355 {
356 int ret = 0;
357 pid_t pid;
358 char url[255];
359
360 if (!port) {
361 port = DEFAULT_NETWORK_VIEWER_PORT;
362 }
363
364 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
365 if (ret < 0) {
366 goto end;
367 }
368
369 MSG("Spawning a relayd daemon");
370 pid = fork();
371 if (pid == 0) {
372 /*
373 * Spawn session daemon and tell
374 * it to signal us when ready.
375 */
376 execlp(pathname, "lttng-relayd", "-L", url, NULL);
377 /* execlp only returns if error happened */
378 if (errno == ENOENT) {
379 ERR("No relayd found. Use --relayd-path.");
380 } else {
381 PERROR("execlp");
382 }
383 kill(getppid(), SIGTERM); /* wake parent */
384 exit(EXIT_FAILURE);
385 } else if (pid > 0) {
386 goto end;
387 } else {
388 PERROR("fork");
389 ret = -1;
390 goto end;
391 }
392
393 end:
394 return ret;
395 }
396
397 /*
398 * Check if relayd is alive.
399 *
400 * Return 1 if found else 0 if NOT found. Negative value on error.
401 */
402 int check_relayd(void)
403 {
404 int ret, fd;
405 struct sockaddr_in sin;
406
407 fd = socket(AF_INET, SOCK_STREAM, 0);
408 if (fd < 0) {
409 PERROR("socket check relayd");
410 ret = -1;
411 goto error_socket;
412 }
413
414 sin.sin_family = AF_INET;
415 sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT);
416 ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
417 if (ret < 1) {
418 PERROR("inet_pton check relayd");
419 ret = -1;
420 goto error;
421 }
422
423 /*
424 * A successful connect means the relayd exists thus returning 0 else a
425 * negative value means it does NOT exists.
426 */
427 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
428 if (ret < 0) {
429 /* Not found. */
430 ret = 0;
431 } else {
432 /* Already spawned. */
433 ret = 1;
434 }
435
436 error:
437 if (close(fd) < 0) {
438 PERROR("close relayd fd");
439 }
440 error_socket:
441 return ret;
442 }
443
444 int print_missing_or_multiple_domains(unsigned int sum)
445 {
446 int ret = 0;
447
448 if (sum == 0) {
449 ERR("Please specify a domain (-k/-u/-j).");
450 ret = -1;
451 } else if (sum > 1) {
452 ERR("Multiple domains specified.");
453 ret = -1;
454 }
455
456 return ret;
457 }
458
459 /*
460 * Get the discarded events and lost packet counts.
461 */
462 void print_session_stats(const char *session_name)
463 {
464 int count, nb_domains, domain_idx, channel_idx, session_idx;
465 struct lttng_domain *domains;
466 struct lttng_channel *channels;
467 uint64_t discarded_events_total = 0, lost_packets_total = 0;
468 struct lttng_session *sessions = NULL;
469 const struct lttng_session *selected_session = NULL;
470
471 count = lttng_list_sessions(&sessions);
472 if (count < 1) {
473 ERR("Failed to retrieve session descriptions while printing session statistics.");
474 goto end;
475 }
476
477 /* Identify the currently-selected sessions. */
478 for (session_idx = 0; session_idx < count; session_idx++) {
479 if (!strcmp(session_name, sessions[session_idx].name)) {
480 selected_session = &sessions[session_idx];
481 break;
482 }
483 }
484 if (!selected_session) {
485 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
486 goto end;
487 }
488
489 nb_domains = lttng_list_domains(session_name, &domains);
490 if (nb_domains < 0) {
491 goto end;
492 }
493 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
494 struct lttng_handle *handle = lttng_create_handle(session_name,
495 &domains[domain_idx]);
496
497 if (!handle) {
498 ERR("Failed to create session handle while printing session statistics.");
499 goto end;
500 }
501
502 count = lttng_list_channels(handle, &channels);
503 for (channel_idx = 0; channel_idx < count; channel_idx++) {
504 int ret;
505 uint64_t discarded_events = 0, lost_packets = 0;
506 struct lttng_channel *channel = &channels[channel_idx];
507
508 ret = lttng_channel_get_discarded_event_count(channel,
509 &discarded_events);
510 if (ret) {
511 ERR("Failed to retrieve discarded event count from channel %s",
512 channel->name);
513 }
514
515 ret = lttng_channel_get_lost_packet_count(channel,
516 &lost_packets);
517 if (ret) {
518 ERR("Failed to retrieve lost packet count from channel %s",
519 channel->name);
520 }
521
522 discarded_events_total += discarded_events;
523 lost_packets_total += lost_packets;
524 }
525 lttng_destroy_handle(handle);
526 }
527 if (discarded_events_total > 0 && !selected_session->snapshot_mode) {
528 MSG("[warning] %" PRIu64 " events discarded, please refer to "
529 "the documentation on channel configuration.",
530 discarded_events_total);
531 }
532 if (lost_packets_total > 0 && !selected_session->snapshot_mode) {
533 MSG("[warning] %" PRIu64 " packets lost, please refer to "
534 "the documentation on channel configuration.",
535 lost_packets_total);
536 }
537
538 end:
539 free(sessions);
540 }
541
542 int show_cmd_help(const char *cmd_name, const char *help_msg)
543 {
544 int ret;
545 char page_name[32];
546
547 ret = sprintf(page_name, "lttng-%s", cmd_name);
548 assert(ret > 0 && ret < 32);
549 ret = utils_show_help(1, page_name, help_msg);
550 if (ret && !help_msg) {
551 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
552 perror("exec");
553 }
554
555 return ret;
556 }
557
558 int print_trace_archive_location(
559 const struct lttng_trace_archive_location *location,
560 const char *session_name)
561 {
562 int ret = 0;
563 enum lttng_trace_archive_location_type location_type;
564 enum lttng_trace_archive_location_status status;
565 bool printed_location = false;
566
567 location_type = lttng_trace_archive_location_get_type(location);
568
569 _MSG("Trace chunk archive for session %s is now readable",
570 session_name);
571 switch (location_type) {
572 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
573 {
574 const char *absolute_path;
575
576 status = lttng_trace_archive_location_local_get_absolute_path(
577 location, &absolute_path);
578 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
579 ret = -1;
580 goto end;
581 }
582 MSG(" at %s", absolute_path);
583 printed_location = true;
584 break;
585 }
586 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
587 {
588 uint16_t control_port, data_port;
589 const char *host, *relative_path, *protocol_str;
590 enum lttng_trace_archive_location_relay_protocol_type protocol;
591
592 /* Fetch all relay location parameters. */
593 status = lttng_trace_archive_location_relay_get_protocol_type(
594 location, &protocol);
595 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
596 ret = -1;
597 goto end;
598 }
599
600 status = lttng_trace_archive_location_relay_get_host(
601 location, &host);
602 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
603 ret = -1;
604 goto end;
605 }
606
607 status = lttng_trace_archive_location_relay_get_control_port(
608 location, &control_port);
609 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
610 ret = -1;
611 goto end;
612 }
613
614 status = lttng_trace_archive_location_relay_get_data_port(
615 location, &data_port);
616 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
617 ret = -1;
618 goto end;
619 }
620
621 status = lttng_trace_archive_location_relay_get_relative_path(
622 location, &relative_path);
623 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
624 ret = -1;
625 goto end;
626 }
627
628 switch (protocol) {
629 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
630 protocol_str = "tcp";
631 break;
632 default:
633 protocol_str = "unknown";
634 break;
635 }
636
637 MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
638 PRIu16 "]", protocol_str, host,
639 relative_path, control_port, data_port);
640 printed_location = true;
641 break;
642 }
643 default:
644 break;
645 }
646 end:
647 if (!printed_location) {
648 MSG(" at an unknown location");
649 }
650 return ret;
651 }
This page took 0.040306 seconds and 3 git commands to generate.