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