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>
31 #include <common/error.h>
32 #include <common/utils.h>
33 #include <common/defaults.h>
39 static const char *str_kernel
= "Kernel";
40 static const char *str_ust
= "UST";
41 static const char *str_jul
= "JUL";
42 static const char *str_log4j
= "LOG4J";
43 static const char *str_python
= "Python";
46 char *_get_session_name(int quiet
)
48 char *path
, *session_name
= NULL
;
50 /* Get path to config file */
51 path
= utils_get_home_dir();
56 /* Get session name from config */
57 session_name
= quiet
? config_read_session_name_quiet(path
) :
58 config_read_session_name(path
);
59 if (session_name
== NULL
) {
63 DBG2("Config file path found: %s", path
);
64 DBG("Session name found: %s", session_name
);
74 * Return allocated string with the session name found in the config
77 char *get_session_name(void)
79 return _get_session_name(0);
83 * get_session_name_quiet (no warnings/errors emitted)
85 * Return allocated string with the session name found in the config
88 char *get_session_name_quiet(void)
90 return _get_session_name(1);
96 * List commands line by line. This is mostly for bash auto completion and to
97 * avoid difficult parsing.
99 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
102 struct cmd_struct
*cmd
= NULL
;
105 while (cmd
->name
!= NULL
) {
106 fprintf(ofp
, "%s\n", cmd
->name
);
115 * Prints a simple list of the options available to a command. This is intended
116 * to be easily parsed for bash completion.
118 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
121 struct poptOption
*option
= NULL
;
123 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
124 option
= &options
[i
];
126 fprintf(ofp
, "--%s\n", option
->longName
);
128 if (isprint(option
->shortName
)) {
129 fprintf(ofp
, "-%c\n", option
->shortName
);
135 * fls: returns the position of the most significant bit.
136 * Returns 0 if no bit is set, else returns the position of the most
137 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
139 #if defined(__i386) || defined(__x86_64)
141 unsigned int fls_u32(uint32_t x
)
149 : "=r" (r
) : "rm" (x
));
155 #if defined(__x86_64)
157 unsigned int fls_u64(uint64_t x
)
165 : "=r" (r
) : "rm" (x
));
172 static __attribute__((unused
))
173 unsigned int fls_u64(uint64_t x
)
180 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
184 if (!(x
& 0xFFFF000000000000ULL
)) {
188 if (!(x
& 0xFF00000000000000ULL
)) {
192 if (!(x
& 0xF000000000000000ULL
)) {
196 if (!(x
& 0xC000000000000000ULL
)) {
200 if (!(x
& 0x8000000000000000ULL
)) {
209 static __attribute__((unused
))
210 unsigned int fls_u32(uint32_t x
)
216 if (!(x
& 0xFFFF0000U
)) {
220 if (!(x
& 0xFF000000U
)) {
224 if (!(x
& 0xF0000000U
)) {
228 if (!(x
& 0xC0000000U
)) {
232 if (!(x
& 0x80000000U
)) {
241 unsigned int fls_ulong(unsigned long x
)
243 #if (CAA_BITS_PER_LONG == 32)
251 * Return the minimum order for which x <= (1UL << order).
252 * Return -1 if x is 0.
254 int get_count_order_u32(uint32_t x
)
259 return fls_u32(x
- 1);
263 * Return the minimum order for which x <= (1UL << order).
264 * Return -1 if x is 0.
266 int get_count_order_u64(uint64_t x
)
271 return fls_u64(x
- 1);
275 * Return the minimum order for which x <= (1UL << order).
276 * Return -1 if x is 0.
278 int get_count_order_ulong(unsigned long x
)
283 return fls_ulong(x
- 1);
286 const char *get_domain_str(enum lttng_domain_type domain
)
291 case LTTNG_DOMAIN_KERNEL
:
292 str_dom
= str_kernel
;
294 case LTTNG_DOMAIN_UST
:
297 case LTTNG_DOMAIN_JUL
:
300 case LTTNG_DOMAIN_LOG4J
:
303 case LTTNG_DOMAIN_PYTHON
:
304 str_dom
= str_python
;
307 /* Should not have an unknown domain or else define it. */
315 * Spawn a lttng relayd daemon by forking and execv.
317 int spawn_relayd(const char *pathname
, int port
)
324 port
= DEFAULT_NETWORK_VIEWER_PORT
;
327 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
332 MSG("Spawning a relayd daemon");
336 * Spawn session daemon and tell
337 * it to signal us when ready.
339 execlp(pathname
, "lttng-relayd", "-L", url
, NULL
);
340 /* execlp only returns if error happened */
341 if (errno
== ENOENT
) {
342 ERR("No relayd found. Use --relayd-path.");
346 kill(getppid(), SIGTERM
); /* wake parent */
348 } else if (pid
> 0) {
361 * Check if relayd is alive.
363 * Return 1 if found else 0 if NOT found. Negative value on error.
365 int check_relayd(void)
368 struct sockaddr_in sin
;
370 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
372 PERROR("socket check relayd");
377 sin
.sin_family
= AF_INET
;
378 sin
.sin_port
= htons(DEFAULT_NETWORK_VIEWER_PORT
);
379 ret
= inet_pton(sin
.sin_family
, "127.0.0.1", &sin
.sin_addr
);
381 PERROR("inet_pton check relayd");
387 * A successful connect means the relayd exists thus returning 0 else a
388 * negative value means it does NOT exists.
390 ret
= connect(fd
, (struct sockaddr
*) &sin
, sizeof(sin
));
395 /* Already spawned. */
401 PERROR("close relayd fd");
407 int print_missing_or_multiple_domains(unsigned int sum
)
412 ERR("Please specify a domain (-k/-u/-j).");
414 } else if (sum
> 1) {
415 ERR("Multiple domains specified.");
423 * Get the discarded events and lost packet counts.
425 void print_session_stats(const char *session_name
)
427 int count
, nb_domains
, domain_idx
, channel_idx
;
428 struct lttng_domain
*domains
;
429 struct lttng_channel
*channels
;
430 uint64_t discarded_total
= 0, lost_total
= 0;
432 nb_domains
= lttng_list_domains(session_name
, &domains
);
433 if (nb_domains
< 0) {
436 for (domain_idx
= 0; domain_idx
< nb_domains
; domain_idx
++) {
437 struct lttng_handle
*handle
= lttng_create_handle(session_name
,
438 &domains
[domain_idx
]);
441 ERR("Failed to create session handle while printing session stats.");
445 count
= lttng_list_channels(handle
, &channels
);
446 for (channel_idx
= 0; channel_idx
< count
; channel_idx
++) {
448 uint64_t discarded
= 0, lost
= 0;
449 struct lttng_channel
*channel
= &channels
[channel_idx
];
451 ret
= lttng_channel_get_discarded_event_count(channel
,
454 ERR("Failed to retrieve discarded event count from channel %s",
458 ret
= lttng_channel_get_lost_packet_count(channel
,
461 ERR("Failed to retrieve lost packet count from channel %s",
465 discarded_total
+= discarded
;
468 lttng_destroy_handle(handle
);
470 if (discarded_total
> 0) {
471 MSG("[warning] %" PRIu64
" events discarded, please refer to "
472 "the documentation on channel configuration.",
475 if (lost_total
> 0) {
476 MSG("[warning] %" PRIu64
" packets lost, please refer to "
477 "the documentation on channel configuration.",
485 int show_cmd_man_page(const char *cmd_name
)
490 ret
= sprintf(page_name
, "lttng-%s", cmd_name
);
491 assert(ret
> 0 && ret
< 32);
493 return utils_show_man_page(1, page_name
);