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