Typo: occured -> occurred
[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";
b9dfb167 44
1dac0189
PPM
45static
46char *_get_session_name(int quiet)
f3ed775e
DG
47{
48 char *path, *session_name = NULL;
49
50 /* Get path to config file */
feb0f3e5 51 path = utils_get_home_dir();
f3ed775e
DG
52 if (path == NULL) {
53 goto error;
54 }
55
56 /* Get session name from config */
1dac0189
PPM
57 session_name = quiet ? config_read_session_name_quiet(path) :
58 config_read_session_name(path);
f3ed775e 59 if (session_name == NULL) {
58a97671 60 goto error;
f3ed775e
DG
61 }
62
3183dbb0 63 DBG2("Config file path found: %s", path);
cd80958d 64 DBG("Session name found: %s", session_name);
f3ed775e 65 return session_name;
3183dbb0
DG
66
67error:
68 return NULL;
f3ed775e 69}
679b4943 70
1dac0189
PPM
71/*
72 * get_session_name
73 *
74 * Return allocated string with the session name found in the config
75 * directory.
76 */
77char *get_session_name(void)
78{
79 return _get_session_name(0);
80}
81
82/*
83 * get_session_name_quiet (no warnings/errors emitted)
84 *
85 * Return allocated string with the session name found in the config
86 * directory.
87 */
88char *get_session_name_quiet(void)
89{
90 return _get_session_name(1);
91}
92
3c9bd23c
SM
93/*
94 * list_commands
95 *
96 * List commands line by line. This is mostly for bash auto completion and to
97 * avoid difficult parsing.
98 */
99void list_commands(struct cmd_struct *commands, FILE *ofp)
100{
101 int i = 0;
102 struct cmd_struct *cmd = NULL;
103
104 cmd = &commands[i];
105 while (cmd->name != NULL) {
106 fprintf(ofp, "%s\n", cmd->name);
107 i++;
108 cmd = &commands[i];
109 }
110}
679b4943
SM
111
112/*
113 * list_cmd_options
114 *
115 * Prints a simple list of the options available to a command. This is intended
116 * to be easily parsed for bash completion.
117 */
118void list_cmd_options(FILE *ofp, struct poptOption *options)
119{
120 int i;
121 struct poptOption *option = NULL;
122
123 for (i = 0; options[i].longName != NULL; i++) {
124 option = &options[i];
125
126 fprintf(ofp, "--%s\n", option->longName);
127
128 if (isprint(option->shortName)) {
129 fprintf(ofp, "-%c\n", option->shortName);
130 }
131 }
132}
8ce58bad
MD
133
134/*
135 * fls: returns the position of the most significant bit.
136 * Returns 0 if no bit is set, else returns the position of the most
137 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
138 */
139#if defined(__i386) || defined(__x86_64)
140static inline
141unsigned int fls_u32(uint32_t x)
142{
143 int r;
144
145 asm("bsrl %1,%0\n\t"
146 "jnz 1f\n\t"
147 "movl $-1,%0\n\t"
148 "1:\n\t"
149 : "=r" (r) : "rm" (x));
150 return r + 1;
151}
152#define HAS_FLS_U32
153#endif
154
155#if defined(__x86_64)
156static inline
157unsigned int fls_u64(uint64_t x)
158{
159 long r;
160
161 asm("bsrq %1,%0\n\t"
162 "jnz 1f\n\t"
163 "movq $-1,%0\n\t"
164 "1:\n\t"
165 : "=r" (r) : "rm" (x));
166 return r + 1;
167}
168#define HAS_FLS_U64
169#endif
170
171#ifndef HAS_FLS_U64
172static __attribute__((unused))
173unsigned int fls_u64(uint64_t x)
174{
175 unsigned int r = 64;
176
177 if (!x)
178 return 0;
179
180 if (!(x & 0xFFFFFFFF00000000ULL)) {
181 x <<= 32;
182 r -= 32;
183 }
184 if (!(x & 0xFFFF000000000000ULL)) {
185 x <<= 16;
186 r -= 16;
187 }
188 if (!(x & 0xFF00000000000000ULL)) {
189 x <<= 8;
190 r -= 8;
191 }
192 if (!(x & 0xF000000000000000ULL)) {
193 x <<= 4;
194 r -= 4;
195 }
196 if (!(x & 0xC000000000000000ULL)) {
197 x <<= 2;
198 r -= 2;
199 }
200 if (!(x & 0x8000000000000000ULL)) {
201 x <<= 1;
202 r -= 1;
203 }
204 return r;
205}
206#endif
207
208#ifndef HAS_FLS_U32
209static __attribute__((unused))
210unsigned int fls_u32(uint32_t x)
211{
212 unsigned int r = 32;
213
214 if (!x)
215 return 0;
216 if (!(x & 0xFFFF0000U)) {
217 x <<= 16;
218 r -= 16;
219 }
220 if (!(x & 0xFF000000U)) {
221 x <<= 8;
222 r -= 8;
223 }
224 if (!(x & 0xF0000000U)) {
225 x <<= 4;
226 r -= 4;
227 }
228 if (!(x & 0xC0000000U)) {
229 x <<= 2;
230 r -= 2;
231 }
232 if (!(x & 0x80000000U)) {
233 x <<= 1;
234 r -= 1;
235 }
236 return r;
237}
238#endif
239
240static
241unsigned int fls_ulong(unsigned long x)
242{
243#if (CAA_BITS_PER_LONG == 32)
244 return fls_u32(x);
245#else
246 return fls_u64(x);
247#endif
248}
249
250/*
251 * Return the minimum order for which x <= (1UL << order).
252 * Return -1 if x is 0.
253 */
254int get_count_order_u32(uint32_t x)
255{
256 if (!x)
257 return -1;
258
259 return fls_u32(x - 1);
260}
261
262/*
263 * Return the minimum order for which x <= (1UL << order).
264 * Return -1 if x is 0.
265 */
266int get_count_order_u64(uint64_t x)
267{
268 if (!x)
269 return -1;
270
271 return fls_u64(x - 1);
272}
273
274/*
275 * Return the minimum order for which x <= (1UL << order).
276 * Return -1 if x is 0.
277 */
278int get_count_order_ulong(unsigned long x)
279{
280 if (!x)
281 return -1;
282
283 return fls_ulong(x - 1);
284}
b9dfb167
DG
285
286const char *get_domain_str(enum lttng_domain_type domain)
287{
288 const char *str_dom;
289
290 switch (domain) {
291 case LTTNG_DOMAIN_KERNEL:
292 str_dom = str_kernel;
293 break;
294 case LTTNG_DOMAIN_UST:
295 str_dom = str_ust;
296 break;
297 case LTTNG_DOMAIN_JUL:
298 str_dom = str_jul;
299 break;
5cdb6027
DG
300 case LTTNG_DOMAIN_LOG4J:
301 str_dom = str_log4j;
302 break;
0e115563
DG
303 case LTTNG_DOMAIN_PYTHON:
304 str_dom = str_python;
305 break;
b9dfb167
DG
306 default:
307 /* Should not have an unknown domain or else define it. */
308 assert(0);
309 }
310
311 return str_dom;
312}
8960e9cd
DG
313
314/*
315 * Spawn a lttng relayd daemon by forking and execv.
316 */
317int spawn_relayd(const char *pathname, int port)
318{
319 int ret = 0;
320 pid_t pid;
321 char url[255];
322
323 if (!port) {
324 port = DEFAULT_NETWORK_VIEWER_PORT;
325 }
326
327 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
328 if (ret < 0) {
329 goto end;
330 }
331
332 MSG("Spawning a relayd daemon");
333 pid = fork();
334 if (pid == 0) {
335 /*
336 * Spawn session daemon and tell
337 * it to signal us when ready.
338 */
339 execlp(pathname, "lttng-relayd", "-L", url, NULL);
340 /* execlp only returns if error happened */
341 if (errno == ENOENT) {
342 ERR("No relayd found. Use --relayd-path.");
343 } else {
6f04ed72 344 PERROR("execlp");
8960e9cd
DG
345 }
346 kill(getppid(), SIGTERM); /* wake parent */
347 exit(EXIT_FAILURE);
348 } else if (pid > 0) {
349 goto end;
350 } else {
6f04ed72 351 PERROR("fork");
8960e9cd
DG
352 ret = -1;
353 goto end;
354 }
355
356end:
357 return ret;
358}
359
360/*
361 * Check if relayd is alive.
362 *
363 * Return 1 if found else 0 if NOT found. Negative value on error.
364 */
365int check_relayd(void)
366{
367 int ret, fd;
368 struct sockaddr_in sin;
369
370 fd = socket(AF_INET, SOCK_STREAM, 0);
371 if (fd < 0) {
6f04ed72 372 PERROR("socket check relayd");
dd02a4c1
DG
373 ret = -1;
374 goto error_socket;
8960e9cd
DG
375 }
376
377 sin.sin_family = AF_INET;
378 sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT);
379 ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
380 if (ret < 1) {
6f04ed72 381 PERROR("inet_pton check relayd");
dd02a4c1 382 ret = -1;
8960e9cd
DG
383 goto error;
384 }
385
386 /*
387 * A successful connect means the relayd exists thus returning 0 else a
388 * negative value means it does NOT exists.
389 */
390 ret = connect(fd, &sin, sizeof(sin));
391 if (ret < 0) {
392 /* Not found. */
393 ret = 0;
394 } else {
395 /* Already spawned. */
396 ret = 1;
397 }
398
8960e9cd 399error:
dd02a4c1 400 if (close(fd) < 0) {
6f04ed72 401 PERROR("close relayd fd");
dd02a4c1
DG
402 }
403error_socket:
404 return ret;
8960e9cd 405}
3ecec76a
PP
406
407int print_missing_or_multiple_domains(unsigned int sum)
408{
409 int ret = 0;
410
411 if (sum == 0) {
412 ERR("Please specify a domain (-k/-u/-j).");
413 ret = -1;
414 } else if (sum > 1) {
415 ERR("Multiple domains specified.");
416 ret = -1;
417 }
418
419 return ret;
420}
20fb9e02
JD
421
422/*
423 * Get the discarded events and lost packet counts.
424 */
425void print_session_stats(const char *session_name)
426{
427 int count, nb_domains, domain_idx, channel_idx;
428 struct lttng_domain *domains;
429 struct lttng_channel *channels;
430 uint64_t discarded_total = 0, lost_total = 0;
431
432 nb_domains = lttng_list_domains(session_name, &domains);
433 if (nb_domains < 0) {
434 goto end;
435 }
436 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
437 struct lttng_handle *handle = lttng_create_handle(session_name,
438 &domains[domain_idx]);
439
440 if (!handle) {
441 ERR("Failed to create session handle while printing session stats.");
442 goto end;
443 }
444
445 count = lttng_list_channels(handle, &channels);
446 for (channel_idx = 0; channel_idx < count; channel_idx++) {
447 int ret;
448 uint64_t discarded = 0, lost = 0;
449 struct lttng_channel *channel = &channels[channel_idx];
450
451 ret = lttng_channel_get_discarded_event_count(channel,
452 &discarded);
453 if (ret) {
454 ERR("Failed to retrieve discarded event count from channel %s",
455 channel->name);
456 }
457
458 ret = lttng_channel_get_lost_packet_count(channel,
459 &lost);
460 if (ret) {
461 ERR("Failed to retrieve lost packet count from channel %s",
462 channel->name);
463 }
464
465 discarded_total += discarded;
466 lost_total += lost;
467 }
468 lttng_destroy_handle(handle);
469 }
470 if (discarded_total > 0) {
471 MSG("[warning] %" PRIu64 " events discarded, please refer to "
472 "the documentation on channel configuration.",
473 discarded_total);
474 }
475 if (lost_total > 0) {
476 MSG("[warning] %" PRIu64 " packets lost, please refer to "
477 "the documentation on channel configuration.",
478 lost_total);
479 }
480
481end:
482 return;
483}
4ba92f18
PP
484
485int show_cmd_man_page(const char *cmd_name)
486{
487 int ret;
488 char page_name[32];
489
490 ret = sprintf(page_name, "lttng-%s", cmd_name);
491 assert(ret > 0 && ret < 32);
492
493 return utils_show_man_page(1, page_name);
494}
This page took 0.082731 seconds and 4 git commands to generate.