2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
30 #include <common/error.h>
31 #include <common/utils.h>
37 static const char *str_kernel
= "Kernel";
38 static const char *str_ust
= "UST";
39 static const char *str_jul
= "JUL";
40 static const char *str_log4j
= "LOG4J";
41 static const char *str_python
= "Python";
44 char *_get_session_name(int quiet
)
46 char *path
, *session_name
= NULL
;
48 /* Get path to config file */
49 path
= utils_get_home_dir();
54 /* Get session name from config */
55 session_name
= quiet
? config_read_session_name_quiet(path
) :
56 config_read_session_name(path
);
57 if (session_name
== NULL
) {
61 DBG2("Config file path found: %s", path
);
62 DBG("Session name found: %s", session_name
);
72 * Return allocated string with the session name found in the config
75 char *get_session_name(void)
77 return _get_session_name(0);
81 * get_session_name_quiet (no warnings/errors emitted)
83 * Return allocated string with the session name found in the config
86 char *get_session_name_quiet(void)
88 return _get_session_name(1);
94 * List commands line by line. This is mostly for bash auto completion and to
95 * avoid difficult parsing.
97 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
100 struct cmd_struct
*cmd
= NULL
;
103 while (cmd
->name
!= NULL
) {
104 fprintf(ofp
, "%s\n", cmd
->name
);
113 * Prints a simple list of the options available to a command. This is intended
114 * to be easily parsed for bash completion.
116 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
119 struct poptOption
*option
= NULL
;
121 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
122 option
= &options
[i
];
124 fprintf(ofp
, "--%s\n", option
->longName
);
126 if (isprint(option
->shortName
)) {
127 fprintf(ofp
, "-%c\n", option
->shortName
);
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).
137 #if defined(__i386) || defined(__x86_64)
139 unsigned int fls_u32(uint32_t x
)
147 : "=r" (r
) : "rm" (x
));
153 #if defined(__x86_64)
155 unsigned int fls_u64(uint64_t x
)
163 : "=r" (r
) : "rm" (x
));
170 static __attribute__((unused
))
171 unsigned int fls_u64(uint64_t x
)
178 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
182 if (!(x
& 0xFFFF000000000000ULL
)) {
186 if (!(x
& 0xFF00000000000000ULL
)) {
190 if (!(x
& 0xF000000000000000ULL
)) {
194 if (!(x
& 0xC000000000000000ULL
)) {
198 if (!(x
& 0x8000000000000000ULL
)) {
207 static __attribute__((unused
))
208 unsigned int fls_u32(uint32_t x
)
214 if (!(x
& 0xFFFF0000U
)) {
218 if (!(x
& 0xFF000000U
)) {
222 if (!(x
& 0xF0000000U
)) {
226 if (!(x
& 0xC0000000U
)) {
230 if (!(x
& 0x80000000U
)) {
239 unsigned int fls_ulong(unsigned long x
)
241 #if (CAA_BITS_PER_LONG == 32)
249 * Return the minimum order for which x <= (1UL << order).
250 * Return -1 if x is 0.
252 int get_count_order_u32(uint32_t x
)
257 return fls_u32(x
- 1);
261 * Return the minimum order for which x <= (1UL << order).
262 * Return -1 if x is 0.
264 int get_count_order_u64(uint64_t x
)
269 return fls_u64(x
- 1);
273 * Return the minimum order for which x <= (1UL << order).
274 * Return -1 if x is 0.
276 int get_count_order_ulong(unsigned long x
)
281 return fls_ulong(x
- 1);
284 const char *get_domain_str(enum lttng_domain_type domain
)
289 case LTTNG_DOMAIN_KERNEL
:
290 str_dom
= str_kernel
;
292 case LTTNG_DOMAIN_UST
:
295 case LTTNG_DOMAIN_JUL
:
298 case LTTNG_DOMAIN_LOG4J
:
301 case LTTNG_DOMAIN_PYTHON
:
302 str_dom
= str_python
;
305 /* Should not have an unknown domain or else define it. */
313 * Spawn a lttng relayd daemon by forking and execv.
315 int spawn_relayd(const char *pathname
, int port
)
322 port
= DEFAULT_NETWORK_VIEWER_PORT
;
325 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
330 MSG("Spawning a relayd daemon");
334 * Spawn session daemon and tell
335 * it to signal us when ready.
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.");
344 kill(getppid(), SIGTERM
); /* wake parent */
346 } else if (pid
> 0) {
359 * Check if relayd is alive.
361 * Return 1 if found else 0 if NOT found. Negative value on error.
363 int check_relayd(void)
366 struct sockaddr_in sin
;
368 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
370 PERROR("socket check relayd");
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
);
379 PERROR("inet_pton check relayd");
385 * A successful connect means the relayd exists thus returning 0 else a
386 * negative value means it does NOT exists.
388 ret
= connect(fd
, &sin
, sizeof(sin
));
393 /* Already spawned. */
399 PERROR("close relayd fd");