1ebe590ad279915e8e3e7ba05784e0333eb9064f
[lttng-tools.git] / src / bin / lttng / commands / create.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <ctype.h>
22 #include <popt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include <common/mi-lttng.h>
32
33 #include "../command.h"
34 #include "../utils.h"
35
36 #include <common/defaults.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/uri.h>
39 #include <common/utils.h>
40 #include <lttng/snapshot.h>
41
42 static char *opt_output_path;
43 static char *opt_session_name;
44 static char *opt_url;
45 static char *opt_ctrl_url;
46 static char *opt_data_url;
47 static int opt_no_consumer;
48 static int opt_no_output;
49 static int opt_snapshot;
50 static unsigned int opt_live_timer;
51
52 enum {
53 OPT_HELP = 1,
54 OPT_LIST_OPTIONS,
55 OPT_LIVE_TIMER,
56 };
57
58 static struct mi_writer *writer;
59
60 static struct poptOption long_options[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
63 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
64 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
65 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
66 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
67 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
68 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
69 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
70 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
71 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
72 {0, 0, 0, 0, 0, 0, 0}
73 };
74
75 /*
76 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
77 * why this declaration exists and used ONLY in for this command.
78 */
79 extern int _lttng_create_session_ext(const char *name, const char *url,
80 const char *datetime, int live_timer);
81
82 /*
83 * usage
84 */
85 static void usage(FILE *ofp)
86 {
87 fprintf(ofp, "usage: lttng create [NAME] [OPTIONS] \n");
88 fprintf(ofp, "\n");
89 fprintf(ofp, "Without a given NAME, the default is 'auto-<yyyymmdd>-<hhmmss>'\n");
90 fprintf(ofp, "\n");
91 fprintf(ofp, "Options:\n");
92 fprintf(ofp, " -h, --help Show this help\n");
93 fprintf(ofp, " --list-options Simple listing of options\n");
94 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
95 fprintf(ofp, " --no-output Traces will not be outputted\n");
96 fprintf(ofp, " --snapshot Set the session in snapshot mode.\n");
97 fprintf(ofp, " Created in no-output mode and uses the URL,\n");
98 fprintf(ofp, " if one, as the default snapshot output.\n");
99 fprintf(ofp, " Every channel will be set in overwrite mode\n");
100 fprintf(ofp, " and with mmap output (splice not supported).\n");
101 fprintf(ofp, " --live [USEC] Set the session in live-reading mode.\n");
102 fprintf(ofp, " The delay parameter in micro-seconds is the\n");
103 fprintf(ofp, " maximum time the user can wait for the data\n");
104 fprintf(ofp, " to be flushed. Can be set with a network\n");
105 fprintf(ofp, " URL (-U or -C/-D) and must have a relayd listening.\n");
106 fprintf(ofp, " By default, %u is used for the timer and the\n",
107 DEFAULT_LTTNG_LIVE_TIMER);
108 fprintf(ofp, " network URL is set to net://127.0.0.1.\n");
109 fprintf(ofp, "\n");
110 fprintf(ofp, "Extended Options:\n");
111 fprintf(ofp, "\n");
112 fprintf(ofp, "Using these options, each API call can be controlled individually.\n");
113 fprintf(ofp, "\n");
114 fprintf(ofp, " -U, --set-url=URL Set URL destination of the trace data.\n");
115 fprintf(ofp, " It is persistent for the session lifetime.\n");
116 fprintf(ofp, " This will set both data and control URL.\n");
117 fprintf(ofp, " -C, --ctrl-url=URL Set control path URL. (Must use -D also)\n");
118 fprintf(ofp, " -D, --data-url=URL Set data path URL. (Must use -C also)\n");
119 fprintf(ofp, "\n");
120 fprintf(ofp, "Please refer to the man page (lttng(1)) for more information on network\n");
121 fprintf(ofp, "streaming mechanisms and explanation of the control and data port\n");
122 fprintf(ofp, "You must have a running remote lttng-relayd for network streaming\n");
123 fprintf(ofp, "\n");
124 fprintf(ofp, "URL format is has followed:\n");
125 fprintf(ofp, "\n");
126 fprintf(ofp, " proto://[HOST|IP][:PORT1[:PORT2]][/TRACE_PATH]\n");
127 fprintf(ofp, "\n");
128 fprintf(ofp, " Supported protocols are (proto):\n");
129 fprintf(ofp, " > file://...\n");
130 fprintf(ofp, " Local filesystem full path.\n");
131 fprintf(ofp, "\n");
132 fprintf(ofp, " > net[6]://...\n");
133 fprintf(ofp, " This will use the default network transport layer which is\n");
134 fprintf(ofp, " TCP for both control (PORT1) and data port (PORT2).\n");
135 fprintf(ofp, " The default ports are respectively 5342 and 5343.\n");
136 fprintf(ofp, "\n");
137 fprintf(ofp, " > tcp[6]://...\n");
138 fprintf(ofp, " Can only be used with -C and -D together\n");
139 fprintf(ofp, "\n");
140 fprintf(ofp, "NOTE: IPv6 address MUST be enclosed in brackets '[]' (rfc2732)\n");
141 fprintf(ofp, "\n");
142 fprintf(ofp, "Examples:\n");
143 fprintf(ofp, " # lttng create -U net://192.168.1.42\n");
144 fprintf(ofp, " Uses TCP and default ports for the given destination.\n");
145 fprintf(ofp, " # lttng create -U net6://[fe80::f66d:4ff:fe53:d220]\n");
146 fprintf(ofp, " Uses TCP, default ports and IPv6.\n");
147 fprintf(ofp, " # lttng create s1 -U net://myhost.com:3229\n");
148 fprintf(ofp, " Set the consumer to the remote HOST on port 3229 for control.\n");
149 fprintf(ofp, "\n");
150 }
151
152 /*
153 * Retrieve the created session and mi output it based on provided argument
154 * This is currently a summary of what was pretty printed and is subject to
155 * enhancements.
156 */
157 static int mi_created_session(const char *session_name)
158 {
159 int ret, i, count, found;
160 struct lttng_session *sessions;
161
162 /* session_name should not be null */
163 assert(session_name);
164 assert(writer);
165
166 count = lttng_list_sessions(&sessions);
167 if (count < 0) {
168 ret = count;
169 ERR("%s", lttng_strerror(ret));
170 goto error;
171 }
172
173 if (count == 0) {
174 ERR("Error session creation failed: session %s not found", session_name);
175 ret = -LTTNG_ERR_SESS_NOT_FOUND;
176 goto end;
177 }
178
179 found = 0;
180 for (i = 0; i < count; i++) {
181 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
182 found = 1;
183 ret = mi_lttng_session(writer, &sessions[i], 0);
184 if (ret) {
185 goto error;
186 }
187 break;
188 }
189 }
190
191 if (!found) {
192 ret = -LTTNG_ERR_SESS_NOT_FOUND;
193 } else {
194 ret = CMD_SUCCESS;
195 }
196
197 error:
198 free(sessions);
199 end:
200 return ret;
201 }
202
203 /*
204 * For a session name, set the consumer URLs.
205 */
206 static int set_consumer_url(const char *session_name, const char *ctrl_url,
207 const char *data_url)
208 {
209 int ret;
210 struct lttng_handle *handle;
211 struct lttng_domain dom;
212
213 assert(session_name);
214
215 /*
216 * Set handle with the session name and the domain set to 0. This means to
217 * the session daemon that the next action applies on the tracing session
218 * rather then the domain specific session.
219 */
220 memset(&dom, 0, sizeof(dom));
221
222 handle = lttng_create_handle(session_name, &dom);
223 if (handle == NULL) {
224 ret = CMD_FATAL;
225 goto error;
226 }
227
228 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
229 if (ret < 0) {
230 goto error;
231 }
232
233 if (ctrl_url) {
234 MSG("Control URL %s set for session %s", ctrl_url, session_name);
235 }
236
237 if (data_url) {
238 MSG("Data URL %s set for session %s", data_url, session_name);
239 }
240
241 error:
242 lttng_destroy_handle(handle);
243 return ret;
244 }
245
246 static int add_snapshot_output(const char *session_name, const char *ctrl_url,
247 const char *data_url)
248 {
249 int ret;
250 struct lttng_snapshot_output *output = NULL;
251
252 assert(session_name);
253
254 output = lttng_snapshot_output_create();
255 if (!output) {
256 ret = CMD_FATAL;
257 goto error_create;
258 }
259
260 if (ctrl_url) {
261 ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output);
262 if (ret < 0) {
263 goto error;
264 }
265 }
266
267 if (data_url) {
268 ret = lttng_snapshot_output_set_data_url(data_url, output);
269 if (ret < 0) {
270 goto error;
271 }
272 }
273
274 /* This call, if successful, populates the id of the output object. */
275 ret = lttng_snapshot_add_output(session_name, output);
276 if (ret < 0) {
277 goto error;
278 }
279
280 error:
281 lttng_snapshot_output_destroy(output);
282 error_create:
283 return ret;
284 }
285
286 /*
287 * Create a tracing session.
288 * If no name is specified, a default name is generated.
289 *
290 * Returns one of the CMD_* result constants.
291 */
292 static int create_session(void)
293 {
294 int ret;
295 char *session_name = NULL, *traces_path = NULL, *alloc_path = NULL;
296 char *alloc_url = NULL, *url = NULL, datetime[16];
297 char session_name_date[NAME_MAX + 17], *print_str_url = NULL;
298 time_t rawtime;
299 struct tm *timeinfo;
300
301 /* Get date and time for automatic session name/path */
302 time(&rawtime);
303 timeinfo = localtime(&rawtime);
304 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
305
306 /* Auto session name creation */
307 if (opt_session_name == NULL) {
308 ret = snprintf(session_name_date, sizeof(session_name_date),
309 DEFAULT_SESSION_NAME "-%s", datetime);
310 if (ret < 0) {
311 PERROR("snprintf session name");
312 goto error;
313 }
314 session_name = session_name_date;
315 DBG("Auto session name set to %s", session_name_date);
316 } else {
317 if (strlen(opt_session_name) > NAME_MAX) {
318 ERR("Session name too long. Length must be lower or equal to %d",
319 NAME_MAX);
320 ret = LTTNG_ERR_SESSION_FAIL;
321 goto error;
322 }
323 /*
324 * Check if the session name begins with "auto-" or is exactly "auto".
325 * Both are reserved for the default session name. See bug #449 to
326 * understand why we need to check both here.
327 */
328 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
329 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
330 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
331 strlen(DEFAULT_SESSION_NAME)) == 0 &&
332 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
333 ERR("%s is a reserved keyword for default session(s)",
334 DEFAULT_SESSION_NAME);
335 ret = CMD_ERROR;
336 goto error;
337 }
338 session_name = opt_session_name;
339 ret = snprintf(session_name_date, sizeof(session_name_date),
340 "%s-%s", session_name, datetime);
341 if (ret < 0) {
342 PERROR("snprintf session name");
343 goto error;
344 }
345 }
346
347 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
348 ERR("You need both control and data URL.");
349 ret = CMD_ERROR;
350 goto error;
351 }
352
353 if (opt_output_path != NULL) {
354 traces_path = utils_expand_path(opt_output_path);
355 if (traces_path == NULL) {
356 ret = CMD_ERROR;
357 goto error;
358 }
359
360 /* Create URL string from the local file system path */
361 ret = asprintf(&alloc_url, "file://%s", traces_path);
362 if (ret < 0) {
363 PERROR("asprintf url path");
364 ret = CMD_FATAL;
365 goto error;
366 }
367 /* URL to use in the lttng_create_session() call */
368 url = alloc_url;
369 print_str_url = traces_path;
370 } else if (opt_url) { /* Handling URL (-U opt) */
371 url = opt_url;
372 print_str_url = url;
373 } else if (opt_data_url && opt_ctrl_url) {
374 /*
375 * With both control and data, we'll be setting the consumer URL after
376 * session creation thus use no URL.
377 */
378 url = NULL;
379 } else if (!opt_no_output) {
380 char *tmp_path;
381
382 /* Auto output path */
383 tmp_path = utils_get_home_dir();
384 if (tmp_path == NULL) {
385 ERR("HOME path not found.\n \
386 Please specify an output path using -o, --output PATH");
387 ret = CMD_FATAL;
388 goto error;
389 }
390 alloc_path = strdup(tmp_path);
391 if (!alloc_path) {
392 PERROR("allocating alloc_path");
393 ret = CMD_FATAL;
394 goto error;
395 }
396 ret = asprintf(&alloc_url,
397 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
398 alloc_path, session_name_date);
399 if (ret < 0) {
400 PERROR("asprintf trace dir name");
401 ret = CMD_FATAL;
402 goto error;
403 }
404
405 url = alloc_url;
406 print_str_url = alloc_url + strlen("file://");
407 } else {
408 /* No output means --no-output or --snapshot mode. */
409 url = NULL;
410 }
411
412 /* Use default live URL if NO url is/are found. */
413 if ((opt_live_timer && !opt_url) && (opt_live_timer && !opt_data_url)) {
414 ret = asprintf(&alloc_url, "net://127.0.0.1");
415 if (ret < 0) {
416 PERROR("asprintf default live URL");
417 ret = CMD_FATAL;
418 goto error;
419 }
420 url = alloc_url;
421 print_str_url = url;
422 }
423
424 if (opt_snapshot && opt_live_timer) {
425 ERR("Snapshot and live modes are mutually exclusive.");
426 ret = CMD_ERROR;
427 goto error;
428 }
429
430 if (opt_snapshot) {
431 /* No output by default. */
432 const char *snapshot_url = NULL;
433
434 if (opt_url) {
435 snapshot_url = url;
436 } else if (!opt_data_url && !opt_ctrl_url) {
437 /* This is the session path that we need to use as output. */
438 snapshot_url = url;
439 }
440 ret = lttng_create_session_snapshot(session_name, snapshot_url);
441 } else if (opt_live_timer) {
442 const char *pathname;
443
444 if (opt_relayd_path) {
445 pathname = opt_relayd_path;
446 } else {
447 pathname = INSTALL_BIN_PATH "/lttng-relayd";
448 }
449 if (!opt_url && !opt_data_url && !check_relayd() &&
450 spawn_relayd(pathname, 0) < 0) {
451 goto error;
452 }
453 ret = lttng_create_session_live(session_name, url, opt_live_timer);
454 } else {
455 ret = _lttng_create_session_ext(session_name, url, datetime, -1);
456 }
457 if (ret < 0) {
458 /* Don't set ret so lttng can interpret the sessiond error. */
459 switch (-ret) {
460 case LTTNG_ERR_EXIST_SESS:
461 WARN("Session %s already exists", session_name);
462 break;
463 default:
464 break;
465 }
466 goto error;
467 }
468
469 if (opt_ctrl_url && opt_data_url) {
470 if (opt_snapshot) {
471 ret = add_snapshot_output(session_name, opt_ctrl_url,
472 opt_data_url);
473 } else {
474 /* Setting up control URI (-C or/and -D opt) */
475 ret = set_consumer_url(session_name, opt_ctrl_url, opt_data_url);
476 }
477 if (ret < 0) {
478 /* Destroy created session because the URL are not valid. */
479 lttng_destroy_session(session_name);
480 goto error;
481 }
482 }
483
484 MSG("Session %s created.", session_name);
485 if (print_str_url && !opt_snapshot) {
486 MSG("Traces will be written in %s", print_str_url);
487
488 if (opt_live_timer) {
489 MSG("Live timer set to %u usec", opt_live_timer);
490 }
491 } else if (opt_snapshot) {
492 if (print_str_url) {
493 MSG("Default snapshot output set to: %s", print_str_url);
494 }
495 MSG("Snapshot mode set. Every channel enabled for that session will "
496 "be set in overwrite mode and mmap output.");
497 }
498
499 /* Mi output */
500 if (lttng_opt_mi) {
501 ret = mi_created_session(session_name);
502 if (ret) {
503 ret = CMD_ERROR;
504 goto error;
505 }
506 }
507
508 /* Init lttng session config */
509 ret = config_init(session_name);
510 if (ret < 0) {
511 ret = CMD_ERROR;
512 goto error;
513 }
514
515 ret = CMD_SUCCESS;
516
517 error:
518 free(alloc_url);
519 free(traces_path);
520 free(alloc_path);
521
522 if (ret < 0) {
523 ERR("%s", lttng_strerror(ret));
524 }
525 return ret;
526 }
527
528 /*
529 * The 'create <options>' first level command
530 *
531 * Returns one of the CMD_* result constants.
532 */
533 int cmd_create(int argc, const char **argv)
534 {
535 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
536 char *opt_arg = NULL;
537 static poptContext pc;
538
539 pc = poptGetContext(NULL, argc, argv, long_options, 0);
540 poptReadDefaultConfig(pc, 0);
541
542 while ((opt = poptGetNextOpt(pc)) != -1) {
543 switch (opt) {
544 case OPT_HELP:
545 usage(stdout);
546 goto end;
547 case OPT_LIST_OPTIONS:
548 list_cmd_options(stdout, long_options);
549 goto end;
550 case OPT_LIVE_TIMER:
551 {
552 unsigned long v;
553
554 errno = 0;
555 opt_arg = poptGetOptArg(pc);
556 if (!opt_arg) {
557 /* Set up default values. */
558 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
559 DBG("Session live timer interval set to default value %d",
560 opt_live_timer);
561 break;
562 }
563
564 v = strtoul(opt_arg, NULL, 0);
565 if (errno != 0 || !isdigit(opt_arg[0])) {
566 ERR("Wrong value in --live parameter: %s", opt_arg);
567 ret = CMD_ERROR;
568 goto end;
569 }
570 if (v != (uint32_t) v) {
571 ERR("32-bit overflow in --live parameter: %s", opt_arg);
572 ret = CMD_ERROR;
573 goto end;
574 }
575 opt_live_timer = (uint32_t) v;
576 DBG("Session live timer interval set to %d", opt_live_timer);
577 break;
578 }
579 default:
580 usage(stderr);
581 ret = CMD_UNDEFINED;
582 goto end;
583 }
584 }
585
586 if (opt_no_consumer) {
587 MSG("The option --no-consumer is obsolete. Use --no-output now.");
588 ret = CMD_WARNING;
589 goto end;
590 }
591
592 /* MI initialization */
593 if (lttng_opt_mi) {
594 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
595 if (!writer) {
596 ret = -LTTNG_ERR_NOMEM;
597 goto end;
598 }
599
600 /* Open command element */
601 ret = mi_lttng_writer_command_open(writer,
602 mi_lttng_element_command_create);
603 if (ret) {
604 ret = CMD_ERROR;
605 goto end;
606 }
607
608 /* Open output element */
609 ret = mi_lttng_writer_open_element(writer,
610 mi_lttng_element_command_output);
611 if (ret) {
612 ret = CMD_ERROR;
613 goto end;
614 }
615 }
616 opt_session_name = (char*) poptGetArg(pc);
617
618 command_ret = create_session();
619 if (command_ret) {
620 success = 0;
621 }
622
623 if (lttng_opt_mi) {
624 /* Close output element */
625 ret = mi_lttng_writer_close_element(writer);
626 if (ret) {
627 ret = CMD_ERROR;
628 goto end;
629 }
630
631 /* Success ? */
632 ret = mi_lttng_writer_write_element_bool(writer,
633 mi_lttng_element_command_success, success);
634 if (ret) {
635 ret = CMD_ERROR;
636 goto end;
637 }
638
639 /* Command element close */
640 ret = mi_lttng_writer_command_close(writer);
641 if (ret) {
642 ret = CMD_ERROR;
643 goto end;
644 }
645 }
646
647 end:
648 /* Mi clean-up */
649 if (writer && mi_lttng_writer_destroy(writer)) {
650 /* Preserve original error code */
651 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
652 }
653
654 /* Overwrite ret if an error occurred in create_session() */
655 ret = command_ret ? command_ret : ret;
656
657 poptFreeContext(pc);
658 return ret;
659 }
This page took 0.043571 seconds and 4 git commands to generate.