Implement lttng-save and lttng-load for userspace-probe
[lttng-tools.git] / src / bin / lttng / utils.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
7 *
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.
12 *
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.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
b9dfb167 19#include <assert.h>
f3ed775e 20#include <stdlib.h>
679b4943 21#include <ctype.h>
3badf2bf 22#include <limits.h>
8960e9cd
DG
23#include <sys/types.h>
24#include <sys/socket.h>
25#include <signal.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
20fb9e02 28#include <inttypes.h>
4ba92f18 29#include <unistd.h>
f3ed775e 30
db758600 31#include <common/error.h>
feb0f3e5 32#include <common/utils.h>
4ba92f18 33#include <common/defaults.h>
f3ed775e 34
beb8c75a 35#include "conf.h"
679b4943 36#include "utils.h"
3c9bd23c 37#include "command.h"
f3ed775e 38
b9dfb167
DG
39static const char *str_kernel = "Kernel";
40static const char *str_ust = "UST";
41static const char *str_jul = "JUL";
5cdb6027 42static const char *str_log4j = "LOG4J";
0e115563 43static const char *str_python = "Python";
b9dfb167 44
1dac0189
PPM
45static
46char *_get_session_name(int quiet)
f3ed775e
DG
47{
48 char *path, *session_name = NULL;
49
50 /* Get path to config file */
feb0f3e5 51 path = utils_get_home_dir();
f3ed775e
DG
52 if (path == NULL) {
53 goto error;
54 }
55
56 /* Get session name from config */
1dac0189
PPM
57 session_name = quiet ? config_read_session_name_quiet(path) :
58 config_read_session_name(path);
f3ed775e 59 if (session_name == NULL) {
58a97671 60 goto error;
f3ed775e
DG
61 }
62
3183dbb0 63 DBG2("Config file path found: %s", path);
cd80958d 64 DBG("Session name found: %s", session_name);
f3ed775e 65 return session_name;
3183dbb0
DG
66
67error:
68 return NULL;
f3ed775e 69}
679b4943 70
1dac0189
PPM
71/*
72 * get_session_name
73 *
74 * Return allocated string with the session name found in the config
75 * directory.
76 */
77char *get_session_name(void)
78{
79 return _get_session_name(0);
80}
81
82/*
83 * get_session_name_quiet (no warnings/errors emitted)
84 *
85 * Return allocated string with the session name found in the config
86 * directory.
87 */
88char *get_session_name_quiet(void)
89{
90 return _get_session_name(1);
91}
92
3c9bd23c
SM
93/*
94 * list_commands
95 *
96 * List commands line by line. This is mostly for bash auto completion and to
97 * avoid difficult parsing.
98 */
99void list_commands(struct cmd_struct *commands, FILE *ofp)
100{
101 int i = 0;
102 struct cmd_struct *cmd = NULL;
103
104 cmd = &commands[i];
105 while (cmd->name != NULL) {
106 fprintf(ofp, "%s\n", cmd->name);
107 i++;
108 cmd = &commands[i];
109 }
110}
679b4943
SM
111
112/*
113 * list_cmd_options
114 *
115 * Prints a simple list of the options available to a command. This is intended
116 * to be easily parsed for bash completion.
117 */
118void list_cmd_options(FILE *ofp, struct poptOption *options)
119{
120 int i;
121 struct poptOption *option = NULL;
122
123 for (i = 0; options[i].longName != NULL; i++) {
124 option = &options[i];
125
126 fprintf(ofp, "--%s\n", option->longName);
127
128 if (isprint(option->shortName)) {
129 fprintf(ofp, "-%c\n", option->shortName);
130 }
131 }
132}
8ce58bad
MD
133
134/*
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).
138 */
139#if defined(__i386) || defined(__x86_64)
140static inline
141unsigned int fls_u32(uint32_t x)
142{
143 int r;
144
145 asm("bsrl %1,%0\n\t"
146 "jnz 1f\n\t"
147 "movl $-1,%0\n\t"
148 "1:\n\t"
149 : "=r" (r) : "rm" (x));
150 return r + 1;
151}
152#define HAS_FLS_U32
153#endif
154
155#if defined(__x86_64)
156static inline
157unsigned int fls_u64(uint64_t x)
158{
159 long r;
160
161 asm("bsrq %1,%0\n\t"
162 "jnz 1f\n\t"
163 "movq $-1,%0\n\t"
164 "1:\n\t"
165 : "=r" (r) : "rm" (x));
166 return r + 1;
167}
168#define HAS_FLS_U64
169#endif
170
171#ifndef HAS_FLS_U64
172static __attribute__((unused))
173unsigned int fls_u64(uint64_t x)
174{
175 unsigned int r = 64;
176
177 if (!x)
178 return 0;
179
180 if (!(x & 0xFFFFFFFF00000000ULL)) {
181 x <<= 32;
182 r -= 32;
183 }
184 if (!(x & 0xFFFF000000000000ULL)) {
185 x <<= 16;
186 r -= 16;
187 }
188 if (!(x & 0xFF00000000000000ULL)) {
189 x <<= 8;
190 r -= 8;
191 }
192 if (!(x & 0xF000000000000000ULL)) {
193 x <<= 4;
194 r -= 4;
195 }
196 if (!(x & 0xC000000000000000ULL)) {
197 x <<= 2;
198 r -= 2;
199 }
200 if (!(x & 0x8000000000000000ULL)) {
201 x <<= 1;
202 r -= 1;
203 }
204 return r;
205}
206#endif
207
208#ifndef HAS_FLS_U32
209static __attribute__((unused))
210unsigned int fls_u32(uint32_t x)
211{
212 unsigned int r = 32;
213
214 if (!x)
215 return 0;
216 if (!(x & 0xFFFF0000U)) {
217 x <<= 16;
218 r -= 16;
219 }
220 if (!(x & 0xFF000000U)) {
221 x <<= 8;
222 r -= 8;
223 }
224 if (!(x & 0xF0000000U)) {
225 x <<= 4;
226 r -= 4;
227 }
228 if (!(x & 0xC0000000U)) {
229 x <<= 2;
230 r -= 2;
231 }
232 if (!(x & 0x80000000U)) {
233 x <<= 1;
234 r -= 1;
235 }
236 return r;
237}
238#endif
239
240static
241unsigned int fls_ulong(unsigned long x)
242{
243#if (CAA_BITS_PER_LONG == 32)
244 return fls_u32(x);
245#else
246 return fls_u64(x);
247#endif
248}
249
250/*
251 * Return the minimum order for which x <= (1UL << order).
252 * Return -1 if x is 0.
253 */
254int get_count_order_u32(uint32_t x)
255{
256 if (!x)
257 return -1;
258
259 return fls_u32(x - 1);
260}
261
262/*
263 * Return the minimum order for which x <= (1UL << order).
264 * Return -1 if x is 0.
265 */
266int get_count_order_u64(uint64_t x)
267{
268 if (!x)
269 return -1;
270
271 return fls_u64(x - 1);
272}
273
274/*
275 * Return the minimum order for which x <= (1UL << order).
276 * Return -1 if x is 0.
277 */
278int get_count_order_ulong(unsigned long x)
279{
280 if (!x)
281 return -1;
282
283 return fls_ulong(x - 1);
284}
b9dfb167
DG
285
286const char *get_domain_str(enum lttng_domain_type domain)
287{
288 const char *str_dom;
289
290 switch (domain) {
291 case LTTNG_DOMAIN_KERNEL:
292 str_dom = str_kernel;
293 break;
294 case LTTNG_DOMAIN_UST:
295 str_dom = str_ust;
296 break;
297 case LTTNG_DOMAIN_JUL:
298 str_dom = str_jul;
299 break;
5cdb6027
DG
300 case LTTNG_DOMAIN_LOG4J:
301 str_dom = str_log4j;
302 break;
0e115563
DG
303 case LTTNG_DOMAIN_PYTHON:
304 str_dom = str_python;
305 break;
b9dfb167
DG
306 default:
307 /* Should not have an unknown domain or else define it. */
308 assert(0);
309 }
310
311 return str_dom;
312}
8960e9cd
DG
313
314/*
315 * Spawn a lttng relayd daemon by forking and execv.
316 */
317int spawn_relayd(const char *pathname, int port)
318{
319 int ret = 0;
320 pid_t pid;
321 char url[255];
322
323 if (!port) {
324 port = DEFAULT_NETWORK_VIEWER_PORT;
325 }
326
327 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
328 if (ret < 0) {
329 goto end;
330 }
331
332 MSG("Spawning a relayd daemon");
333 pid = fork();
334 if (pid == 0) {
335 /*
336 * Spawn session daemon and tell
337 * it to signal us when ready.
338 */
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.");
343 } else {
6f04ed72 344 PERROR("execlp");
8960e9cd
DG
345 }
346 kill(getppid(), SIGTERM); /* wake parent */
347 exit(EXIT_FAILURE);
348 } else if (pid > 0) {
349 goto end;
350 } else {
6f04ed72 351 PERROR("fork");
8960e9cd
DG
352 ret = -1;
353 goto end;
354 }
355
356end:
357 return ret;
358}
359
360/*
361 * Check if relayd is alive.
362 *
363 * Return 1 if found else 0 if NOT found. Negative value on error.
364 */
365int check_relayd(void)
366{
367 int ret, fd;
368 struct sockaddr_in sin;
369
370 fd = socket(AF_INET, SOCK_STREAM, 0);
371 if (fd < 0) {
6f04ed72 372 PERROR("socket check relayd");
dd02a4c1
DG
373 ret = -1;
374 goto error_socket;
8960e9cd
DG
375 }
376
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);
380 if (ret < 1) {
6f04ed72 381 PERROR("inet_pton check relayd");
dd02a4c1 382 ret = -1;
8960e9cd
DG
383 goto error;
384 }
385
386 /*
387 * A successful connect means the relayd exists thus returning 0 else a
388 * negative value means it does NOT exists.
389 */
56efeab3 390 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
8960e9cd
DG
391 if (ret < 0) {
392 /* Not found. */
393 ret = 0;
394 } else {
395 /* Already spawned. */
396 ret = 1;
397 }
398
8960e9cd 399error:
dd02a4c1 400 if (close(fd) < 0) {
6f04ed72 401 PERROR("close relayd fd");
dd02a4c1
DG
402 }
403error_socket:
404 return ret;
8960e9cd 405}
3ecec76a
PP
406
407int print_missing_or_multiple_domains(unsigned int sum)
408{
409 int ret = 0;
410
411 if (sum == 0) {
412 ERR("Please specify a domain (-k/-u/-j).");
413 ret = -1;
414 } else if (sum > 1) {
415 ERR("Multiple domains specified.");
416 ret = -1;
417 }
418
419 return ret;
420}
20fb9e02
JD
421
422/*
423 * Get the discarded events and lost packet counts.
424 */
425void print_session_stats(const char *session_name)
426{
3e8f2238 427 int count, nb_domains, domain_idx, channel_idx, session_idx;
20fb9e02
JD
428 struct lttng_domain *domains;
429 struct lttng_channel *channels;
3e8f2238
JG
430 uint64_t discarded_events_total = 0, lost_packets_total = 0;
431 struct lttng_session *sessions = NULL;
432 const struct lttng_session *selected_session = NULL;
433
434 count = lttng_list_sessions(&sessions);
435 if (count < 1) {
436 ERR("Failed to retrieve session descriptions while printing session statistics.");
437 goto end;
438 }
439
440 /* Identify the currently-selected sessions. */
441 for (session_idx = 0; session_idx < count; session_idx++) {
442 if (!strcmp(session_name, sessions[session_idx].name)) {
443 selected_session = &sessions[session_idx];
444 break;
445 }
446 }
447 if (!selected_session) {
448 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
449 goto end;
450 }
20fb9e02
JD
451
452 nb_domains = lttng_list_domains(session_name, &domains);
453 if (nb_domains < 0) {
454 goto end;
455 }
456 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
457 struct lttng_handle *handle = lttng_create_handle(session_name,
458 &domains[domain_idx]);
459
460 if (!handle) {
3e8f2238 461 ERR("Failed to create session handle while printing session statistics.");
20fb9e02
JD
462 goto end;
463 }
464
465 count = lttng_list_channels(handle, &channels);
466 for (channel_idx = 0; channel_idx < count; channel_idx++) {
467 int ret;
3e8f2238 468 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
469 struct lttng_channel *channel = &channels[channel_idx];
470
471 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 472 &discarded_events);
20fb9e02
JD
473 if (ret) {
474 ERR("Failed to retrieve discarded event count from channel %s",
475 channel->name);
476 }
477
478 ret = lttng_channel_get_lost_packet_count(channel,
3e8f2238 479 &lost_packets);
20fb9e02
JD
480 if (ret) {
481 ERR("Failed to retrieve lost packet count from channel %s",
482 channel->name);
483 }
484
3e8f2238
JG
485 discarded_events_total += discarded_events;
486 lost_packets_total += lost_packets;
20fb9e02
JD
487 }
488 lttng_destroy_handle(handle);
489 }
3e8f2238 490 if (discarded_events_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
491 MSG("[warning] %" PRIu64 " events discarded, please refer to "
492 "the documentation on channel configuration.",
3e8f2238 493 discarded_events_total);
20fb9e02 494 }
3e8f2238 495 if (lost_packets_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
496 MSG("[warning] %" PRIu64 " packets lost, please refer to "
497 "the documentation on channel configuration.",
3e8f2238 498 lost_packets_total);
20fb9e02
JD
499 }
500
501end:
3e8f2238 502 free(sessions);
20fb9e02 503}
4ba92f18 504
4fc83d94 505int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
506{
507 int ret;
508 char page_name[32];
509
510 ret = sprintf(page_name, "lttng-%s", cmd_name);
511 assert(ret > 0 && ret < 32);
4fc83d94
PP
512 ret = utils_show_help(1, page_name, help_msg);
513 if (ret && !help_msg) {
514 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
515 perror("exec");
516 }
4ba92f18 517
4fc83d94 518 return ret;
4ba92f18 519}
This page took 0.067445 seconds and 4 git commands to generate.