Clean code base from redundant verification
[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
DG
53{
54 char *path, *session_name = NULL;
55
56 /* Get path to config file */
feb0f3e5 57 path = utils_get_home_dir();
f3ed775e
DG
58 if (path == NULL) {
59 goto error;
60 }
61
62 /* Get session name from config */
1dac0189
PPM
63 session_name = quiet ? config_read_session_name_quiet(path) :
64 config_read_session_name(path);
f3ed775e 65 if (session_name == NULL) {
58a97671 66 goto error;
f3ed775e
DG
67 }
68
3183dbb0 69 DBG2("Config file path found: %s", path);
cd80958d 70 DBG("Session name found: %s", session_name);
f3ed775e 71 return session_name;
3183dbb0
DG
72
73error:
74 return NULL;
f3ed775e 75}
679b4943 76
1dac0189
PPM
77/*
78 * get_session_name
79 *
80 * Return allocated string with the session name found in the config
81 * directory.
82 */
83char *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 */
94char *get_session_name_quiet(void)
95{
96 return _get_session_name(1);
97}
98
3c9bd23c
SM
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 */
105void 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}
679b4943
SM
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 */
124void 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}
8ce58bad
MD
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)
146static inline
147unsigned 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)
162static inline
163unsigned 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
178static __attribute__((unused))
179unsigned 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
215static __attribute__((unused))
216unsigned 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
246static
247unsigned 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 */
260int 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 */
272int 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 */
284int get_count_order_ulong(unsigned long x)
285{
286 if (!x)
287 return -1;
288
289 return fls_ulong(x - 1);
290}
b9dfb167
DG
291
292const 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;
5cdb6027
DG
306 case LTTNG_DOMAIN_LOG4J:
307 str_dom = str_log4j;
308 break;
0e115563
DG
309 case LTTNG_DOMAIN_PYTHON:
310 str_dom = str_python;
311 break;
b9dfb167
DG
312 default:
313 /* Should not have an unknown domain or else define it. */
314 assert(0);
315 }
316
317 return str_dom;
318}
8960e9cd 319
4fd2697f
FD
320const 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
8960e9cd
DG
351/*
352 * Spawn a lttng relayd daemon by forking and execv.
353 */
354int 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 {
6f04ed72 381 PERROR("execlp");
8960e9cd
DG
382 }
383 kill(getppid(), SIGTERM); /* wake parent */
384 exit(EXIT_FAILURE);
385 } else if (pid > 0) {
386 goto end;
387 } else {
6f04ed72 388 PERROR("fork");
8960e9cd
DG
389 ret = -1;
390 goto end;
391 }
392
393end:
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 */
402int 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) {
6f04ed72 409 PERROR("socket check relayd");
dd02a4c1
DG
410 ret = -1;
411 goto error_socket;
8960e9cd
DG
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) {
6f04ed72 418 PERROR("inet_pton check relayd");
dd02a4c1 419 ret = -1;
8960e9cd
DG
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 */
56efeab3 427 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
8960e9cd
DG
428 if (ret < 0) {
429 /* Not found. */
430 ret = 0;
431 } else {
432 /* Already spawned. */
433 ret = 1;
434 }
435
8960e9cd 436error:
dd02a4c1 437 if (close(fd) < 0) {
6f04ed72 438 PERROR("close relayd fd");
dd02a4c1
DG
439 }
440error_socket:
441 return ret;
8960e9cd 442}
3ecec76a
PP
443
444int 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}
20fb9e02
JD
458
459/*
460 * Get the discarded events and lost packet counts.
461 */
462void print_session_stats(const char *session_name)
463{
3e8f2238 464 int count, nb_domains, domain_idx, channel_idx, session_idx;
20fb9e02
JD
465 struct lttng_domain *domains;
466 struct lttng_channel *channels;
3e8f2238
JG
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 }
20fb9e02
JD
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) {
3e8f2238 498 ERR("Failed to create session handle while printing session statistics.");
20fb9e02
JD
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;
3e8f2238 505 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
506 struct lttng_channel *channel = &channels[channel_idx];
507
508 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 509 &discarded_events);
20fb9e02
JD
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,
3e8f2238 516 &lost_packets);
20fb9e02
JD
517 if (ret) {
518 ERR("Failed to retrieve lost packet count from channel %s",
519 channel->name);
520 }
521
3e8f2238
JG
522 discarded_events_total += discarded_events;
523 lost_packets_total += lost_packets;
20fb9e02
JD
524 }
525 lttng_destroy_handle(handle);
526 }
3e8f2238 527 if (discarded_events_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
528 MSG("[warning] %" PRIu64 " events discarded, please refer to "
529 "the documentation on channel configuration.",
3e8f2238 530 discarded_events_total);
20fb9e02 531 }
3e8f2238 532 if (lost_packets_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
533 MSG("[warning] %" PRIu64 " packets lost, please refer to "
534 "the documentation on channel configuration.",
3e8f2238 535 lost_packets_total);
20fb9e02
JD
536 }
537
538end:
3e8f2238 539 free(sessions);
20fb9e02 540}
4ba92f18 541
4fc83d94 542int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
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);
4fc83d94
PP
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 }
4ba92f18 554
4fc83d94 555 return ret;
4ba92f18 556}
This page took 0.070796 seconds and 4 git commands to generate.