2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/compat/time.hpp>
19 #include <sys/types.h>
22 #include <unordered_map>
24 #include <common/mi-lttng.hpp>
26 #include "../command.hpp"
27 #include "../utils.hpp"
29 #include <common/defaults.hpp>
30 #include <common/sessiond-comm/sessiond-comm.hpp>
31 #include <common/uri.hpp>
32 #include <common/utils.hpp>
33 #include <common/path.hpp>
34 #include <lttng/lttng.h>
36 static char *opt_output_path
;
37 static char *opt_session_name
;
39 static char *opt_ctrl_url
;
40 static char *opt_data_url
;
41 static char *opt_shm_path
;
42 static char *opt_trace_format
;
43 static int opt_no_consumer
;
44 static int opt_no_output
;
45 static int opt_snapshot
;
46 static uint32_t opt_live_timer
;
48 #ifdef LTTNG_EMBED_HELP
49 static const char help_msg
[] =
50 #include <lttng-create.1.h>
67 static struct mi_writer
*writer
;
68 static struct poptOption long_options
[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
70 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
71 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
72 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
73 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
74 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
75 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
76 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
77 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
78 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
79 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
80 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
81 {"trace-format", 0, POPT_ARG_STRING
, &opt_trace_format
, 0, 0, 0},
86 * Retrieve the created session and mi output it based on provided argument
87 * This is currently a summary of what was pretty printed and is subject to
90 static int mi_created_session(const char *session_name
)
92 int ret
, i
, count
, found
;
93 struct lttng_session
*sessions
;
95 /* session_name should not be null */
96 LTTNG_ASSERT(session_name
);
99 count
= lttng_list_sessions(&sessions
);
102 ERR("%s", lttng_strerror(ret
));
107 ERR("Error session creation failed: session %s not found", session_name
);
108 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
113 for (i
= 0; i
< count
; i
++) {
114 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
116 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
125 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
137 struct lttng_session_descriptor
*create_session_descriptor(void)
140 enum output_type output_type
;
141 struct lttng_uri
*uris
= NULL
;
142 struct lttng_session_descriptor
*descriptor
= NULL
;
143 const char *uri_str1
= NULL
, *uri_str2
= NULL
;
144 char local_output_path
[LTTNG_PATH_MAX
] = {};
147 output_type
= OUTPUT_NONE
;
148 } else if (opt_output_path
) {
149 char *expanded_output_path
;
152 output_type
= OUTPUT_LOCAL
;
153 expanded_output_path
= utils_expand_path(opt_output_path
);
154 if (!expanded_output_path
) {
155 ERR("Failed to expand output path.");
158 ret
= lttng_strncpy(local_output_path
, expanded_output_path
,
159 sizeof(local_output_path
));
160 free(expanded_output_path
);
162 ERR("Output path exceeds the maximal supported length (%zu bytes)",
163 sizeof(local_output_path
));
166 } else if (opt_url
|| opt_ctrl_url
) {
169 uri_str1
= opt_ctrl_url
? opt_ctrl_url
: opt_url
;
170 uri_str2
= opt_data_url
;
172 uri_count
= uri_parse_str_urls(uri_str1
, uri_str2
, &uris
);
173 if (uri_count
!= 1 && uri_count
!= 2) {
174 ERR("Unrecognized URL format.");
180 output_type
= OUTPUT_LOCAL
;
181 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
182 ERR("Unrecognized URL format.");
185 ret
= lttng_strncpy(local_output_path
, uris
[0].dst
.path
,
186 sizeof(local_output_path
));
188 ERR("Output path exceeds the maximal supported length (%zu bytes)",
189 sizeof(local_output_path
));
193 output_type
= OUTPUT_NETWORK
;
196 /* Already checked. */
200 output_type
= OUTPUT_UNSPECIFIED
;
204 /* Snapshot session. */
205 switch (output_type
) {
206 case OUTPUT_UNSPECIFIED
:
208 descriptor
= lttng_session_descriptor_snapshot_local_create(
210 output_type
== OUTPUT_LOCAL
?
211 local_output_path
: NULL
);
214 descriptor
= lttng_session_descriptor_snapshot_create(
218 descriptor
= lttng_session_descriptor_snapshot_network_create(
219 opt_session_name
, uri_str1
, uri_str2
);
224 } else if (opt_live_timer
) {
226 if (output_type
!= OUTPUT_UNSPECIFIED
&&
227 output_type
!= OUTPUT_NETWORK
) {
228 ERR("Unsupported output type specified for live session.");
231 descriptor
= lttng_session_descriptor_live_network_create(
232 opt_session_name
, uri_str1
, uri_str2
,
235 /* Regular session. */
236 switch (output_type
) {
237 case OUTPUT_UNSPECIFIED
:
239 descriptor
= lttng_session_descriptor_local_create(
241 output_type
== OUTPUT_LOCAL
?
242 local_output_path
: NULL
);
245 descriptor
= lttng_session_descriptor_create(
249 descriptor
= lttng_session_descriptor_network_create(
250 opt_session_name
, uri_str1
, uri_str2
);
257 ERR("Failed to initialize session creation command.");
260 * Auto-launch the relay daemon when a live session
261 * is created using default URLs.
263 if (!opt_url
&& !opt_ctrl_url
&& !opt_data_url
&&
264 opt_live_timer
&& !check_relayd()) {
266 const char *pathname
= opt_relayd_path
? :
267 INSTALL_BIN_PATH
"/lttng-relayd";
269 ret
= spawn_relayd(pathname
, 0);
276 if (opt_trace_format
) {
277 std::unordered_map
<std::string
,
278 std::function
<struct lttng_trace_format_descriptor
*()>>
280 {"ctf1", lttng_trace_format_ctf_1_descriptor_create
},
281 {"ctf2", lttng_trace_format_ctf_2_descriptor_create
},
284 lttng_session_descriptor_status status
;
285 lttng_trace_format_descriptor
*trace_format_descriptor
= nullptr;
286 std::string
s_trace_format(opt_trace_format
);
288 auto it
= factory
.find(s_trace_format
);
290 if (it
== factory
.end()) {
291 ERR("Unknown trace format");
295 trace_format_descriptor
= it
->second();
296 if (trace_format_descriptor
== nullptr) {
297 ERR("Failed to create trace format descriptor for trace format %s",
302 status
= lttng_session_descriptor_set_trace_format_descriptor(
303 descriptor
, trace_format_descriptor
);
304 lttng_trace_format_descriptor_destroy(trace_format_descriptor
);
305 if (status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
306 ERR("Failed to set trace format descriptor");
314 lttng_session_descriptor_destroy(descriptor
);
320 * Create a tracing session.
321 * If no name is specified, a default name is generated.
323 * Returns one of the CMD_* result constants.
325 static int create_session(void)
328 char shm_path
[LTTNG_PATH_MAX
] = {};
329 struct lttng_session_descriptor
*session_descriptor
= NULL
;
330 enum lttng_session_descriptor_status descriptor_status
;
331 enum lttng_error_code ret_code
;
332 struct lttng_session
*sessions
= NULL
;
333 const struct lttng_session
*created_session
= NULL
;
334 const char *created_session_name
;
336 /* Validate options. */
337 if (opt_session_name
) {
338 if (strlen(opt_session_name
) > NAME_MAX
) {
339 ERR("Session name too long. Length must be lower or equal to %d",
345 * Check if the session name begins with "auto-" or is exactly "auto".
346 * Both are reserved for the default session name. See bug #449 to
347 * understand why we need to check both here.
349 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
350 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
351 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
352 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
353 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
354 ERR("%s is a reserved keyword for default session(s)",
355 DEFAULT_SESSION_NAME
);
361 if (opt_snapshot
&& opt_live_timer
) {
362 ERR("Snapshot and live modes are mutually exclusive.");
367 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
368 ERR("Both control and data URLs must be specified.");
373 session_descriptor
= create_session_descriptor();
374 if (!session_descriptor
) {
378 ret_code
= lttng_create_session_ext(session_descriptor
);
379 if (ret_code
!= LTTNG_OK
) {
380 ERR("%s", lttng_strerror(-ret_code
));
385 descriptor_status
= lttng_session_descriptor_get_session_name(
386 session_descriptor
, &created_session_name
);
387 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
388 ERR("Failed to obtain created session name");
393 ret
= lttng_list_sessions(&sessions
);
395 ERR("Failed to fetch properties of created session: %s",
396 lttng_strerror(ret
));
400 for (i
= 0; i
< ret
; i
++) {
401 if (!strcmp(created_session_name
, sessions
[i
].name
)) {
402 created_session
= &sessions
[i
];
406 if (!created_session
) {
407 ERR("Failed to fetch properties of created session");
413 char datetime_suffix
[17] = {};
416 * An auto-generated session name already includes the creation
419 if (opt_session_name
) {
420 uint64_t creation_time
;
422 time_t creation_time_t
;
425 ret_code
= lttng_session_get_creation_time(
428 if (ret_code
!= LTTNG_OK
) {
429 ERR("%s", lttng_strerror(-ret_code
));
433 creation_time_t
= (time_t) creation_time
;
434 timeinfo
= localtime(&creation_time_t
);
436 PERROR("Failed to interpret session creation time");
440 strftime_ret
= strftime(datetime_suffix
,
441 sizeof(datetime_suffix
),
442 "-%Y%m%d-%H%M%S", timeinfo
);
443 if (strftime_ret
== 0) {
444 ERR("Failed to format session creation time.");
450 ret
= snprintf(shm_path
, sizeof(shm_path
),
451 "%s/%s%s", opt_shm_path
, created_session_name
,
453 if (ret
< 0 || ret
>= sizeof(shm_path
)) {
454 ERR("Failed to format the shared memory path.");
458 ret
= lttng_set_session_shm_path(created_session_name
,
461 lttng_destroy_session(created_session_name
);
468 MSG("Snapshot session %s created.", created_session_name
);
469 } else if (opt_live_timer
) {
470 MSG("Live session %s created.", created_session_name
);
472 MSG("Session %s created.", created_session_name
);
475 if (*created_session
->path
&& !opt_snapshot
) {
476 MSG("Traces will be output to %s", created_session
->path
);
478 if (opt_live_timer
) {
479 MSG("Live timer interval set to %u %s", opt_live_timer
,
482 } else if (opt_snapshot
) {
483 struct lttng_snapshot_output_list
*list
;
484 struct lttng_snapshot_output
*iter
;
485 char snapshot_url
[LTTNG_PATH_MAX
] = {};
487 ret
= lttng_snapshot_list_output(created_session_name
, &list
);
489 ERR("Failed to list snapshot outputs.");
494 while ((iter
= lttng_snapshot_output_list_get_next(list
))) {
495 const char *url
= NULL
;
497 url
= lttng_snapshot_output_get_ctrl_url(
499 ret
= lttng_strncpy(snapshot_url
, url
,
500 sizeof(snapshot_url
));
502 snapshot_url
[0] = '\0';
503 ERR("Failed to retrieve snapshot output destination");
507 lttng_snapshot_output_list_destroy(list
);
510 MSG("Default snapshot output set to %s",
513 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
516 MSG("Shared memory path set to %s", shm_path
);
521 ret
= mi_created_session(created_session_name
);
528 /* Init lttng session config */
529 ret
= config_init(created_session_name
);
537 lttng_session_descriptor_destroy(session_descriptor
);
545 * Spawn a session daemon by forking and execv.
547 static int spawn_sessiond(const char *pathname
)
552 MSG("Spawning a session daemon");
556 * Spawn session daemon in daemon mode.
558 execlp(pathname
, "lttng-sessiond",
559 "--daemonize", NULL
);
560 /* execlp only returns if error happened */
561 if (errno
== ENOENT
) {
562 ERR("No session daemon found. Use --sessiond-path.");
566 kill(getppid(), SIGTERM
); /* wake parent */
568 } else if (pid
> 0) {
570 * In daemon mode (--daemonize), sessiond only exits when
571 * it's ready to accept commands.
575 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
577 if (wait_pid_ret
< 0) {
578 if (errno
== EINTR
) {
586 if (WIFSIGNALED(status
)) {
587 ERR("Session daemon was killed by signal %d",
591 } else if (WIFEXITED(status
)) {
592 DBG("Session daemon terminated normally (exit status: %d)",
593 WEXITSTATUS(status
));
595 if (WEXITSTATUS(status
) != 0) {
596 ERR("Session daemon terminated with an error (exit status: %d)",
597 WEXITSTATUS(status
));
619 * Check if the session daemon is available using
620 * the liblttngctl API for the check. If not, try to
623 static int launch_sessiond(void)
626 const char *pathname
= NULL
;
628 ret
= lttng_session_daemon_alive();
630 /* Sessiond is alive, not an error */
635 /* Try command line option path */
636 pathname
= opt_sessiond_path
;
638 /* Try LTTNG_SESSIOND_PATH env variable */
639 if (pathname
== NULL
) {
640 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
643 /* Try with configured path */
644 if (pathname
== NULL
) {
645 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
646 pathname
= CONFIG_SESSIOND_BIN
;
650 /* Try the default path */
651 if (pathname
== NULL
) {
652 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
655 DBG("Session daemon binary path: %s", pathname
);
657 /* Check existence and permissions */
658 ret
= access(pathname
, F_OK
| X_OK
);
660 ERR("No such file or access denied: %s", pathname
);
664 ret
= spawn_sessiond(pathname
);
667 ERR("Problem occurred while launching session daemon (%s)",
674 int validate_url_option_combination(void)
679 used_count
+= !!opt_url
;
680 used_count
+= !!opt_output_path
;
681 used_count
+= (opt_data_url
|| opt_ctrl_url
);
682 if (used_count
> 1) {
683 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
691 * The 'create <options>' first level command
693 * Returns one of the CMD_* result constants.
695 int cmd_create(int argc
, const char **argv
)
697 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
698 char *opt_arg
= NULL
;
699 const char *leftover
= NULL
;
700 static poptContext pc
;
702 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
703 poptReadDefaultConfig(pc
, 0);
705 while ((opt
= poptGetNextOpt(pc
)) != -1) {
710 case OPT_LIST_OPTIONS
:
711 list_cmd_options(stdout
, long_options
);
723 opt_arg
= poptGetOptArg(pc
);
725 /* Set up default values. */
726 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
727 DBG("Session live timer interval set to default value %d",
732 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
733 ERR("Wrong value for --live parameter: %s", opt_arg
);
738 if (v
!= (uint32_t) v
) {
739 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
745 ERR("Live timer interval must be greater than zero");
750 opt_live_timer
= (uint32_t) v
;
751 DBG("Session live timer interval set to %d", opt_live_timer
);
760 if (opt_no_consumer
) {
761 MSG("The option --no-consumer is obsolete. Use --no-output now.");
766 ret
= validate_url_option_combination();
772 /* Spawn a session daemon if needed */
773 if (!opt_no_sessiond
) {
774 ret
= launch_sessiond();
781 /* MI initialization */
783 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
785 ret
= -LTTNG_ERR_NOMEM
;
789 /* Open command element */
790 ret
= mi_lttng_writer_command_open(writer
,
791 mi_lttng_element_command_create
);
797 /* Open output element */
798 ret
= mi_lttng_writer_open_element(writer
,
799 mi_lttng_element_command_output
);
805 opt_session_name
= (char*) poptGetArg(pc
);
807 leftover
= poptGetArg(pc
);
809 ERR("Unknown argument: %s", leftover
);
814 command_ret
= create_session();
820 /* Close output element */
821 ret
= mi_lttng_writer_close_element(writer
);
828 ret
= mi_lttng_writer_write_element_bool(writer
,
829 mi_lttng_element_command_success
, success
);
835 /* Command element close */
836 ret
= mi_lttng_writer_command_close(writer
);
845 if (writer
&& mi_lttng_writer_destroy(writer
)) {
846 /* Preserve original error code */
847 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
850 /* Overwrite ret if an error occurred in create_session() */
851 ret
= command_ret
? command_ret
: ret
;