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";
43 char *_get_session_name(int quiet
)
45 char *path
, *session_name
= NULL
;
47 /* Get path to config file */
48 path
= utils_get_home_dir();
53 /* Get session name from config */
54 session_name
= quiet
? config_read_session_name_quiet(path
) :
55 config_read_session_name(path
);
56 if (session_name
== NULL
) {
60 DBG2("Config file path found: %s", path
);
61 DBG("Session name found: %s", session_name
);
71 * Return allocated string with the session name found in the config
74 char *get_session_name(void)
76 return _get_session_name(0);
80 * get_session_name_quiet (no warnings/errors emitted)
82 * Return allocated string with the session name found in the config
85 char *get_session_name_quiet(void)
87 return _get_session_name(1);
93 * List commands line by line. This is mostly for bash auto completion and to
94 * avoid difficult parsing.
96 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
99 struct cmd_struct
*cmd
= NULL
;
102 while (cmd
->name
!= NULL
) {
103 fprintf(ofp
, "%s\n", cmd
->name
);
112 * Prints a simple list of the options available to a command. This is intended
113 * to be easily parsed for bash completion.
115 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
118 struct poptOption
*option
= NULL
;
120 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
121 option
= &options
[i
];
123 fprintf(ofp
, "--%s\n", option
->longName
);
125 if (isprint(option
->shortName
)) {
126 fprintf(ofp
, "-%c\n", option
->shortName
);
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).
136 #if defined(__i386) || defined(__x86_64)
138 unsigned int fls_u32(uint32_t x
)
146 : "=r" (r
) : "rm" (x
));
152 #if defined(__x86_64)
154 unsigned int fls_u64(uint64_t x
)
162 : "=r" (r
) : "rm" (x
));
169 static __attribute__((unused
))
170 unsigned int fls_u64(uint64_t x
)
177 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
181 if (!(x
& 0xFFFF000000000000ULL
)) {
185 if (!(x
& 0xFF00000000000000ULL
)) {
189 if (!(x
& 0xF000000000000000ULL
)) {
193 if (!(x
& 0xC000000000000000ULL
)) {
197 if (!(x
& 0x8000000000000000ULL
)) {
206 static __attribute__((unused
))
207 unsigned int fls_u32(uint32_t x
)
213 if (!(x
& 0xFFFF0000U
)) {
217 if (!(x
& 0xFF000000U
)) {
221 if (!(x
& 0xF0000000U
)) {
225 if (!(x
& 0xC0000000U
)) {
229 if (!(x
& 0x80000000U
)) {
238 unsigned int fls_ulong(unsigned long x
)
240 #if (CAA_BITS_PER_LONG == 32)
248 * Return the minimum order for which x <= (1UL << order).
249 * Return -1 if x is 0.
251 int get_count_order_u32(uint32_t x
)
256 return fls_u32(x
- 1);
260 * Return the minimum order for which x <= (1UL << order).
261 * Return -1 if x is 0.
263 int get_count_order_u64(uint64_t x
)
268 return fls_u64(x
- 1);
272 * Return the minimum order for which x <= (1UL << order).
273 * Return -1 if x is 0.
275 int get_count_order_ulong(unsigned long x
)
280 return fls_ulong(x
- 1);
283 const char *get_domain_str(enum lttng_domain_type domain
)
288 case LTTNG_DOMAIN_KERNEL
:
289 str_dom
= str_kernel
;
291 case LTTNG_DOMAIN_UST
:
294 case LTTNG_DOMAIN_JUL
:
297 case LTTNG_DOMAIN_LOG4J
:
300 case LTTNG_DOMAIN_PYTHON
:
301 str_dom
= str_python
;
304 /* Should not have an unknown domain or else define it. */
312 * Spawn a lttng relayd daemon by forking and execv.
314 int spawn_relayd(const char *pathname
, int port
)
321 port
= DEFAULT_NETWORK_VIEWER_PORT
;
324 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
329 MSG("Spawning a relayd daemon");
333 * Spawn session daemon and tell
334 * it to signal us when ready.
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.");
343 kill(getppid(), SIGTERM
); /* wake parent */
345 } else if (pid
> 0) {
358 * Check if relayd is alive.
360 * Return 1 if found else 0 if NOT found. Negative value on error.
362 int check_relayd(void)
365 struct sockaddr_in sin
;
367 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
369 PERROR("socket check relayd");
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
);
378 PERROR("inet_pton check relayd");
384 * A successful connect means the relayd exists thus returning 0 else a
385 * negative value means it does NOT exists.
387 ret
= connect(fd
, &sin
, sizeof(sin
));
392 /* Already spawned. */
398 PERROR("close relayd fd");
404 int print_missing_or_multiple_domains(unsigned int sum
)
409 ERR("Please specify a domain (-k/-u/-j).");
411 } else if (sum
> 1) {
412 ERR("Multiple domains specified.");