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