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