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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
29 #include <common/error.h>
30 #include <common/utils.h>
36 static const char *str_kernel
= "Kernel";
37 static const char *str_ust
= "UST";
38 static const char *str_jul
= "JUL";
39 static const char *str_log4j
= "LOG4J";
40 static const char *str_python
= "Python";
45 * Return allocated string with the session name found in the config
48 char *get_session_name(void)
50 char *path
, *session_name
= NULL
;
52 /* Get path to config file */
53 path
= utils_get_home_dir();
58 /* Get session name from config */
59 session_name
= config_read_session_name(path
);
60 if (session_name
== NULL
) {
64 DBG2("Config file path found: %s", path
);
65 DBG("Session name found: %s", session_name
);
75 * List commands line by line. This is mostly for bash auto completion and to
76 * avoid difficult parsing.
78 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
81 struct cmd_struct
*cmd
= NULL
;
84 while (cmd
->name
!= NULL
) {
85 fprintf(ofp
, "%s\n", cmd
->name
);
94 * Prints a simple list of the options available to a command. This is intended
95 * to be easily parsed for bash completion.
97 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
100 struct poptOption
*option
= NULL
;
102 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
103 option
= &options
[i
];
105 fprintf(ofp
, "--%s\n", option
->longName
);
107 if (isprint(option
->shortName
)) {
108 fprintf(ofp
, "-%c\n", option
->shortName
);
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).
118 #if defined(__i386) || defined(__x86_64)
120 unsigned int fls_u32(uint32_t x
)
128 : "=r" (r
) : "rm" (x
));
134 #if defined(__x86_64)
136 unsigned int fls_u64(uint64_t x
)
144 : "=r" (r
) : "rm" (x
));
151 static __attribute__((unused
))
152 unsigned int fls_u64(uint64_t x
)
159 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
163 if (!(x
& 0xFFFF000000000000ULL
)) {
167 if (!(x
& 0xFF00000000000000ULL
)) {
171 if (!(x
& 0xF000000000000000ULL
)) {
175 if (!(x
& 0xC000000000000000ULL
)) {
179 if (!(x
& 0x8000000000000000ULL
)) {
188 static __attribute__((unused
))
189 unsigned int fls_u32(uint32_t x
)
195 if (!(x
& 0xFFFF0000U
)) {
199 if (!(x
& 0xFF000000U
)) {
203 if (!(x
& 0xF0000000U
)) {
207 if (!(x
& 0xC0000000U
)) {
211 if (!(x
& 0x80000000U
)) {
220 unsigned int fls_ulong(unsigned long x
)
222 #if (CAA_BITS_PER_LONG == 32)
230 * Return the minimum order for which x <= (1UL << order).
231 * Return -1 if x is 0.
233 int get_count_order_u32(uint32_t x
)
238 return fls_u32(x
- 1);
242 * Return the minimum order for which x <= (1UL << order).
243 * Return -1 if x is 0.
245 int get_count_order_u64(uint64_t x
)
250 return fls_u64(x
- 1);
254 * Return the minimum order for which x <= (1UL << order).
255 * Return -1 if x is 0.
257 int get_count_order_ulong(unsigned long x
)
262 return fls_ulong(x
- 1);
265 const char *get_domain_str(enum lttng_domain_type domain
)
270 case LTTNG_DOMAIN_KERNEL
:
271 str_dom
= str_kernel
;
273 case LTTNG_DOMAIN_UST
:
276 case LTTNG_DOMAIN_JUL
:
279 case LTTNG_DOMAIN_LOG4J
:
282 case LTTNG_DOMAIN_PYTHON
:
283 str_dom
= str_python
;
286 /* Should not have an unknown domain or else define it. */
294 * Spawn a lttng relayd daemon by forking and execv.
296 int spawn_relayd(const char *pathname
, int port
)
303 port
= DEFAULT_NETWORK_VIEWER_PORT
;
306 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
311 MSG("Spawning a relayd daemon");
315 * Spawn session daemon and tell
316 * it to signal us when ready.
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.");
325 kill(getppid(), SIGTERM
); /* wake parent */
327 } else if (pid
> 0) {
340 * Check if relayd is alive.
342 * Return 1 if found else 0 if NOT found. Negative value on error.
344 int check_relayd(void)
347 struct sockaddr_in sin
;
349 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
351 perror("socket check relayd");
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
);
360 perror("inet_pton check relayd");
366 * A successful connect means the relayd exists thus returning 0 else a
367 * negative value means it does NOT exists.
369 ret
= connect(fd
, &sin
, sizeof(sin
));
374 /* Already spawned. */
380 perror("close relayd fd");