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";
46 * Return allocated string with the session name found in the config
49 char *get_session_name(void)
51 char *path
, *session_name
= NULL
;
53 /* Get path to config file */
54 path
= utils_get_home_dir();
59 /* Get session name from config */
60 session_name
= config_read_session_name(path
);
61 if (session_name
== NULL
) {
65 DBG2("Config file path found: %s", path
);
66 DBG("Session name found: %s", session_name
);
76 * List commands line by line. This is mostly for bash auto completion and to
77 * avoid difficult parsing.
79 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
82 struct cmd_struct
*cmd
= NULL
;
85 while (cmd
->name
!= NULL
) {
86 fprintf(ofp
, "%s\n", cmd
->name
);
95 * Prints a simple list of the options available to a command. This is intended
96 * to be easily parsed for bash completion.
98 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
101 struct poptOption
*option
= NULL
;
103 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
104 option
= &options
[i
];
106 fprintf(ofp
, "--%s\n", option
->longName
);
108 if (isprint(option
->shortName
)) {
109 fprintf(ofp
, "-%c\n", option
->shortName
);
115 * fls: returns the position of the most significant bit.
116 * Returns 0 if no bit is set, else returns the position of the most
117 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
119 #if defined(__i386) || defined(__x86_64)
121 unsigned int fls_u32(uint32_t x
)
129 : "=r" (r
) : "rm" (x
));
135 #if defined(__x86_64)
137 unsigned int fls_u64(uint64_t x
)
145 : "=r" (r
) : "rm" (x
));
152 static __attribute__((unused
))
153 unsigned int fls_u64(uint64_t x
)
160 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
164 if (!(x
& 0xFFFF000000000000ULL
)) {
168 if (!(x
& 0xFF00000000000000ULL
)) {
172 if (!(x
& 0xF000000000000000ULL
)) {
176 if (!(x
& 0xC000000000000000ULL
)) {
180 if (!(x
& 0x8000000000000000ULL
)) {
189 static __attribute__((unused
))
190 unsigned int fls_u32(uint32_t x
)
196 if (!(x
& 0xFFFF0000U
)) {
200 if (!(x
& 0xFF000000U
)) {
204 if (!(x
& 0xF0000000U
)) {
208 if (!(x
& 0xC0000000U
)) {
212 if (!(x
& 0x80000000U
)) {
221 unsigned int fls_ulong(unsigned long x
)
223 #if (CAA_BITS_PER_LONG == 32)
231 * Return the minimum order for which x <= (1UL << order).
232 * Return -1 if x is 0.
234 int get_count_order_u32(uint32_t x
)
239 return fls_u32(x
- 1);
243 * Return the minimum order for which x <= (1UL << order).
244 * Return -1 if x is 0.
246 int get_count_order_u64(uint64_t x
)
251 return fls_u64(x
- 1);
255 * Return the minimum order for which x <= (1UL << order).
256 * Return -1 if x is 0.
258 int get_count_order_ulong(unsigned long x
)
263 return fls_ulong(x
- 1);
266 const char *get_domain_str(enum lttng_domain_type domain
)
271 case LTTNG_DOMAIN_KERNEL
:
272 str_dom
= str_kernel
;
274 case LTTNG_DOMAIN_UST
:
277 case LTTNG_DOMAIN_JUL
:
280 case LTTNG_DOMAIN_LOG4J
:
283 case LTTNG_DOMAIN_PYTHON
:
284 str_dom
= str_python
;
287 /* Should not have an unknown domain or else define it. */
295 * Spawn a lttng relayd daemon by forking and execv.
297 int spawn_relayd(const char *pathname
, int port
)
304 port
= DEFAULT_NETWORK_VIEWER_PORT
;
307 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
312 MSG("Spawning a relayd daemon");
316 * Spawn session daemon and tell
317 * it to signal us when ready.
319 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
320 /* execlp only returns if error happened */
321 if (errno
== ENOENT
) {
322 ERR("No relayd found. Use --relayd-path.");
326 kill(getppid(), SIGTERM
); /* wake parent */
328 } else if (pid
> 0) {
341 * Check if relayd is alive.
343 * Return 1 if found else 0 if NOT found. Negative value on error.
345 int check_relayd(void)
348 struct sockaddr_in sin
;
350 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
352 perror("socket check relayd");
357 sin
.sin_family
= AF_INET
;
358 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
359 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
361 perror("inet_pton check relayd");
367 * A successful connect means the relayd exists thus returning 0 else a
368 * negative value means it does NOT exists.
370 ret
= connect(fd
, &sin
, sizeof(sin
));
375 /* Already spawned. */
381 perror("close relayd fd");